micropython开发者指南

2018-11-30  本文已影响0人  shaniadolphin

Author shaniadolphin
e-mail 349948204@qq.com

获取git工程

mkdir micropython
cd micropython
git clone https://github.com/micropython/micropython.git
git submodule update --init

运行的结果如下:

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仓库里的主要组件:


安装gcc-arm-embedded

Build instructions

在为目标板编译固件之前,必须编译MicroPython交叉编译器,它用于将一些内置的脚本预编译为字节码。在主机上编译交叉编译器:

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

安装好编译器后即可进行编译,可以通过参数BOARD= 来指定目标板,比如make BOARD=PYBV11make 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,按复位按键重启,进入DFU模式来更新(非win10的UBUNTU子系统中):

make BOARD=PYBV11 deploy

编译ESP32

ESP32的esp-idf在github仓库中的地址:https://github.com/espressif/esp-idf。在本地创建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中的相关文件:

esp-idf

components
docs
examples
make
tools
xtensa-esp32-elf

其中xtensa-esp32-elf是工具链,可以通过官网下载:

下载完后运行tar -xzf解压到esp-idf目录中。
进入前文下载的micropython的ports/esp32目录,在目录下新建脚本env.sh,将esp-idf及xtensa-esp32-elf所在的路径写入脚本中,在命令行中运行source env.sh设置编译所需的环境变量:

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文档,在文档中写入以下内容:

#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

编译生成固件的有关信息:

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连接到电脑的串口上,串口的端口,FLASH的设置预定义在Makefile中,也可以通过makefile重新定义。
在烧录前请用以下命令擦除FLASH:

$ make erase

如果要将micropython固件写入esp32,运行以下指令,通过esp-idf中的esptool.py脚本下载固件:

$ make deploy

以下是micropython连接WiFi的一个应用示例:

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/ 官网资料
2 http://www.micropython.org.cn/bbs/ 官方社区
上一篇下一篇

猜你喜欢

热点阅读