# How Delay mechanism works in embedded EFM32

#### yef smith

Joined Aug 2, 2020
81
Hello,I cant see how the following code dmaking a delay?
We have SysTick iterrupt which i dont know what it means.
What is the meaning of SysTick_Config(CMU_ClockFreqGet(cmuClock_CORE)?
Thanks.
Code:
#include <stdint.h>
#include <stdbool.h>
#include "em_device.h"
#include "em_chip.h"
#include "em_cmu.h"
#include "em_emu.h"
#include "bsp.h"
#include "bsp_trace.h"

void SysTick_Handler(void)
{
msTicks++;       /* increment counter necessary in Delay()*/
}

void Delay(uint32_t dlyTicks)
{
uint32_t curTicks;

curTicks = msTicks;
while ((msTicks - curTicks) < dlyTicks) ;
}

int main(void)
{
/* Chip errata */
CHIP_Init();
CMU_ClockEnable(cmuClock_GPIO,true);
/* Setup SysTick Timer for 1 msec interrupts  */
if (SysTick_Config(CMU_ClockFreqGet(cmuClock_CORE) / 1000)) {
while (1) ;
}

}

#### Analog Ground

Joined Apr 24, 2019
419
The Systick timer causes an interrupt every millisecond. The interrupt handler at line 10 of your code increments a variable. This variable keeps track of how many milliseconds have gone by. Compare values of this variable to knkow how much timer has gone by (the delay). I think there is some code missing. Where are the calls to delay(). Also, the clearing of msTicks at the beginning of the delay.

Last edited:

#### yef smith

Joined Aug 2, 2020
81
How do i reduce the resolution of the systick timer so it will interrupt every 1micro second ?
Thanks.

#### Analog Ground

Joined Apr 24, 2019
419
How do i reduce the resolution of the systick timer so it will interrupt every 1micro second ?
Thanks.
The SysTick timer counts by one every cycle of the main clock. For example, if the main clock is 50 MHz, the period of each cycle is 20 nanoseconds. To get 1 microsecond, divide 1 microsecond by 20 nanoseconds to get a count of 50. There is one additional thing explained in the article. The counter adds a count of 1 to the count value put in the count register. So, subtract 1 from 50 to get the exact value needed. 49 + 1 = 50. Lots of people ignore the extra count but to be precise, take it into account.

BTW, interrupting every microsecond is pretty fast but if the interrupt handler is short, it can be done. Entering and exiting the handler in the ARM architecture is very fast. If your main clock is around 50 MHz it can work. Try it!