Skip to content

Commit d2772f8

Browse files
committed
bug: resolve low speed I2C BAUD register overflow
1 parent e0a883c commit d2772f8

1 file changed

Lines changed: 7 additions & 1 deletion

File tree

cores/arduino/SERCOM.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -478,7 +478,13 @@ void SERCOM::initMasterWIRE( uint32_t baudrate )
478478
// Enable all interrupts
479479
// sercom->I2CM.INTENSET.reg = SERCOM_I2CM_INTENSET_MB | SERCOM_I2CM_INTENSET_SB | SERCOM_I2CM_INTENSET_ERROR ;
480480

481-
if (sercom->I2CM.CTRLA.bit.SPEED == 0x2)
481+
uint8_t speedBit = sercom->I2CM.CTRLA.bit.SPEED;
482+
uint32_t topSpeeds[3] = {400000, 1000000, 3400000}; // {(sm/fm), (fm+), (hs)}
483+
const uint32_t minBaudrate = freqRef / 512; // BAUD = 255: SAMD51 ~195kHz, SAMD21 ~94kHz
484+
const uint32_t maxBaudrate = topSpeeds[speedBit];
485+
baudrate = max(minBaudrate, min(baudrate, maxBaudrate));
486+
487+
if (speedBit == 0x2)
482488
sercom->I2CM.BAUD.bit.HSBAUD = freqRef / (2 * baudrate) - 1;
483489
else
484490
sercom->I2CM.BAUD.bit.BAUD = freqRef / (2 * baudrate) - 5 - freqRef * WIRE_RISE_TIME_NANOSECONDS / (2 * 1e9f);

0 commit comments

Comments
 (0)