diff --git a/src/rp2_common/pico_crt0/crt0.S b/src/rp2_common/pico_crt0/crt0.S index eb477bd1e..f7522b4e1 100644 --- a/src/rp2_common/pico_crt0/crt0.S +++ b/src/rp2_common/pico_crt0/crt0.S @@ -15,6 +15,11 @@ #include "boot/picobin.h" #include "pico/bootrom.h" +// PICO_CONFIG: PICO_CRT0_NEAR_CALLS, Whether calls from crt0 into the binary are near (<16M away) - ignored for PICO_COPY_TO_RAM, default=0, type=bool, group=pico_crt0 +#ifndef PICO_CRT0_NEAR_CALLS +#define PICO_CRT0_NEAR_CALLS 0 +#endif + #ifdef NDEBUG #ifndef COLLAPSE_IRQS #define COLLAPSE_IRQS @@ -371,6 +376,12 @@ _entry_point: sev 1: #endif +#if !__ARM_ARCH_6M__ + // Make sure stack limit is 0 if we came in thru the debugger; we do not know what it should be + movs r0, #0 + msr msplim, r0 +#endif + ldr r0, =__vectors // Vector through our own table (SP, VTOR will not have been set up at // this point). Same path for debugger entry and bootloader entry. @@ -397,6 +408,9 @@ _enter_vtable_in_r0: .type _reset_handler,%function .thumb_func _reset_handler: + // Note if we entered thru here on core 0, then we should have gone thru bootrom, so SP (and MSPLIM) on Armv8-M + // should already be set + // Only core 0 should run the C runtime startup code; core 1 is normally // sleeping in the bootrom at this point but check to be sure (e.g. if // debugger put core 1 at the ELF entry point for some reason) @@ -459,20 +473,20 @@ bss_fill_test: bne bss_fill_loop platform_entry: // symbol for stack traces +#if PICO_CRT0_NEAR_CALLS && !PICO_COPY_TO_RAM + bl runtime_init + bl main + bl exit +#else // Use 32-bit jumps, in case these symbols are moved out of branch range // (e.g. if main is in SRAM and crt0 in flash) -#if !__ARM_ARCH_6M__ - // Make sure stack limit is 0 - the user can set it themselves - // todo probably worth adding to the EXE_DEF in the future - movs r0, #0 - msr msplim, r0 -#endif ldr r1, =runtime_init blx r1 ldr r1, =main blx r1 ldr r1, =exit blx r1 +#endif // exit should not return. If it does, hang the core. 1: // separate label because _exit can be moved out of branch range bkpt #0 diff --git a/src/rp2_common/pico_runtime/CMakeLists.txt b/src/rp2_common/pico_runtime/CMakeLists.txt index 59c825201..e190d5106 100644 --- a/src/rp2_common/pico_runtime/CMakeLists.txt +++ b/src/rp2_common/pico_runtime/CMakeLists.txt @@ -68,6 +68,7 @@ endif() # FPGA_CHECK - checks for FPGA which allows Raspberry Pi to run your binary on FPGA; # PANIC - default panic impl which brings in stdio; # AUTO_INIT_MUTEX - auto init mutexes, without this you get no printf mutex either; +# CRT0_FAR_CALLS - use blx not bl for calls from crt0 to user overridable functions; # # \param\ INCLUDE The items to include # \param\ EXCLUDE The items to exclude @@ -148,4 +149,7 @@ function(pico_minimize_runtime TARGET) if (NOT RUNTIME_INCLUDE_FPGA_CHECK) target_compile_definitions(${TARGET} PRIVATE PICO_NO_FPGA_CHECK=1) endif() -endfunction() \ No newline at end of file + if (NOT RUNTIME_CRT0_FAR_CALLS) + target_compile_definitions(${TARGET} PRIVATE PICO_CRT0_NEAR_CALLS=1) + endif() +endfunction()