← index #9280Issue #7188
Related · high · value 0.801
QUERY · ISSUE

utime.sleep_us() overflow

openby socketpairopened 2022-09-11updated 2022-09-12
docs
>>> from utime import sleep_us
>>> sleep_us(24*3600*1000_000)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
OverflowError: overflow converting long int to machine word

It's not documented about any limits on the argument. Either fix in documentation or fix. Fixing is better.

CANDIDATE · ISSUE

rp2: irq_disable irq_enable - sleep for more than 10 micro seconds - rp2 locks

openby kevindawsonopened 2021-04-29updated 2021-05-04
port-rp2

rp2 Hacking around with irq_disable/irq_enable
inspired by Writing interrupt handlers

This is an instance of a hazard known as a race condition: the ISR and the main program loop race to alter the variables. To avoid inconsistency a means must be employed to ensure that the ISR does not alter the values for the duration of the critical section. One way to achieve this is to issue pyb.disable_irq() before the start of the section, and pyb.enable_irq() at the end.

compleat code sample:

# Passive Infrared Sensor (pis)

import micropython
micropython.alloc_emergency_exception_buf(100)

from machine import Pin, lightsleep, disable_irq, enable_irq, soft_reset
from utime import sleep, sleep_ms, sleep_us

escape_count = 0
led = Pin(25, Pin.OUT)
# Then set up the pin you wired your HC-SR501 sensor to:
ADC0 = 26
pir = Pin(ADC0, Pin.IN, Pin.PULL_DOWN)

def action_plus_ips_resetting(placebo):
    print("going to sleep", end=" ")
    global escape_count
    escape_count += 1
    for _ in range(8):
        print("z", end="")
        led.toggle()
        sleep(1)
    print("z!")

def pir_handler(pin):
    # prevent the alarm from being triggered by any jitter in the signal from the PIR sensor.
    # The process of smoothing out fluctuations, as we’ve done here, is known as debouncing.
    sleep_ms(80)

    if pin.value():
        print("\tALARM! Motion detected!")
        micropython.schedule(action_plus_ips_resetting, True)

print("PIS acclimatising")
lightsleep(31000)
print("PIS ready")

pir.irq(handler=pir_handler, trigger=Pin.IRQ_RISING)

while escape_count <= 33:
    print("pondering")
    sleep(1)

    irq_state = disable_irq()
    print("start critical")
    sleep_us(10) # max value 
    escape_count += 1
    print(escape_count)
    print("end critical")
    enable_irq(irq_state)

soft_reset()

Output as expected

pondering
start critical
25
end critical
pondering
        ALARM! Motion detected!
start critical
26
end critical
going to sleep zzzzzzzzz!
pondering
start critical
28
end critical
pondering

But if you sleep_us() > 10 rp2 locks

    irq_state = disable_irq()
    print("start critical")
    sleep_us(10) # max value 
    escape_count += 1
    print(escape_count)
    print("end critical")
    enable_irq(irq_state)

sleep for more than 10 micro seconds and rp2 locks up.

is this me?
or an issue

Keyboard

j / / n
next pair
k / / p
previous pair
1 / / h
show query pane
2 / / l
show candidate pane
c
copy suggested comment
r
toggle reasoning
g i
go to index
?
show this help
esc
close overlays

press ? or esc to close

copied