Skip to content

ports/rps: Make FLASH LENGTH match PICO_FLASH_SIZE_BYTES in .ld files. #17344

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

sfe-SparkFro
Copy link
Contributor

Summary

Resolves #17343

This dynamically sets the FLASH LENGTH value in the .ld files to match PICO_FLASH_SIZE_BYTES, meaning it should match the size of the actual flash chip on whatever board is being built.

Testing

I have a project that requires over 4MB of flash for the firmware. I'm building the project for a board that has a 16MB flash chip, which is set by PICO_FLASH_SIZE_BYTES. Without these changes, I get the following error from the linker:

firmware.elf section `.text' will not fit in region `FLASH'`
region `FLASH' overflowed by xxxx bytes

After these changes, it works fine.

Trade-offs and Alternatives

I do not believe this has any tradeoffs.

However, a possible alternative is to make the FLASH LENGTH truly configurable instead of dynamically set to match the board's flash size. I don't know whether that would actually be useful for anything though, so IMO best to keep it simple.

Copy link

Code size report:

   bare-arm:    +0 +0.000% 
minimal x86:    +0 +0.000% 
   unix x64:    +0 +0.000% standard
      stm32:    +0 +0.000% PYBV10
     mimxrt:    +0 +0.000% TEENSY40
        rp2:    +0 +0.000% RPI_PICO_W
       samd:    +0 +0.000% ADAFRUIT_ITSYBITSY_M4_EXPRESS
  qemu rv32:    +0 +0.000% VIRT_RV32

Signed-off-by: Dryw Wade <dryw.wade@sparkfun.com>
@sfe-SparkFro sfe-SparkFro force-pushed the rp2_dynamic_flash_length_ld branch from d3a2e29 to 83c8f52 Compare May 22, 2025 20:53
@dpgeorge
Copy link
Member

In principle this looks reasonable, although I have two concerns:

  1. It requires board header files to correctly use pico_cmake_set_default PICO_FLASH_SIZE_BYTES, and not all of them do (the pico-sdk has some magic to grep for these strings and then expose the settings to cmake...). Eg NULLBITS_BIT_C_PRO doesn't have that definition and doesn't build with this PR.
  2. Eventually I want to auto-detect the size of the SPI flash, and allow the user to specify the size of filesystems (ROMFS, littlefs, FAT, etc) using a partition table. That might require a bit of trickery in the linker script, similar to what's done here.

(1) is pretty easy to fix for the boards in this repo (there's also POLOLU_ZUMO_2040_ROBOT, WEACTSTUDIO and WEACTSTUDIO_RP2350B_CORE to fix), although anyone with a custom board outside this repo might also need to add such a definition.

(2) is probably not an issue but worth considering.

Signed-off-by: Dryw Wade <dryw.wade@sparkfun.com>
@sfe-SparkFro
Copy link
Contributor Author

@dpgeorge How about this? If PICO_FLASH_SIZE_BYTES is not set (actually, it appears to get set to ""), then it uses the same defaults as before (2MB for RP2040 or 4MB for RP2350).

IMO the default probably doesn't need to be different between the RP2040 and RP2350, but I'm just keeping the same behavior as before.

Copy link
Member

@dpgeorge dpgeorge left a comment

Choose a reason for hiding this comment

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

Yes, this looks good now. I tested NULLBITS_BIT_C_PRO and it now builds.

@dpgeorge
Copy link
Member

Squashed, rebased and merged in 6bfb83e

@dpgeorge dpgeorge closed this May 28, 2025
@sfe-SparkFro sfe-SparkFro deleted the rp2_dynamic_flash_length_ld branch June 18, 2025 15:54
@sfe-SparkFro
Copy link
Contributor Author

Hmm, this check is not working for me:

if(PICO_FLASH_SIZE_BYTES GREATER 0)

This does seem to work though:

if(DEFINED PICO_FLASH_SIZE_BYTES)

This statement:

If PICO_FLASH_SIZE_BYTES is not set (actually, it appears to get set to "")

appears to be wrong. Not sure if something changed since I last looked at this, but I tested a few boards, and if(DEFINED PICO_FLASH_SIZE_BYTES) seems to work fine.

I didn't catch this until now, because my project's firmware size dropped below 4MB shortly after this PR, and only just came back up over 4MB.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

ports/rp2: Make FLASH length configurable in linker script.
2 participants