-
Notifications
You must be signed in to change notification settings - Fork 90
/
SoftI2CMaster.h
executable file
·515 lines (472 loc) · 20.5 KB
/
SoftI2CMaster.h
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
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
/* Arduino SoftI2C library.
*
* This is a very fast and very light-weight software I2C-master library
* written in assembler. It is based on Peter Fleury's I2C software
* library: http://homepage.hispeed.ch/peterfleury/avr-software.html
*
*
* This Library is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This Library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with the Arduino I2cMaster Library. If not, see
* <http://www.gnu.org/licenses/>.
*/
/* In order to use the library, you need to define SDA_PIN, SCL_PIN,
* SDA_PORT and SCL_PORT before including this file. Have a look at
* http://www.arduino.cc/en/Reference/PortManipulation for finding out
* which values to use. For example, if you use digital pin 3 for
* SDA and digital pin 13 for SCL you have to use the following
* definitions:
* #define SDA_PIN 3
* #define SDA_PORT PORTB
* #define SCL_PIN 5
* #define SCL_PORT PORTB
*
* You can also define the following constants (see also below):
* - I2C_CPUFREQ, when changing CPU clock frequency dynamically
* - I2C_FASTMODE = 1 meaning that the I2C bus allows speeds up to 400 kHz
* - I2C_SLOWMODE = 1 meaning that the I2C bus will allow only up to 25 kHz
* - I2C_NOINTERRUPT = 1 in order to prohibit interrupts while
* communicating (see below). This can be useful if you use the library
* for communicationg with SMbus devices, which have timeouts.
* Note, however, that interrupts are disabledfrom issuing a start condition
* until issuing a stop condition. So use this option with care!
* - I2C_TIMEOUT = 0..10000 mssec in order to return from the I2C functions
* in case of a I2C bus lockup (i.e., SCL constantly low). 0 means no timeout
*/
/* Changelog:
* Version 1.1:
* - removed I2C_CLOCK_STRETCHING
* - added I2C_TIMEOUT time in msec (0..10000) until timeout or 0 if no timeout
* - changed i2c_init to return true iff both SDA and SCL are high
* - changed interrupt disabling so that the previous IRQ state is retored
* Version 1.0: basic functionality
*/
#include <avr/io.h>
#include <Arduino.h>
#ifndef _SOFTI2C_H
#define _SOFTI2C_H 1
#if defined (__AVR__)
// Init function. Needs to be called once in the beginning.
// Returns false if SDA or SCL are low, which probably means
// a I2C bus lockup or that the lines are not pulled up.
boolean __attribute__ ((noinline)) i2c_init(void);
// Start transfer function: <addr> is the 8-bit I2C address (including the R/W
// bit).
// Return: true if the slave replies with an "acknowledge", false otherwise
bool __attribute__ ((noinline)) i2c_start(uint8_t addr);
// Similar to start function, but wait for an ACK! Be careful, this can
// result in an infinite loop!
void __attribute__ ((noinline)) i2c_start_wait(uint8_t addr);
// Repeated start function: After having claimed the bus with a start condition,
// you can address another or the same chip again without an intervening
// stop condition.
// Return: true if the slave replies with an "acknowledge", false otherwise
bool __attribute__ ((noinline)) i2c_rep_start(uint8_t addr);
// Issue a stop condition, freeing the bus.
void __attribute__ ((noinline)) i2c_stop(void) asm("ass_i2c_stop");
// Write one byte to the slave chip that had been addressed
// by the previous start call. <value> is the byte to be sent.
// Return: true if the slave replies with an "acknowledge", false otherwise
bool __attribute__ ((noinline)) i2c_write(uint8_t value) asm("ass_i2c_write");
// Read one byte. If <last> is true, we send a NAK after having received
// the byte in order to terminate the read sequence.
uint8_t __attribute__ ((noinline)) i2c_read(bool last);
// You can set I2C_CPUFREQ independently of F_CPU if you
// change the CPU frequency on the fly. If do not define it,
// it will use the value of F_CPU
#ifndef I2C_CPUFREQ
#define I2C_CPUFREQ F_CPU
#endif
// If I2C_FASTMODE is set to 1, then the highest possible frequency below 400kHz
// is selected. Be aware that not all slave chips may be able to deal with that!
#ifndef I2C_FASTMODE
#define I2C_FASTMODE 0
#endif
// If I2C_FASTMODE is not defined or defined to be 0, then you can set
// I2C_SLOWMODE to 1. In this case, the I2C frequency will not be higher
// than 25KHz. This could be useful for problematic buses.
#ifndef I2C_SLOWMODE
#define I2C_SLOWMODE 0
#endif
// if I2C_NOINTERRUPT is 1, then the I2C routines are not interruptable.
// This is most probably only necessary if you are using a 1MHz system clock,
// you are communicating with a SMBus device, and you want to avoid timeouts.
// Be aware that the interrupt bit is enabled after each call. So the
// I2C functions should not be called in interrupt routines or critical regions.
#ifndef I2C_NOINTERRUPT
#define I2C_NOINTERRUPT 0
#endif
// I2C_TIMEOUT can be set to a value between 1 and 10000.
// If it is defined and nonzero, it leads to a timeout if the
// SCL is low longer than I2C_TIMEOUT milliseconds, i.e., max timeout is 10 sec
#ifndef I2C_TIMEOUT
#define I2C_TIMEOUT 0
#else
#if I2C_TIMEOUT > 10000
#error I2C_TIMEOUT is too large
#endif
#endif
#define I2C_TIMEOUT_DELAY_LOOPS (I2C_CPUFREQ/1000UL)*I2C_TIMEOUT/4000UL
#if I2C_TIMEOUT_DELAY_LOOPS < 1
#define I2C_MAX_STRETCH 1
#else
#if I2C_TIMEOUT_DELAY_LOOPS > 60000UL
#define I2C_MAX_STRETCH 60000UL
#else
#define I2C_MAX_STRETCH I2C_TIMEOUT_DELAY_LOOPS
#endif
#endif
#if I2C_FASTMODE
#define I2C_DELAY_COUNTER (((I2C_CPUFREQ/400000L)/2-19)/3)
#else
#if I2C_SLOWMODE
#define I2C_DELAY_COUNTER (((I2C_CPUFREQ/25000L)/2-19)/3)
#else
#define I2C_DELAY_COUNTER (((I2C_CPUFREQ/100000L)/2-19)/3)
#endif
#endif
// Table of I2C bus speed in kbit/sec:
// CPU clock: 1MHz 2MHz 4MHz 8MHz 16MHz 20MHz
// Fast I2C mode 40 80 150 300 400 400
// Standard I2C mode 40 80 100 100 100 100
// Slow I2C mode 25 25 25 25 25 25
// constants for reading & writing
#define I2C_READ 1
#define I2C_WRITE 0
// map the IO register back into the IO address space
#define SDA_DDR (_SFR_IO_ADDR(SDA_PORT) - 1)
#define SCL_DDR (_SFR_IO_ADDR(SCL_PORT) - 1)
#define SDA_OUT _SFR_IO_ADDR(SDA_PORT)
#define SCL_OUT _SFR_IO_ADDR(SCL_PORT)
#define SDA_IN (_SFR_IO_ADDR(SDA_PORT) - 2)
#define SCL_IN (_SFR_IO_ADDR(SCL_PORT) - 2)
#ifndef __tmp_reg__
#define __tmp_reg__ 0
#endif
// Internal delay functions.
void __attribute__ ((noinline)) i2c_delay_half(void) asm("ass_i2c_delay_half");
void __attribute__ ((noinline)) i2c_wait_scl_high(void) asm("ass_i2c_wait_scl_high");
void i2c_delay_half(void)
{ // function call 3 cycles => 3C
#if I2C_DELAY_COUNTER < 1
__asm__ __volatile__ (" ret");
// 7 cycles for call and return
#else
__asm__ __volatile__
(
" ldi r25, %[DELAY] ;load delay constant ;; 4C \n\t"
"_Lidelay: \n\t"
" dec r25 ;decrement counter ;; 4C+xC \n\t"
" brne _Lidelay ;;5C+(x-1)2C+xC\n\t"
" ret ;; 9C+(x-1)2C+xC = 7C+xC"
: : [DELAY] "M" I2C_DELAY_COUNTER : "r25");
// 7 cycles + 3 times x cycles
#endif
}
void i2c_wait_scl_high(void)
{
#if I2C_TIMEOUT <= 0
__asm__ __volatile__
("_Li2c_wait_stretch: \n\t"
" sbis %[SCLIN],%[SCLPIN] ;wait for SCL high \n\t"
" rjmp _Li2c_wait_stretch \n\t"
" cln ;signal: no timeout \n\t"
" ret "
: : [SCLIN] "I" (SCL_IN), [SCLPIN] "I" (SCL_PIN));
#else
__asm__ __volatile__
( " ldi r27, %[HISTRETCH] ;load delay counter \n\t"
" ldi r26, %[LOSTRETCH] \n\t"
"_Lwait_stretch: \n\t"
" clr __tmp_reg__ ;do next loop 255 times \n\t"
"_Lwait_stretch_inner_loop: \n\t"
" rcall _Lcheck_scl_level ;call check function ;; 12C \n\t"
" brpl _Lstretch_done ;done if N=0 ;; +1 = 13C\n\t"
" dec __tmp_reg__ ;dec inner loop counter;; +1 = 14C\n\t"
" brne _Lwait_stretch_inner_loop ;; +2 = 16C\n\t"
" sbiw r26,1 ;dec outer loop counter \n\t"
" brne _Lwait_stretch ;continue with outer loop \n\t"
" sen ;timeout -> set N-bit=1 \n\t"
" rjmp _Lwait_return ;and return with N=1\n\t"
"_Lstretch_done: ;SCL=1 sensed \n\t"
" cln ;OK -> clear N-bit \n\t"
" rjmp _Lwait_return ; and return with N=0 \n\t"
"_Lcheck_scl_level: ;; call = 3C\n\t"
" cln ;; +1C = 4C \n\t"
" sbic %[SCLIN],%[SCLPIN] ;skip if SCL still low ;; +2C = 6C \n\t"
" rjmp _Lscl_high ;; +0C = 6C \n\t"
" sen ;; +1 = 7C\n\t "
"_Lscl_high: "
" nop ;; +1C = 8C \n\t"
" ret ;return N-Bit=1 if low ;; +4 = 12C\n\t"
"_Lwait_return:"
: : [SCLIN] "I" (SCL_IN), [SCLPIN] "I" (SCL_PIN),
[HISTRETCH] "M" (I2C_MAX_STRETCH>>8),
[LOSTRETCH] "M" (I2C_MAX_STRETCH&0xFF)
: "r26", "r27");
#endif
}
boolean i2c_init(void)
{
__asm__ __volatile__
(" cbi %[SDADDR],%[SDAPIN] ;release SDA \n\t"
" cbi %[SCLDDR],%[SCLPIN] ;release SCL \n\t"
" cbi %[SDAOUT],%[SDAPIN] ;clear SDA output value \n\t"
" cbi %[SCLOUT],%[SCLPIN] ;clear SCL output value \n\t"
" clr r24 ;set return value to false \n\t"
" clr r25 ;set return value to false \n\t"
" sbis %[SDAIN],%[SDAPIN] ;check for SDA high\n\t"
" ret ;if low return with false \n\t"
" sbis %[SCLIN],%[SCLPIN] ;check for SCL high \n\t"
" ret ;if low return with false \n\t"
" ldi r24,1 ;set return value to true \n\t"
" ret "
: :
[SCLDDR] "I" (SCL_DDR), [SCLPIN] "I" (SCL_PIN),
[SCLIN] "I" (SCL_IN), [SCLOUT] "I" (SCL_OUT),
[SDADDR] "I" (SDA_DDR), [SDAPIN] "I" (SDA_PIN),
[SDAIN] "I" (SDA_IN), [SDAOUT] "I" (SDA_OUT));
return true;
}
bool i2c_start(uint8_t addr)
{
__asm__ __volatile__
(
#if I2C_NOINTERRUPT
" cli ;clear IRQ bit \n\t"
#endif
" sbis %[SCLIN],%[SCLPIN] ;check for clock stretching slave\n\t"
" rcall ass_i2c_wait_scl_high ;wait until SCL=H\n\t"
" sbi %[SDADDR],%[SDAPIN] ;force SDA low \n\t"
" rcall ass_i2c_delay_half ;wait T/2 \n\t"
" rcall ass_i2c_write ;now write address \n\t"
" ret"
: : [SDADDR] "I" (SDA_DDR), [SDAPIN] "I" (SDA_PIN),
[SCLIN] "I" (SCL_IN),[SCLPIN] "I" (SCL_PIN));
return true; // we never return here!
}
bool i2c_rep_start(uint8_t addr)
{
__asm__ __volatile__
(
#if I2C_NOINTERRUPT
" cli \n\t"
#endif
" sbi %[SCLDDR],%[SCLPIN] ;force SCL low \n\t"
" rcall ass_i2c_delay_half ;delay T/2 \n\t"
" cbi %[SDADDR],%[SDAPIN] ;release SDA \n\t"
" rcall ass_i2c_delay_half ;delay T/2 \n\t"
" cbi %[SCLDDR],%[SCLPIN] ;release SCL \n\t"
" rcall ass_i2c_delay_half ;delay T/2 \n\t"
" sbis %[SCLIN],%[SCLPIN] ;check for clock stretching slave\n\t"
" rcall ass_i2c_wait_scl_high ;wait until SCL=H\n\t"
" sbi %[SDADDR],%[SDAPIN] ;force SDA low \n\t"
" rcall ass_i2c_delay_half ;delay T/2 \n\t"
" rcall ass_i2c_write \n\t"
" ret"
: : [SCLDDR] "I" (SCL_DDR), [SCLPIN] "I" (SCL_PIN),[SCLIN] "I" (SCL_IN),
[SDADDR] "I" (SDA_DDR), [SDAPIN] "I" (SDA_PIN));
return true; // just to fool the compiler
}
void i2c_start_wait(uint8_t addr)
{
__asm__ __volatile__
(
" push r24 ;save original parameter \n\t"
"_Li2c_start_wait1: \n\t"
" pop r24 ;restore original parameter\n\t"
" push r24 ;and save again \n\t"
#if I2C_NOINTERRUPT
" cli ;disable interrupts \n\t"
#endif
" sbis %[SCLIN],%[SCLPIN] ;check for clock stretching slave\n\t"
" rcall ass_i2c_wait_scl_high ;wait until SCL=H\n\t"
" sbi %[SDADDR],%[SDAPIN] ;force SDA low \n\t"
" rcall ass_i2c_delay_half ;delay T/2 \n\t"
" rcall ass_i2c_write ;write address \n\t"
" tst r24 ;if device not busy -> done \n\t"
" brne _Li2c_start_wait_done \n\t"
" rcall ass_i2c_stop ;terminate write & enable IRQ \n\t"
" rjmp _Li2c_start_wait1 ;device busy, poll ack again \n\t"
"_Li2c_start_wait_done: \n\t"
" pop __tmp_reg__ ;pop off orig argument \n\t"
" ret "
: : [SDADDR] "I" (SDA_DDR), [SDAPIN] "I" (SDA_PIN),
[SCLIN] "I" (SCL_IN),[SCLPIN] "I" (SCL_PIN));
}
void i2c_stop(void)
{
__asm__ __volatile__
(
" sbi %[SCLDDR],%[SCLPIN] ;force SCL low \n\t"
" sbi %[SDADDR],%[SDAPIN] ;force SDA low \n\t"
" rcall ass_i2c_delay_half ;T/2 delay \n\t"
" cbi %[SCLDDR],%[SCLPIN] ;release SCL \n\t"
" rcall ass_i2c_delay_half ;T/2 delay \n\t"
" sbis %[SCLIN],%[SCLPIN] ;check for clock stretching slave\n\t"
" rcall ass_i2c_wait_scl_high ;wait until SCL=H\n\t"
" cbi %[SDADDR],%[SDAPIN] ;release SDA \n\t"
" rcall ass_i2c_delay_half \n\t"
#if I2C_NOINTERRUPT
" sei ;enable interrupts again!\n\t"
#endif
: : [SCLDDR] "I" (SCL_DDR), [SCLPIN] "I" (SCL_PIN), [SCLIN] "I" (SCL_IN),
[SDADDR] "I" (SDA_DDR), [SDAPIN] "I" (SDA_PIN));
}
bool i2c_write(uint8_t value)
{
__asm__ __volatile__
(
" sec ;set carry flag \n\t"
" rol r24 ;shift in carry and shift out MSB \n\t"
" rjmp _Li2c_write_first \n\t"
"_Li2c_write_bit:\n\t"
" lsl r24 ;left shift into carry ;; 1C\n\t"
"_Li2c_write_first:\n\t"
" breq _Li2c_get_ack ;jump if TXreg is empty;; +1 = 2C \n\t"
" sbi %[SCLDDR],%[SCLPIN] ;force SCL low ;; +2 = 4C \n\t"
" nop \n\t"
" nop \n\t"
" nop \n\t"
" brcc _Li2c_write_low ;;+1/+2=5/6C\n\t"
" nop ;; +1 = 7C \n\t"
" cbi %[SDADDR],%[SDAPIN] ;release SDA ;; +2 = 9C \n\t"
" rjmp _Li2c_write_high ;; +2 = 11C \n\t"
"_Li2c_write_low: \n\t"
" sbi %[SDADDR],%[SDAPIN] ;force SDA low ;; +2 = 9C \n\t"
" rjmp _Li2c_write_high ;;+2 = 11C \n\t"
"_Li2c_write_high: \n\t"
#if I2C_DELAY_COUNTER >= 1
" rcall ass_i2c_delay_half ;delay T/2 ;;+X = 11C+X\n\t"
#endif
" cbi %[SCLDDR],%[SCLPIN] ;release SCL ;;+2 = 13C+X\n\t"
" cln ;clear N-bit ;;+1 = 14C+X\n\t"
" nop \n\t"
" nop \n\t"
" nop \n\t"
" sbis %[SCLIN],%[SCLPIN] ;check for SCL high ;;+2 = 16C+X\n\t"
" rcall ass_i2c_wait_scl_high \n\t"
" brpl _Ldelay_scl_high ;;+2 = 18C+X\n\t"
"_Li2c_write_return_false: \n\t"
" clr r24 ; return false because of timeout \n\t"
" rjmp _Li2c_write_return \n\t"
"_Ldelay_scl_high: \n\t"
#if I2C_DELAY_COUNTER >= 1
" rcall ass_i2c_delay_half ;delay T/2 ;;+X= 18C+2X\n\t"
#endif
" rjmp _Li2c_write_bit \n\t"
" ;; +2 = 20C +2X for one bit-loop \n\t"
"_Li2c_get_ack: \n\t"
" sbi %[SCLDDR],%[SCLPIN] ;force SCL low ;; +2 = 5C \n\t"
" nop \n\t"
" nop \n\t"
" cbi %[SDADDR],%[SDAPIN] ;release SDA ;;+2 = 7C \n\t"
#if I2C_DELAY_COUNTER >= 1
" rcall ass_i2c_delay_half ;delay T/2 ;; +X = 7C+X \n\t"
#endif
" clr r25 ;; 17C+2X \n\t"
" clr r24 ;return 0 ;; 14C + X \n\t"
" cbi %[SCLDDR],%[SCLPIN] ;release SCL ;; +2 = 9C+X\n\t"
"_Li2c_ack_wait: \n\t"
" cln ; clear N-bit ;; 10C + X\n\t"
" nop \n\t"
" sbis %[SCLIN],%[SCLPIN] ;wait SCL high ;; 12C + X \n\t"
" rcall ass_i2c_wait_scl_high \n\t"
" brmi _Li2c_write_return_false ;; 13C + X \n\t "
" sbis %[SDAIN],%[SDAPIN] ;if SDA hi -> return 0 ;; 15C + X \n\t"
" ldi r24,1 ;return true ;; 16C + X \n\t"
#if I2C_DELAY_COUNTER >= 1
" rcall ass_i2c_delay_half ;delay T/2 ;; 16C + 2X \n\t"
#endif
"_Li2c_write_return: \n\t"
" nop \n\t "
" nop \n\t "
" sbi %[SCLDDR],%[SCLPIN] ;force SCL low so SCL=H is short\n\t"
" ret \n\t"
" ;; + 4 = 17C + 2X for acknowldge bit"
::
[SCLDDR] "I" (SCL_DDR), [SCLPIN] "I" (SCL_PIN), [SCLIN] "I" (SCL_IN),
[SDADDR] "I" (SDA_DDR), [SDAPIN] "I" (SDA_PIN), [SDAIN] "I" (SDA_IN));
return true; // fooling the compiler
}
uint8_t i2c_read(bool last)
{
__asm__ __volatile__
(
" ldi r23,0x01 \n\t"
"_Li2c_read_bit: \n\t"
" sbi %[SCLDDR],%[SCLPIN] ;force SCL low ;; 2C \n\t"
" cbi %[SDADDR],%[SDAPIN] ;release SDA(prev. ACK);; 4C \n\t"
" nop \n\t"
" nop \n\t"
" nop \n\t"
#if I2C_DELAY_COUNTER >= 1
" rcall ass_i2c_delay_half ;delay T/2 ;; 4C+X \n\t"
#endif
" cbi %[SCLDDR],%[SCLPIN] ;release SCL ;; 6C + X \n\t"
#if I2C_DELAY_COUNTER >= 1
" rcall ass_i2c_delay_half ;delay T/2 ;; 6C + 2X \n\t"
#endif
" cln ; clear N-bit ;; 7C + 2X \n\t"
" nop \n\t "
" nop \n\t "
" nop \n\t "
" sbis %[SCLIN], %[SCLPIN] ;check for SCL high ;; 9C +2X \n\t"
" rcall ass_i2c_wait_scl_high \n\t"
" brmi _Li2c_read_return ;return if timeout ;; 10C + 2X\n\t"
" clc ;clear carry flag ;; 11C + 2X\n\t"
" sbic %[SDAIN],%[SDAPIN] ;if SDA is high ;; 11C + 2X\n\t"
" sec ;set carry flag ;; 12C + 2X\n\t"
" rol r23 ;store bit ;; 13C + 2X\n\t"
" brcc _Li2c_read_bit ;while receiv reg not full \n\t"
" ;; 15C + 2X for one bit loop \n\t"
"_Li2c_put_ack: \n\t"
" sbi %[SCLDDR],%[SCLPIN] ;force SCL low ;; 2C \n\t"
" cpi r24,0 ;; 3C \n\t"
" breq _Li2c_put_ack_low ;if (ack=0) ;; 5C \n\t"
" cbi %[SDADDR],%[SDAPIN] ;release SDA \n\t"
" rjmp _Li2c_put_ack_high \n\t"
"_Li2c_put_ack_low: ;else \n\t"
" sbi %[SDADDR],%[SDAPIN] ;force SDA low ;; 7C \n\t"
"_Li2c_put_ack_high: \n\t"
" nop \n\t "
" nop \n\t "
" nop \n\t "
#if I2C_DELAY_COUNTER >= 1
" rcall ass_i2c_delay_half ;delay T/2 ;; 7C + X \n\t"
#endif
" cbi %[SCLDDR],%[SCLPIN] ;release SCL ;; 9C +X \n\t"
" cln ;clear N ;; +1 = 10C\n\t"
" nop \n\t "
" nop \n\t "
" sbis %[SCLIN],%[SCLPIN] ;wait SCL high ;; 12C + X\n\t"
" rcall ass_i2c_wait_scl_high \n\t"
#if I2C_DELAY_COUNTER >= 1
" rcall ass_i2c_delay_half ;delay T/2 ;; 11C + 2X\n\t"
#endif
"_Li2c_read_return: \n\t"
" nop \n\t "
" nop \n\t "
"sbi %[SCLDDR],%[SCLPIN] ;force SCL low so SCL=H is short\n\t"
" mov r24,r23 ;; 12C + 2X \n\t"
" clr r25 ;; 13 C + 2X\n\t"
" ret ;; 17C + X"
::
[SCLDDR] "I" (SCL_DDR), [SCLPIN] "I" (SCL_PIN), [SCLIN] "I" (SCL_IN),
[SDADDR] "I" (SDA_DDR), [SDAPIN] "I" (SDA_PIN), [SDAIN] "I" (SDA_IN)
);
return ' '; // fool the compiler!
}
#else
#error "ONLY SUPPORTED ON AVR PROCESSORS"
#endif // defined (__AVR__)
#endif