Skip to content

ports/rp2: Make linker script configurable. #17029

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

Gadgetoid
Copy link
Contributor

@Gadgetoid Gadgetoid commented Mar 28, 2025

Add MICROPY_BOARD_LINKER_SCRIPT to specify a custom linker script for RP2 boards/variants.

This may, for example, include a PSRAM region so that C buffers or otherwise can be allocated into PSRAM.

Summary

This is a much leaner version of #8761, but broadly attempts to accomplish some of the same things.

I use this feature (with some small modifications to the PSRAM code shown here: pimoroni@79d7cc2) for building Presto with some non-critical C buffers moved to PSRAM so we can fit a large framebuffer in SRAM and use the remaining PSRAM for GC heap.

This change reflects #17028 and aims to bring some edge feature which don't cause too much immediate upset to the MicroPython codebase, but which are useful to those of us pushing weird and experimental things into shipping products 😆

Testing

Currently a critical part of the Presto build process.

Trade-offs and Alternatives

As with #17028 it seems reasonable to make linker scripts configurable, however:

  1. They are complicated (they're composable but not really in a way that makes sense here)
  2. There may be precious few uses for this particular feature
  3. If the canonical linker script ever changes, then that change must be merged into any boards/variants

@Gadgetoid
Copy link
Contributor Author

Since we've had variant functionality merged since, I think this probably represents a good first step toward #8761 and, specifically, configuring flash sizes via the linker script so that Filesystem/Flash overlaps can be caught at compile time.

The blocker with flash/app sizes was an incompatibility with bi_decl which I'm duplicating in below in case anyone can shed some light on this:

  1. What can we do about bi_decl not being compatible with dynamic linker symbols? The suggestion to make the .ld file the source of truth seems reasonable, but is incompatible with Pico SDK's binary descriptor macros. Does anyone know if there's a reasonable way to work around this? - the problem code is
    // Tag the flash drive in the binary as readable/writable (but not reformatable)
    bi_decl(bi_block_device(
    BINARY_INFO_TAG_MICROPYTHON,
    "MicroPython",
    XIP_BASE + MICROPY_HW_FLASH_STORAGE_BASE,
    MICROPY_HW_FLASH_STORAGE_BYTES,
    NULL,
    BINARY_INFO_BLOCK_DEV_FLAG_READ |
    BINARY_INFO_BLOCK_DEV_FLAG_WRITE |
    BINARY_INFO_BLOCK_DEV_FLAG_PT_UNKNOWN));

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

@Gadgetoid
Copy link
Contributor Author

The more I think about this the more I think it would be ideal to get back some of what was discussed/proposed in #8761 - I need to delve into bi_decl and see if I can figure out whether it's possible to inerop with linker symbols. Could have been fixed, or could be fixed.

endif()
endif()

if (MICROPY_BOARD_LINKER_SCRIPT AND PICO_ON_DEVICE AND NOT PICO_NO_FLASH AND NOT PICO_COPY_TO_RAM)
Copy link
Member

Choose a reason for hiding this comment

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

I don't think the check for MICROPY_BOARD_LINKER_SCRIPT is needed here, it should always be valid (the above if-block makes sure of that) and we'll get an error if there's something other than RP2040 or RP2350.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oof. Agreed!

@dpgeorge
Copy link
Member

This looks fine to me, a very easy way to make the linker script configurable.

The more I think about this the more I think it would be ideal to get back some of what was discussed/proposed in #8761

That PR does have some good bits in it. It's just really complicated and touches lots of things and so requires lots of review and decision making 😅

Add MICROPY_BOARD_LINKER_SCRIPT to specify a custom linker script
for RP2 boards/variants.

This may, for example, include a PSRAM region so that C buffers
or otherwise can be allocated into PSRAM.

Signed-off-by: Phil Howard <github@gadgetoid.com>
@Gadgetoid Gadgetoid force-pushed the patch-rp2-linker-script branch from a1a6916 to 8812411 Compare June 17, 2025 14:31
@dpgeorge
Copy link
Member

Rebased and merged in c16a4db

@dpgeorge dpgeorge closed this Jun 19, 2025
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.

2 participants