SPI write

  1'''
  2SPI - SPI_write.py with asynchronous mode.
  3
  4This example demonstrates how to communicate with USBDAQF1D (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).
  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 asyncio
 22
 23## WPC
 24
 25from wpcsys import pywpc
 26
 27async def main():
 28    ## Get Python driver version
 29    print(f'{pywpc.PKG_FULL_NAME} - Version {pywpc.__version__}')
 30
 31    ## Create device handle
 32    dev = pywpc.USBDAQF1D()
 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        '''
 45        Take 25LC640 for example
 46        '''
 47
 48        ## Parameters setting
 49        port = 2 ## Depend on your device
 50        datasize = 0  ## Mode: 0 = 8-bit data, 1 = 16-bit data.
 51        first_bit = 0 ## Mode: 0 = MSB first, 1 = LSB first.
 52        prescaler = 64
 53        mode = 0    ## 0 : CPOL = 0 CPHA = 0 ## 1 : CPOL = 0 CPHA = 1
 54                    ## 2 : CPOL = 1 CPHA = 0 ## 3 : CPOL = 1 CPHA = 1
 55
 56        if port == 1:
 57            DO_port = 2
 58            DO_index = [0] ## CS pin
 59
 60        elif port == 2:
 61            DO_port = 3
 62            DO_index = [2] ## CS pin
 63
 64        WRITE = 0x02
 65        WREN = 0x06
 66
 67        ## Get firmware model & version
 68        driver_info = await dev.Sys_getDriverInfo_async()
 69        print("Model name: " + driver_info[0])
 70        print("Firmware version: " + driver_info[-1])
 71
 72        '''
 73        Open DO pins & SPI port & set CS(pin0) to high
 74        '''
 75
 76        ## Open pin0 with digital output
 77        err = await dev.DO_openPins_async(DO_port, DO_index)
 78        print(f"DO_openPins_async in port {DO_port}, status: {err}")
 79
 80        ## Open SPI
 81        err = await dev.SPI_open_async(port)
 82        print(f"SPI_open_async in port {port}, status: {err}")
 83
 84        ## Set CS(pin0) to high
 85        err = await dev.DO_writePins_async(DO_port, DO_index, [1])
 86        print(f"DO_writePins_async in port {DO_port}, status: {err}")
 87
 88        '''
 89        Set SPI parameter
 90        '''
 91
 92        ## Set SPI port and set datasize to 8-bits data
 93        err = await dev.SPI_setDataSize_async(port, datasize)
 94        print(f"SPI_setDataSize_async in port {port}, status: {err}")
 95
 96        ## Set SPI port and set first_bit to MSB first
 97        err = await dev.SPI_setFirstBit_async(port, first_bit)
 98        print(f"SPI_setFirstBit_async in port {port}, status: {err}")
 99
100        ## Set SPI port and set prescaler to 64
101        err = await dev.SPI_setPrescaler_async(port, prescaler)
102        print(f"SPI_setPrescaler_async in port {port}, status: {err}")
103
104        ## Set SPI port and set CPOL and CPHA to 0 (mode 0)
105        err = await dev.SPI_setMode_async(port, mode)
106        print(f"SPI_setMode_async in port {port}, status: {err}")
107
108        '''
109        Write data via SPI
110        '''
111
112        ## Set CS(pin0) to low
113        err = await dev.DO_writePins_async(DO_port, DO_index, [0])
114        print(f"DO_writePins_async in port {DO_port}, status: {err}")
115
116        ## Write WREN byte
117        err = await dev.SPI_write_async(port, [WREN])
118        print(f"SPI_write_async in port {port}, status: {err}")
119
120        ## Set CS(pin0) to high
121        err = await dev.DO_writePins_async(DO_port, DO_index, [1])
122        print(f"DO_writePins_async in port {DO_port}, status: {err}")
123
124        '''
125        Write data via SPI
126        '''
127
128        ## Set CS(pin0) to low
129        err = await dev.DO_writePins_async(DO_port, DO_index, [0])
130        print(f"DO_writePins_async in port {DO_port}, status: {err}")
131
132        ## Write data byte 0x55 in to address 0x0002
133        err = await dev.SPI_write_async(port, [WRITE, 0x00, 0x02, 0x55])
134        print(f"SPI_write_async in port {port}, status: {err}")
135
136        ## Set CS(pin0) to high
137        err = await dev.DO_writePins_async(DO_port, DO_index, [1])
138        print(f"DO_writePins_async in port {DO_port}, status: {err}")
139
140        '''
141        Close DO pins and SPI port
142        '''
143
144        ## Close SPI
145        err = await dev.SPI_close_async(port)
146        print(f"SPI_close_async in port {port}, status: {err}")
147
148        ## Close pin0 with digital output
149        err = await dev.DO_closePins_async(DO_port, DO_index)
150        print(f"DO_closePins_async in port {DO_port}, status: {err}")
151    except Exception as err:
152        pywpc.printGenericError(err)
153
154    ## Disconnect device
155    dev.disconnect()
156
157    ## Release device handle
158    dev.close()
159
160    return
161
162def main_for_spyder(*args):
163    if asyncio.get_event_loop().is_running():
164        return asyncio.create_task(main(*args)).result()
165    else:
166        return asyncio.run(main(*args))
167
168if __name__ == '__main__':
169    asyncio.run(main()) ## Use terminal
170    # await main() ## Use Jupyter or IPython(>=7.0)
171    # main_for_spyder() ## Use Spyder