MicroPython作为一种专门为嵌入式系统设计的Python实现,大大简化了嵌入式开发的过程。通过本文的介绍,读者可以了解到MicroPython固件下载,如何部署在STM32F412ZG开发板上以及简单项目的实现。
EEPW测评中心每月都动态提供当下最热门的板卡。本文用到的STM32F412ZG开发板就是去年测评完成后保留下来的。在浏览MicroPython官网的时候,发现官方已经适配好了固件。赶紧来烧录体验一下。
官网也介绍了几种不同的烧录方式:
Installation instructionsSTM32 via ST-Link
Nucleo and Discovery boards typically include a built-in ST-Link programmer.
A .bin or .hex file can be flashed using st-flash.
# Optional erase to clear existing filesystem. st-flash erase # Flash .bin st-flash write firmware.bin 0x08000000 # or, flash .hex st-flash --format ihex write firmware.hex
A .hex file can be flashed using STM32 Cube Programmer.
STM32_Programmer.sh -c port=SWD -d firmware.hex -hardRst
STM32 via DFU
Boards with USB support can also be programmed via the ST DFU bootloader, using e.g. dfu-util or pydfu.py.
To enter the bootloader the BOOT0 pin can be connected to VCC during reset, or you can use machine.bootloader() from the MicroPython REPL.
dfu-util --alt 0 -D firmware.dfu
这里选择的最新的HEX固件:

打开STM32 CubeProgrammer,右侧ST-LINK区域,点击Connect:

选择Micropython固件,点击Start Programming.等待烧录完成。

打开Thonny,右下角选择端口与板子:

在输出窗口可以看到已经显示出了F412ZG的固件信息。

编写简单的脚本测试一下:
import pyb
import time
led_g = pyb.LED(1)
led_b = pyb.LED(2)
led_r = pyb.LED(3)
i = 0
while True:
i = i + 1
led_g.toggle()
led_b.toggle()
led_r.toggle()
time.sleep(1)
print("Blinking Times: ", i)板子三个LED闪烁:
结语:
Micropython在使用上感觉跟CubeIDE或者Keil开发体验差异很大,可能是刚接触micropython的原因。在开源社区可以看到很多开发者分享的各类传感器、屏幕、协议的驱动库,随着物联网和智能硬件的快速发展,MicroPython在嵌入式开发中的应用前景将更加广阔。
25


