-
Notifications
You must be signed in to change notification settings - Fork 0
/
myI2C.py
269 lines (232 loc) · 12.7 KB
/
myI2C.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
import ctypes
from time import sleep
class ChannelConfig(ctypes.Structure):
_fields_ = [("ClockRate", ctypes.c_int),
("LatencyTimer",ctypes.c_ubyte),
("Options", ctypes.c_int)]
class I2C_TRANSFER_OPTION(object):
START_BIT = 0x01
STOP_BIT = 0x02
BREAK_ON_NACK = 0x04
NACK_LAST_BYTE = 0x08
FAST_TRANSFER_BYTES = 0x10
FAST_TRANSFER_BITS = 0x20
FAST_TRANSFER = 0x30
NO_ADDRESS = 0x40
class I2cFTDI():
"""
allows to use FTDI devices and evaluation boards such as the FT4232H as master devices over the I²C protocol to communicate with slave devices
"""
libMPSSE = ctypes.cdll.LoadLibrary("libMPSSE.dll") # use of the MPSSE library from FTDI which allows synchronous protocols on FTDI devices
chn_count = ctypes.c_int()
def __init__(self, chn_no = 0, clockrate = 100000):
self.chn_conf = ChannelConfig(clockrate, 5, 0) # clockrate à confirmer # commenter le LatencyTimer = 5 et Options = 0
self.chn_no = chn_no # channels 0 ( = A ) et 1 ( = B ) configurables en MPSSE (dont i2C) grâce à FT_Prog
self.handle = ctypes.c_void_p()
self.bytes_transfered = ctypes.c_int()
self.options = I2C_TRANSFER_OPTION() ## S = Start, P = stoP
self.modeS = I2C_TRANSFER_OPTION.START_BIT # START-bit only, used for register reading purposes
self.modeSP = I2C_TRANSFER_OPTION.START_BIT|I2C_TRANSFER_OPTION.STOP_BIT # START and STOP-bits
self.modeP = I2C_TRANSFER_OPTION.STOP_BIT # STOP-bit
self.retO = -1 # return value for opening functions
self.retI = -1 # return value for initialization functions
self.retW = -1 # return value for writing functions
self.retR = -1 # return value for reading functions
self.retC = -1 # return value for closing functions
@classmethod
def get_nb_channels(cls):
ret = cls.libMPSSE.I2C_GetNumChannels(ctypes.byref(cls.chn_count))
return(ret, cls.chn_count)
def open_channel(self):
self.retO = I2cFTDI.libMPSSE.I2C_OpenChannel(self.chn_no, ctypes.byref(self.handle))
self.retI = I2cFTDI.libMPSSE.I2C_InitChannel(self.handle, ctypes.byref(self.chn_conf))
return(self.retO == 0 and self.retI == 0) # ret value of 0 means no error encountered
def read_register(self, address, reg):
bufT_len = 1
bufT = ctypes.create_string_buffer(bufT_len)
bufR_len = 1
bufR = ctypes.create_string_buffer(bufR_len)
bufT[0] = reg
self.retW = I2cFTDI.libMPSSE.I2C_DeviceWrite(self.handle, address, bufT_len, bufT, ctypes.byref(self.bytes_transfered), self.modeS)
self.retR = I2cFTDI.libMPSSE.I2C_DeviceRead(self.handle, address, bufR_len, bufR, ctypes.byref(self.bytes_transfered), self.modeSP) # repeated start sans stop avant, pour garder la main sur le bus
return(self.retW == 0 and self.retR == 0, bufR[0]) # ret value of 0 means no error encountered
def write_register(self, address, reg, value):
bufT_len = 2
bufT = ctypes.create_string_buffer(bufT_len)
bufT[0] = reg
bufT[1] = value
self.retW = I2cFTDI.libMPSSE.I2C_DeviceWrite(self.handle, address, bufT_len, bufT, ctypes.byref(self.bytes_transfered), self.modeSP)
print(self.retW)
return(self.retW == 0) # ret value of 0 means no error encountered
def write_verify_register(self, address, reg, value):
"""
return : 0 read value = written value
1 read value != written value
2 reading unsuccessful
3 writing unsuccessful
"""
if self.write_register(address, reg, value):
ret, reg_val = self.read_register(address, reg)
if not ret == 1 :
return 2
else :
return(not reg_val == value)
else :
return 3
def send_command(self, address, cmd, lenR):
bufT_len = len(cmd)
bufR_len = 1
bufT = ctypes.create_string_buffer(bufT_len)
bufR = ctypes.create_string_buffer(bufR_len)
bufOut = bytearray()
for i in range(bufT_len):
bufT[i] = cmd[i]
self.retW = I2cFTDI.libMPSSE.I2C_DeviceWrite(self.handle, address, bufT_len, bufT, ctypes.byref(self.bytes_transfered), self.modeSP)
for i in range(lenR-1):
self.retR = I2cFTDI.libMPSSE.I2C_DeviceRead(self.handle, address, bufR_len, bufR, ctypes.byref(self.bytes_transfered), self.modeS) # repeated start sans stop avant, pour garder la main sur le bus
bufOut.append(int.from_bytes(bufR[0], byteorder='big', signed=False))
if not self.retR == 0:
return(False,bufOut)
self.retR = I2cFTDI.libMPSSE.I2C_DeviceRead(self.handle, address, bufR_len, bufR, ctypes.byref(self.bytes_transfered), self.modeSP) # repeated start sans stop avant, pour garder la main sur le bus
print(bufR[0])
bufOut.append(int.from_bytes(bufR[0], byteorder='big', signed=False))
print(bufOut)
return(self.retW == 0 and self.retR == 0, bufOut)
def write_two_bytes(self, address = 0b1010101, registers = [0x00, 0x01], bytes = [0x7c, 0x40]):
bufT_len = 3
#bufR_len = 1
bufT = ctypes.create_string_buffer(bufT_len)
#bufR = ctypes.create_string_buffer(bufR_len)
bufT[0] = 0x00
bufT[1] = 0x40
bufT[2] = 0x7C
self.retW = I2cFTDI.libMPSSE.I2C_DeviceWrite(self.handle, address, 2, bufT, ctypes.byref(self.bytes_transfered), self.modeSP)
print(self.retW)
sleep(2)
return
def close_channel(self):
self.retC = I2cFTDI.libMPSSE.I2C_CloseChannel(self.handle)
return(self.retC == 0)
def test1(self):
while(True):
bufT_len = 1
bufT = ctypes.create_string_buffer(3)
bufR_len = 1
bufR = ctypes.create_string_buffer(bufR_len)
bufT[0] = 0x00
bufT[1] = 0x7C
bufT[2] = 0x40
I2Caddress = 0b1010101 #0b1100000 #0b1100010 #0b1010101
ret = I2cFTDI.libMPSSE.I2C_DeviceWrite(self.handle, I2Caddress, bufT_len, bufT, ctypes.byref(self.bytes_transfered), self.modeS)
print("I2C_DeviceWrite status:",ret, "transfered:",self.bytes_transfered)
ret = I2cFTDI.libMPSSE.I2C_DeviceRead(self.handle, I2Caddress, bufR_len, bufR, ctypes.byref(self.bytes_transfered), self.modeSP) # repeated start sans stop avant, pour garder la main sur le bus
print("I2C_DeviceRead status:",ret, "received:", bufR, self.bytes_transfered)
for i in range(bufR_len):
print(bufR[i])
bufT_len = 3
ret= I2cFTDI.libMPSSE.I2C_DeviceWrite(self.handle, I2Caddress, bufT_len, bufT, ctypes.byref(self.bytes_transfered), self.modeSP)
print("I2C_DeviceWrite status:",ret, "transfered:",self.bytes_transfered)
bufT_len = 1
ret = I2cFTDI.libMPSSE.I2C_DeviceWrite(self.handle, I2Caddress, bufT_len, bufT, ctypes.byref(self.bytes_transfered), self.modeS)
print("I2C_DeviceWrite status:",ret, "transfered:",self.bytes_transfered)
ret = I2cFTDI.libMPSSE.I2C_DeviceRead(self.handle, I2Caddress, bufR_len, bufR, ctypes.byref(self.bytes_transfered), self.modeSP) # repeated start sans stop avant, pour garder la main sur le bus
print("I2C_DeviceRead status:",ret, "received:", bufR, self.bytes_transfered)
for i in range(bufR_len):
print(bufR[i])
sleep(1)
def test2(self):
I2Caddress = 0b1100000 #0b1100010 #0b1010101
print(self.read_register(I2Caddress, 0x26)[1])
print(self.write_verify_register(I2Caddress, 0x26, 0xB8))
def test3(self):
I2Caddress = 0b1010101
bufT_len = 1
bufT = ctypes.create_string_buffer(2)
bufR_len = 2
bufR = ctypes.create_string_buffer(bufR_len)
bufT[0] = 0x0
bufT[1] = 0xB8
ret = I2cFTDI.libMPSSE.I2C_DeviceWrite(self.handle, I2Caddress, bufT_len, bufT, ctypes.byref(self.bytes_transfered), self.modeS)
print("I2C_DeviceWrite status:",ret, "transfered:",self.bytes_transfered)
ret = I2cFTDI.libMPSSE.I2C_DeviceRead(self.handle, I2Caddress, bufR_len, bufR, ctypes.byref(self.bytes_transfered), self.modeSP) # repeated start sans stop avant, pour garder la main sur le bus
print("I2C_DeviceRead status:",ret, "received:", bufR, self.bytes_transfered)
for i in range(bufR_len):
print(bufR[i])
def test4(self):
I2Caddress = 0b1010101
bufT_len = 2
bufR_len = 1
bufT = ctypes.create_string_buffer(bufT_len)
bufR = ctypes.create_string_buffer(bufR_len)
bufT[0] = 0x10
bufT[1] = 0x11
ret = I2cFTDI.libMPSSE.I2C_DeviceWrite(self.handle, I2Caddress, bufT_len, bufT, ctypes.byref(self.bytes_transfered), self.modeSP)
print("I2C_DeviceWrite status:",ret, "transfered:",self.bytes_transfered)
"""
bufT[0] = 0x11
ret = I2cFTDI.libMPSSE.I2C_DeviceWrite(self.handle, I2Caddress, 1, bufT, ctypes.byref(self.bytes_transfered), self.modeSP)
print("I2C_DeviceWrite status:",ret, "transfered:",self.bytes_transfered)
"""
ret = I2cFTDI.libMPSSE.I2C_DeviceRead(self.handle, I2Caddress, bufR_len, bufR, ctypes.byref(self.bytes_transfered), self.modeS) # repeated start sans stop avant, pour garder la main sur le bus
print("I2C_DeviceRead status:",ret, "received:", bufR, self.bytes_transfered)
for i in range(bufR_len):
print(bufR[i])
ret = I2cFTDI.libMPSSE.I2C_DeviceRead(self.handle, I2Caddress, 2, bufR, ctypes.byref(self.bytes_transfered), self.modeSP) # repeated start sans stop avant, pour garder la main sur le bus
print("I2C_DeviceRead status:",ret, "received:", bufR, self.bytes_transfered)
for i in range(bufR_len):
print(bufR[i])
#testé : écriture de 2 SP puid lecture de 2 SP : LSByte reçu, MSByte :: FF
# à tester : écriture de 1 SP puis lecture de 1 SP, puis encore
# écriture de 2 SP et deux lectures de 1 SP (ou S puis SP)
def test5(self):
I2Caddress = 0b1010101
cmd = [0x08, 0x09]
out = self.send_command(I2Caddress, cmd, 2)[1]
print(out)
outInt = I2cDevice.bytes_to_dec(out, 'little', True)
print(outInt)
def __repr__(self):
return("self.chn_conf :{}, self.chn_no :{}, self.handle :{}, self.bytes_transfered :{}, self.options :{}, self.modeS :{}, self.modeSP :{}, self.modeP :{}, self.retO :{}, self.retI :{}, self.retW :{}, self.retR :{}, self.retC :{}".format(
self.chn_conf, self.chn_no, self.handle, self.bytes_transfered, self.options, self.modeS, self.modeSP,
self.modeP, self.retO, self.retI, self.retW, self.retR, self.retC))
class I2cDevice():
def __init__(self, address, channel = 0, clockrate = 100000):
self.master = I2cFTDI(channel, clockrate)
self.address = address
def open_channel(self):
return(self.master.open_channel())
def read_register(self, reg):
return(self.master.read_register(self.address, reg))
def write_register(self, reg, value):
return(self.master.write_register(self.address, reg, value))
def write_verify_register(self, reg, value):
return(self.master.write_verify_register(self.address, reg, value))
def send_command(self, cmd, lenR):
return(self.master.send_command(self.address, cmd, lenR))
def close_channel(self):
return(self.master.close_channel())
@staticmethod
def bytes_to_dec(bytes, byteorder = 'big', signed = False):
return(int.from_bytes(bytes, byteorder = byteorder, signed = signed))
def __repr__(self):
return("master :{}, address :{}".format(self.master, self.address))
if __name__ == "__main__":
"""
baro = I2cDevice(0b1100000) # 0b1100000 address of the MPL3115A2 barometer
print(baro.open_channel())
print(baro.write_verify_register(0x26, 0xB8))
"""
ftdi = I2cFTDI()
ftdi.open_channel()
ftdi.write_two_bytes()
"""
read register :
while(True):
ret = libMPSSE.I2C_DeviceWrite(handle, I2Caddress, bufT_len, bufT, ctypes.byref(bytes_transfered), modeS)
print("I2C_DeviceWrite status:",ret, "transfered:",bytes_transfered)
ret = libMPSSE.I2C_DeviceRead(handle, I2Caddress, bufR_len, bufR, ctypes.byref(bytes_transfered), modeSP) # repeated start sans stop avant, pour garder la main sur le bus
print("I2C_DeviceRead status:",ret, "received:", bufR, bytes_transfered)
for i in range(bufR_len):
print(buf[i])
sleep(1)
"""