Nucleo-WBA55CG: a stopwatch on a 32-bit timer, with an EXTI button
Bare-metal C on the STM32WBA55 (Cortex-M33): raise the clock to 100 MHz on a PLL with a fractional divider, time with a 32-bit timer, and take B1 on its own…
Firmware · Intermediate
In a real product: Lap timer for a wireless sports sensor (Consumer electronics): A free-running timer gives each press an exact time, without the CPU counting anything.
The STM32WBA is a wireless chip, but under the radio it is an ordinary Cortex-M33 with an unusual clock tree. This project takes it from its 16 MHz start-up clock to 100 MHz, lets a 32-bit timer run at 1 MHz, and prints the time at each press of B1. The LED lights while the stopwatch counts. The radio itself is not simulated, and nothing here uses it.
What you'll learn
- Raising the clock safely: voltage range first, flash wait states next, the system clock last
- A PLL with a fractional multiplier: how a 16 MHz input gives exactly 100 MHz
- Why a 32-bit timer needs no overflow handling for over an hour
- EXTI on this generation: the port choice in
EXTI->EXTICR, separate rising and falling pending registers - An LED wired to light when its pin is low
How it works
Order matters when speeding up. At 16 MHz the chip runs in voltage range 2, which only goes up to 16 MHz, so PWR->VOSR asks for range 1 first and waits for VOSRDY. Then FLASH->ACR gets three wait states, since flash cannot keep up with a 100 MHz CPU without them. Only then does RCC->CFGR1 switch the system clock to the PLL.
The 32 MHz crystal goes through HSEPRE to give 16 MHz. PLL1 divides that by M and multiplies by N plus a fraction: 16 MHz times 12.5 is 200 MHz, and the R output divides by 2. PLL1FRACN holds the fraction in 8192ths, so 4096 is one half. The fraction only takes effect with PLL1FRACEN set.
TIM2 is 32 bits wide on this chip. With a prescaler of 100 it counts microseconds, and it would take over 70 minutes to wrap.
B1 is on PC13, so EXTI line 13. EXTI->EXTICR[3] picks port C for that line: each register holds four lines, one byte each. This generation has its own EXTI13_IRQn for the line, and keeps falling edges in FPR1, so that is the flag to clear.
Try this
- Print the time in seconds and milliseconds.
- Leave out
FLASH->ACRand read the warning about wait states. - Change
PLL1FRACRto 0, keepPLL1FRACENon, and see whatSystemCoreClocksays. - Take away the debounce check and press quickly: the times may repeat on real hardware.
- Use
TIM2->CCR1in input-capture mode to catch the press in hardware, with no interrupt delay.
main.c
// Bare-metal C on the Nucleo-WBA55CG: no Arduino core and no HAL. CMSIS names every register.
// 100 MHz through PLL1's fraction, a 32-bit timer at 1 MHz, and B1 on EXTI line 13.
#include <stdio.h>
#include "stm32wbaxx.h"
static volatile uint32_t pressed_at;
static volatile int pressed;
void EXTI13_IRQHandler(void) {
EXTI->FPR1 = 1u << 13; // B1 pulls PC13 low: a falling edge
pressed_at = TIM2->CNT;
pressed = 1;
}
// printf sends each character here: USART1 on PB12, which the ST-LINK carries to the Serial Monitor.
int __io_putchar(int ch) {
while (!(USART1->ISR & USART_ISR_TXE)) {}
USART1->TDR = ch;
return ch;
}
static void clock_100mhz(void) {
// Voltage first, then wait states, then the clock.
RCC->AHB4ENR |= RCC_AHB4ENR_PWREN;
PWR->VOSR |= PWR_VOSR_VOS;
while (!(PWR->VOSR & PWR_VOSR_VOSRDY)) {}
FLASH->ACR = (FLASH->ACR & ~FLASH_ACR_LATENCY) | 3u; // three wait states
while ((FLASH->ACR & FLASH_ACR_LATENCY) != 3u) {}
// The 32 MHz crystal over 2 is 16 MHz; times 12.5 is 200 MHz; R divides by 2.
RCC->CR |= RCC_CR_HSEON | RCC_CR_HSEPRE;
while (!(RCC->CR & RCC_CR_HSERDY)) {}
RCC->PLL1CFGR = RCC_PLL1CFGR_PLL1SRC | RCC_PLL1CFGR_PLL1FRACEN | RCC_PLL1CFGR_PLL1REN | (1u << RCC_PLL1CFGR_PLL1RGE_Pos);
RCC->PLL1DIVR = (11u << RCC_PLL1DIVR_PLL1N_Pos) | (1u << RCC_PLL1DIVR_PLL1R_Pos); // N = 12, R = 2
RCC->PLL1FRACR = 4096u << RCC_PLL1FRACR_PLL1FRACN_Pos; // + 4096/8192
RCC->CR |= RCC_CR_PLL1ON;
while (!(RCC->CR & RCC_CR_PLL1RDY)) {}
RCC->CFGR1 |= RCC_CFGR1_SW;
while ((RCC->CFGR1 & RCC_CFGR1_SWS) != RCC_CFGR1_SWS) {}
SystemCoreClockUpdate();
}
int main(void) {
clock_100mhz();
RCC->AHB2ENR |= RCC_AHB2ENR_GPIOBEN | RCC_AHB2ENR_GPIOCEN;
RCC->APB2ENR |= RCC_APB2ENR_USART1EN;
RCC->APB1ENR1 |= RCC_APB1ENR1_TIM2EN;
GPIOB->MODER = (GPIOB->MODER & ~GPIO_MODER_MODE12) | GPIO_MODER_MODE12_1;
GPIOB->AFR[1] = (GPIOB->AFR[1] & ~(0xFu << 16)) | (7u << 16); // PB12: AF7, USART1 TX
USART1->BRR = SystemCoreClock / 115200;
USART1->CR1 = USART_CR1_TE | USART_CR1_UE;
GPIOB->BSRR = GPIO_BSRR_BR4; // LD1 lights with its pin low
GPIOB->MODER = (GPIOB->MODER & ~GPIO_MODER_MODE4) | GPIO_MODER_MODE4_0;
// PC13 is an input with the chip's pull-up, as the board has none; line 13 listens to port C, on a falling edge.
GPIOC->MODER &= ~GPIO_MODER_MODE13;
GPIOC->PUPDR = (GPIOC->PUPDR & ~GPIO_PUPDR_PUPD13) | GPIO_PUPDR_PUPD13_0;
EXTI->EXTICR[3] = (EXTI->EXTICR[3] & ~EXTI_EXTICR4_EXTI13) | (2u << EXTI_EXTICR4_EXTI13_Pos);
EXTI->FTSR1 |= 1u << 13;
EXTI->IMR1 |= 1u << 13;
NVIC_EnableIRQ(EXTI13_IRQn);
TIM2->PSC = SystemCoreClock / 1000000 - 1; // counts microseconds
TIM2->EGR = TIM_EGR_UG;
TIM2->CR1 = TIM_CR1_CEN;
printf("running at %lu MHz: press B1\r\n", (unsigned long)(SystemCoreClock / 1000000));
uint32_t last = 0;
while (1) {
__WFI();
if (!pressed) continue;
pressed = 0;
if (pressed_at - last < 50000) continue; // a bounce: under 50 ms
printf("lap at %lu us, %lu us since the last\r\n", (unsigned long)pressed_at, (unsigned long)(pressed_at - last));
last = pressed_at;
}
}