最近為項目增加了GPIO外部觸發中斷功能,原理是為GPIO32注冊了上升沿觸發事件,事件觸發后,會向RTOS隊列寫入數據。在RTOS事件中檢測到該隊列中有新加入的事件,就讀出,並執行相應代碼。
#define GPIO_INPUT_IO_WAKEUP 32 #define GPIO_INPUT_PIN_SEL (1ULL<<GPIO_INPUT_IO_WAKEUP) #define ESP_INTR_FLAG_DEFAULT 0 static void IRAM_ATTR gpio_isr_handler(void* arg) { uint32_t gpio_num = (uint32_t) arg; xQueueSendFromISR(gpio_evt_queue, &gpio_num, NULL); } static void gpio_task_example(void* arg) { uint32_t io_num; for(;;) { if(xQueueReceive(gpio_evt_queue, &io_num, portMAX_DELAY))
{ printf("GPIO[%d] intr, val: %d\n", io_num, gpio_get_level(io_num)); } } } void app_main() { gpio_config_t io_conf; io_conf.intr_type = GPIO_PIN_INTR_POSEDGE;//interrupt of rising edge io_conf.pin_bit_mask = GPIO_INPUT_PIN_SEL;//bit mask of the pins, use GPIO32 here io_conf.mode = GPIO_MODE_INPUT;//set as input mode io_conf.pull_up_en = 0;//enable pull-up mode gpio_config(&io_conf); //install gpio isr service gpio_install_isr_service(ESP_INTR_FLAG_DEFAULT); //hook isr handler for specific gpio pin gpio_isr_handler_add(GPIO_INPUT_IO_WAKEUP, gpio_isr_handler, (void*) GPIO_INPUT_IO_WAKEUP); xTaskCreate(gpio_task_example, "gpio_task_example", 2048, NULL, 10, NULL); while(1) { } }