@@ -195,8 +195,8 @@ __attribute__((used)) void _mutex_release (OS_ID *mutex) {
195
195
*---------------------------------------------------------------------------*/
196
196
197
197
/* Main Thread definition */
198
- extern int main (void );
199
- osThreadDef_t os_thread_def_main = {(os_pthread )main , osPriorityNormal , 0 , NULL };
198
+ extern void pre_main (void );
199
+ osThreadDef_t os_thread_def_main = {(os_pthread )pre_main , osPriorityNormal , 0 , NULL };
200
200
201
201
// This define should be probably moved to the CMSIS layer
202
202
@@ -230,16 +230,57 @@ void set_main_stack(void) {
230
230
231
231
#if defined (__CC_ARM )
232
232
#ifdef __MICROLIB
233
+
234
+ int main (void );
233
235
void _main_init (void ) __attribute__((section (".ARM.Collect$$$$000000FF" )));
236
+ void $Super$$__cpp_initialize__aeabi_ (void );
237
+
234
238
void _main_init (void ) {
235
239
osKernelInitialize ();
236
240
set_main_stack ();
237
241
osThreadCreate (& os_thread_def_main , NULL );
238
242
osKernelStart ();
239
243
for (;;);
240
244
}
245
+
246
+ void $Sub$$__cpp_initialize__aeabi_ (void )
247
+ {
248
+ // this should invoke C++ initializers prior _main_init, we keep this empty and
249
+ // invoke them after _main_init (=starts RTX kernel)
250
+ }
251
+
252
+ void pre_main ()
253
+ {
254
+ $Super$$__cpp_initialize__aeabi_ ();
255
+ main ();
256
+ }
257
+
241
258
#else
242
259
260
+ void * armcc_heap_base ;
261
+ void * armcc_heap_top ;
262
+
263
+ __asm void pre_main (void )
264
+ {
265
+ IMPORT __rt_lib_init
266
+ IMPORT main
267
+ IMPORT armcc_heap_base
268
+ IMPORT armcc_heap_top
269
+
270
+ LDR R0 ,= armcc_heap_base
271
+ LDR R1 ,= armcc_heap_top
272
+ LDR R0 , [R0 ]
273
+ LDR R1 , [R1 ]
274
+ /* Save link register (keep 8 byte alignment with dummy R4) */
275
+ PUSH {R4 , LR }
276
+ BL __rt_lib_init
277
+ BL main
278
+ /* Return to the thread destroy function.
279
+ */
280
+ POP {R4 , PC }
281
+ ALIGN
282
+ }
283
+
243
284
/* The single memory model is checking for stack collision at run time, verifing
244
285
that the heap pointer is underneath the stack pointer.
245
286
@@ -251,127 +292,109 @@ void _main_init (void) {
251
292
__asm void __rt_entry (void ) {
252
293
253
294
IMPORT __user_setup_stackheap
254
- IMPORT __rt_lib_init
295
+ IMPORT armcc_heap_base
296
+ IMPORT armcc_heap_top
255
297
IMPORT os_thread_def_main
256
298
IMPORT osKernelInitialize
257
299
IMPORT set_main_stack
258
300
IMPORT osKernelStart
259
301
IMPORT osThreadCreate
260
- IMPORT exit
261
302
303
+ /* __user_setup_stackheap returns:
304
+ * - Heap base in r0 (if the program uses the heap).
305
+ * - Stack base in sp.
306
+ * - Heap limit in r2 (if the program uses the heap and uses two-region memory).
307
+ *
308
+ * More info can be found in:
309
+ * ARM Compiler ARM C and C++ Libraries and Floating-Point Support User Guide
310
+ */
262
311
BL __user_setup_stackheap
263
- MOV R1 ,R2
264
- BL __rt_lib_init
312
+ LDR R3 ,= armcc_heap_base
313
+ LDR R4 ,= armcc_heap_top
314
+ STR R0 , [R3 ]
315
+ STR R2 , [R4 ]
265
316
BL osKernelInitialize
266
317
BL set_main_stack
267
318
LDR R0 ,= os_thread_def_main
268
319
MOVS R1 ,#0
269
320
BL osThreadCreate
270
321
BL osKernelStart
271
- BL exit
322
+ /* osKernelStart should not return */
323
+ B .
272
324
273
325
ALIGN
274
326
}
327
+
275
328
#endif
276
329
277
330
#elif defined (__GNUC__ )
278
331
279
- #ifdef __CS3__
280
-
281
- /* CS3 start_c routine.
282
- *
283
- * Copyright (c) 2006, 2007 CodeSourcery Inc
284
- *
285
- * The authors hereby grant permission to use, copy, modify, distribute,
286
- * and license this software and its documentation for any purpose, provided
287
- * that existing copyright notices are retained in all copies and that this
288
- * notice is included verbatim in any distributions. No written agreement,
289
- * license, or royalty fee is required for any of the authorized uses.
290
- * Modifications to this software may be copyrighted by their authors
291
- * and need not follow the licensing terms described here, provided that
292
- * the new terms are clearly indicated on the first page of each file where
293
- * they apply.
294
- */
295
-
296
- #include "cs3.h"
297
-
332
+ extern void __libc_fini_array (void );
298
333
extern void __libc_init_array (void );
334
+ extern int main (int argc , char * * argv );
299
335
300
- __attribute ((noreturn )) void __cs3_start_c (void ){
301
- unsigned regions = __cs3_region_num ;
302
- const struct __cs3_region * rptr = __cs3_regions ;
303
-
304
- /* Initialize memory */
305
- for (regions = __cs3_region_num , rptr = __cs3_regions ; regions -- ; rptr ++ ) {
306
- long long * src = (long long * )rptr -> init ;
307
- long long * dst = (long long * )rptr -> data ;
308
- unsigned limit = rptr -> init_size ;
309
- unsigned count ;
310
-
311
- if (src != dst )
312
- for (count = 0 ; count != limit ; count += sizeof (long long ))
313
- * dst ++ = * src ++ ;
314
- else
315
- dst = (long long * )((char * )dst + limit );
316
- limit = rptr -> zero_size ;
317
- for (count = 0 ; count != limit ; count += sizeof (long long ))
318
- * dst ++ = 0 ;
319
- }
320
-
321
- /* Run initializers. */
322
- __libc_init_array ();
323
-
324
- osKernelInitialize ();
325
- set_main_stack ();
326
- osThreadCreate (& os_thread_def_main , NULL );
327
- osKernelStart ();
328
- for (;;);
336
+ void pre_main (void ) {
337
+ atexit (__libc_fini_array );
338
+ __libc_init_array ();
339
+ main (0 , NULL );
329
340
}
330
341
331
- #else
332
-
333
342
__attribute__((naked )) void software_init_hook (void ) {
334
343
__asm (
335
344
".syntax unified\n"
336
345
".thumb\n"
337
- "movs r0,#0\n"
338
- "movs r1,#0\n"
339
- "mov r8,r0\n"
340
- "mov r9,r1\n"
341
- "ldr r0,= __libc_fini_array\n"
342
- "bl atexit\n"
343
- "bl __libc_init_array\n"
344
- "mov r0,r8\n"
345
- "mov r1,r9\n"
346
346
"bl osKernelInitialize\n"
347
347
"bl set_main_stack\n"
348
348
"ldr r0,=os_thread_def_main\n"
349
349
"movs r1,#0\n"
350
350
"bl osThreadCreate\n"
351
351
"bl osKernelStart\n"
352
- "bl exit\n"
352
+ /* osKernelStart should not return */
353
+ "B .\n"
353
354
);
354
355
}
355
356
356
- #endif
357
-
358
357
#elif defined (__ICCARM__)
359
358
359
+ extern void * __vector_table ;
360
360
extern int __low_level_init (void );
361
361
extern void __iar_data_init3 (void );
362
+ extern __weak void __iar_init_core ( void );
363
+ extern __weak void __iar_init_vfp ( void );
364
+ extern void __iar_dynamic_initialization (void );
365
+ extern void mbed_sdk_init (void );
362
366
extern void exit (int arg );
363
367
364
- __noreturn __stackless void __cmain (void ) {
365
- int a ;
368
+ static uint8_t low_level_init_needed ;
366
369
367
- if (__low_level_init () != 0 ) {
370
+ void pre_main (void ) {
371
+ if (low_level_init_needed ) {
372
+ __iar_dynamic_initialization ();
373
+ }
374
+ main ();
375
+ }
376
+
377
+ #pragma required=__vector_table
378
+ void __iar_program_start ( void )
379
+ {
380
+ __iar_init_core ();
381
+ __iar_init_vfp ();
382
+
383
+ uint8_t low_level_init_needed_local ;
384
+
385
+ low_level_init_needed_local = __low_level_init ();
386
+ if (low_level_init_needed_local ) {
368
387
__iar_data_init3 ();
388
+ mbed_sdk_init ();
369
389
}
390
+ /* Store in a global variable after RAM has been initialized */
391
+ low_level_init_needed = low_level_init_needed_local ;
370
392
osKernelInitialize ();
371
393
set_main_stack ();
372
394
osThreadCreate (& os_thread_def_main , NULL );
373
- a = osKernelStart ();
374
- exit (a );
395
+ osKernelStart ();
396
+ /* osKernelStart should not return */
397
+ while (1 );
375
398
}
376
399
377
400
#endif
0 commit comments