Posts: 40
Joined: Wed Jan 25, 2012 3:53 pm

Programming Pico Flash In-System

Tue Feb 16, 2021 1:53 pm


I'm just starting playing with the Pico and have a potential design (actually a DMX (RS485-ish!) controller) which would use a couple of Pico's for peripheral work, with a CPU sitting between them controlling other things. The CPU may even end up being a Pi4.

I'm currently looking at in-system programming of the Pico(s). They will be on their own modules, powered at 3.3v through the VSys pin. They'll communicate with the CPU via SPI. The official programming methods are via USB mass storage, or through the debug ports.

However, a third option struck me as an interesting possibility. In the official documentation for the Pi Pico is this code, ... uke/nuke.c, which zeros out the Pico's flash. Could I use the same approach for programming the flash? In other words: have my binaries all compiled with the PICO_NO_FLASH flag so they run directly from RAM. If I have new firmware to program from the CPU, the CPU signals this to the Pico via SPI. The Pico then writes the binary to its own flash using the program currently running in RAM. When the Pico reboots, it would then run the new binary image?

Presumably, this is just a case of taking the UF2 and writing it as a binary directly to a particular location in flash, so that the Pico boot ROM reads and loads it as expected?


User avatar
Posts: 5340
Joined: Fri Jul 22, 2016 9:09 pm
Location: Eberbach, Germany

Re: Programming Pico Flash In-System

Wed Feb 17, 2021 10:30 am ... f#page=266
no_flash does load .uf2 directly into RAM and executes -- each time.
default store .uf2 on flash, and Pico runs from flash.
With copy_to_ram code is stored on flash, but copied into RAM before getting executed.

It does not make sense to store something on flash with no_flash mode.

Posts: 1006
Joined: Sat May 26, 2012 5:32 pm

Re: Programming Pico Flash In-System

Wed Feb 17, 2021 7:20 pm

this is what the bootrom does right now yes? it fakes a thumb drive then takes the uf2 file it reads and extracts the data and writes it to the spi flash. Just do that using whatever method you want to get the data into the memory of the mcu so you can then write it to flash.

Can probably borrow the bootrom code.

Posts: 1
Joined: Thu May 06, 2021 9:18 am

Re: Programming Pico Flash In-System

Thu May 06, 2021 9:35 am

I'm following this as I would like to be able to provide some form of firmware update by placing a file on an SD card, checked at boot time....rather like the Arduino SDU

I'm wondering if this is possible to be done in the "constructor" code called before main(). for example by declaring a function:

static void __attribute__((constructor)) boot_firmware_check(void)
// Do some stuff in here to initialise SD card drivers, and look for a file (UF2/Bin/whatever) .... all before main() is called.

int main()
// My application code.

It would be really cool if the code could also verify the checksum of the loaded image, and perhaps even save the old firmware image higher up in flash too so that if there was an issue with the reprogramming you didn't end up with a brick. The end user won't necessarily have the facility to reprogram the device using SWD or plugging in the USB and hitting BOOTSEL without dismantling the unit.

Posts: 1006
Joined: Sat May 26, 2012 5:32 pm

Re: Programming Pico Flash In-System

Sun May 09, 2021 2:05 pm

There is no magic here, these are external off the shelf flash parts. Just like an sd card uses flash parts and an ssd drive and a usb thumb whenever you want.

If you want to erase part of the flash and write stuff to it...then just do that, whenever you want, during boot, after boot, etc...

No magic, so yes if you wanted to do something like that you could.

Return to “Bare metal, Assembly language”