SPI read and write

  1'''
  2SPI - SPI_read_and_write.py with synchronous mode.
  3
  4This example demonstrates how to communicate with USBDAQF1AD (master) and 25LC640 (slave) with SPI interface.
  5
  6First, it shows how to open SPI port & DIO pins and configure SPI parameters.
  7Second, write some bytes with address into EEPROM (25LC640). We have to make sure that bytes written in address is correct however read address from EEPROM (25LC640).
  8Last, close SPI port & DIO pins.
  9
 10-------------------------------------------------------------------------------------
 11Please change correct serial number or IP and port number BEFORE you run example code.
 12
 13For other examples please check:
 14    https://github.com/WPC-Systems-Ltd/WPC_Python_driver_release/tree/main/examples
 15See README.md file to get detailed usage of this example.
 16
 17Copyright (c) 2022-2024 WPC Systems Ltd. All rights reserved.
 18'''
 19
 20## Python
 21import time
 22
 23## WPC
 24
 25from wpcsys import pywpc
 26
 27def main():
 28    ## Get Python driver version
 29    print(f'{pywpc.PKG_FULL_NAME} - Version {pywpc.__version__}')
 30
 31    ## Create device handle
 32    dev = pywpc.USBDAQF1AD()
 33
 34    ## Connect to device
 35    try:
 36        dev.connect("default") ## Depend on your device
 37    except Exception as err:
 38        pywpc.printGenericError(err)
 39        ## Release device handle
 40        dev.close()
 41        return
 42
 43    try:
 44        ## Parameters setting
 45        port = 2 ## Depend on your device
 46        datasize = 0  ## Mode: 0 = 8-bit data, 1 = 16-bit data.
 47        first_bit = 0 ## Mode: 0 = MSB first, 1 = LSB first.
 48        prescaler = 64
 49        mode = 0    ## 0 : CPOL = 0 CPHA = 0 ## 1 : CPOL = 0 CPHA = 1
 50                    ## 2 : CPOL = 1 CPHA = 0 ## 3 : CPOL = 1 CPHA = 1
 51
 52        if port == 1:
 53            DO_port = 2
 54            DO_index = [0] ## CS pin
 55
 56        elif port == 2:
 57            DO_port = 3
 58            DO_index = [2] ## CS pin
 59
 60        timeout = 3 ## second
 61
 62        ## Generate random data
 63        import numpy as np
 64        addr = np.random.randint(8) ## Generate a random address
 65        value = np.random.randint(256) ## Generate a random value
 66
 67        WRITE = 0x02
 68        DUMMY = 0x01
 69        READ = 0x03
 70        WREN = 0x06
 71
 72        '''
 73        Take 25LC640 for example
 74        '''
 75
 76        ## Get firmware model & version
 77        driver_info = dev.Sys_getDriverInfo(timeout)
 78        print("Model name: " + driver_info[0])
 79        print("Firmware version: " + driver_info[-1])
 80
 81        '''
 82        Open DO pins & SPI port & set CS(pin0) to high
 83        '''
 84
 85        ## Open pin0 with digital output
 86        err = dev.DO_openPins(DO_port, DO_index, timeout)
 87        print(f"DO_openPins in port {DO_port}, status: {err}")
 88
 89        ## Open SPI
 90        err = dev.SPI_open(port)
 91        print(f"SPI_open in port {port}, status: {err}")
 92
 93        ## Set CS(pin0) to high
 94        err = dev.DO_writePins(DO_port, DO_index, [1], timeout)
 95        print(f"DO_writePins in port {DO_port}, status: {err}")
 96
 97        '''
 98        Set SPI parameter
 99        '''
100
101        ## Set SPI port and set datasize to 8-bits data
102        err = dev.SPI_setDataSize(port, datasize, timeout)
103        print(f"SPI_setDataSize in port {port}, status: {err}")
104
105        ## Set SPI port and set first_bit to MSB first
106        err = dev.SPI_setFirstBit(port, first_bit, timeout)
107        print(f"SPI_setFirstBit in port {port}, status: {err}")
108
109        ## Set SPI port and set prescaler to 64
110        err = dev.SPI_setPrescaler(port, prescaler, timeout)
111        print(f"SPI_setPrescaler in port {port}, status: {err}")
112
113        ## Set SPI port and set CPOL and CPHA to 0 (mode 0)
114        err = dev.SPI_setMode(port, mode, timeout)
115        print(f"SPI_setMode in port {port}, status: {err}")
116
117        '''
118        Write data via SPI
119        '''
120
121        ## Set CS(pin0) to low
122        err = dev.DO_writePins(DO_port, DO_index, [0], timeout)
123        print(f"DO_writePins in port {DO_port}, status: {err}")
124        time.sleep(0.01) ## delay [s]
125
126        ## Write WREN byte
127        err = dev.SPI_write(port, [WREN], timeout)
128        print(f"SPI_write in port {port}, status: {err}")
129        time.sleep(0.01) ## delay [s]
130
131        ## Set CS(pin0) to high
132        err = dev.DO_writePins(DO_port, DO_index, [1], timeout)
133        print(f"DO_writePins in port {DO_port}, status: {err}")
134        time.sleep(0.05) ## delay [s]
135
136        '''
137        Write data via SPI
138        '''
139
140        ## Set CS(pin0) to low
141        err = dev.DO_writePins(DO_port, DO_index, [0], timeout)
142        print(f"DO_writePins in port {DO_port}, status: {err}")
143        time.sleep(0.01) ## delay [s]
144
145        ## Write the generated byte into the generated address
146        err = dev.SPI_write(port, [WRITE, 0x00, addr, value], timeout)
147        print(f"SPI_write in port {port}, status: {err}")
148        print(f"Write data: 0x{value:02X}")
149        time.sleep(0.01) ## delay [s]
150
151        ## Set CS(pin0) to high
152        err = dev.DO_writePins(DO_port, DO_index, [1], timeout)
153        print(f"DO_writePins in port {DO_port}, status: {err}")
154        time.sleep(0.05) ## delay [s]
155
156        '''
157        Read data via SPI
158        '''
159
160        ## Set CS(pin0) to low
161        err = dev.DO_writePins(DO_port, DO_index, [0], timeout)
162        print(f"DO_writePins in port {DO_port}, status: {err}")
163        time.sleep(0.01) ## delay [s]
164
165        ## Read the written byte from the generated address
166        data = dev.SPI_readAndWrite(port, [READ, 0x00, addr, DUMMY], timeout)
167        data = ['{:02x}'.format(value) for value in data]
168        print("read data :", data)
169        time.sleep(0.01) ## delay [s]
170
171        ## Set CS(pin0) to high
172        err = dev.DO_writePins(DO_port, DO_index, [1], timeout)
173        print(f"DO_writePins in port {DO_port}, status: {err}")
174
175        '''
176        Close DO pins and SPI port
177        '''
178
179        ## Close SPI
180        err = dev.SPI_close(port, timeout)
181        print(f"SPI_close in port {port}, status: {err}")
182
183        ## Close pin0 with digital output
184        err = dev.DO_closePins(DO_port, DO_index, timeout)
185        print(f"DO_closePins in port {DO_port}, status: {err}")
186    except Exception as err:
187        pywpc.printGenericError(err)
188
189    ## Disconnect device
190    dev.disconnect()
191
192    ## Release device handle
193    dev.close()
194
195    return
196
197if __name__ == '__main__':
198    main()