安装开发工具
在官方网站下载 esp idf 开发工具,并按照要求安装。
创建项目
使用官方的 esp idf template 或者 https://github.com/twn39/esp-idf-template 创建项目,进行项目名称等参数更改,可参考 esp idf 目录自带的 example 中的 hello world 项目。
配置编译组件和选项
打开 idf 命令行,切换路径到项目目录,运行:
idf.py menuconfig
自定义编译命令和组件,配置完成之后会保存为 sdkconfig。
编写代码
这里参考 hello world 示例代码:
#include <stdio.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "driver/gpio.h"
#include "sdkconfig.h"
/* Can use project configuration menu (idf.py menuconfig) to choose the GPIO to blink,
or you can edit the following line and set a number here.
*/
#define BLINK_GPIO 2
void app_main()
{
/* Configure the IOMUX register for pad BLINK_GPIO (some pads are
muxed to GPIO on reset already, but some default to other
functions and need to be switched to GPIO. Consult the
Technical Reference for a list of pads and their default
functions.)
*/
gpio_pad_select_gpio(BLINK_GPIO);
/* Set the GPIO as a push/pull output */
gpio_set_direction(BLINK_GPIO, GPIO_MODE_OUTPUT);
while(1) {
/* Blink off (output low) */
printf("Turning off the LED\n");
gpio_set_level(BLINK_GPIO, 0);
vTaskDelay(1000 / portTICK_PERIOD_MS);
/* Blink on (output high) */
printf("Turning on the LED\n");
gpio_set_level(BLINK_GPIO, 1);
vTaskDelay(1000 / portTICK_PERIOD_MS);
}
}
编译,烧入和监控
编译:
idf.py build
烧入和监控:
idf.py -p COM5 -b 115200 flash monitor
需要注意的是这里用到的 esp32 波特率为 115200
, 默认的是 460800
。