-
Notifications
You must be signed in to change notification settings - Fork 3
/
i2c-util.s
432 lines (323 loc) · 8.19 KB
/
i2c-util.s
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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
/*
* I2C ULP utility routines
*/
#include "soc/rtc_cntl_reg.h"
#include "soc/rtc_io_reg.h"
#include "soc/soc_ulp.h"
#include "stack.s"
.bss
.global cnt_read
cnt_read:
.long 0
.global icounter
icounter:
.long 0
.global resul_pointer
resul_pointer:
.long 0
odd_sign:
.long 0
.text
write_intro:
psr
jump i2c_start_cond
ld r2,r3,20 // Address
lsh r2,r2,1
psr
jump i2c_write_byte
jumpr popfail,1,ge
ld r2,r3,16 // Register
psr
jump i2c_write_byte
jumpr popfail,1,ge
ret
.global write8
write8:
psr
jump write_intro
write_b:
ld r2,r3,8 // data byte
psr
jump i2c_write_byte
jumpr fail,1,ge
psr
jump i2c_stop_cond
move r2, 0 // Ok
ret
.global write16
write16:
psr
jump write_intro
ld r2,r3,8 // data byte 1
rsh r2,r2,8
psr
jump i2c_write_byte
jumpr fail,1,ge
jump write_b
read_intro:
psr
jump i2c_start_cond
ld r2,r3,16 // Address
lsh r2,r2,1
psr
jump i2c_write_byte
jumpr popfail,1,ge
ld r2,r3,12 // Register
psr
jump i2c_write_byte
jumpr popfail,1,ge
psr
jump i2c_start_cond
ld r2,r3,16
lsh r2,r2,1
or r2,r2,1 // Address Read
psr
jump i2c_write_byte
jumpr popfail,1,ge
ret
popfail:
pop r1 // pop caller return address
move r2,1
ret
.global read8
read8:
psr
jump read_intro
move r2,1 // last byte
psr
jump i2c_read_byte
push r0
psr
jump i2c_stop_cond
pop r0
move r2,0 // OK
ret
fail:
move r2,1
ret
.global read16
read16:
psr
jump read_intro
move r2,0
psr
jump i2c_read_byte
push r0
move r2,1 // last byte
psr
jump i2c_read_byte
push r0
psr
jump i2c_stop_cond
pop r0
pop r2 // first byte
lsh r2,r2,8
or r2,r2,r0
move r0,r2
move r2,0 // OK
ret
/*
.global readMULTY //
readMULTY:
ret
*/
.global i2c_MultyHandler
i2c_MultyHandler:
.long 0
i2c_fail:
move r2,1
ret
// - why i use stack here and do not write directly in memory??
// - i want to write common code for reading different sensor, as short as possible, and for some sensors i want to use filter of date that i will be skipped
// (filtering stack as i think is much better understandable than write on fly)
.global readMultyToStack //
readMultyToStack:
move r1, cnt_read
st r0, r1, 0
move r1, resul_pointer
st r2, r1, 0
store odd_sign 0
psr
jump read_intro
store_mem cnt_read icounter
next_byteN:
dec icounter // icounter -> r0
// push r0
jumpr last_byte, 0, eq
move r2, 0
jump end_byte_select
last_byte:
move r2, 1 // last byte
end_byte_select:
psr
jump i2c_read_byte
push r0
inc odd_sign // compress data logic
and r0, r0, 0x1 //
jumpr odd_bit_rmulty, 0x1, eq
pop r0
pop r1
lsh r1,r1,8 // compress data to uint16
or r1,r1,r0
push r1
odd_bit_rmulty:
move r2, icounter
ld r0, r2, 0
jumpr next_byteN, 0, gt
psr
jump i2c_stop_cond
// in stack now packed data in next func i should read data to memory (skip some data for example)
move r1, i2c_MultyHandler
ld r2, r1, 0x0
jump r2
.global i2cMultyHandlerEnd
i2cMultyHandlerEnd:
ret
.global processMultyFromStack //
processMultyFromStack:
// ahtung! should go strongly after readMultyToStack, cnt_read -- global value that set in that func once
// and in stack shoul be cnt_read packed bytes
move r2, cnt_read
ld r0, r2, 0
and r0, r0, 0x1 // detect odd or not odd
ld r1, r2, 0
rsh r1, r1, 1 // = r1 / 2
add r0, r0, r1 // cnt word in memory, in r0 iterater, do not use r0 below!
move r2, resul_pointer
ld r1, r2, 0
next_elem:
pop r2
st r2, r1, 0 // Save data in memory
sub r1, r1, 1 // move pointer to next memory cell
sub r0, r0, 1
jumpr next_elem, 0, gt
move r2, 0
jump i2cMultyHandlerEnd
// -- ret // this is end of handler
.global processBytesFromStack //
processBytesFromStack:
// ahtung! should go strongly after readMultyToStack, cnt_read -- global value that set in that func once
// and in stack shoul be cnt_read packed bytes
st r2, r1, 0 //
move r2, resul_pointer
ld r1, r2, 0
move r2, cnt_read
ld r0, r2, 0
move r2, icounter
st r0, r2, 0 //
and r0, r0, 0x1 // detect odd or not odd
jumpr next_bytes_elem, 0, eq
pop r2
st r2, r1, 0 // Save data in memory
sub r1, r1, 1 // move pointer to next memory cell
dec icounter // icounter -> r0
jumpr it_was_one_byte, 0, eq
next_bytes_elem:
pop r2
move r0, r2
and r0, r0, 0xff
st r0, r1, 0 // Save data in memory
sub r1, r1, 1 // move pointer to next memory cell
rsh r2, r2, 8
st r2, r1, 0 // Save data in memory
sub r1, r1, 1 // move pointer to next memory cell
dec icounter // icounter -> r0
dec icounter
jumpr next_bytes_elem, 0, gt
it_was_one_byte:
move r2, 0
jump i2cMultyHandlerEnd
// -- ret // this is end of handler
/*
.global process16Bit25HzFifo30100FromStack //
process16Bit25HzFifo30100FromStack:
// ahtung! should go strongly after readMultyToStack, cnt_read -- global value that set in that func once
// and in stack shoul be cnt_read packed bytes
move r1, cnt_read // icounter = cnt_read / 2 ;// i have 2 * uint8 in one uint16 (32)
ld r2, r1, 0
rsh r2, r2, 1
move r1, icounter
st r2, r1, 0 //
store odd_sign 0
move r2, resul_pointer
ld r1, r2, 0
ld r1, r1, 0 // r1 -- shouldn't be not used from here !!!!
next_elemFifo30100:
inc odd_sign // compress data logic // without r1!
and r0, r0, 0x3
jumpr Fifo30100_IR_1_byte, 0x02, eq // store only 2st uint16 (ir data) skip
pop r0
jump Fifo30100_skip_store
Fifo30100_IR_1_byte:
pop r0
st r0, r1, 0 // Save data in memory
add r1, r1, 1 // move pointer to next
Fifo30100_skip_store:
dec icounter // icounter -> r0
jumpr next_elemFifo30100, 0, gt
move r2, resul_pointer
ld r0, r2, 0
st r1, r0, 0 /// store result from r1 !!!!!!
jump i2cMultyHandlerEnd
// -- ret // this is end of handler
*/
.global process16Bit50HzFifo30100FromStack //
process16Bit50HzFifo30100FromStack:
// ahtung! should go strongly after readMultyToStack, cnt_read -- global value that set in that func once
// and in stack shoul be cnt_read packed bytes
move r1, cnt_read // icounter = cnt_read / 2 ;// i have 2 * uint8 in one uint16 (32)
ld r2, r1, 0
rsh r2, r2, 1
move r1, icounter
st r2, r1, 0 //
store odd_sign 0
move r2, resul_pointer
ld r1, r2, 0
ld r1, r1, 0 // r1 -- shouldn't be not used from here !!!!
next_elemFifo30100:
inc odd_sign // compress data logic // without r1!
and r0, r0, 0x1
jumpr Fifo30100_IR_1_byte, 0x00, eq // store only 2st uint16 (ir data) skip
pop r0
jump Fifo30100_skip_store
Fifo30100_IR_1_byte:
pop r0
st r0, r1, 0 // Save data in memory
add r1, r1, 1 // move pointer to next
Fifo30100_skip_store:
dec icounter // icounter -> r0
jumpr next_elemFifo30100, 0, gt
move r2, resul_pointer
ld r0, r2, 0
st r1, r0, 0 /// store result from r1 !!!!!!
jump i2cMultyHandlerEnd
// -- ret // this is end of handler
.global writeMULTY // i
writeMULTY:
ld r0,r3,8 // data byte 1
move r1, cnt_read
st r0, r1, 0
move r1, resul_pointer
st r2, r1, 0
psr
jump write_intro
store_mem cnt_read icounter
write_next_byteN:
dec icounter // icounter -> r0
move r1, resul_pointer
ld r1,r1,0 // load pointer from memory
ld r2,r1,0 // data byte
psr
jump i2c_write_byte
jumpr i2c_fail_2, 1, ge
inc resul_pointer // move pointer to next
move r2, icounter
ld r0, r2, 0
jumpr write_next_byteN, 0, gt
psr
jump i2c_stop_cond
move r2, 0 // Ok
ret
i2c_fail_2://same code but i can't go to first due to "relocation out of range"
move r2, 1
ret