New LiquidCrystal library  1.3.1
Generic LCD control library
/Users/fmalpartida/Documents/development/mercurial repos/SW/NewliquidCrystal/SoftI2CMaster.h
1 /* Arduino SoftI2C library.
2  *
3  * This is a very fast and very light-weight software I2C-master library
4  * written in assembler. It is based on Peter Fleury's I2C software
5  * library: http://homepage.hispeed.ch/peterfleury/avr-software.html
6  *
7  *
8  * This Library is free software: you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation, either version 3 of the License, or
11  * (at your option) any later version.
12  *
13  * This Library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with the Arduino I2cMaster Library. If not, see
20  * <http://www.gnu.org/licenses/>.
21  */
22 
23 /* In order to use the library, you need to define SDA_PIN, SCL_PIN,
24  * SDA_PORT and SCL_PORT before including this file. Have a look at
25  * http://www.arduino.cc/en/Reference/PortManipulation for finding out
26  * which values to use. For example, if you use digital pin 3 for
27  * SDA and digital pin 13 for SCL you have to use the following
28  * definitions:
29  * #define SDA_PIN 3
30  * #define SDA_PORT PORTB
31  * #define SCL_PIN 5
32  * #define SCL_PORT PORTB
33  *
34  * You can also define the following constants (see also below):
35  * - I2C_CPUFREQ, when changing CPU clock frequency dynamically
36  * - I2C_FASTMODE = 1 meaning that the I2C bus allows speeds up to 400 kHz
37  * - I2C_SLOWMODE = 1 meaning that the I2C bus will allow only up to 25 kHz
38  * - I2C_NOINTERRUPT = 1 in order to prohibit interrupts while
39  * communicating (see below). This can be useful if you use the library
40  * for communicationg with SMbus devices, which have timeouts.
41  * Note, however, that interrupts are disabledfrom issuing a start condition
42  * until issuing a stop condition. So use this option with care!
43  * - I2C_TIMEOUT = 0..10000 mssec in order to return from the I2C functions
44  * in case of a I2C bus lockup (i.e., SCL constantly low). 0 means no timeout
45  */
46 
47 /* Changelog:
48  * Version 1.1:
49  * - removed I2C_CLOCK_STRETCHING
50  * - added I2C_TIMEOUT time in msec (0..10000) until timeout or 0 if no timeout
51  * - changed i2c_init to return true iff both SDA and SCL are high
52  * - changed interrupt disabling so that the previous IRQ state is retored
53  * Version 1.0: basic functionality
54  */
55 #include <avr/io.h>
56 #include <Arduino.h>
57 
58 #ifndef _SOFTI2C_H
59 #define _SOFTI2C_H 1
60 
61 // Init function. Needs to be called once in the beginning.
62 // Returns false if SDA or SCL are low, which probably means
63 // a I2C bus lockup or that the lines are not pulled up.
64 boolean __attribute__ ((noinline)) i2c_init(void);
65 
66 // Start transfer function: <addr> is the 8-bit I2C address (including the R/W
67 // bit).
68 // Return: true if the slave replies with an "acknowledge", false otherwise
69 bool __attribute__ ((noinline)) i2c_start(uint8_t addr);
70 
71 // Similar to start function, but wait for an ACK! Be careful, this can
72 // result in an infinite loop!
73 void __attribute__ ((noinline)) i2c_start_wait(uint8_t addr);
74 
75 // Repeated start function: After having claimed the bus with a start condition,
76 // you can address another or the same chip again without an intervening
77 // stop condition.
78 // Return: true if the slave replies with an "acknowledge", false otherwise
79 bool __attribute__ ((noinline)) i2c_rep_start(uint8_t addr);
80 
81 // Issue a stop condition, freeing the bus.
82 void __attribute__ ((noinline)) i2c_stop(void) asm("ass_i2c_stop");
83 
84 // Write one byte to the slave chip that had been addressed
85 // by the previous start call. <value> is the byte to be sent.
86 // Return: true if the slave replies with an "acknowledge", false otherwise
87 bool __attribute__ ((noinline)) i2c_write(uint8_t value) asm("ass_i2c_write");
88 
89 // Read one byte. If <last> is true, we send a NAK after having received
90 // the byte in order to terminate the read sequence.
91 uint8_t __attribute__ ((noinline)) i2c_read(bool last);
92 
93 // You can set I2C_CPUFREQ independently of F_CPU if you
94 // change the CPU frequency on the fly. If do not define it,
95 // it will use the value of F_CPU
96 #ifndef I2C_CPUFREQ
97 #define I2C_CPUFREQ F_CPU
98 #endif
99 
100 // If I2C_FASTMODE is set to 1, then the highest possible frequency below 400kHz
101 // is selected. Be aware that not all slave chips may be able to deal with that!
102 #ifndef I2C_FASTMODE
103 #define I2C_FASTMODE 0
104 #endif
105 
106 // If I2C_FASTMODE is not defined or defined to be 0, then you can set
107 // I2C_SLOWMODE to 1. In this case, the I2C frequency will not be higher
108 // than 25KHz. This could be useful for problematic buses.
109 #ifndef I2C_SLOWMODE
110 #define I2C_SLOWMODE 0
111 #endif
112 
113 // if I2C_NOINTERRUPT is 1, then the I2C routines are not interruptable.
114 // This is most probably only necessary if you are using a 1MHz system clock,
115 // you are communicating with a SMBus device, and you want to avoid timeouts.
116 // Be aware that the interrupt bit is enabled after each call. So the
117 // I2C functions should not be called in interrupt routines or critical regions.
118 #ifndef I2C_NOINTERRUPT
119 #define I2C_NOINTERRUPT 0
120 #endif
121 
122 // I2C_TIMEOUT can be set to a value between 1 and 10000.
123 // If it is defined and nonzero, it leads to a timeout if the
124 // SCL is low longer than I2C_TIMEOUT milliseconds, i.e., max timeout is 10 sec
125 #ifndef I2C_TIMEOUT
126 #define I2C_TIMEOUT 0
127 #else
128 #if I2C_TIMEOUT > 10000
129 #error I2C_TIMEOUT is too large
130 #endif
131 #endif
132 
133 #define I2C_TIMEOUT_DELAY_LOOPS (I2C_CPUFREQ/1000UL)*I2C_TIMEOUT/4000UL
134 #if I2C_TIMEOUT_DELAY_LOOPS < 1
135 #define I2C_MAX_STRETCH 1
136 #else
137 #if I2C_TIMEOUT_DELAY_LOOPS > 60000UL
138 #define I2C_MAX_STRETCH 60000UL
139 #else
140 #define I2C_MAX_STRETCH I2C_TIMEOUT_DELAY_LOOPS
141 #endif
142 #endif
143 
144 #if I2C_FASTMODE
145 #define I2C_DELAY_COUNTER (((I2C_CPUFREQ/400000L)/2-19)/3)
146 #else
147 #if I2C_SLOWMODE
148 #define I2C_DELAY_COUNTER (((I2C_CPUFREQ/25000L)/2-19)/3)
149 #else
150 #define I2C_DELAY_COUNTER (((I2C_CPUFREQ/100000L)/2-19)/3)
151 #endif
152 #endif
153 
154 // Table of I2C bus speed in kbit/sec:
155 // CPU clock: 1MHz 2MHz 4MHz 8MHz 16MHz 20MHz
156 // Fast I2C mode 40 80 150 300 400 400
157 // Standard I2C mode 40 80 100 100 100 100
158 // Slow I2C mode 25 25 25 25 25 25
159 
160 // constants for reading & writing
161 #define I2C_READ 1
162 #define I2C_WRITE 0
163 
164 // map the IO register back into the IO address space
165 #define SDA_DDR (_SFR_IO_ADDR(SDA_PORT) - 1)
166 #define SCL_DDR (_SFR_IO_ADDR(SCL_PORT) - 1)
167 #define SDA_OUT _SFR_IO_ADDR(SDA_PORT)
168 #define SCL_OUT _SFR_IO_ADDR(SCL_PORT)
169 #define SDA_IN (_SFR_IO_ADDR(SDA_PORT) - 2)
170 #define SCL_IN (_SFR_IO_ADDR(SCL_PORT) - 2)
171 
172 #ifndef __tmp_reg__
173 #define __tmp_reg__ 0
174 #endif
175 
176 
177 // Internal delay functions.
178 void __attribute__ ((noinline)) i2c_delay_half(void) asm("ass_i2c_delay_half");
179 void __attribute__ ((noinline)) i2c_wait_scl_high(void) asm("ass_i2c_wait_scl_high");
180 
181 void i2c_delay_half(void)
182 { // function call 3 cycles => 3C
183 #if I2C_DELAY_COUNTER < 1
184  __asm__ __volatile__ (" ret");
185  // 7 cycles for call and return
186 #else
187  __asm__ __volatile__
188  (
189  " ldi r25, %[DELAY] ;load delay constant ;; 4C \n\t"
190  "_Lidelay: \n\t"
191  " dec r25 ;decrement counter ;; 4C+xC \n\t"
192  " brne _Lidelay ;;5C+(x-1)2C+xC\n\t"
193  " ret ;; 9C+(x-1)2C+xC = 7C+xC"
194  : : [DELAY] "M" I2C_DELAY_COUNTER : "r25");
195  // 7 cycles + 3 times x cycles
196 #endif
197 }
198 
199 void i2c_wait_scl_high(void)
200 {
201 #if I2C_TIMEOUT <= 0
202  __asm__ __volatile__
203  ("_Li2c_wait_stretch: \n\t"
204  " sbis %[SCLIN],%[SCLPIN] ;wait for SCL high \n\t"
205  " rjmp _Li2c_wait_stretch \n\t"
206  " cln ;signal: no timeout \n\t"
207  " ret "
208  : : [SCLIN] "I" (SCL_IN), [SCLPIN] "I" (SCL_PIN));
209 #else
210  __asm__ __volatile__
211  ( " ldi r27, %[HISTRETCH] ;load delay counter \n\t"
212  " ldi r26, %[LOSTRETCH] \n\t"
213  "_Lwait_stretch: \n\t"
214  " clr __tmp_reg__ ;do next loop 255 times \n\t"
215  "_Lwait_stretch_inner_loop: \n\t"
216  " rcall _Lcheck_scl_level ;call check function ;; 12C \n\t"
217  " brpl _Lstretch_done ;done if N=0 ;; +1 = 13C\n\t"
218  " dec __tmp_reg__ ;dec inner loop counter;; +1 = 14C\n\t"
219  " brne _Lwait_stretch_inner_loop ;; +2 = 16C\n\t"
220  " sbiw r26,1 ;dec outer loop counter \n\t"
221  " brne _Lwait_stretch ;continue with outer loop \n\t"
222  " sen ;timeout -> set N-bit=1 \n\t"
223  " rjmp _Lwait_return ;and return with N=1\n\t"
224  "_Lstretch_done: ;SCL=1 sensed \n\t"
225  " cln ;OK -> clear N-bit \n\t"
226  " rjmp _Lwait_return ; and return with N=0 \n\t"
227 
228  "_Lcheck_scl_level: ;; call = 3C\n\t"
229  " cln ;; +1C = 4C \n\t"
230  " sbic %[SCLIN],%[SCLPIN] ;skip if SCL still low ;; +2C = 6C \n\t"
231  " rjmp _Lscl_high ;; +0C = 6C \n\t"
232  " sen ;; +1 = 7C\n\t "
233  "_Lscl_high: "
234  " nop ;; +1C = 8C \n\t"
235  " ret ;return N-Bit=1 if low ;; +4 = 12C\n\t"
236 
237  "_Lwait_return:"
238  : : [SCLIN] "I" (SCL_IN), [SCLPIN] "I" (SCL_PIN),
239  [HISTRETCH] "M" (I2C_MAX_STRETCH>>8),
240  [LOSTRETCH] "M" (I2C_MAX_STRETCH&0xFF)
241  : "r26", "r27");
242 #endif
243 }
244 
245 
246 boolean i2c_init(void)
247 {
248  __asm__ __volatile__
249  (" cbi %[SDADDR],%[SDAPIN] ;release SDA \n\t"
250  " cbi %[SCLDDR],%[SCLPIN] ;release SCL \n\t"
251  " cbi %[SDAOUT],%[SDAPIN] ;clear SDA output value \n\t"
252  " cbi %[SCLOUT],%[SCLPIN] ;clear SCL output value \n\t"
253  " clr r24 ;set return value to false \n\t"
254  " clr r25 ;set return value to false \n\t"
255  " sbis %[SDAIN],%[SDAPIN] ;check for SDA high\n\t"
256  " ret ;if low return with false \n\t"
257  " sbis %[SCLIN],%[SCLPIN] ;check for SCL high \n\t"
258  " ret ;if low return with false \n\t"
259  " ldi r24,1 ;set return value to true \n\t"
260  " ret "
261  : :
262  [SCLDDR] "I" (SCL_DDR), [SCLPIN] "I" (SCL_PIN),
263  [SCLIN] "I" (SCL_IN), [SCLOUT] "I" (SCL_OUT),
264  [SDADDR] "I" (SDA_DDR), [SDAPIN] "I" (SDA_PIN),
265  [SDAIN] "I" (SDA_IN), [SDAOUT] "I" (SDA_OUT));
266  return true;
267 }
268 
269 bool i2c_start(uint8_t addr)
270 {
271  __asm__ __volatile__
272  (
273 #if I2C_NOINTERRUPT
274  " cli ;clear IRQ bit \n\t"
275 #endif
276  " sbis %[SCLIN],%[SCLPIN] ;check for clock stretching slave\n\t"
277  " rcall ass_i2c_wait_scl_high ;wait until SCL=H\n\t"
278  " sbi %[SDADDR],%[SDAPIN] ;force SDA low \n\t"
279  " rcall ass_i2c_delay_half ;wait T/2 \n\t"
280  " rcall ass_i2c_write ;now write address \n\t"
281  " ret"
282  : : [SDADDR] "I" (SDA_DDR), [SDAPIN] "I" (SDA_PIN),
283  [SCLIN] "I" (SCL_IN),[SCLPIN] "I" (SCL_PIN));
284  return true; // we never return here!
285 }
286 
287 bool i2c_rep_start(uint8_t addr)
288 {
289  __asm__ __volatile__
290 
291  (
292 #if I2C_NOINTERRUPT
293  " cli \n\t"
294 #endif
295  " sbi %[SCLDDR],%[SCLPIN] ;force SCL low \n\t"
296  " rcall ass_i2c_delay_half ;delay T/2 \n\t"
297  " cbi %[SDADDR],%[SDAPIN] ;release SDA \n\t"
298  " rcall ass_i2c_delay_half ;delay T/2 \n\t"
299  " cbi %[SCLDDR],%[SCLPIN] ;release SCL \n\t"
300  " rcall ass_i2c_delay_half ;delay T/2 \n\t"
301  " sbis %[SCLIN],%[SCLPIN] ;check for clock stretching slave\n\t"
302  " rcall ass_i2c_wait_scl_high ;wait until SCL=H\n\t"
303  " sbi %[SDADDR],%[SDAPIN] ;force SDA low \n\t"
304  " rcall ass_i2c_delay_half ;delay T/2 \n\t"
305  " rcall ass_i2c_write \n\t"
306  " ret"
307  : : [SCLDDR] "I" (SCL_DDR), [SCLPIN] "I" (SCL_PIN),[SCLIN] "I" (SCL_IN),
308  [SDADDR] "I" (SDA_DDR), [SDAPIN] "I" (SDA_PIN));
309  return true; // just to fool the compiler
310 }
311 
312 void i2c_start_wait(uint8_t addr)
313 {
314  __asm__ __volatile__
315  (
316  " push r24 ;save original parameter \n\t"
317  "_Li2c_start_wait1: \n\t"
318  " pop r24 ;restore original parameter\n\t"
319  " push r24 ;and save again \n\t"
320 #if I2C_NOINTERRUPT
321  " cli ;disable interrupts \n\t"
322 #endif
323  " sbis %[SCLIN],%[SCLPIN] ;check for clock stretching slave\n\t"
324  " rcall ass_i2c_wait_scl_high ;wait until SCL=H\n\t"
325  " sbi %[SDADDR],%[SDAPIN] ;force SDA low \n\t"
326  " rcall ass_i2c_delay_half ;delay T/2 \n\t"
327  " rcall ass_i2c_write ;write address \n\t"
328  " tst r24 ;if device not busy -> done \n\t"
329  " brne _Li2c_start_wait_done \n\t"
330  " rcall ass_i2c_stop ;terminate write & enable IRQ \n\t"
331  " rjmp _Li2c_start_wait1 ;device busy, poll ack again \n\t"
332  "_Li2c_start_wait_done: \n\t"
333  " pop __tmp_reg__ ;pop off orig argument \n\t"
334  " ret "
335  : : [SDADDR] "I" (SDA_DDR), [SDAPIN] "I" (SDA_PIN),
336  [SCLIN] "I" (SCL_IN),[SCLPIN] "I" (SCL_PIN));
337 }
338 
339 void i2c_stop(void)
340 {
341  __asm__ __volatile__
342  (
343  " sbi %[SCLDDR],%[SCLPIN] ;force SCL low \n\t"
344  " sbi %[SDADDR],%[SDAPIN] ;force SDA low \n\t"
345  " rcall ass_i2c_delay_half ;T/2 delay \n\t"
346  " cbi %[SCLDDR],%[SCLPIN] ;release SCL \n\t"
347  " rcall ass_i2c_delay_half ;T/2 delay \n\t"
348  " sbis %[SCLIN],%[SCLPIN] ;check for clock stretching slave\n\t"
349  " rcall ass_i2c_wait_scl_high ;wait until SCL=H\n\t"
350  " cbi %[SDADDR],%[SDAPIN] ;release SDA \n\t"
351  " rcall ass_i2c_delay_half \n\t"
352 #if I2C_NOINTERRUPT
353  " sei ;enable interrupts again!\n\t"
354 #endif
355  : : [SCLDDR] "I" (SCL_DDR), [SCLPIN] "I" (SCL_PIN), [SCLIN] "I" (SCL_IN),
356  [SDADDR] "I" (SDA_DDR), [SDAPIN] "I" (SDA_PIN));
357 }
358 
359 bool i2c_write(uint8_t value)
360 {
361  __asm__ __volatile__
362  (
363  " sec ;set carry flag \n\t"
364  " rol r24 ;shift in carry and shift out MSB \n\t"
365  " rjmp _Li2c_write_first \n\t"
366  "_Li2c_write_bit:\n\t"
367  " lsl r24 ;left shift into carry ;; 1C\n\t"
368  "_Li2c_write_first:\n\t"
369  " breq _Li2c_get_ack ;jump if TXreg is empty;; +1 = 2C \n\t"
370  " sbi %[SCLDDR],%[SCLPIN] ;force SCL low ;; +2 = 4C \n\t"
371  " nop \n\t"
372  " nop \n\t"
373  " nop \n\t"
374  " brcc _Li2c_write_low ;;+1/+2=5/6C\n\t"
375  " nop ;; +1 = 7C \n\t"
376  " cbi %[SDADDR],%[SDAPIN] ;release SDA ;; +2 = 9C \n\t"
377  " rjmp _Li2c_write_high ;; +2 = 11C \n\t"
378  "_Li2c_write_low: \n\t"
379  " sbi %[SDADDR],%[SDAPIN] ;force SDA low ;; +2 = 9C \n\t"
380  " rjmp _Li2c_write_high ;;+2 = 11C \n\t"
381  "_Li2c_write_high: \n\t"
382 #if I2C_DELAY_COUNTER >= 1
383  " rcall ass_i2c_delay_half ;delay T/2 ;;+X = 11C+X\n\t"
384 #endif
385  " cbi %[SCLDDR],%[SCLPIN] ;release SCL ;;+2 = 13C+X\n\t"
386  " cln ;clear N-bit ;;+1 = 14C+X\n\t"
387  " nop \n\t"
388  " nop \n\t"
389  " nop \n\t"
390  " sbis %[SCLIN],%[SCLPIN] ;check for SCL high ;;+2 = 16C+X\n\t"
391  " rcall ass_i2c_wait_scl_high \n\t"
392  " brpl _Ldelay_scl_high ;;+2 = 18C+X\n\t"
393  "_Li2c_write_return_false: \n\t"
394  " clr r24 ; return false because of timeout \n\t"
395  " rjmp _Li2c_write_return \n\t"
396  "_Ldelay_scl_high: \n\t"
397 #if I2C_DELAY_COUNTER >= 1
398  " rcall ass_i2c_delay_half ;delay T/2 ;;+X= 18C+2X\n\t"
399 #endif
400  " rjmp _Li2c_write_bit \n\t"
401  " ;; +2 = 20C +2X for one bit-loop \n\t"
402  "_Li2c_get_ack: \n\t"
403  " sbi %[SCLDDR],%[SCLPIN] ;force SCL low ;; +2 = 5C \n\t"
404  " nop \n\t"
405  " nop \n\t"
406  " cbi %[SDADDR],%[SDAPIN] ;release SDA ;;+2 = 7C \n\t"
407 #if I2C_DELAY_COUNTER >= 1
408  " rcall ass_i2c_delay_half ;delay T/2 ;; +X = 7C+X \n\t"
409 #endif
410  " clr r25 ;; 17C+2X \n\t"
411  " clr r24 ;return 0 ;; 14C + X \n\t"
412  " cbi %[SCLDDR],%[SCLPIN] ;release SCL ;; +2 = 9C+X\n\t"
413  "_Li2c_ack_wait: \n\t"
414  " cln ; clear N-bit ;; 10C + X\n\t"
415  " nop \n\t"
416  " sbis %[SCLIN],%[SCLPIN] ;wait SCL high ;; 12C + X \n\t"
417  " rcall ass_i2c_wait_scl_high \n\t"
418  " brmi _Li2c_write_return_false ;; 13C + X \n\t "
419  " sbis %[SDAIN],%[SDAPIN] ;if SDA hi -> return 0 ;; 15C + X \n\t"
420  " ldi r24,1 ;return true ;; 16C + X \n\t"
421 #if I2C_DELAY_COUNTER >= 1
422  " rcall ass_i2c_delay_half ;delay T/2 ;; 16C + 2X \n\t"
423 #endif
424  "_Li2c_write_return: \n\t"
425  " nop \n\t "
426  " nop \n\t "
427  " sbi %[SCLDDR],%[SCLPIN] ;force SCL low so SCL=H is short\n\t"
428  " ret \n\t"
429  " ;; + 4 = 17C + 2X for acknowldge bit"
430  ::
431  [SCLDDR] "I" (SCL_DDR), [SCLPIN] "I" (SCL_PIN), [SCLIN] "I" (SCL_IN),
432  [SDADDR] "I" (SDA_DDR), [SDAPIN] "I" (SDA_PIN), [SDAIN] "I" (SDA_IN));
433  return true; // fooling the compiler
434 }
435 
436 uint8_t i2c_read(bool last)
437 {
438  __asm__ __volatile__
439  (
440  " ldi r23,0x01 \n\t"
441  "_Li2c_read_bit: \n\t"
442  " sbi %[SCLDDR],%[SCLPIN] ;force SCL low ;; 2C \n\t"
443  " cbi %[SDADDR],%[SDAPIN] ;release SDA(prev. ACK);; 4C \n\t"
444  " nop \n\t"
445  " nop \n\t"
446  " nop \n\t"
447 #if I2C_DELAY_COUNTER >= 1
448  " rcall ass_i2c_delay_half ;delay T/2 ;; 4C+X \n\t"
449 #endif
450  " cbi %[SCLDDR],%[SCLPIN] ;release SCL ;; 6C + X \n\t"
451 #if I2C_DELAY_COUNTER >= 1
452  " rcall ass_i2c_delay_half ;delay T/2 ;; 6C + 2X \n\t"
453 #endif
454  " cln ; clear N-bit ;; 7C + 2X \n\t"
455  " nop \n\t "
456  " nop \n\t "
457  " nop \n\t "
458  " sbis %[SCLIN], %[SCLPIN] ;check for SCL high ;; 9C +2X \n\t"
459  " rcall ass_i2c_wait_scl_high \n\t"
460  " brmi _Li2c_read_return ;return if timeout ;; 10C + 2X\n\t"
461  " clc ;clear carry flag ;; 11C + 2X\n\t"
462  " sbic %[SDAIN],%[SDAPIN] ;if SDA is high ;; 11C + 2X\n\t"
463  " sec ;set carry flag ;; 12C + 2X\n\t"
464  " rol r23 ;store bit ;; 13C + 2X\n\t"
465  " brcc _Li2c_read_bit ;while receiv reg not full \n\t"
466  " ;; 15C + 2X for one bit loop \n\t"
467 
468  "_Li2c_put_ack: \n\t"
469  " sbi %[SCLDDR],%[SCLPIN] ;force SCL low ;; 2C \n\t"
470  " cpi r24,0 ;; 3C \n\t"
471  " breq _Li2c_put_ack_low ;if (ack=0) ;; 5C \n\t"
472  " cbi %[SDADDR],%[SDAPIN] ;release SDA \n\t"
473  " rjmp _Li2c_put_ack_high \n\t"
474  "_Li2c_put_ack_low: ;else \n\t"
475  " sbi %[SDADDR],%[SDAPIN] ;force SDA low ;; 7C \n\t"
476  "_Li2c_put_ack_high: \n\t"
477  " nop \n\t "
478  " nop \n\t "
479  " nop \n\t "
480 #if I2C_DELAY_COUNTER >= 1
481  " rcall ass_i2c_delay_half ;delay T/2 ;; 7C + X \n\t"
482 #endif
483  " cbi %[SCLDDR],%[SCLPIN] ;release SCL ;; 9C +X \n\t"
484  " cln ;clear N ;; +1 = 10C\n\t"
485  " nop \n\t "
486  " nop \n\t "
487  " sbis %[SCLIN],%[SCLPIN] ;wait SCL high ;; 12C + X\n\t"
488  " rcall ass_i2c_wait_scl_high \n\t"
489 #if I2C_DELAY_COUNTER >= 1
490  " rcall ass_i2c_delay_half ;delay T/2 ;; 11C + 2X\n\t"
491 #endif
492  "_Li2c_read_return: \n\t"
493  " nop \n\t "
494  " nop \n\t "
495  "sbi %[SCLDDR],%[SCLPIN] ;force SCL low so SCL=H is short\n\t"
496  " mov r24,r23 ;; 12C + 2X \n\t"
497  " clr r25 ;; 13 C + 2X\n\t"
498  " ret ;; 17C + X"
499  ::
500  [SCLDDR] "I" (SCL_DDR), [SCLPIN] "I" (SCL_PIN), [SCLIN] "I" (SCL_IN),
501  [SDADDR] "I" (SDA_DDR), [SDAPIN] "I" (SDA_PIN), [SDAIN] "I" (SDA_IN)
502  );
503  return ' '; // fool the compiler!
504 }
505 
506 #endif
507 
508 
509