replace queue with semaphore

Signed-off-by: Peter Siegmund <mars3142@users.noreply.github.com>
This commit is contained in:
2025-06-07 13:08:51 +02:00
parent 0ff025c73f
commit 785aa0326c

View File

@@ -1,6 +1,5 @@
#include "beacon.h" #include "beacon.h"
#include "driver/gpio.h"
#include "driver/gptimer.h" #include "driver/gptimer.h"
#include "esp_log.h" #include "esp_log.h"
#include "freertos/FreeRTOS.h" #include "freertos/FreeRTOS.h"
@@ -8,15 +7,10 @@
#include "inttypes.h" #include "inttypes.h"
#include "led_strip.h" #include "led_strip.h"
#include "sdkconfig.h" #include "sdkconfig.h"
#include "semaphore.h"
static const char *TAG = "beacon"; static const char *TAG = "beacon";
typedef struct
{
uint32_t count;
uint32_t alarm;
} TimerEvent;
typedef struct typedef struct
{ {
led_strip_handle_t ledStrip; led_strip_handle_t ledStrip;
@@ -24,24 +18,25 @@ typedef struct
} LedMatrix; } LedMatrix;
static LedMatrix ledMatrix = {.size = 64}; static LedMatrix ledMatrix = {.size = 64};
static QueueHandle_t timerEvtQueue = NULL; static SemaphoreHandle_t timer_semaphore;
gptimer_handle_t gpTimer = NULL; gptimer_handle_t gpTimer = NULL;
bool IRAM_ATTR timerCallback(gptimer_handle_t timer, const gptimer_alarm_event_data_t *edata, void *userCtx) bool IRAM_ATTR timerCallback(gptimer_handle_t timer, const gptimer_alarm_event_data_t *edata, void *userCtx)
{ {
TimerEvent evt = {.count = edata->count_value, .alarm = edata->alarm_value}; BaseType_t high_task_wakeup = pdFALSE;
BaseType_t highTaskWakeup = pdFALSE; xSemaphoreGiveFromISR(timer_semaphore, &high_task_wakeup);
xQueueSendFromISR(timerEvtQueue, &evt, &highTaskWakeup); if (high_task_wakeup)
{
return (highTaskWakeup == pdTRUE); portYIELD_FROM_ISR();
}
return true;
} }
void timerEventTask(void *arg) void timerEventTask(void *arg)
{ {
TimerEvent evt;
while (true) while (true)
{ {
if (xQueueReceive(timerEvtQueue, &evt, portMAX_DELAY)) if (xSemaphoreTake(timer_semaphore, portMAX_DELAY))
{ {
static bool level = false; static bool level = false;
level = !level; level = !level;
@@ -53,8 +48,7 @@ void timerEventTask(void *arg)
} }
led_strip_refresh(ledMatrix.ledStrip); led_strip_refresh(ledMatrix.ledStrip);
} }
ESP_LOGI(TAG, "Timer Event: count = %" PRIu32 ", alarm = %" PRIu32 ", GPIO now %s", evt.count, evt.alarm, ESP_LOGD(TAG, "Timer Event, LED now %s", level ? "ON" : "OFF");
level ? "HIGH" : "LOW");
} }
} }
} }
@@ -128,13 +122,8 @@ esp_err_t stopBeacon(void)
esp_err_t initBeacon(void) esp_err_t initBeacon(void)
{ {
esp_err_t ret = ESP_OK; esp_err_t ret = ESP_OK;
timerEvtQueue = xQueueCreate(10, sizeof(TimerEvent));
if (timerEvtQueue == NULL) timer_semaphore = xSemaphoreCreateBinary();
{
ESP_LOGE(TAG, "Failed to create timer event queue");
ret = ESP_ERR_NO_MEM;
goto exit;
}
gptimer_config_t timerConfig = { gptimer_config_t timerConfig = {
.clk_src = GPTIMER_CLK_SRC_DEFAULT, .direction = GPTIMER_COUNT_UP, .resolution_hz = 1000000}; .clk_src = GPTIMER_CLK_SRC_DEFAULT, .direction = GPTIMER_COUNT_UP, .resolution_hz = 1000000};
@@ -142,7 +131,7 @@ esp_err_t initBeacon(void)
if (ret != ESP_OK) if (ret != ESP_OK)
{ {
ESP_LOGE(TAG, "Failed to create new gptimer: %s", esp_err_to_name(ret)); ESP_LOGE(TAG, "Failed to create new gptimer: %s", esp_err_to_name(ret));
goto cleanupQueue; goto exit;
} }
gptimer_event_callbacks_t callbacks = {.on_alarm = timerCallback}; gptimer_event_callbacks_t callbacks = {.on_alarm = timerCallback};
@@ -191,12 +180,6 @@ cleanupTimer:
gptimer_del_timer(gpTimer); gptimer_del_timer(gpTimer);
gpTimer = NULL; gpTimer = NULL;
} }
cleanupQueue:
if (timerEvtQueue)
{
vQueueDelete(timerEvtQueue);
timerEvtQueue = NULL;
}
exit: exit:
return ret; return ret;
} }