Skip to content

Commit 99fb83a

Browse files
committed
Merge pull request #1730 from 0xc0170/dev_rtos_init
RTX - init sequence (C++ array init)
2 parents 03d8ea8 + daf5c9e commit 99fb83a

File tree

4 files changed

+182
-207
lines changed

4 files changed

+182
-207
lines changed

hal/api/semihost_api.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ extern "C" {
2828
#ifndef __CC_ARM
2929

3030
#if defined(__ICCARM__)
31-
inline int __semihost(int reason, const void *arg) {
31+
static inline int __semihost(int reason, const void *arg) {
3232
return __semihosting(reason, (void*)arg);
3333
}
3434
#else

rtos/rtx/TARGET_ARM7/RTX_CM_lib.h

Lines changed: 97 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -195,8 +195,8 @@ __attribute__((used)) void _mutex_release (OS_ID *mutex) {
195195
*---------------------------------------------------------------------------*/
196196

197197
/* 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};
200200

201201
// This define should be probably moved to the CMSIS layer
202202

@@ -230,16 +230,57 @@ void set_main_stack(void) {
230230

231231
#if defined (__CC_ARM)
232232
#ifdef __MICROLIB
233+
234+
int main(void);
233235
void _main_init (void) __attribute__((section(".ARM.Collect$$$$000000FF")));
236+
void $Super$$__cpp_initialize__aeabi_(void);
237+
234238
void _main_init (void) {
235239
osKernelInitialize();
236240
set_main_stack();
237241
osThreadCreate(&os_thread_def_main, NULL);
238242
osKernelStart();
239243
for (;;);
240244
}
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+
241258
#else
242259

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+
243284
/* The single memory model is checking for stack collision at run time, verifing
244285
that the heap pointer is underneath the stack pointer.
245286
@@ -251,127 +292,109 @@ void _main_init (void) {
251292
__asm void __rt_entry (void) {
252293

253294
IMPORT __user_setup_stackheap
254-
IMPORT __rt_lib_init
295+
IMPORT armcc_heap_base
296+
IMPORT armcc_heap_top
255297
IMPORT os_thread_def_main
256298
IMPORT osKernelInitialize
257299
IMPORT set_main_stack
258300
IMPORT osKernelStart
259301
IMPORT osThreadCreate
260-
IMPORT exit
261302

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+
*/
262311
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]
265316
BL osKernelInitialize
266317
BL set_main_stack
267318
LDR R0,=os_thread_def_main
268319
MOVS R1,#0
269320
BL osThreadCreate
270321
BL osKernelStart
271-
BL exit
322+
/* osKernelStart should not return */
323+
B .
272324

273325
ALIGN
274326
}
327+
275328
#endif
276329

277330
#elif defined (__GNUC__)
278331

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);
298333
extern void __libc_init_array (void);
334+
extern int main(int argc, char **argv);
299335

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);
329340
}
330341

331-
#else
332-
333342
__attribute__((naked)) void software_init_hook (void) {
334343
__asm (
335344
".syntax unified\n"
336345
".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"
346346
"bl osKernelInitialize\n"
347347
"bl set_main_stack\n"
348348
"ldr r0,=os_thread_def_main\n"
349349
"movs r1,#0\n"
350350
"bl osThreadCreate\n"
351351
"bl osKernelStart\n"
352-
"bl exit\n"
352+
/* osKernelStart should not return */
353+
"B .\n"
353354
);
354355
}
355356

356-
#endif
357-
358357
#elif defined (__ICCARM__)
359358

359+
extern void* __vector_table;
360360
extern int __low_level_init(void);
361361
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);
362366
extern void exit(int arg);
363367

364-
__noreturn __stackless void __cmain(void) {
365-
int a;
368+
static uint8_t low_level_init_needed;
366369

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) {
368387
__iar_data_init3();
388+
mbed_sdk_init();
369389
}
390+
/* Store in a global variable after RAM has been initialized */
391+
low_level_init_needed = low_level_init_needed_local;
370392
osKernelInitialize();
371393
set_main_stack();
372394
osThreadCreate(&os_thread_def_main, NULL);
373-
a = osKernelStart();
374-
exit(a);
395+
osKernelStart();
396+
/* osKernelStart should not return */
397+
while (1);
375398
}
376399

377400
#endif

rtos/rtx/TARGET_CORTEX_A/RTX_CM_lib.h

Lines changed: 0 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -447,59 +447,6 @@ __asm void __rt_entry (void) {
447447

448448
#elif defined (__GNUC__)
449449

450-
#ifdef __CS3__
451-
452-
/* CS3 start_c routine.
453-
*
454-
* Copyright (c) 2006, 2007 CodeSourcery Inc
455-
*
456-
* The authors hereby grant permission to use, copy, modify, distribute,
457-
* and license this software and its documentation for any purpose, provided
458-
* that existing copyright notices are retained in all copies and that this
459-
* notice is included verbatim in any distributions. No written agreement,
460-
* license, or royalty fee is required for any of the authorized uses.
461-
* Modifications to this software may be copyrighted by their authors
462-
* and need not follow the licensing terms described here, provided that
463-
* the new terms are clearly indicated on the first page of each file where
464-
* they apply.
465-
*/
466-
467-
#include "cs3.h"
468-
469-
extern void __libc_init_array (void);
470-
471-
__attribute ((noreturn)) void __cs3_start_c (void){
472-
unsigned regions = __cs3_region_num;
473-
const struct __cs3_region *rptr = __cs3_regions;
474-
475-
/* Initialize memory */
476-
for (regions = __cs3_region_num, rptr = __cs3_regions; regions--; rptr++) {
477-
long long *src = (long long *)rptr->init;
478-
long long *dst = (long long *)rptr->data;
479-
unsigned limit = rptr->init_size;
480-
unsigned count;
481-
482-
if (src != dst)
483-
for (count = 0; count != limit; count += sizeof (long long))
484-
*dst++ = *src++;
485-
else
486-
dst = (long long *)((char *)dst + limit);
487-
limit = rptr->zero_size;
488-
for (count = 0; count != limit; count += sizeof (long long))
489-
*dst++ = 0;
490-
}
491-
492-
/* Run initializers. */
493-
__libc_init_array ();
494-
495-
osKernelInitialize();
496-
osThreadCreate(&os_thread_def_main, NULL);
497-
osKernelStart();
498-
for (;;);
499-
}
500-
501-
#else
502-
503450
__attribute__((naked)) void software_init_hook (void) {
504451
__asm (
505452
".syntax unified\n"
@@ -522,8 +469,6 @@ __attribute__((naked)) void software_init_hook (void) {
522469
);
523470
}
524471

525-
#endif
526-
527472
#elif defined (__ICCARM__)
528473

529474
extern void exit(int arg);

0 commit comments

Comments
 (0)