rhulme / pico-flashloader

Small (<4k) flashloader for the RP2040 to allow a power-fail safe update in situations where the built-in bootloader is not suitable.
68 stars 16 forks source link

Platformio build support? #13

Open tlalexander opened 1 year ago

tlalexander commented 1 year ago

Hello! Wonderful code you have, thank you for sharing. It looks like I will be able to use this to update code over CAN bus on my robot.

My project currently builds with platformio. A lot of the stuff that this library does is beyond my skill set.

I am going to try to convert my project to cmake so that I can use this, but I am not presently sure if that will create other problems for me.

I am curious, do you have any experience with platformio, and do you happen to have any idea how this library could be implemented with that build system?

Thanks!

rhulme commented 1 year ago

Hi,

I've only used PlatformIO once in a different context and only for a simple project so I don't really know much about the ins and outs of more detailed configuration.

I wouldn't expect many changes to be necessary to the code other than any you might have to make anyway to make it fit your project's needs. Obviously you'll need to implement the application side yourself but the flashloader (and urloader in case you use that branch) can probably be used as-is.

The main thing here is that you are building two (or three with the urloader) separate images rather than a classical library that can be linked in.

You have to be able to specify a separate linker script for each image and, as the main linker scripts include other scripts, be able to indicate the dependencies to ensure that any changes trigger the linker again.

You also need a final UF2 image containing all the other images. This is not absolutely essential but makes initial flashing easier. For this you need to define a custom target to run the 'uf2tool.py' script and define its dependencies and what it generates.

I afraid don't know how easy this is to do within PlatformIO (particularly the "multiple images from one project" side) but I'd be interested to hear if you get it working.

tlalexander commented 1 year ago

Well gosh, I have done some digging and I have come to a remarkable conclusion. After learning all about the internals of platformio and getting just to the point of trying to compile in your code with an arduino example, I noticed some differences in the memmap_default.ld file between your library and the arduino one I was using. And the one I was using mentioned something about "ota updates" in the memmap file.

I did some more digging, and basically this functionality is already implemented for the RP2040 when using this arduino core: https://github.com/earlephilhower/arduino-pico

See this concise example for updating from a local file, but there are also functions to update from wifi. https://github.com/earlephilhower/arduino-pico/blob/master/libraries/PicoOTA/examples/OTAfromFile/OTAfromFile.ino

I need to update via CAN bus so I will stream in the file using my existing mechanisms.

It seems that OTA updates are a commonly implemented feature for Wifi arduinos, and so this library already supports it.

It might be nice to point people in that direction in the Readme if they need other features as I do. I am glad to see that I can use a stock library without maintaining a custom codebase for this!