Quality RTOS & Embedded Software

 Real time embedded FreeRTOS RSS feed 
Quick Start Supported MCUs PDF Books Trace Tools Ecosystem


Loading

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;


[ Back to the top ]    [ About FreeRTOS ]    [ Privacy ]    [ Sitemap ]    [ ]


Copyright (C) Amazon Web Services, Inc. or its affiliates. All rights reserved.

Latest News

NXP tweet showing LPC5500 (ARMv8-M Cortex-M33) running FreeRTOS.

Meet Richard Barry and learn about running FreeRTOS on RISC-V at FOSDEM 2019

Version 10.1.1 of the FreeRTOS kernel is available for immediate download. MIT licensed.

View a recording of the "OTA Update Security and Reliability" webinar, presented by TI and AWS.


Careers

FreeRTOS and other embedded software careers at AWS.



FreeRTOS Partners

ARM Connected RTOS partner for all ARM microcontroller cores

Espressif ESP32

IAR Partner

Microchip Premier RTOS Partner

RTOS partner of NXP for all NXP ARM microcontrollers

Renesas

STMicro RTOS partner supporting ARM7, ARM Cortex-M3, ARM Cortex-M4 and ARM Cortex-M0

Texas Instruments MCU Developer Network RTOS partner for ARM and MSP430 microcontrollers

OpenRTOS and SafeRTOS

Xilinx Microblaze and Zynq partner