ESP32:搭建Ubuntu開發環境

圖片描述

安裝工具鏈

整個ESP-IDF環境須要用到不少軟件,使用以下命令安裝:html

$ sudo apt-get install git wget make libncurses-dev flex bison gperf python python-serial

64位操系統和32位操做系統所使用的工具鏈不一樣,因此根據系統選擇對應的工具鏈安裝。這裏使用wget命令下載64位的工具鏈,並解壓安裝:python

$ 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操做,讓該腳本當即生效。linux

$ echo "export PATH=$PATH:/opt/esp/xtensa-esp32-elf/bin" >> ~/.bashrc

下載編譯

ESP-IDF的全稱是Espressif IoT Development Framework(樂鑫IoT開發框架),即一般所說的SDK,它裏面提供了豐富的可供調用的庫和API。下載SDKgit

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

clone後面的參數--recursive表示遞歸克隆該倉庫的子倉庫,這是一種在開源界很是流行的作法,能夠方便地對多個倉庫進行控制。在clone時使用到這個參數,也可使用下面的命令來手動更新該倉庫的子模塊:github

$ git submodule update --init

ESP-IDF下載完成後,還須要導出環境變量IDF_PATH,從此在編譯工程代碼時會使用這個變量來查找ESP-IDF所在路徑。一樣使用echo命令將其追加到腳本文件~/.bashrc,完成後執行source ~/.bashrc命令。bash

$ echo "export IDF_PATH=/srv/esp/esp-idf" >> ~/.bashrc

ESP-IDF提供了豐富的demo程序,位於路徑examples下面。這裏先試試hello world,進入源碼所在目錄,而後編譯:app

$ cd examples/get-started/hello_world/
$ make

若是是第一次執行make命令,系統會彈出一個圖形配置界面,以下圖所示:
圖片描述框架

能夠經過鍵盤上的tab鍵在下面的菜單中進行跳轉。當光標跳轉到<Exit>菜單後,按下回車鍵,退出配置菜單,而後系統會自動編譯ESP-IDF目錄下的各個組件以及hello world目錄下的源碼。編譯完成後,能夠在hello wolrd工程所在目錄下生成一個build文件,用於存放編譯過程當中生成的臨時文件以及最終能夠直接燒寫到開發板的.bin文件。此外,系統還會直接在 hello world工程所在目錄生成一個配置文件sdkconfig,它存放了在配置界面所配置的各類值。tcp

燒錄

USB將開發板與PC鏈接在一塊兒,系統會識別到設備/dev/ttyUSB0,先修改該設備的權限,而後燒寫並監視串口輸出:ide

$ 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),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:0x00
clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00
mode:DIO, 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 入門試玩以前言

相關文章
相關標籤/搜索