Running on a Pi Pico with MicroPython 1.23.0 and whatever the latest version of usb-device-cdc is in mip; code to reproduce:
from utime import sleep_ms, time
from _thread import start_new_thread
import usb.device
from usb.device.cdc import CDCInterface
SERIAL_TWO = None
def update():
while True:
print("Update: {}".format(time()), file=SERIAL_TWO, end="\r\n")
sleep_ms(1000)
def main():
global SERIAL_TWO
SERIAL_TWO = CDCInterface()
SERIAL_TWO.init(timeout=0)
usb.device.get().init(SERIAL_TWO, builtin_driver=True)
while not SERIAL_TWO.is_open():
sleep_ms(100)
start_new_thread(update, ())
while True:
sleep_ms(1000)
if __name__ == "__main__": main()
If you connect to both ports with mpremote a0 and mpremote a1, you'll see this error sporadically:
Traceback (most recent call last):
File "usb/device/core.py", line 316, in _xfer_cb
File "usb/device/cdc.py", line 327, in _wr_cb
File "usb/device/core.py", line 833, in finish_read
AssertionError:
But I did see this error once:
Unhandled exception in thread started by <function update at 0x2000aef0>
Traceback (most recent call last):
File "<stdin>", line 12, in update
File "usb/device/cdc.py", line 356, in write
File "usb/device/cdc.py", line 322, in _wr_xfer
File "usb/device/core.py", line 570, in submit_xfer
File "usb/device/core.py", line 302, in _submit_xfer
RuntimeError: xfer_pending
Please give some more details about this means: what exactly do you do, and what error output do you see?
A soft reset (i.e. Ctrl-D in the REPL) will still disconnect the USB port each time, because the runtime USB interface objects have to all be freed and recreated.
A hard reset (i.e. plugging the rp2 in, or calling
machine.reset()) should come up immediately with the two CDC ports.The only difference between boot.py and main.py is that the first time the device boots from a hard reset, it runs boot.py before it initialises USB. If you put the code in main.py instead of boot.py then the rp2 would first try create a device using the default USB interface, then immediately switch to the custom USB interface. This can show up on the host as connect/disconnect/connect cycle, or as an error depending on the timing.
With the init line commented out in boot.py:
mpremote fs cp boot.py :mpremote run main.pyI see the print statements I expect:
With the init line uncommented in boot.py:
mpremote fs cp boot.py :mpremote run main.pyI do not see the print statements I expect:
I get dropped back to the terminal, and connecting with
mpremote a0yields no output. Attempting to replace boot.py even after unplugging/replugging the pico also yields the above OSError; I have to drop to a shell and useos.unlink().Ah snap,
mpremote rundoes a soft reset before it runs the provided file. Because boot.py has loaded the custom USB interface, it disconnects the USB port every time (then boot.py runs and adds it back, so you end up where you started.)If you copy both boot.py and main.py to the filesystem then main.py works and you can use
mpremote a0to view the output, but you still can't usempremote runany more.I think the fix for this will be the planned work to have mpremote detect when the USB port is in a disconnect/reconnect cycle, and recover from it instead of erroring out.
Is there a timeline for when the planned work might be finished?
Not at the moment, I'm afraid.
I assume this is impacting
mpremote connectas well (also including a soft reset)? Running similar as the OP above, I'm finding that I can connect directly to REPL by connecting to its COM port - but not usingmpremote connect.Not sure if anything has changed since the last update here - though revisiting this a year later, now on MicroPython v1.27 - everything seems to be working, and I'm not sure I can find a way to make it not?
I'm seeing success with both (separately) MicroPico 4.3.4 in VS Code, and mpremote 1.27.0.
Curious if anyone else here still watching can help test / validate? Thanks!
Though for reasons I don't yet fully understand, having
usb.device.get().init(...)included as part ofboot.pyinterferes with running "MicroPico: Run current file on Pico" (micropico.run) from MicroPico in VS Code.Disabling "Micropico: No Soft Reset On Run" (
micropico.noSoftResetOnRun) seems to then resolve this.Though then, and I suspect unrelated, I get into trouble if I try using Ctrl+C to quit a loop - same as reported in https://github.com/paulober/MicroPico/issues/294 - though pretty sure this is an issue with MicroPico and not micropython.