cancel
Showing results for 
Search instead for 
Did you mean: 

I2C Question

Pje
Visitor

Hi All,

I am new to programming in general and I want to ask what is the best practices to timeout on I2C bus communication  Appreciate any ideas and methods ,As I am starting a project with I2C communication for a production device and need to ensure that the communication is stable and not getting blocked by any means.

 

Regards 

4 REPLIES 4
Danish1
Lead II

The first question is whether your stm32 will be the only master in your system. I2C is capable of being used in “multi-master“ systems although it is rare, and places extra responsibility on all masters including the stm32.

The most common “lockup” I experience with I2C is when I reset the stm32, either during debugging or due to a crash in my code. If that happens part-way through a byte then a peripheral might still want to send the remainder of the byte and so drive SDA low. The easiest way to fix this is, before using I2C for the first time coming out of reset, to “clock out” the byte by manually bit-banging 9 low pulses on SCL before issuing a “Stop Condition”. This will cleanly tell all peripherals to stop ongoing communication and be ready.

Of course you mustn’t do that if a different master is using the I2C bus.

Sadly this “clocking out” has to be done manually before enabling the I2C peripheral with HAL or your own code

Pje
Visitor

Thank you for your reply the STM32 is the only master on the Bus,

I will get going with it and I am sure I will be back to ask some more questions

 

Regards

lilyanne
Visitor

For reliable I2C communication, implementing a timeout mechanism is crucial. You can set a timeout value for your I2C operations to ensure they don't get blocked indefinitely. Consider using timeouts in your code to handle scenarios where communication stalls or fails to respond within expected timeframes.

Thank you very much !

What would you recommned to use the SysTick Timer Intterupt or any general purpose timer that checks if the I2C has cleared a flag that it came out of the I2C transfers .

This is all new to me fisrt time on I2C ,I have good communication but I have to ensure a robust operation.

Regards