0

I have a Raspberry Pi Pico running a Python script, but the end-user will not have Thonny, nor access to the button on the Raspberry Pi Pico. I want to update the script via the USB port from a PC. Is that possible?

Using the C SDK is inappropriate in this case.

user4157124
  • 2,809
  • 13
  • 27
  • 42
PhilTilson
  • 55
  • 5
  • What specific type of Python are you running? There is MicroPython and CircuitPython and there might be others. When the Python interpreter is running and your Pico is plugged into USB, does your computer recognize it as a USB disk drive? – David Grayson Jul 10 '23 at 17:46
  • Running MicroPython - I think this is the only version that runs on the Pico? When Pico is plugged into USB, the port acts like a serial port. However, if I press the button on the Pico before powering up, then it is seen as a mass storage device (USB disk drive). – PhilTilson Jul 11 '23 at 21:27

1 Answers1

0

In general, you're supposed to use the MicroPython remote control: mpremote to update files on a MicroPython device.

However, if you are expecting end users to do this, and they aren't very technical, you might want to create a firmware image as a UF2 file that contains both the Python interpreter and your code (either as filesystem or as frozen MicroPython modules). Then have the user install that file using the RP2040's built-in USB bootloader, which requires no additional software installation. Also, doing it this way ensures they are using the right version of MicroPython, which you've already tested with that code.

David Grayson
  • 84,103
  • 24
  • 152
  • 189
  • Thanks David, but this is my problem. Non-technical end-users who will not have access to the module, so can't push the button in order to get it into bootloader mode! I guess it must be possible, as Thonny manages to upload code directly to the module - but I don't know how they do it, and I am not C- or Linux-literate enough to be able to work through the code! – PhilTilson Jul 13 '23 at 09:58
  • Oh yeah, Thonny does have some features for updating the firmware on boards so maybe you could convince it to do what you want, but you don't really want to be relying on third-party software that could change at any time. For what it's worth, you can run `machine.bootloader()` in your Python code to jump into the RP2040's built-in USB bootloader. – David Grayson Jul 13 '23 at 18:07
  • Indeed - that works! But is there any way to move a file from the virtual RP2 drive into the flash area that is used to run programs? There seem to be two completely different (and non-communicating) storage areas on the Pico. – PhilTilson Jul 14 '23 at 21:25
  • No, they aren't separate; there is just one flash chip (check the board's schematic). The RP2 USB bootloader can be used to overwrite the entire flash chip or any number of sections of it. Many UF2 files you find for Python interpreters only overwrite the interpreter section of flash (just an arbitrary part of flash used for the interpreter), but if you learn a little bit about UF2 and write some scripts, you can generate your own UF2 file that overwrites the interpreter section and also writes your own filesystem to the filesystem section of flash, which holds Python code, etc. – David Grayson Jul 14 '23 at 21:59
  • I know this because I've actually done it before, but I was using a FAT filesystem and you're probably using littlefs: https://github.com/pololu/micropython-build/blob/925375112a688937bdcd168141db788070caa854/image_builder.sh – David Grayson Jul 14 '23 at 22:03
  • To expand on something I said in my answer: If you don't want to understand UF2 and filesystems and write your own scripts for that stuff, it actually might be easier to compile MicroPython yourself, and just drop your scripts into the `ports/rp2/modules` directory. Then they get compiled ahead of time and included as "frozen modules" in the interpreter section of the flash. – David Grayson Jul 14 '23 at 22:07