芯片:乐鑫官网ESP32-C3-DevKitM-1 v1.6
IDE:VSCode+ESP_IDF
程序源码:
#include <stdio.h>
#include "driver/gpio.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/queue.h"
#include "esp_log.h"
#define BOOT 9
#define GPIO_INPUT_PIN_SEL (1ULL << BOOT)
#define ESP_INTR_FLAG_DEFAULT 0
static QueueHandle_t gpio_evt_queue = NULL;
void IRAM_ATTR gpio_isr_handler(void *arg)
{
uint32_t gpio_num = (uint32_t)arg;
xQueueSendFromISR(gpio_evt_queue,&gpio_num,NULL);
}
void gpio_task(void *arg)
{
printf("\r\n start gpio task ... \r\n");
uint32_t io_num;
while(1)
{
if (xQueueReceive(gpio_evt_queue,&io_num,portMAX_DELAY))
{
printf("\r\n intriggered from GPIO%ld \r\n",io_num);
//ESP_LOGI("gpio_task","intriggered from GPIO%ld",io_num);
}
}
}
void Key_Init()
{
gpio_config_t gpio_struct =
{
.intr_type = GPIO_INTR_POSEDGE,
.mode=GPIO_MODE_INPUT,
.pull_up_en=1,
.pull_down_en=0,
.pin_bit_mask=GPIO_INPUT_PIN_SEL
};
gpio_config(&gpio_struct);
}
void Intr_init(void)
{
gpio_install_isr_service(ESP_INTR_FLAG_DEFAULT);
gpio_isr_handler_add(BOOT,gpio_isr_handler,(void *)BOOT);
gpio_evt_queue = xQueueCreate(10,sizeof(uint32_t));
xTaskCreate(gpio_task //任务函数
,
"gpio_task_example" //任务名
,
2048//堆栈大小
,
NULL//传递参数
,
10//任务优先级
,
NULL);//任务句柄
}
void app_main(void)
{
Key_Init();
Intr_init();
while (1)
{
vTaskDelay(1000/ portTICK_PERIOD_MS);
}
}
出现问题:
一次按键无反应,要按多次才会输出,而且换行无效。
解决方法:
调用ESP_LOGI,输出正常。
疑问:
无法理解,printf不行但是ESP_LOGI可以的逻辑,存疑