main.cc 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. #include <esp_log.h>
  2. #include <esp_err.h>
  3. #include <nvs.h>
  4. #include <nvs_flash.h>
  5. #include <driver/gpio.h>
  6. #include <esp_event.h>
  7. #include "application.h"
  8. #include "system_info.h"
  9. #define TAG "main"
  10. #include <stdio.h>
  11. #include <string.h>
  12. #include <stdlib.h>
  13. #include "freertos/FreeRTOS.h"
  14. #include "freertos/task.h"
  15. #include "freertos/queue.h"
  16. // 使用GPIO_NUM_5替代直接整数,确保类型正确
  17. #define GPIO_INPUT_IO_0 GPIO_NUM_5
  18. #define GPIO_INPUT_PIN_SEL (1ULL << GPIO_INPUT_IO_0)
  19. #define ESP_INTR_FLAG_DEFAULT 0
  20. // 使用QueueHandle_t替代xQueueHandle(FreeRTOS现代版本兼容)
  21. static QueueHandle_t gpio_evt_queue = NULL;
  22. static void IRAM_ATTR gpio_isr_handler(void* arg)
  23. {
  24. uint32_t gpio_num = (uint32_t)arg;
  25. // 优化中断处理:添加上下文切换判断
  26. BaseType_t xHigherPriorityTaskWoken = pdFALSE;
  27. xQueueSendFromISR(gpio_evt_queue, &gpio_num, &xHigherPriorityTaskWoken);
  28. if (xHigherPriorityTaskWoken) {
  29. portYIELD_FROM_ISR();
  30. }
  31. }
  32. static void gpio_task_example(void* arg)
  33. {
  34. uint32_t io_num;
  35. for (;;) {
  36. if (xQueueReceive(gpio_evt_queue, &io_num, portMAX_DELAY)) {
  37. // 修复格式符和类型转换
  38. printf("GPIO[%u] intr, val: %d\n",
  39. (unsigned int)io_num,
  40. gpio_get_level((gpio_num_t)io_num));
  41. }
  42. }
  43. }
  44. extern "C" void app_main(void)
  45. {
  46. // 初始化默认事件循环
  47. ESP_ERROR_CHECK(esp_event_loop_create_default());
  48. // 初始化NVS闪存
  49. esp_err_t ret = nvs_flash_init();
  50. if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) {
  51. ESP_LOGW(TAG, "Erasing NVS flash to fix corruption");
  52. ESP_ERROR_CHECK(nvs_flash_erase());
  53. ret = nvs_flash_init();
  54. }
  55. ESP_ERROR_CHECK(ret);
  56. // 配置GPIO
  57. gpio_config_t io_conf = {};
  58. io_conf.intr_type = GPIO_INTR_POSEDGE; // 上升沿触发
  59. io_conf.mode = GPIO_MODE_INPUT; // 输入模式
  60. io_conf.pin_bit_mask = GPIO_INPUT_PIN_SEL; // 引脚掩码
  61. io_conf.pull_up_en = GPIO_PULLUP_ENABLE; // 使用枚举值替代整数1
  62. io_conf.pull_down_en = GPIO_PULLDOWN_DISABLE;// 显式禁用下拉
  63. gpio_config(&io_conf);
  64. // 创建事件队列
  65. gpio_evt_queue = xQueueCreate(10, sizeof(uint32_t));
  66. if (gpio_evt_queue == NULL) {
  67. ESP_LOGE(TAG, "Failed to create GPIO event queue!");
  68. return;
  69. }
  70. // 创建GPIO处理任务
  71. xTaskCreate(gpio_task_example,
  72. "gpio_task_example",
  73. 2048,
  74. NULL,
  75. 10,
  76. NULL);
  77. // 安装GPIO中断服务
  78. ESP_ERROR_CHECK(gpio_install_isr_service(ESP_INTR_FLAG_DEFAULT));
  79. // 注册中断处理函数
  80. ESP_ERROR_CHECK(gpio_isr_handler_add(GPIO_INPUT_IO_0,
  81. gpio_isr_handler,
  82. (void*)GPIO_INPUT_IO_0));
  83. ESP_LOGI(TAG, "GPIO5 interrupt (rising edge) configured successfully");
  84. // 主循环
  85. while (1) {
  86. // 修复宏定义名称(portTICK_RATE_MS已废弃)
  87. vTaskDelay(pdMS_TO_TICKS(1000));
  88. }
  89. // 应用程序启动代码(如需启用请取消注释)
  90. // Application::GetInstance().Start();
  91. }