MicroPython on STM32F407G-DISC1: Difference between revisions

From Embedded Workshop
Jump to navigation Jump to search
No edit summary
No edit summary
Line 17: Line 17:
Use TeraTerm, Putty, or other serial terminal program to connect to the USB Virtual COM Port<br>
Use TeraTerm, Putty, or other serial terminal program to connect to the USB Virtual COM Port<br>


'''What Can I Do with MicroPython and the STM32F407G-DISC1 Discovery Board?'''<br>
==What Can I Do with MicroPython and the STM32F407G-DISC1 Discovery Board?==
Let's begin with some simple exercises...
Let's begin with some simple exercises...
  '''Turn Blue LED on and off''' (4 is the blue LED)
=== Blue LED ON / Blue LED OFF ===
See https://docs.micropython.org/en/latest/library/pyb.LED.html
  '''# Turn Blue LED on and off''' (4 is the blue LED)
  blue=pyb.LED(4)
  blue=pyb.LED(4)
  blue.on()
  blue.on()
Line 26: Line 28:
The LED member functions '''on()''', and '''off()''' perform as expected.<br>
The LED member functions '''on()''', and '''off()''' perform as expected.<br>


'''# Blink Four LEDs (blinky1.py)'''
===# Blink Four LEDs (blinky1.py)===
  # LED color vs number: 1: red, 2: green, 3: orange, 4: blue
  # LED color vs number: 1: red, 2: green, 3: orange, 4: blue
  import pyb
  import pyb
Line 43: Line 45:
  That will cause the file, blinky1.py, to be loaded and executed
  That will cause the file, blinky1.py, to be loaded and executed
  - Use control-C  ^C  to break from loop and return to REPL interpreter
  - Use control-C  ^C  to break from loop and return to REPL interpreter
===# Switch with Orange LED (switch1.py)===
See: https://docs.micropython.org/en/latest/library/pyb.Switch.html
# When switch pressed, turn on orange LED, when released, turn off orange LED
orange=pyb.LED(3)
sw=pyb.Switch()
while True:
    if sw():
        orange.on()
    else:
        orange.off()
==Reference / Additional Information==
https://micropython.org

Revision as of 14:02, 24 April 2021

MicroPython, https://micropython.org/, a subset of the popular Python 3 programming language, is available to run on many development boards, including a large selection from STMicro.
The original development platform for MicroPython is the Pyboard, using an STM32F405RG microcontroller with 168 MHz Cortex M4 CPU, hardware floating point, 1024KiB flash ROM, and 192KiB RAM.
https://store.micropython.org/product/PYBv1.1H

Due to its feature set, lower price, and additional hardware, I chose the {{#if:|{{#ifexpr:({{#time:U|{{{3}}}}} - {{#time:U|now}}) > 0|STM32F407G-DISC1 Discovery Board|STM32F407G-DISC1 Discovery Board}}|STM32F407G-DISC1 Discovery Board}}, https://www.st.com/en/evaluation-tools/stm32f4discovery.html, for $19.50.

A MicroPython image for the board: https://micropython.org/download/stm32/ - Scroll down to Firmware For STM32F4DISC board
I just loaded the {{#if:|{{#ifexpr:({{#time:U|{{{3}}}}} - {{#time:U|now}}) > 0|STM32F4DISC-20210418-v1.15.dfu|STM32F4DISC-20210418-v1.15.dfu}}|STM32F4DISC-20210418-v1.15.dfu}} image onto my board.

Although many of the associated web pages use the DFU loader to program the MicroPython image into the microcontroller,
I chose to use the on-board JTAG adapter, making this a very easy two step process:

* After downloading the image, convert the .DFU image to .HEX using the DFU File Manager (Part of the DfuSe package).
  https://www.st.com/en/development-tools/stsw-stm32080.html
* Using STM32CubeProgrammer, write the .HEX file to the STM32F407G target processor.
  https://www.st.com/en/development-tools/stm32cubeprog.html

MicroPython with the STM32F407G-DISC1 Discovery Board creates a USB Virtual COM Port as well as a USB Drive, PYBFLASH, when connected to a Host computer.
Files may be added (or modified) within the PYBFLASH drive.
Use TeraTerm, Putty, or other serial terminal program to connect to the USB Virtual COM Port

What Can I Do with MicroPython and the STM32F407G-DISC1 Discovery Board?

Let's begin with some simple exercises...

Blue LED ON / Blue LED OFF

See https://docs.micropython.org/en/latest/library/pyb.LED.html

# Turn Blue LED on and off (4 is the blue LED)
blue=pyb.LED(4)
blue.on()
blue.off()

The above exercise, entered via the REPL interpreter, will create an LED object, called "blue"
The LED member functions on(), and off() perform as expected.

# Blink Four LEDs (blinky1.py)

# LED color vs number: 1: red, 2: green, 3: orange, 4: blue
import pyb
msdelay = 300 # ms delay
# Create list of LED objects in the order you wish to have them blink
leds = [pyb.LED(3),pyb.LED(1),pyb.LED(4),pyb.LED(2)]
while True:
    for led in leds:
        led.on()
        pyb.delay(msdelay)
        led.off()

The above text may be copied - paste into a file, blinky1.py, on the PYBFLASH drive
At the REPL prompt, enter:
>>> import blinky1
That will cause the file, blinky1.py, to be loaded and executed
- Use control-C  ^C  to break from loop and return to REPL interpreter

# Switch with Orange LED (switch1.py)

See: https://docs.micropython.org/en/latest/library/pyb.Switch.html

# When switch pressed, turn on orange LED, when released, turn off orange LED
orange=pyb.LED(3)
sw=pyb.Switch()
while True:
    if sw():
        orange.on()
    else:
        orange.off()

Reference / Additional Information

https://micropython.org