Page 1 of 1

Simple Semaphore as task delay with interrupt

Posted: Fri Feb 21, 2020 11:00 pm
by Insomniac
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).

Re: Simple Semaphore as task delay with interrupt

Posted: Mon Feb 24, 2020 11:41 am
by ESP_Sprite
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.