This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

I2C while statements

In the I2C example code, there are a few statements such as the one below: 

    UCB0CTL1 |= UCTXSTT;                    // I2C start condition
while (UCB0CTL1 & UCTXSTT); // Start condition sent?

Are these while statements considered safe - the program doesn't hang here if there's a problem?  When debugging, my program frequently hangs on these statements - usually it means something else is wrong - but for the life of a product, I worry that a mishandled byte could leave the MCU waiting forever for response to a start, stop, transmit, or receive.  

I could certainly use the watchdog timer, but I'm wondering if it's wiser to write something besides a while statement to check for the I2C response.  Thanks for any feedback!

  • BuffaloEngineer said:
    Are these while statements considered safe - the program doesn't hang here if there's a problem?

    If for some reason the slave pulls SCL down (clock stretching, maybe a 'hold master' feature'), this while may loop forever. To be sure, other things like the bus low flag can be tested, or maybe even a timeout may be put into the while. However, if SCL is permanently low, then the bus is stalled and no software can revive it. And so your application is stalled anyway (in most cases).

**Attention** This is a public forum