Try Embedded

Black Pill: count KEY presses with an EXTI interrupt

Bare-metal C on the STM32F411: route the KEY button on PA0 to EXTI line 0, count presses in its interrupt and toggle the PC13 LED.

Input · Beginner

In a real product: Doorbell button with a press counter (Smart home): A button handled by an interrupt is never missed, even while the main loop is busy.

The Black Pill carries an STM32F411CE: an Arm Cortex-M4F with 512 KB of flash and 128 KB of RAM, on a small board with its own KEY button on PA0 and a blue LED on PC13. Here the button raises an interrupt through the EXTI controller, the handler counts the press and toggles the LED, and the main loop only prints the count when it changes.

What you'll learn

  • Giving a pin a pull-up with GPIOx->PUPDR, since the KEY button only connects PA0 to ground
  • Routing a pin to an EXTI line with SYSCFG->EXTICR, and choosing the edge with EXTI->FTSR
  • Enabling the interrupt in EXTI->IMR and the NVIC, and clearing its pending bit in the handler
  • Ignoring switch bounce by refusing presses that come too soon after the last

How it works

The KEY button joins PA0 to ground when pressed and leaves it floating otherwise, so PUPDR gives PA0 its internal pull-up: released reads 1, pressed reads 0. A press is therefore a falling edge.

Every pin numbered 0 (PA0, PB0, PC0...) can drive EXTI line 0; SYSCFG->EXTICR[0] picks which port. Port A is 0, the reset value, but writing it says what the code means. SYSCFG needs its own clock, SYSCFGEN in RCC->APB2ENR. EXTI->FTSR bit 0 asks for falling edges and EXTI->IMR bit 0 lets the line interrupt. NVIC_EnableIRQ(EXTI0_IRQn) lets the core take it.

In EXTI0_IRQHandler, writing 1 to bit 0 of EXTI->PR clears the pending bit: without that the handler would run again as soon as it returned. A real button bounces for a few milliseconds, making several edges for one press, so a press less than 50 ms after the last one is ignored.

The handler only counts. Printing is slow, so the main loop does it when the count changes: a handler should finish quickly and leave the slow work to the code it interrupts.

Try this

  • Press KEY on the drawing a few times and watch the count and the LED.
  • Remove the PUPDR line: PA0 floats, and presses are no longer seen reliably.
  • Change FTSR to RTSR: the count now goes up when the button is released.

main.c

// Bare-metal C on the Black Pill (STM32F411CE): no Arduino core and no HAL. CMSIS names every register.
// Count presses of the KEY button (PA0) in its EXTI interrupt and toggle the blue LED (PC13, lit low).
#include <stdio.h>
#include "stm32f4xx.h"

static volatile uint32_t ticks;     // milliseconds since SysTick started
static volatile uint32_t presses;
static volatile uint32_t last_press;

void SysTick_Handler(void) {
  ticks++;
}

// A falling edge on PA0: KEY pressed.
void EXTI0_IRQHandler(void) {
  EXTI->PR = EXTI_PR_PR0;                 // clear the pending bit, or the handler runs again at once
  if (ticks - last_press < 50) return;    // a bounce of the last press
  last_press = ticks;
  presses++;
  GPIOC->ODR ^= GPIO_ODR_OD13;
}

// 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->AHB1ENR |= RCC_AHB1ENR_GPIOAEN | RCC_AHB1ENR_GPIOCEN;
  RCC->APB2ENR |= RCC_APB2ENR_USART1EN | RCC_APB2ENR_SYSCFGEN;

  GPIOC->MODER = (GPIOC->MODER & ~GPIO_MODER_MODER13) | GPIO_MODER_MODER13_0;   // PC13: output
  GPIOC->BSRR = GPIO_BSRR_BS13;                                                // LED off (lit low)
  GPIOA->MODER &= ~GPIO_MODER_MODER0;                                          // PA0: input
  GPIOA->PUPDR = (GPIOA->PUPDR & ~GPIO_PUPDR_PUPD0) | GPIO_PUPDR_PUPD0_0;      // with a pull-up
  GPIOA->MODER = (GPIOA->MODER & ~GPIO_MODER_MODER9) | GPIO_MODER_MODER9_1;    // PA9: USART1 TX
  GPIOA->AFR[1] = (GPIOA->AFR[1] & ~(0xFu << 4)) | (7u << 4);                  // AF7
  USART1->BRR = SystemCoreClock / 115200;                                      // 16 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;

  SYSCFG->EXTICR[0] = (SYSCFG->EXTICR[0] & ~SYSCFG_EXTICR1_EXTI0) | SYSCFG_EXTICR1_EXTI0_PA;   // EXTI0 from PA0
  EXTI->FTSR |= EXTI_FTSR_TR0;                  // falling edges
  EXTI->IMR |= EXTI_IMR_MR0;
  NVIC_EnableIRQ(EXTI0_IRQn);

  printf("Press KEY\r\n");
  uint32_t shown = 0;
  while (1) {
    if (presses != shown) {
      shown = presses;
      printf("presses: %lu\r\n", (unsigned long)shown);
    }
  }
}

Lessons behind it