Skip to content

Commit 66f851a

Browse files
committed
Configure I2C Fast Mode when I2C freq is more than 100kHz
For STM32 families that have a DutyCycle init parameter, we need to select the Fast Mode dedicated configuration which will set the fast mode bit in CCR register.
1 parent b043838 commit 66f851a

File tree

1 file changed

+12
-1
lines changed

1 file changed

+12
-1
lines changed

cores/arduino/stm32/twi.c

+12-1
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,12 @@ void i2c_custom_init(i2c_t *obj, i2c_timing_e timing, uint32_t addressingMode, u
263263
handle->Init.Timing = timing;
264264
#else
265265
handle->Init.ClockSpeed = timing;
266-
handle->Init.DutyCycle = I2C_DUTYCYCLE_2;
266+
/* Standard mode (sm) is up to 100kHz, then it's Fast mode (fm) */
267+
/* In fast mode duty cyble bit must be set in CCR register */
268+
if(timing > 100000)
269+
handle->Init.DutyCycle = I2C_DUTYCYCLE_16_9;
270+
else
271+
handle->Init.DutyCycle = I2C_DUTYCYCLE_2;
267272
#endif
268273
handle->Init.OwnAddress1 = ownAddress;
269274
handle->Init.OwnAddress2 = 0xFF;
@@ -331,6 +336,12 @@ void i2c_setTiming(i2c_t *obj, uint32_t frequency)
331336
obj->handle.Init.Timing = f;
332337
#else
333338
obj->handle.Init.ClockSpeed = f;
339+
/* Standard mode (sm) is up to 100kHz, then it's Fast mode (fm) */
340+
/* In fast mode duty cyble bit must be set in CCR register */
341+
if(frequency > 100000)
342+
obj->handle.Init.DutyCycle = I2C_DUTYCYCLE_16_9;
343+
else
344+
obj->handle.Init.DutyCycle = I2C_DUTYCYCLE_2;
334345
#endif
335346
/*
336347
else if(frequency <= 600000)

0 commit comments

Comments
 (0)