Difference between revisions of "STM32"

From RevSpace
Jump to navigation Jump to search
m (Pinout LQFP48)
(40 intermediate revisions by 2 users not shown)
Line 3: Line 3:
 
   |Picture=HTB1PemDJpXXXXXHXpXXq6xXFXXXM.jpg
 
   |Picture=HTB1PemDJpXXXXXHXpXXq6xXFXXXM.jpg
 
   |Omschrijving=STM32 setup guide
 
   |Omschrijving=STM32 setup guide
   |Status=Initializing
+
   |Status=Completed
 
   |Contact=bertrik
 
   |Contact=bertrik
 
   }}
 
   }}
  
 
== Introduction ==
 
== Introduction ==
 +
[[File:bluepill.jpg|thumb|right]]
 
This page is about inexpensive microcontroller boards containing an STM32 processor and how to get them to work.
 
This page is about inexpensive microcontroller boards containing an STM32 processor and how to get them to work.
  
You can find these board on AliExpress for less than E2,- if you search for "stm32f103c8t6".
+
You can find these boards on AliExpress for less than €2,- if you search for "stm32f103c8t6".
 
Yet they have nice specifications, see also http://www.st.com/en/microcontrollers/stm32f103c8.html  
 
Yet they have nice specifications, see also http://www.st.com/en/microcontrollers/stm32f103c8.html  
  
Line 25: Line 26:
 
* [http://grauonline.de/wordpress/?page_id=1004 Arduino goes STM32], a quick tutorial
 
* [http://grauonline.de/wordpress/?page_id=1004 Arduino goes STM32], a quick tutorial
 
* [https://github.com/rogerclarkmelbourne/Arduino_STM32 Arduino STM32 page of Roger Clark]
 
* [https://github.com/rogerclarkmelbourne/Arduino_STM32 Arduino STM32 page of Roger Clark]
 +
 +
[http://www.oddwires.com/blog/using-a-generic-stm32-board-with-arduino Another, very similar, guide to getting started with STM32 and Arduino].
 +
 +
There are several ways to program an STM32:
 +
* serial: using a built-in serial port bootloader, you need a USB-serial converter for this and you need to move some jumpers each time
 +
* stlink: using an ST-LINK programmer and 4 wires, clones can be found very cheaply on AliExpress (about E2,-)
 +
* stm32duino USB bootloader: after installing the bootloader once (using the serial method), you can rewrite a new program over USB, you just need a USB micro cable
 +
 +
I like the STM32duino bootloader method.
 +
This way, you need to wire up a USB/serial conversion board and set/reset jumpers only once for bootloader installation and from that point on you can program the board over USB.
  
 
== Setting up the tool chain ==
 
== Setting up the tool chain ==
 
This describes the steps I did to get a "blue pill" board to work on Debian Jessie.
 
This describes the steps I did to get a "blue pill" board to work on Debian Jessie.
 +
 +
=== Linux specific ===
 +
Some things you need to do on a system level:
 +
* the usual stuff to add your user to the 'dialout' group so you have permission to access the serial port (log out and log in after this)
 +
  sudo adduser <username> dialout
 +
* uninstall the 'modemmanager' package, it interferes with the serial port device created by the STM32duino bootloader
 +
  sudo apt autoremove modemmanager
 +
* install the 32-bit libraries if you're on a 64-bit OS but using the 32-bit binaries (stmflash etc.)
 +
  sudo apt install libc6-i386
  
 
=== Arduino IDE ===
 
=== Arduino IDE ===
Line 47: Line 67:
 
* restart the Arduino IDE.
 
* restart the Arduino IDE.
  
=== stm32flash ===
+
=== Platform IO ===
If you're running a 64-bit Linux, it can be convenient to add support for the 32-bit stm32flash utility, run:
+
Platformio allows you to build/upload/monitor Arduino programs without using the Arduino IDE (and more).
 +
All settings (which target, which libraries, etc) are stored in a platformio.ini file (instead of existing only as selections in the Arduino IDE).
 +
 
 +
Below is an example platformio configuration for a blue pill with the USB bootloader.
 +
Save this to a file named platformio.ini in the directory where the .ino file is.
 +
You can build the binary with 'pio run', upload it with 'pio run -t upload' and interact with your sketch over the serial port with 'pio device monitor'.
 +
 
 
<pre>
 
<pre>
sudo apt-get install libc6-i386
+
[platformio]
 +
src_dir = .
 +
 
 +
[env:default]
 +
platform = ststm32
 +
board = genericSTM32F103C8
 +
framework = arduino
 +
upload_protocol = dfu
 
</pre>
 
</pre>
  
The tool chain installation should now be ready.
+
Note: don't use 'bluepill_f103c8' as board, it is not compatible with the stm32duino bootloader!
  
 
== Hardware ==
 
== Hardware ==
[[File:bluepill.jpg|thumb|right]]
+
[[File:bluepill_pinout.png|right|thumb]]
  
 
The hardware I'm using, is the [http://wiki.stm32duino.com/index.php?title=Blue_Pill blue pill], I soldered on the headers for easy plugging with dupont-wire.
 
The hardware I'm using, is the [http://wiki.stm32duino.com/index.php?title=Blue_Pill blue pill], I soldered on the headers for easy plugging with dupont-wire.
You'll also need a USB-serial converter.
 
  
I hooked it up as follows:
+
=== Pinout LQFP48 ===
 +
[[File:stm32_lqfp48_pinout.png|right|thumb]]
 +
[http://www.olliw.eu/ OlliW] made a nice pinout diagram of the STM32 in the LQFP48 package, which I'm reproducing here (converted from JPG to PNG).
 +
 
 +
=== Flashing the USB bootloader ===
 +
The USB bootloader makes it easy/fast to upload new software into the board over USB.
 +
 
 +
You need a USB-serial converter for this.
 +
 
 +
Wire it up as follows:
 
* converter 5V -> board 5V
 
* converter 5V -> board 5V
 
* converter GND -> board GND
 
* converter GND -> board GND
Line 67: Line 108:
 
* converter TXD -> board A10
 
* converter TXD -> board A10
  
To put the board into programming mode, set the BOOT0 jumper to the "1" position and the BOOT1 jumper to the "0" position, then push the reset button.
 
 
To just run the program that was last flashed into the board, set both the BOOT0 and the BOOT1 jumper to the "0" position.
 
  
=== Unlocking/erasing the first time ===
+
For the blue pill the steps to install the bootloader are as follows:
You might find that the flash is protected on a new board, this means you cannot put your own program on the board.
+
* clone the git archive containing the STM32 bootloaders:
You can fix this as follows:
+
  git clone https://github.com/rogerclarkmelbourne/STM32duino-bootloader
* go the stm32flash directory
+
* cd into the directory Arduino_STM32/tools/linux/stm32flash
<pre>cd ~/Arduino/hardware/Arduino_STM32/tools/linux/stm32flash</pre>
+
* set the BOOT0 jumper to the "1" position and the BOOT1 jumper to the "0" position, then push the reset button.
* use the stm32flash utility to disable the flash-read protection
+
* disable the flash-read protection:
<pre>./stm32flash /dev/ttyUSB0 -k</pre>
+
  ./stm32flash /dev/ttyUSB0 -k
* use the stm32flash utility to erase the flash (not completely sure if this is needed)
+
* flash the bootloader:
<pre>./stm32flash /dev/ttyUSB0 -o</pre>
+
  ./stm32flash -w ~/code/stm32/STM32duino-bootloader/binaries/generic_boot20_pc13.bin -v -g 0x0 /dev/ttyUSB0
 +
* return both BOOT jumpers to the "0" position.
  
 
== Software ==
 
== Software ==
To load your application:
+
To load your application using the Arduino IDE:
* make sure you have the BOOT jumpers in the right position and press the reset button
 
 
* in the Arduino IDE, under menu Tools / Board, select "Generic STM32F103C Series"
 
* in the Arduino IDE, under menu Tools / Board, select "Generic STM32F103C Series"
* in the Arduino IDE, under menu Tools / Upload Method, select "Serial"
+
* in the Arduino IDE, under menu Tools / Upload Method, select "STM32duino bootloader"
 
* use the following program to blink the on-board LED:
 
* use the following program to blink the on-board LED:
 
<pre>
 
<pre>
Line 102: Line 140:
 
</pre>
 
</pre>
 
* ctrl-U to upload it to the board, the program will start automatically
 
* ctrl-U to upload it to the board, the program will start automatically
* to make the program run after a reboot, place the BOOT0 jumper back to its original position.
 
 
The Arduino console should show something like this:
 
<pre>
 
Sketch uses 7,140 bytes (5%) of program storage space. Maximum is 131,072 bytes.
 
Global variables use 1,984 bytes of dynamic memory.
 
/home/bertrik/Arduino/hardware/Arduino_STM32/tools/linux/serial_upload ttyUSB0 {upload.altID} {upload.usbID} /tmp/arduino_build_33274/stm32.ino.bin
 
stm32flash Arduino_STM32_0.9
 
 
http://github.com/rogerclarkmelbourne/arduino_stm32
 
  
Using Parser : Raw BINARY
+
= Future work =
Interface serial_posix: 230400 8E1
+
Actually build something with an STM32!
Version      : 0x22
 
Option 1    : 0x00
 
Option 2    : 0x00
 
Device ID    : 0x0410 (Medium-density)
 
- RAM        : 20KiB  (512b reserved by bootloader)
 
- Flash      : 128KiB (sector size: 4x1024)
 
- Option RAM : 16b
 
- System RAM : 2KiB
 
Write to memory
 
Erasing memory
 
  
Wrote address 0x08000100 (3.59%)
+
Ideas:
Wrote address 0x08000200 (7.17%)
+
* a LoRaWAN node for The-Things-Network
...
+
* a bat call recorder, this thing is faster and has more memory than the LPC2148 in my previous bat call recorder prototype
Wrote address 0x08001be4 (100.00%) Done.
 
  
Starting execution at address 0x08000000... done.
+
Investigate the [https://github.com/blacksphere/blackmagic/wiki Blackmagic] debugging firmware.
</pre>
+
This should allow you to debug your applications (e.g. step through code in an IDE such as eclipse).
 +
See also:
 +
* https://nuft.github.io/arm/2015/08/24/blackmagic-stlink.html
 +
* https://medium.com/@paramaggarwal/converting-an-stm32f103-board-to-a-black-magic-probe-c013cf2cc38c#.h2md3y5hl

Revision as of 12:16, 21 April 2021

Project STM32
HTB1PemDJpXXXXXHXpXXq6xXFXXXM.jpg
STM32 setup guide
Status Completed
Contact bertrik
Last Update 2021-04-21

Introduction

Bluepill.jpg

This page is about inexpensive microcontroller boards containing an STM32 processor and how to get them to work.

You can find these boards on AliExpress for less than €2,- if you search for "stm32f103c8t6". Yet they have nice specifications, see also http://www.st.com/en/microcontrollers/stm32f103c8.html

To name a few:

  • 32-bit ARM Cortex-M3 processor running at up to 72 MHz
  • 128 kB flash memory, 20 kB SRAM
  • USB and CAN controllers
  • 32 kHz crystal for RTC
  • dual 1 us A/D converter, DMA controller
  • the usual stuff like SPI, UART, I2C

They are even Arduino compatible, see:

Another, very similar, guide to getting started with STM32 and Arduino.

There are several ways to program an STM32:

  • serial: using a built-in serial port bootloader, you need a USB-serial converter for this and you need to move some jumpers each time
  • stlink: using an ST-LINK programmer and 4 wires, clones can be found very cheaply on AliExpress (about E2,-)
  • stm32duino USB bootloader: after installing the bootloader once (using the serial method), you can rewrite a new program over USB, you just need a USB micro cable

I like the STM32duino bootloader method. This way, you need to wire up a USB/serial conversion board and set/reset jumpers only once for bootloader installation and from that point on you can program the board over USB.

Setting up the tool chain

This describes the steps I did to get a "blue pill" board to work on Debian Jessie.

Linux specific

Some things you need to do on a system level:

  • the usual stuff to add your user to the 'dialout' group so you have permission to access the serial port (log out and log in after this)
 sudo adduser <username> dialout
  • uninstall the 'modemmanager' package, it interferes with the serial port device created by the STM32duino bootloader
 sudo apt autoremove modemmanager
  • install the 32-bit libraries if you're on a 64-bit OS but using the 32-bit binaries (stmflash etc.)
 sudo apt install libc6-i386

Arduino IDE

Steps to set up the IDE:

  • get and install the latest Arduino IDE from here.
  • under menu Tools / Board / Board Manager, search for "zero" and install the Arduino Zero toolchain
  • get the Arduino_STM32 source code, for example, run in the console:
cd ~/code
mkdir stm32
cd stm32
git clone https://github.com/rogerclarkmelbourne/Arduino_STM32
  • create a symlink to the Arduino_STM32 source tree in your ~/Arduino/hardware directory, for example:
cd ~/Arduino/hardware
ln -s ~/code/stm32/Arduino_STM32 .
  • restart the Arduino IDE.

Platform IO

Platformio allows you to build/upload/monitor Arduino programs without using the Arduino IDE (and more). All settings (which target, which libraries, etc) are stored in a platformio.ini file (instead of existing only as selections in the Arduino IDE).

Below is an example platformio configuration for a blue pill with the USB bootloader. Save this to a file named platformio.ini in the directory where the .ino file is. You can build the binary with 'pio run', upload it with 'pio run -t upload' and interact with your sketch over the serial port with 'pio device monitor'.

[platformio]
src_dir = .

[env:default]
platform = ststm32
board = genericSTM32F103C8
framework = arduino
upload_protocol = dfu

Note: don't use 'bluepill_f103c8' as board, it is not compatible with the stm32duino bootloader!

Hardware

Bluepill pinout.png

The hardware I'm using, is the blue pill, I soldered on the headers for easy plugging with dupont-wire.

Pinout LQFP48

Stm32 lqfp48 pinout.png

OlliW made a nice pinout diagram of the STM32 in the LQFP48 package, which I'm reproducing here (converted from JPG to PNG).

Flashing the USB bootloader

The USB bootloader makes it easy/fast to upload new software into the board over USB.

You need a USB-serial converter for this.

Wire it up as follows:

  • converter 5V -> board 5V
  • converter GND -> board GND
  • converter RXD -> board A9
  • converter TXD -> board A10


For the blue pill the steps to install the bootloader are as follows:

  • clone the git archive containing the STM32 bootloaders:
 git clone https://github.com/rogerclarkmelbourne/STM32duino-bootloader
  • cd into the directory Arduino_STM32/tools/linux/stm32flash
  • set the BOOT0 jumper to the "1" position and the BOOT1 jumper to the "0" position, then push the reset button.
  • disable the flash-read protection:
 ./stm32flash /dev/ttyUSB0 -k
  • flash the bootloader:
 ./stm32flash -w ~/code/stm32/STM32duino-bootloader/binaries/generic_boot20_pc13.bin -v -g 0x0 /dev/ttyUSB0
  • return both BOOT jumpers to the "0" position.

Software

To load your application using the Arduino IDE:

  • in the Arduino IDE, under menu Tools / Board, select "Generic STM32F103C Series"
  • in the Arduino IDE, under menu Tools / Upload Method, select "STM32duino bootloader"
  • use the following program to blink the on-board LED:
#define pinLED PC13

void setup() {
  pinMode(pinLED, OUTPUT);
}

void loop() {
  digitalWrite(pinLED, HIGH);
  delay(200);
  digitalWrite(pinLED, LOW);
  delay(100);
}
  • ctrl-U to upload it to the board, the program will start automatically

Future work

Actually build something with an STM32!

Ideas:

  • a LoRaWAN node for The-Things-Network
  • a bat call recorder, this thing is faster and has more memory than the LPC2148 in my previous bat call recorder prototype

Investigate the Blackmagic debugging firmware. This should allow you to debug your applications (e.g. step through code in an IDE such as eclipse). See also: