-
Notifications
You must be signed in to change notification settings - Fork 7
/
bb_I2C.py
288 lines (198 loc) · 4.19 KB
/
bb_I2C.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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
#!/usr/bin/env python
import time
import pigpio
# v0 SCL, v1 SDA, v2 dly, v3 ack, v4 tx_byte
def bb_I2C_script():
return (
" dcr p0"
" jp 100"
# command 0 (init)
" ld v0 p1" # save SCL
" ld v1 p2" # save SDA
" ld v2 p3" # save dly
" halt"
" tag 100"
" dcr p0"
" jp 200"
# command 1 (start sequence)
" call 1000"
" halt"
" tag 200"
" dcr p0"
" jp 300"
# command 2 (stop sequence)
" call 2000"
" halt"
" tag 300"
" dcr p0"
" jp 400"
# command 3 (receive byte)
" ld v3 p1" # store Ack
" call 3000"
" halt"
" tag 400"
" dcr p0"
" jp 500"
# command 4 (transmit byte)
" ld v4 p1" # tx_byte
" call 4000"
" halt"
" tag 500"
" halt"
# Start sequence.
# v0 SCL, v1 SDA, v2 dly
" tag 1000"
" m v1 r" # SDA=1 (pull-up)
" mics v2"
" m v0 r" # SCL=1 (pull-up)
" mics v2"
" w v1 0 " # SDA=0
" mics v2"
" w v0 0" # SCL=0
" mics v2"
" ret"
# Stop sequence.
# v0 SCL, v1 SDA, v2 dly
" tag 2000"
" w v1 0" # SDA=0
" mics v2"
" m v0 r" # SCL=1 (pull-up)
" mics v2"
" m v1 r" # SDA=1 (pull-up)
" mics v2"
" ret"
# Receive byte.
# v0 SCL, v1 SDA, v2 dly, v3 Ack
" tag 3000"
" ld v140 0" # v140=0
" m v1 r" # SDA=1 (pull-up)
" ld v141 8" # v141=8
" tag 3100"
" rl v140 1" # v140 = v140 * 2
" m v0 r" # SCL=1 (pull-up)
" mics v2"
" r v1" # A=SDA
" or v140" # A|=v140
" sta v140" # v140=A
" w v0 0" # SCL=0
" mics v2"
" dcr v141" # --v141
" jnz 3100" # loop x 8
" lda v3" # A=ack
" dcra" # ack 1?
" jp 3200"
" m v1 r" # SDA=1 (pull-up)
" jmp 3300"
" tag 3200"
" w v1 0" # SDA=0
" tag 3300"
" mics v2"
" m v0 r" # SCL=1 (pull-up)
" mics v2"
" w v0 0" # SCL=0
" m v1 r" # SDA=1 (pull-up)
" lda v140" # A=v140
" sta p9" # p9=A
" ret"
# Transmit byte.
# v0 SCL, v1 SDA, v2 dly, v4 tx_byte
" tag 4000"
" ld v140 128" # v140=128
" ld v141 8" # v141=8
" tag 4100"
" lda v4" # A=tx_byte
" and v140" # A&=v140
" jz 4200"
" m v1 r" # SDA=1 (pull-up)
" jmp 4300"
" tag 4200"
" w v1 0" # SDA=0
" tag 4300"
" mics v2"
" m v0 r" # SCL=1 (pull-up)
" rr v140 1" # v140 >> 1
" mics v2"
" w v0 0" # SCL=0
" dcr v141" # --v141
" jnz 4100"
" m v1 r" # SDA=1 (pull-up)
" mics v2"
" m v0 r" # SCL=1 (pull-up)
" mics v2"
" r v1" # get ack
" sta p9" # p9=ack
" w v0 0" # SCL=0
" ret"
)
class I2C:
"""
"""
def __init__(self, pi, SCL, SDA, baud):
"""
Instantiate.
"""
self.pi = pi
self.SCL = SCL
self.SDA = SDA
self.p=[0]*10
self.baud = baud
self.dly = 500000/baud
pi.set_mode(SCL, pigpio.OUTPUT)
pi.set_mode(SDA, pigpio.OUTPUT)
self.sid = pi.store_script(bb_I2C_script())
if self.sid >= 0:
time.sleep(1.0) # give time for script to start
pi.run_script(self.sid, [0, SCL, SDA, self.dly])
def wait_for_script(self, sid, delay):
time.sleep(delay/1000000.0)
while True:
s, self.p = self.pi.script_status(sid)
if s != pigpio.PI_SCRIPT_RUNNING:
break
time.sleep(0.001)
def S(self):
self.pi.run_script(self.sid, [1])
self.wait_for_script(self.sid, self.dly * 4)
def E(self):
self.pi.run_script(self.sid, [2])
self.wait_for_script(self.sid, self.dly * 4)
def RX(self, ack):
self.pi.run_script(self.sid, [3, ack])
self.wait_for_script(self.sid, self.dly * 8)
return self.p[9]
def TX(self, byte):
self.pi.run_script(self.sid, [4, byte])
self.wait_for_script(self.sid, self.dly * 8)
return self.p[9]
def cancel(self):
"""Cancel I2C"""
self.pi.delete_script(self.sid)
if __name__ == "__main__":
import time
import pigpio
import bb_I2C
pigpio.exceptions = False
pi = pigpio.pi()
# instantiate with SCL, SDA, baud rate
s = bb_I2C.I2C(pi, 1, 0, 600)
# The following code assumes a SRF02 sonar ranger is attached.
for x in xrange(200):
s.S()
s.TX(0xE0) # 0x70 * 2 write
s.TX(0x00)
s.TX(0x50)
s.E()
time.sleep(0.07)
s.S()
s.TX(0xE0) # write
s.TX(0x00)
s.S()
s.TX(0xE1) # read
b=[0]*8
for y in range(7):
b[y] = s.RX(1)
b[7] = s.RX(0)
s.E()
print(b)
s.cancel()
pi.stop()