ESP32開發之路(1)— 基於idf框架的HelloWorld工程
本次開發基於Ubuntu虛擬機
一、ESP32簡介
首先,上圖,看一下我手上的ESP32開發板
其中使用的是ESP32-WROOM-32模組,該模組內置 ESP32-D0WDQ6 芯片,是一款基礎且常用的 ESP32 模組。ESP32-WROOM-32模組 集成了4 MB 的SPI flash。然後我們看一下官方技術手冊對ESP32-D0WDQ6芯片 CPU 和片上存儲的描述:
簡單來說就是4MB的外部SPI Flash,448KB的內部ROM,520KB的內部SRAM
。
下圖是ESP32的地址映射圖:
詳細信息可以參考官方編程指南
二、安裝準備
首先,安裝依賴:
sudo apt-get install git wget flex bison gperf python python-pip python-setuptools python-serial python-click python-cryptography python-future python-pyparsing python-pyelftools cmake ninja-build ccache libffi-dev libssl-dev
然後還有工具鏈的依賴
sudo apt-get install git make gcc libncurses5-dev flex bison gperf python-serial
對於不同的Ubuntu版本可能會出現不一樣的情況,不過按照提示進行安裝應該就可以了
三、獲取源碼
用git命令來拉取下來,這就可以可以同步最新的框架版本!不能直接一個downLoad 下載,發現會有錯誤,切記!
不過,樂鑫的開放源碼都放在GitHub,而GitHub又被微軟收購了,這一年的訪問下載速度慢得烏龜一樣,所以,這裏有位大佬提供了一個可供代替的方法https://blog.csdn.net/xh870189248/article/details/80208099
首先,拉取代碼,不過不拉取子模塊,注意,不要加sudo,加sudo後面編譯時會出問題
sudo git clone https://gitee.com/xuhongv/esp-idf.git
然後打開.gitmodules
,我們看到下子模塊源路徑是不規範的,這裏,我們再修改爲碼雲上的:
1、進入esp-idf目錄,打開.gitmodules
sudo vim .gitmodules
2、修改替換爲下面的代碼!
[submodule "components/esptool_py/esptool"]
path = components/esptool_py/esptool
url = https://gitee.com/xuhongv/esptool.git
[submodule "components/bt/controller/lib"]
path = components/bt/controller/lib
url = https://gitee.com/xuhongv/esp32-bt-lib.git
[submodule "components/bootloader/subproject/components/micro-ecc/micro-ecc"]
path = components/bootloader/subproject/components/micro-ecc/micro-ecc
url = https://gitee.com/xuhongv/micro-ecc.git
[submodule "components/coap/libcoap"]
path = components/coap/libcoap
url = https://gitee.com/xuhongv/libcoap.git
[submodule "components/nghttp/nghttp2"]
path = components/nghttp/nghttp2
url = https://gitee.com/xuhongv/nghttp2.git
[submodule "components/libsodium/libsodium"]
path = components/libsodium/libsodium
url = https://gitee.com/xuhongv/libsodium.git
[submodule "components/spiffs/spiffs"]
path = components/spiffs/spiffs
url = https://gitee.com/xuhongv/spiffs.git
[submodule "components/json/cJSON"]
path = components/json/cJSON
url = https://gitee.com/xuhongv/cJSON.git
[submodule "components/mbedtls/mbedtls"]
path = components/mbedtls/mbedtls
url = https://gitee.com/xuhongv/mbedtls.git
[submodule "components/asio/asio"]
path = components/asio/asio
url = https://gitee.com/xuhongv/asio.git
[submodule "components/expat/expat"]
path = components/expat/expat
url = https://gitee.com/xuhongv/libexpat.git
[submodule "components/lwip/lwip"]
path = components/lwip/lwip
url = https://gitee.com/xuhongv/esp-lwip.git
[submodule "components/mqtt/esp-mqtt"]
path = components/mqtt/esp-mqtt
url = https://gitee.com/xuhongv/esp-mqtt.git
[submodule "components/protobuf-c/protobuf-c"]
path = components/protobuf-c/protobuf-c
url = https://gitee.com/xuhongv/protobuf-c.git
[submodule "components/unity/unity"]
path = components/unity/unity
url = https://gitee.com/xuhongv/Unity.git
[submodule "examples/build_system/cmake/import_lib/main/lib/tinyxml2"]
path = examples/build_system/cmake/import_lib/main/lib/tinyxml2
url = https://gitee.com/xuhongv/tinyxml2.git
[submodule "components/bt/host/nimble/nimble"]
path = components/bt/host/nimble/nimble
url = https://gitee.com/xuhongv/esp-nimble.git
[submodule "components/cbor/tinycbor"]
path = components/cbor/tinycbor
url = https://gitee.com/xuhongv/tinycbor.git
[submodule "components/esp_wifi/lib"]
path = components/esp_wifi/lib
url = https://gitee.com/xuhongv/esp32-wifi-lib.git
[submodule "components/tinyusb/tinyusb"]
path = components/tinyusb/tinyusb
url = https://gitee.com/xuhongv/tinyusb.git
3、然後再更新子模塊 , 你會發現快的一匹,
git submodule update --init --recursive
四、獲取工具鏈
工具鏈下載地址https://dl.espressif.com/dl/xtensa-esp32-elf-gcc8_2_0-esp-2020r1-linux-amd64.tar.gz
然後使用命令解壓:
tar -xzvf xtensa-esp32-elf-linux64-1.22.0-80-g6c4433a-5.2.0.tar.gz
五、設置 PATH 路徑
先拿到上述的SDK路徑即爲IDF路徑,以及工具鏈的路徑, 比如我的路徑即設置爲:
export IDF_PATH=/home/william/esp/esp-idf
export PATH=$PATH:/home/william/esp/tools/xtensa-esp32-elf/bin
1、打開.bashrc
sudo vim ~/.bashrc
2、添加IDF_PATH和PATH環境變量:
3、使之生效
source ~/.bashrc
4、IDF_PATH 路徑測試是否設置成功:
echo $IDF_PATH
5、測試工具鏈環境是否設置成功:
xtensa-esp32-elf-gcc -v
六、安裝 Python 軟件包
ESP-IDF 所需的 Python 軟件包位於 IDF_PATH/requirements.txt
中,運行以下命令進行安裝
python -m pip install --user -r $IDF_PATH/requirements.txt
七、開始編譯工程
進入esp-idf/examples/get-started/hello_world
目錄下,執行make menuconfig
可以保持默認配置,按【s】會Save,然後回車,按【Esc】或【q】退出即可,然後使用make all -j2
編譯,編譯成功如下
八、燒錄下載
將當前用戶增加至 dialout 組 :sudo usermod -aG dialout 用戶名
sudo usermod -aG dialout william
然後重啓系統
通過dmesg | grep ttyS*
命令查看串口
dmesg | grep ttyS*
然後燒錄
make flash
或者指定端口
make flash ESPPORT=/dev/ttyUSB0
等到出現Connecting.......
按住開發板上的BOOT
按鍵
下載成功
然後使用make monitor
命令查看串口輸出
make monitor
可以看到,Hello world!
已經打印出來了,大功告成