I2C addressing on STM32F4-Discovery

In past month STM published new set of tools and software examples that implement HAL (wikipedia: Hardware abstraction layer) on the many of theirs µC. They are calling this set of tools STM32CubeF4. This post is a little public note to myself and all others who are banging or will be banging their heads at wall because they aren’t able to make I2C communication to work.

stm32f4cube

I2C addressing (not the 10bit, i am talking about 7bit) has only 7 bits because one bit of 8 bit addressing space is reserved for flag denoting if operation will be reading or writing. That one bit is LSB (Least significant bit). If you are into details, you can more here.

i2c-address1

Till now i worked with libraries that input a 7bit address and they automatically shift it for one bit to left. After that libs set LSB to the right value depending on the operation.

 STM32CubeF4’s way

(At the time writting this: HAL version is 1.1.0 )

Long story short: You have to shift the 7 bit address yourself. You can see two functions from my I2C library that are more or less self explanatory – note the shifted addresses  (uint16_t)I2C_ADDRESS<<1 

 

And ReadBuffer :

 

I hope i saved you an hour or two :-).