SPI write

  1'''
  2SPI - SPI_write.py with asynchronous mode.
  3
  4This example demonstrates how to communicate with USBDAQF1TD (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-2025 WPC Systems Ltd. All rights reserved.
 18'''
 19
 20## WPC
 21from wpcsys import pywpc
 22
 23## Python
 24import asyncio
 25import sys
 26sys.path.insert(0, 'src/')
 27
 28
 29async def main():
 30    ## Get Python driver version
 31    print(f'{pywpc.PKG_FULL_NAME} - Version {pywpc.__version__}')
 32
 33    ## Create device handle
 34    dev = pywpc.USBDAQF1TD()
 35
 36    ## Connect to device
 37    try:
 38        dev.connect("default")  ## Depend on your device
 39    except Exception as err:
 40        pywpc.printGenericError(err)
 41        ## Release device handle
 42        dev.close()
 43        return
 44
 45    try:
 46        '''
 47        Take 25LC640 for example
 48        '''
 49
 50        ## Parameters setting
 51        port = 2  ## Depend on your device
 52        datasize = 0  ## Mode: 0 = 8-bit data, 1 = 16-bit data.
 53        first_bit = 0  ## Mode: 0 = MSB first, 1 = LSB first.
 54        prescaler = 64
 55        mode = 0  ## 0 : CPOL = 0 CPHA = 0, 1 : CPOL = 0 CPHA = 1, 2 : CPOL = 1 CPHA = 0, 3 : CPOL = 1 CPHA = 1
 56
 57        if port == 1:
 58            DO_port = 2
 59            DO_index = [0]  ## CS pin
 60
 61        elif port == 2:
 62            DO_port = 3
 63            DO_index = [2]  ## CS pin
 64
 65        WRITE = 0x02
 66        WREN = 0x06
 67
 68        ## Get firmware model & version
 69        driver_info = await dev.Sys_getDriverInfo_async()
 70        print(f"Model name: {driver_info[0]}, 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    finally:
155        ## Disconnect device
156        dev.disconnect()
157
158        ## Release device handle
159        dev.close()
160
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
168
169if __name__ == '__main__':
170    asyncio.run(main())  ## Use terminal
171    # await main()  ## Use Jupyter or IPython(>=7.0)
172    # main_for_spyder()  ## Use Spyder