The ME405 Python code library contains a set of routines which implement a simple cooperative scheduler that runs in MicroPython. This library is used for teaching the development of mechatronic systems, including modestly complex real-time software for use in embedded systems. Its use is not recommended in production systems because the code has not been thoroughly tested to ensure reliability. As of this writing, the same warning should be made for MicroPython, on which this software is based; as a new product, MicroPython still has bugs and features which significantly affect reliability. It is expected that the reliability of both ME405 code and MicroPython will improve rapidly in the next few years, and users of this code are encouraged to contribute to the development of both.
The ME405 course runs MicroPython on Nucleo-64TM boards from ST Microelectronics, taking advantage of the boards' high value for the money (we're a state supported school teaching some of the best of the 99%). We usually employ a simple custom board called the Shoe of Brian which sits below the Nucleo and houses a USB OTG "On-The-Go" connector. The USB OTG connector allows mounting some of the microcontroller's flash memory as a USB file system, making for convenient programming. We prefer to leave the Nucleo's ST-Link 2.1 programmer attached so that the board can be programmed in C++ for high-performance applications if needed in other mechatronics and Senior project courses. The ME405 software was deleloped using a Nucleo-L476RG board; this version works much better for our purposes than the -F401 and -F411 versions because of the much larger available flash memory. The processors with 512MB flash cannot easily accommodate MicroPython itself plus a substantial group of Python programs such as the ME405 software suite, unless provided with SD cards, which add to the expense and complexity of the system.
If you are interested in building your own ME405 circuit, please refer to the following page: The Shoe of Brian
If you'd rather not build your own board, note that many STM32 DiscoveryTM boards are supplied with built-in USB OTG connectors, allowing similarly convenient programming without having to build custom circuitry. The original MicroPython PyBoard has both USB OTG and Micro-SD card connectors and makes an excellent platform for this software, though minor modifications may be needed to accommodate the slightly different processor and pinouts.
As shipped, 64-pin NucleoTM boards get power from the USB connector on the ST-Link programmer attached to those boards. In order to be powered from the Shoe of Brian boards underneath and reliably operate their USB-OTG file systems, Nucleo-64 boards need their JP5 jumpers moved from the as-delivered connection of pins 1 to 2 to the other side of the header block, pins 2 to 3. See ST's user manual UM1724, the user manual for Nucleo-64 boards for more details and schematics. Strangeness: One batch of STM32L476RG boards has worked without moving this jumper, but another batch requires the jumper to be moved to operate correctly.
cotask.py
.task_share.py
.This section contains some quick hints about how to use various functions of the NucleoTM boards with MicroPython as they are set up for ME405.
MicroPython has two time related libraries, time
and utime
, which provide the same functionality. The use of utime
is recommended to avoid confusion with the standard Python library called time
, which has different functionality.
Reference: https://docs.micropython.org/en/latest/pyboard/library/utime.html
Reference: https://docs.micropython.org/en/latest/pyboard/library/pyb.Pin.html
Reference: https://docs.micropython.org/en/latest/pyboard/library/pyb.ExtInt.html
The frequency given in the freq
parameter of the pyb.Timer
constructor is the frequency at which the timer overflows and restarts counting at zero. When using timer interrupts, this is the frequency at which interrupts occur. Only certain pins can be used with timers. Look for pins associated with the channels of timers in the STM32 data sheet.
Reference: https://docs.micropython.org/en/latest/pyboard/library/pyb.Timer.html
Only certain pins can be used for PWM. Look for pins associated with the channels of timers in the STM32 data sheet.
Reference: https://docs.micropython.org/en/latest/pyboard/library/pyb.Timer.html
It is very important to apply only voltages in the range from 0 to 3.3V to each A/D pin. Excessive or negative voltages can destroy things. Not all pins can be used as A/D pins; see the STM32 datasheet pin alternate function table to find out which ones will work (or just try some in the REPL and see which don't give you a ValueError
).
Reference: https://docs.micropython.org/en/latest/pyboard/library/pyb.ADC.html
Some STM32 chips have digial to analog converters, and others don't. Digital to analog conversion is not officially supported on ME405 boards, but you can look for class pyb.DAC
and if it's there, search for MicroPython documentation on how to use it.
The following example is for hardware I²C on the STM32.
Reference: https://docs.micropython.org/en/latest/pyboard/library/pyb.I2C.html
It is possible that the flash memory on a Nucleo may become corrupted. This problem seems to manifest itself by read or write errors with source files on the PYBFLASH drive, or by Thonny complaining about its backend. A solution may be attempted by reformatting the processor's internal flash drive (unless you are using a MicroSD card, in which case just take that out and reformat it in a PC). The following procedures should reformat the flash, but your mileage may vary.
There are no guarantees this will work and not harm your system. Before you run these commands, make sure to back up any files on the MicroPython board if you can read them.
Recommended Procedure - as of 2024, anyway
Hold the blue button down and briefly push the black reset button while continuing to hold the blue button. The green LED will blink once, then twice, then three times; after the three flashes, release the blue button and wait a few seconds while the green light does more blinky things – and then wait another 10 seconds after it stops. Your PYBFLASH
drive should now be freshly formatted. Unplug the board from your PC to power it off, wait a few seconds, then plug it back in. All files except boot.py
, main.py
, pybcdc.inf
, and README.txt
should be gone. If your old files are still there, try ejecting (or unmounting, or "removing" depending on what your OS calls it) the PYBFLASH drive on the PC, then unplugging the MicroPython board, waiting several seconds, and plugging it in again.
Reference: https://docs.micropython.org/en/latest/pyboard/pyboard/tutorial/reset.html
Alternative Procedure - MicroPython 1.13 from 2021 and later (maybe)
Make sure you're at the REPL prompt and run the following lines:
The flash drive may lose its name PYBFLASH and get a random numerical name, but it will have been newly set up and should work well again.