Bladeren bron

技术原因,把gpio中断改到application里

xuxinyi 4 maanden geleden
bovenliggende
commit
adcceee219
1 gewijzigde bestanden met toevoegingen van 75 en 1 verwijderingen
  1. 75 1
      main/application.cc

+ 75 - 1
main/application.cc

@@ -18,8 +18,82 @@
 #include <esp_app_desc.h>
 #include "tianwen_wake_up_word/tianwen.h"
 
+#include "esp_err.h"
+#include "freertos/FreeRTOS.h"
+#include "freertos/queue.h"
+#include "freertos/task.h"
+
+// 使用GPIO_NUM_5替代直接整数,确保类型正确
+#define GPIO_INPUT_IO_0     GPIO_NUM_5
+#define GPIO_INPUT_PIN_SEL  (1ULL << GPIO_INPUT_IO_0)
+#define ESP_INTR_FLAG_DEFAULT 0
+
 #define TAG "Application"
 
+static QueueHandle_t gpio_evt_queue = NULL;
+
+
+static void IRAM_ATTR gpio_isr_handler(void* arg)
+{
+    uint32_t gpio_num = (uint32_t)arg;
+    // 优化中断处理:添加上下文切换判断
+    BaseType_t xHigherPriorityTaskWoken = pdFALSE;
+    xQueueSendFromISR(gpio_evt_queue, &gpio_num, &xHigherPriorityTaskWoken);
+    if (xHigherPriorityTaskWoken) {
+        portYIELD_FROM_ISR();
+    }
+}
+
+static void gpio_task_example(void* arg)
+{
+    uint32_t io_num;
+    for (;;) {
+        if (xQueueReceive(gpio_evt_queue, &io_num, portMAX_DELAY)) {
+            // 修复格式符和类型转换
+            printf("GPIO[%u] intr, val: %d\n", 
+                   (unsigned int)io_num, 
+                   gpio_get_level((gpio_num_t)io_num));
+        }
+    }
+}
+
+static void gpio5_interrupt_start() { 
+    // 配置GPIO
+    gpio_config_t io_conf = {};
+    io_conf.intr_type = GPIO_INTR_POSEDGE;       // 上升沿触发
+    io_conf.mode = GPIO_MODE_INPUT;              // 输入模式
+    io_conf.pin_bit_mask = GPIO_INPUT_PIN_SEL;   // 引脚掩码
+    io_conf.pull_up_en = GPIO_PULLUP_ENABLE;     // 使用枚举值替代整数1
+    io_conf.pull_down_en = GPIO_PULLDOWN_DISABLE;// 显式禁用下拉
+    gpio_config(&io_conf);
+
+    // 创建事件队列
+    gpio_evt_queue = xQueueCreate(10, sizeof(uint32_t));
+    if (gpio_evt_queue == NULL) {
+        ESP_LOGE(TAG, "Failed to create GPIO event queue!");
+        return;
+    }
+
+    // 创建GPIO处理任务
+    xTaskCreate(gpio_task_example, 
+                "gpio_task_example", 
+                2048, 
+                NULL, 
+                10, 
+                NULL);
+
+    // 安装GPIO中断服务
+    ESP_ERROR_CHECK(gpio_install_isr_service(ESP_INTR_FLAG_DEFAULT));
+    
+    // 注册中断处理函数
+    ESP_ERROR_CHECK(gpio_isr_handler_add(GPIO_INPUT_IO_0, 
+                                         gpio_isr_handler, 
+                                         (void*)GPIO_INPUT_IO_0));
+
+    ESP_LOGI(TAG, "GPIO5 interrupt (rising edge) configured successfully");
+}
+
+
 
 static const char* const STATE_STRINGS[] = {
     "unknown",
@@ -322,7 +396,7 @@ void Application::Start() {
     //     ESP_LOGE("main", "GPIO5 interrupt initialization failed");
     //     return;
     // }
-
+    gpio5_interrupt_start();
     /* Setup the display */
     auto display = board.GetDisplay();