@@ -45,13 +45,15 @@ static uint32_t twi_clockStretchLimit;
45
45
46
46
void twi_setClock (unsigned int freq ){
47
47
#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
49
50
else if (freq <= 200000 ) twi_dcount = 8 ;//about 200KHz
50
51
else if (freq <= 300000 ) twi_dcount = 3 ;//about 300KHz
51
52
else if (freq <= 400000 ) twi_dcount = 1 ;//about 400KHz
52
53
else twi_dcount = 1 ;//about 400KHz
53
54
#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
55
57
else if (freq <= 200000 ) twi_dcount = 14 ;//about 200KHz
56
58
else if (freq <= 300000 ) twi_dcount = 8 ;//about 300KHz
57
59
else if (freq <= 400000 ) twi_dcount = 5 ;//about 400KHz
@@ -85,14 +87,16 @@ static void twi_delay(unsigned char v){
85
87
#pragma GCC diagnostic push
86
88
#pragma GCC diagnostic ignored "-Wunused-but-set-variable"
87
89
unsigned int reg ;
88
- for (i = 0 ;i < v ;i ++ ) reg = GPI ;
90
+ for (i = 0 ;i < v ;i ++ )
91
+ reg = GPI ;
89
92
#pragma GCC diagnostic pop
90
93
}
91
94
92
95
static bool twi_write_start (void ) {
93
96
SCL_HIGH ();
94
97
SDA_HIGH ();
95
- if (SDA_READ () == 0 ) return false;
98
+ if (SDA_READ () == 0 )
99
+ return false;
96
100
twi_delay (twi_dcount );
97
101
SDA_LOW ();
98
102
twi_delay (twi_dcount );
@@ -116,8 +120,10 @@ static bool twi_write_stop(void){
116
120
static bool twi_write_bit (bool bit ) {
117
121
uint32_t i = 0 ;
118
122
SCL_LOW ();
119
- if (bit ) SDA_HIGH ();
120
- else SDA_LOW ();
123
+ if (bit )
124
+ SDA_HIGH ();
125
+ else
126
+ SDA_LOW ();
121
127
twi_delay (twi_dcount + 1 );
122
128
SCL_HIGH ();
123
129
while (SCL_READ () == 0 && (i ++ ) < twi_clockStretchLimit );// Clock stretching
@@ -149,25 +155,30 @@ static bool twi_write_byte(unsigned char byte) {
149
155
static unsigned char twi_read_byte (bool nack ) {
150
156
unsigned char byte = 0 ;
151
157
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 ();
153
160
twi_write_bit (nack );
154
161
return byte ;
155
162
}
156
163
157
164
unsigned char twi_writeTo (unsigned char address , unsigned char * buf , unsigned int len , unsigned char sendStop ){
158
165
unsigned int i ;
159
- if (!twi_write_start ()) return 4 ;//line busy
166
+ if (!twi_write_start ())
167
+ return 4 ;//line busy
160
168
if (!twi_write_byte (((address << 1 ) | 0 ) & 0xFF )) {
161
- if (sendStop ) twi_write_stop ();
169
+ if (sendStop )
170
+ twi_write_stop ();
162
171
return 2 ; //received NACK on transmit of address
163
172
}
164
173
for (i = 0 ; i < len ; i ++ ) {
165
174
if (!twi_write_byte (buf [i ])) {
166
- if (sendStop ) twi_write_stop ();
175
+ if (sendStop )
176
+ twi_write_stop ();
167
177
return 3 ;//received NACK on transmit of data
168
178
}
169
179
}
170
- if (sendStop ) twi_write_stop ();
180
+ if (sendStop )
181
+ twi_write_stop ();
171
182
i = 0 ;
172
183
while (SDA_READ () == 0 && (i ++ ) < 10 ){
173
184
SCL_LOW ();
@@ -180,14 +191,18 @@ unsigned char twi_writeTo(unsigned char address, unsigned char * buf, unsigned i
180
191
181
192
unsigned char twi_readFrom (unsigned char address , unsigned char * buf , unsigned int len , unsigned char sendStop ){
182
193
unsigned int i ;
183
- if (!twi_write_start ()) return 4 ;//line busy
194
+ if (!twi_write_start ())
195
+ return 4 ;//line busy
184
196
if (!twi_write_byte (((address << 1 ) | 1 ) & 0xFF )) {
185
- if (sendStop ) twi_write_stop ();
197
+ if (sendStop )
198
+ twi_write_stop ();
186
199
return 2 ;//received NACK on transmit of address
187
200
}
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);
189
203
buf [len - 1 ] = twi_read_byte (true);
190
- if (sendStop ) twi_write_stop ();
204
+ if (sendStop )
205
+ twi_write_stop ();
191
206
i = 0 ;
192
207
while (SDA_READ () == 0 && (i ++ ) < 10 ){
193
208
SCL_LOW ();
0 commit comments