如何使用 WSL + VSCode 搭建 ESP32 開發(fā)環(huán)境
什么是 WSL ?
有了?WSL ,用戶既能使用 Windows 桌面環(huán)境,又能使用更強大的 Linux 終端工具
WSL(Windows Subsystem for Linux):WSL 是運行在 Windows 上的 GNU/Linux 環(huán)境,Linux 程序無需修改即可在 Windows 上運行,包括大多數(shù)命令行工具、實用程序和應(yīng)用程序,沒有傳統(tǒng)虛擬機或雙引導設(shè)置的開銷。
注意:只支持 Windows 10 Version 1903 或更高版本
WSL2 提升了文件系統(tǒng)性能
WSL2 升級了 WSL 的軟件架構(gòu),支持在 Windows 上直接運行 ELF64 Linux 二進制文件。它的優(yōu)點是提高了文件系統(tǒng)性能(2-5 倍提升),以及增加整個系統(tǒng)調(diào)用的兼容性。

WSL 官方文檔:https://docs.microsoft.com/en-us/windows/wsl/
1. 啟用 WSL
方法 1:在啟用或關(guān)閉 Windows 功能中,勾選“適用于 Linux 的 Windows 子系統(tǒng)” (Windows Subsystem for Linux),然后按照提示重啟電腦。

方法 2(推薦):可以通過命令行指令開啟,管理員模式進入?powershell
?,然后運行:
dism.exe /online /enable-feature /featurename:Microsoft-Windows-Subsystem-Linux /all /norestart
有些電腦可能提示安裝?Linux kernel update package(https://docs.microsoft.com/en-us/windows/wsl/install-win10#step-4---download-the-linux-kernel-update-package),下載安裝即可。
如何切換 WSL 版本 ?
如果需要切換?WSL
?版本,可通過命令行?wsl --set-version <distribution name> <versionNumber>
進行切換,例如將?Ubuntu-18.04
?版本切換為?WSL2
:
wsl --set-version Ubuntu-18.04 2
是否要切換取決于你的應(yīng)用場景,如果需要使用 USB 和串口,可以暫時使用?WSL1
,目前?WSL2
?還不支持操作 USB 和串口(2020.09)。
2. 選擇并安裝一個 Linux 發(fā)行版
這里建議選擇?ubuntu 18.04
,直接在?Microsoft 應(yīng)用商店
搜索并安裝:

應(yīng)用安裝好以后,可以直接運行:

默認只有終端,也可以手動安裝 Ubuntu 的圖形界面,但是并不建議。
3. 克隆 ESP-IDF 代碼倉庫
方法 1:從 github 下載,可以直接克隆主倉庫和子倉庫:
git clone --recursive https://github.com/espressif/esp-idf.git
方法 2(推薦):從國內(nèi)鏡像 gitee 下載,需要使用以下操作:
下載子倉庫重定向工具?
esp-gitee-tools
git clone https://gitee.com/EspressifSystems/esp-gitee-tools.git
克隆主倉庫代碼
git clone https://gitee.com/EspressifSystems/esp-idf.git
克隆重定向后的子倉庫代碼
cd esp-gitee-tools
export EGT_PATH=$(pwd)
cd ..
cd esp-idf
$EGT_PATH/submodule-update.sh
官方 gitee 操作指南,請參考:
submodule-update(https://gitee.com/EspressifSystems/esp-gitee-tools/blob/master/docs/README-submodule-update.md)
4. 安裝 ESP-IDF 工具鏈
1.?將默認 python 切換為 python3 (可選步驟,推薦)
1.1 修改 ubuntu 源到國內(nèi)鏡像:
cd /etc/apt/
sudo cp sources.list sources.list.bak
sudo vim sources.list
刪除該文件全部內(nèi)容,粘貼以下內(nèi)容并保存:
deb http://mirrors.aliyun.com/ubuntu/ bionic main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ bionic-security main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ bionic-updates main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ bionic-proposed main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ bionic-backports main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-security main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-updates main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-proposed main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-backports main restricted universe multiverse
1.2 安裝 python3 :
sudo apt-get install python3 python3-pip python3-setuptools
1.3 將 python 默認切換為 python3:
sudo update-alternatives --install /usr/bin/python python /usr/bin/python3 10
2.?將 pip 源更換到國內(nèi)鏡像(可選步驟,推薦)
方法 1?:pip 版本 >= 10.0.0,可以使用以下方式:
pip --version
pip config set global.index-url http://mirrors.aliyun.com/pypi/simple
pip config set global.trusted-host mirrors.aliyun.com
方法 2(通用):可以創(chuàng)建并修改配置文件?~/.pip/pip.conf
?:
mkdir ~/.pip
vim ~/.pip/pip.conf
將下面內(nèi)容添加到文件并保存:
[global]
index-url = http://mirrors.aliyun.com/pypi/simple
[install]
trusted-host = mirrors.aliyun.com
3.?安裝依賴
在終端輸入以下指令,安裝依賴工具:
sudo apt-get install git wget flex bison gperf python python-pip python-setuptools cmake ninja-build ccache libffi-dev libssl-dev dfu-util
進入 esp-idf 目錄,安裝 Python 依賴包:(該步驟可忽略,后續(xù) install.sh 會默認安裝)
cd esp-idf pip3 install -r requirements.txt
4.?安裝編譯工具鏈
方法 1?:從默認路徑下載并安裝工具鏈
./install.sh
方法 2(推薦)?:從國內(nèi)鏡像下載并安裝工具鏈,需要使用以下操作:
cd esp-gitee-tools
export EGT_PATH=$(pwd)
cd ..
cd esp-idf
$EGT_PATH/install.sh
5. 安裝 VSCode WSL 插件

如果從 WSL 啟動 VSCode,一般會自動安裝插件,之后就能直接在 VSCode 中使用 Linux 的終端編譯程序啦??!

6. 編譯示例程序
添加環(huán)境變量:
. $HOME/esp-idf/export.sh
進入?helloworld
?示例程序:
cd ~/esp-idf/examples/get-started/hello_world/
直接在終端輸入:
idf.py build

編譯成功?。?/p>
7. 下載程序
方法 1?: 直接在 WSL 終端使用?idf.py flash
(目前僅限 WSL1)?
WSL1 可直接訪問串口下載程序,需要注意的是,如果你的串口號在 Windows 上顯示為 COM3,那么在 wsl 下載程序時,需要對應(yīng)修改為?/dev/ttyS3
,直接使用以下命令下載:
idf.py -p /dev/ttyS3 -b 115200 flash
終端顯示下載過程:

部分電腦可能遇到的問題:CP2102 USB 轉(zhuǎn)串口芯片導致不能下載的問題(https://github.com/microsoft/WSL/issues/3795)
方法 2?: 使用 Flash Download Tools 下載 (WSL2 推薦使用)
注意:截止到 2020.09 月,WSL2 還不支持訪問 usb 和 串口,Windows 燒錄工具下載:Flash Download Tools(https://www.espressif.com/en/support/download/other-tools)
在 wsl 終端輸入explorer.exe .
,即可使用 Windows 上的文件管理器打開當前的 wsl 目錄
explorer.exe .

使用 Flash Download Tools 輸入 bin 的路徑和地址進行下載

8. Hello world !
方法 1?: 直接在 WSL 終端使用?idf.py monitor
(目前僅限 WSL1)?
WSL1 可直接訪問串口下載程序,需要注意的是,如果你的串口號在 Windows 上顯示為 COM3,那么在 wsl 下載程序時,需要對應(yīng)修改為?/dev/ttyS3
,直接使用以下命令下載并打?。?/p>
idf.py -p /dev/ttyS3 -b 115200 monitor
方法 2(通用)?: 使用任意 Windows 串口工具(WSL2 推薦使用)
配置端口號?COMX
,波特率?115200
,直接打開即可顯示:
