目录
重新调整工程目录结构 主要参考官方的例程进行简单更改
一、新工程创建
- 进入到ESP32 idf 安装目录,复制一个例程到自己的文件目录并改一个名字
- vs code 打开,新建一下文件夹
components
---bsp
---boards
---led.c
---led.h
---key.c
---key.h
---include
----bsp_board.h
---src
----bsp_board.h
---CMakeList.txt
3.在components下的CMakeList.txt 添加以下代码
file(GLOB_RECURSE BSP_SRCS
"bsp/boards/led.c"
"bsp/boards/key.c"
)
list(APPEND BSP_BOARD_SRC "./bsp/src/bsp_board.c")
idf_component_register(
SRCS
${BSP_SRCS}
${BSP_BOARD_SRC}
INCLUDE_DIRS
"bsp/include"
"bsp/boards"
REQUIRES
spi_flash
freertos
)
- 更改工程目录 文件下的 CMakeList.txt 文件代码,将project(blink) 改为 project(demo)
二、添加代码
led.c
/**
* @file led.c
* @brief
* @version 0.1
* @date 2021-12-18
*
* @copyright
*
*/
#include "driver/gpio.h"
#include "esp_err.h"
#include "esp_log.h"
#include "esp_rom_sys.h"
#include "bsp_board.h"
#include "led.h"
/**
* @brief init led pin
*
* @return
* - ESP_OK: Success
* - Others: Fail
*/
esp_err_t led_init(void)
{
gpio_reset_pin(led_pin);
/* Set the GPIO as a push/pull output */
gpio_set_direction(led_pin, GPIO_MODE_OUTPUT);
return ESP_OK;
}
/**
* @brief open the led
* @return
* - ESP_OK: Success
* - Others: Fail
*/
esp_err_t led_open(void)
{
printf("Turning on the LED\n");
gpio_set_level(led_pin, 0);
return ESP_OK ;
}
/**
* @brief close the led
* @return
* - ESP_OK: Success
* - Others: Fail
*/
esp_err_t led_close(void)
{
printf("Turning off the LED\n");
gpio_set_level(led_pin, 1);
return ESP_OK ;
}
led.h
#ifndef _LED_H_
#define _LED_H_
#ifdef __cplusplus
extern "C" {
#endif
#include <stdbool.h>
#include "esp_err.h"
#define led_pin GPIO_NUM_23
esp_err_t led_init(void);
esp_err_t led_open(void);
esp_err_t led_close(void);
#ifdef __cplusplus
}
#endif
#endif
key.c
/**
* @file key.c
* @brief
* @version 0.1
* @date 2021-12-18
*
* @copyright
*
*/
#include "driver/gpio.h"
#include "esp_err.h"
#include "esp_log.h"
#include "esp_rom_sys.h"
#include "bsp_board.h"
#include "key.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/queue.h"
/*创建消息队列*/
static xQueueHandle gpio_evt_queue = NULL;
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 key_gpio_task(void* arg)
{
uint32_t io_num;
for(;;) {
if(xQueueReceive(gpio_evt_queue, &io_num, portMAX_DELAY)) {
printf("收到消息\r\n");
printf("GPIO[%d] intr, val: %d\n", io_num, gpio_get_level(io_num));
}
}
}
/**
* @brief init key pin
*
* @return
* - ESP_OK: Success
* - Others: Fail
*/
esp_err_t key_init(void)
{
gpio_config_t io_config=
{
io_config.pin_bit_mask =(1ull<<KEY_PIN),
io_config.mode =GPIO_MODE_INPUT,
io_config.pull_up_en =0x01,
io_config.pull_down_en =0x00,
io_config.intr_type =GPIO_INTR_POSEDGE
};
gpio_config(&io_config);
//create a queue to handle gpio event from isr
gpio_evt_queue = xQueueCreate(10, sizeof(uint32_t));
//start gpio task
xTaskCreate(key_gpio_task, "key_gpio_task", 2048, NULL, 10, NULL);
//install gpio isr service
gpio_install_isr_service(ESP_INTR_FLAG_DEFAULT);
//hook isr handler for specific gpio pin
gpio_isr_handler_add(KEY_PIN, gpio_isr_handler, (void*) KEY_PIN);
return ESP_OK;
}
key.h
#ifndef _KEY_H_
#define _KEY_H_
#ifdef __cplusplus
extern "C" {
#endif
#include <stdbool.h>
#include "esp_err.h"
#define KEY_PIN GPIO_NUM_5
#define ESP_INTR_FLAG_DEFAULT 0
esp_err_t key_init(void);
#ifdef __cplusplus
}
#endif
#endif
main.c
#include <stdio.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "driver/gpio.h"
#include "sdkconfig.h"
#include "led.h"
#include "bsp_board.h"
void app_main(void)
{
bsp_board_init();
while(1) {
led_open();
vTaskDelay(1000 / portTICK_PERIOD_MS);
led_close();
vTaskDelay(1000 / portTICK_PERIOD_MS);
}
}