Try Embedded

Blue Pill: blink with the F1's CRH register

Bare-metal C on the STM32F103: set pins up with the F1's four-bit CRL and CRH fields and blink the board's PC13 LED and one on PB12.

Basic · Beginner

In a real product: Status light on a low-cost controller (Consumer electronics): A blinking LED shows the firmware is alive.

The Blue Pill carries an STM32F103C8: an Arm Cortex-M3 with 64 KB of flash and 20 KB of RAM. It is the oldest STM32 family on the site, and its GPIO is set up differently from every later one: each pin has four bits in CRL (pins 0 to 7) or CRH (pins 8 to 15) instead of two in MODER. This program blinks the board's own LED on PC13 and an LED on PB12 in turn, with no Arduino core and no HAL.

What you'll learn

  • Turning on GPIO ports' clocks in RCC->APB2ENR: on the F1 the ports sit on APB2, not on an AHB bus
  • Setting a pin up with its four bits in CRL or CRH: two for the mode (input, or output at 2, 10 or 50 MHz) and two for the configuration (push-pull, open-drain, alternate function)
  • Driving an LED wired to 3.3 V (lit low), as the Blue Pill's PC13 LED is

How it works

After reset the F103 runs from its 8 MHz internal oscillator (HSI). main() sets IOPAEN, IOPBEN and IOPCEN in RCC->APB2ENR, so ports A, B and C get a clock, and USART1EN for the Serial Monitor.

PC13 is pin 13, so its four bits are bits 20 to 23 of GPIOC->CRH. The value 0010 means: output at up to 2 MHz (MODE = 10), push-pull (CNF = 00). PB12 takes the same value in bits 16 to 19 of GPIOB->CRH. PA9, USART1's TX, gets 1011: output at 50 MHz, alternate function push-pull, so the USART drives it rather than ODR.

BSRR sets a pin with its low 16 bits and clears one with its high 16 bits. The board's LED is wired from 3.3 V through a resistor to PC13, so it lights when PC13 is low: clearing PC13 turns it on.

SysTick interrupts every millisecond and delay_ms() waits on its count, as on every Cortex-M.

Try this

  • Open the register viewer and find bits 20 to 23 of GPIOC's CRH: they read 0010.
  • Change PB12's value to 0110 (open-drain): the LED on PB12 no longer lights, because an open-drain pin can only pull low. Wire the LED to 3.3 V instead of ground and it works again, lit low.
  • Compare with the Nucleo-F401RE blink example: the same job with MODER, OTYPER and OSPEEDR.

main.c

// Bare-metal C on the Blue Pill (STM32F103C8): no Arduino core and no HAL. CMSIS names every register.
// Blink the board's LED (PC13, lit low) and an LED on PB12 in turn, every half second.
#include <stdio.h>
#include "stm32f1xx.h"

static volatile uint32_t ticks;   // milliseconds since SysTick started

void SysTick_Handler(void) {
  ticks++;
}

static void delay_ms(uint32_t ms) {
  uint32_t start = ticks;
  while ((uint32_t)(ticks - start) < ms) {}
}

// printf sends each character here: USART1 on PA9.
int __io_putchar(int ch) {
  while (!(USART1->SR & USART_SR_TXE)) {}
  USART1->DR = ch;
  return ch;
}

int main(void) {
  RCC->APB2ENR |= RCC_APB2ENR_IOPAEN | RCC_APB2ENR_IOPBEN | RCC_APB2ENR_IOPCEN | RCC_APB2ENR_USART1EN;
  // Four bits a pin: MODE (bits 1:0) and CNF (bits 3:2).
  GPIOC->CRH = (GPIOC->CRH & ~(0xFu << 20)) | (0x2u << 20);   // PC13: output 2 MHz, push-pull
  GPIOB->CRH = (GPIOB->CRH & ~(0xFu << 16)) | (0x2u << 16);   // PB12: output 2 MHz, push-pull
  GPIOA->CRH = (GPIOA->CRH & ~(0xFu << 4)) | (0xBu << 4);     // PA9: output 50 MHz, alternate function (USART1 TX)
  USART1->BRR = SystemCoreClock / 115200;                     // 8 MHz HSI after reset
  USART1->CR1 = USART_CR1_TE | USART_CR1_UE;

  SysTick->LOAD = SystemCoreClock / 1000 - 1;   // 1 ms
  SysTick->VAL = 0;
  SysTick->CTRL = SysTick_CTRL_CLKSOURCE_Msk | SysTick_CTRL_TICKINT_Msk | SysTick_CTRL_ENABLE_Msk;

  printf("Blinking PC13 and PB12\r\n");
  while (1) {
    GPIOC->BSRR = GPIO_BSRR_BR13;   // PC13 low: the board's LED on
    GPIOB->BSRR = GPIO_BSRR_BR12;   // PB12 low: its LED off
    delay_ms(500);
    GPIOC->BSRR = GPIO_BSRR_BS13;   // PC13 high: the board's LED off
    GPIOB->BSRR = GPIO_BSRR_BS12;   // PB12 high: its LED on
    delay_ms(500);
  }
}

Lessons behind it