概述
本文简要分析esp32 gpio例程。
正文
项目文件结构:
main文件夹:存放工程的资源文件
CMakeLists.txt:这里定义了cmake环境
Makefile:工程makefile,这里可以修改项目名称
main文件夹结构:
CMakeLists.txt:这里是工程src、inc文件的定义,多个src文件的工程需要在这里进行包含
component.mk:组件makefile文件,不常用,一般不作更改。
gpio_example_main.c:项目源文件
gpio_example_main.c分析
1、gpio配置结构体,对于gpio初始化主要是对这个接口进行配置。
需包含gpio.h头文件
typedef struct {
uint32_t pin_bit_mask; /*!< GPIO pin: 配置某一号管脚 */
gpio_mode_t mode; /*!< GPIO mode: 设置输入输出模式 */
gpio_pullup_t pull_up_en; /*!< GPIO pull-up:使能上拉 */
gpio_pulldown_t pull_down_en; /*!< GPIO pull-down:使能下拉 */
gpio_int_type_t intr_type; /*!< GPIO interrupt type:是否使能中断,并配置中断模式 */
} gpio_config_t;
2、配置完gpio_config_t后,调用 gpio_config()函数来完成配置。
3、之后只需要执行gpio_set_level函数即可设置IO口高低电平。
例程讲解
/* GPIO Example
This example code is in the Public Domain (or CC0 licensed, at your option.)
Unless required by applicable law or agreed to in writing, this
software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
CONDITIONS OF ANY KIND, either express or implied.
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/queue.h"
#include "driver/gpio.h"
/**
* Brief:
* This test code shows how to configure gpio and how to use gpio interrupt.
*
* GPIO status:
* GPIO18: output
* GPIO19: output
* GPIO4: input, pulled up, interrupt from rising edge and falling edge
* GPIO5: input, pulled up, interrupt from rising edge.
*
* Test:
* Connect GPIO18 with GPIO4
* Connect GPIO19 with GPIO5
* Generate pulses on GPIO18/19, that triggers interrupt on GPIO4/5
*
*/
#define GPIO_OUTPUT_IO_0 18 //定义为输出的IO编号
#define GPIO_OUTPUT_IO_1 19
#define GPIO_OUTPUT_PIN_SEL ((1ULL<<GPIO_OUTPUT_IO_0) | (1ULL<<GPIO_OUTPUT_IO_1)) //打开引脚标志,1UL为打开,0UL为关闭
#define GPIO_INPUT_IO_0 4 //定义为输入的IO编号
#define GPIO_INPUT_IO_1 5
#define GPIO_INPUT_PIN_SEL ((1ULL<<GPIO_INPUT_IO_0) | (1ULL<<GPIO_INPUT_IO_1))
#define ESP_INTR_FLAG_DEFAULT 0 //设置中断编号
static xQueueHandle gpio_evt_queue = NULL; //定义gpio消息队列,用于传递消息
/* 定义gpio中断回调函数 */
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(void)
{
gpio_config_t io_conf;
//disable interrupt
io_conf.intr_type = GPIO_INTR_DISABLE; //关闭中断
//set as output mode
io_conf.mode = GPIO_MODE_OUTPUT; //设置输出模式
//bit mask of the pins that you want to set,e.g.GPIO18/19
io_conf.pin_bit_mask = GPIO_OUTPUT_PIN_SEL;
//disable pull-down mode
io_conf.pull_down_en = 0; //关闭下拉
//disable pull-up mode
io_conf.pull_up_en = 0; //关闭上拉
//configure GPIO with the given settings
gpio_config(&io_conf);
//interrupt of rising edge
io_conf.intr_type = GPIO_INTR_POSEDGE; //设置中断为上升沿触发
//bit mask of the pins, use GPIO4/5 here
io_conf.pin_bit_mask = GPIO_INPUT_PIN_SEL;
//set as input mode
io_conf.mode = GPIO_MODE_INPUT;
//enable pull-up mode
io_conf.pull_up_en = 1;
gpio_config(&io_conf);
//change gpio intrrupt type for one pin
gpio_set_intr_type(GPIO_INPUT_IO_0, GPIO_INTR_ANYEDGE); //改变中断触发方式为双边沿触发
//create a queue to handle gpio event from isr
gpio_evt_queue = xQueueCreate(10, sizeof(uint32_t)); //创建长度为10的消息队列
//start gpio task
xTaskCreate(gpio_task_example, "gpio_task_example", 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(GPIO_INPUT_IO_0, gpio_isr_handler, (void*) GPIO_INPUT_IO_0); //添加中断回调函数
//hook isr handler for specific gpio pin
gpio_isr_handler_add(GPIO_INPUT_IO_1, gpio_isr_handler, (void*) GPIO_INPUT_IO_1);
//remove isr handler for gpio number.
gpio_isr_handler_remove(GPIO_INPUT_IO_0); //移除中断
//hook isr handler for specific gpio pin again
gpio_isr_handler_add(GPIO_INPUT_IO_0, gpio_isr_handler, (void*) GPIO_INPUT_IO_0); //重新添加中断
printf("Minimum free heap size: %d bytes\n", esp_get_minimum_free_heap_size());
int cnt = 0;
while(1) {
printf("cnt: %d\n", cnt++);
vTaskDelay(1000 / portTICK_RATE_MS);
gpio_set_level(GPIO_OUTPUT_IO_0, cnt % 2); //设置电平
gpio_set_level(GPIO_OUTPUT_IO_1, cnt % 2);
}
}
编译烧录
1、idf.py build编译生成bin文件
2、使用Flash Tools烧录
注意:先将开发板断电,再点击START或者ERASE,提示等待上电同步后,再上电。
3、也可以使用idf.py -p COM3 -b flash来烧录,使用idf.py -p COM3 erase_flash来擦除Flash
总结
到这里,就完成简单分析gpio例程。