Skip to content

Commit

Permalink
Builds stm32 I2C driver for F0, fixes timings. (#662)
Browse files Browse the repository at this point in the history
- Adds the STM32I2C.cxx to the sources built in the f091 tree.
- Separates the timing settings for each stm32 family member we have.
- Adds correct timings for 48, 72, 80 and 216 MHz chips for 400 kHz I2C frequency.
  • Loading branch information
balazsracz authored Oct 7, 2022
1 parent 906871a commit a8d1910
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 5 deletions.
31 changes: 26 additions & 5 deletions src/freertos_drivers/st/Stm32I2C.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,37 @@

#if defined(STM32F072xB) || defined(STM32F091xC)
#include "stm32f0xx_ll_rcc.h"
#include "stm32f0xx_ll_i2c.h"

// This timing is assuming 48 MHz main clock, the I2C module being clocked from
// the main clock, and gives 400 kHz clock (fast mode).
#define I2C_TIMING (__LL_I2C_CONVERT_TIMINGS(5, 0x3, 0x3, 0x3, 0x9))

#elif defined(STM32F103xB)
#include "stm32f1xx_ll_rcc.h"
#elif defined(STM32F303xC) || defined(STM32F303xE)
#include "stm32f3xx_ll_rcc.h"

// This timing is assuming 72 MHz main clock, the I2C module being clocked from
// the main clock, and gives 400 kHz clock (fast mode).
#define I2C_TIMING (__LL_I2C_CONVERT_TIMINGS(8, 0x3, 0x3, 0x3, 0x9))

#elif defined(STM32L431xx) || defined(STM32L432xx)
#include "stm32l4xx_ll_rcc.h"
#include "stm32l4xx_ll_i2c.h"

// This timing is assuming 80 MHz main clock, the I2C module being clocked from
// the main clock, and gives 400 kHz clock (fast mode).
#define I2C_TIMING (__LL_I2C_CONVERT_TIMINGS(9, 0x3, 0x3, 0x3, 0x9))

#elif defined(STM32F767xx)
#include "stm32f7xx_ll_rcc.h"
#include "stm32f7xx_ll_i2c.h"

// This timing is assuming 216 MHz main clock, the I2C module being clocked
// from the main clock, and gives 400 kHz clock (fast mode).
#define I2C_TIMING (__LL_I2C_CONVERT_TIMINGS(8, 9, 9, 9, 27))

#else
#error Dont know what STM32 chip you have.
#endif
Expand All @@ -68,7 +89,9 @@ static void i2c_reset(I2C_TypeDef *port)
#endif
#ifdef I2C2
case I2C2_BASE:
#ifdef LL_RCC_I2C2_CLKSOURCE_SYSCLK
LL_RCC_SetI2CClockSource(LL_RCC_I2C2_CLKSOURCE_SYSCLK);
#endif
__HAL_RCC_I2C2_CLK_ENABLE();
__HAL_RCC_I2C2_FORCE_RESET();
__HAL_RCC_I2C2_RELEASE_RESET();
Expand Down Expand Up @@ -125,10 +148,6 @@ static void i2c_reset(I2C_TypeDef *port)
}
}

// This timing is assuming 72 MHz main clock, the I2C module being clocked from
// the main clock, and gives 400 kHz clock (fast mode).
#define I2C_TIMING (__LL_I2C_CONVERT_TIMINGS(8, 0x3, 0x3, 0x3, 0x9))

/** Constructor.
* @param name name of this device instance in the file system
* @param port hardware instance of this device, e.g. I2C1
Expand Down Expand Up @@ -162,9 +181,11 @@ Stm32I2C::Stm32I2C(const char *name, I2C_TypeDef *port, uint32_t ev_interrupt,
// call above.
i2cHandle_.Init.Timing = (uint32_t) this;

#ifdef configKERNEL_INTERRUPT_PRIORITY // cortex-m3 or more
SetInterruptPriority((IRQn_Type)ev_interrupt, configKERNEL_INTERRUPT_PRIORITY);
HAL_NVIC_EnableIRQ((IRQn_Type)ev_interrupt);
SetInterruptPriority((IRQn_Type)er_interrupt, configKERNEL_INTERRUPT_PRIORITY);
#endif
HAL_NVIC_EnableIRQ((IRQn_Type)ev_interrupt);
HAL_NVIC_EnableIRQ((IRQn_Type)er_interrupt);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ CSRCS += stm32f0xx_hal.c \
CXXSRCS += Stm32Can.cxx \
Stm32Uart.cxx \
Stm32SPI.cxx \
Stm32I2C.cxx \
Stm32EEPROMEmulation.cxx \
Stm32RailcomSender.cxx

Expand Down

0 comments on commit a8d1910

Please sign in to comment.