🛠 Firmware libre sur Blue Pill avec libopencm3 et OpenOCD

STM32F103 "Blue Pill" (STM32F103C8T6) · avance · Compiler le miniblink adapté à la LED PC13 avec la bibliothèque open-source écrite d'après les datasheets, et flasher via OpenOCD/stlink.

Why libopencm3?

libopencm3 is an open-source GCC library for Cortex-M "written completely from scratch from the manufacturer's datasheets" — with no ST code, under the LGPL3 license. The STM32 F1xx family is supported, and the project documents flashing via OpenOCD. It is the ideal option for understanding what the HAL really does, with a lightweight API (rcc, gpio, usart, spi, i2c, adc, can…) and canonical examples: the libopencm3-examples repository contains stm32-maple targets (miniblink, USB CDC-ACM, SPI/I2C demos) that correspond to Blue Pill-class boards.

The miniblink, PC13 version

Adapted from the stm32-maple/miniblink example, taking into account the LED in sink mode (HIGH = off) and the low drive capability of PC13:

#include <libopencm3/stm32/rcc.h>
#include <libopencm3/stm32/gpio.h>

static void gpio_setup(void)
{
    /* Active l'horloge du périphérique GPIOC (RCC_APB2ENR |= IOPCEN). */
    rcc_periph_clock_enable(RCC_GPIOC);

    /* PC13 en sortie push-pull 2 MHz (broche à faible drive). */
    gpio_set_mode(GPIOC, GPIO_MODE_OUTPUT_2_MHZ,
                  GPIO_CNF_OUTPUT_PUSHPULL, GPIO13);
}

int main(void)
{
    int i;
    gpio_setup;
    while (1) {
        gpio_set(GPIOC, GPIO13);      /* HIGH -> LED ÉTEINTE (sink) */
        for (i = 0; i < 800000; i++)
            __asm__("nop");
        gpio_clear(GPIOC, GPIO13);    /* LOW -> LED ALLUMÉE */
        for (i = 0; i < 800000; i++)
            __asm__("nop");
    }
}

Two hardware lessons are encapsulated in this file: you must enable the APB2 clock for the GPIOC port before any access (otherwise writes have no effect), and PC13 must remain a slow control pin — hence the 2 MHz mode.

Compilation chain and flashing

The PlatformIO board definition for genericSTM32F103C8 also lists the frameworks arduino, cmsis, libopencm3, stm32cube and the upload protocols jlink, cmsis-dap, stlink, blackmagic, serial, dfu — proof that this board lends itself to all levels of abstraction.

Other tutorials for this board

Code examples for this board

← Back to the board 🛠 All tutorials