sterossi84 wrote on Tuesday, March 27, 2018:
Hi guys,
I’m new to FreeRTOS and I’m using some drivers developed for bare-metal single-threaded environment.
Some drivers (e.g. I2C EEPROM device driver) poll related devices for events with timeout guard.
Time-out check is implemented using system tick value.
In my current implementation I’ve set-up a task that acts as gatekeeper for device.
Task user can issue a device operation (e.g. EEPROM blanking to 0xFF) and task performs it without stalling the other system tasks.
My doubt is about task preemption: higher priority task can preempt device gatekeeper and use CPU time. This behaviour could lead to device driver time-out: since the latter is tick-based the time-out could expire due to other task execution.
My current solution - that actually doesn’t satisfy me - has been to enlarge device time-out to cope for other task preemption.
Is there a best way to do it / to avoid the problem without recurring to critical sections?
Below is a brief code snippets to better explain my question.
If a preemption occurs soon after I2C_Read and gatekeeper task is interrupted for more than device time-out it will result in a device timeout even if this is not true.
/*
* Poll EEPROM for write Op end
* note: Executed inside EEPROM gatekeeper task context
*/
{
BaseType_t tWrite = xTaskGetTickCount();
uint8_t rB[3] = {0U};
/*
* Wait Operation End:
* Device replies an I2C NACK for each request till write is in progress
* Read at address 0x000000 to check NACK answer
*/
while(I2C_Read(EEPROM_I2C_PORT, EEPROM_I2C_ADDR, &rB[0], 0x03U) != I2C_RC_OK)
{
if( (xTaskGetTickCount() - tWrite) >= EEPROM_TOUT_Ticks )
{
return (retval | EEPROM_RC_TOUT);
}
}
}