-
Notifications
You must be signed in to change notification settings - Fork 0
/
jm_i2c.spin2
339 lines (257 loc) · 13 KB
/
jm_i2c.spin2
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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
'' =================================================================================================
''
'' File....... jm_i2c.spin2
'' Purpose.... Low-level I2C routines for the P2
'' Author..... Jon "JonnyMac" McPhalen
'' Copyright (c) 2020-2022 Jon McPhalen
'' -- see below for terms of use
'' E-mail..... jon.mcphalen@gmail.com
'' Started....
'' Updated.... 24 FEB 2022
''
'' =================================================================================================
' Note: The pin modes drive the high output through a resistor; this simulates the use of a pull-up
' on the pin.
'
' PU_NONE changed to PU_EXT for clarity.
con { fixed io pins }
PGM_RX = 63 { I } ' programming / debug
PGM_TX = 62 { O }
SF_CS = 61 { O } ' serial flash
SF_SCK = 60 { O }
SF_SDO = 59 { O }
SF_SDI = 58 { I }
con
#0, PU_EXT, PU_1K5, PU_3K3, PU_15K ' pull-up options
#0, ACK, NAK
var
long sclpin ' i2c bus pins
long sdapin
long clktix ' system ticks in 1/4 period
pub null()
'' This is not a top-level object
pub setup(scl, sda, khz, pullup) | tix
'' Define I2C SCL (clock) and SDA (data) pins
'' -- khz is bus frequency: 100 (standard), 400 (full), 1000 (fast)
'' * circuit/connections will affect maximum bus speed
'' -- pullup controls high level drive configuration of SCL and SDA
longmove(@sclpin, @scl, 2) ' copy pins
clktix := tix := ((clkfreq / (khz * 1_000)) >> 2) - 4 ' calculate ticks in 1/4 period
case pullup
PU_EXT : pullup := P_HIGH_FLOAT ' external pull-up
PU_1K5 : pullup := P_HIGH_1K5 ' 1.5k
PU_3K3 : pullup := P_HIGH_1MA ' acts like ~3.3k
other : pullup := P_HIGH_15K ' 15K
org
wrpin pullup, scl ' configure high drive
wrpin pullup, sda
drvh scl ' both high
drvh sda
waitx tix
waitx tix
rep #8, #9 ' bus clear (if SDA stuck low)
testp sda wc ' sample sda
if_c jmp #.done ' abort loop if sda == 1
drvl scl ' scl low
waitx tix
waitx tix
drvh scl ' scl high
waitx tix
waitx tix
.done
end
pub present(slaveid) : result
'' Pings device, returns true if device on bus.
start()
result := (write(slaveid) == ACK)
pub wait(slaveid) | ackbit
'' Waits for device to be ready for new command.
'' -- Note: Use present() to detect device before using wait()
repeat
start()
ackbit := write(slaveid)
until (ackbit == ACK)
pub start() | scl, sda, tix
'' Create I2C start sequence
'' -- will wait if I2C bus SCL pin is held low
longmove(@scl, @sclpin, 3) ' copy pins & timing
org
drvh sda ' both high
drvh scl
waitx tix
waitx tix
drvl sda ' start sequence
waitx tix
waitx tix
drvl scl
waitx tix
waitx tix
end
pub write(i2cbyte) : ackbit | scl, sda, tix, bits
'' Write byte to I2C bus
'' -- leaves SCL low
longmove(@scl, @sclpin, 3) ' copy pins & timing
org
shl i2cbyte, #24 ' align i2cbyte.[7] to i2cbyte.[31]
.wr_byte mov bits, #8 ' output 8 bits, msbfirst
.wb0 shl i2cbyte, #1 wc ' msb --> c
drvc sda ' c -- sda
waitx tix ' let sda settle
drvh scl ' scl high
testp scl wc ' check for clock stretch
if_nc jmp #$-2
waitx tix
waitx tix
drvl scl ' scl low
waitx tix
djnz bits, #.wb0
.get_ack drvh sda ' pull-up sda
waitx tix
drvh scl ' scl high
testp scl wc ' check for clock stretch
if_nc jmp #$-2
waitx tix
testp sda wc ' sample sda (ack bit)
muxc ackbit, #1 ' update return value
waitx tix
drvl scl ' scl low
waitx tix
waitx tix
end
pub wr_block(p_block, count) : ackbit | scl, sda, tix, i2cbyte, bits
'' Write count bytes from p_block to I2C bus
'' -- p_block is pointer to bytes
'' -- leaves SCL low
longmove(@scl, @sclpin, 3) ' copy pins & timing
org
.get_byte rdbyte i2cbyte, p_block ' read byte from block
add p_block, #1 ' increment block pointer
shl i2cbyte, #24 ' align i2cbyte.[7] to i2cbyte.[31]
.wr_byte mov bits, #8 ' output 8 bits, msbfirst
.wb0 shl i2cbyte, #1 wc ' msb --> c
drvc sda ' c -- sda
waitx tix ' let sda settle
drvh scl ' scl high
testp scl wc ' check for clock stretch
if_nc jmp #$-2
waitx tix
waitx tix
drvl scl ' scl low
waitx tix
djnz bits, #.wb0
.get_ack drvh sda ' pull-up sda
waitx tix
drvh scl ' scl high
testp scl wc ' check for clock stretch
if_nc jmp #$-2
waitx tix
testp sda wc ' sample sda (ack bit)
if_c or ackbit, #1 ' update return value
waitx tix
drvl scl ' scl low
waitx tix
waitx tix
djnz count, #.get_byte
end
pub read(ackbit) : i2cbyte | scl, sda, tix, bits
'' Read byte from I2C bus
'' -- ackbit is state of ack bit
'' * usually NAK for last byte read
longmove(@scl, @sclpin, 3) ' copy pins & timing
org
drvh sda ' pull-up sda
.rd_byte mov bits, #8 ' read 8 bits, msb first
.rb0 waitx tix
drvh scl ' scl high
testp scl wc ' check for clock stretch
if_nc jmp #$-2
waitx tix
testp sda wc ' sample sda
shl i2cbyte, #1 ' make room for new bit
muxc i2cbyte, #1 ' sda --> i2cbyte.[0]
waitx tix
drvl scl ' scl low
waitx tix
djnz bits, #.rb0
.put_ack testb ackbit, #0 wc ' ackbit.[0] --> c
drvc sda ' c --> sda
waitx tix ' let sda settle
drvh scl ' scl high
testp scl wc ' check for clock stretch
if_nc jmp #$-2
waitx tix
waitx tix
drvl scl ' scl low
waitx tix
waitx tix
end
pub rd_block(p_block, count, ackbit) | i2cbyte, scl, sda, tix, bits
'' Read count bytes from I2C bus
'' -- p_block is pointer to storage location for bytes
'' -- ackbit is state of ack for final byte read
longmove(@scl, @sclpin, 3) ' copy pins & timing
org
.rd_block drvh sda ' pull-up sda
mov i2cbyte, #0 ' clear workspace
.rd_byte mov bits, #8 ' read 8 bits, msb first
.rb0 waitx tix
drvh scl ' scl high
testp scl wc ' check for clock stretch
if_nc jmp #$-2
waitx tix
testp sda wc ' sample sda
shl i2cbyte, #1 ' make room for new bit
muxc i2cbyte, #1 ' sda --> i2cbyte.[0]
waitx tix
drvl scl ' scl low
waitx tix
djnz bits, #.rb0
.put_ack cmp count, #1 wz ' last byte?
if_nz drvl sda ' 0 --> sda (ack) if not last byte
if_z testb ackbit, #0 wc ' last byte, ackbit.[0] --> c
if_z drvc sda ' last byte, c --> sda
waitx tix ' let sda settle
drvh scl ' scl high
testp scl wc ' check for clock stretch
if_nc jmp #$-2
waitx tix
waitx tix
drvl scl ' scl low
waitx tix
waitx tix
.put_byte wrbyte i2cbyte, p_block ' write byte to block
add p_block, #1 ' increment block pointer
djnz count, #.rd_block
end
pub stop() | scl, sda, tix
'' Create I2C stop sequence
longmove(@scl, @sclpin, 3) ' copy pins & timing
org
drvl sda ' hold sda low
waitx tix
drvh scl ' release scl
testp scl wc ' check for clock stretch
if_nc jmp #$-2
waitx tix
waitx tix
drvh sda ' finish stop sequence
end
con { license }
{{
Terms of Use: MIT License
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
}}