Join GitHub today
GitHub is home to over 28 million developers working together to host and review code, manage projects, and build software together.Sign up
STM32: Correct I2C master error handling #4012
If I2C slave support is included, then the I2C error handler would always reset the I2C address, resulting in incorrectly changing the I2C state to listen for a controller configured as I2C master. This change conditionalizes the address setting to only occur if the controller was in slave mode when the error occurred.
Steps to test or reproduce
Create a build for an STM32 based board with DEVICE_I2CSLAVE and DEVICE_I2C_ASYNCH enabled (current default). Create an I2C master and issue a .transfer() call to a non-existent I2C device address, so that the transfer will be NACKed. HAL_I2C_ErrorCallback() will be called on the NACK and call i2c_slave_address(), resulting in the state being set to LISTEN. Subsequent I2C transfer attempts to any address will then immediately fail because i2c_active() returns 1 (true) for any state other than READY.
@LMESTM Thanks for the review. I have updated the formatting as you request (my preferred formatting, actually ; I had copied the format from the slave "if" a few lines above, but I see other "if"s in the file using the improved formatting). I also noticed that i2c_init() would reset the slave field back to 0, so I added a line just before the slave address restore to set slave field to 1, as it should be.