From e35771657648d0ce9d8df48188b3aa22889507b4 Mon Sep 17 00:00:00 2001 From: dragonmux Date: Tue, 22 Oct 2024 23:17:11 +0100 Subject: [PATCH] flashstub: Finished getting the RP2040 Flash stub integrated into the Meson build system --- meson.build | 9 ++-- src/target/flashstub/meson.build | 82 ++++++++++++++++++++++---------- src/target/flashstub/rp.c | 7 ++- src/target/meson.build | 4 +- 4 files changed, 68 insertions(+), 34 deletions(-) diff --git a/meson.build b/meson.build index 531e3fe16c9..9da8c4eee12 100644 --- a/meson.build +++ b/meson.build @@ -156,6 +156,11 @@ endif probe_host = disabler() probe_bootloader = disabler() +# Deal with the print memory usage option before recursing so the Flash stubs don't screw things up +if is_firmware_build and get_option('print_memory_usage') + add_project_link_arguments('-Wl,--print-memory-usage', language: 'c') +endif + subdir('src') ## Black Magic Firmware (BMF) targets @@ -171,10 +176,6 @@ If you did not touch the build system this is not your fault, please report it ''', ) - if get_option('print_memory_usage') - add_project_link_arguments('-Wl,--print-memory-usage', language: 'c') - endif - # System binary utilities size = find_program('size') objcopy = find_program('objcopy') diff --git a/src/target/flashstub/meson.build b/src/target/flashstub/meson.build index 83261011cab..e655f5bd0cb 100644 --- a/src/target/flashstub/meson.build +++ b/src/target/flashstub/meson.build @@ -28,19 +28,24 @@ # OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -hexdump = find_program('hexdump', native: true, required: false) +# Default definitions of the resulting stubs for all this that are harmless for, eg, BMDA +rp2040_stub = [] -write_stub = generator( - hexdump, - arguments: [ - '-v', - '-e', '/2 "0x%04X, "', - '@INPUT@' - ], - ouptut: '@CURRENT_SOURCE_DIR@/@BASENAME@.stub' - capture: true, -) +# If we're doing a firmware build, type to find hexdump +if is_firmware_build + hexdump = find_program('hexdump', required: false) +else + hexdump = disabler() +endif +# If we cannot or we're not a firmware build, we're done +if not hexdump.found() + subdir_done() +endif + +# We found hexdump, so build the stubs and their conversions to text files containing suitably formatted +# hexadecimal of their code, ready to go onto a target. +# Default options for all stub builds stub_build_args = [ '-mthumb', '-nostartfiles', @@ -49,26 +54,53 @@ stub_build_args = [ '-ffreestanding', ] -lmi_stub_elf = executable( - 'lmi_stub', - 'lmi.c', - c_args: [ - '-mcpu=cortexm-m4', - stub_build_args, - ] -) +# Flash stub for Stellaris/Tiva-C parts +# lmi_stub_elf = executable( +# 'lmi_stub', +# 'lmi.c', +# c_args: [ +# '-mcpu=cortex-m4', +# stub_build_args, +# ] +# ) -efm32_stub_elf = executable( - 'efm32_stub' - 'efm32.c', - c_args: stub_build_args, -) +# Flash stub for EFM32 parts +# efm32_stub_elf = executable( +# 'efm32_stub', +# 'efm32.c', +# c_args: [ +# '-mcpu=cortex-m3', +# stub_build_args, +# ] +# ) +# Flash stub for the RP2040 rp2040_stub_elf = executable( 'rp_stub', 'rp.c', c_args: [ - '-mcpu=cortexm-m0plus', + '-mcpu=cortex-m0plus', stub_build_args ], + link_args: [ + '-mcpu=cortex-m0plus', + stub_build_args, + '-T', '@0@/rp.ld'.format(meson.current_source_dir()), + ], + link_depends: files('rp.ld'), + pie: false, + install: false, +) + +rp2040_stub = custom_target( + 'rp_stub-hex', + command: [ + hexdump, + '-v', + '-e', '/2 "0x%04X, "', + '@INPUT@' + ], + input: rp2040_stub_elf, + output: 'rp.stub', + capture: true, ) diff --git a/src/target/flashstub/rp.c b/src/target/flashstub/rp.c index 2a521febba6..9600a3b0ab1 100644 --- a/src/target/flashstub/rp.c +++ b/src/target/flashstub/rp.c @@ -71,8 +71,7 @@ static gpio_qspi_s *const gpio_qspi = (gpio_qspi_s *)RP_GPIO_QSPI_BASE_ADDR; #define MIN(x, y) (((x) < (y)) ? (x) : (y)) -void __attribute__((naked, used, section(".entry"))) -rp_flash_write_stub(const uint16_t command, const uint32_t dest, const uint8_t *const src, const uint32_t length) +void __attribute__((naked, used, section(".entry"))) rp_flash_write_stub() { /* Create a stack for our own sanity */ __asm__("ldr r4, =#0x20042000\n" @@ -150,8 +149,8 @@ static void rp_spi_write(const uint32_t address, const uint8_t *const src, const rp_spi_flash_deselect(); } -static void __attribute__((used, section(".entry"))) -rp_flash_write(const uint32_t dest, const uint8_t *const src, const size_t length, const uint32_t page_size) +static void __attribute__((used, section(".entry"))) rp_flash_write( + const uint32_t dest, const uint8_t *const src, const size_t length, const uint32_t page_size) { for (size_t offset = 0; offset < length; offset += page_size) { /* Try to write-enable the Flash */ diff --git a/src/target/meson.build b/src/target/meson.build index 95167c95d87..bffa93b89b0 100644 --- a/src/target/meson.build +++ b/src/target/meson.build @@ -31,6 +31,8 @@ target_common_includes = include_directories('.') +subdir('flashstub') + target_common_sources = files( 'adi.c', 'adiv5.c', @@ -241,7 +243,7 @@ target_rp = declare_dependency( sources: files( 'rp2040.c', 'rp2350.c', - ), + ) + rp2040_stub, dependencies: target_cortexm, )