From bd61d0ce566ad32986cca6dde697aefc6c20e866 Mon Sep 17 00:00:00 2001 From: Phil Howard Date: Mon, 31 Mar 2025 17:44:37 +0100 Subject: [PATCH 1/2] ports/rp2: Per-board linker flash config. Add per-board flash config to avoid silently overwriting user code on vfs init. Make linker FLASH section reflect board size. Add linker APP section for user code. Add linker FILESYSTEM section for vfs. Add per-board config for FLASH + APP sizes: * MICROPY_HW_FLASH_SIZE_BYTES * MICROPY_HW_APP_SIZE_BYTES CMake "configure_file" memmap_mp.ld.in. Signed-off-by: Phil Howard --- ports/rp2/CMakeLists.txt | 23 ++++++-- ports/rp2/boards/RPI_PICO/mpconfigboard.cmake | 3 ++ ports/rp2/boards/RPI_PICO/mpconfigboard.h | 1 - .../rp2/boards/RPI_PICO2/mpconfigboard.cmake | 3 ++ ports/rp2/boards/RPI_PICO2/mpconfigboard.h | 1 - ports/rp2/boards/RPI_PICO2_W/mpconfigboard.h | 1 - .../boards/RPI_PICO2_W/mpconfigvariant.cmake | 3 ++ .../RPI_PICO2_W/mpconfigvariant_RISCV.cmake | 3 ++ .../rp2/boards/RPI_PICO_W/mpconfigboard.cmake | 3 ++ ports/rp2/boards/RPI_PICO_W/mpconfigboard.h | 3 -- ...ap_mp_rp2040.ld => memmap_mp_rp2040.ld.in} | 52 ++++++++++++------ ...ap_mp_rp2350.ld => memmap_mp_rp2350.ld.in} | 54 +++++++++++++------ ports/rp2/rp2_flash.c | 52 +++++++++--------- 13 files changed, 135 insertions(+), 67 deletions(-) rename ports/rp2/{memmap_mp_rp2040.ld => memmap_mp_rp2040.ld.in} (75%) rename ports/rp2/{memmap_mp_rp2350.ld => memmap_mp_rp2350.ld.in} (79%) diff --git a/ports/rp2/CMakeLists.txt b/ports/rp2/CMakeLists.txt index f89e2792c6479..3c0770c31987d 100644 --- a/ports/rp2/CMakeLists.txt +++ b/ports/rp2/CMakeLists.txt @@ -549,6 +549,10 @@ set_source_files_properties( COMPILE_OPTIONS "-Wno-error=array-bounds;-Wno-error=unused-but-set-variable" ) +if(NOT MICROPY_HW_ROMFS_SIZE_BYTES) + set(MICROPY_HW_ROMFS_SIZE_BYTES 0) +endif() + target_compile_definitions(${MICROPY_TARGET} PRIVATE ${MICROPY_DEF_BOARD} FFCONF_H=\"${MICROPY_OOFATFS_DIR}/ffconf.h\" @@ -562,6 +566,9 @@ target_compile_definitions(${MICROPY_TARGET} PRIVATE PICO_NO_PROGRAM_VERSION_STRING=1 # do it ourselves in main.c MICROPY_BUILD_TYPE="${CMAKE_C_COMPILER_ID} ${CMAKE_C_COMPILER_VERSION} ${CMAKE_BUILD_TYPE}" PICO_NO_BI_STDIO_UART=1 # we call it UART REPL + MICROPY_HW_FLASH_SIZE_BYTES=${MICROPY_HW_FLASH_SIZE_BYTES} + MICROPY_HW_APP_SIZE_BYTES=${MICROPY_HW_APP_SIZE_BYTES} + MICROPY_HW_ROMFS_SIZE_BYTES=${MICROPY_HW_ROMFS_SIZE_BYTES} ) if(PICO_RP2040) @@ -588,14 +595,24 @@ endif() # todo this is a bit brittle, but we want to move a few source files into RAM (which requires # a linker script modification) until we explicitly add macro calls around the function # defs to move them into RAM. -if (PICO_ON_DEVICE AND NOT PICO_NO_FLASH AND NOT PICO_COPY_TO_RAM) +if (NOT MICROPY_BOARD_LINKER_SCRIPT) if(PICO_RP2040) - pico_set_linker_script(${MICROPY_TARGET} ${CMAKE_CURRENT_LIST_DIR}/memmap_mp_rp2040.ld) + set(MICROPY_BOARD_LINKER_SCRIPT ${CMAKE_CURRENT_LIST_DIR}/memmap_mp_rp2040.ld.in) elseif(PICO_RP2350) - pico_set_linker_script(${MICROPY_TARGET} ${CMAKE_CURRENT_LIST_DIR}/memmap_mp_rp2350.ld) + set(MICROPY_BOARD_LINKER_SCRIPT ${CMAKE_CURRENT_LIST_DIR}/memmap_mp_rp2350.ld.in) endif() endif() +# Parse the linker file if needed, replacing ${MICROPY_HW_FLASH_SIZE_BYTES}, etc with their values +if(MICROPY_BOARD_LINKER_SCRIPT MATCHES "\.in$") + configure_file(${MICROPY_BOARD_LINKER_SCRIPT} ${CMAKE_CURRENT_BINARY_DIR}/memmap_mp.ld) + set(MICROPY_BOARD_LINKER_SCRIPT ${CMAKE_CURRENT_BINARY_DIR}/memmap_mp.ld) +endif() + +if (MICROPY_BOARD_LINKER_SCRIPT AND PICO_ON_DEVICE AND NOT PICO_NO_FLASH AND NOT PICO_COPY_TO_RAM) + pico_set_linker_script(${MICROPY_TARGET} ${MICROPY_BOARD_LINKER_SCRIPT}) +endif() + pico_add_extra_outputs(${MICROPY_TARGET}) pico_find_compiler_with_triples(PICO_COMPILER_SIZE "${PICO_GCC_TRIPLE}" size) diff --git a/ports/rp2/boards/RPI_PICO/mpconfigboard.cmake b/ports/rp2/boards/RPI_PICO/mpconfigboard.cmake index 386bd33285890..e4181bc50bc98 100644 --- a/ports/rp2/boards/RPI_PICO/mpconfigboard.cmake +++ b/ports/rp2/boards/RPI_PICO/mpconfigboard.cmake @@ -1,3 +1,6 @@ # cmake file for Raspberry Pi Pico set(PICO_BOARD "pico") set(PICO_PLATFORM "rp2040") + +set(MICROPY_HW_FLASH_SIZE_BYTES "2 * 1024 * 1024") +set(MICROPY_HW_APP_SIZE_BYTES "640 * 1024") \ No newline at end of file diff --git a/ports/rp2/boards/RPI_PICO/mpconfigboard.h b/ports/rp2/boards/RPI_PICO/mpconfigboard.h index c39bc4d489bb6..c9e92c44c3139 100644 --- a/ports/rp2/boards/RPI_PICO/mpconfigboard.h +++ b/ports/rp2/boards/RPI_PICO/mpconfigboard.h @@ -1,3 +1,2 @@ // Board and hardware specific configuration #define MICROPY_HW_BOARD_NAME "Raspberry Pi Pico" -#define MICROPY_HW_FLASH_STORAGE_BYTES (1408 * 1024) diff --git a/ports/rp2/boards/RPI_PICO2/mpconfigboard.cmake b/ports/rp2/boards/RPI_PICO2/mpconfigboard.cmake index 48b6545aa3428..f31f02bf85513 100644 --- a/ports/rp2/boards/RPI_PICO2/mpconfigboard.cmake +++ b/ports/rp2/boards/RPI_PICO2/mpconfigboard.cmake @@ -1,5 +1,8 @@ # cmake file for Raspberry Pi Pico2 set(PICO_BOARD "pico2") +set(MICROPY_HW_FLASH_SIZE_BYTES "4 * 1024 * 1024") +set(MICROPY_HW_APP_SIZE_BYTES "2560 * 1024") + # To change the gpio count for QFN-80 # set(PICO_NUM_GPIOS 48) diff --git a/ports/rp2/boards/RPI_PICO2/mpconfigboard.h b/ports/rp2/boards/RPI_PICO2/mpconfigboard.h index 4b5eac6eb3ae3..c0ab8e77b091b 100644 --- a/ports/rp2/boards/RPI_PICO2/mpconfigboard.h +++ b/ports/rp2/boards/RPI_PICO2/mpconfigboard.h @@ -1,3 +1,2 @@ // Board and hardware specific configuration #define MICROPY_HW_BOARD_NAME "Raspberry Pi Pico2" -#define MICROPY_HW_FLASH_STORAGE_BYTES (PICO_FLASH_SIZE_BYTES - 1024 * 1024) diff --git a/ports/rp2/boards/RPI_PICO2_W/mpconfigboard.h b/ports/rp2/boards/RPI_PICO2_W/mpconfigboard.h index fe688c2803192..e47091768ff37 100644 --- a/ports/rp2/boards/RPI_PICO2_W/mpconfigboard.h +++ b/ports/rp2/boards/RPI_PICO2_W/mpconfigboard.h @@ -1,6 +1,5 @@ // Board and hardware specific configuration #define MICROPY_HW_BOARD_NAME "Raspberry Pi Pico 2 W" -#define MICROPY_HW_FLASH_STORAGE_BYTES (PICO_FLASH_SIZE_BYTES - 1536 * 1024) // Enable networking. #define MICROPY_PY_NETWORK 1 diff --git a/ports/rp2/boards/RPI_PICO2_W/mpconfigvariant.cmake b/ports/rp2/boards/RPI_PICO2_W/mpconfigvariant.cmake index 6fe039ba51bba..416d0dc4d036a 100644 --- a/ports/rp2/boards/RPI_PICO2_W/mpconfigvariant.cmake +++ b/ports/rp2/boards/RPI_PICO2_W/mpconfigvariant.cmake @@ -1 +1,4 @@ set(PICO_PLATFORM "rp2350") + +set(MICROPY_HW_FLASH_SIZE_BYTES "4 * 1024 * 1024") +set(MICROPY_HW_APP_SIZE_BYTES "2560 * 1024") diff --git a/ports/rp2/boards/RPI_PICO2_W/mpconfigvariant_RISCV.cmake b/ports/rp2/boards/RPI_PICO2_W/mpconfigvariant_RISCV.cmake index 65a97fc3350d1..39c56984b87fe 100644 --- a/ports/rp2/boards/RPI_PICO2_W/mpconfigvariant_RISCV.cmake +++ b/ports/rp2/boards/RPI_PICO2_W/mpconfigvariant_RISCV.cmake @@ -1 +1,4 @@ set(PICO_PLATFORM "rp2350-riscv") + +set(MICROPY_HW_FLASH_SIZE_BYTES "4 * 1024 * 1024") +set(MICROPY_HW_APP_SIZE_BYTES "2560 * 1024") diff --git a/ports/rp2/boards/RPI_PICO_W/mpconfigboard.cmake b/ports/rp2/boards/RPI_PICO_W/mpconfigboard.cmake index cea6c38d73b4e..328aef73c3b94 100644 --- a/ports/rp2/boards/RPI_PICO_W/mpconfigboard.cmake +++ b/ports/rp2/boards/RPI_PICO_W/mpconfigboard.cmake @@ -2,6 +2,9 @@ set(PICO_BOARD "pico_w") +set(MICROPY_HW_FLASH_SIZE_BYTES "2 * 1024 * 1024") +set(MICROPY_HW_APP_SIZE_BYTES "1200 * 1024") + set(MICROPY_PY_LWIP ON) set(MICROPY_PY_NETWORK_CYW43 ON) diff --git a/ports/rp2/boards/RPI_PICO_W/mpconfigboard.h b/ports/rp2/boards/RPI_PICO_W/mpconfigboard.h index 45ad6107ba14e..2ed2bd1be96ec 100644 --- a/ports/rp2/boards/RPI_PICO_W/mpconfigboard.h +++ b/ports/rp2/boards/RPI_PICO_W/mpconfigboard.h @@ -1,9 +1,6 @@ // Board and hardware specific configuration #define MICROPY_HW_BOARD_NAME "Raspberry Pi Pico W" -// todo: We need something to check our binary size -#define MICROPY_HW_FLASH_STORAGE_BYTES (848 * 1024) - // Enable networking. #define MICROPY_PY_NETWORK 1 #define MICROPY_PY_NETWORK_HOSTNAME_DEFAULT "PicoW" diff --git a/ports/rp2/memmap_mp_rp2040.ld b/ports/rp2/memmap_mp_rp2040.ld.in similarity index 75% rename from ports/rp2/memmap_mp_rp2040.ld rename to ports/rp2/memmap_mp_rp2040.ld.in index a5799cd88be0e..b78f06fd394ee 100644 --- a/ports/rp2/memmap_mp_rp2040.ld +++ b/ports/rp2/memmap_mp_rp2040.ld.in @@ -21,12 +21,32 @@ __stack (== StackTop) */ +/* Default RP2 flash size. Assumes no user filesystem. */ +_micropy_hw_internal_flash_storage_start = 0x10000000; +_micropy_hw_internal_flash_storage_size_bytes = ${MICROPY_HW_FLASH_SIZE_BYTES}; +_micropy_hw_internal_flash_storage_end = _micropy_hw_internal_flash_storage_start + _micropy_hw_internal_flash_storage_size_bytes; + +_micropy_hw_application_start = _micropy_hw_internal_flash_storage_start; +_micropy_hw_application_size_bytes = ${MICROPY_HW_APP_SIZE_BYTES}; +_micropy_hw_application_end = _micropy_hw_application_start + _micropy_hw_application_size_bytes; + +_micropy_hw_romfs_start = _micropy_hw_internal_flash_storage_start + _micropy_hw_application_size_bytes; +_micropy_hw_romfs_size_bytes = ${MICROPY_HW_ROMFS_SIZE_BYTES}; +_micropy_hw_romfs_end = _micropy_hw_romfs_start + _micropy_hw_romfs_size_bytes; + +_micropy_hw_filesystem_start = _micropy_hw_internal_flash_storage_start + _micropy_hw_application_size_bytes + _micropy_hw_romfs_size_bytes; +_micropy_hw_filesystem_size_bytes = _micropy_hw_internal_flash_storage_size_bytes - _micropy_hw_application_size_bytes - _micropy_hw_romfs_size_bytes; +_micropy_hw_filesystem_end = _micropy_hw_filesystem_start + _micropy_hw_filesystem_size_bytes; + MEMORY { - FLASH(rx) : ORIGIN = 0x10000000, LENGTH = 2048k - RAM(rwx) : ORIGIN = 0x20000000, LENGTH = 256k - SCRATCH_X(rwx) : ORIGIN = 0x20040000, LENGTH = 4k - SCRATCH_Y(rwx) : ORIGIN = 0x20041000, LENGTH = 4k + FLASH(rx) : ORIGIN = _micropy_hw_internal_flash_storage_start, LENGTH = _micropy_hw_internal_flash_storage_size_bytes + APP(rx) : ORIGIN = _micropy_hw_application_start, LENGTH = _micropy_hw_application_size_bytes + ROMFS(rx) : ORIGIN = _micropy_hw_romfs_start, LENGTH = _micropy_hw_romfs_size_bytes + FILESYSTEM(r) : ORIGIN = _micropy_hw_filesystem_start, LENGTH = _micropy_hw_filesystem_size_bytes + RAM(rwx) : ORIGIN = 0x20000000, LENGTH = 256k + SCRATCH_X(rwx) : ORIGIN = 0x20040000, LENGTH = 4k + SCRATCH_Y(rwx) : ORIGIN = 0x20041000, LENGTH = 4k } ENTRY(_entry_point) @@ -40,13 +60,13 @@ SECTIONS .flash_begin : { __flash_binary_start = .; - } > FLASH + } > APP .boot2 : { __boot2_start__ = .; KEEP (*(.boot2)) __boot2_end__ = .; - } > FLASH + } > APP ASSERT(__boot2_end__ - __boot2_start__ == 256, "ERROR: Pico second stage bootloader must be 256 bytes in size") @@ -66,7 +86,7 @@ SECTIONS KEEP (*(.reset)) /* TODO revisit this now memset/memcpy/float in ROM */ /* bit of a hack right now to exclude all floating point and time critical (e.g. memset, memcpy) code from - * FLASH ... we will include any thing excluded here in .data below by default */ + * flash ... we will include any thing excluded here in .data below by default */ *(.init) /* Change for MicroPython... exclude gc.c, parse.c, vm.c from flash */ *(EXCLUDE_FILE(*libgcc.a: *libc.a: *lib_a-mem*.o *libm.a: *gc.c.obj *vm.c.obj *parse.c.obj) .text*) @@ -86,25 +106,25 @@ SECTIONS *(.eh_frame*) . = ALIGN(4); - } > FLASH + } > APP .rodata : { *(EXCLUDE_FILE(*libgcc.a: *libc.a:*lib_a-mem*.o *libm.a:) .rodata*) . = ALIGN(4); *(SORT_BY_ALIGNMENT(SORT_BY_NAME(.flashdata*))) . = ALIGN(4); - } > FLASH + } > APP .ARM.extab : { *(.ARM.extab* .gnu.linkonce.armextab.*) - } > FLASH + } > APP __exidx_start = .; .ARM.exidx : { *(.ARM.exidx* .gnu.linkonce.armexidx.*) - } > FLASH + } > APP __exidx_end = .; /* Machine inspectable binary information */ @@ -114,7 +134,7 @@ SECTIONS { KEEP(*(.binary_info.keep.*)) *(.binary_info.*) - } > FLASH + } > APP __binary_info_end = .; . = ALIGN(4); @@ -173,7 +193,7 @@ SECTIONS . = ALIGN(4); /* All data end */ __data_end__ = .; - } > RAM AT> FLASH + } > RAM AT> APP .uninitialized_data (COPY): { . = ALIGN(4); @@ -192,7 +212,7 @@ SECTIONS *(.scratch_x.*) . = ALIGN(4); __scratch_x_end__ = .; - } > SCRATCH_X AT > FLASH + } > SCRATCH_X AT > APP __scratch_x_source__ = LOADADDR(.scratch_x); .scratch_y : { @@ -200,7 +220,7 @@ SECTIONS *(.scratch_y.*) . = ALIGN(4); __scratch_y_end__ = .; - } > SCRATCH_Y AT > FLASH + } > SCRATCH_Y AT > APP __scratch_y_source__ = LOADADDR(.scratch_y); .bss : { @@ -240,7 +260,7 @@ SECTIONS .flash_end : { __flash_binary_end = .; - } > FLASH + } > APP /* stack limit is poorly named, but historically is maximum heap ptr */ __StackLimit = __bss_end__ + __micropy_c_heap_size__; diff --git a/ports/rp2/memmap_mp_rp2350.ld b/ports/rp2/memmap_mp_rp2350.ld.in similarity index 79% rename from ports/rp2/memmap_mp_rp2350.ld rename to ports/rp2/memmap_mp_rp2350.ld.in index 1e1cbbfd70296..08d6afd614c70 100644 --- a/ports/rp2/memmap_mp_rp2350.ld +++ b/ports/rp2/memmap_mp_rp2350.ld.in @@ -21,12 +21,32 @@ __stack (== StackTop) */ +/* Default RP2 flash size. Assumes no user filesystem. */ +_micropy_hw_internal_flash_storage_start = 0x10000000; +_micropy_hw_internal_flash_storage_size_bytes = ${MICROPY_HW_FLASH_SIZE_BYTES}; +_micropy_hw_internal_flash_storage_end = _micropy_hw_internal_flash_storage_start + _micropy_hw_internal_flash_storage_size_bytes; + +_micropy_hw_application_start = _micropy_hw_internal_flash_storage_start; +_micropy_hw_application_size_bytes = ${MICROPY_HW_APP_SIZE_BYTES}; +_micropy_hw_application_end = _micropy_hw_application_start + _micropy_hw_application_size_bytes; + +_micropy_hw_romfs_start = _micropy_hw_internal_flash_storage_start + _micropy_hw_application_size_bytes; +_micropy_hw_romfs_size_bytes = ${MICROPY_HW_ROMFS_SIZE_BYTES}; +_micropy_hw_romfs_end = _micropy_hw_romfs_start + _micropy_hw_romfs_size_bytes; + +_micropy_hw_filesystem_start = _micropy_hw_internal_flash_storage_start + _micropy_hw_application_size_bytes + _micropy_hw_romfs_size_bytes; +_micropy_hw_filesystem_size_bytes = _micropy_hw_internal_flash_storage_size_bytes - _micropy_hw_application_size_bytes - _micropy_hw_romfs_size_bytes; +_micropy_hw_filesystem_end = _micropy_hw_filesystem_start + _micropy_hw_filesystem_size_bytes; + MEMORY { - FLASH(rx) : ORIGIN = 0x10000000, LENGTH = 4096k - RAM(rwx) : ORIGIN = 0x20000000, LENGTH = 512k - SCRATCH_X(rwx) : ORIGIN = 0x20080000, LENGTH = 4k - SCRATCH_Y(rwx) : ORIGIN = 0x20081000, LENGTH = 4k + FLASH(rx) : ORIGIN = _micropy_hw_internal_flash_storage_start, LENGTH = _micropy_hw_internal_flash_storage_size_bytes + APP(rx) : ORIGIN = _micropy_hw_application_start, LENGTH = _micropy_hw_application_size_bytes + ROMFS(rx) : ORIGIN = _micropy_hw_romfs_start, LENGTH = _micropy_hw_romfs_size_bytes + FILESYSTEM(r) : ORIGIN = _micropy_hw_filesystem_start, LENGTH = _micropy_hw_filesystem_size_bytes + RAM(rwx) : ORIGIN = 0x20000000, LENGTH = 512k + SCRATCH_X(rwx) : ORIGIN = 0x20080000, LENGTH = 4k + SCRATCH_Y(rwx) : ORIGIN = 0x20081000, LENGTH = 4k } ENTRY(_entry_point) @@ -35,7 +55,7 @@ SECTIONS { .flash_begin : { __flash_binary_start = .; - } > FLASH + } > APP /* The bootrom will enter the image at the point indicated in your IMAGE_DEF, which is usually the reset handler of your vector table. @@ -57,7 +77,7 @@ SECTIONS KEEP (*(.reset)) /* TODO revisit this now memset/memcpy/float in ROM */ /* bit of a hack right now to exclude all floating point and time critical (e.g. memset, memcpy) code from - * FLASH ... we will include any thing excluded here in .data below by default */ + * flash ... we will include any thing excluded here in .data below by default */ *(.init) *libgcc.a:cmse_nonsecure_call.o /* Change for MicroPython... exclude gc.c, parse.c, vm.c from flash */ @@ -98,7 +118,7 @@ SECTIONS PROVIDE_HIDDEN (__fini_array_end = .); *(.eh_frame*) . = ALIGN(4); - } > FLASH + } > APP /* Note the boot2 section is optional, and should be discarded if there is no reference to it *inside* the binary, as it is not called by the @@ -116,7 +136,7 @@ SECTIONS __boot2_start__ = .; *(.boot2) __boot2_end__ = .; - } > FLASH + } > APP ASSERT(__boot2_end__ - __boot2_start__ <= 256, "ERROR: Pico second stage bootloader must be no more than 256 bytes in size") @@ -127,18 +147,18 @@ SECTIONS . = ALIGN(4); *(SORT_BY_ALIGNMENT(SORT_BY_NAME(.flashdata*))) . = ALIGN(4); - } > FLASH + } > APP .ARM.extab : { *(.ARM.extab* .gnu.linkonce.armextab.*) - } > FLASH + } > APP __exidx_start = .; .ARM.exidx : { *(.ARM.exidx* .gnu.linkonce.armexidx.*) - } > FLASH + } > APP __exidx_end = .; /* Machine inspectable binary information */ @@ -148,7 +168,7 @@ SECTIONS { KEEP(*(.binary_info.keep.*)) *(.binary_info.*) - } > FLASH + } > APP __binary_info_end = .; . = ALIGN(4); @@ -187,14 +207,14 @@ SECTIONS *(.jcr) . = ALIGN(4); - } > RAM AT> FLASH + } > RAM AT> APP .tdata : { . = ALIGN(4); *(.tdata .tdata.* .gnu.linkonce.td.*) /* All data end */ __tdata_end = .; - } > RAM AT> FLASH + } > RAM AT> APP PROVIDE(__data_end__ = .); /* __etext is (for backwards compatibility) the name of the .data init source pointer (...) */ @@ -240,7 +260,7 @@ SECTIONS *(.scratch_x.*) . = ALIGN(4); __scratch_x_end__ = .; - } > SCRATCH_X AT > FLASH + } > SCRATCH_X AT > APP __scratch_x_source__ = LOADADDR(.scratch_x); .scratch_y : { @@ -248,7 +268,7 @@ SECTIONS *(.scratch_y.*) . = ALIGN(4); __scratch_y_end__ = .; - } > SCRATCH_Y AT > FLASH + } > SCRATCH_Y AT > APP __scratch_y_source__ = LOADADDR(.scratch_y); /* .stack*_dummy section doesn't contains any symbols. It is only @@ -272,7 +292,7 @@ SECTIONS .flash_end : { KEEP(*(.embedded_end_block*)) PROVIDE(__flash_binary_end = .); - } > FLASH =0xaa + } > APP =0xaa /* stack limit is poorly named, but historically is maximum heap ptr */ __StackLimit = __bss_end__ + __micropy_c_heap_size__; diff --git a/ports/rp2/rp2_flash.c b/ports/rp2/rp2_flash.c index f3f154a1402d9..a5e1c7d6bf6da 100644 --- a/ports/rp2/rp2_flash.c +++ b/ports/rp2/rp2_flash.c @@ -36,18 +36,21 @@ #define BLOCK_SIZE_BYTES (FLASH_SECTOR_SIZE) -static_assert(MICROPY_HW_ROMFS_BYTES % 4096 == 0, "ROMFS size must be a multiple of 4K"); -static_assert(MICROPY_HW_FLASH_STORAGE_BYTES % 4096 == 0, "Flash storage size must be a multiple of 4K"); +extern uint8_t _micropy_hw_internal_flash_storage_start; +extern size_t _micropy_hw_internal_flash_storage_size_bytes; +extern uint8_t _micropy_hw_internal_flash_storage_end; -#ifndef MICROPY_HW_FLASH_STORAGE_BASE -#define MICROPY_HW_FLASH_STORAGE_BASE (PICO_FLASH_SIZE_BYTES - MICROPY_HW_FLASH_STORAGE_BYTES) -#endif +extern uint8_t _micropy_hw_application_start; +extern size_t _micropy_hw_application_size_bytes; +extern uint8_t _micropy_hw_application_end; -// Put ROMFS at the upper end of the code space. -#define MICROPY_HW_ROMFS_BASE (MICROPY_HW_FLASH_STORAGE_BASE - MICROPY_HW_ROMFS_BYTES) +extern uint8_t _micropy_hw_romfs_start; +extern size_t _micropy_hw_romfs_size_bytes; +extern uint8_t _micropy_hw_romfs_end; -static_assert(MICROPY_HW_FLASH_STORAGE_BYTES <= PICO_FLASH_SIZE_BYTES, "MICROPY_HW_FLASH_STORAGE_BYTES too big"); -static_assert(MICROPY_HW_FLASH_STORAGE_BASE + MICROPY_HW_FLASH_STORAGE_BYTES <= PICO_FLASH_SIZE_BYTES, "MICROPY_HW_FLASH_STORAGE_BYTES too big"); +extern uint8_t _micropy_hw_filesystem_start; +extern size_t _micropy_hw_filesystem_size_bytes; +extern uint8_t _micropy_hw_filesystem_end; typedef struct _rp2_flash_obj_t { mp_obj_base_t base; @@ -57,24 +60,22 @@ typedef struct _rp2_flash_obj_t { #if MICROPY_HW_ROMFS_BYTES > 0 static rp2_flash_obj_t rp2_flash_romfs_obj = { - .base = { &rp2_flash_type }, - .flash_base = MICROPY_HW_ROMFS_BASE, - .flash_size = MICROPY_HW_ROMFS_BYTES, + .base = { &rp2_flash_type } }; #endif static rp2_flash_obj_t rp2_flash_obj = { - .base = { &rp2_flash_type }, - .flash_base = MICROPY_HW_FLASH_STORAGE_BASE, - .flash_size = MICROPY_HW_FLASH_STORAGE_BYTES, + .base = { &rp2_flash_type } }; // 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, + // TODO: Linker symbols are not const at compile time so we can't use them here + // is there an alternative? + XIP_BASE + MICROPY_HW_FLASH_SIZE_BYTES - MICROPY_HW_APP_SIZE_BYTES, + MICROPY_HW_FLASH_SIZE_BYTES, NULL, BINARY_INFO_BLOCK_DEV_FLAG_READ | BINARY_INFO_BLOCK_DEV_FLAG_WRITE | @@ -117,12 +118,9 @@ static mp_obj_t rp2_flash_make_new(const mp_obj_type_t *type, size_t n_args, siz mp_arg_parse_all_kw_array(n_args, n_kw, all_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args); if (args[ARG_start].u_int == -1 && args[ARG_len].u_int == -1) { - #ifndef NDEBUG - extern char __flash_binary_end; - assert((uintptr_t)&__flash_binary_end - XIP_BASE <= MICROPY_HW_FLASH_STORAGE_BASE); - #endif - // Default singleton object that accesses entire flash + rp2_flash_obj.flash_base = _micropy_hw_filesystem_start; + rp2_flash_obj.flash_size = _micropy_hw_filesystem_size_bytes; return MP_OBJ_FROM_PTR(&rp2_flash_obj); } @@ -131,18 +129,18 @@ static mp_obj_t rp2_flash_make_new(const mp_obj_type_t *type, size_t n_args, siz mp_int_t start = args[ARG_start].u_int; if (start == -1) { start = 0; - } else if (!(0 <= start && start < MICROPY_HW_FLASH_STORAGE_BYTES && start % BLOCK_SIZE_BYTES == 0)) { + } else if (!(0 <= start && start < _micropy_hw_filesystem_size_bytes && start % BLOCK_SIZE_BYTES == 0)) { mp_raise_ValueError(NULL); } mp_int_t len = args[ARG_len].u_int; if (len == -1) { len = MICROPY_HW_FLASH_STORAGE_BYTES - start; - } else if (!(0 < len && start + len <= MICROPY_HW_FLASH_STORAGE_BYTES && len % BLOCK_SIZE_BYTES == 0)) { + } else if (!(0 < len && start + len <= _micropy_hw_filesystem_size_bytes && len % BLOCK_SIZE_BYTES == 0)) { mp_raise_ValueError(NULL); } - self->flash_base = MICROPY_HW_FLASH_STORAGE_BASE + start; + self->flash_base = _micropy_hw_filesystem_start + start; self->flash_size = len; return MP_OBJ_FROM_PTR(self); @@ -252,6 +250,10 @@ mp_obj_t mp_vfs_rom_ioctl(size_t n_args, const mp_obj_t *args) { case MP_VFS_ROM_IOCTL_GET_NUMBER_OF_SEGMENTS: return MP_OBJ_NEW_SMALL_INT(1); case MP_VFS_ROM_IOCTL_GET_SEGMENT: + // TODO: Since linker symbols aren't known at compile time, we need + // to set these values at runtime. Find a better place to hang this. + rp2_flash_romfs_obj.flash_base = _micropy_hw_romfs_start; + rp2_flash_romfs_obj.flash_size = _micropy_hw_romfs_size_bytes; return MP_OBJ_FROM_PTR(&rp2_flash_romfs_obj); #endif default: From 780ac229f9fc6e1fe861cf2b3037dd2318f09b81 Mon Sep 17 00:00:00 2001 From: Phil Howard Date: Mon, 31 Mar 2025 17:46:43 +0100 Subject: [PATCH 2/2] ports/rp2: Add print-memory-usage. Add --print-memory-usage to print memory usage at the end of a build. Shows used/remaining app storage. Signed-off-by: Phil Howard --- ports/rp2/CMakeLists.txt | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/ports/rp2/CMakeLists.txt b/ports/rp2/CMakeLists.txt index 3c0770c31987d..c3c8331ed9634 100644 --- a/ports/rp2/CMakeLists.txt +++ b/ports/rp2/CMakeLists.txt @@ -585,6 +585,10 @@ target_link_libraries(${MICROPY_TARGET} ${PICO_SDK_COMPONENTS} ) +target_link_options(${MICROPY_TARGET} PRIVATE + -Wl,--print-memory-usage +) + if (MICROPY_HW_ENABLE_DOUBLE_TAP) # Enable double tap reset into bootrom. target_link_libraries(${MICROPY_TARGET} pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy