Is Systick the way to generate a delay function
Options
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2013-04-11 11:49 PM
Posted on April 12, 2013 at 08:49
Hi Folks (and Clive)
I have generated timing delays using systick and I want some comments on whether it was the right way to go:-GPIO_InitTypeDef GPIO_InitStructure;static __IO uint32_t TimingDelay;void Delay(__IO uint32_t nTime) { TimingDelay = nTime; while (TimingDelay != 0) ; }void TimingDelay_Decrement(void) { if (TimingDelay != 0x00) { TimingDelay--; } }extern ''C'' void SysTick_Handler(void) { TimingDelay_Decrement(); }I believe that systick is a low priority - so can this code get stuck if there is a pending higher priority interrupt??CS
This discussion is locked. Please start a new topic to ask your question.
3 REPLIES 3
Options
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2013-04-12 2:29 AM
Posted on April 12, 2013 at 11:29
I make short delays without interrupts:
#define TICKSGN ((SysTick_VAL_CURRENT_Msk+1)>>1)
#define WAITMAX (SysTick_VAL_CURRENT_Msk >> 1)
static inline void waittick(uint32_t step)
{
static uint32_t tick0;
if (!step)
{
tick0 = SysTick->VAL;
return;
}
tick0 -= step;
while ((tick0 - SysTick->VAL) & TICKSGN)
{
__NOP();
__NOP();
}
}
static inline void sleep(uint32_t delay)
{
waittick(0);
delay++;
while (delay > WAITMAX)
{
waittick(WAITMAX);
delay -= WAITMAX;
}
waittick(delay);
}
Options
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2013-04-12 4:30 AM
Posted on April 12, 2013 at 13:30
You can change the priority of the SysTick interrupt. Generally speaking you shouldn't be spending enough time in another interrupt for this to be a problem, or using multi millisecond delays in other interrupt routines.
My preference would be for SysTick to increment a millisecond tick variable continuously, and have the delay routine observe that. For finer precision, delays could also use DWT_CYCCNT
Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
Up vote any posts that you find helpful, it shows what's working..
Options
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2013-04-16 1:20 AM
Posted on April 16, 2013 at 10:20
Hi folks
Thanks for your comments - its good to know I'm not fundamentally going in the wrong direction!! I will have a look at the suggestions for other types of delays as I'm sure they will be useful.CS