FreeRTOS Support Archive
The FreeRTOS support forum is used to obtain active support directly from Real
Time Engineers Ltd. In return for using our top quality software and services for
free, we request you play fair and do your bit to help others too! Sign up
to receive notifications of new support topics then help where you can.
This is a read only archive of threads posted to the FreeRTOS support forum.
The archive is updated every week, so will not always contain the very latest posts.
Use these archive pages to search previous posts. Use the Live FreeRTOS Forum
link to reply to a post, or start a new support thread.
[FreeRTOS Home] [Live FreeRTOS Forum] [FAQ] [Archive Top] [January 2015 Threads] ISR to take/give binary semaphore to pause task execution?Posted by pugglewuggle on January 8, 2015 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!
ISR to take/give binary semaphore to pause task execution?Posted by richard_damon on January 8, 2015 You could have the task take the semaphore and then immediately give it back, thus the only point where the ISR couldn't pause the task is that small window.
The other option is just use the global variable isRunning, and if it is clear, pend on the semaphore, and if set just keep will. Then the ISR to stop the task clears isRunning, and to start running sets isRunning and gives the semaphore to restart the task if it is pending on it.
ISR to take/give binary semaphore to pause task execution?Posted by pugglewuggle on January 8, 2015 Can you please explain the second part?
ISR to take/give binary semaphore to pause task execution?Posted by richard_damon on January 9, 2015 Global variable run and semaphore seem.
ISR to stop running:
run = 0;
ISR to resume running:
run = 1;
give seem;
Task at the top of the loop:
while(!run) wait for sema;
Copyright (C) Amazon Web Services, Inc. or its affiliates. All rights reserved.
|