From 0c0741aef350936ad903d2213532fcd3ebf35c18 Mon Sep 17 00:00:00 2001 From: antopoid Date: Thu, 3 Nov 2022 16:41:20 +0100 Subject: [PATCH] lab2-4_single_msg_queue_interrupt --- .../lab2-1_single_msg_queue/main/main.c | 2 +- .../main/main.c | 72 ++++++++++++------- .../main/my_helper_fct.h | 47 ++++++++++++ .../sdkconfig.defaults | 9 +++ 4 files changed, 102 insertions(+), 28 deletions(-) create mode 100644 part3_freertos/lab2-4_single_msg_queue_interrupt/main/my_helper_fct.h create mode 100644 part3_freertos/lab2-4_single_msg_queue_interrupt/sdkconfig.defaults diff --git a/part3_freertos/lab2-1_single_msg_queue/main/main.c b/part3_freertos/lab2-1_single_msg_queue/main/main.c index b17d5c4..02f08e5 100644 --- a/part3_freertos/lab2-1_single_msg_queue/main/main.c +++ b/part3_freertos/lab2-1_single_msg_queue/main/main.c @@ -104,7 +104,7 @@ void app_main(void) { DISPLAY("Start of app_main task, priority = %d",uxTaskPriorityGet(NULL)); vTaskSuspendAll(); - xTaskCreatePinnedToCore(vTaskFunction1, "Task 1", STACK_SIZE, (void*)"Task 1", T1_PRIO, NULL,CORE_1); + xTaskCreatePinnedToCore(vTaskFunction2, "Task 2", STACK_SIZE, (void*)"Task 2", T2_PRIO, NULL,CORE_0); xTaskCreatePinnedToCore(vTaskFunction3, "Task 3", STACK_SIZE, (void*)"Task 3", T3_PRIO, NULL,CORE_0); diff --git a/part3_freertos/lab2-4_single_msg_queue_interrupt/main/main.c b/part3_freertos/lab2-4_single_msg_queue_interrupt/main/main.c index 990c743..3f69aa8 100644 --- a/part3_freertos/lab2-4_single_msg_queue_interrupt/main/main.c +++ b/part3_freertos/lab2-4_single_msg_queue_interrupt/main/main.c @@ -13,19 +13,7 @@ * * @see https://github.com/fmuller-pns/esp32-vscode-project-template */ -#include -#include -#include -#include -#include "esp_log.h" -/* FreeRTOS.org includes. */ -#include "freertos/FreeRTOSConfig.h" -#include "freertos/FreeRTOS.h" -#include "freertos/task.h" -#include "freertos/semphr.h" - -#include "my_helper_fct.h" #include #include @@ -42,7 +30,11 @@ #include "my_helper_fct.h" +QueueHandle_t xMsgQ; static const char* TAG = "MsgQ"; +static const uint32_t T1_PRIO = 2; +static volatile uint32_t isrCount = 0; + // Push button for interrupt static const gpio_num_t PIN_PUSH_BUTTON = 15; @@ -50,19 +42,35 @@ static const gpio_num_t PIN_PUSH_BUTTON = 15; /* Stack size for all tasks */ const uint32_t TASK_STACK_SIZE = 4096; +static void IRAM_ATTR Push_button_isr_handler(void *args); +void vCounterTask(void *pvParameters); + void app_main(void) { /* Config GPIO */ - + gpio_config_t config_in = { + .intr_type = GPIO_INTR_NEGEDGE, + .mode = GPIO_MODE_INPUT, + .pull_down_en = GPIO_PULLDOWN_DISABLE, + .pull_up_en = GPIO_PULLUP_ENABLE, + .pin_bit_mask = (1ULL << PIN_PUSH_BUTTON) + }; + gpio_config(&config_in); /* Create Message Queue and Check if created */ - + xMsgQ = xQueueCreate(1, sizeof(uint32_t)); + if(xMsgQ == NULL){ + //DISPLAY("xMsgQ was not created and must not be used!"); + ESP_LOGE(TAG,"MsgQ cannot be created."); + } /* Create vCounterTask task */ - + xTaskCreate(vCounterTask, "Counter Task", TASK_STACK_SIZE, NULL, T1_PRIO, NULL); /* Install ISR */ + gpio_install_isr_service(0); + gpio_isr_handler_add(PIN_PUSH_BUTTON, Push_button_isr_handler, (void *)PIN_PUSH_BUTTON); /* to ensure its exit is clean */ @@ -70,36 +78,46 @@ void app_main(void) { } void vCounterTask(void *pvParameters) { - + int pinNumber; for (;; ) { /* Wait for message with 5 sec. otherwise DISPLAY a message to push it */ - + if(xQueueReceive(xMsgQ,&pinNumber, pdMS_TO_TICKS(5000))){ /* If pushed */ // DISPLAY "Button pushed" and the isrCount variable - + DISPLAY("Button pushed, isrCount:%d", isrCount); // Get the number of items in the queue - + uint32_t msgNumber = uxQueueMessagesWaiting(xMsgQ); // DISPLAYI (Information display) number of items if greater than 1 - + if(msgNumber > 1){ + DISPLAYI(TAG, "More than one interrupt occurs : %d", msgNumber); + } // Waiting for push button signal is 1 again (test every 20 ms) + do { + vTaskDelay(pdMS_TO_TICKS(20)); + } while (gpio_get_level(pinNumber) == 0); // DISPLAY "Button released" - + DISPLAY("Button released, isrCount: %d", isrCount); + } /* Else If Time out */ - + else + { // Display message "Please, push the button!" + DISPLAY("Please, push the button!"); + } + } } -} static void IRAM_ATTR Push_button_isr_handler(void *args) { + int pinNumber = (int)args; // Increment isrCount - + isrCount++; + // Send message - -} - + xQueueSendFromISR(xMsgQ, &pinNumber, NULL); +} \ No newline at end of file diff --git a/part3_freertos/lab2-4_single_msg_queue_interrupt/main/my_helper_fct.h b/part3_freertos/lab2-4_single_msg_queue_interrupt/main/my_helper_fct.h new file mode 100644 index 0000000..7e0bc39 --- /dev/null +++ b/part3_freertos/lab2-4_single_msg_queue_interrupt/main/my_helper_fct.h @@ -0,0 +1,47 @@ +#ifndef _MY_HELPER_FCT_ +#define _MY_HELPER_FCT_ + +#include +#include "esp_log.h" + + +/* Core constants fot xTaskCreatePinnedToCore() function */ +const uint32_t CORE_0 = 0; +const uint32_t CORE_1 = 1; +const uint32_t PRO_CPU = 0; +const uint32_t APP_CPU = 1; + +/** + * @brief Macro to display the buffer with arguments. msg is a formatted string as printf() function. + * + */ +#define DISPLAY(msg, ...) printf("%d:%d>\t"msg"\r\n", xTaskGetTickCount(), xPortGetCoreID(), ##__VA_ARGS__); + + +/** + * @brief Macro to display error/warning/info for the buffer with arguments. msg is a formatted string as printf() function. + * + */ +#define DISPLAYE(tag, msg, ...) ESP_LOGE(tag, "%d:%d>\t"msg"", xTaskGetTickCount(), xPortGetCoreID(), ##__VA_ARGS__); + +#define DISPLAYW(tag, msg, ...) ESP_LOGW(tag, "%d:%d>\t"msg"", xTaskGetTickCount(), xPortGetCoreID(), ##__VA_ARGS__); + +#define DISPLAYI(tag, msg, ...) ESP_LOGI(tag, "%d:%d>\t"msg"", xTaskGetTickCount(), xPortGetCoreID(), ##__VA_ARGS__); + +/** + * @brief Macro to display the buffer without argument + * + */ +#define DISPLAYB(msg) printf("%d:%d>\t", xTaskGetTickCount(), xPortGetCoreID());\ + printf(msg);\ + printf("\r\n"); + + +/* Consume CPU cycles. Time parameter is for the default frequency: 160MHz (ESP32_DEFAULT_CPU_FREQ_MHZ=160) */ +#define COMPUTE_IN_TIME_US(time_us) {register uint32_t i=(uint32_t)time_us*5.02*100/22; while (i-- > 0) {asm(" nop");}} + +#define COMPUTE_IN_TIME_MS(time_ms) {register uint32_t i=(uint32_t)time_ms*1000*5.02*100/22; while (i-- > 0) {asm(" nop");}} + +#define COMPUTE_IN_TICK(tick) {register uint32_t i=(uint32_t)(tick*1000/configTICK_RATE_HZ)*1000*5.02*100/22; while (i-- > 0) {asm(" nop");}} + +#endif \ No newline at end of file diff --git a/part3_freertos/lab2-4_single_msg_queue_interrupt/sdkconfig.defaults b/part3_freertos/lab2-4_single_msg_queue_interrupt/sdkconfig.defaults new file mode 100644 index 0000000..5714a1f --- /dev/null +++ b/part3_freertos/lab2-4_single_msg_queue_interrupt/sdkconfig.defaults @@ -0,0 +1,9 @@ +CONFIG_ESPTOOLPY_FLASHSIZE_4MB=y +CONFIG_ESPTOOLPY_FLASHSIZE="4MB" + +# disable watchdog for task +CONFIG_ESP_TASK_WDT=n + +# 1 core to perform freertos +CONFIG_FREERTOS_UNICORE=y +