I have a state machine where a task_manager controls the execution of different tasks, and suspends/resumes them according to the current state. When I resume a task, it continues from the same instruction where it was when it was suspended, as it is expected.
However, I have a task that reads and process data from a sensor and I need that every time the task is resumed it jumps back to the beginning of the for loop instead of continuing with its next instruction, in order to reset the variables to their initial values. Otherwise, when it is resumed it would carry on with the old values from the previous iteration, which would lead to wrong data.
Reviewing the FreeRTOS forum and StackOverflow I have found similar questions, with three possible near solutions, still not clear to me:
1.- Add a reset flag and check it regularly through the code or at the beginnning of the loop, as in:
Checking the flag at the beginning of the loop as in this two previous links doesn’t fit for my solution, as there will be several instructions that will still be executed before the condition is checked again, creating errors.
On the other hand, checking the flag regularly through the code would imply doing it almost before every line of code (and I have 500+), and I guess there must be a more efficient way.
2.- Call vTaskDelete and vTaskCreate again for that function. I’m not sure if this will create memory fragmentation problems, as the task might be resumed many times.
3.- Call vTaskCreate for that function to use the same stack and TCB and reset its values to the initial state.
Is there another direct and safe way to tell the resuming task that it should start at the beginning instead where it was? If not, which of this three options is the most appropiate?
Many thanks in advance,