Problem using 2 identical delays (with sleep()) in main & in thread

RP2040 based microcontroller boards running MicroPython.
Target audience: MicroPython users with an RP2040 boards.
This does not include conventional Linux-based Raspberry Pi boards.
Post Reply
jmbelisle
Posts: 1
Joined: Fri Mar 05, 2021 3:01 am

Problem using 2 identical delays (with sleep()) in main & in thread

Post by jmbelisle » Fri Mar 05, 2021 3:20 am

There is a Pico freeze issue when using 2 identical delays (with sleep() or sleep_ms()) in a thread code and a "main code".
In the example below the green led (Pico onboard one) flashes 8 times a second (with a timer) while the other two (external leds connected to pins 0 and 1 with 330ohm resistors) both flash 4 times a second: one is running in a thread while the other one runs in the main.
When the program is run, the Pico freezes after a few seconds. Interestingly enough...
a) if you use the SAME constant for both the THREAD run led_white and the MAIN run led_red, (eg 0.25 and 0.25 or 1.0 and 1.0) the Pico ALWAYS fails as long as the constant is the same.
b) if you use TWO DIFFERENT constants (say 0.25 and 0.24, or 0.50 and 1.00) the Pico NEVER fails.
c) the same is true if you use import sleep_ms() from utime and specify delays in milliseconds

The issue seems to be with the sleep() function. I don't know the root cause but will raise that as an issue on the github forum
As a workaround for the problem, one can use 2 different but close constants (say 250 and 251 in milliseconds).
But the issue remains. Opinions, ideas, analysis ????
jmb

Code: Select all

# Using Thonny 3.3.5 on Windows PC with Pico connected to PC USB port
# tested on Pico running 2 different uf2 versions (same results)
# rp2-pico-20210305-unstable-v1.14-83-g680ce4532.uf2 AND 
# rp2-pico-20210222-unstable-v1.14-80-g75db0b907.uf2
from machine import Pin, Timer
from time import sleep
import _thread

led_green = Pin(25, Pin.OUT)
led_green.off()
led_red = Pin(0, Pin.OUT)
led_red.off()
led_white = Pin(1, Pin.OUT)
led_white.off()

tim1 = Timer()

def blink_green(timer):
    led_green.toggle()
#  
tim1.init(freq=8, mode=Timer.PERIODIC, callback=blink_green)
#   
def blink_white(delay):
    while True:
        led_white.toggle()
        sleep(delay)
#
_thread.start_new_thread(blink_white, (1.00,))
#
def blink_red():
    while True:
        led_red.toggle()
        sleep(1.00)  
#
blink_red()

User avatar
aivarannamaa
Posts: 171
Joined: Fri Sep 22, 2017 3:19 pm
Location: Estonia
Contact:

Re: Problem using 2 identical delays (with sleep()) in main & in thread

Post by aivarannamaa » Thu Mar 11, 2021 8:15 am

I have also met problems with Pico threads (https://github.com/micropython/micropython/issues/6899), so I suspect the threading support isn't very robust yet.

When you are testing different parameters, then make sure you hard reset your device between tests. I've been scratching my head a lot by alternating two sets of parameters, when actually the program simply failed on every 2nd run regardless of the parameters.
Aivar Annamaa
https://thonny.org

Post Reply