From 0a622830b385a88eb79f4a0c8b1a7436e4595dd5 Mon Sep 17 00:00:00 2001 From: midilab Date: Tue, 5 Sep 2023 09:47:15 -0300 Subject: [PATCH] esp32: change semaphore per task notification implementation for low latency response between clock signal and task action --- src/platforms/esp32.h | 25 ++++++++++--------------- 1 file changed, 10 insertions(+), 15 deletions(-) diff --git a/src/platforms/esp32.h b/src/platforms/esp32.h index 2cbb5d6..f4665ab 100644 --- a/src/platforms/esp32.h +++ b/src/platforms/esp32.h @@ -10,8 +10,7 @@ hw_timer_t * _uclockTimer = NULL; // FreeRTOS main clock task size in bytes #define CLOCK_STACK_SIZE 2048 -// semaphore to signal the task from the ISR -SemaphoreHandle_t _semaphore; +TaskHandle_t taskHandle; // mutex to protect the shared resource SemaphoreHandle_t _mutex; // mutex control for task @@ -28,21 +27,20 @@ void uClockHandler(); // ISR handler void ARDUINO_ISR_ATTR handlerISR(void) { - // notify the clockTask using the semaphore BaseType_t xHigherPriorityTaskWoken = pdFALSE; - xSemaphoreGiveFromISR(_semaphore, &xHigherPriorityTaskWoken); - - // context switch is required? request it - if (xHigherPriorityTaskWoken == pdTRUE) - portYIELD_FROM_ISR(xHigherPriorityTaskWoken); + // Send a notification to task1 + vTaskNotifyGiveFromISR(taskHandle, &xHigherPriorityTaskWoken); + portYIELD_FROM_ISR(xHigherPriorityTaskWoken); } // task for user clock process void clockTask(void *pvParameters) { - while (1) - if (xSemaphoreTake(_semaphore, portMAX_DELAY) == pdTRUE) - uClockHandler(); + while (1) { + // wait for a notification from ISR + ulTaskNotifyTake(pdTRUE, portMAX_DELAY); + uClockHandler(); + } } void initTimer(uint32_t init_clock) @@ -58,14 +56,11 @@ void initTimer(uint32_t init_clock) // activate it! timerAlarmEnable(_uclockTimer); - // initialize the semaphore - _semaphore = xSemaphoreCreateBinary(); - // initialize the mutex for shared resource access _mutex = xSemaphoreCreateMutex(); // create the clockTask - xTaskCreate(clockTask, "clockTask", CLOCK_STACK_SIZE, NULL, 1, NULL); + xTaskCreate(clockTask, "clockTask", CLOCK_STACK_SIZE, NULL, 1, &taskHandle); } void setTimer(uint32_t us_interval)