Try Embedded

Nucleo-F767ZI: chase the three user LEDs from a timer interrupt

Bare-metal C on the STM32F767 (Cortex-M7): step LD1, LD2 and LD3 in turn from TIM6's update interrupt while the core sleeps in WFI.

Output · Beginner

In a real product: Running status lights on an industrial controller (Industrial): A timer interrupt keeps the lights moving at an exact rate while the core sleeps between steps.

The Nucleo-F767ZI carries an STM32F767ZI: an Arm Cortex-M7 with a double-precision FPU, 2 MB of flash and 512 KB of RAM, on a Nucleo-144 board with three user LEDs: LD1 green on PB0, LD2 blue on PB7 and LD3 red on PB14. TIM6, a basic timer, interrupts five times a second and its handler lights the next LED. Between interrupts the core sleeps.

What you'll learn

  • Setting a basic timer's period with its prescaler (PSC) and auto-reload register (ARR)
  • Loading the prescaler at once with an update event (EGR UG) and clearing the flag it leaves
  • Clearing the update flag in the handler, and sleeping with __WFI() until the next interrupt

How it works

After reset the F767 runs from its 16 MHz HSI. TIM6 counts that clock divided by PSC + 1: with PSC = 15999 it counts at 1 kHz, and with ARR = 199 it wraps every 200 counts, 200 ms, raising an update event each time.

PSC is buffered: a new value takes effect at the next update. Writing UG to EGR makes one now, so the first period is right too; it also sets UIF, so SR is cleared before the interrupt is enabled, or the handler would run at once. DIER UIE lets updates interrupt, and TIM6 shares its NVIC line with the DAC (TIM6_DAC_IRQn).

TIM6_DAC_IRQHandler clears UIF first, then turns the current LED off and the next one on with one BSRR write. The main loop has nothing to do, so it calls __WFI(): the core stops until an interrupt arrives.

Try this

  • Change ARR to 49: the LEDs chase four times as fast.
  • Make the chase bounce back and forth: LD1, LD2, LD3, LD2, LD1...
  • Remove the TIM6->SR = 0 line in the handler: it runs again as soon as it returns and the LEDs flicker.

main.c

// Bare-metal C on the Nucleo-F767ZI: no Arduino core and no HAL. CMSIS names every register.
// TIM6 interrupts every 200 ms; its handler lights LD1 (PB0), LD2 (PB7) and LD3 (PB14) in turn.
#include <stdio.h>
#include "stm32f7xx.h"

static const uint32_t leds[] = { GPIO_ODR_OD0, GPIO_ODR_OD7, GPIO_ODR_OD14 };
static volatile unsigned current;

void TIM6_DAC_IRQHandler(void) {
  TIM6->SR = 0;                                      // clear UIF first
  GPIOB->BSRR = leds[current] << 16;                 // this LED off
  current = (current + 1) % 3;
  GPIOB->BSRR = leds[current];                       // the next one on
}

// printf sends each character here: USART3 on PD8, which the ST-LINK carries to the Serial Monitor.
int __io_putchar(int ch) {
  while (!(USART3->ISR & USART_ISR_TXE)) {}
  USART3->TDR = ch;
  return ch;
}

int main(void) {
  RCC->AHB1ENR |= RCC_AHB1ENR_GPIOBEN | RCC_AHB1ENR_GPIODEN;
  RCC->APB1ENR |= RCC_APB1ENR_TIM6EN | RCC_APB1ENR_USART3EN;

  // PB0, PB7 and PB14: outputs.
  GPIOB->MODER = (GPIOB->MODER & ~(GPIO_MODER_MODER0 | GPIO_MODER_MODER7 | GPIO_MODER_MODER14))
               | GPIO_MODER_MODER0_0 | GPIO_MODER_MODER7_0 | GPIO_MODER_MODER14_0;
  GPIOB->BSRR = leds[0];                             // start with LD1
  GPIOD->MODER = (GPIOD->MODER & ~GPIO_MODER_MODER8) | GPIO_MODER_MODER8_1;   // PD8: USART3 TX
  GPIOD->AFR[1] = (GPIOD->AFR[1] & ~0xFu) | 7u;                              // AF7
  USART3->BRR = SystemCoreClock / 115200;                                    // 16 MHz HSI after reset
  USART3->CR1 = USART_CR1_TE | USART_CR1_UE;

  TIM6->PSC = 16000 - 1;     // 16 MHz / 16000: 1 kHz
  TIM6->ARR = 200 - 1;       // 200 counts: 200 ms
  TIM6->EGR = TIM_EGR_UG;    // load PSC now
  TIM6->SR = 0;              // and clear the UIF that set
  TIM6->DIER = TIM_DIER_UIE;
  NVIC_EnableIRQ(TIM6_DAC_IRQn);
  TIM6->CR1 = TIM_CR1_CEN;

  printf("Chasing LD1, LD2 and LD3\r\n");
  while (1) {
    __WFI();                 // sleep until the next interrupt
  }
}

Lessons behind it