I spent today trying to troubleshoot a bug in my code for writing/reading an Atmel at24cm01 EEPROM chip via I2C on my Tiva TM4C1230C3PM running at 50Mhz, with the I2C clock in slow mode (100 Kbps).
I discovered a few helpful posts on this forum, mostly from Amit Ashara, giving the advice to wait on both while(!I2CMasterBusy()) as well as while(I2CMasterBusy()) after calling I2CMasterControl(), instead of just waiting on while(I2CMasterBusy()). This lead me to my solution, but didn't quite work out in my case since sometimes the program would hang forever in the loop while(!I2CMasterBusy()).
Instead, the solution that worked for me was to have a SystemCtlDelay(100) before every while(I2CMasterBusy()) loop, to allow some time for the I2C busy flag to be set before waiting, but not loop infinitely. This works consistently for me.
I can post my relevant code if need be, but my question is about the general case: how can I figure out the shortest safe argument for the call to SystemCtlDelay(), where I am still giving enough time for the busy flag to be set. I've tried with SystemCtlDelay(10) (30 cycles) and it seems to work consistently, but I bumped it up to 100 to be on the safe side since 300 clock cycles isn't a crazy-long time to wait.
Is this something I can only figure out by experimentation or is there some math that will lead me to a safe but short delay time?
Thanks!