@@ -45,13 +45,15 @@ static uint32_t twi_clockStretchLimit;
4545
4646void twi_setClock (unsigned int freq ){
4747#if F_CPU == FCPU80
48- if (freq <= 100000 ) twi_dcount = 19 ;//about 100KHz
48+ if (freq <= 50000 ) twi_dcount = 38 ;//about 50KHz
49+ else if (freq <= 100000 ) twi_dcount = 19 ;//about 100KHz
4950 else if (freq <= 200000 ) twi_dcount = 8 ;//about 200KHz
5051 else if (freq <= 300000 ) twi_dcount = 3 ;//about 300KHz
5152 else if (freq <= 400000 ) twi_dcount = 1 ;//about 400KHz
5253 else twi_dcount = 1 ;//about 400KHz
5354#else
54- if (freq <= 100000 ) twi_dcount = 32 ;//about 100KHz
55+ if (freq <= 50000 ) twi_dcount = 64 ;//about 50KHz
56+ else if (freq <= 100000 ) twi_dcount = 32 ;//about 100KHz
5557 else if (freq <= 200000 ) twi_dcount = 14 ;//about 200KHz
5658 else if (freq <= 300000 ) twi_dcount = 8 ;//about 300KHz
5759 else if (freq <= 400000 ) twi_dcount = 5 ;//about 400KHz
@@ -85,14 +87,16 @@ static void twi_delay(unsigned char v){
8587#pragma GCC diagnostic push
8688#pragma GCC diagnostic ignored "-Wunused-but-set-variable"
8789 unsigned int reg ;
88- for (i = 0 ;i < v ;i ++ ) reg = GPI ;
90+ for (i = 0 ;i < v ;i ++ )
91+ reg = GPI ;
8992#pragma GCC diagnostic pop
9093}
9194
9295static bool twi_write_start (void ) {
9396 SCL_HIGH ();
9497 SDA_HIGH ();
95- if (SDA_READ () == 0 ) return false;
98+ if (SDA_READ () == 0 )
99+ return false;
96100 twi_delay (twi_dcount );
97101 SDA_LOW ();
98102 twi_delay (twi_dcount );
@@ -116,8 +120,10 @@ static bool twi_write_stop(void){
116120static bool twi_write_bit (bool bit ) {
117121 uint32_t i = 0 ;
118122 SCL_LOW ();
119- if (bit ) SDA_HIGH ();
120- else SDA_LOW ();
123+ if (bit )
124+ SDA_HIGH ();
125+ else
126+ SDA_LOW ();
121127 twi_delay (twi_dcount + 1 );
122128 SCL_HIGH ();
123129 while (SCL_READ () == 0 && (i ++ ) < twi_clockStretchLimit );// Clock stretching
@@ -149,25 +155,30 @@ static bool twi_write_byte(unsigned char byte) {
149155static unsigned char twi_read_byte (bool nack ) {
150156 unsigned char byte = 0 ;
151157 unsigned char bit ;
152- for (bit = 0 ; bit < 8 ; bit ++ ) byte = (byte << 1 ) | twi_read_bit ();
158+ for (bit = 0 ; bit < 8 ; bit ++ )
159+ byte = (byte << 1 ) | twi_read_bit ();
153160 twi_write_bit (nack );
154161 return byte ;
155162}
156163
157164unsigned char twi_writeTo (unsigned char address , unsigned char * buf , unsigned int len , unsigned char sendStop ){
158165 unsigned int i ;
159- if (!twi_write_start ()) return 4 ;//line busy
166+ if (!twi_write_start ())
167+ return 4 ;//line busy
160168 if (!twi_write_byte (((address << 1 ) | 0 ) & 0xFF )) {
161- if (sendStop ) twi_write_stop ();
169+ if (sendStop )
170+ twi_write_stop ();
162171 return 2 ; //received NACK on transmit of address
163172 }
164173 for (i = 0 ; i < len ; i ++ ) {
165174 if (!twi_write_byte (buf [i ])) {
166- if (sendStop ) twi_write_stop ();
175+ if (sendStop )
176+ twi_write_stop ();
167177 return 3 ;//received NACK on transmit of data
168178 }
169179 }
170- if (sendStop ) twi_write_stop ();
180+ if (sendStop )
181+ twi_write_stop ();
171182 i = 0 ;
172183 while (SDA_READ () == 0 && (i ++ ) < 10 ){
173184 SCL_LOW ();
@@ -180,14 +191,18 @@ unsigned char twi_writeTo(unsigned char address, unsigned char * buf, unsigned i
180191
181192unsigned char twi_readFrom (unsigned char address , unsigned char * buf , unsigned int len , unsigned char sendStop ){
182193 unsigned int i ;
183- if (!twi_write_start ()) return 4 ;//line busy
194+ if (!twi_write_start ())
195+ return 4 ;//line busy
184196 if (!twi_write_byte (((address << 1 ) | 1 ) & 0xFF )) {
185- if (sendStop ) twi_write_stop ();
197+ if (sendStop )
198+ twi_write_stop ();
186199 return 2 ;//received NACK on transmit of address
187200 }
188- for (i = 0 ; i < (len - 1 ); i ++ ) buf [i ] = twi_read_byte (false);
201+ for (i = 0 ; i < (len - 1 ); i ++ )
202+ buf [i ] = twi_read_byte (false);
189203 buf [len - 1 ] = twi_read_byte (true);
190- if (sendStop ) twi_write_stop ();
204+ if (sendStop )
205+ twi_write_stop ();
191206 i = 0 ;
192207 while (SDA_READ () == 0 && (i ++ ) < 10 ){
193208 SCL_LOW ();
0 commit comments