How to Create a SysTick Timer Interrupt Driver with an STM32F446 Microcontroller Board in C

Nucleo STM32F446 board

In this article, we explain how to create a SysTick timer interrupt driver with an STM32F446 microcontroller board in C.

With a SysTick timer interrupt driver, we can have a timer running that can execute some specific task at some specific interval we determine.

In our program, we will have a SysTick timer interrupt driver which will have a 1-second delay and toggle an LED every second.

Previously, we created a program on, How to Create a Delay using the SysTick Timer with an STM32F446 Board in C. If you need to, please familiarize yourself with how to create a delay with a SysTick timer first before using interrupts, because we will not go over that code as extensively; we will simply be going over how to turn that code into it being done with an interrupt driver.

So we take this program and modify it a bit by enabling the UIE interrupt bit in the TIMx DMA/Interrupt enable register and then enabling the interrupt for UART on the processor side, meaning for the Cortex-M4 processor.

We do both of these tasks within the the code to initialize the timer 2.

The next thing we need is an interrupt service routine for the timer. This is the function our code will execute when an interrupt does occur. In this case, we will toggle an LED on and off at a 1-second interval. It's simply to demonstrate how we can run code through an interrupt service routine.

We need 3 major code files in order for this code to work.

We need our header files, which contains many macros and structures that describe the various registers and needed values.

We then need our main C file, which contains the code of the timer interrupt driver.

Below is the systick.h header file, which contains many macros and registers for the SysTick timer.

Below is the header file which in this case is named, stm32f407xx.h

So this header file contains all the definitions we need to create our main C file.

The contents of the main.c file is shown below.

We will now go over the code.

So we place the prototypes of our functions into our code.

We then have our main() function.

We turn on the peripheral clock for GPIO Port A. We then set pin PA5 as output, which on the STM32F446 board is connected to an onboard green LED.

We then initialize the SysTick timer with the systick_1hz_interrupt() function.

Our infinite while loop is empty.

We then have our systick_1hz_interrupt() function.

In this function, we set the reload value to 16000000, which makes the timer have a 1-second delay.

We clear the SysTick current value register, so that any residual number in it is cleared.

We then enable the SysTick timer in the control register and select the internal system clock source.

Lastly, we enable the systick interrupt in the control register by setting the TICKINT bit, bit 1, to HIGH, which enables the SysTick exception request.

The SysTick control and status register (SYS_CSR) is shown below.

SysTick Control and Status Register (SYST_CSR) in a Cortex-M4 processor

We then go to our SysTick_Handler() interrupt service routine function. In this function, we simply toggle the LED at pin PA5.

The SysTick timer interrupt is one of the easiest to implement, as no IRQ number is needed.

The program will now toggle an LED on and off every 1 second.

And this is how we can code a SysTick timer interrupt driver with an STM32F446 board in C.

Related Resources

HTML Comment Box is loading comments...