STM32 Arm Programming for Embedded Systems by Mazidi Muhammad Ali & Chen Shujen & Ghaemi Eshragh

STM32 Arm Programming for Embedded Systems by Mazidi Muhammad Ali & Chen Shujen & Ghaemi Eshragh

Author:Mazidi, Muhammad Ali & Chen, Shujen & Ghaemi, Eshragh
Language: eng
Format: epub
Publisher: UNKNOWN
Published: 2019-08-11T16:00:00+00:00


Section 6.5: SysTick Programming and Interrupt

Another useful interrupt in Arm is the SysTick. The SysTick timer was discussed in Chapter 5. Next, you learn how to use the SysTick interrupt. See figure below.

Figure 6-15: SysTick Internal Structure

If TICKINT=1, when the COUNT flag is set, it generates an interrupt. TICKINT is D1 of the CSR register, as shown in Figure 6-16.

Figure 6-16: SysTick Control and Status Register (SYST_CSR) The SysTick interrupt can be used to initiate an action on a periodic basis. This action is performed at a fixed rate without external signal. For example, in a given application we can use SysTick to read a sensor every 200 msec. SysTick is used widely for an operating system so that the system software may interrupt the application software periodically (often at 10 ms interval) to monitor and control the system operations.

Using SysTick with Interrupt The Program 6-4 uses the SysTick to toggle an LED every second. We need the RELOAD value of 16,000,000-1. The CLK_SRC bit of CTRL register is set so the system clock is used as the clock source of SysTick. In the STM32F4xx Arm, the alternative clock for SysTick is connected to a prescaler that divides the system clock by 16. The system clock is running at 16 MHz. The COUNT flag is raised every 16,000,000 clocks and an interrupt occurs. Then the RELOAD register is loaded with 16,000,000-1 automatically.

The interrupt is enabled in SysTick Control and Status register. Examining interrupt vector table for Arm Cortex, we see that the SysTick is assigned to INT15. Because SysTick is an interrupt below INT16, the enable/disable and the priority are not managed by the NVIC. Its priority is controlled by a System Handler Priority register (SHPR) of System Control Block (SCB). Instead for finding out which register to use, we may use NVIC_SetPriority(SysTick_IRQn, prio) function defined in core_cm4.h to change the priority. Although the function name has NVIC in it, it modifies SHP registers for interrupts below 16. There is no need to clear interrupt flag in the interrupt handler for SysTick.

Program 6-4: SysTick interrupt /* p6_4.c Toggle the LED using the SysTick interrupt

*

* This program sets up the SysTick to interrupt at 1 Hz. * The system clock is running at 16 MHz.

* 1 sec/1 us = 16,000,000-1 for RELOAD register.

* In the interrupt handler, the LED is toggled.

*

* This program was tested with Keil uVision v5.24a with DFP v2.11.0 */

#include "stm32F4xx.h"

int main(void) {

__disable_irq(); /* global disable IRQs */ RCC->AHB1ENR |= 1; /* enable GPIOA clock */ GPIOA->MODER &= ~0x00000C00; /* clear pin mode */ GPIOA->MODER |= 0x00000400; /* set pin to output mode */

/* Configure SysTick */

SysTick->LOAD = 16000000-1; /* reload with number of clocks per second */ SysTick->VAL = 0;

SysTick->CTRL = 7; /* enable SysTick interrupt, use system clock */ __enable_irq();

void SysTick_Handler(void) {

while (1) { }

}

GPIOA->ODR ^= 0x00000020; /* turn on LED */ }



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.