人生似水岂无涯,浮云吹作雪,世味煮成茶...

Ubuntu下搭建ESP32编译环境

2022年06月08日 15:27    2 人评论    923 人阅读

前言

本文记录在Ubuntu下搭建ESP32编译环境的过程。

编译环境:Ubuntu18.04 开发板: NodeMCU-32S 核心开发板

安装必备工具

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

下载ESP-IDF

这里下载当前最新的稳定版本 v4.4.1

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

设置ESP-IDF工具

由于 github 访问比较慢,在安装工具时优先选择 Espressif 下载服务器,这样会比较快:

$ cd ~/esp/esp-idf
$ export IDF_GITHUB_ASSETS="dl.espressif.com/github_assets"
$ ./install.sh 

然后执行 install.sh 安装:

$ ./install.sh 

导出 ESP-IDF tools 环境变量:

$ . $HOME/esp/esp-idf/export.sh

为了方便你也可以将“ . $HOME/esp/esp-idf/export.sh”加入~/.bashrc文件里面,这样打开终端会自动执行,不需要每次都手段执行一遍。

测试ESP32环境是否正常

创建工程

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

连接设备

将 ESP32 开发板连接到 PC,并查看开发板使用的串口。

$ ls /dev/ttyUSB0
/dev/ttyUSB0

通常,串口在不同操作系统下显示的名称有所不同:

Windows 操作系统: COM1 等 Linux 操作系统: 以 /dev/tty 开始 MacOS 操作系统: 以 /dev/cu. 开始

配置工程

$ cd ~/esp/hello_world
$ idf.py menuconfig

编译工程

$ idf.py build

运行以上命令可以编译应用程序和所有 ESP-IDF 组件,接着生成 bootloader、分区表和应用程序二进制文件。

烧录固件

将刚刚生成的二进制文件烧录至您的 ESP32 开发板:

idf.py -p PORT [-b BAUD] flash

请将 PORT 替换为 ESP32 开发板的串口名称。

$ idf.py -p /dev/ttyUSB0 flash
Executing action: flash
Running ninja in directory /home/arkinux/work/esp/demos/hello_world/build
Executing "ninja flash"...
[1/5] cd /home/arkinux/work/esp/demos/...emos/hello_world/build/hello_world.bin
hello_world.bin binary size 0x297a0 bytes. Smallest app partition is 0x100000 bytes. 0xd6860 bytes (84%) free.
[2/5] Performing build step for 'bootloader'
[1/1] cd /home/arkinux/work/esp/demos/hello_world/build/bootloader/esp-idf/esptool_py && /home/arkinux/.espressif/python_env/idf4.4_py3.6_env/bin/python /home/arkinux/work/esp/esp-idf/components/partition_table/check_sizes.py --offset 0x8000 bootloader 0x1000 /home/arkinux/work/esp/demos/hello_world/build/bootloader/bootloader.bin
Bootloader binary size 0x62c0 bytes. 0xd40 bytes (12%) free.
[2/3] cd /home/arkinux/work/esp/esp-id...nents/esptool_py/run_serial_tool.cmake
esptool.py esp32 -p /dev/ttyUSB0 -b 460800 --before=default_reset --after=hard_reset write_flash --flash_mode dio --flash_freq 40m --flash_size 2MB 0x1000 bootloader/bootloader.bin 0x10000 hello_world.bin 0x8000 partition_table/partition-table.bin
esptool.py v3.3-dev
Serial port /dev/ttyUSB0
Connecting...................................

当出现 Connecting... 时,长按 IO0 按键复位后开始烧录软件,一切顺利的话,会有以下信息:

Chip is ESP32-D0WDQ6 (revision 1)
Features: WiFi, BT, Dual Core, Coding Scheme None
Crystal is 40MHz
MAC: 24:0a:c4:10:d3:cc
Uploading stub...
Running stub...
Stub running...
Changing baud rate to 460800
Changed.
Configuring flash size...
Flash will be erased from 0x00001000 to 0x00007fff...
Flash will be erased from 0x00010000 to 0x00039fff...
Flash will be erased from 0x00008000 to 0x00008fff...
Compressed 25280 bytes to 15815...
Writing at 0x00001000... (100 %)
Wrote 25280 bytes (15815 compressed) at 0x00001000 in 0.7 seconds (effective 280.2 kbit/s)...
Hash of data verified.
Compressed 169888 bytes to 89569...
Writing at 0x00010000... (16 %)
Writing at 0x0001b095... (33 %)
Writing at 0x00020820... (50 %)
Writing at 0x00025edf... (66 %)
Writing at 0x0002e634... (83 %)
Writing at 0x00036a00... (100 %)
Wrote 169888 bytes (89569 compressed) at 0x00010000 in 2.5 seconds (effective 545.5 kbit/s)...
Hash of data verified.
Compressed 3072 bytes to 103...
Writing at 0x00008000... (100 %)
Wrote 3072 bytes (103 compressed) at 0x00008000 in 0.0 seconds (effective 526.5 kbit/s)...
Hash of data verified.

Leaving...
Hard resetting via RTS pin...
Done

如果提示[Errno 13] could not open port /dev/ttyUSB0: [Errno 13] Permission denied: '/dev/ttyUSB0',是因为不是root用户,对端口没有权限,只需要:

$ sudo chmod 777 /dev/ttyUSB0

修改权限为可读可写可执行,但是这种设置电脑重启后,又会出现这种问题,还要重新设置。因此我们需要使用另一种办法,使其设置永久生效:

$ sudo usermod -a -G dialout $USER

这是把当前登录用户添加到 dialout 组,从而获许读写权限。注意,第一次执行以上命令后需要重启 Ubuntu 虚拟机才能生效。

监视打印信息

您可以使用 idf.py -p PORT monitor 命令,监视 hello_world 的运行情况。注意,不要忘记将 PORT 替换为您的串口名称。

运行该命令后,IDF 监视器 应用程序将启动:

$ idf.py -p /dev/ttyUSB0 monitor
Executing action: monitor
Running idf_monitor in directory /home/arkinux/work/esp/demos/hello_world
Executing "/home/arkinux/.espressif/python_env/idf4.4_py3.6_env/bin/python /home/arkinux/work/esp/esp-idf/tools/idf_monitor.py -p /dev/ttyUSB0 -b 115200 --toolchain-prefix xtensa-esp32-elf- --target esp32 --revision 0 /home/arkinux/work/esp/demos/hello_world/build/hello_world.elf -m '/home/arkinux/.espressif/python_env/idf4.4_py3.6_env/bin/python' '/home/arkinux/work/esp/esp-idf/tools/idf.py' '-p' '/dev/ttyUSB0'"...
--- idf_monitor on /dev/ttyUSB0 115200 ---
--- Quit: Ctrl+] | Menu: Ctrl+T | Help: Ctrl+T followed by Ctrl+H ---
ets Jun  8 2016 00:22:57

rst:0x1 (POWERON_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)
configsip: 0, SPIWP:0xee
clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00
mode:DIO, clock div:2
load:0x3fff0030,len:6612
load:0x40078000,len:14788
load:0x40080400,len:3792
0x40080400: _init at ??:?

entry 0x40080694
I (27) boot: ESP-IDF v4.4.1-dirty 2nd stage bootloader
I (27) boot: compile time 12:03:04
I (27) boot: chip revision: 1
I (30) boot_comm: chip revision: 1, min. bootloader chip revision: 0
I (37) boot.esp32: SPI Speed      : 40MHz
I (42) boot.esp32: SPI Mode       : DIO
I (46) boot.esp32: SPI Flash Size : 2MB
I (51) boot: Enabling RNG early entropy source...
I (56) boot: Partition Table:
I (60) boot: ## Label            Usage          Type ST Offset   Length
I (67) boot:  0 nvs              WiFi data        01 02 00009000 00006000
I (74) boot:  1 phy_init         RF data          01 01 0000f000 00001000
I (82) boot:  2 factory          factory app      00 00 00010000 00100000
I (89) boot: End of partition table
I (94) boot_comm: chip revision: 1, min. application chip revision: 0
I (101) esp_image: segment 0: paddr=00010020 vaddr=3f400020 size=07874h ( 30836) map
I (120) esp_image: segment 1: paddr=0001789c vaddr=3ffb0000 size=02340h (  9024) load
I (124) esp_image: segment 2: paddr=00019be4 vaddr=40080000 size=06434h ( 25652) load
I (139) esp_image: segment 3: paddr=00020020 vaddr=400d0020 size=14988h ( 84360) map
I (170) esp_image: segment 4: paddr=000349b0 vaddr=40086434 size=04db4h ( 19892) load
I (178) esp_image: segment 5: paddr=0003976c vaddr=50000000 size=00010h (    16) load
I (184) boot: Loaded app from partition at offset 0x10000
I (184) boot: Disabling RNG early entropy source...
I (198) cpu_start: Pro cpu up.
I (199) cpu_start: Starting app cpu, entry point is 0x40080ff4
0x40080ff4: call_start_cpu1 at /home/arkinux/work/esp/esp-idf/components/esp_system/port/cpu_start.c:160

I (0) cpu_start: App cpu up.
I (213) cpu_start: Pro cpu start user code
I (213) cpu_start: cpu freq: 160000000
I (213) cpu_start: Application information:
I (217) cpu_start: Project name:     hello_world
I (223) cpu_start: App version:      1
I (227) cpu_start: Compile time:     Jun  3 2022 12:03:00
I (233) cpu_start: ELF file SHA256:  ca612e48c27dd695...
I (239) cpu_start: ESP-IDF:          v4.4.1-dirty
I (245) heap_init: Initializing. RAM available for dynamic allocation:
I (252) heap_init: At 3FFAE6E0 len 00001920 (6 KiB): DRAM
I (258) heap_init: At 3FFB2C28 len 0002D3D8 (180 KiB): DRAM
I (264) heap_init: At 3FFE0440 len 00003AE0 (14 KiB): D/IRAM
I (270) heap_init: At 3FFE4350 len 0001BCB0 (111 KiB): D/IRAM
I (277) heap_init: At 4008B1E8 len 00014E18 (83 KiB): IRAM
I (284) spi_flash: detected chip: gd
I (287) spi_flash: flash io: dio
W (291) spi_flash: Detected size(4096k) larger than the size in the binary image header(2048k). Using the size in the binary image header.
I (305) cpu_start: Starting scheduler on PRO CPU.
I (0) cpu_start: Starting scheduler on APP CPU.
Hello world!
This is esp32 chip with 2 CPU core(s), WiFi/BT/BLE, silicon revision 1, 2MB external flash
Minimum free heap size: 295372 bytes
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.

可以看到 Hello world!,并且10秒之后开发板会自动重启。 使用快捷键 Ctrl+],可以退出 IDF 监视器。

参考

ESP-IDF 编程指南


如果文章对您有所帮助, 请随意打赏! 您的支持将鼓励我写出更好的文章!

发表评论 已发布 2


cialis online prescription    2023/10/31 06:04

wonderful points altogether, you simply won a brand new reader. What may you recommend in regards to your submit that you made some days in the past? Any positive?

ordiple    2023/10/18 06:33

<a href=http://sildenafi.cfd>female version of viagra</a> My testosterone level was 1130ng dl and my LH was borderline high as well