

LED configuration will be taken from last tutorial, if you are not familiar how to configure GPIO and AHB you can check it there. Let’s create an application which will blink LEDs with 1 second period. When processor is halted – for debugging purposed clock is stopped.Processor Clock (AHB) – 4MHz in this case as AHB prescaler is 1.Otherwise it will go to Default Handler – infinitive loop.In case it is configured with exception request user needs to create a IRQ handler to handle it.In case it is configured without exception request user needs to poll countflag to see when it reaches 1.It can work with or without interrupts:.As already said clock configuration won’t be touched it will stay in default settings (4MHz). On the picture above you can see clock source for System Timer. For this tutorial it is out of scope to describe whole clock tree but we’ll focus on the things we need. So it is not a surprise there is almost nothing in STM32L4 datasheet about it. System Timer is bound to the Cortex Core every Cortex processor with the same Core will have the same System Timer. As you can see this is bad approach as system, usually, needs a constant delay or at least one which doesn’t waste time doing nothing.
#USING SYSTICK TIMER IAR ARM CORTEX M4 CODE#
You would have: LED_ON -> Delay -> Some code -> LED_OFF … Therefore real delay, until LED is turned ON – OFF, will also account Some code as it needs to execute and occupy processor time.

In this case delay is increasing proportionally with the code. (Search: 3.3.1 Table of processor instructions for Cortex M4 Processor).Ġ80002e0: ldr r2, (0x80002f4 )įor the sake of conversation let’s not dwell into instruction counting, there will be time for that, and let’s take, with a grain of salt, that produced delay with this For loop is one second.

You can try to count how many clock cycles one For loop cycle takes with the help from ARM documentation. If you look at disassembly you see that generated code is not even close to one instruction cycle. If we take an example from last tutorial where For loop counted to 400000, so 1/10 of clock frequency, you would expect a delay to be ~1/10th of a second but… It doesn’t work like that as one clock cycle != one instruction cycle and in this case one loop cycle doesn’t contain only counting. That means that processor, in this example, can do 4 million clock cycles, time between two adjacent pulses of the oscillator, in 1 second. When processor, in this case Arm Cortex M4, is reset MSI, an internal RC oscillator, is used as a system clock with default frequency of 4MHz. However, problem with that approach is that your application is wasting processor time, processor can’t switch to other tasks as it is stuck counting, and delay will never be the same as it depends on code optimization and clock speed. In last tutorial you saw an example of a blinking LED using simple For loop. Examples will contain blinking LEDs using System Timer for Cortex M4. In this tutorial you will learn how to configure and use System Timer (SysTick) for STM32L4 with polling or interrupt.
