cancel
Showing results for 
Search instead for 
Did you mean: 

SysTick , I dont understand whats happening.

Scott Dev
Senior
Posted on September 11, 2017 at 17:17

Hi,

   I am still working my way through the STM32 , and decided to  move away from CubeMX, to use the registers.  I have started by writing the following just to get the systick to work. Im not really interested on the time span, just want to see the interupt working. I have done the following, and wondering if anyone can tell me whats happening? I simply setup ticker interupt, and setup a delay. The delay should exit when count down to 0, but for some reason the Delay function never exits. I am using Keil and step through the Delay function and see it count down..

I am initialising the systick, and initialising the Load register.

I then simply create a Delay(x) routine to continue to check the variable to see if reached 0.

Many Thanks

Scott

#include 'stm32l073xx.h'

#include 'header.h'

uint32_t TimeDelay=0;

int main(void)

{

    SysTick_Initialize(100); // << init Load and set interupt

    Delay(5);  //<<do a simple countdown. In here  is the issue

   return 0;

}

void SysTick_Initialize(uint32_t ticks)

{

    SysTick->CTRL=0;

    SysTick->LOAD=ticks-1;

    

    NVIC_SetPriority(SysTick_IRQn,(1UL<<__NVIC_PRIO_BITS)-1UL);

    

    SysTick->VAL=0;

    SysTick->CTRL |= SysTick_CTRL_CLKSOURCE_Msk;

    SysTick->CTRL |= SysTick_CTRL_TICKINT_Msk;    

    SysTick->CTRL |= SysTick_CTRL_ENABLE_Msk;    

}

void SysTick_Handler(void)

{

    if(TimeDelay>0) //simply count down

        TimeDelay--; //just to test I have setup the interupt okay

}

void Delay(uint32_t nTime)

{

    TimeDelay=nTime;

    while(TimeDelay !=0) //<<< this loop never breaks out..  The variable does count down to 0.

     ;

    

}
7 REPLIES 7
David SIORPAES
ST Employee
Posted on September 11, 2017 at 17:29

I think you missed applying 'volatile' qualifier to 

TimeDelay.

Posted on September 11, 2017 at 17:49

Hi

You did't enable the NVIC systick IRQ

NVIC_EnableIRQ(SysTick_IRQn);

rgrds

vf

Scott Dev
Senior
Posted on September 11, 2017 at 18:04

Hi

  Thanks for your replies.

When I changed the line SysTick_Initialize(10) from 10 to 100, it works everytime. I can only assume the interupts were firing too often. Is this possible?  I thought the debugger would catch this.

Regards

Scott

Posted on September 11, 2017 at 18:01

Hi vf

  I assumed the line SysTick->CTRL |= SysTick_CTRL_TICKINT_Msk; enabled the interupt as the interupt routine gets called.

Regards

Scott

Posted on September 11, 2017 at 18:02

Hi David,

  Sorry I had a typo , I left out the volatile when I posted the code. See below how I think I gt it to work.

Regards

Scott

Posted on September 11, 2017 at 18:12

Hi!

Yes you are right. Was a mistake.

Rgrds

vf

Posted on September 11, 2017 at 18:26

That may well be possible.

The SysTick is firing every 10 cycles but its handler takes at least 15 cycles just to get served.