Skip to content

Add persistent partition to RPi Pico Flash #87918

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed

Conversation

Snap-A
Copy link
Contributor

@Snap-A Snap-A commented Mar 31, 2025

Updated Summary

The added partition storage_partition can be used for the littelfs sample application. It is sized to have enough capacity for the created files in this sample code.

Details

The changed file rpi_pico-common.dtsi now contains a third partition, that describes "storage" are for a file system. The "code" partition was reduced in size to make room for this area. The new partition has a capacity of 16 kB, which is enough for the littelfs sample to store two files.

Tests

Compilation

The compile step for each platform succeeds:

$ west build --pristine -b rpi_pico samples/subsys/fs/littlefs
...
[160/160] Linking C executable zephyr/zephyr.elf
Memory region         Used Size  Region Size  %age Used
      BOOT_FLASH:         256 B        256 B    100.00%
           FLASH:       52616 B    2080512 B      2.53%
             RAM:       11072 B       264 KB      4.10%
        IDT_LIST:          0 GB        32 KB      0.00%
Generating files from /home/awolf/zephyrproject-fork/zephyr/build/zephyr/zephyr.elf for board: rpi_pico
Converted to uf2, output size: 105984, start address: 0x10000000
Wrote 105984 bytes to zephyr.uf2
$ west build --pristine -b rpi_pico/rp2040/w samples/subsys/fs/littlefs
...
[164/164] Linking C executable zephyr/zephyr.elf
Memory region         Used Size  Region Size  %age Used
      BOOT_FLASH:         256 B        256 B    100.00%
           FLASH:       52756 B    2080512 B      2.54%
             RAM:       11072 B       264 KB      4.10%
        IDT_LIST:          0 GB        32 KB      0.00%
Generating files from /home/awolf/zephyrproject-fork/zephyr/build/zephyr/zephyr.elf for board: rpi_pico
Converted to uf2, output size: 106496, start address: 0x10000000
Wrote 106496 bytes to zephyr.uf2

Test Run

The test were run in a Pico-W device.

First Run On Device

Log after flashing the littlefs sample:

Sample program to r/w files on littlefs
Area 2 at 0x1fc000 on flash-controller@18000000 for 16384 bytes
I: LittleFS version 2.10, disk version 2.1
I: FS at flash-controller@18000000:0x1fc000 is 4 0x1000-byte blocks with 512 cycle
I: partition sizes: rd 16 ; pr 16 ; ca 64 ; la 32
E: WEST_TOPDIR/modules/fs/littlefs/lfs.c:4604: Invalid block count (1 != 4)
W: can't mount (LFS -22); formatting
I: /lfs mounted
/lfs mount: 0
/lfs: bsize = 16 ; frsize = 4096 ; blocks = 4 ; bfree = 2

Listing dir /lfs ...
/lfs/boot_count read count:0 (bytes: 0)
/lfs/boot_count write new boot count 1: [wr:1]
I: Test file: /lfs/pattern.bin not found, create one!
------ FILE: /lfs/pattern.bin ------
01 55 55 55 55 55 55 55 02 55 55 55 55 55 55 55
...
45 55 aa 
I: /lfs unmounted
/lfs unmount: 0

After performing a power cycle:

Sample program to r/w files on littlefs
Area 2 at 0x1fc000 on flash-controller@18000000 for 16384 bytes
I: LittleFS version 2.10, disk version 2.1
I: FS at flash-controller@18000000:0x1fc000 is 4 0x1000-byte blocks with 512 cycle
I: partition sizes: rd 16 ; pr 16 ; ca 64 ; la 32
/lfs mount: 0
/lfs: bsize = 16 ; frsize = 4096 ; blocks = 4 ; bfree = 1

Listing dir /lfs ...
[FILE] boot_count (size = 1)
[FILE] pattern.bin (size = 547)
/lfs/boot_count read count:1 (bytes: 1)
/lfs/boot_count write new boot count 2: [wr:1]
------ FILE: /lfs/pattern.bin ------
02 55 55 55 55 55 55 55 03 55 55 55 55 55 55 55
...
46 55 ab 
I: /lfs unmounted
/lfs unmount: 0

Old Text

The below text is kept to document previous changes to make the littlefs sample work.

IGNORE TEXT BELOW

Summary

The added overlay files support the compilation for the Pico and Pico_w of the sample for the littlefs filesystem.

Details

The file rpi_pico.overlay reduces the size of the "code" partition and adds another partition at the end.
This partition has a capacity of 64 kB.

Tests

Compilation

The compile step for each platform succeeds:

$ west build -b rpi_pico samples/subsys/fs/littlefs
...
-- Found BOARD.dts: /home/awolf/zephyrproject-fork/zephyr/boards/raspberrypi/rpi_pico/rpi_pico.dts
-- Found devicetree overlay: /home/awolf/zephyrproject-fork/zephyr/samples/subsys/fs/littlefs/boards/rpi_pico.overlay
-- Generated zephyr.dts: /home/awolf/zephyrproject-fork/zephyr/build/zephyr/zephyr.dts
...
Converted to uf2, output size: 105984, start address: 0x10000000
Wrote 105984 bytes to zephyr.uf2
$ west build -b rpi_pico/rp2040/w samples/subsys/fs/littlefs
...
-- Found BOARD.dts: /home/awolf/zephyrproject-fork/zephyr/boards/raspberrypi/rpi_pico/rpi_pico_rp2040_w.dts
-- Found devicetree overlay: /home/awolf/zephyrproject-fork/zephyr/samples/subsys/fs/littlefs/boards/rpi_pico_rp2040_w.overlay
-- Generated zephyr.dts: /home/awolf/zephyrproject-fork/zephyr/build/zephyr/zephyr.dts
...
Converted to uf2, output size: 106496, start address: 0x10000000
Wrote 106496 bytes to zephyr.uf2

Test Run

The code was run on the Pico_w test platform

  1. First run included a flash erase (per config)
*** Booting Zephyr OS build v4.1.0-1562-g478212efc4e1 ***
Sample program to r/w files on littlefs
Area 2 at 0x1f0000 on flash-controller@18000000 for 65536 bytes
E: Erasing flash area ... 0
I: LittleFS version 2.10, disk version 2.1
I: FS at flash-controller@18000000:0x1f0000 is 16 0x1000-byte blocks with 512 cycle
I: partition sizes: rd 16 ; pr 16 ; ca 64 ; la 32
E: WEST_TOPDIR/modules/fs/littlefs/lfs.c:1389: Corrupted dir pair at {0x0, 0x1}
W: can't mount (LFS -84); formatting
I: /lfs mounted
/lfs mount: 0
/lfs: bsize = 16 ; frsize = 4096 ; blocks = 16 ; bfree = 14

Listing dir /lfs ...
/lfs/boot_count read count:0 (bytes: 0)
/lfs/boot_count write new boot count 1: [wr:1]
I: Test file: /lfs/pattern.bin not found, create one!
  1. The second run read the per-existing files:
*** Booting Zephyr OS build v4.1.0-1562-g478212efc4e1 ***
Sample program to r/w files on littlefs
Area 2 at 0x1f0000 on flash-controller@18000000 for 65536 bytes
I: LittleFS version 2.10, disk version 2.1
I: FS at flash-controller@18000000:0x1f0000 is 16 0x1000-byte blocks with 512 cycle
I: partition sizes: rd 16 ; pr 16 ; ca 64 ; la 32
/lfs mount: 0
/lfs: bsize = 16 ; frsize = 4096 ; blocks = 16 ; bfree = 13

Listing dir /lfs ...
[FILE] boot_count (size = 1)
[FILE] pattern.bin (size = 547)
/lfs/boot_count read count:1 (bytes: 1)
/lfs/boot_count write new boot count 2: [wr:1]

Copy link

Hello @Snap-A, and thank you very much for your first pull request to the Zephyr project!
Our Continuous Integration pipeline will execute a series of checks on your Pull Request commit messages and code, and you are expected to address any failures by updating the PR. Please take a look at our commit message guidelines to find out how to format your commit messages, and at our contribution workflow to understand how to update your Pull Request. If you haven't already, please make sure to review the project's Contributor Expectations and update (by amending and force-pushing the commits) your pull request if necessary.
If you are stuck or need help please join us on Discord and ask your question there. Additionally, you can escalate the review when applicable. 😊

Copy link
Contributor

@ajf58 ajf58 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Functionally this looks good. It looks like you've got a merge commit that probably doesn't want to be here, however.


code_partition: partition@100 {
label = "code-partition";
reg = <0x100 (DT_SIZE_M(2)-0x100-DT_SIZE_K(64))>;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
reg = <0x100 (DT_SIZE_M(2)-0x100-DT_SIZE_K(64))>;
reg = <0x100 (DT_SIZE_M(2) - 0x100 - DT_SIZE_K(64))>;

Is there a reason not to use single whitespace either side of binary operators here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will add the whitespaces. Did not even notice I "compacted" the text like that.


storage_partition: storage_partition@1f0000 {
label = "storage-partition";
reg = <(DT_SIZE_M(2)-DT_SIZE_K(64)) (DT_SIZE_K(64))>;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
reg = <(DT_SIZE_M(2)-DT_SIZE_K(64)) (DT_SIZE_K(64))>;
reg = <(DT_SIZE_M(2) - DT_SIZE_K(64)) (DT_SIZE_K(64))>;

Same comment as above. Is there a reason not to use single whitespace either side of binary operators here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here.

Regarding the merge commit, I had re-merged the branch before the PR creation. Did not realize this would be visible, here.

In the Contribution Guidelines, it says to use --rebase <commit>^, and then a push --force to update this PR. I will try this out, it may clean up things...

Additionally, I missed the "copyright / license" comment lines at the start of these files. Will fix that, too.

/delete-node/ &code_partition;

&flash0 {
partitions {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

dts indent is tab, and don't see why this should be an overlay when it can go in the board file itself (which would also make it usable for many other samples needing a storage partition)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will fix the tabs.
Not sure if the change is welcomed in the board file, but I'm open to adjust the PR to place it in there if people agree.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure if the change is welcomed in the board file,

I think it's a good idea.

I think my questions would be (sharing my train of thought):

  1. What's the idiomatic thing in the Zephyr codebase? A very crude search suggests, yes, a lot of boards have a storage partition defined for a good "out-of-the-box" experience.
  2. What is motivating the choice of size? > 1 flash sector seems like a minimum. Has something motivated the choice of 64kB in particular?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Regarding (2):
The choice of 64 kB gave the FS 16 blocks to work with. That seemed sufficient for the root dir and another few files (15 if each is smaller than a couple of kB in size).

Space for only two files are needed in this specific example and so it could be reduced to 16 kB (4 blocks), but the Pico has 2MB flash, so the 64 kB seem seemed to be more realistic to me.

ajf58
ajf58 previously approved these changes Apr 1, 2025
/delete-node/ &code_partition;

&flash0 {
partitions {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure if the change is welcomed in the board file,

I think it's a good idea.

I think my questions would be (sharing my train of thought):

  1. What's the idiomatic thing in the Zephyr codebase? A very crude search suggests, yes, a lot of boards have a storage partition defined for a good "out-of-the-box" experience.
  2. What is motivating the choice of size? > 1 flash sector seems like a minimum. Has something motivated the choice of 64kB in particular?

Comment on lines 21 to 22
storage_partition: storage_partition@1f0000 {
label = "storage-partition";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
storage_partition: storage_partition@1f0000 {
label = "storage-partition";
storage_partition: partition@1f0000 {
label = "storage";

Seems to be a more common idiom in the codebase: I think partitions labelled <foo>-partition is perhaps a bit redundant.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That name could be addressed if this is made a change to the Pico board dts. As an overlay, it probably should stick with the base naming scheme...

@@ -0,0 +1,7 @@
/*
* Copyright (c) 2025 Andreas Wolf <awolf002@gmai.com>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* Copyright (c) 2025 Andreas Wolf <awolf002@gmai.com>
* Copyright (c) 2025 Andreas Wolf <awolf002@gmail.com>

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixing that.

@Snap-A Snap-A force-pushed the fix_87850_littlefs_pico branch 2 times, most recently from d1a027f to 92df7d2 Compare April 5, 2025 14:08
@Snap-A
Copy link
Contributor Author

Snap-A commented Apr 5, 2025

  • Reduced the size of the partition to a 2^n number that is large enough to hold the two test files;
  • Fixed the line where I missed the white space change (' ' -> '\t')
  • Adjusted the commit message;

@Snap-A Snap-A force-pushed the fix_87850_littlefs_pico branch 2 times, most recently from 9e8bfd5 to 4dd9df1 Compare April 11, 2025 13:57
@Snap-A Snap-A force-pushed the fix_87850_littlefs_pico branch from 4dd9df1 to 43eff44 Compare May 17, 2025 15:02
@github-actions github-actions bot added the platform: Raspberry Pi Pico Raspberry Pi Pico (RPi Pico) label May 17, 2025
@github-actions github-actions bot requested review from soburi, ThreeEights and yonsch May 17, 2025 15:03
@Snap-A Snap-A changed the title Add Pico and Pico_w Board Overlay Files to littlefs Sample Application Add persistent partition to RPi Pico Flash May 17, 2025
@Snap-A
Copy link
Contributor Author

Snap-A commented May 17, 2025

I adjusted the PR to a change of the DTS include file boards/raspberrypi/rpi_pico/rpi_pico-common.dtsi and removed the added overlay files.

I documented the tests that were run with this change in an update to the initial description.

Please give feedback if this would be acceptable as a PR.

nordicjm
nordicjm previously approved these changes May 19, 2025
@nordicjm nordicjm requested a review from ajf58 May 19, 2025 08:51
@Snap-A
Copy link
Contributor Author

Snap-A commented May 19, 2025

The twister build found a problem in the sample code for USB mass storage: It also defines a "storage_partition". This will fail with this proposed change, unless it is "deleted" and re-defined in the overlay file.

I will add this change to this PR, so that the twister is "happy".

@Snap-A Snap-A force-pushed the fix_87850_littlefs_pico branch from 43eff44 to 49ef52e Compare May 19, 2025 21:11
@github-actions github-actions bot added the area: USB Universal Serial Bus label May 19, 2025
Comment on lines 96 to 98
storage_partition: partition@1fc000 {
label = "storage-partition";
reg = <(DT_SIZE_M(2) - DT_SIZE_K(16)) (DT_SIZE_K(16))>;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is fine, but the @xxxxxxxx at the end of the node path and the value at the beginning of reg must match. So, represent in the calculations make bit hard to understand.

IMO, If you want to make it easier to read, you might want to introduce

#define STORAGE_PARTITION_SIZE DT_SIZE_K(16)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will take a look at that. This should improved readability, I agree.

@de-nordic
Copy link
Contributor

Re-assigning to RPI maintainer.

@de-nordic de-nordic assigned soburi and unassigned de-nordic May 20, 2025
@Snap-A Snap-A force-pushed the fix_87850_littlefs_pico branch from 49ef52e to c79397f Compare May 20, 2025 19:03
Comment on lines 65 to 66
#define FLASH_SIZE DT_SIZE_M(2)
#define STORAGE_PARTITION_SIZE DT_SIZE_K(16)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Having a #defines in the middle of a .dtsi file doesn't seem very idiomatic. Have you come across this elsewhere in the codebase?

I appreciate what you're trying to do here, however, but I think for reasons I don't understand, this is frowned upon in the codebase.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alright, I will remove the #define ... lines and update this PR.

Add a 16 KB 'storage' partition at the end of the Pico flash.
That size is sufficient to hold the two test files created by
the 'littlefs' sample.

The sample 'subsys/USB/mass' uses an overlay to add the same
partition and it was changed to delete the one defined by this
DTSI file before doing that.

Signed-off-by: Andreas Wolf <awolf002@gmail.com>
@Snap-A Snap-A force-pushed the fix_87850_littlefs_pico branch from c79397f to 8221ed6 Compare May 23, 2025 13:00
Copy link

Copy link
Member

@soburi soburi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

#90411
This review seems to have been taken first.

#90758
If this change is also included,

will the rpi_pico-common.dtsi fix already be included?

If so, only the overlay part is needed, so please fix the patch.

@Snap-A
Copy link
Contributor Author

Snap-A commented May 29, 2025

It seems that #90411 has now added a partition to a set of DTSI files, and #90758 adds this layout to the "standard" boards/raspberrypi/rpi_pico/rpi_pico-common.dtsi file. That would make the changes to that file proposed in this PR obsolete.

Probably, the fix of the overlay file in samples/subsys/usb/mass/boards is no longer needed, since the chosen "standard" include file will have a 1 MB partition as expected by that application. So do you want me to just close/cancel this PR?

@soburi
Copy link
Member

soburi commented May 29, 2025

@Snap-A
Yes. If no additional modifications are required, I think it's best to close it.

@Snap-A
Copy link
Contributor Author

Snap-A commented May 31, 2025

I tested compiling the USB "mass storage" sample with the changes in #90758 and no further changes to the sample code.
This succeeded, and the created binary seems to successfully run on the Pico, since I see the mass storage device in the USB list:

Bus 002 Device 007: ID 2fe3:0008 NordicSemiconductor Zephyr MSC sample

I am closing this PR.

@Snap-A Snap-A closed this May 31, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area: File System area: Samples Samples area: USB Universal Serial Bus platform: Raspberry Pi Pico Raspberry Pi Pico (RPi Pico)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add Pico and Pico_w Board Overlay Files to littlefs Sample Application
5 participants