安装工具链
整个ESP-IDF
环境需要用到很多软件,使用如下命令安装:
$ sudo apt-get install git wget make libncurses-dev flex bison gperf python python-serial
64
位操系统和32
位操作系统所使用的工具链不同,所以根据系统选择对应的工具链安装。这里使用wget
命令下载64
位的工具链,并解压安装:
$ mkdir -p /opt/esp $ cd /opt/esp/ $ wget https://dl.espressif.com/dl/xtensa-esp32-elf-linux64-1.22.0-73-ge28a011-5.2.0.tar.gz $ tar -xzvf xtensa-esp32-elf-linux64-1.22.0-73-ge28a011-5.2.0.tar.gz
解压后的工具链位于路径/opt/esp/xtensa-esp32-elf
下面,该目录下面的bin
目录就是用来编译ESP-IDF
工程所需的工具。如果要使系统能够直接使用这些程序,需要将这个bin
文件夹路径添加到系统的环境变量PATH
中去,这里直接使用echo
命令将其添加到脚本文件~/.bashrc
中(系统每次启动时都会执行这个脚本)。第一次添加后需要对该脚本执行source ~/.bashrc
操作,让该脚本立即生效。
$ echo "export PATH=$PATH:/opt/esp/xtensa-esp32-elf/bin" >> ~/.bashrc
下载编译
ESP-IDF
的全称是Espressif IoT Development Framework
(乐鑫IoT
开发框架),即通常所说的SDK
,它里面提供了丰富的可供调用的库和API
。下载SDK
:
$ mkdir -p /srv/esp $ cd /srv/esp $ git clone --recursive https://github.com/espressif/esp-idf.git
clone
后面的参数--recursive
表示递归克隆该仓库的子仓库,这是一种在开源界非常流行的做法,可以方便地对多个仓库进行控制。在clone
时使用到这个参数,也可以使用下面的命令来手动更新该仓库的子模块:
$ git submodule update --init
ESP-IDF
下载完成后,还需要导出环境变量IDF_PATH
,今后在编译工程代码时会使用这个变量来查找ESP-IDF
所在路径。同样使用echo
命令将其追加到脚本文件~/.bashrc
,完成后执行source ~/.bashrc
命令。
$ echo "export IDF_PATH=/srv/esp/esp-idf" >> ~/.bashrc
ESP-IDF
提供了丰富的demo
程序,位于路径examples
下面。这里先试试hello world
,进入源码所在目录,然后编译:
$ cd examples/get-started/hello_world/ $ make
如果是第一次执行make
命令,系统会弹出一个图形配置界面,如下图所示:
可以通过键盘上的tab
键在下面的菜单中进行跳转。当光标跳转到<Exit>
菜单后,按下回车键,退出配置菜单,然后系统会自动编译ESP-IDF
目录下的各个组件以及hello world
目录下的源码。编译完成后,可以在hello wolrd
工程所在目录下生成一个build
文件,用于存放编译过程中生成的临时文件以及最终可以直接烧写到开发板的.bin
文件。此外,系统还会直接在 hello world
工程所在目录生成一个配置文件sdkconfig
,它存放了在配置界面所配置的各种值。
烧录
用USB
将开发板与PC
连接在一起,系统会识别到设备/dev/ttyUSB0
,先修改该设备的权限,然后烧写并监视串口输出:
$ sudo chmod 777 /det/ttyUSB0 $ make flash monitor Flashing binaries to serial port /dev/ttyUSB0 (app at offset 0x10000)... esptool.py v2.1 Connecting........_ Chip is ESP32D0WDQ5 (revision 0) Uploading stub... Running stub... Stub running... Configuring flash size... Auto-detected Flash size: 4MB Compressed 19600 bytes to 11523... Wrote 19600 bytes (11523 compressed) at 0x00001000 in 1.0 seconds (effective 153.3 kbit/s)... Hash of data verified. Compressed 130336 bytes to 69700... Wrote 130336 bytes (69700 compressed) at 0x00010000 in 6.2 seconds (effective 169.1 kbit/s)... Hash of data verified. Compressed 3072 bytes to 82... Wrote 3072 bytes (82 compressed) at 0x00008000 in 0.0 seconds (effective 2107.5 kbit/s)... Hash of data verified. Leaving... Hard resetting... MONITOR --- 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) ets Jun 8 2016 00:22:57 rst:0x10 (RTCWDT_RTC_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:0x3fff0018,len:4 load:0x3fff001c,len:5692 load:0x40078000,len:0 load:0x40078000,len:13804 entry 0x40079030 I (68) boot: Detected ESP32 I (32) boot: ESP-IDF v3.0-dev-1295-g08be5213 2nd stage bootloader I (32) boot: compile time 23:46:05 I (32) boot: Enabling RNG early entropy source... I (38) boot: SPI Speed : 40MHz I (42) boot: SPI Mode : dio I (46) boot: SPI Flash Size : 4MB I (50) boot: Partition Table: I (53) boot: ## Label Usage Type ST Offset Length I (61) boot: 0 nvs WiFi data 01 02 00009000 00006000 I (68) boot: 1 phy_init RF data 01 01 0000f000 00001000 I (76) boot: 2 factory factory app 00 00 00010000 00100000 I (83) boot: End of partition table I (87) esp_image: segment 0: paddr=0x00010020 vaddr=0x3f400020 size=0x04ad0 ( 19152) map I (103) esp_image: segment 1: paddr=0x00014af8 vaddr=0x3ffb0000 size=0x02118 ( 8472) load I (108) esp_image: segment 2: paddr=0x00016c18 vaddr=0x40080000 size=0x00400 ( 1024) load 0x40080000: _iram_start at /srv/esp/esp-idf/components/freertos/./xtensa_vectors.S:1685 I (114) esp_image: segment 3: paddr=0x00017020 vaddr=0x40080400 size=0x081f4 ( 33268) load I (137) esp_image: segment 4: paddr=0x0001f21c vaddr=0x400c0000 size=0x00000 ( 0) load I (137) esp_image: segment 5: paddr=0x0001f224 vaddr=0x00000000 size=0x00dec ( 3564) I (144) esp_image: segment 6: paddr=0x00020018 vaddr=0x400d0018 size=0x0fcdc ( 64732) map 0x400d0018: _stext at ??:? I (180) boot: Loaded app from partition at offset 0x10000 I (180) boot: Disabling RNG early entropy source... I (180) cpu_start: Pro cpu up. I (184) cpu_start: Starting app cpu,entry point is 0x40080fe0 0x40080fe0: call_start_cpu1 at /srv/esp/esp-idf/components/esp32/./cpu_start.c:222 I (0) cpu_start: App cpu up. I (194) heap_init: Initializing. RAM available for dynamic allocation: I (201) heap_init: At 3FFAE6E0 len 00001920 (6 KiB): DRAM I (207) heap_init: At 3FFB2920 len 0002D6E0 (181 KiB): DRAM I (213) heap_init: At 3FFE0440 len 00003BC0 (14 KiB): D/IRAM I (220) heap_init: At 3FFE4350 len 0001BCB0 (111 KiB): D/IRAM I (226) heap_init: At 400885F4 len 00017A0C (94 KiB): IRAM I (232) cpu_start: Pro cpu start user code I (250) 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 cores,WiFi/BT/BLE,silicon revision 0,4MB 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.
注意,再执行make flash monitor
时,若是其它应用打开了串口,则应该先把它给关闭,否则该命令不能成功执行。
首次开箱,开发板启动后,串口信息如下:
rst:0x1 (POWERON_RESET),SPIWP:0x00 clk_drv:0x00,clock div:2 load:0x3ffc0000,len:0 load:0x3ffc0000,len:2304 load:0x40078000,len:3788 ho 0 tail 12 room 4 load:0x40098000,len:532 entry 0x4009813c ************************************** * hello espressif ESP32! * * 2nd boot is running! * * version (V0.1) * ************************************** compile time 18:16:58 SPI Speed : 40MHz SPI Mode : dio SPI Flash Size : 4MB Partition Table: ## Label Usage Type ST Offset Length 0 factory factory app 00 00 00010000 00100000 1 rfdata RF data 01 01 00110000 00040000 2 wifidata WiFi data 01 02 00150000 00040000 End of partition table Loading app partition at offset 00010000 section 0: paddr=0x00000020 vaddr=0x00000000 size=0x0ffe8 ( 65512) section 1: paddr=0x00010010 vaddr=0x3f400010 size=0x05b64 ( 23396) map section 2: paddr=0x00015b7c vaddr=0x3ffba720 size=0x01378 ( 4984) load section 3: paddr=0x00016efc vaddr=0x40080000 size=0x00400 ( 1024) load section 4: paddr=0x00017304 vaddr=0x40080400 size=0x126ac ( 75436) load section 5: paddr=0x000299b8 vaddr=0x00000000 size=0x06658 ( 26200) section 6: paddr=0x00030018 vaddr=0x400d0018 size=0x325b4 (206260) map start: 0x400807ac Initializing heap allocator: Region 19: 3FFBBA98 len 00024568 tag 0 Region 25: 3FFE8000 len 00018000 tag 1 Pro cpu up. Pro cpu start user code nvs_flash_init frc2_timer_task_hdl:3ffbc564,prio:22,stack:2048 tcpip_task_hdlxxx : 3ffbeca8,prio:20,stack:2048 phy_version: 80,Aug 26 2016,13:04:06,0 pp_task_hdl : 3ffc34f0,prio:23,stack:8192 :>enter uart init uart init wait fifo succeed exit uart init IDF version : master(db93bceb) WIFI LIB version : master(934d079b) ssc version : master(r283 4d376412) !!!ready!!! mode : softAP(32:ae:a4:80:53:f0) dhcp server start:(ip: 192.168.4.1,mask: 255.255.255.0,gw: 192.168.4.1) +WIFI:AP_START
参考文章
ESP-IDF Programming Guide
搭建 ESP32 开发环境 —— Hello World
深入浅出ESP32开发之二ESP-IDF下载、编译与运行
ESP32开发笔记-跟着小狂玩ESP32之编译环境搭建篇
ESP32开发(1)-Windows搭建ESP32开发环境
ESP32 入门试玩之前言