[Feature] Build HAT flash memory support
The Raspberry Pi Build HAT has a 2MB flash memory chip that is basically only used for the bootloader.
I suspect there will be many ideas on how we could use this. So I will share some ideas on why we shouldn't use it. But don't let that stop anyone from having fun brainstorming about it and sharing ideas anyway. 😄
- We could put the Pybricks firmware on the flash memory instead of running it from RAM and run it from there. Since the flash memory operates over SPI, this would make the execution slower. We should of course measure it to be sure though.
- If we allow writing to the flash memory, we could accidentally wipe out the bootloader. The only way to recover then would be using a SWT debugger. This is special hardware that most people don't have, so effectively it would brick their Build HAT.
- There isn't a button on the Build HAT to start user programs, so there isn't much use is storing user programs in the flash memory.
The only other feature we would be missing from Pybricks that requires flash memory is storing user data between power cycles. This could be offloaded to the Raspberry Pi though and stored in a file there.
Some minor nitpicks:
Since the flash memory operates over SPI, this would make the execution slower.
Doesn't the BuildHAT use the RP2040 internally? If so, that thing is designed to load code from flash using its XIP cache. See section 2.6.3.1 of the rp2040 datasheet. It won't be as fast as having all the code in RAM, but it might not be much slower. It is the default configuration for programs compiled with the RP2040 SDK.
If we allow writing to the flash memory
If we do allow it, we should definitely do so via some mechanism that only allows writes to certain known ranges that we're not using for other stuff. That being said, many rp2040 programs do successfully use the flash for storage. Here's an example: https://github.com/jaguilar/awning_arduino/blob/main/lib/pico_rolling_code_storage/pico_rolling_code_storage.cc.
Doesn't the BuildHAT use the RP2040 internally?
Yes.
It won't be as fast as having all the code in RAM, but it might not be much slower.
I know. That's why I said we should measure it to be sure. 😄