Troubleshooting: I²C on STM32

One (not) good day you may have decided to use the I²C interface of your STM32 MCU but you cannot get it working. Your I²C slave does not response at all or you're getting garbage. This guide lists very common issues. Please step through each point and make sure you got things right.

Check first

  • Ensure that you're using external pullups. The internal pullup resistors of the STM32 are too weak, never use them for I²C. Please note that it's important that your resistor values match the speed of your I²C bus. You may not use more than 10kΩ for 100kHz and note more than 4,7kΩ for 400kHz. If you go up to 1MHz you mustn't use more than 2kΩ.
  • Ensure SDA/SCL lines not swapped.
  • Ensure sure the PAL mode of both, the SDA and the SCL line, are set to alternate function and open drain. You can find some examples below.
  • Ensure that you're using the proper I²C driver (For example, you may not use the I2CD1 if your slave is connected to the I2CD2).
  • You may use the provided diagnostic functions of the I²C API in order to get some useful return codes. You may also use the driver routine i2cGetErrors() to gain some more informations.
  • Look for the I²C examples inside the testhal directory. They are much simpler then the projects in the demos folder.

Examples of the required PAL settings

For STM32F1xx they are:

palSetPadMode(GPIOB, 6, PAL_MODE_STM32_ALTERNATE_OPENDRAIN);   /* SCL */
palSetPadMode(GPIOB, 7, PAL_MODE_STM32_ALTERNATE_OPENDRAIN);   /* SDA */

For STM32F4xx they are:

palSetPadMode(GPIOB, 8, PAL_MODE_ALTERNATE(4) | PAL_STM32_OTYPE_OPENDRAIN);   /* SCL */
palSetPadMode(GPIOB, 9, PAL_MODE_ALTERNATE(4) | PAL_STM32_OTYPE_OPENDRAIN);   /* SDA */

Still no luck?

If you checked all the points listed above but you still doesn't seem to get it working, please try to make sure that your hardware is not broken. You might want to use a scope or a logic analyzer in order to check if the master is sending any data. In case of the slave, you might want to replace it by another chip when your hardware design allows it. Otherwise, feel free to ask people for help in our discussions forum. Please give as many details as possible.

Useful links

The following lists a few links which might be helpful to understand how I²C works and therefore track down your issues.

 
chibios/community/guides/i2c_trouble_shooting.txt · Last modified: 2014/02/25 11:09 by tectu
 
Except where otherwise noted, content on this wiki is licensed under the following license:GNU Free Documentation License 1.3