|
|
@@ -10,12 +10,51 @@
|
|
|
|
|
|
#define TAG "main"
|
|
|
|
|
|
+#include <stdio.h>
|
|
|
+#include <string.h>
|
|
|
+#include <stdlib.h>
|
|
|
+#include "freertos/FreeRTOS.h"
|
|
|
+#include "freertos/task.h"
|
|
|
+#include "freertos/queue.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
|
|
|
+
|
|
|
+// 使用QueueHandle_t替代xQueueHandle(FreeRTOS现代版本兼容)
|
|
|
+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));
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
extern "C" void app_main(void)
|
|
|
{
|
|
|
- // Initialize the default event loop
|
|
|
+ // 初始化默认事件循环
|
|
|
ESP_ERROR_CHECK(esp_event_loop_create_default());
|
|
|
|
|
|
- // Initialize NVS flash for WiFi configuration
|
|
|
+ // 初始化NVS闪存
|
|
|
esp_err_t ret = nvs_flash_init();
|
|
|
if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) {
|
|
|
ESP_LOGW(TAG, "Erasing NVS flash to fix corruption");
|
|
|
@@ -24,9 +63,46 @@ extern "C" void app_main(void)
|
|
|
}
|
|
|
ESP_ERROR_CHECK(ret);
|
|
|
|
|
|
+ // 配置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));
|
|
|
|
|
|
- // Launch the application
|
|
|
+ ESP_LOGI(TAG, "GPIO5 interrupt (rising edge) configured successfully");
|
|
|
+
|
|
|
+ // 主循环
|
|
|
+ while (1) {
|
|
|
+ // 修复宏定义名称(portTICK_RATE_MS已废弃)
|
|
|
+ vTaskDelay(pdMS_TO_TICKS(1000));
|
|
|
+ }
|
|
|
+
|
|
|
+ // 应用程序启动代码(如需启用请取消注释)
|
|
|
// Application::GetInstance().Start();
|
|
|
- // The main thread will exit and release the stack memory
|
|
|
-}
|
|
|
+}
|