Nucleo-F401RE: move the vector table to RAM
Read the vector table the chip boots from, copy it into RAM, point VTOR at the copy, and swap the SysTick handler while the program runs.
Firmware · Advanced
In a real product: Bootloader handing over to an application (Industrial): A bootloader moves the vector table so the application's own interrupt handlers take over.
When a Cortex-M comes out of reset it does two things before running a single instruction: it loads the stack pointer from address 0 of the vector table and jumps to the address in word 1, the reset handler. Every exception and interrupt later finds its handler in that same table, at the address in VTOR.
This program prints the first entries of the table, copies the whole table into RAM, points VTOR at the copy, and then swaps the SysTick entry between two handlers every two seconds. LD2 blinks slowly with one and quickly with the other, and no code ever calls either of them.
What you'll learn
- Why VTOR is 0 after reset, and how the boot pins map flash to address 0
- What the vector table holds: the initial stack pointer, the reset handler, 14 more system exceptions, then one entry per interrupt (85 on the STM32F401)
- Why handler addresses are odd: bit 0 set means Thumb code, the only kind a Cortex-M runs
- VTOR's alignment rule: the table must start on a power of two at least as big as itself, so 512 bytes here
- How bootloaders use this, and why the
__DSB()after writing VTOR matters
How it works
The table is at the start of flash, 0x08000000. Yet VTOR reads 0 after reset: booting from main flash (BOOT0 low), the chip also shows the flash at address 0, so the core finds the same table there. The startup file (startup_stm32f401xe.s) builds it from the handler names. Every handler you don't write is a weak alias of Default_Handler, an endless loop. That is why EXTI0's and EXTI1's entries print the same address.
The linker script places the table first: its .isr_vector section comes before .text. It also defines _estack, the top of RAM, which is word 0.
The copy is an ordinary uint32_t array of 101 words in RAM. The F401's table needs 101 × 4 = 404 bytes, and the next power of two is 512, so the array is declared aligned(512). After memcpy, writing its address to SCB->VTOR makes the core read handlers from RAM. The __DSB() makes sure the write has finished before the next interrupt can be taken.
Now entry 15, SysTick, is just a word in RAM. Writing another function's address there changes which code runs on the next tick. A bootloader does the same thing on a larger scale: before it jumps to the application, it sets VTOR to the application's own table.
Try this
- Remove
aligned(512)and print(uint32_t)ram_vectors & 511: if it isn't 0, VTOR silently ignores the low bits. - Point entry 15 at
Default_Handler's address (EXTI0's entry): LD2 stops changing and the program hangs in the endless loop on the first tick. - Print
SCB->ICSR & 0x1FFinside a handler: it is the number of the exception being handled (15 for SysTick).
main.c
// Bare-metal C on the Nucleo-F401RE: no Arduino core and no HAL. CMSIS names every register.
// Print the vector table the chip booted from, copy it to RAM, set VTOR to the copy, and swap the SysTick handler
// in it every 2 s: LD2 (PA5) blinks slowly with one handler and fast with the other.
#include <stdio.h>
#include <string.h>
#include "stm32f4xx.h"
#define VECTORS (16 + 85) // 16 system exceptions, 85 interrupts on the F401
static uint32_t ram_vectors[VECTORS] __attribute__((aligned(512)));
static volatile uint32_t ms;
void SysTick_Handler(void) { // in the flash table: toggle LD2 every 500 ms
if (++ms % 500 == 0) GPIOA->ODR ^= 1u << 5;
}
static void fast_tick(void) { // installed later, in the RAM table only: every 100 ms
if (++ms % 100 == 0) GPIOA->ODR ^= 1u << 5;
}
int __io_putchar(int ch) {
while (!(USART2->SR & USART_SR_TXE)) {}
USART2->DR = ch;
return ch;
}
int main(void) {
RCC->AHB1ENR |= RCC_AHB1ENR_GPIOAEN;
RCC->APB1ENR |= RCC_APB1ENR_USART2EN;
GPIOA->MODER = (GPIOA->MODER & ~(GPIO_MODER_MODER5 | GPIO_MODER_MODER2)) | GPIO_MODER_MODER5_0 | GPIO_MODER_MODER2_1;
GPIOA->AFR[0] = (GPIOA->AFR[0] & ~(0xFu << 8)) | (7u << 8); // PA2: USART2 TX
USART2->BRR = SystemCoreClock / 115200;
USART2->CR1 = USART_CR1_TE | USART_CR1_UE;
const uint32_t *flash_vectors = (const uint32_t *)SCB->VTOR;
printf("VTOR 0x%08lx\r\n", (unsigned long)SCB->VTOR);
printf("[0] stack top 0x%08lx\r\n", (unsigned long)flash_vectors[0]);
printf("[1] reset 0x%08lx (bit 0 set: Thumb)\r\n", (unsigned long)flash_vectors[1]);
printf("[15] SysTick 0x%08lx SysTick_Handler is at 0x%08lx\r\n", (unsigned long)flash_vectors[15],
(unsigned long)(uint32_t)SysTick_Handler);
printf("[22] EXTI0 0x%08lx\r\n", (unsigned long)flash_vectors[16 + EXTI0_IRQn]);
printf("[23] EXTI1 0x%08lx (the same: Default_Handler)\r\n", (unsigned long)flash_vectors[16 + EXTI1_IRQn]);
memcpy(ram_vectors, flash_vectors, sizeof ram_vectors);
__disable_irq();
SCB->VTOR = (uint32_t)ram_vectors;
__DSB(); // the write is done before any exception is taken
__enable_irq();
printf("VTOR now 0x%08lx (RAM)\r\n", (unsigned long)SCB->VTOR);
SysTick_Config(SystemCoreClock / 1000);
int fast = 0;
uint32_t next = 2000;
for (;;) {
if ((int32_t)(ms - next) < 0) continue;
next += 2000;
fast = !fast;
ram_vectors[15] = (uint32_t)(fast ? fast_tick : SysTick_Handler); // the next tick runs the new handler
printf("t=%lu ms: SysTick -> %s (0x%08lx)\r\n", (unsigned long)ms, fast ? "fast_tick" : "SysTick_Handler",
(unsigned long)ram_vectors[15]);
}
}