I have a really specific problem and was wondering if some experts here who are more familiar
with the older compilers have an idea what might cause this issues.
I am using a port for the SAM9G20 core (which is already considered legacy)
There is a macro function to restore the port context which is written in inline assembly:
#define portRESTORE_CONTEXT() \
{ \
extern volatile void * volatile pxCurrentTCB; \
extern volatile unsigned portLONG ulCriticalNesting; \
\
/* Set the LR to the task stack. */ \
asm volatile ( \
"LDR R1, =pxCurrentTCB \n\t" \
"LDR R0, [R1] \n\t" \
"LDR LR, [R0] \n\t" \
\
/* The critical nesting depth is the first item on the stack. */ \
/* Load it into the ulCriticalNesting variable. */ \
"LDR R0, =ulCriticalNesting \n\t" \
"LDMFD LR!, {R1} \n\t" \
"STR R1, [R0] \n\t" \
\
/* Get the SPSR from the stack. */ \
"LDMFD LR!, {R0} \n\t" \
"MSR SPSR, R0 \n\t" \
\
/* Restore all system mode registers for the task. */ \
"LDMFD LR, {R0-R14}^ \n\t" \
"NOP \n\t" \
\
/* Restore the return address. */ \
"LDR LR, [LR, #+60] \n\t" \
\
/* And return - correcting the offset in the LR to obtain the */ \
/* correct address. */ \
"SUBS PC, LR, #4 \n\t" \
); \
( void ) ulCriticalNesting; \
( void ) pxCurrentTCB; \
}
When I flash the hardware using the J-Link, it works without issues. If I transfer the binary on the boot flash memory and restart the core (letting the bootloader do the job of loading the software), the program gets stuck in the macro when starting the task scheduler. This only happens for code optimized with Link Time Optimization (-flto flag).
The following change fixed the issue:
I removed
extern volatile void * volatile pxCurrentTCB;
extern volatile unsigned portLONG ulCriticalNesting;
and
( void ) ulCriticalNesting;
( void ) pxCurrentTCB;
and now the program does not hang up in the macro anymore but starts regularly (although I still need to run some advanced tests, everything seems to work up until now).
I am using the newest ARM GCC Toolchain available (9.3.1-1.1.1) so maybe it’s just the combination of the new toolchain and very old code but maybe someone also has an idea why
those 4 lines cause issues… Especially because functions like portSAVE_CONTEXT use similar code.
UPDATE: Alright, it worked for some time and now it is stuck again in the same function so the problem is propably unrelated…
Kind Regards
Robin