MicroPython是什么
程序猿中有句俗语: 人生苦短,我用Python。 Python的强大和易用性让它不仅可以写网站,编程序,在嵌入式领域也有一席之地。 MicroPython,是Python3编程语言的一个完整软件实现,包括Python标准库的一小部分,用C语言编写,经过优化可在微控制器和受限环境中运行。MicroPython是运行在微控制器硬件之上的完全的Python编译器和运行时系统。提供给用户一个交互式提示符(REPL)来立即执行所支持的命令。除了包括选定的核心Python库,MicroPython还包括了给予编程者访问低层硬件的模块。 MicroPython支持的开发板从官方网站我们可以了解到,官方开发板主要有以下几种: PYB Nano V1.1基于STM32F401 PYB Nano V2基于STM32F411 PyBoard CN V2基于STM32F405 MicroPython ESP32
另外还支持其他系列的开发板: WiPy ESP8266 boards ESP32 boards STM32F4 Discovery board NUCLEO-F401RE board NUCLEO-F411RE board NUCLEO-F767ZI board NUCLEO-L476RG board Espruino Pico
MicroPython移植和板子关系不大,主要支持的是芯片,如果自己的板子芯片是上面的这些型号,也可以刷MicroPython固件,对应的IO口需要更改,而且需要重新编译生成对应的固件。具体操作方法可以查看:官方Github地址:http://github.com/micropython/micropython,里面包括了源代码和Linux下的编译方法。开发板固件下载:MicroPython downloads Nucleo-F411RE移植MicroPython固件
正好MicroPython支持本次申请的Nucleo-F411RE开发板,就试着把刷成MicroPython的固件,尝试一下使用Python来开发STM32,具体移植过程。
1.准备工作支持Nucleo-F411RE的MicroPython固件:NUCLEO_F411RE-20190604-v1.11-25-gce8262a16.dfu 用于STM32 DFU下载的软件:STSWSTM32080V3.0.6.zip PUTTY串口终端:putty-64bit-0.71-installer.msi
2.安装Dfu下载软件下载完成后,安装DFU下载软件DfuSeDemo,非常简单,一路Next就行,在选择安装目录时,可以选择非系统盘。 3.硬件连接由于Nucleo-F411RE板子的USB口是连接到ST-Link调试器,并没有一个连接到STM32 USB引脚的接口,所以我使用的是这种转接板,把USB的5个信号转接成5个排针,并和板子上的引脚连接:
4.烧录MicroPython固件和串口下载程序一样,使用DFU烧录固件前,也要先把STM32切换为系统存储器启动模式:即下载模式。上电之前要先设置BOOT0=1,BOOT1(PB2)=0,然后烧录MicroPython固件。
5.安装Putty安装Putty,打开串口终端,选择ST-Link虚拟串口号,波特率115200
按一下板子上的黑色复位按键,串口输出: MicroPython v1.11-12-g6077d1715 on 2019-06-03; NUCLEO-F411RE with STM32F411xE Type "help()" for more information. >>> help() Welcome to MicroPython!
For online help please visit http://micropython.org/help/.
Quick overview of commands for the board: pyb.info() -- print some general information pyb.delay(n) -- wait for n milliseconds pyb.millis() -- get number of milliseconds since hard reset pyb.Switch() -- create a switch object Switch methods: (), callback(f) pyb.LED(n) -- create an LED object for LED n (n=1,2,3,4) LED methods: on(), off(), toggle(), intensity(<n>) pyb.Pin(pin) -- get a pin, eg pyb.Pin('X1') pyb.Pin(pin, m, [p]) -- get a pin and configure it for IO mode m, pull mode p Pin methods: init(..), value([v]), high(), low() pyb.ExtInt(pin, m, p, callback) -- create an external interrupt object pyb.ADC(pin) -- make an analog object from a pin ADC methods: read(), read_timed(buf, freq) pyb.DAC(port) -- make a DAC object DAC methods: triangle(freq), write(n), write_timed(buf, freq) pyb.RTC() -- make an RTC object; methods: datetime([val]) pyb.rng() -- get a 30-bit hardware random number pyb.Servo(n) -- create Servo object for servo n (n=1,2,3,4) Servo methods: calibration(..), angle([x, [t]]), speed([x, [t ]]) pyb.Accel() -- create an Accelerometer object Accelerometer methods: x(), y(), z(), tilt(), filtered_xyz()
Pins are numbered X1-X12, X17-X22, Y1-Y12, or by their MCU name Pin IO modes are: pyb.Pin.IN, pyb.Pin.OUT_PP, pyb.Pin.OUT_OD Pin pull modes are: pyb.Pin.PULL_NONE, pyb.Pin.PULL_UP, pyb.Pin.PULL_DOWN Additional serial bus objects: pyb.I2C(n), pyb.SPI(n), pyb.UART(n)
Control commands: CTRL-A -- on a blank line, enter raw REPL mode CTRL-B -- on a blank line, enter normal REPL mode CTRL-C -- interrupt a running program CTRL-D -- on a blank line, do a soft reset of the board CTRL-E -- on a blank line, enter paste mode
For further help on a specific object, type help(obj) For a list of available modules, type help('modules') >>>
6.开始Python开发之旅——点亮一个LED点亮板载的绿色LED,串口输入命令点亮和熄灭LED >>> pyb.LED(1).on() >>> pyb.LED(1).off() >>> pyb.LED(1).on() >>>
更多Python控制外设的命令:Quick reference for the pyboard: http://docs.micropython.org/en/latest/pyboard/quickref.html 一些问题官方介绍移植成功之后,会在电脑上显示一个PYBFLASH的盘符,但是我这次移植并没有出现,不知道是什么问题。 参考资料
|