Skip to content

Commit b753095

Browse files
committed
Correct I2C frequency calculation formula
Allow variant to override the default rise time in nanoseconds via define
1 parent a7c33fe commit b753095

File tree

1 file changed

+7
-1
lines changed

1 file changed

+7
-1
lines changed

cores/arduino/SERCOM.cpp

+7-1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,12 @@
1919
#include "SERCOM.h"
2020
#include "variant.h"
2121

22+
#ifndef WIRE_RISE_TIME_NANOSECONDS
23+
// Default rise time in nanoseconds, based on 4.7K ohm pull up resistors
24+
// you can override this value in your variant if needed
25+
#define WIRE_RISE_TIME_NANOSECONDS 125
26+
#endif
27+
2228
SERCOM::SERCOM(Sercom* s)
2329
{
2430
sercom = s;
@@ -445,7 +451,7 @@ void SERCOM::initMasterWIRE( uint32_t baudrate )
445451
// sercom->I2CM.INTENSET.reg = SERCOM_I2CM_INTENSET_MB | SERCOM_I2CM_INTENSET_SB | SERCOM_I2CM_INTENSET_ERROR ;
446452

447453
// Synchronous arithmetic baudrate
448-
sercom->I2CM.BAUD.bit.BAUD = SystemCoreClock / ( 2 * baudrate) - 1 ;
454+
sercom->I2CM.BAUD.bit.BAUD = SystemCoreClock / ( 2 * baudrate) - 5 - (((SystemCoreClock / 1000000) * WIRE_RISE_TIME_NANOSECONDS) / (2 * 1000));
449455
}
450456

451457
void SERCOM::prepareNackBitWIRE( void )

0 commit comments

Comments
 (0)