Picosystem how to get persistent storage in C++?

Hello, I’ve been going through the C++ SDK and API documentation, but I’ve not seen any methods to create and read a file. I’m making a game, and would be great to allow players to save their game state to pickup where they left off, rather than restart each time.

I’ve played the game Gate Keeper on the Picosystem, and that game allows saved game states. Can anyone point out how to save data using C++ on the Picosystem.


There is this post: The basics of writing/reading user data to the flash memory for long term storage - Raspberry Pi Forums
Note it has a correction at the end, so read the complete post.

This is only reading/writing raw bytes, which should be enough to save the state of a game. If you really need files, you should add an sd-card and use one of the fatfs-libraries available.

1 Like

You can, of course, use fatfs to wrangle files in flash too - it’s what I do with my Picow Boilerplate. But you are somewhat limited by size, of course!

Good point. Regarding the size limit: I have added an xtsd chip to one of my Pico-designs. This is a flash chip with emulated sd-controller. Compared to the cost of a sd-reader and sd-card, these chips are really cheap (and faster and non-mechanical). Adafruit has one on a breakout, nice for testing, but very expensive.

Thanks for your response, this seems to be the path I’ll want to take. However, Visual Studio Code is not finding the #include "hardware\flash.h"

I suspect this is a cmake issue, but I’m somewhat new to cmake. Any suggestions?

Have you tried with a slash instead of a backslash in your #include?

Yes, of course. Either way slash or backslash, Visual Studio Code reports
cannot open source file "hardware/flash.h" C/C++(1696)
when hovering over the red-wavy line for the #include.

I suspect this is a cmake issue, as I found within within pico-sdk/src/rp2_common/CMakeList.txt the first line is:

option(PICO_NO_FLASH "Default binaries to not not use flash")

So I’m assuming PICO_NO_FLASH is being set and I’m not sure where I can set that option allowing the flash.h to be included.

The issue is to include add hardware_flash to CMakeLists.txt :

target_link_libraries( your_target_name PRIVATE

Someone had responded in the Raspberry Pi Forum linked above.

1 Like