From b5149c014197220994b7c3c2c0e3dacb506fe759 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Sch=C3=A4pers?= Date: Thu, 1 Aug 2024 08:37:01 +0200 Subject: [PATCH] Add option to keep the main stack intact on CM7 I was surprised by that behaviour, and apparently I'm not the only one: https://forums.freertos.org/t/silently-breaking-compiler-standards-by-corrupting-main-stack/11165 https://forums.freertos.org/t/main-stack-pointer-reset-when-starting-the-scheduler/5227 Add an option to disable that. My use case is that I initialize some stuff before the OS and give it to the tasks. No global variable, because I want to reduce the visibility. --- portable/GCC/ARM_CM7/r0p1/port.c | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/portable/GCC/ARM_CM7/r0p1/port.c b/portable/GCC/ARM_CM7/r0p1/port.c index ac067274d..60c2f9d83 100644 --- a/portable/GCC/ARM_CM7/r0p1/port.c +++ b/portable/GCC/ARM_CM7/r0p1/port.c @@ -272,15 +272,19 @@ void vPortSVCHandler( void ) static void prvPortStartFirstTask( void ) { - /* Start the first task. This also clears the bit that indicates the FPU is - * in use in case the FPU was used before the scheduler was started - which - * would otherwise result in the unnecessary leaving of space in the SVC stack - * for lazy saving of FPU registers. */ +#ifndef configKEEP_MAIN_STACK __asm volatile ( " ldr r0, =0xE000ED08 \n" /* Use the NVIC offset register to locate the stack. */ " ldr r0, [r0] \n" " ldr r0, [r0] \n" " msr msp, r0 \n" /* Set the msp back to the start of the stack. */ + ); +#endif + /* Start the first task. This also clears the bit that indicates the FPU is + * in use in case the FPU was used before the scheduler was started - which + * would otherwise result in the unnecessary leaving of space in the SVC stack + * for lazy saving of FPU registers. */ + __asm volatile ( " mov r0, #0 \n" /* Clear the bit that indicates the FPU is in use, see comment above. */ " msr control, r0 \n" " cpsie i \n" /* Globally enable interrupts. */