Forewarning: I have zero experience with python on an embedded target and the Raspberry Pi Pico.
I know, I am bit late to the Pico party (perhaps past fashionable late) but, I am still quite thrilled to be mingling with the Pico today. Before I even had a chance to document a basic blinky application, I was taken on a wild goose chase. While trying to understand what was causing the timing variability in a very basic python script, I opted to disabled the garbage collector in a main.py script. This proved to be a very bad idea☹…
Within a blink of an eye I was greeted with a memory error in the Thonny Shell ☹☹. After unplugging and replugging in the Pico I see the following message:
Unable to connect to COM7: could not open port ‘COM7’: PermissionError(13, ‘Access is denied.’, None, 5)
If you have serial connection to the device from another program, then disconnect it there first.
Backend terminated or disconnected. Use ‘Stop/Restart’ to restart.
This really threw me off. I tried running Thonny as admin; same result. I tried reflahing the Pico with on die bootloader, reinstalling CDC drivers with Zadig, nothing, nada, same problem persisted.
Somehow, I stumbled onto the idea of nuking the flash memory. Fortunately, Adafruit provides a small utility that will wipe/nuke the entire contents of the flash memory. It is available at: https://learn.adafruit.com/getting-started-with-raspberry-pi-Pico-circuitpython/circuitpython
(Thank you so much Lady Ada!!! Maybe RPI has one too?)
Ultimately, I suppose the upython environment would run long enough to enumerate over USB as CDC device but not long enough for you to reflash a new main.py script from Thonny.
Moral of the story is that reflashing a upython UF2 doesn’t actually restore the Pico to an original pristine state, it perseveres the existing on flash python scripts. So, if you have a naughty main.py as I did, you may have to nuke your flash memory. I believe the RPI organization should make this fact crystal clear. Or, perhaps, when reflashing a UF2, rename main.py to main-old.py or something of the like, so you can get back to Thonny with ease.
Takeaway for total beginners like myself:
- Don’t disable the garbage collector.
- Maybe don’t use the filename main.py until you need to have the Pico operate without Thonny.
- When you reflash a uPython UF2 from the USB bootloader your existing scripts in flash memory are persevered. This is good in that you don’t loose you scripts. It can be bad if you have troublesome main.py, in which case you may have to nuke your flash memory.