STM32F7 I2C HAL

I needed to interface an I2C slave with an STM32F7xx microcontroller without using the STM32Cube HAL. I’d like to share my code to save you the time should you have to do the same thing.

The code provides basic I2C features. No interrupts, no fancy things. Just a standalone HAL to use the I2C peripheral of an STM32F7 microcontroller.

Currently the following functions are available:

bool_t     i2cInit(I2C_TypeDef* i2c);
void       i2cSend(I2C_TypeDef* i2c, uint8_t slaveAddr, uint8_t* data, uint16_t length);
void       i2cSendByte(I2C_TypeDef* i2c, uint8_t slaveAddr, uint8_t data);
void       i2cWriteReg(I2C_TypeDef* i2c, uint8_t slaveAddr, uint8_t regAddr, uint8_t value);
void       i2cRead(I2C_TypeDef* i2c, uint8_t slaveAddr, uint8_t* data, uint16_t length);
uint8_t    i2cReadByte(I2C_TypeDef* i2c, uint8_t slaveAddr, uint8_t regAddr);
uint16_t   i2cReadWord(I2C_TypeDef* i2c, uint8_t slaveAddr, uint8_t regAddr);

You can find the code over at BitBucket: Link
The same code should also work for other STM32 microcontrollers such as the F0 and F3 series as they use the same I2C peripheral.

Leave a Comment

Your email address will not be published.