From e6a05046e8641219a9ba749a6e939f6900591c82 Mon Sep 17 00:00:00 2001 From: Imbus <> Date: Tue, 8 Jul 2025 14:17:57 +0200 Subject: [PATCH] Jujnk --- main/main.c | 3 +++ main/task_blink.c | 67 +++++++++++++++++++++++++++++++++++++++++++++++ main/tasks.h | 2 ++ 3 files changed, 72 insertions(+) diff --git a/main/main.c b/main/main.c index 2f1ea70..d2e8874 100644 --- a/main/main.c +++ b/main/main.c @@ -19,4 +19,7 @@ void app_main(void) { if (xTaskCreate(task_blink_cycle, "task_blink_cycle", 1 << 11, NULL, 0, &task_handle_blink_cycle) != pdPASS) printf("Error creating task: task_blink_cycle"); + + if (xTaskCreate(vPeriodicTask, "task_periodic", 1 << 11, NULL, 0, NULL) != pdPASS) + printf("Error creating task: task_periodic"); } diff --git a/main/task_blink.c b/main/task_blink.c index d4ca13b..6a977a9 100644 --- a/main/task_blink.c +++ b/main/task_blink.c @@ -1,5 +1,6 @@ #include "freertos/idf_additions.h" #include "freertos/projdefs.h" +#include "freertos/semphr.h" #include "portmacro.h" #include #include @@ -129,3 +130,69 @@ void task_blink_cycle(void *pvParams) { vTaskDelete(NULL); } + +void vPeriodicTask(void *pvParameters) { + TickType_t xLastWakeTime; + const TickType_t xDelay3ms = pdMS_TO_TICKS(2000); + + /* + * The xLastWakeTime variable needs to be initialized with the current tick + * count. Note that this is the only time the variable is explicitly + * written to. After this xLastWakeTime is managed automatically by the + * vTaskDelayUntil() API function. + */ + xLastWakeTime = xTaskGetTickCount(); + + char *taskname = pcTaskGetName(NULL); + + /* As per most tasks, this task is implemented in an infinite loop. */ + for (;;) { + /* Print out the name of this task. */ + printf("%s - %" PRIu32 ": Running...\n", taskname, pdTICKS_TO_MS(xLastWakeTime)); + + /* + * The task should execute every 3 milliseconds exactly – see the + * declaration of xDelay3ms in this function. + */ + vTaskDelayUntil(&xLastWakeTime, xDelay3ms); + } +} + +#define BUTTON_GPIO GPIO_NUM_0 +SemaphoreHandle_t button_sem; + +static void IRAM_ATTR button_isr_handler(void *arg) { + BaseType_t xHigherPriorityTaskWoken = pdFALSE; + xSemaphoreGiveFromISR(button_sem, &xHigherPriorityTaskWoken); + if (xHigherPriorityTaskWoken) { + portYIELD_FROM_ISR(); // Request context switch if needed + } +} + +void button_task(void *pvParams) { + while (1) { + if (xSemaphoreTake(button_sem, portMAX_DELAY)) { + printf("Button pressed (from ISR)\n"); + } + } +} + +void task_gpio_demo(void *pvParams) { + button_sem = xSemaphoreCreateBinary(); + + // Setup GPIO + gpio_config_t io_conf = { + .intr_type = GPIO_INTR_NEGEDGE, + .mode = GPIO_MODE_INPUT, + .pin_bit_mask = (1ULL << BUTTON_GPIO), + .pull_up_en = 1, + }; + gpio_config(&io_conf); + + // Install ISR handler + gpio_install_isr_service(0); + gpio_isr_handler_add(BUTTON_GPIO, button_isr_handler, NULL); + + // Start the task + xTaskCreate(button_task, "button_task", 2048, NULL, 10, NULL); +} diff --git a/main/tasks.h b/main/tasks.h index e4e3c29..703ad57 100644 --- a/main/tasks.h +++ b/main/tasks.h @@ -44,4 +44,6 @@ void task_blink_cycle(void *pvParams); /* End: task_blink.c */ +void vPeriodicTask(void *pvParameters); + #endif