in my programming, I need a semaphore so that only one FreeRTOS task can access the SPI resource at a time, and the others wait until it becomes available.
During the testing phase, I do not use any tasks, and only one procedure calls the "task()" function.
This is the relevant part of my code:
Code: Select all
#if defined(CONFIG_SEMAPHORE_TYPE_FREERTOS)
#include "freertos/semphr.h"
SemaphoreHandle_t semaphore_spi;
#elif defined(CONFIG_SEMAPHORE_TYPE_POSIX)
#include <semaphore.h>
sem_t semaphore_spi;
#endif
...
esp_err_t init()
{
esp_err_t ret = ESP_OK;
#if defined(CONFIG_SEMAPHORE_TYPE_FREERTOS)
if ((semaphore_spi = xSemaphoreCreateMutex()) == NULL)
{
ESP_LOGE(MODULE_TAG, "ERROR: Creating semaphore for SPI%d_HOST failed", interface->spi->spi_host + 1);
return(ESP_FAIL);
}
#elif defined(CONFIG_SEMAPHORE_TYPE_POSIX)
if ((ret = sem_init(&semaphore_spi, 1, 1)) != NO_ERROR)
{
ESP_LOGE(MODULE_TAG, "ERROR: Creating semaphore for SPI%d_HOST failed, ret = %d",
interface->spi->spi_host + 1, ret);
return(ret);
}
#endif
}
esp_err_t task()
{
esp_err_t ret = ESP_OK;
#if defined(CONFIG_SEMAPHORE_TYPE_FREERTOS)
if (xSemaphoreTake(semaphore_spi, pdMS_TO_TICKS(1000)) == pdTRUE)
#elif defined(CONFIG_SEMAPHORE_TYPE_POSIX)
if ((ret = sem_wait(&semaphore_spi)) == NO_ERROR)
#endif
{
...
#if defined(CONFIG_SEMAPHORE_TYPE_FREERTOS)
xSemaphoreGive(semaphore_spi);
#elif defined(CONFIG_SEMAPHORE_TYPE_POSIX)
sem_post(&semaphore_spi);
#endif
}
else
{
ESP_LOGE(MODULE_TAG, "ERROR : xSemaphoreTake failed");
#if defined(CONFIG_SEMAPHORE_TYPE_FREERTOS)
ret = ESP_FAIL;
#endif
}
return(ret);
}
}
However, the call to "xSemaphoreTake()" with semaphore created by the call „xSemaphoreCreateBinary()“ always returns pdFALSE!
The call to "xSemaphoreTake()" with semaphore created by the call „xSemaphoreCreateMutex()“ and the call to "sem_wait()" hang.
What am I doing wrong?
Does anyone have experience using semaphores with ESP-IDF?
Can anyone help me?
Thanks in advance