Hi There,
Often we use SPI or I2C with a while into some flag bits, for instance
while(I2C2CONbits.SEN); for Microchip I2C.
Question is how the best handle such while with considering that our task should not be allowed to stop in case that a component fails.
Several ways to work around, maybe the simplest is a
for (i=0; I2C2CONbits.SEN; i++) if (i>500) return ERROR;
But with an RTOS we probably can go further like
for (i=0; I2C2CONbits.SEN; i++) { if (i>5) return ERROR; vTaskDelay( 1/ portTICK_RATE_MS ); }
But in this case 1ms delay will probably be too long in most bus.
Maybe another way would be to use co-routine an create a routine for each of the sub components, if one fails then the co routine fails and a check on failed co-routine could stop them to release the bus resources ?
Of course there is always the interrupt-way but this pose other problem and the failing device always pause the same problem. In a RTOS environment I would prefer the while style.
I’d like to know how the expert are handling this the nicest way.
Thanks