STM32F103C8 – Blinking LED

Abstract

In this tutorial i will describe how to use CubeMX and STM32CubeIDE to create a „Blinky Sketch“.

As there are allready gazillions of such tutorials availiable – e.g. on youtube, which describe how to make the LED of a Bluepill blink, the main purpose of this blog entry is simply to have an article, which describes the basic setup procedure in Cube MX, which I can refer to in other articles.

Introduction

I’m a fan of the STM32F103C8 Microprocessor. The reason why I use this MPUs is simple: they are the core of the „Bluepill“ prototyping board, which is available on ebay or ali-express for a very reasonable price.

While this prototyping board is most probably not the best choice for a professional application, the MPU on it is pretty neat and has a lot of hardware capabilities packed inside that little chip, which makes it almost perfect for playing around and testing. I guess, this is the reason, why the „Bluepill“ became so popular.

For a few Euros you get a MPU with a Cortex M3 core, GPIO, analog input, U(S)ART, USB, SPI, I2C, CAN, a Real Time Clock and 4 Timers

Goals

In this tutorial I am using CubeMX (inside the STM32CubeID) to configure:

  • The Debug Interface
  • The 8 MHz Crystal Resonator on the Bluepill Board
  • the MPU Main clock
  • the “LED1” GPIO
  • and the general project settings

Hardware

Bluepill "Blinky Sketch" - Hardware
Bluepill "Blinky Sketch" - Hardware

I am using a Bluepill on a breadboard, together with an ST-Link V2 clone. The Datasheet of the MPU can be found here.

Create a new Project

To start with a fresh new project in STM32CubeID click on File >> New >> STM32 Project. The target selector will open and will let you choose your MPU:

Bluepill "Blinky Sketch" - Select MPU
Bluepill "Blinky Sketch" - Select MPU

Type „F103C8“ into the Part Number Filter Control, which leaves 1 entry in the MPU List (STM32F103C8T6). Click on that element ni the list and selct „Next >“ on the bottom of the page.

CubeMX will ask you for a project name and for general project settings. In this case we leave all options as is (Target-Language = C) and klick „Finish“.

CubeMX: Hardware configuration

In the next step of the project creation process we have to configure the hardware. For this purpose, CubeMX provides us a GUI where we can configure all hardware related aspects of the MPU, without reading 1000 pages of the hardware reference manual in detail and spending a significant amount of our lifetime in shifting bits into registers.

Bluepill "Blinky Sketch" - RCC Setup: High Speed Clock
Bluepill "Blinky Sketch" - RCC Setup: High Speed Clock

Step 1: enable the High Speed Clock. As the Bluepill has an 8 MHz crystal on board, it’s a good idea to use it.Simply select „Crystal/Ceramic Resonator“ in the Drop Down Menu. This also allows you to set the max. MPU Clock Speed to 72 MHz in the clock setup. Without a Crystal or Ceramic Resonator the max. MPU speed is 64 MHz. We don’t necessarily need a 72 MHz MPU speed for this very simple example, but – as mentioned above – we will need it in other projects.

Step 2: enable „SWD“ (Serial Wire Debug) in the SYS section, by selecting „Serial Wire“ from the Drop Down Menu labeled „Debug“.

Bluepill "Blinky Sketch" - Debug Interface Setup
Bluepill "Blinky Sketch" - Debug Interface Setup

Step 3: add an Output on Pin PC13 by right clicking on the Pin in the MPU Pinout overview on the right side, selct „GPIO Output“.

Bluepill "Blinky Sketch" - GPIO Overview
Bluepill "Blinky Sketch" - GPIO Overview

Select the entry for PC13 in the GPIO overvie to edit the details of this Digital IO:

Bluepill "Blinky Sketch" - LED1 GPIO Setup
Bluepill "Blinky Sketch" - LED1 GPIO Setup

The default value for GPIO Mode is „Push-Pull“. In this case we can set it to „Open Drain“, as the GPIO is on the low side (Cathode) of the LED. Don’t forget to enter the User Label for the LED, either by entering the name in the GPIO Configuration, or by right clicking on the Pin in the Pinout view and selcting „Enter User Label.

When you followed all these steps correctly, out Pinout view should look like this:

Bluepill "Blinky Sketch" - MPU Pinout
Bluepill "Blinky Sketch" - MPU Pinout

Clock Configuration

The Clocks are configured in the Tab „Clock Configuration“. The easiest way to get this job done is: enter the desired Main Clock Speed in the „HCLK (Mhz)“ field and let CubeMX do the configuration and resolve possible issues.

MPU Clock Configuration in CubeMX
MPU Clock Configuration in CubeMX

Project Settings

Bluepill "Blinky Sketch" - general Project Properties
Bluepill "Blinky Sketch" - general Project Properties

Last but not least, switch to the „Project Manager“ tab, go to the section „Code Generatur“ and activate the options „Generate peripheral initialization …“ and „Set all free pins …“ to keep the main.c more readable and minimize the power consumption of the Processor.

Now the Hardware configuration is finished. You can close the CubeMX Tab, a Dialog will pop up asking, if you want to generate code, click „yes“ and the new project is configured and created.

Generate Code Dialog
Generate Code Dialog

Write the Code

The last step is to write the code, that makes the LED blink:

Open the main.c file, scroll down to the infinite loop and between the lines while(1) { … and /* USER CODE END WHILE*/ type:

HAL_GPIO_TogglePin(LED1_GPIO_Port, LED1_Pin);
HAL_Delay(500);

That’s it! Compile the progamm, download it to your bluepill and see the LED blink …