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 :
- gcc-arm-none-eabi
- gdb-arm-none-eabi
- libnewlib-arm-none-eabi
- openocd
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 :
- get the sources :
git clone https://github.com/micropython/micropython.git
- go to the right directory
cd micropython/stmhal
- compile for the board
make BOARD=STM32F4DISC
Now we will flash the board :
- Using openocd / gdb
- run openocd :
sudo openocd -f board/stm32f4discovery.cfg
- run gdb to flash :
arm-none-eabi-gdb -ex "target extended-remote :3333" build-STM32F4DISC/firmware.elf
- if you have error jtag status messages try pressing the reset button while launching openocd and gdb
- update the firmware :
- run openocd :
(gdb) monitor reset halt
(gdb) load
(gdb) continue
-
or using dfu :
- run :
sudo dfu-util -a 0 -d 0483:df11 -D build-CERB40/firmware.dfu
- run :
-
or using openOCD alone :
sudo openocd -f board/stm32f4discovery.cfg -c "program build-STM32F4DISC/firmware.elf"
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 :
- boot.py : all the things to launch on boot, escpecially USB mode (HID or storage), and the program to launch then.
- main.py : the program that will be launched by boot.py
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 :
-
you can inspect the files with :
print("".join(open("main.py", "r").readlines()[:20]))
print("".join(open("boot.py", "r").readlines()))
-
open("boot.py", "w")
to erase the boot.py file (equivalent to a hard reset, it will be automatically recreated on next boot)
you can use the
pyb.hard_reset
manually to simulate the button- #protip : you can bind a callback on a button on a gpio to execute
pyb.hard_reset
when pressed, put this on yourboot.py
to keep your script files focused on what they should do. - Thanks to Michel Amberg we know how the flash is constructed. We can erase only the part containing the boot.py and main.py, they will be recreated during next boot :
- create a file zoro.bin containing 1000 0-bytes :
dd count=1 bs=1000 if=/dev/zero of=zero.bin
- load the file to the STM32 with dfu :
sudo dfu-util -a 0 -d 0483:df11 -s 0x8004000 -D zero.bin
- to load with openocd:
sudo openocd -f board/stm32f4discovery.cfg -c "program zero_fs.bin 0x8004000"
- create a file zoro.bin containing 1000 0-bytes :
Tools
A list of tools that can be interesting :
- PybKick : push code to the board without using the mass storage, can be interesting when used in HID mode (no need to hard reset the board).