millis() interfere with Task Scheduler? #20
Replies: 7 comments
-
The use of any function that disables interrupts globally will prevent the scheduler from triggering. Unfortunately Normally it wouldn't be a problem, as the time where the global interrupt is disabled is very short, but if Unless there are very tight timing requirements, that would normally be supported by a hardware timer, it is better to use the FreeRTOS API delay functions to block tasks. This enables you to put the MCU to sleep in the idle Task when all productive Tasks are blocked waiting for a timer to expire (and therefore the idle Task can run). unsigned long millis()
{
unsigned long m;
uint8_t oldSREG = SREG;
// disable interrupts while we read timer0_millis or we might get an
// inconsistent value (e.g. in the middle of a write to timer0_millis)
cli();
m = timer0_millis;
SREG = oldSREG;
return m;
} |
Beta Was this translation helpful? Give feedback.
-
That was exactly the problem, thank you. |
Beta Was this translation helpful? Give feedback.
-
Good luck. |
Beta Was this translation helpful? Give feedback.
-
That sounds contrary to the datasheet. Quoting from ATMega328/P page 32:
The WDT interrupt has priority 7 whereas Timer 0 has 15-17. Wouldn't that mean, that even in a tight loop the WDT interrupt would be remembered and executed as soon as Best, |
Beta Was this translation helpful? Give feedback.
-
Yes, generally, there would be a hang for as long as the global interrupt is disabled. Since the usage of I've also noticed the same effect with other Arduino code that disables the global interrupt for long periods, like software serial. Far better to use the FreeRTOS based timers to manage delays, or in the worst case the |
Beta Was this translation helpful? Give feedback.
-
Aha, so the problem was more like: cli();
while (millis() < 50);
doSomething();
sei(); Thus it's absolutely fine to use Thanks for the explanation! Much appreciated (and I'm less confused now). 👍
Of course, but you only get 15ms slices with the WDT. Maybe worth to mention: When dealing with clock cycles __builtin_avr_nops() looks handy. Thanks, Phillip! Best, |
Beta Was this translation helpful? Give feedback.
-
Reading the arduino/arduino it is clear that Actually, the problem is not inherent in
Normally, I use the Timer0 or Timer2 for up to 1ms accuracy. Just that Arduino configures all the hardware Timers itself, so they can't be reconfigured by a library without errors. |
Beta Was this translation helpful? Give feedback.
-
Does using millis() function inside a callable block interfere with the task scheduler? I'm using an RF library which checks for timeouts using the millis() function, and I noticed that the scheduler hangs.
Beta Was this translation helpful? Give feedback.
All reactions