1'''
2SPI - SPI_write.py with synchronous 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-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.USBDAQF1TD()
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 WRITE = 0x02
63 WREN = 0x06
64
65 '''
66 Take 25LC640 for example
67 '''
68
69 ## Get firmware model & version
70 driver_info = dev.Sys_getDriverInfo(timeout)
71 print("Model name: " + driver_info[0])
72 print("Firmware version: " + driver_info[-1])
73
74 '''
75 Open DO pins & SPI port & set CS(pin0) to high
76 '''
77
78 ## Open pin0 with digital output
79 err = dev.DO_openPins(DO_port, DO_index, timeout)
80 print(f"DO_openPins in port {DO_port}, status: {err}")
81
82 ## Open SPI
83 err = dev.SPI_open(port, timeout)
84 print(f"SPI_open in port {port}, status: {err}")
85
86 ## Set CS(pin0) to high
87 err = dev.DO_writePins(DO_port, DO_index, [1], timeout)
88 print(f"DO_writePins in port {DO_port}, status: {err}")
89
90 '''
91 Set SPI parameter
92 '''
93
94 ## Set SPI port and set datasize to 8-bits data
95 err = dev.SPI_setDataSize(port, datasize, timeout)
96 print(f"SPI_setDataSize in port {port}, status: {err}")
97
98 ## Set SPI port and set first_bit to MSB first
99 err = dev.SPI_setFirstBit(port, first_bit, timeout)
100 print(f"SPI_setFirstBit in port {port}, status: {err}")
101
102 ## Set SPI port and set prescaler to 64
103 err = dev.SPI_setPrescaler(port, prescaler, timeout)
104 print(f"SPI_setPrescaler in port {port}, status: {err}")
105
106 ## Set SPI port and set CPOL and CPHA to 0 (mode 0)
107 err = dev.SPI_setMode(port, mode, timeout)
108 print(f"SPI_setMode in port {port}, status: {err}")
109
110 '''
111 Write data via SPI
112 '''
113
114 ## Set CS(pin0) to low
115 err = dev.DO_writePins(DO_port, DO_index, [0], timeout)
116 print(f"DO_writePins in port {DO_port}, status: {err}")
117 time.sleep(0.01) ## delay [s]
118
119 ## Write WREN byte
120 err = dev.SPI_write(port, [WREN], timeout)
121 print(f"SPI_write in port {port}, status: {err}")
122 time.sleep(0.01) ## delay [s]
123
124 ## Set CS(pin0) to high
125 err = dev.DO_writePins(DO_port, DO_index, [1], timeout)
126 print(f"DO_writePins in port {DO_port}, status: {err}")
127 time.sleep(0.05) ## delay [s]
128
129 '''
130 Write data via SPI
131 '''
132
133 ## Set CS(pin0) to low
134 err = dev.DO_writePins(DO_port, DO_index, [0], timeout)
135 print(f"DO_writePins in port {DO_port}, status: {err}")
136 time.sleep(0.01) ## delay [s]
137
138 ## Write data byte 0x55 in to address 0x0002
139 err = dev.SPI_write(port, [WRITE, 0x00, 0x02, 0x55], timeout)
140 print(f"SPI_write in port {port}, status: {err}")
141 time.sleep(0.01) ## delay [s]
142
143 ## Set CS(pin0) to high
144 err = dev.DO_writePins(DO_port, DO_index, [1], timeout)
145 print(f"DO_writePins in port {DO_port}, status: {err}")
146
147 '''
148 Close DO pins and SPI port
149 '''
150
151 ## Close SPI
152 err = dev.SPI_close(port)
153 print(f"SPI_close in port {port}, status: {err}")
154
155 ## Close pin0 with digital output
156 err = dev.DO_closePins(DO_port, DO_index)
157 print(f"DO_closePins in port {DO_port}, status: {err}")
158 except Exception as err:
159 pywpc.printGenericError(err)
160
161 ## Disconnect device
162 dev.disconnect()
163
164 ## Release device handle
165 dev.close()
166
167 return
168
169if __name__ == '__main__':
170 main()