相关资料

ESP-IDF编程指南
ESP-IDF代码仓库


安装环境

安装依赖

Ubuntu: 更新apt,并安装依赖。

sudo apt update    
sudo apt upgrade
sudo apt-get install -y git wget flex bison gperf python3 python3-pip python3-setuptools cmake ninja-build ccache libffi-dev libssl-dev dfu-util libusb-1.0-0

CentOS7: 更新yum源,并安装依赖。

sudo yum -y update && sudo yum install git wget flex bison gperf python3 cmake ninja-build ccache dfu-util

Python: ESP需要使用Python3。查看Python版本python --version

如果默认python为python2,点开此选项。


Ubuntu:

# 安装 Python 3:
sudo apt-get install python3 python3-pip python3-setuptools
# 设置 Python 3 为默认 Python 版本:
sudo update-alternatives --install /usr/bin/python python /usr/bin/python3 10 && alias pip=pip3

CentOS7:

# 安装 Python 3:
sudo yum -y update && sudo yum install python3 python3-pip python3-setuptools
# 设置 Python 3 为默认 Python 版本:
sudo update-alternatives --install /usr/bin/python python /usr/bin/python3 10 && alias pip=pip3


配置 ESP-IDF

下载 ESP-IDF

下载代码仓库

mkdir -p ~/esp
cd ~/esp
git clone --recursive https://github.com/espressif/esp-idf.git

版本选择最新发布版,这篇文章时,最新发布版本为 v4.2,对应的commitc40f259

git checkout v4.2
git submodule update --init --recursive

或者直接下载对应版本,如:

git clone -b v4.2 --recursive https://github.com/espressif/esp-idf.git

配置使用工具

除了 ESP-IDF 本身,您还需要安装 ESP-IDF 使用的各种工具,比如编译器、调试器、Python 包等。
这些进入esp-idf目录,执行对应脚本即可。

cd ~/esp/esp-idf
./install.sh

配置环境变量

执行脚本,配置环境变量。注意:配置的是IDF虚拟环境,关闭终端后再次使用需重新配置。

. ./export.sh  #或者执行  . $HOME/esp/esp-idf/export.sh

查看环境变量

env

创建工程

拷贝Hello Word!工程

ESP有很多工程可以参考,我们以hello_world为例。
拷贝hello_world工程到esp目录

cd ~/esp
cp -r $IDF_PATH/examples/get-started/hello_world .

配置工程

设置目标芯片,这里使用的是ESP32

cd ~/esp/hello_world
idf.py set-target esp32
esp32 — 适用于 ESP32-D0WD、ESP32-D2WD、ESP32-S0WD、ESP32-U4WDH、ESP32-PICO-D4
esp32s2— 适用于 ESP32-S2

运行工程配置工具 menuconfig

可以通过此菜单设置项目的具体变量,包括 Wi-Fi 网络名称、密码和处理器速度等。
hello_world 示例项目会以默认配置运行,因此也可以跳过使用 menuconfig 进行项目配置这一步骤。

idf.py menuconfig


menuconfig列表。

     SDK tool configuration  --->
     Build type  --->
     Application manager  --->
     Bootloader config  --->
     Security features  --->
     Serial flasher config  --->
     Partition Table  --->
     Compiler options  --->
     Component config  --->
     Compatibility options  --->

如果您使用的是 ESP32-DevKitC(板载 ESP32-SOLO-1 模组),请在烧写示例程序前,前往 menuconfig 中使能单核模式(CONFIG_FREERTOS_UNICORE)。

编译下载工程

编译工程

首次编译时间较长,耐心等待。

idf.py build

运行以上命令可以编译应用程序和所有 ESP-IDF 组件,
相当于执行了idf.py app idf.py bootloader idf.py partition_table
分别生成 bootloader bootloader.bin分区表 partition-table.bin应用程序二进制bin文件 hello-world.bin

查看端口

查看板子连接端口提示如下:

dmesg | grep tty
# [    0.083320] printk: console [tty0] enabled
# [    0.771868] 00:05: ttyS0 at I/O 0x3f8 (irq = 4, base_baud = 115200) is a 16550A
# [79380.712416] usb 2-2.2: cp210x converter now attached to ttyUSB0

此时设备连接端口为ttyUSB0

烧录到设备

应用烧录指令idf.py -p PORT [-b BAUD] flash
PORT : 是我们连接开发板的串口名称;
BAUD : 为烧录的波特率[选填],默认波特率为 460800。

idf.py -p /dev/ttyUSB0 flash 

查看监视器

使用 idf.py -p PORT monitor 命令,监视 hello_world 工程的运行情况。

idf.py -p /dev/ttyUSB0 monitor

可使用快捷键 Ctrl+],退出 IDF 监视器。


监视器内容

Hello world!
This is ESP32 chip with 2 CPU cores, WiFi/BT/BLE, silicon revision 1, 2MB external flash
Restarting in 10 seconds...
Restarting in 9 seconds...
Restarting in 8 seconds...
Restarting in 7 seconds...
Restarting in 6 seconds...
Restarting in 5 seconds...
Restarting in 4 seconds...
Restarting in 3 seconds...
Restarting in 2 seconds...
Restarting in 1 seconds...
Restarting in 0 seconds...
Restarting now.
ets Jun  8 2016 00:22:57

更改项目

更改打印输出的内容

cd ~/esp/hello_world/main
vim hello_world_main.c


hello_world_main.c内容

/* Hello World 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 "sdkconfig.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "esp_system.h"
#include "esp_spi_flash.h"

#ifdef CONFIG_IDF_TARGET_ESP32
#define CHIP_NAME "ESP32"
#endif

#ifdef CONFIG_IDF_TARGET_ESP32S2BETA
#define CHIP_NAME "ESP32-S2 Beta"
#endif

void app_main(void)
{
    printf("Hello DaoBanMoJie!\n");
    printf("http://daobanmojie.com/19.html\n");

    /* Print chip information */
    esp_chip_info_t chip_info;
    esp_chip_info(&chip_info);
    printf("This is %s chip with %d CPU cores, WiFi%s%s, ",
            CHIP_NAME,
            chip_info.cores,
            (chip_info.features & CHIP_FEATURE_BT) ? "/BT" : "",
            (chip_info.features & CHIP_FEATURE_BLE) ? "/BLE" : "");

    printf("silicon revision %d, ", chip_info.revision);

    printf("%dMB %s flash\n", spi_flash_get_chip_size() / (1024 * 1024),
            (chip_info.features & CHIP_FEATURE_EMB_FLASH) ? "embedded" : "external");

    for (int i = 10; i >= 0; i--) {
        printf("Restarting in %d seconds...\n", i);
        vTaskDelay(1000 / portTICK_PERIOD_MS);
    }
    printf("Restarting now.\n");
    fflush(stdout);
    esp_restart();
}


使用idf.py app,仅编译app内容,然后烧录并打印到监视器。

cd ~/esp/hello_world/
idf.py -p /dev/ttyUSB0 app-flash monitor # 仅构建并下载应用程序,之后打开监视器。

到这里整个工程搭建就结束了。


以下为扩展内容


构建系统idf.py

idf.py


idf.py 应用在包含 CMakeLists.txt 文件的项目目录下。
idf.py 命令行工具提供了一个前端,可以帮助您轻松管理项目的构建过程,它管理了以下工具:
CMake : 配置待构建的项目
NinjaGNU Make: 命令行构建工具
esptool.py: 烧录目标硬件设备

idf.py --help 查看完整的命令列表。
idf.py <command> --help 命令列出针对某一子命令的选项。
下面总结了最常用的命令:

命令含义
idf.py set-target <target>设置构建项目的目标芯片。
idf.py menuconfig运行 menuconfig 工具来配置项目。
idf.py build在当前项目下构建工程。
包含如下几部操作:
创建build构建目录
运行CMake构建项目
运行构建工具Ninja或GUN Make
改指令相当于:
idf.py app+idf.py bootloader+idf.py partition_table
idf.py app构建项目的应用程序
idf.py bootloader构建项目的引导程序
idf.py partition_table构建项目的分区表
idf.py flash需要构建时自动构建项目,并将生成的二进制程序烧录进目标 ESP32 设备中。
-p-b选项可分别设置串口的设备名和烧录时的波特率。
常用idf.py -p /dev/ttyUSB0 flash
idf.py app-flash仅烧写应用程序,如果应用程序有更改,构建后下载。
idf.py monitor显示目标 ESP32 设备的串口输出。
-p 选项可用于设置主机端串口的设备名
按下 Ctrl-] 可退出监视器
idf.py clean清除构建生成文件,不会删除 CMake 配置输出及其他文件
idf.py fullclean会将整个 build 目录下的内容全部删除
包括所有 CMake 的配置和构建生成文件
idf.py size打印应用程序相关的大小信息
idf.py -p PORT erase_flash会使用 esptool.py 擦除 ESP32 的整个 Flash。

idf.py 选项:

  • -C <dir> 可用来从默认的当前工作目录覆盖项目目录。
  • -B <dir> 可用来从项目目录默认的 build 子目录覆盖构建目录。
  • --ccache 可用来在编译源文件时启用 CCache,安装了 CCache 工具后可极大缩短编译时间。
  • -v 可以让 idf.py 和编译系统产生详细的编译输出,对于调试编译问题会非常有用。


错误解决

CMake版本过低

CMake版本过低


通常发生在CentOS系统,因为CentOS为求稳定,通常版本较低,升级CMake版本即可。
这里以升级到CMake V3.6为例。

# CMake目录 https://cmake.org/files/
# 下载Cmake
wget https://cmake.org/files/v3.6/cmake-3.6.2.tar.gz
# 解压Cmake
tar xvf cmake-3.6.2.tar.gz && cd cmake-3.6.2/
# 编译安装cmake
./bootstrap
gmake
gmake install
# 查看编译后的cmake版本
/usr/local/bin/cmake --version
# 移除原来的cmake版本
yum remove cmake -y
# 新建软连接
ln -s /usr/local/bin/cmake /usr/bin/
# 终端查看版本
cmake --version

ninja: build stopped: subcommand failed.

ninja: build stopped: subcommand failed.


错误提示:
ninja: no work to do.
ninja: build stopped: subcommand failed.
错误原因:
一、可能是ESP-IDF的子模块没有更新
解决办法:更新ESP-IDF的submodule模块。

cd $IDF_PATH             # 进入ESP-IDF 目录 
git submodule update

二、编译环境不完整
解决办法:更改编译方式使用make指令

make flash monitor

打开串口失败

打开串口失败


错误提示:
serial.serialutil.SerialException: [Errno 13] could not open port /dev/ttyUSB0: [Errno 13] Permission denied: '/dev/ttyUSB0'
产看一下端口发现权限不够

ll /dev/ttyUSB0 
crw-rw---- 1 root dialout 188, 0 3月  24 14:10 /dev/ttyUSB0

解决办法:
方法一:临时,插拔后需要重新配置

sudo chmod -R 777 /dev/ttyUSB0

方法二:
创建文件

sudo gedit /etc/udev/rules.d/70-ttyusb.rules

在文件内增加一行,修改串口的访问权限:

KERNEL=="ttyUSB[0-9]*", MODE="0666",GROUP="plugdev"

修改70-ttyusb.rules文件的权限:

sudo chmod -R 777 70-ttyusb.rules

将登陆的账户添加到plugdev

sudo usermod -a -G plugdev yjw

产看用户所在组

yjw@ubuntu:~$ sudo usermod -a -G plugdev yjw
yjw@ubuntu:~$ groups yjw
yjw : yjw adm dialout cdrom sudo dip plugdev lpadmin lxd sambashare

重新加载 udev 规则

sudo udevadm control --reload

mbedtls错误

FAILED: esp-idf/mbedtls/x509_crt_bundle


错误提示:

FAILED: esp-idf/mbedtls/x509_crt_bundle 

解决办法:

方法一:
打开sdkconfig文件,找到如下:

CONFIG_MBEDTLS_CERTIFICATE_BUNDLE_DEFAULT_FULL=y
# CONFIG_MBEDTLS_CERTIFICATE_BUNDLE_DEFAULT_CMN is not set

更改为

#YJW CONFIG_MBEDTLS_CERTIFICATE_BUNDLE_DEFAULT_FULL=y
CONFIG_MBEDTLS_CERTIFICATE_BUNDLE_DEFAULT_CMN=y

方法二:
打开idf.py menuconfig按照如下路径选择
Component config --->
mbedTLS --->
Certificate Bundle --->
[*] Enable trusted root certificate bundle
Default certificate bundle options (Use the full default certificate bundle) --->
选择(X) Use only the most common certificates from the default bundles
之后保存退出。

( ) Use the full default certificate bundle
(X) Use only the most common certificates from the default bundles
( ) Do not use the default certificate bundle

最后修改:2021 年 12 月 29 日
男宾一位~ 欢迎下次再来!