Micropython

Wed Nov 19 10:45:10 UTC 2014

Aim

We can have a high level development language on the board.

It make easierer the developement on any computer, as the STM32 appears as a USB stick. To update the code, you need to change files in it, and not flashing it.

The development is also easier as the project provides an interpreter which can be usefull for quick and dirty tests.

Install micropython on a stm34f407

Disclaimer :

Micropython was intended to work on a dedicated board. Nevetheless, it uses the same microcontroller as the STM32f407 discovery, so porting have been done on this board. Nevertheless,

You need a STM3207 (ie: the development board without screen) if you want to use micropython, I didn't suceesfully install it on the dev board with a screen.

Inspiration :

Most of the installation is describe here : http://gpio.kaltpost.de/?p=2082

Nevertheless, we won't use stlink utility to flash the device but openocd with gdb.

Tools needed :

We will use the stm32f4-vagrant project on github. All the tools are provided in it.

Otherwise, you will need standart cross compilation tools :

Steps :

You need to plug only the USB mini (at the top of the board, near the ST-LINK inscription) at this stage.

The begining is the same as in the tutorial :

Now we will flash the board :

    (gdb) monitor reset halt
    (gdb) load
    (gdb) continue

Now disconnect the USB mini to power off the board. Connect the USB mini and the micro-USB (the micro is used to communicate with the computer and the mini to power the board) and the magic should happen : your OS should notify you that a USB key has been plugged.

Let's play

Now that micropython is correctly installed, we can use it to do real science. When you plug the micro-USB in your computer, a PYBFLASH device will appear, if you open it you will find 4 files (boot.py, main.py, pybcdc.inf and README.txt). Read the information provided in the README to know how to basically use the board.

Use the interpreter

If you follow the instructions in the Readme, you can connect to the board using screen or any serial terminal emulator.

For instance, in Ubuntu 14.04, I use screen /dev/ttyACM0 to connect to the board.

Once logged in, you should have access to the interpreter. If you have a black screen, maybe an empty program is running and prevents you from getting the interpreter, try to press Ctrl-C to terminate it and get the micropython interpreter.

Now you should see this :

Micro Python v1.3.6-27-g3bdb23d on 2014-11-19; F4DISC with STM32F407
Type "help()" for more information.

>>>

You can type your command like in any python interpreter, try :

import math
import os
import sys

print(sys.version)  # python 3.4 \o/
a = (math.cos(i/100) for i in range(-100, 100))  # supports enumerator types
print(" - ".join(map(str, a)))  # all the python you love, try doing this in C...

If you kill the interpreter with Ctrl-D, the board will run a Soft reboot and will relaunch the program in the main.py.

Update code

The code run on the board lives in two places :

These files are accessible within the USB device, you can modify them, replug the board and the program should be running.

You can monitor the program with a screen session (you will see the print and use USB_VCP() to query informations). If a program has an uncaught exception, it will show the interpreter with the current context (all variales are set) to investigate the problem.

Hard reset

If you have changed the USB mode to HID the USB flash won't appear, thus, you won't be able to update your code again :(

On the classic PyBoard, you can remove the µSD card to modify the files (especially boot.py) on your computer or press a hard reset button.

On a STM32 discovery board you can't do this. Don't panic, you can still do a screen to access the interpreter and then :

Tools

A list of tools that can be interesting :