@@ -480,26 +480,25 @@ void SERCOM::initMasterWIRE( uint32_t baudrate )
480480
481481 // Determine speed mode based on requested baudrate
482482 const uint32_t topSpeeds[3 ] = {400000 , 1000000 , 3400000 }; // {(sm/fm), (fm+), (hs)}
483- uint8_t speedBit = 0 ;
484- bool clockStretchMode = false ;
485- sercom->I2CM .CTRLA .bit .SCLSM = 0 ; // See: 28.6.2.4.6
483+ uint8_t speedBit;
484+ uint8_t clockStretchMode; // See: 28.6.2.4.6 (SERCOM I2C Highspeed mode)
485+
486+ if (baudrate <= topSpeeds[0 ]) {
487+ speedBit = 0 ; // Standard/Fast mode up to 400 khz
488+ clockStretchMode = 0 ;
489+ } else if (baudrate <= topSpeeds[1 ]) {
490+ speedBit = 1 ; // Fast mode+ up to 1 Mhz
491+ clockStretchMode = 0 ;
492+ } else {
493+ // High speed up to 3.4 Mhz
494+ speedBit = 2 ;
495+ clockStretchMode = 1 ;
496+ }
486497
487- if (baudrate > topSpeeds[0 ] && baudrate <= topSpeeds[1 ])
488- {
489- speedBit = 1 ; // Fast mode+
490- clockStretchMode = false ; // See: 28.6.2.4.6
491- }
492- else if (baudrate > topSpeeds[1 ])
493- {
494- speedBit = 2 ; // High speed
495- clockStretchMode = true ; // See: 28.6.2.4.6
496- }
497- // else speedBit = 0 and clockStretchMode = false for Standard/Fast mode (up to 400kHz)
498-
499498 sercom->I2CM .CTRLA .bit .SPEED = speedBit;
500- sercom->I2CM .CTRLA .bit .SCLSM = clockStretchMode; // See: 28.6.2.4.6
499+ sercom->I2CM .CTRLA .bit .SCLSM = clockStretchMode;
501500
502- uint32_t minBaudrate = freqRef / 512 ; // BAUD = 255: SAMD51(@100MHz) ~195kHz, SAMD21 ~94kHz
501+ uint32_t minBaudrate = freqRef / 512 ; // BAUD = 255: SAMD51(@100MHz) ~195kHz, SAMD21 ~94kHz
503502 uint32_t maxBaudrate = topSpeeds[speedBit];
504503 baudrate = max (minBaudrate, min (baudrate, maxBaudrate));
505504
0 commit comments