I’m trying to pause/run execution of a task based on toggling from an interrupt (push button). I thought using a binary semaphore for this would make sense so I created my ISR to use either SemaphoreGiveFromISR() and xSemaphoreTakeFromISR() based on the value of a flag, isRunning. Within the task’s for loop I start with if(xSemaphoreTake(handle) == pdTRUE)… and end with xSemaphoreGive(handle). This does not work… I can see that the give starts the task running but the take doesn’t do anything (returns pdFALSE). I suspect this is because when the ISR runs the semaphore is actually held by the task and I interrupted the task in the middle of execution, therefore the ISR wouldn’t be able to take it.
However, the task MUST finish its execution cycle (code within the for loop) completely or the hardware I’m working with will be left in an inconsistent state the next time the task was able to run. Because of this, even if the ISR were able to stop the task from running, it should only be able to stop it if all execution of the for loop has completed. I’m still trying to wrap my mind around this so any suggestions of a better way to do this or explanations would be great!