Do you want to start STM32 programming? Get the Blue Pill which is featuring the powerful & cheap ARM Cortex-M3 32-bit microcontroller then, let’s go!
We are going to build a LED blink app from scratch, using only the manual of the STM32F103C8T6 chip. The host platform used for this guide is Linux Mint 19.3. You can use as well, Ubuntu, Raspbian, and even Microsoft Windows.
The hardware: the blue pill and for uploading the firmware we need at least a ST-Link v2 device.
Prior knowledge: basic Linux operation, not being afraid of command line, C language, basic embedded stuff.
Steps to do
We create in the same directory four files named:
Make a test build
Connect the ST-Link
Explain the schematic and documentation
Write the code in main.c
registers and bits definitions
infinite loop that turns on and off the LED
Build and upload to the board
void main (void)
For now it is just an infinite loop that does nothing.
C run-time assembly file crt.s
// end of 20K RAM
If you are not familiar with the ARM/thumb assembly and you don’t want to be, just skip the explanations below.
The first two lines are instructing the assembler to generate thumb code for the Cortex-M3 core. The thumb code is more compact in terms of memory footprint, but is a bit slower at run time. The value written at first .word line will be placed at the very beginning of the binary file at address 0x0000_0000 and represents the end of RAM. The second .word line is the reset vector and is placed at 0x0000_0004. The .thumb_func directive instructs the assembler to generate a thumb executable code from the subsequent lines. The last two lines are the jump to main() and the infinite loop, in case someone wants to exit from the main function.
This makefile can be used for multiple things: for build, clean, upload or erase target. If you copy&paste the content from above, be careful at tab characters, could be replaced by spaces and make utility does not like this.
For build, cd to the files directory, then use any of these, there is no difference:
When need the removal of the output files, use:
Connect the ST-Link v2
Before uploading the code into the board, you need to seat both jumpers towards the micro-USB connector, then connect the debugger to the Blue Pill like in the picture:
Caution! Do not use two USB connection at the same time (on debugger and on-board) because you risk to fry the board or PC’s USB ports. Disconnect one before plugging-in the other one.
For uploading the binary to the Blue Pill, respectively complete target erase, use either:
Let’s make the LED blinking
The Blue Pill has two LEDs, one for power and one driven by MCU, connected to port C, bit 13, as we can see in the schematic got from https://vcc-gnd.world.taobao.com/
Schematic and documentation
For making the LED to blink we need to configure GPIO port C13 and add some delay. We see in the schematic that LED turns on when GPIO pin is ‘0’ logic and turns off when ‘1’.
The real story is a bit longer, but I’ll tell you here the short version. To make a GPIO toggling in STM32, you need to work with two peripherals: RCC (reset and clock control) and GPIOx (general purpose input/output). The RCC is necessary because the GPIO has disabled clock by default.
In the STM32F10xxx reference manual at page 51 is the memory map, where we find out the GPIO Port C has the base address at 0x4001-1000 and the RCC is at 0x4002-1000.
First, we are going to activate the clock for GPIOC port. For that, we set bit IOPCEN in register RCC_APB2ENR (offset 0x18, bit 4).
Next, we need to configure the port C as push-pull output. We do this by writing into register GPIOC_CRH (offset 0x04), CNF = 0b00 (push-pull output) and MODE = 0b10 (low speed). Thus, for port C13 bits 23:20 are going to be set to 0x2.
So, at the beginning of main.c let’s add the registers and bits definitions: