Author | shaniadolphin |
---|---|
349948204@qq.com |
獲取git工程
mkdir micropython
cd micropython
git clone https://github.com/micropython/micropython.git
git submodule update --init
運行的結(jié)果如下:
dolphin@DESKTOP-DR5OKOG:/mnt/h/proj/micropython$ cd micropython/
dolphin@DESKTOP-DR5OKOG:/mnt/h/proj/micropython/micropython$ git submodule update --init
Submodule 'lib/axtls' (https://github.com/pfalcon/axtls) registered for path 'lib/axtls'
Submodule 'lib/berkeley-db-1.xx' (https://github.com/pfalcon/berkeley-db-1.xx) registered for path 'lib/berkeley-db-1.xx'
Submodule 'lib/libffi' (https://github.com/atgreen/libffi) registered for path 'lib/libffi'
Submodule 'lib/lwip' (https://git.savannah.gnu.org/r/lwip.git) registered for path 'lib/lwip'
Submodule 'lib/nrfx' (https://github.com/NordicSemiconductor/nrfx.git) registered for path 'lib/nrfx'
Submodule 'lib/stm32lib' (https://github.com/micropython/stm32lib) registered for path 'lib/stm32lib'
Cloning into '/mnt/h/proj/micropython/micropython/lib/axtls'...
Cloning into '/mnt/h/proj/micropython/micropython/lib/berkeley-db-1.xx'...
Cloning into '/mnt/h/proj/micropython/micropython/lib/libffi'...
Cloning into '/mnt/h/proj/micropython/micropython/lib/lwip'...
GIT倉庫里的主要組件:
- py/ -- Python核心應(yīng)用,包括編譯器,runtime和核心庫趴乡。
- mpy-cross/ -- MicroPython交叉編譯器,用于將腳本轉(zhuǎn)換成預(yù)編譯的字節(jié)碼。
- ports/unix/ -- 運行在UNIX上的MyPython版本威创。
- ports/stm32/ -- 運行在PyBoard(使用ST Cube HAL驅(qū)動)和類似的STM32板上的MicroPython版本。
- ports/esp32/ -- 運行在Espressif的ESP32 SoC上的MicroPython版本谎懦。
- ports/nrf/ -- 運行在Nordic的NRF 51和NFR52 MCU上的MicroPython版本肚豺。
- ports/minimal/ -- 最小的MicroPython接口,用于將MicroPython移植到另一個MCU界拦。
- tests/ -- 測試框架和測試腳本吸申。
- docs/ -- 用戶文檔。
- extmod/ -- 附加(非內(nèi)核)C模塊。
- tools/ -- 各種工具截碴,包括PyBoard.Py模塊梳侨。
- examples/ -- 一些Python腳本示例。
安裝gcc-arm-embedded
Build instructions
在為目標板編譯固件之前隐岛,必須編譯MicroPython交叉編譯器猫妙,它用于將一些內(nèi)置的腳本預(yù)編譯為字節(jié)碼。在主機上編譯交叉編譯器:
dolphin@DESKTOP-DR5OKOG:/mnt/h/proj/micropython/micropython$ make -C mpy-cross
make: Entering directory '/mnt/h/proj/micropython/micropython/mpy-cross'
Use make V=1 or set BUILD_VERBOSE in your environment to increase build verbosity.
mkdir -p build/genhdr
GEN build/genhdr/mpversion.h
GEN build/genhdr/qstr.i.last
GEN build/genhdr/qstr.split
GEN build/genhdr/qstrdefs.collected.h
QSTR updated
GEN build/genhdr/qstrdefs.generated.h
mkdir -p build/py/
CC ../py/mpstate.c
編譯完后生成mpy-cross:
LINK mpy-cross
text data bss dec hex filename
139992 12976 864 153832 258e8 mpy-cross
make: Leaving directory '/mnt/h/proj/micropython/micropython/mpy-cross'
編譯PYBV11
STM32的目標板需要ARM編譯器arm-none-eabi-gcc:
sudo add-apt-repository ppa:team-gcc-arm-embedded/ppa
sudo apt-get update
sudo apt-get install gcc-arm-embedded
安裝好編譯器后即可進行編譯聚凹,可以通過參數(shù)BOARD= 來指定目標板割坠,比如make BOARD=PYBV11
、make BOARD=STM32F4DISC
:
dolphin@DESKTOP-DR5OKOG:/mnt/h/proj/micropython/micropython$ cd ports/stm32
dolphin@DESKTOP-DR5OKOG:/mnt/h/proj/micropython/micropython/ports/stm32$ make BOARD=PYBV11
Use make V=1 or set BUILD_VERBOSE in your environment to increase build verbosity.
mkdir -p build-PYBV11/genhdr
GEN build-PYBV11/genhdr/pins.h
GEN stmconst build-PYBV11/modstm_qstr.h
GEN build-PYBV11/genhdr/pybcdc.inf
GEN build-PYBV11/genhdr/pybcdc_inf.h
GEN build-PYBV11/genhdr/pllfreqtable.h
GEN build-PYBV11/genhdr/mpversion.h
GEN build-PYBV11/genhdr/qstr.i.last
編譯生成firmware妒牙,隨后便可通過windows端的JFLASH或者DFU工具將固件燒錄到目標板中:
LINK build-PYBV11/firmware.elf
text data bss dec hex filename
331204 36 28028 359268 57b64 build-PYBV11/firmware.elf
GEN build-PYBV11/firmware.dfu
GEN build-PYBV11/firmware.hex
也可通過將PYBV11的P1/DFU引腳連接到3.3V彼哼,按復(fù)位按鍵重啟,進入DFU模式來更新(非win10的UBUNTU子系統(tǒng)中):
make BOARD=PYBV11 deploy
編譯ESP32
ESP32的esp-idf在github倉庫中的地址:https://github.com/espressif/esp-idf湘今。在本地創(chuàng)建esp-idf所在的目錄敢朱,比如/mnt/h/proj/,使用git clone將esp-idf下載到該目錄中:
cd /mnt/h/proj/
git clone --recursive https://github.com/espressif/esp-idf.git
更新esp-idf中的子模塊:
#Do not miss the --recursive option. If you have already cloned ESP-IDF without this option, run another command to get all the submodules:
cd ~/esp/esp-idf
git submodule update --init --recursive
由于micropython僅支持hash值30545f4cccec7460634b656d278782dd7151098e
的版本摩瞎,所以需要切換到這個版本拴签,否則會丟失一些定義,造成編譯失敗旗们。
git reset 30545f4cccec7460634b656d278782dd7151098e --hard
進入esp-idf的目錄蚓哩,通過以下指令安裝requirements.txt中的Python包。
pip3 install --user -r $IDF_PATH/requirements.txt
注意esp32中的相關(guān)文件:
esp-idf
components
docs
examples
make
tools
xtensa-esp32-elf
其中xtensa-esp32-elf是工具鏈上渴,可以通過官網(wǎng)下載:
- 64-bit Linux:
https://dl.espressif.com/dl/xtensa-esp32-elf-linux64-1.22.0-80-g6c4433a-5.2.0.tar.gz - 32-bit Linux:
https://dl.espressif.com/dl/xtensa-esp32-elf-linux32-1.22.0-80-g6c4433a-5.2.0.tar.gz
下載完后運行tar -xzf
解壓到esp-idf目錄中岸梨。
進入前文下載的micropython的ports/esp32目錄,在目錄下新建腳本env.sh稠氮,將esp-idf及xtensa-esp32-elf所在的路徑寫入腳本中曹阔,在命令行中運行source env.sh
設(shè)置編譯所需的環(huán)境變量:
if ! echo "${PATH}" | grep -q "xtensa-esp32-elf"; then
export PATH="/mnt/h/proj/esp-idf/xtensa-esp32-elf/bin:$PATH"
fi
export IDF_PATH=/mnt/h/proj/esp-idf
export CROSS_COMPILE=xtensa-esp32-elf-
再在該目錄下新建makefie文檔,在文檔中寫入以下內(nèi)容:
#PORT = /dev/ttyUSB0
#FLASH_MODE = qio
#FLASH_SIZE = 4MB
#CROSS_COMPILE = xtensa-esp32-elf-
CONFIG_SPIRAM_SUPPORT = 1
include Makefile
通過以下命令編譯micropython
$ cd ports/esp32
$ make
編譯生成固件的有關(guān)信息:
LINK build/application.elf
text data bss dec hex filename
787097 192024 74368 1053489 101331 build/application.elf
Create build/application.bin
esptool.py v2.5.1
Create build/firmware.bin
bootloader 17808
partitions 3072
application 979264
total 1044800
esp32所需要的bootloader.bin隔披、partitions.bin和application.bin生成在build目錄中赃份。
將esp32連接到電腦的串口上,串口的端口奢米,F(xiàn)LASH的設(shè)置預(yù)定義在Makefile中抓韩,也可以通過makefile重新定義。
在燒錄前請用以下命令擦除FLASH:
$ make erase
如果要將micropython固件寫入esp32恃慧,運行以下指令园蝠,通過esp-idf中的esptool.py腳本下載固件:
$ make deploy
以下是micropython連接WiFi的一個應(yīng)用示例:
def wlan_connect(ssid='MYSSID', password='MYPASS'):
import network
wlan = network.WLAN(network.STA_IF)
if not wlan.active() or not wlan.isconnected():
wlan.active(True)
print('connecting to:', ssid)
wlan.connect(ssid, password)
while not wlan.isconnected():
pass
print('network config:', wlan.ifconfig())
參考文檔
# | 鏈接地址 | 文檔名稱 |
---|---|---|
1 | http://www.micropython.org/ |
官網(wǎng)資料 |
2 | http://www.micropython.org.cn/bbs/ |
官方社區(qū) |