MODM7AE70 blocking millisecond delay
Posted: Sun Nov 01, 2020 9:12 am
Hi,
Quick code for a high resolution blocking delay.
Paste code and call delayms(10) for a 10ms delay. It blocks the calling task/prio for that time.
Seems to work fine, tweaks or fixes (if any) appreciated.
Cheers,
John
Quick code for a high resolution blocking delay.
Paste code and call delayms(10) for a 10ms delay. It blocks the calling task/prio for that time.
Seems to work fine, tweaks or fixes (if any) appreciated.
Cheers,
John
Code: Select all
#include <nbrtos.h>
#include <IntervalTimer.h>
/* variables -----------------------------------------------------------------*/
OS_SEM delayms_Semaphores[OS_MAX_PRIOS]; // semaphores for each prio
uint8_t delayms_active[OS_MAX_PRIOS]={0}; // is current PRIO blocking?
volatile int32_t delayms_Timers[OS_MAX_PRIOS]; // we set this to eg, 200 for 200ms delay
bool delayms_initialized = false; // first time init
/* delayms_callback - callback for every tick of the timer -------------------*/
void delayms_callback()
{
for(int i=0;i<OS_MAX_PRIOS;i++)
{
if(delayms_active[i])
{
if(delayms_Timers[i]<=0) // we use signed int and <=0 to prevent a possible deadlock
{
delayms_active[i] = 0;
delayms_Semaphores[i].Post();
} else delayms_Timers[i]--;
}
}
}
/* delayms_init - initializes timer ------------------------------------------*/
void delayms_init()
{
IntervalInterruptCallback(&delayms_callback, 1000);
delayms_initialized = true;
}
/* delayms - parameter is milliseconds to block the current PRIO -------------*/
void delayms(uint16_t ms)
{
if(delayms_initialized==false)
delayms_init();
// 1ms can be between 0ms and 1ms
delayms_Timers[nPrioOfCurTask] = ms;
delayms_active[nPrioOfCurTask] = 1;
delayms_Semaphores[nPrioOfCurTask].Init(0);
delayms_Semaphores[nPrioOfCurTask].Pend();
return;
}