From f8a5a29e6e38061646be18881ebba7fa4898a4bd Mon Sep 17 00:00:00 2001 From: Laurent Meunier Date: Mon, 13 Aug 2018 10:33:37 +0200 Subject: [PATCH] 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. --- cores/arduino/stm32/twi.c | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/cores/arduino/stm32/twi.c b/cores/arduino/stm32/twi.c index ccd633fb40..4ed2eb8e04 100644 --- a/cores/arduino/stm32/twi.c +++ b/cores/arduino/stm32/twi.c @@ -261,7 +261,12 @@ void i2c_custom_init(i2c_t *obj, i2c_timing_e timing, uint32_t addressingMode, u handle->Init.Timing = timing; #else handle->Init.ClockSpeed = timing; - handle->Init.DutyCycle = I2C_DUTYCYCLE_2; + /* Standard mode (sm) is up to 100kHz, then it's Fast mode (fm) */ + /* In fast mode duty cyble bit must be set in CCR register */ + if(timing > 100000) + handle->Init.DutyCycle = I2C_DUTYCYCLE_16_9; + else + handle->Init.DutyCycle = I2C_DUTYCYCLE_2; #endif handle->Init.OwnAddress1 = ownAddress; handle->Init.OwnAddress2 = 0xFF; @@ -326,6 +331,12 @@ void i2c_setTiming(i2c_t *obj, uint32_t frequency) obj->handle.Init.Timing = f; #else obj->handle.Init.ClockSpeed = f; + /* Standard mode (sm) is up to 100kHz, then it's Fast mode (fm) */ + /* In fast mode duty cyble bit must be set in CCR register */ + if(frequency > 100000) + obj->handle.Init.DutyCycle = I2C_DUTYCYCLE_16_9; + else + obj->handle.Init.DutyCycle = I2C_DUTYCYCLE_2; #endif /* else if(frequency <= 600000)