This commit is contained in:
Imbus 2025-07-08 14:17:57 +02:00
parent f899e9b57b
commit e6a05046e8
3 changed files with 72 additions and 0 deletions

View file

@ -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");
}

View file

@ -1,5 +1,6 @@
#include "freertos/idf_additions.h"
#include "freertos/projdefs.h"
#include "freertos/semphr.h"
#include "portmacro.h"
#include <driver/gpio.h>
#include <esp_rom_gpio.h>
@ -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);
}

View file

@ -44,4 +44,6 @@ void task_blink_cycle(void *pvParams);
/* End: task_blink.c */
void vPeriodicTask(void *pvParameters);
#endif