Search code examples
arduinofreertosesp32

Using semaphore in FreeRTOS


I am trying to use a semaphore of the arduino core for ESP32. My code is a follows:

#include <Arduino.h>
#include <freertos/task.h>
#include <freertos/queue.h>
#define configUSE_MUTEXES 1
#define configUSE_COUNTING_SEMAPHORES 1
void vTaskExample(void *pvParameters);
void accessSharedResource{}
volatile SemaphoreHandle_t xResourceSemaphore = NULL;
void setup()
{
    xTaskCreatePinnedToCore(&vTaskExample, "example task", 1024, NULL, 2, NULL, 1);
}

void loop()
{
    // Do nothing
}

void vTaskExample(void *pvParameters)
{
    vSemaphoreCreateBinary(xResourceSemaphore);
    while (true)
    {
        if (xSemaphoreAltTake(xResourceSemaphore, (TickType_t)0))
        {
            accessSharedResource();
            xSemaphoreAltGive(xResourceSemaphore);
        }
    }
}

Unfortunately, during compilation (in the linking phase to be exact), I get the following error message:

main.cpp:(.text._Z12vTaskExamplePv+0x37): undefined reference to `xQueueAltGenericReceive'
main.cpp:(.text._Z12vTaskExamplePv+0x4b): undefined reference to `xQueueAltGenericSend'

I have looked up the freeRTOS documentation, and it indicates that the two functions are located in the queue.h; thus, should be available. Also, I have set the necessary freeRTOS configuration by setting configUSE_MUTEXES and configUSE_COUNTING_SEMAPHORES flags

Any suggestions why this does not compile?


Solution

  • Only prototypes are provided in queue.h - nothing executable. If you look at the FreeRTOS documentation you will note that the alternative API has been deprecated for a long time, and is only included in the build if configUSE_ALTERNATIVE_API is set to 1.