samm_flynn
Active member
I’m implementing a real-time control system on a Teensy 4.1 and wanted to keep the loop() function completely empty, handling all computation inside interrupts. To achieve this, I tried the following approach:
However, after looking into the source code of delay.c , I found this :
To better understand its behaviour, I roughly translated this into Python:
When I run this in Python, one CPU core maxes out for 10 seconds, which makes me wonder if the same thing happens on the Teensy.
Now to get around this is python one could do the following -
Does calling delay(maxUnsignedLong); cause a performance penalty by keeping the core busy? If so, is there a way to release the core while waiting, similar to time.sleep() or event.wait() in Python?
Thanks for taking the time to read my question!
C++:
const uint32_t maxUnsignedLong = ~0UL; // Maximum value of uint32_t
void setup()
{
}
void loop()
{
delay(maxUnsignedLong);
}
C++:
// Wait for a number of milliseconds. During this time, interrupts remain
// active, but the rest of your program becomes effectively stalled. Usually
// delay() is used in very simple programs. To achieve delay without waiting
// use millis() or elapsedMillis. For shorter delay, use delayMicroseconds()
// or delayNanoseconds().
void delay(uint32_t msec)
{
uint32_t start;
if (msec == 0) return;
start = micros();
while (1) {
while ((micros() - start) >= 1000) {
if (--msec == 0) return;
start += 1000;
}
yield();
}
// TODO...
}
Python:
import time
def delay(msec):
if msec == 0:
return
start = time.monotonic_ns() # Get time in nanoseconds
while True:
while (time.monotonic_ns() - start) >= 1_000_000:
msec -= 1
if msec == 0:
return
start += 1_000_000
delay(10000)
Now to get around this is python one could do the following -
Python:
import time
import threading
def delay(msec):
if msec == 0:
return
event = threading.Event()
event.wait(msec / 1000)
delay(10000)
time.sleep(10000)
Does calling delay(maxUnsignedLong); cause a performance penalty by keeping the core busy? If so, is there a way to release the core while waiting, similar to time.sleep() or event.wait() in Python?
Thanks for taking the time to read my question!