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