cancel
Showing results for 
Search instead for 
Did you mean: 

Systick Delay functions

jdcowpland
Associate II
Posted on January 07, 2015 at 11:31

Hi folks,

I have had some code working for quite a while now using the sytick to create delay to the order of ms. This was made up of the following functions:

static uint32_t TimingDelay;
void Delay( uint32_t nTime)
{
TimingDelay = nTime;
while(TimingDelay != 0);
}
void TimingDelay_Decrement(void)
{
if (TimingDelay != 0x00)
{
TimingDelay--;
}
}
void SysTick_Handler(void)
{
TimingDelay_Decrement();
}

With the systick config setup with the systemcoreclock/1000. This all worked fine. What I am trying do now, is to introduce a us delay. I thought it should be as simple as changing the systick config to systemcoreclock/1000000, and creating a Delay_us() function, and then modifying my Delay() function to

TimingDelay = nTime*1000;

This seems to encounter some issues where it never seems to leave the Delay function. The Delay_us function seems to work fine. Has anyone else ever implemented a similar idea?
7 REPLIES 7
AvaTar
Lead
Posted on January 07, 2015 at 13:10

I thought it should be as simple as changing the systick config to systemcoreclock/1000000,

...

 

This seems to encounter some issues where it never seems to leave the Delay function.

 

An interrupt frequency of 1MHz (one per microsecond) is just cluttering your MCU.

A M4 core might still have some cycles left, but M0 cores less so.

If you need the usec delay rarely (e.g. less than ever millisecond), use a timer you configure to 1.0 us cycle time, and the delay time as counter value.

And if you need precision in the low usec range, measure the setup runtime, and subtract it from the counter value.

Posted on January 07, 2015 at 13:31

As indicated interrupting a 1 MHz is ridiculous. You're going to saturate the processor with useless work.

If you need tight timing, consider a spin loop looking at DWT_CYCCNT, or other rapidly clocked free running timer. These also have the benefit of being usable in interrupts, although you'd generally want to avoid blocking.

https://my.st.com/public/STe2ecommunities/mcu/Lists/cortex_mx_stm32/Flat.aspx?RootFolder=https://my.st.com/public/STe2ecommunities/mcu/Lists/cortex_mx_stm32/compensating%20latencies%20on%20STM32F4%20interrupts&FolderCTID=0x01200200770978C69A1141439FE559EB459D7580009C4E14902C3CDE46A77F0FFD06506F5B&cu...

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
childresss
Associate II
Posted on January 08, 2015 at 08:27

HAL_Delay(msec);

Posted on January 08, 2015 at 14:37

HAL_Delay(msec);

Dude said microseconds

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
jdcowpland
Associate II
Posted on January 09, 2015 at 10:31

Ah of course! Feeling a bit stupid now. Have now setup TIM2 but it doesn't seem to be working (this is the first time I've played with timers). My code is as follows:

void Timer_Setup(void)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
TIM_TimeBaseStructure.TIM_Period = 1000 - 1; //ms
TIM_TimeBaseStructure.TIM_Prescaler = 42 - 1;
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Down;
TIM_TimeBaseStructure.TIM_RepetitionCounter = 0;
TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);
//TIM_Cmd(TIM2,ENABLE);
}
void Delay_us( int nTime)
{
u16 counter=nTime&0xffff;
TIM_Cmd(TIM2,ENABLE);
TIM_SetCounter(TIM2,counter);
while(counter>1)
{
counter=TIM_GetCounter(TIM2);
}
TIM_Cmd(TIM2,DISABLE);
}

Why wouldn't the Delay_us function be working. All it does is keep returning a counter value equal to nTime, as if the timer were not counting down properly.
jdcowpland
Associate II
Posted on January 12, 2015 at 16:44

Anyone able to point out what I'm doing wrong?

Posted on January 12, 2015 at 17:06

Nothing is immediately jumping out to me. Could it be the initialization code isn't run, or that it's not preloading?

TIM2 is 32-bit, you could just let it up count and do a B-A type comparison.
Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..