Skip to content

Instantly share code, notes, and snippets.

@nebelgrau77
Last active March 12, 2024 16:11
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save nebelgrau77/62916ed8d52007991f9be3eb6c5b09e2 to your computer and use it in GitHub Desktop.
Save nebelgrau77/62916ed8d52007991f9be3eb6c5b09e2 to your computer and use it in GitHub Desktop.
how to install MicroPython on cheap STM32F4xx boards ("black pill", "black F407")

Installing MicroPython on inexpensive STM32F4xx boards

This tutorial will explain step by step how to build and deploy MicroPython on STM32F407xx and STM32F411CEU boards, using both DFU mode over USB, as well as SWD with ST-Link and OpenOCD in case you can't get the board into DFU mode (happened to me with the "black pill").

It should be applicable to other boards, such as the other "black pill" STM32F401, Nucleo boards etc., provided you can find an appropriate board definition to build.

1. clone the MicroPython repo:

git clone https://github.com/micropython/micropython

2. build submodules

in micropython/ports/stm32 folder:

make submodules

(this will take a while, as it will built the necessary STM32 pieces of code)

3. build mpy-cross

in micropython/ folder:

make -C mpy-cross

(this will take a while, as it will build the necessary cross compiler code)

4. clone the board repo:

in micropython/ports/stm32/boards folder:

  • STM32F407xxx 'black tile'

git clone https://github.com/mcauser/BLACK_F407VE.git
  • STM32F411CEx 'black pill'

git clone https://github.com/mcauser/WEACT_F411CEU6.git

5. build uPy for the boards

in micropython/ports/stm32/ folder:

  • STM32F407xxx 'black tile'

make BOARD=BLACK_F407VE
  • STM32F411CEx 'black pill'

make BOARD=WEACT_F411CEU6

6. flash the board

  • using DFU mode: STM32F407xxx 'black tile' example

put the board into the DFU mode, connecting BT0 pin to 3V3 with a jumper as described, then connect the USB cable and do:

make BOARD=BLACK_F407VE deploy

after it's done, unplug the board, connect BT0 to GND with the jumper, and reconnect the board

  • using SWD with ST-Link: STM32F411CEx 'black pill' example

cd micropython/ports/stm32/build-WEACT_F411CEU6

openocd -f interface/stlink-v2.cfg -f target/stm32f0x.cfg

in another terminal window:

telnet localhost 4444

> reset halt
> flash write_image erase build-STM32F4NUCLEO/firmware.elf

7. test the MicroPython installation

rshell -p /dev/ttyACM0 repl

sources:

@mogplus88
Copy link

Hi,
I tried to put micropython on my black pill following these instructions, but it failed at the deploy step.

ian@Mintian:~/micropython/ports/stm32$ make BOARD=WEACT_F411CEU6 deploy
Use make V=1 or set BUILD_VERBOSE in your environment to increase build verbosity.
Writing build-WEACT_F411CEU6/firmware.dfu to the board
Traceback (most recent call last):
  File "/home/ian/micropython/ports/stm32/../../tools/pydfu.py", line 22, in <module>
    import usb.core
ModuleNotFoundError: No module named 'usb'
make: *** [Makefile:604: deploy] Error 1

I used the 'hold down the boot button while pressing and releasing the nrst button' to put it into dfu mode as documented in various places, rather than shorting bt0 to 3v3 as you recommend, but I'm not sure that would be an issue. It looks to me like something bad has happened during the build process and a library has been missed somehow. There were no error messages displayed in any of the steps before this one. How do I get around this problem?
Thanks, Ian

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment