Skip to content

Commit edb39c6

Browse files
authored
Merge pull request #12366 from OpenNuvoton/nuvoton_m2351_gcc
M2351: Support GCC
2 parents b140fd0 + 9faa236 commit edb39c6

File tree

2 files changed

+66
-29
lines changed

2 files changed

+66
-29
lines changed

targets/TARGET_NUVOTON/TARGET_M2351/device/startup_M2351.c

Lines changed: 64 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -54,18 +54,33 @@ void FUN(void) __attribute__ ((weak, alias(#FUN_ALIAS)));
5454

5555
#endif
5656

57-
5857
/* Initialize segments */
5958
#if defined(__ARMCC_VERSION)
6059
#if defined (__ARM_FEATURE_CMSE) && (__ARM_FEATURE_CMSE == 3U) && (TFM_LVL > 0)
6160
extern uint32_t Image$$ARM_LIB_STACK_MSP$$ZI$$Limit;
61+
extern uint32_t Image$$ARM_LIB_STACK$$ZI$$Limit;
6262
#else
6363
extern uint32_t Image$$ARM_LIB_STACK$$ZI$$Limit;
6464
#endif
6565
extern void __main(void);
6666
#elif defined(__ICCARM__)
67+
#if defined (__ARM_FEATURE_CMSE) && (__ARM_FEATURE_CMSE == 3U) && (TFM_LVL > 0)
68+
extern uint32_t Image$$ARM_LIB_STACK_MSP$$ZI$$Limit;
69+
extern uint32_t Image$$ARM_LIB_STACK$$ZI$$Limit;
70+
extern uint32_t CSTACK_MSP$$Limit;
71+
extern uint32_t CSTACK$$Limit;
72+
#else
73+
extern uint32_t Image$$ARM_LIB_STACK$$ZI$$Limit;
74+
extern uint32_t CSTACK$$Limit;
75+
#endif
6776
void __iar_program_start(void);
6877
#elif defined(__GNUC__)
78+
#if defined (__ARM_FEATURE_CMSE) && (__ARM_FEATURE_CMSE == 3U) && (TFM_LVL > 0)
79+
extern uint32_t Image$$ARM_LIB_STACK_MSP$$ZI$$Limit;
80+
extern uint32_t Image$$ARM_LIB_STACK$$ZI$$Limit;
81+
#else
82+
extern uint32_t Image$$ARM_LIB_STACK$$ZI$$Limit;
83+
#endif
6984
extern uint32_t __StackTop;
7085
extern uint32_t __copy_table_start__;
7186
extern uint32_t __copy_table_end__;
@@ -195,12 +210,6 @@ WEAK_ALIAS_FUNC(TRNG_IRQHandler, Default_Handler) // 101:
195210
__attribute__ ((section("RESET")))
196211
const uint32_t __vector_handlers[] = {
197212
#elif defined(__ICCARM__)
198-
#if defined (__ARM_FEATURE_CMSE) && (__ARM_FEATURE_CMSE == 3U) && (TFM_LVL > 0)
199-
extern uint32_t CSTACK_MSP$$Limit;
200-
extern uint32_t CSTACK$$Limit;
201-
#else
202-
extern uint32_t CSTACK$$Limit;
203-
#endif
204213
const uint32_t __vector_table[] @ ".intvec" = {
205214
#elif defined(__GNUC__)
206215
__attribute__ ((section(".vector_table")))
@@ -347,65 +356,91 @@ const uint32_t __vector_handlers[] = {
347356
(uint32_t) TRNG_IRQHandler, // 101:
348357
};
349358

350-
#if defined (__ARM_FEATURE_CMSE) && (__ARM_FEATURE_CMSE == 3U)
351-
352359
/* Some reset handler code cannot implement in pure C. Implement it in inline/embedded assembly.
353360
*
354361
* Reset_Handler:
355-
* For non-secure PSA/non-secure non-PSA/secure non-PSA, do as usual
356-
* For secure PSA, switch from MSP to PSP, then jump to Reset_Handler_1 for usual work
362+
* For non-secure PSA/non-secure non-PSA/secure non-PSA, jump directly to Reset_Handler_1
363+
* For secure PSA, switch from MSP to PSP, then jump to Reset_Handler_1
357364
*
358-
* Reset_Handler_1
365+
* Reset_Handler_1:
366+
* Platform initialization
359367
* C/C++ runtime initialization
360368
*/
361369

370+
/* Forward declaration */
371+
#if defined (__ARM_FEATURE_CMSE) && (__ARM_FEATURE_CMSE == 3U) && (TFM_LVL > 0)
372+
/* Limited by inline assembly syntax, esp. on IAR, we cannot get stack limit
373+
* for PSP just from external symbol. To avoid re-write in assembly, We make up
374+
* a function here to get this value indirectly. */
375+
uint32_t StackLimit_PSP(void);
376+
#endif
362377
void Reset_Handler_1(void);
363378

364379
/* Add '__attribute__((naked))' here to make sure compiler does not generate prologue and
365380
* epilogue sequences for Reset_Handler. We don't want MSP is updated by compiler-generated
366-
* code during stack switch. */
381+
* code during stack switch.
382+
*
383+
* Don't allow extended assembly in naked functions:
384+
* The compiler only supports basic __asm statements in __attribute__((naked))
385+
* functions. Using extended assembly, parameter references or mixing C code with
386+
* __asm statements might not work reliably.
387+
*/
367388
__attribute__((naked)) void Reset_Handler(void)
368389
{
369-
#if !defined(__ICCARM__)
390+
#if defined(__GNUC__)
370391
__asm(".syntax unified \n");
371-
__asm(".globl Reset_Handler_1 \n");
372392
#endif
373393

374394
/* Secure TFM requires PSP as boot stack */
375-
#if TFM_LVL != 0
395+
#if defined (__ARM_FEATURE_CMSE) && (__ARM_FEATURE_CMSE == 3U) && (TFM_LVL > 0)
396+
/* Get stack limit for PSP */
376397
#if !defined(__ICCARM__)
377-
__asm(".globl Image$$ARM_LIB_STACK$$ZI$$Limit \n");
378-
__asm("movw r0, #:lower16:Image$$ARM_LIB_STACK$$ZI$$Limit \n"); // Initialize PSP
379-
__asm("movt r0, #:upper16:Image$$ARM_LIB_STACK$$ZI$$Limit \n");
398+
__asm("movw r0, #:lower16:StackLimit_PSP \n");
399+
__asm("movt r0, #:upper16:StackLimit_PSP \n");
380400
#else
381-
__asm(".globl Image$$ARM_LIB_STACK$$ZI$$Limit \n");
382-
__asm("mov32 r0, Image$$ARM_LIB_STACK$$ZI$$Limit \n");
401+
__asm("mov32 r0, StackLimit_PSP \n");
383402
#endif
403+
__asm("blx r0 \n");
404+
405+
/* Switch from MSP to PSP */
384406
__asm("msr psp, r0 \n");
385-
__asm("mrs r0, control \n"); // Switch SP to PSP
407+
__asm("mrs r0, control \n");
386408
__asm("movs r1, #2 \n");
387409
__asm("orrs r0, r1 \n");
388410
__asm("msr control, r0 \n");
389411
#endif
390412

413+
/* Jump to Reset_Handler_1 */
391414
#if !defined(__ICCARM__)
392415
__asm("movw r0, #:lower16:Reset_Handler_1 \n");
393416
__asm("movt r0, #:upper16:Reset_Handler_1 \n");
394417
#else
395-
__asm("mov32 r0, Reset_Handler_1 \n");
418+
__asm("mov32 r0, Reset_Handler_1 \n");
396419
#endif
397420
__asm("bx r0 \n");
398421
}
399422

400-
void Reset_Handler_1(void)
401-
402-
#else
403-
404-
void Reset_Handler(void)
423+
#if defined (__ARM_FEATURE_CMSE) && (__ARM_FEATURE_CMSE == 3U) && (TFM_LVL > 0)
424+
/* Return stack limit for PSP */
425+
uint32_t StackLimit_PSP(void)
426+
{
427+
uint32_t stacklimit_psp;
405428

406-
#endif /* defined(__ARM_FEATURE_CMSE) && (__ARM_FEATURE_CMSE == 3U) */
429+
__asm volatile (
430+
#if defined(__GNUC__)
431+
".syntax unified \n"
432+
#endif
433+
"mov %[Rd], %[Rn] \n"
434+
: [Rd] "=r" (stacklimit_psp) /* comma-separated list of output operands */
435+
: [Rn] "r" (&Image$$ARM_LIB_STACK$$ZI$$Limit) /* comma-separated list of input operands */
436+
: "cc" /* comma-separated list of clobbered resources */
437+
);
407438

439+
return stacklimit_psp;
440+
}
441+
#endif
408442

443+
void Reset_Handler_1(void)
409444
{
410445
#if defined(__ARM_FEATURE_CMSE) && (__ARM_FEATURE_CMSE == 3U)
411446
/* Disable register write-protection function */

targets/targets.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12682,6 +12682,7 @@
1268212682
"trustzone": true,
1268312683
"supported_toolchains": [
1268412684
"ARMC6",
12685+
"GCC_ARM",
1268512686
"IAR"
1268612687
],
1268712688
"extra_labels_add": [
@@ -12743,6 +12744,7 @@
1274312744
"core": "Cortex-M23-NS",
1274412745
"supported_toolchains": [
1274512746
"ARMC6",
12747+
"GCC_ARM",
1274612748
"IAR"
1274712749
],
1274812750
"tfm.level": 1,

0 commit comments

Comments
 (0)