Joe Ferner Blog     About     Archive     Feed

Getting to Blink on a STM32 Nucleo F103RB using STM32CubeMX, Make and GCC



Start by creating a directory and copy my Makefile into it.

Create a file called in the root of your new directory with the following content:

SRCS = src/main.c

This file tells the Makefile where your source files are.


Create a new project in STM32CubeMX that matches your Nucleo board.

New Project

Create a more friendly name for your LED pin. Right click PA5 (LD2 [Green Led]), select Enter User Label, and enter MYLED.

Pin Name

Save the STM32CubeMX project into your directory.


Create a file called src/main.c with the following content:

#include <pinout.h> // generated by Makefile to build/pinout.h

volatile GPIO_PinState lastState;

void sleep_ms(uint32_t ms);
void sleep_us(uint32_t us);

void setup() {
  lastState = GPIO_PIN_RESET;

void loop() {
  GPIO_PinState newState = lastState == GPIO_PIN_RESET ? GPIO_PIN_SET : GPIO_PIN_RESET;
  lastState = newState;

void sleep_ms(uint32_t ms) {
  volatile uint32_t i;
  for (i = ms; i != 0; i--) {

void sleep_us(uint32_t us) {
  volatile uint32_t i;
  for (i = ((SystemCoreClock / 8000000) * us); i != 0; i--) {}

Similar to an Arduino there is a setup function which gets called when the program starts. There is also a loop function which gets continuouly called until the power is turned off.

Flash your Nucleo

After you plug your Nucleo board in, run make write. You should now see the green LED flash.

Additional Help

To get additional help run make help.

You can find the example project and Makefile here.