======Programming the SysTick Timer====== Check out [[arm_cortex-m3:stm32:basics#systick_timer|SysTick timer Basics]] and [[arm_cortex-m3:stm32:c-runtime|A simple C run-time framework for the STM32 processor]] for background information. Our objective is to program the SysTick timer at a rate of 1Hz and write a SysTick_ISR which will toggle the RED LED on the STM32 Primer. Here is the //main// program: #include "regs.h" extern void systick_configure(void); volatile unsigned int c = 1; void systick_isr(void) { if(c == 1) { GPIOB_BSRR = (1 << 9); c = 0; } else { GPIOB_BSRR = (1 << 25); c = 1; } } main() { RCC_APB2ENR = (1 << 3); GPIOB_CRH = 0x44444414; systick_configure(); while(1); } //systick_configure// is part of the startup code: #define STACK_TOP 0x20005000 #include "regs.h" void c_startup(void); void dummy_fn(void); void systick_isr(void); extern unsigned long _etext, _sdata, _edata, _sbss, _ebss; __attribute__((section(".isr_vector"))) void (*vectors[])(void) = { STACK_TOP, c_startup, dummy_fn, dummy_fn, dummy_fn, dummy_fn, dummy_fn, dummy_fn, dummy_fn, dummy_fn, dummy_fn, dummy_fn, dummy_fn, dummy_fn, dummy_fn, systick_isr }; void dummy_fn(void) { while(1); } void c_startup(void) { unsigned long *src, *dst; src = &_etext; dst = &_sdata; while(dst < &_edata) *(dst++) = *(src++); src = &_sbss; while(src < &_ebss) *(src++) = 0; main(); } void systick_configure(void) { /* Select AHB Clock (HCLK) as SysTick clock source */ SYSTICK_CTRL = 0x4; SYSTICK_LOAD = 8000000; /* Frequency of 1 Hz */ SYSTICK_CTRL |= 1; /* Enable Counter */ SYSTICK_CTRL |= 2; /* Enabler interrupts */ } Here is the file which contains the register definitions: #ifndef __REGS_H #define __REGS_H #define REG_UL(x) (*((volatile unsigned long*)(x))) #define GPIOB_CRH REG_UL(0x40010c00 + 0x4) #define GPIOB_BSRR REG_UL(0x40010c00 + 0x10) #define RCC_APB2ENR REG_UL(0x40021000 + 0x018) #define SYSTICK_CTRL REG_UL(0xe000e010) #define SYSTICK_LOAD REG_UL(0xe000e010 + 0x4) #define SYSTICK_VAL REG_UL(0xe000e010 + 0x8) #endif Note that the new .isr_vector section must be included in the binary file to be written to Flash. An example compilation sequence, where the main program is in "main.c", the startup code is contained in "startup.c", and the linker script is in "standalone.ld": arm-none-eabi-gcc -mcpu=cortex-m3 -mthumb -c main.c arm-none-eabi-gcc -mcpu=cortex-m3 -mthumb -c startup.c arm-none-eabi-ld -T standalone.ld main.o startup.o arm-none-eabi-objcopy a.out main.bin -O binary