1. 程式人生 > >macOS 搭建ESP8266編譯環境

macOS 搭建ESP8266編譯環境

前言:在折騰完 Visual Studio Code 與 Win10 64bit Ubuntu bash 的ESP8266 編譯開發環境搭建(無需編譯toolchain)之後,又想在macOS下折騰一次,於是有了這篇博文。
系統版本:macOS Mojave

參考文章:

1. https://www.esp8266.com/wiki/doku.php?id=setup-osx-compiler-esp8266
2. https://github.com/pfalcon/esp-open-sdk
3. 在Mac OS上設定ESP 8266開發環境
4. ESP8266 MAC(OSX)開發環境搭建


5. Visual Studio Code 與 Win10 64bit Ubuntu bash 的ESP8266 編譯開發環境搭建(無需編譯toolchain)
我搜到的關於macOS建立開發環境的網文,都已經過時了。

第一,他們提到的 brew tap Homebrew/dupes 已經不能用了(因為2018年3月已經合併到 Homebrew/core裡面了)。

第二,他們都要求在mac系統上建立一個大小寫敏感的映象重新編譯toolchain,太麻煩了。

第三,這些文章都是基於ESP8266 open SDK的,而我目前不在esp open sdk上編譯,只需要編譯基於non_os SDK的專案。

所以在上一篇博文中我有提到一個參考文章: 快速建立esp8266-linux開發環境 ,裡面提到其實各平臺的toolchain可以從ESP8266 Core for Arduino提供的package_esp8266com_index.json 文件裡面解析出來,裡面包含了 win 32/64bit, linux 32/64bit, osx 32/64bit的編譯好的toolchain。

1. 安裝Homebrew

如果沒有先安裝Homebrew

/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

2. 安裝依賴包

brew install binutils coreutils automake autoconf wget gawk libtool help2man gperf gnu-sed --with-default-names grep

3. 下載toolchain

建立資料夾並授予許可權,下載toolchain, 然後解壓

sudo mkdir /opt/espressif
sudo chmod 777 /opt/espressif
cd /opt/espressif
wget https://github.com/esp8266/Arduino/releases/download/2.3.0/osx-xtensa-lx106-elf-gb404fb9-2.tar.gz
tar zxvf osx-xtensa-lx106-elf-gb404fb9-2.tar.gz

4. 編輯shell profile

編輯~/.zshrc(因為我用zsh),如果你用的是預設bash,那麼需要編輯 ~/.bashrc,新增兩行

export PATH=/usr/local/opt/gnu-sed/libexec/gnubin:$PATH
export PATH=/opt/espressif/xtensa-lx106-elf/bin:$PATH

5. source profile使其生效

source ~/.zshrc
或者
source ~/.bashrc

6. 確認路徑已經存在於$PATH中

然後 echo $PATH確認兩個路徑已經存在於$PATH中

echo $PATH

7. 檢視toolchain版本

/opt/espressif/xtensa-lx106-elf/xtensa-lx106-elf >cd ~
➜ /Users/simonliu >xtensa-lx106-elf-gcc -v

看到如下資訊即可

Using built-in specs.
COLLECT_GCC=xtensa-lx106-elf-gcc
COLLECT_LTO_WRAPPER=/opt/espressif/xtensa-lx106-elf/bin/../libexec/gcc/xtensa-lx106-elf/4.8.2/lto-wrapper
Target: xtensa-lx106-elf
Configured with: /Users/igrokhotkov/e/ESPTools/crosstool-NG/.build/src/gcc-4.8.2/configure --build=x86_64-build_apple-darwin14.1.0 --host=x86_64-build_apple-darwin14.1.0 --target=xtensa-lx106-elf --prefix=/Users/igrokhotkov/e/ESPTools/crosstool-NG/builds/xtensa-lx106-elf --with-local-prefix=/Users/igrokhotkov/e/ESPTools/crosstool-NG/builds/xtensa-lx106-elf/xtensa-lx106-elf/sysroot --disable-libmudflap --with-sysroot=/Users/igrokhotkov/e/ESPTools/crosstool-NG/builds/xtensa-lx106-elf/xtensa-lx106-elf/sysroot --with-newlib --enable-threads=no --disable-shared --with-pkgversion='crosstool-NG 1.20.0' --disable-__cxa_atexit --with-gmp=/Users/igrokhotkov/e/ESPTools/crosstool-NG/.build/xtensa-lx106-elf/buildtools --with-mpfr=/Users/igrokhotkov/e/ESPTools/crosstool-NG/.build/xtensa-lx106-elf/buildtools --with-mpc=/Users/igrokhotkov/e/ESPTools/crosstool-NG/.build/xtensa-lx106-elf/buildtools --with-isl=/Users/igrokhotkov/e/ESPTools/crosstool-NG/.build/xtensa-lx106-elf/buildtools --with-cloog=/Users/igrokhotkov/e/ESPTools/crosstool-NG/.build/xtensa-lx106-elf/buildtools --with-libelf=/Users/igrokhotkov/e/ESPTools/crosstool-NG/.build/xtensa-lx106-elf/buildtools --enable-lto --enable-target-optspace --disable-libgomp --disable-libmudflap --disable-nls --disable-multilib --enable-languages=c,c++
Thread model: single
gcc version 4.8.2 (crosstool-NG 1.20.0)

然後在Visual Studio Code匯入工作區目錄後,在終端窗口裡面也可以編譯了,我的MacBook Pro (Retina, 13-inch, Late 2013),make clean只需要1秒不到,編譯只需要15秒不到,太爽了。
在這裡插入圖片描述

關於燒寫

安裝esptoolpyserial及其依賴包

pip install esptool
pip install pyserial

如果安裝esptool遇到困難,可以嘗試 python -m pip install esptool

如果遇到Cannot uninstall ‘pyserial’. It is a distutils installed project and thus we cannot accurately determine which files belong to it which would lead to only a partial uninstall.錯誤,那麼可以使用pip install --ignore-installed pyserial安裝

燒寫命令

首先檢視一下usb轉ttl裝置的名稱:

>ls /dev/tty.*
>/dev/tty.Bluetooth-Incoming-Port /dev/tty.wchusbserial1410

我的裝置名稱就是/dev/tty.wchusbserial1410
在app目錄下執行如下命令即可燒寫基於機智雲SOC程式碼

esptool.py --port /dev/tty.wchusbserial1410 --baud 345600 write_flash -fm dio -fs 32m -ff 40m 0x00000 ../bin/boot_v1.6.bin 0x3fc000 ../bin/esp_init_data_default.bin 0x3fe000 ../bin/blank.bin 0x01000 ../bin/upgrade/user1.4096.new.6.bin

所以我最後建立了一個alias,以後直接mkflash就好了

alias mkflash="make clean && ./gen_misc.sh && esptool.py --port /dev/tty.wchusbserial1410 --baud 345600 write_flash -fm dio -fs 32m -ff 40m 0x00000 ../bin/boot_v1.6.bin 0x3fc000 ../bin/esp_init_data_default.bin 0x3fe000 ../bin/blank.bin 0x01000 ../bin/upgrade/user1.4096.new.6.bin"

最後我又對alias進行了一些修改:

mk='make clean && make COMPILE=gcc BOOT=new APP=1 SPI_SPEED=40 SPI_MODE=QIO SPI_SIZE_MAP=6'
mk2='make clean && make COMPILE=gcc BOOT=new APP=2 SPI_SPEED=40 SPI_MODE=QIO SPI_SIZE_MAP=6'
mkflash='mk && esptool.py --port /dev/tty.wchusbserial1410 --baud 921600 write_flash -fm qio -fs 32m-c1 -ff 40m 0x00000 ./bin/boot_v1.6.bin 0x3fc000 ./bin/esp_init_data_default.bin 0x3fe000 ./bin/blank.bin 0x01000 ./bin/upgrade/user1.4096.new.6.bin'