This repository was archived by the owner on Sep 6, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 216
modutime.c sleep_ms() occasionally affects ticks_ms() #223
Copy link
Copy link
Open
Description
Issuing sleep_ms(t) can cause ticks to be missed. The number of ms lost is min(10, t). The following script demonstrates this with timer callbacks apparently occurring early. Note that they never occur correspondingly late so this is not a manifestation of latency. Examination of the lastrun output around an early callback seems to show this happening. The script runs for 15s which is long enough for at least one instance to occur.
Behaviour with Timer(1) and Timer(-1) is identical. It is specific to the ESP32 port: this does not occur on ESP8266 or Pyboard.
from machine import Timer
from utime import ticks_ms, ticks_diff, sleep_ms
from micropython import schedule
timer = Timer(-1)
lastrun = None
maxtime = 0
mintime = 1000
def timer_cb(timer):
global lastrun, maxtime, mintime
if lastrun is not None:
delta = ticks_diff(ticks_ms(), lastrun)
maxtime = max(maxtime, delta)
mintime = min(mintime, delta)
schedule(print_me, delta)
lastrun = ticks_ms()
def print_me(delta):
print("timer_cb", delta, lastrun)
def start(duration, sleeptime):
timer.init(period=100, mode=Timer.PERIODIC, callback=timer_cb)
count = 0
while count <= duration // sleeptime:
sleep_ms(sleeptime)
count += 1
timer.deinit()
print('{}ms max {}ms min'.format(maxtime, mintime))
start(15000, 50) # Run for 15s sleeping for 50msMetadata
Metadata
Assignees
Labels
No labels