山姆的編程實作分享。。。

Sam Blog, Sam Sharing, Sam Studio

2020年10月21日

[MicroPython 教學] Build ESP32 MicroPython Firmware


學習 MicroPython (MPY),除了瞭解如何使用他以及更改 main.py 來控制你的 Embedded Board 外;Build MPY Image 更是能深入瞭解 MP 的程式碼,客製化 MPY Image,因為 MPY 比起 Arduino ,他的優勢就是在 Package 的豐富性,官方給的都是通用的 package;想要創造差異性,唯有客製化自己的 MPY Image,不是嗎 ?


所以瞭解如何 Build MPY Image 是必要的,也是深入學習應用 MPY 的重要步驟。

以下是 ESP32 MPY Building Guide  以及 原始碼的連接

MicroPython Source Code on GitHub

ESP32 Building Guide :  MicroPython port to the ESP32

以下是我 Build ESP32 MicroPython Image 過程及步驟


  1. 取得 MicroPython v1.13 stable version source code 


git clone -b v1.13 --recursive https://github.com/micropython/micropython


  1. 在正式進入 Build ESP32 MPY Code 前,我們需要 ESP-IDF tool,

因為 ESP32 MPY 的底層還是以 C Code 完成的,所以我們還是需要

ESP-IDF tool 來 Build ESP32 底層的 C Code。

可以參考我這篇文章 ( [Arduino] Build ESP32 code on Ubuntu ),

,如何取得,設定 ESP-IDF 以及 Build ESP32 Code 或參考官方連接

 ESP32 Get Started 


  1. 這一步是 Build “ MicroPython Cross-Complier”,簡稱 mpy-cross,

它的做用是轉換 (pre-compile) Python script (.py) 成 .mpy file 檔案。

例如,原來是 foo.py 的 Python script ,透過以下command 的執行

 $ ./mpy-cross foo.py

foo.py 就轉換成 foo.mpy ,然後就可以被置放在 MPY 的檔案系統內了,

你就可透過 import foo 來使用他了。

Build MicroPython Cross-Complier 的作法 :

$ cd mpy-cross

$ make


  1. 完成前面兩項前置工作後,是時候 build ESP32 MPY image 了

$ cd .. /ports/esp32

$ make


很不幸的,遇到以下的錯誤 :-(

那是因為 ESP-IDF 的路徑還沒設定


  1. 我們需要打開 Makefile 做一下修改,設定 ESP-IDF 的路徑。

$ vi Makefile

$ code Makefile (如果你有裝 Visual Studio Code)


把這一行

ESPIDF = $(IDF_PATH)

修改成這樣

ESPIDF = $(/home/sam/esp/esp-idf)

 (/sam 是我 Ubuntu account name,請改成你的)


或是直接執行 (如果你已經有安裝 ESP-IDF Tool)

. $HOME/esp/esp-idf/export.sh


  1. 再下一次 make 指令

還是遇到以下的錯誤 :-(

首先是 ESP-IDF 的版本和 MPY 使用的版本不合,所以給了以下警告!

"The build may complete and the firmware may work but it is not guaranteed",是警告所以還能繼續Build

後頭就出現錯誤了

kconfiglib.KconfigError: /home/sam/esp/esp-idf/Kconfig:185: '$COMPONENT_KCONFIGS_PROJBUILD_SOURCE_FILE' not found (in 'source "$COMPONENT_KCONFIGS_PROJBUILD_SOURCE_FILE"'). 


所以我們就乖乖的換成他支援的 ESP-IDF 的版本 (v4.0) 吧!

Supported git hash (v3.3): 9e70825d1e1cbf7988cf36981774300066580ea7

Supported git hash (v4.0) (experimental): 4c81978a3e2220674a432a588292a4c860eef27b


作法 : 

cd $IDF_PATH/


sudo git reset --hard 4c81978a3e2220674a432a588292a4c860eef27b


sudo git submodule update --init --recursive


執行上面3行指令後,記得要再執行一次

./install.sh


這樣才是真正完成 ESP-IDF 版本的切換


  1. 完成 ESP-IDF 版本的切換後,這樣就可以再下一次 make 指令了

解決了 ESP-IDF 版本不合的錯誤了,但還有新的錯誤!

這裡是顯示是 1) 已經 Build & Link 完成了 2) 在做 .bin 階段時, 出現 Python2 呼叫不到的問題。

這裡的解法可以是 1) 建立一個 Python2 symlink 或 2) 直接改成呼叫 Python3,而我選擇了第2種做法。

把 行976 mark 掉,改成 行977。

完成了, 產生 firmware.bin 檔案,就放在 build-GENERIC/ folder 下

  1. 下載 MPY Image cd

這是 MicroPython 官方給的下載指令

http://micropython.org/download/esp32/


首先是清除 Flash ,尤其是有下載過 Arduino 程式碼的


esptool.py --chip esp32 --port /dev/ttyUSB0 erase_flash

接著才是燒錄我們自己 build 過的 Image,這裡有一點要注意的是 firmware 燒錄的起始位置是在 0x1000

esptool.py --chip esp32 --port /dev/ttyUSB0 --baud 460800 write_flash -z 0x1000 firmware.bin

燒錄完成!

使用 PuTTY 來測試一下吧

成功!


2020年10月16日

[Arduino 教學] Build ESP32 code on Ubuntu


先前在收到 ESP-CAM 這塊開發板時,曾經在 Ubuntu 下 build 過 ESP32 的 原始碼,

請參考這篇文章  ESP32-CAM ( Ai-Thinker Module )。

最近在玩 MicroPython (MP) ,想深入研究 MP,就必須自己 Build Code 研究,

上次 (ESP32-CAM ( Ai-Thinker Module )) 沒把過程詳細記入下來,

如今在 Ubuntu VM 下又實作一次,又是跌跌撞撞的,做到一半就想把過程步驟寫下來,

希望對日後有幫助。


這次改在 Ubuntu 18.04 VM 來建立開發環境,方便 Ubuntu 以及 Windows 10 同時操作

,文章也可即時撰寫。不廢話了,進入步驟描述吧!


步驟 :

1. Get ESP-IDF

1.1 Change directory to your working directory

1.2 Create an ESP folder at your working directory

sudo mkdir -p esp

cd esp

1.3 Download ESP-IDF under esp folder

sudo git clone --recursive https://github.com/espressif/esp-idf.git

1.4 做到 1.3 這一步,遇到一個 Error : "Command 'git' not found"

那是因為新裝好的 Ubuntu 18.04 VM,還沒裝任何 Package。

So, I need to install git first then run step 1.3 command again

 sudo apt-get install git

 

  Result from step 1


2. Setup ESP-IDF Tools

2.1 Change directory to /esp-idf by below commands

cd esp-idf    

         2.2 Execute below command to install ESP-IDF Tools

./install.sh

2.3 做到這一步,還是因為新裝好的 Ubuntu 18.04 VM,還沒裝任何 Package,

      Python 以及 Python-pip 沒有安裝。

 

                Please execute the below commands to install them。

                sudo apt-get install python

                sudo apt-get install python-pip


                If you want to install Python3, run below commands

                sudo apt-get install python3

      sudo apt-get install python3-pip


     After installed Python package if you still meet below error 

                /usr/bin/env: ‘python’: No such file or directory

         

                Please execute below commands to create a symlink

                sudo ln -s /usr/bin/python3 /usr/bin/python

 

    Result from step 2


3. Setup the environment variables

3.1 Every time, you want to build code before, please run below

command to setup the environment variables

. ./export.sh

or

. $HOME/esp/esp-idf/export.sh

Result from step 3

 

4. Start a project by using built-in examples

4.1 Copy built-in example "Blink" under \esp folder 

        cd $IDF_PATH/..

        sudo cp -r $IDF_PATH/examples/get-started/blink .

4.2 Need cmake tool before build, please run below command to install it,    

otherwise you will get an error as below 

          sudo apt-get install cmake


5. Connect ESP32 board to PC

5.1 Run below command before and after to get serial port number

        ls /dev/tty*

In my result, the serial port number from my two runs is

          /dev/ttyUSB0


6. Configure then physical build

6.1 Configure target build by execute below command

           idf.py set-target esp32

PS: Setting the target with  idf.py set-target esp32 should be 

done once, after opening a new project.

If the project contains some existing builds and configuration,

they will be cleared and initialized.

6.2 Execute below command to check build environment,

the following menu will appear if previous steps are correct.

      idf.py menuconfig

6.3 Execute below command to physical build.

      idf.py build

Build Complete Result


7. Flash code 

7.1 From result screen, already tell us to run idf.py -p (port) flash command

to flash code。

Before flash code, please execute below command to get serial port right 

sudo chmod 666 /dev/ttyUSB0

7.2 It's time to flash code now 

          idf.py -p /dev/ttyUSB0 flash

7.3 把 LED delay time 從 1000ms 改成 500ms,讓 LED 閃爍快些;

重新再Build &  Flash 一次;看是否如預期。




後記 : 


前面 ESP-IDF 的版本是使用 Master / 當時 Latest 的版本, 並非正式 Release 版本,

在 Build MicroPython Image 時遇到了問題,因為 MicroPython 只支援 ESP-IDF v3.0

以及 v4.0,所以我就重新安裝成  MicroPython 所支援的  v4.0。

重新再 Build 一次 Blink Project 發現 v4.0 和 Master 的差異有些大。

以下就是差異點 :

1. “idf.py set-target esp32” command is not support,直接下“idf.py menuconfig”

2. 下“idf.py menuconfig”command 後,會有一堆 Errors,那是因為還缺一些 packages

要再執行以下commands 安裝 packages

$ sudo apt-get install libncurses5-dev libncursesw5-dev

$ sudo apt install flex gperf bison

這樣才能順利執行 “idf.py menuconfig”以及“idf.py build”


熱門文章