uasyncio and inaccessible REPL

RP2040 based microcontroller boards running MicroPython.
Target audience: MicroPython users with an RP2040 boards.
This does not include conventional Linux-based Raspberry Pi boards.
rtborg
Posts: 16
Joined: Tue Sep 07, 2021 6:01 am

uasyncio and inaccessible REPL

Post by rtborg » Sun Nov 07, 2021 5:37 pm

I am trying to use uasyncio with a RPi Pico, and the first thing I did was write a small blinky program, using the firmware skeleton from uasyncio docs. Here's my main.py:

Code: Select all

import uasyncio as asyncio
import machine

def set_global_exception():
    def handle_exception(loop, context):
        import sys
        sys.print_exception(context["exception"])
        sys.exit()
    loop = asyncio.get_event_loop()
    loop.set_exception_handler(handle_exception)
    
    
async def blink_task():
    led = machine.Pin(25, machine.Pin.OUT)
    
    while True:
        led.toggle()
        await asyncio.sleep_ms(150)

    
async def main():
    set_global_exception()  # Debug aid
    
    blinky = asyncio.create_task(blink_task())
    
    await blinky
try:
    asyncio.run(main())
finally:
    asyncio.new_event_loop() 
Once I run it, the on-board LED blinks, but I lose connection to the REPL once I close the IDE. When I reopen it (Thonny on Linux), connection fails. The same happens on Windows too.

What could be the reason for that?

EDIT: I think I found the main reason, being not reading the docs as thoroughly as I should. The program restarts thanks to the *finally* clause. If I remove the *asyncio.new_event_loop()*, I get the REPL when resetting the device again.

EDIT 2: The problem persists - after disconnecting the board, it's impossible to connect to REPL again.

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: uasyncio and inaccessible REPL

Post by pythoncoder » Mon Nov 08, 2021 1:20 pm

If your program is saved to /pyboard/main.py it will restart every time the board reboots. It's usually best to save it to a different name and have main.py empty.
Peter Hinch
Index to my micropython libraries.

rtborg
Posts: 16
Joined: Tue Sep 07, 2021 6:01 am

Re: uasyncio and inaccessible REPL

Post by rtborg » Mon Nov 08, 2021 1:34 pm

I tried that - left an empty main.py file, and saved my code as application.py - still can't get REPL once I close the IDE. When I add a print task, which outputs a 'Hello' every 2 seconds for example, I can see that the IDE connects to the port of the REPL, and prints the string. However, I can't reset the board. That may mean that main.py is not executed if empty.

I placed a print() line in main.py - now it appears to execute correctly upon boot, and I get the REPL. However, I'd like to get the uasyncio scheduler running upon power-on, and be able to interrupt it.

EDIT: Tried executing a while True loop with a delay and toggle LED inside. When that happens, I can't get REPL, so the issue is not uasyncio.

I am not sure if this is the expected behavior of Micropython, but it may be.

User avatar
Roberthh
Posts: 3668
Joined: Sat May 09, 2015 4:13 pm
Location: Rhineland, Europe

Re: uasyncio and inaccessible REPL

Post by Roberthh » Mon Nov 08, 2021 6:07 pm

If a script is running then there is no REPL.

rtborg
Posts: 16
Joined: Tue Sep 07, 2021 6:01 am

Re: uasyncio and inaccessible REPL

Post by rtborg » Mon Nov 08, 2021 7:03 pm

Is there a way to interrupt that script, and get the REPL? I am stuck on that.

User avatar
Roberthh
Posts: 3668
Joined: Sat May 09, 2015 4:13 pm
Location: Rhineland, Europe

Re: uasyncio and inaccessible REPL

Post by Roberthh » Mon Nov 08, 2021 7:31 pm

Try Ctrl-C

rtborg
Posts: 16
Joined: Tue Sep 07, 2021 6:01 am

Re: uasyncio and inaccessible REPL

Post by rtborg » Mon Nov 08, 2021 7:35 pm

Tried that on both Windows and Linux, not working except when I initially flash the board. Once I exit Thonny, there's no way to get the REPL when starting it again, ctrl-d and ctrl-c both fail.

User avatar
Roberthh
Posts: 3668
Joined: Sat May 09, 2015 4:13 pm
Location: Rhineland, Europe

Re: uasyncio and inaccessible REPL

Post by Roberthh » Mon Nov 08, 2021 8:57 pm

Did you try a simple terminal emulator like Putty or Picocom? Then you can be sure that nothing else than you intend is communicated to the board. How do you start the script that does not terminate?

rtborg
Posts: 16
Joined: Tue Sep 07, 2021 6:01 am

Re: uasyncio and inaccessible REPL

Post by rtborg » Tue Nov 09, 2021 5:38 am

I tried Putty (Windows & Linux) and Moserial (Linux).

The script that does not terminate is loaded with Thonny and named main.py.

User avatar
Roberthh
Posts: 3668
Joined: Sat May 09, 2015 4:13 pm
Location: Rhineland, Europe

Re: uasyncio and inaccessible REPL

Post by Roberthh » Tue Nov 09, 2021 7:39 am

Here is a method to delete main.py and boot.py but keep the other files on the device. It loads a special firmware version. https://forums.raspberrypi.com/viewtopic.php?t=305432
If you do not care about the file son the device, you can as well use the flash_nuke.uf2 firmware from RPI to clear the flash.
In both cases to have to reinstall the standard Micropython again after clearing the flash or erasing main.py.

Post Reply