Simple Semaphore as task delay with interrupt

Insomniac
Posts: 2
Joined: Fri Nov 23, 2018 2:55 pm

Simple Semaphore as task delay with interrupt

Postby Insomniac » Fri Feb 21, 2020 11:00 pm

Hi,

I´m using a vTaskDelay right now to suspend my task for a while, but not being able to unblock before time runs up is a problem.
After reading a while, it seems a binary semaphore is a good solution. I have taken a look on the example of xSemaphoreGiveFromISR
https://www.freertos.org/a00124.html
, which seems to be a bit more then I need. I now deleted everything not necessary (from what I can say), but as I am not familiar with semaphores, I´d like to get a feedback if there could be a problem the way I handled it:

Code: Select all

#include <Arduino.h>

#define LONG_TIME 5000

SemaphoreHandle_t xSemaphore = NULL;

void vATask( void * pvParameters );

void setup() {  
    Serial.begin(115200);

    xTaskCreate(vATask, "vATask Loop", 1000, NULL, 1, NULL);
}

void loop() {
  delay(1000);
  /* if (xSemaphore != NULL)
    xSemaphoreGiveFromISR( xSemaphore, NULL ); */
}

void vATask( void * pvParameters )
{
    xSemaphore = xSemaphoreCreateBinary();
    while(1)
    {
        Serial.println("Begin Loop");
        xSemaphoreTake( xSemaphore, LONG_TIME );        
        Serial.println("End Loop");
    }
}

void vTimerISR( void * pvParameters )
{
    portYIELD_FROM_ISR();
}
As said before, all I need is a delay of a task, with the option to unblock before (which seems to work fine when uncommenting the loop code).

ESP_Sprite
Posts: 9727
Joined: Thu Nov 26, 2015 4:08 am

Re: Simple Semaphore as task delay with interrupt

Postby ESP_Sprite » Mon Feb 24, 2020 11:41 am

That should work (given you uncomment the two lines in loop()). Two things of note:
- vTimerISR is unused in your code, and
- xSemaphoreGiveFromISR will work but is more useful for use in interrupt handlers. If you're not in an interrupt handler (ISR) you can use xSemaphoreGive instead.

Who is online

Users browsing this forum: Majestic-12 [Bot] and 56 guests