File tree 1 file changed +32
-1
lines changed
1 file changed +32
-1
lines changed Original file line number Diff line number Diff line change @@ -259,9 +259,40 @@ uint8_t Adafruit_I2CDevice::address(void) { return _addr; }
259
259
* Not necessarily that the speed was achieved!
260
260
*/
261
261
bool Adafruit_I2CDevice::setSpeed (uint32_t desiredclk) {
262
- #if (ARDUINO >= 157) && !defined(ARDUINO_STM32_FEATHER) && !defined(TinyWireM_h)
262
+ #if defined(__AVR__) // fix arduino core set clock
263
+ // calculate TWBR correctly
264
+ uint8_t prescaler = 1 ;
265
+ uint32_t atwbr = ((F_CPU / desiredclk) - 16 ) / 2 ;
266
+ if (atwbr <= 255 ) {
267
+ prescaler = 1 ;
268
+ TWSR = 0x0 ;
269
+ } else if (atwbr <= 1020 ) {
270
+ atwbr /= 4 ;
271
+ prescaler = 4 ;
272
+ TWSR = 0x1 ;
273
+ } else if (atwbr <= 4080 ) {
274
+ atwbr /= 16 ;
275
+ prescaler = 16 ;
276
+ TWSR = 0x2 ;
277
+ } else if (atwbr <= 16320 ) {
278
+ atwbr /= 64 ;
279
+ prescaler = 64 ;
280
+ TWSR = 0x3 ;
281
+ }
282
+ #ifdef DEBUG_SERIAL
283
+ Serial.print (F (" TWSR prescaler = " ));
284
+ Serial.println (prescaler);
285
+ Serial.print (F (" TWBR = " ));
286
+ Serial.println (atwbr);
287
+ #endif
288
+ TWBR = atwbr;
289
+ return true ;
290
+
291
+ #elif (ARDUINO >= 157) && !defined(ARDUINO_STM32_FEATHER) && \
292
+ !defined (TinyWireM_h)
263
293
_wire->setClock (desiredclk);
264
294
return true ;
295
+
265
296
#else
266
297
(void )desiredclk;
267
298
return false ;
You can’t perform that action at this time.
0 commit comments