ST HAL I2C address (SHIFTED!)

stlogo

Quick note, for those who might be banging their head, like I did.

ST HAL library 7 bit I2C address has to be shifted before passed to their functions.

EDIT: Let me google that for you situation. I’ve been working with the official ST documentation, when I just had to google to find this beautiful blog post from 2 years ago.

Explanation

Using the simplest API call to send something to device 0x3E:

/**ST HAL handler, address, data, size, timeout*/
HAL_I2C_Master_Transmit(hi2c, 
                        0x3E, 
                        buffer, 
                        bufferSize, 
                        timeout);

And plugging a Saleae logic analyzer, I get:

I2C Capture

I2C Capture

Surprise, Surprise Mother******, 0x1F for you.

This has a simple explanation that escapes my comprehension. The API writes directly to I2C CR2 register (check the reference manual for your ST µC) without shifting. Isn’t the goal of an API to be clear and easy?. This is not stated anywhere and I lost a good amount of time tracking this problem.

I2C CR2 Register

I2C CR2 Register

My quick solution:

#define I2C_ADDRESS_ST(a,m) (((m) == I2C_ADDRESSINGMODE_7BIT) ? (a << 1) : (a))

Hope that if this happens to you, you find this post, or Jures’ one before wasting too much time.

Advertisements

Leave a comment

Filed under code, curious

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s