stm32笔记[10]-micropython的点灯

摘要

移植MicroPython固件到Alios Things Dev kit开发板;使用MicroPython在stm32l496vgt6上实现点灯.

平台信息

  • MicroPython
  • stm32l496vgt6

开源地址

[https://gitcode.net/QS2002/pasta-stm32]

超链接

stm32笔记[7]-串口多字节收发

MicroPython简介

[https://micropython.org]
MicroPython is a lean and efficient implementation of the Python 3 programming language that includes a small subset of the Python standard library and is optimised to run on microcontrollers and in constrained environments.

The MicroPython pyboard is a compact electronic circuit board that runs MicroPython on the bare metal, giving you a low-level Python operating system that can be used to control all kinds of electronic projects.

MicroPython is packed full of advanced features such as an interactive prompt, arbitrary precision integers, closures, list comprehension, generators, exception handling and more. Yet it is compact enough to fit and run within just 256k of code space and 16k of RAM.

MicroPython aims to be as compatible with normal Python as possible to allow you to transfer code with ease from the desktop to a microcontroller or embedded system.

MicroPython是Python 3编程语言的一种精简高效实现,它包含Python标准库的一个小子集,并经过优化,可在微控制器和受限环境中运行。

MicroPython pyboard是一个紧凑的电子电路板,在裸机上运行MicroPython,为您提供可用于控制各种电子项目的低级Python操作系统。

MicroPython 包含许多高级功能,如交互式提示、任意精度整数、闭包、列表推导式、生成器、异常处理等。然而,它足够紧凑,可以在仅 256k 的代码空间和 16k 的 RAM 内安装和运行。

MicroPython旨在尽可能与普通Python兼容,以允许您轻松地将代码从桌面传输到微控制器或嵌入式系统。

实现

移植(适配)MicroPython到Alios Dev kit v1开发板

预编译固件下载,目前所支持的stm32型号有这些:stm32f0, stm32f4, stm32f7, stm32g0, stm32g4, stm32h7, stm32l0, stm32l1, stm32l4, stm32wb, stm32wl等
[https://www.bilibili.com/read/cv18843041/]
[https://github.com/thonny/thonny/releases/download/v4.1.4/thonny-4.1.4.pkg]
[https://www.bilibili.com/read/cv23047560/]
[https://hub.docker.com/r/jasonyangee/stm32-builder]

  1. 配置编译环境
# arm64
docker pull jasonyangee/stm32-builder:ubuntu-latest
git clone https://github.com/micropython/micropython.git --recurse-submodules
cd micropython
# 禁用Enterpoint属性
docker run -it --rm --entrypoint="" -v $PWD:/build jasonyangee/stm32-builder:ubuntu-latest bash
cd /build/mpy-cross
make
# 测试编译模版工程
cd /build/ports/stm32
make submodules
make BOARD=STM32L496GDISC
  1. 新建"Aliosdevkit_stm32l496vgt6"文件夹
cp -r /build/ports/stm32/STM32L496GDISC /build/ports/stm32/Aliosdevkit_stm32l496vgt6

文件目录:

.
├── board.json
├── mpconfigboard.h
├── mpconfigboard.mk
├── pins.csv
└── stm32l4xx_hal_conf.h

board.json

{
    "deploy": [
        "../deploy.md"
    ],
    "docs": "",
    "features": [],
    "images": [],
    "mcu": "stm32l4",
    "product": "Alios Things Dev Kit",
    "thumbnail": "",
    "url": "",
    "vendor": "ST Microelectronics"
}

mpconfigboard.h

#define MICROPY_HW_BOARD_NAME       "Aliosdevkit_stm32l496vgt6"
#define MICROPY_HW_MCU_NAME         "STM32L496"

#define MICROPY_HW_HAS_SWITCH       (1)
#define MICROPY_HW_ENABLE_RNG       (1)
#define MICROPY_HW_ENABLE_RTC       (1)
#define MICROPY_HW_ENABLE_USB       (1)

// MSI is used and is 4MHz,
// Resulting core frequency is 80MHz:
#define MICROPY_HW_CLK_PLLM (1)
#define MICROPY_HW_CLK_PLLN (40)
#define MICROPY_HW_CLK_PLLP (RCC_PLLP_DIV7)
#define MICROPY_HW_CLK_PLLR (RCC_PLLR_DIV2)
#define MICROPY_HW_CLK_PLLQ (RCC_PLLQ_DIV2)
#define MICROPY_HW_FLASH_LATENCY    FLASH_LATENCY_4

// The board does not have an external 32kHz crystal
#define MICROPY_HW_RTC_USE_LSE      (0)

// USART config
#define MICROPY_HW_LPUART1_TX     (pin_B11)
#define MICROPY_HW_LPUART1_RX     (pin_B10)
// LPUART_1 is connected to the virtual com port on the ST-LINK
#define MICROPY_HW_UART_REPL        PYB_LPUART_1
#define MICROPY_HW_UART_REPL_BAUD   115200

// I2C buses
#define MICROPY_HW_I2C1_SCL (pin_G14)
#define MICROPY_HW_I2C1_SDA (pin_G13)
#define MICROPY_HW_I2C2_SCL (pin_B13)
#define MICROPY_HW_I2C2_SDA (pin_B14)

// SPI buses
// -> To the arduino connector
#define MICROPY_HW_SPI1_NSS     (pin_A4)
#define MICROPY_HW_SPI1_SCK     (pin_A5)
#define MICROPY_HW_SPI1_MISO    (pin_A11)
#define MICROPY_HW_SPI1_MOSI    (pin_A7)

// Use Sel from joystick. Joystick is pulled low. Pressing the button makes the input go high.
#define MICROPY_HW_USRSW_PIN        (pin_C13)
#define MICROPY_HW_USRSW_PULL       (GPIO_PULLDOWN)
#define MICROPY_HW_USRSW_EXTI_MODE  (GPIO_MODE_IT_RISING)
#define MICROPY_HW_USRSW_PRESSED    (1)

// LED (The orange LED is controlled over MFX)
#define MICROPY_HW_LED1             (pin_B6) // Orange
#define MICROPY_HW_LED2             (pin_E3) // Orange
#define MICROPY_HW_LED3             (pin_D15) // Orange
#define MICROPY_HW_LED_ON(pin)      (mp_hal_pin_low(pin))
#define MICROPY_HW_LED_OFF(pin)     (mp_hal_pin_high(pin))
// USB config
#define MICROPY_HW_USB_FS (1)
#define MICROPY_HW_USB_OTG_ID_PIN      (pin_A10)

mpconfigboard.mk

MCU_SERIES = l4
CMSIS_MCU = STM32L496xx
AF_FILE = boards/stm32l496_af.csv
LD_FILES = boards/stm32l496xg.ld boards/common_basic.ld
OPENOCD_CONFIG = boards/openocd_stm32l4.cfg

pins.csv

PA0,PA0
PA1,PA1
PA2,PA2
PA3,PA3
PA4,PA4
PA5,PA5
PA6,PA6
PA7,PA7
PA8,PA8
PA9,PA9
PA10,PA10
PA11,PA11
PA12,PA12
PA13,PA13
PA14,PA14
PA15,PA15
PB0,PB0
PB1,PB1
PB2,PB2
PB3,PB3
PB4,PB4
PB5,PB5
PB6,PB6
PB7,PB7
PB8,PB8
PB9,PB9
PB10,PB10
PB11,PB11
PB12,PB12
PB13,PB13
PB14,PB14
PB15,PB15
PC0,PC0
PC1,PC1
PC2,PC2
PC3,PC3
PC4,PC4
PC5,PC5
PC6,PC6
PC7,PC7
PC8,PC8
PC9,PC9
PC10,PC10
PC11,PC11
PC12,PC12
PC13,PC13
PC14,PC14
PC15,PC15
PD0,PD0
PD1,PD1
PD2,PD2
PD3,PD3
PD4,PD4
PD5,PD5
PD6,PD6
PD7,PD7
PD8,PD8
PD9,PD9
PD10,PD10
PD11,PD11
PD12,PD12
PD13,PD13
PD14,PD14
PD15,PD15
PE0,PE0
PE1,PE1
PE2,PE2
PE3,PE3
PE4,PE4
PE5,PE5
PE6,PE6
PE7,PE7
PE8,PE8
PE9,PE9
PE10,PE10
PE11,PE11
PE12,PE12
PE13,PE13
PE14,PE14
PE15,PE15
PF0,PF0
PF1,PF1
PF2,PF2
PF3,PF3
PF4,PF4
PF5,PF5
PF6,PF6
PF7,PF7
PF8,PF8
PF9,PF9
PF10,PF10
PF11,PF11
PF12,PF12
PF13,PF13
PF14,PF14
PF15,PF15
PG0,PG0
PG1,PG1
PG2,PG2
PG3,PG3
PG4,PG4
PG5,PG5
PG6,PG6
PG7,PG7
PG8,PG8
PG9,PG9
PG10,PG10
PG11,PG11
PG12,PG12
PG13,PG13
PG14,PG14
PG15,PG15
PH0,PH0
PH1,PH1
PH2,PH2
PH3,PH3
PH4,PH4
PH5,PH5
PH6,PH6
PH7,PH7
PH8,PH8
PH9,PH9
PH10,PH10
PH11,PH11
PH12,PH12
PH13,PH13
PH14,PH14
PH15,PH15
PI0,PI0
PI1,PI1
PI2,PI2
PI3,PI3
PI4,PI4
PI5,PI5
PI6,PI6
PI7,PI7
PI8,PI8
PI9,PI9
PI10,PI10
PI11,PI11

stm32l4xx_hal_conf.h

/* This file is part of the MicroPython project, http://micropython.org/
 * The MIT License (MIT)
 * Copyright (c) 2019 Damien P. George
 */
#ifndef MICROPY_INCLUDED_STM32L4XX_HAL_CONF_H
#define MICROPY_INCLUDED_STM32L4XX_HAL_CONF_H

// Oscillator values in Hz
#define HSE_VALUE (8000000)
#define LSE_VALUE (32768)
#define EXTERNAL_SAI1_CLOCK_VALUE (48000)
#define EXTERNAL_SAI2_CLOCK_VALUE (48000)

// Oscillator timeouts in ms
#define HSE_STARTUP_TIMEOUT (100)
#define LSE_STARTUP_TIMEOUT (5000)

#include "boards/stm32l4xx_hal_conf_base.h"

#endif // MICROPY_INCLUDED_STM32L4XX_HAL_CONF_H
  1. 构建MicroPython解释器固件
cd /build/ports/stm32
make BOARD=Aliosdevkit_stm32l496vgt6
  1. 烧录&测试
    Sending commands to MicroPhyton via serial connection
# 烧录固件
st-flash --format ihex write ./files/micropython/ports/stm32/build-Aliosdevkit_stm32l496vgt6/firmware.hex

使用串口助手测试(波特率:115200 bps):

# 语句末尾需要添加\r
help()

输出:

Welcome to MicroPython!

For online docs please visit http://docs.micropython.org/

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.RTC()     -- make an RTC object; methods: datetime([val])
  pyb.rng()     -- get a 30-bit hardware random number

Pins are numbered X1-X12, X17-X22, Y1-Y12, or by their MCU name
Pin IO modes are: pyb.Pin.IN, pyb.Pin.OUT, pyb.Pin.OPEN_DRAIN
Pin pull modes are: 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')
>>> 

使用MicroPython点灯

# -*- encoding:utf-8
# MicroPython
# STM32L496VGT6
# st-flash --format ihex write ./firmware/firmware.hex

'''
- Alios Things Dev kit 开发板
- STM32L496VGT6
- LED1:PB6(低电平有效)
- LED2:PE3(低电平有效)
- LED3:PD15(低电平有效)
- LCD_NSS:PA4
- LCD_SCK:PA5
- LCD_DC:PA6
- LCD_MOSI:PA7
- LCD_RST:PB2
- LCD_PWR:PE7
- LPUART1_RX:PB10
- LPUART1_TX:PB11
- AUDIO_RST:PD6
- AUDIO_CTL:PD5
- AUDIO_WAKE:PD4
- AUDIO_SDA:PD13
- AUDIO_SCL:PD12
- AUDIO_I2S_FS:PB12
- AUDIO_I2S_BCLK:PD10
- AUDIO_I2S_MCLK:PC6
- WIFI_RST:PB0
- WIFI_WU:PB1
- SHTC1_SCL:PB13(0xE0)
- SHTC1_SDA:PB14
- ALS_PROX_SDA:PB14(0x46)
- ALS_PROX_SCL:PB13
- ALS_PROX_INT:PB15
- PRESSURE_SDA:PB14
- PRESSURE_SCL:PB13
'''

from machine import Pin
import time
led = Pin("PB6", Pin.OUT)
while 1:
  led.high()
  time.sleep(1)
  led.low()
  time.sleep(1)

效果

ThonnyIDE发送MicroPython代码 开发板闪灯 串口调试
posted @ 2023-11-26 02:53  qsBye  阅读(144)  评论(0编辑  收藏  举报