@@ -257,8 +257,8 @@ __attribute__((used)) void _mutex_release (OS_ID *mutex) {
257
257
*---------------------------------------------------------------------------*/
258
258
259
259
/* Main Thread definition */
260
- extern int main (void );
261
- osThreadDef_t os_thread_def_main = {(os_pthread )main , osPriorityNormal , 1U , 0U , NULL };
260
+ extern void pre_main (void );
261
+ osThreadDef_t os_thread_def_main = {(os_pthread )pre_main , osPriorityNormal , 1U , 0U , NULL };
262
262
263
263
// This define should be probably moved to the CMSIS layer
264
264
#if defined(TARGET_LPC1768 )
@@ -445,6 +445,32 @@ void _main_init (void) {
445
445
}
446
446
#else
447
447
448
+ void * armcc_heap_base ;
449
+ void * armcc_heap_top ;
450
+
451
+ __asm void pre_main (void )
452
+ {
453
+ IMPORT __rt_lib_init
454
+ IMPORT main
455
+ IMPORT armcc_heap_base
456
+ IMPORT armcc_heap_top
457
+
458
+ LDR R0 ,= armcc_heap_base
459
+ LDR R1 ,= armcc_heap_top
460
+ LDR R0 , [R0 ]
461
+ LDR R1 , [R1 ]
462
+ /* Save link register (keep 8 byte alignment with dummy r4) */
463
+ push {r4 , lr }
464
+ BL __rt_lib_init
465
+ /* Restore link register and branch so when main returns it
466
+ * goes to the thread destroy function.
467
+ */
468
+ pop {r4 , lr }
469
+ B main
470
+
471
+ ALIGN
472
+ }
473
+
448
474
/* The single memory model is checking for stack collision at run time, verifing
449
475
that the heap pointer is underneath the stack pointer.
450
476
@@ -456,19 +482,29 @@ void _main_init (void) {
456
482
__asm void __rt_entry (void ) {
457
483
458
484
IMPORT __user_setup_stackheap
459
- IMPORT __rt_lib_init
485
+ IMPORT armcc_heap_base
486
+ IMPORT armcc_heap_top
460
487
IMPORT os_thread_def_main
461
488
IMPORT osKernelInitialize
462
489
#ifdef __MBED_CMSIS_RTOS_CM
463
490
IMPORT set_main_stack
464
491
#endif
465
492
IMPORT osKernelStart
466
493
IMPORT osThreadCreate
467
- IMPORT exit
468
494
495
+ /* __user_setup_stackheap returns:
496
+ * - Heap base in r0 (if the program uses the heap).
497
+ * - Stack base in sp.
498
+ * - Heap limit in r2 (if the program uses the heap and uses two-region memory).
499
+ *
500
+ * More info can be found in:
501
+ * ARM Compiler ARM C and C++ Libraries and Floating-Point Support User Guide
502
+ */
469
503
BL __user_setup_stackheap
470
- MOV R1 ,R2
471
- BL __rt_lib_init
504
+ LDR R3 ,= armcc_heap_base
505
+ LDR R4 ,= armcc_heap_top
506
+ STR R0 , [R3 ]
507
+ STR R2 , [R4 ]
472
508
BL osKernelInitialize
473
509
#ifdef __MBED_CMSIS_RTOS_CM
474
510
BL set_main_stack
@@ -477,7 +513,8 @@ __asm void __rt_entry (void) {
477
513
MOVS R1 ,#0
478
514
BL osThreadCreate
479
515
BL osKernelStart
480
- BL exit
516
+ /* osKernelStart should not return */
517
+ B .
481
518
482
519
ALIGN
483
520
}
0 commit comments