2025-04-10 1:56 AM
Setup: I have two F103 MCUs connected via I2C. All pins are configured correctly with STM32CubeMX on both devices and boilerplate code was generated with it. I have gdb-multiarch connected to the Master MCU using openocd as an interface for debugging. The program loads and runs and I am able to use the debugger without issues.
Problem: When the slave is not connected to the master, calling HAL_I2C_Master_Transmit with the Master MCU returns HAL_ERROR, which to my knowledge, is expected. However when I connect the Slave MCU the first time HAL_I2C_Master_Transmit is called it returns HAL_ERROR and on subsequent calls it returns HAL_BUSY. Whenever I disconnect and reconnect the Slave, the behaviour reoccurs. I only reconnect the slave (full reset), Master stays up.
Code: The code is attached with the post. I have stripped out all initialization function definitions because they are auto-generated so the problem can't really be there. I have added comments to explain what the code should do.
I don't think the problem can be the known busy flag silicon bug with F10x boards because I don't reset the Master at any point.
Thanks for help in advance and if you need extra info, just ask :).
2025-04-10 6:15 PM - edited 2025-04-10 6:17 PM
Use HAL_I2C_IsDeviceReady and ensure it returns HAL_OK before using any other HAL_I2C_ function.
The ErrorCode member of the handle will gives clues as to what went wrong.
Use a logic analyzer to see what's happening on the lines.
At a wild guess, it sounds like the slave might have buggy code and could be stretching the clock.