Skip to content

Commit b2dba0e

Browse files
committed
Add pico_set_modified_binary_type function
Allows creating binaries using an existing binary type & linker script, but with modified RAM/SCRATCH addresses For example, to only use SRAM1 so you can power down SRAM0 in your binary you could use `pico_set_modified_binary_type(<my_exe> no_flash RAM "0x20040000" "256k"`
1 parent 299fb7d commit b2dba0e

File tree

2 files changed

+50
-0
lines changed

2 files changed

+50
-0
lines changed

src/rp2_common/pico_standard_link/CMakeLists.txt

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,51 @@ if (NOT TARGET pico_standard_link)
7878
set_target_properties(${TARGET} PROPERTIES PICO_TARGET_BINARY_TYPE ${TYPE})
7979
endfunction()
8080

81+
function(pico_set_modified_binary_type TARGET TYPE)
82+
set(multiValueArgs RAM SCRATCH_X SCRATCH_Y)
83+
cmake_parse_arguments(PARSE_ARGV 0 args
84+
"" "" "${multiValueArgs}"
85+
)
86+
87+
pico_set_binary_type(${TARGET} ${TYPE})
88+
89+
# Scripts that will be created by this function
90+
set(LINKER_CMAKE_SCRIPT "${CMAKE_CURRENT_BINARY_DIR}/memmap_${TARGET}.cmake")
91+
set(LINKER_LD_SCRIPT "${CMAKE_CURRENT_BINARY_DIR}/memmap_${TARGET}.ld")
92+
93+
# Configure memmap_${TARGET}.cmake script file to create memmap_${TARGET}.ld
94+
if (args_RAM)
95+
list(POP_FRONT args_RAM ORIGIN LENGTH)
96+
set(RAM "string(REGEX REPLACE \"RAM\\\\(rwx\\\\) *: *ORIGIN *= *[0-9xX]*, *LENGTH *= *[0-9kKmM]*\\n\" \"RAM(rwx) : ORIGIN = ${ORIGIN}, LENGTH = ${LENGTH}\\n\" LINKER_SCRIPT \"\${LINKER_SCRIPT}\")\n")
97+
endif()
98+
if (args_SCRATCH_X)
99+
list(POP_FRONT args_SCRATCH_X ORIGIN LENGTH)
100+
set(SCRATCH_X "string(REGEX REPLACE \"SCRATCH_X\\\\(rwx\\\\) *: *ORIGIN *= *[0-9xX]*, *LENGTH *= *[0-9kKmM]*\\n\" \"SCRATCH_X(rwx) : ORIGIN = ${ORIGIN}, LENGTH = ${LENGTH}\\n\" LINKER_SCRIPT \"\${LINKER_SCRIPT}\")\n")
101+
endif()
102+
if (args_SCRATCH_Y)
103+
list(POP_FRONT args_SCRATCH_Y ORIGIN LENGTH)
104+
set(SCRATCH_Y "string(REGEX REPLACE \"SCRATCH_Y\\\\(rwx\\\\) *: *ORIGIN *= *[0-9xX]*, *LENGTH *= *[0-9kKmM]*\\n\" \"SCRATCH_Y(rwx) : ORIGIN = ${ORIGIN}, LENGTH = ${LENGTH}\\n\" LINKER_SCRIPT \"\${LINKER_SCRIPT}\")\n")
105+
endif()
106+
if (${CMAKE_VERSION} VERSION_GREATER_EQUAL "3.17.0")
107+
# CMAKE_CURRENT_FUNCTION_LIST_DIR added in 3.17.0
108+
configure_file(${CMAKE_CURRENT_FUNCTION_LIST_DIR}/memmap_script.template.cmake ${LINKER_CMAKE_SCRIPT} @ONLY)
109+
else()
110+
configure_file(${PICO_SDK_PATH}src/rp2_common/pico_standard_link/memmap_script.template.cmake ${LINKER_CMAKE_SCRIPT} @ONLY)
111+
endif()
112+
113+
# Add command to run this script whenever it or memmap_${TYPE}.ld changes
114+
add_custom_command(OUTPUT ${LINKER_LD_SCRIPT}
115+
COMMAND ${CMAKE_COMMAND}
116+
-DPICO_LINKER_SCRIPT_PATH:PATH=${PICO_LINKER_SCRIPT_PATH}
117+
-Doutput_file:FILEPATH=${LINKER_LD_SCRIPT}
118+
-P "${LINKER_CMAKE_SCRIPT}"
119+
DEPENDS ${PICO_LINKER_SCRIPT_PATH}/memmap_${TYPE}.ld ${LINKER_CMAKE_SCRIPT})
120+
add_custom_target(memmap_${TARGET}_ld DEPENDS ${LINKER_LD_SCRIPT})
121+
add_dependencies(${TARGET} memmap_${TARGET}_ld)
122+
123+
pico_set_linker_script(${TARGET} ${CMAKE_CURRENT_BINARY_DIR}/memmap_${TARGET}.ld)
124+
endfunction()
125+
81126
# slightly messy as we support both the preferred PICO_DEFAULT_BINARY_TYPE and the individual variables
82127
if (NOT PICO_DEFAULT_BINARY_TYPE)
83128
if (PICO_NO_FLASH)
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
file(READ ${PICO_LINKER_SCRIPT_PATH}/memmap_@TYPE@.ld LINKER_SCRIPT)
2+
@RAM@
3+
@SCRATCH_X@
4+
@SCRATCH_Y@
5+
file(WRITE ${output_file} "${LINKER_SCRIPT}")

0 commit comments

Comments
 (0)