How to build and run MicroPython on STM32F7-Disco board
Posted: Sat Nov 05, 2016 7:11 pm
Hi!
I recently discovered the MicroPython project, and I wanted to give it a try. After some research, I found out that MicroPython is ported to the STM32F7 microcontroller, and the STM32F7-Disco board. Nice, I have one laying around!
I did not find complete documentation about building and running MicroPython on this board, but after some tries and errors, I managed to build and run it on my board! Here is what I did.
First, my setup : I mainly work on Windows 10, but I have a Debian virtual machine (VirtualBox).
I used the Debian virtual machine as most of the documentation about building MicroPython is related to Linux (and that's a very good point!).
First, get the source using git:
$ git clone https://github.com/micropython/micropython.git
This will download the source to the directory ./micropython
You can build the Unix version of MicroPython if you want. For that, you need to install some dependancies:
$ apt-get install build-essential libffi-dev pkg-config
Then go into the Unix folder:
$ cd micropython/unix
And build MicroPython for Unix:
$ make
You can then run the interpreter :
$ ./micropython
Now, let's build MicroPython for our STM32F7DISCO board. We need a toolchain (compiler and other tools) to cross-compile micropython. Indeed, my computer is running a 64bits Intel Processor, and we need to build MicroPython to run on an ARM Cortex M7 microcontroller.
There is a package in Debian named 'gcc-arm-none-eabi' which should do the job, except that this is an old version (4.8) of GCC that has no knowledge of the Cortex-M7 architecture.
We'll need to find another compiler. I found useful information about this on this page : http://gnuarmeclipse.github.io/toolchain/install/
First, download the toolchain (GCC ARM Embedded) : https://launchpad.net/gcc-arm-embedded/+download. You'll want the 'Linux installation tarball'.
Download it and uncompress it on your local hard-drive.
As this toolchain is built for 32bits Linux and as I use a 64bits Debian, I needed to install extra packages :
$ apt-get -y install lib32z1 lib32ncurses5
Then, go to the micropython directory and start the compilation:
$ cd ~/micropython
$ cd stmhal
$ make BOARD=STM32F7DISC CROSS_COMPILE=<Path where you uncompressed the toolchain>/bin/arm-none-eabi-
This will build Micropython into the folder build-STM32F7DISC. Just look for the files firmware.dfu, firmware.hex,...
Next, we need to flash the board with this binary file. It is easily done using the STLINK USB interface of the board.
I did this step in Windows, as I never used the STLINK on Linux.
Download STLINK drivers and utility from this page : http://www.st.com/en/development-tools/st-link-v2.html
Connect a mini-USB cable between the board and your computer. Windows should detect the STLINK interface and a new COM port (it'll be used as the REPL port once the firmware will be uploaded).
Start the STM32 ST-Link Utility, and open (File->Open File) the file 'firmware.hex' that was created before.
Screenshot : https://pydio.codingfield.com/data/public/2c7c2d--en
Then, connect to the target (Target->Connect) and upload the file (Target->Program).
Screenshot : https://pydio.codingfield.com/data/public/4813f5--en
I use Putty as a serial terminal (just open a connection on COMx, 115200 bauds). After the uploading of the firmware, the board reset and... the Python interpreter appeard on the screen!
Screenshot : https://pydio.codingfield.com/data/public/715c4a--en
How is that possible? MicroPython is configured to use the UART1 of the µC as the REPL port. It is connected to the STLINK device on the board, and the USB interface of the STLINK exposes this UART as a USB-serial port to Windows.
And I've just discovered that the micro-USB port on the right of the STLINK port is seen by windows as a USB storage, which is used by MicroPython to store your .py files.
Now, I'm ready to play with MicroPython
Pictures : https://pydio.codingfield.com/data/public/362b88--en and https://pydio.codingfield.com/data/public/e27154--en
I recently discovered the MicroPython project, and I wanted to give it a try. After some research, I found out that MicroPython is ported to the STM32F7 microcontroller, and the STM32F7-Disco board. Nice, I have one laying around!
I did not find complete documentation about building and running MicroPython on this board, but after some tries and errors, I managed to build and run it on my board! Here is what I did.
First, my setup : I mainly work on Windows 10, but I have a Debian virtual machine (VirtualBox).
I used the Debian virtual machine as most of the documentation about building MicroPython is related to Linux (and that's a very good point!).
First, get the source using git:
$ git clone https://github.com/micropython/micropython.git
This will download the source to the directory ./micropython
You can build the Unix version of MicroPython if you want. For that, you need to install some dependancies:
$ apt-get install build-essential libffi-dev pkg-config
Then go into the Unix folder:
$ cd micropython/unix
And build MicroPython for Unix:
$ make
You can then run the interpreter :
$ ./micropython
Now, let's build MicroPython for our STM32F7DISCO board. We need a toolchain (compiler and other tools) to cross-compile micropython. Indeed, my computer is running a 64bits Intel Processor, and we need to build MicroPython to run on an ARM Cortex M7 microcontroller.
There is a package in Debian named 'gcc-arm-none-eabi' which should do the job, except that this is an old version (4.8) of GCC that has no knowledge of the Cortex-M7 architecture.
We'll need to find another compiler. I found useful information about this on this page : http://gnuarmeclipse.github.io/toolchain/install/
First, download the toolchain (GCC ARM Embedded) : https://launchpad.net/gcc-arm-embedded/+download. You'll want the 'Linux installation tarball'.
Download it and uncompress it on your local hard-drive.
As this toolchain is built for 32bits Linux and as I use a 64bits Debian, I needed to install extra packages :
$ apt-get -y install lib32z1 lib32ncurses5
Then, go to the micropython directory and start the compilation:
$ cd ~/micropython
$ cd stmhal
$ make BOARD=STM32F7DISC CROSS_COMPILE=<Path where you uncompressed the toolchain>/bin/arm-none-eabi-
This will build Micropython into the folder build-STM32F7DISC. Just look for the files firmware.dfu, firmware.hex,...
Next, we need to flash the board with this binary file. It is easily done using the STLINK USB interface of the board.
I did this step in Windows, as I never used the STLINK on Linux.
Download STLINK drivers and utility from this page : http://www.st.com/en/development-tools/st-link-v2.html
Connect a mini-USB cable between the board and your computer. Windows should detect the STLINK interface and a new COM port (it'll be used as the REPL port once the firmware will be uploaded).
Start the STM32 ST-Link Utility, and open (File->Open File) the file 'firmware.hex' that was created before.
Screenshot : https://pydio.codingfield.com/data/public/2c7c2d--en
Then, connect to the target (Target->Connect) and upload the file (Target->Program).
Screenshot : https://pydio.codingfield.com/data/public/4813f5--en
I use Putty as a serial terminal (just open a connection on COMx, 115200 bauds). After the uploading of the firmware, the board reset and... the Python interpreter appeard on the screen!
Screenshot : https://pydio.codingfield.com/data/public/715c4a--en
How is that possible? MicroPython is configured to use the UART1 of the µC as the REPL port. It is connected to the STLINK device on the board, and the USB interface of the STLINK exposes this UART as a USB-serial port to Windows.
And I've just discovered that the micro-USB port on the right of the STLINK port is seen by windows as a USB storage, which is used by MicroPython to store your .py files.
Now, I'm ready to play with MicroPython
Pictures : https://pydio.codingfield.com/data/public/362b88--en and https://pydio.codingfield.com/data/public/e27154--en