上一章我们搭建好了开发环境,并且用串口输出了hello world, 接下来我们先点灯
参考资料: 官方入门教程: https://docs.espressif.com/projects/esp-idf/zh_CN/latest/esp32/get-started/index.html#get-started-start-project
一 移植示例工程
1. 移植generic_gpio工程
将esp-idf\examples\peripherals\gpio文件夹下的generic_gpio文件夹复制到某一个路径下(不能有空格),我的在 F:\duke_work\ESP_IDF下, 下面是我的文件夹结构:
2. 设置目标芯片
打开一个新项目后,应首先设置“目标”芯片 idf.py set-target esp32。注意,此操作将清除并初始化项目之前的编译和配置(如有)。 您也可以直接将“目标”配置为环境变量(此时可跳过该步骤)。更多信息,请见 选择目标芯片。CMD下面输入:
F:\duke_work\ESP_IDF\esp-idf\tools\idf.py set-target esp32
idf.py的路径在esp-idf\tools下, 请将你的路径替换掉
3. 编译工程
编译命令:
F:\duke_work\ESP_IDF\esp-idf\tools\idf.py build
同理: 将idf.py的路径替换为你的, 漫长的编译过程。
4. 烧录固件
F:\duke_work\ESP_IDF\esp-idf\tools\idf.py -p COM9 flash
请将idf的路径替换掉, 还有串口号(COM9)也替换掉
烧录成功!!!
二. 修改GPIO管脚
我的硬件是esp32-wroom-32
根据原理图可知,可编程GPIO为GPIO2, 并且为高电平触发
代码参考:gpio_example_main.c
/* 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 2
#define GPIO_OUTPUT_IO_1 19
#define GPIO_OUTPUT_PIN_SEL GPIO_SEL_2
static xQueueHandle gpio_evt_queue = NULL;
static void gpio_task_example(void* arg)
{
uint32_t io_num;
for(;;) {
printf("turn off led0!\r\n");
gpio_set_level(GPIO_OUTPUT_IO_0, 0);
vTaskDelay(1000 / portTICK_RATE_MS);
printf("turn on led0!\r\n");
gpio_set_level(GPIO_OUTPUT_IO_0, 1);
vTaskDelay(1000 / portTICK_RATE_MS);
}
}
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);
//start gpio task
xTaskCreate(gpio_task_example, "gpio_task_example", 2048, NULL, 10, NULL);
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);
}
}
关键api解读:(对应官方资料的章节)
- esp_err_tgpio_config(constgpio_config_t *pGPIOConfig)
该函数配置GPIO的一些属性:是否使能中断,输入输出模式,上下拉
这里需要注意的是pin_bit_mask参数, 这个参数表示哪个管脚被选中使用 - esp_err_tgpio_set_level(gpio_num_tgpio_num, uint32_t level)
参数写的很清楚了。
代码中涉及的其他函数都为freeRTOS相关的, 这部分需要这方面的基础, 在这不做过多的讲解。
烧入完程序后可以看到GPIO2会亮灭。