zoukankan      html  css  js  c++  java
  • 【ST开发板评测】使用Python来开发STM32F411

    前言

    板子局部图

    板子申请了也有一段时间了,也快到评测截止时间了,想着做点有意思的东西,正好前一段时间看到过可以在MCU上移植MicroPython的示例,就自己尝试一下,记录移植过程。

    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地址:https://github.com/micropython/micropython,里面包括了源代码和Linux下的编译方法。开发板固件下载:MicroPython downloads

    Nucleo-F411RE移植MicroPython固件

    板子背面Nucleo标志

    正好MicroPython支持本次申请的Nucleo-F411RE开发板,就试着把刷成MicroPython的固件,尝试一下使用Python来开发STM32,具体移植过程。

    1.准备工作

    2.安装Dfu下载软件

    下载完成后,安装DFU下载软件DfuSeDemo,非常简单,一路Next就行,在选择安装目录时,可以选择非系统盘。

    3.硬件连接

    由于Nucleo-F411RE板子的USB口是连接到ST-Link调试器,并没有一个连接到STM32 USB引脚的接口,所以我使用的是这种转接板,把USB的5个信号转接成5个排针,并和板子上的引脚连接:

    转接板 开发板
    VBUS 3.3
    D- PA11
    D+ PA12
    IO GND
    GND GND

    4.烧录MicroPython固件

    和串口下载程序一样,使用DFU烧录固件前,也要先把STM32切换为系统存储器启动模式:即下载模式。上电之前要先设置BOOT0=1,BOOT1(PB2)=0,然后烧录MicroPython固件。

    • 设置完成之后,连接电脑,设备管理会出现一个DFU设备

    • 点击Upgrade升级,在弹出的界面,点击“是”。

    • 等待升级完成,断电,把BOOT短接线拔掉,重新上电。

    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

    参考资料

    历史精选


    欢迎关注我的个人博客www.wangchaochao.top

    或微信扫码关注我的公众号

  • 相关阅读:
    某公司的软件测试笔试题,要是不看书,有的题目还真是不好答
    性能测试面试题总结(答案全)
    最简单实用的docker专题
    什么是LAMP和WAMP,一篇文章和你讲清楚
    版本管理工具Visual SourceSafe的超详细使用教程
    抓包工具Fidder详解(主要来抓取Android中app的请求)
    除了angular/vue/react的新轮子lit
    你可以在浏览器中直接运行 TypeScript 吗?
    Introducing the .NET Hot Reload experience for editing code at runtime
    vscode编辑器快捷方式
  • 原文地址:https://www.cnblogs.com/whik/p/10976755.html
Copyright © 2011-2022 走看看