Merge pull request #69 from c1728p9/update_rtos_init_sequence

Update rtos init sequence
Sam Grove 2016-05-05 13:53:15 -05:00
commit 27c6721d23
3 changed files with 95 additions and 190 deletions

View File

@ -276,60 +276,6 @@ __asm void __rt_entry (void) {
#elif defined (__GNUC__) #elif defined (__GNUC__)
#ifdef __CS3__
/* CS3 start_c routine.
*
* Copyright (c) 2006, 2007 CodeSourcery Inc
*
* The authors hereby grant permission to use, copy, modify, distribute,
* and license this software and its documentation for any purpose, provided
* that existing copyright notices are retained in all copies and that this
* notice is included verbatim in any distributions. No written agreement,
* license, or royalty fee is required for any of the authorized uses.
* Modifications to this software may be copyrighted by their authors
* and need not follow the licensing terms described here, provided that
* the new terms are clearly indicated on the first page of each file where
* they apply.
*/
#include "cs3.h"
extern void __libc_init_array (void);
__attribute ((noreturn)) void __cs3_start_c (void){
unsigned regions = __cs3_region_num;
const struct __cs3_region *rptr = __cs3_regions;
/* Initialize memory */
for (regions = __cs3_region_num, rptr = __cs3_regions; regions--; rptr++) {
long long *src = (long long *)rptr->init;
long long *dst = (long long *)rptr->data;
unsigned limit = rptr->init_size;
unsigned count;
if (src != dst)
for (count = 0; count != limit; count += sizeof (long long))
*dst++ = *src++;
else
dst = (long long *)((char *)dst + limit);
limit = rptr->zero_size;
for (count = 0; count != limit; count += sizeof (long long))
*dst++ = 0;
}
/* Run initializers. */
__libc_init_array ();
osKernelInitialize();
set_main_stack();
osThreadCreate(&os_thread_def_main, NULL);
osKernelStart();
for (;;);
}
#else
__attribute__((naked)) void software_init_hook (void) { __attribute__((naked)) void software_init_hook (void) {
__asm ( __asm (
".syntax unified\n" ".syntax unified\n"
@ -353,8 +299,6 @@ __attribute__((naked)) void software_init_hook (void) {
); );
} }
#endif
#elif defined (__ICCARM__) #elif defined (__ICCARM__)
extern int __low_level_init(void); extern int __low_level_init(void);

View File

@ -447,59 +447,6 @@ __asm void __rt_entry (void) {
#elif defined (__GNUC__) #elif defined (__GNUC__)
#ifdef __CS3__
/* CS3 start_c routine.
*
* Copyright (c) 2006, 2007 CodeSourcery Inc
*
* The authors hereby grant permission to use, copy, modify, distribute,
* and license this software and its documentation for any purpose, provided
* that existing copyright notices are retained in all copies and that this
* notice is included verbatim in any distributions. No written agreement,
* license, or royalty fee is required for any of the authorized uses.
* Modifications to this software may be copyrighted by their authors
* and need not follow the licensing terms described here, provided that
* the new terms are clearly indicated on the first page of each file where
* they apply.
*/
#include "cs3.h"
extern void __libc_init_array (void);
__attribute ((noreturn)) void __cs3_start_c (void){
unsigned regions = __cs3_region_num;
const struct __cs3_region *rptr = __cs3_regions;
/* Initialize memory */
for (regions = __cs3_region_num, rptr = __cs3_regions; regions--; rptr++) {
long long *src = (long long *)rptr->init;
long long *dst = (long long *)rptr->data;
unsigned limit = rptr->init_size;
unsigned count;
if (src != dst)
for (count = 0; count != limit; count += sizeof (long long))
*dst++ = *src++;
else
dst = (long long *)((char *)dst + limit);
limit = rptr->zero_size;
for (count = 0; count != limit; count += sizeof (long long))
*dst++ = 0;
}
/* Run initializers. */
__libc_init_array ();
osKernelInitialize();
osThreadCreate(&os_thread_def_main, NULL);
osKernelStart();
for (;;);
}
#else
__attribute__((naked)) void software_init_hook (void) { __attribute__((naked)) void software_init_hook (void) {
__asm ( __asm (
".syntax unified\n" ".syntax unified\n"
@ -522,8 +469,6 @@ __attribute__((naked)) void software_init_hook (void) {
); );
} }
#endif
#elif defined (__ICCARM__) #elif defined (__ICCARM__)
extern void exit(int arg); extern void exit(int arg);

View File

@ -257,8 +257,8 @@ __attribute__((used)) void _mutex_release (OS_ID *mutex) {
*---------------------------------------------------------------------------*/ *---------------------------------------------------------------------------*/
/* Main Thread definition */ /* Main Thread definition */
extern int main (void); extern void pre_main (void);
osThreadDef_t os_thread_def_main = {(os_pthread)main, osPriorityNormal, 1U, 0U, NULL}; osThreadDef_t os_thread_def_main = {(os_pthread)pre_main, osPriorityNormal, 1U, 0U, NULL};
// This define should be probably moved to the CMSIS layer // This define should be probably moved to the CMSIS layer
#if defined(TARGET_LPC1768) #if defined(TARGET_LPC1768)
@ -433,7 +433,11 @@ void set_main_stack(void) {
#if defined (__CC_ARM) #if defined (__CC_ARM)
#ifdef __MICROLIB #ifdef __MICROLIB
int main(void);
void _main_init (void) __attribute__((section(".ARM.Collect$$$$000000FF"))); void _main_init (void) __attribute__((section(".ARM.Collect$$$$000000FF")));
void $Super$$__cpp_initialize__aeabi_(void);
void _main_init (void) { void _main_init (void) {
osKernelInitialize(); osKernelInitialize();
#ifdef __MBED_CMSIS_RTOS_CM #ifdef __MBED_CMSIS_RTOS_CM
@ -443,8 +447,47 @@ void _main_init (void) {
osKernelStart(); osKernelStart();
for (;;); for (;;);
} }
void $Sub$$__cpp_initialize__aeabi_(void)
{
// this should invoke C++ initializers prior _main_init, we keep this empty and
// invoke them after _main_init (=starts RTX kernel)
}
void pre_main()
{
$Super$$__cpp_initialize__aeabi_();
main();
}
#else #else
void * armcc_heap_base;
void * armcc_heap_top;
__asm void pre_main (void)
{
IMPORT __rt_lib_init
IMPORT main
IMPORT armcc_heap_base
IMPORT armcc_heap_top
LDR R0,=armcc_heap_base
LDR R1,=armcc_heap_top
LDR R0,[R0]
LDR R1,[R1]
/* Save link register (keep 8 byte alignment with dummy r4) */
push {r4, lr}
BL __rt_lib_init
/* Restore link register and branch so when main returns it
* goes to the thread destroy function.
*/
pop {r4, lr}
B main
ALIGN
}
/* The single memory model is checking for stack collision at run time, verifing /* The single memory model is checking for stack collision at run time, verifing
that the heap pointer is underneath the stack pointer. that the heap pointer is underneath the stack pointer.
@ -456,7 +499,8 @@ void _main_init (void) {
__asm void __rt_entry (void) { __asm void __rt_entry (void) {
IMPORT __user_setup_stackheap IMPORT __user_setup_stackheap
IMPORT __rt_lib_init IMPORT armcc_heap_base
IMPORT armcc_heap_top
IMPORT os_thread_def_main IMPORT os_thread_def_main
IMPORT osKernelInitialize IMPORT osKernelInitialize
#ifdef __MBED_CMSIS_RTOS_CM #ifdef __MBED_CMSIS_RTOS_CM
@ -464,11 +508,20 @@ __asm void __rt_entry (void) {
#endif #endif
IMPORT osKernelStart IMPORT osKernelStart
IMPORT osThreadCreate IMPORT osThreadCreate
IMPORT exit
/* __user_setup_stackheap returns:
* - Heap base in r0 (if the program uses the heap).
* - Stack base in sp.
* - Heap limit in r2 (if the program uses the heap and uses two-region memory).
*
* More info can be found in:
* ARM Compiler ARM C and C++ Libraries and Floating-Point Support User Guide
*/
BL __user_setup_stackheap BL __user_setup_stackheap
MOV R1,R2 LDR R3,=armcc_heap_base
BL __rt_lib_init LDR R4,=armcc_heap_top
STR R0,[R3]
STR R2,[R4]
BL osKernelInitialize BL osKernelInitialize
#ifdef __MBED_CMSIS_RTOS_CM #ifdef __MBED_CMSIS_RTOS_CM
BL set_main_stack BL set_main_stack
@ -477,7 +530,8 @@ __asm void __rt_entry (void) {
MOVS R1,#0 MOVS R1,#0
BL osThreadCreate BL osThreadCreate
BL osKernelStart BL osKernelStart
BL exit /* osKernelStart should not return */
B .
ALIGN ALIGN
} }
@ -486,75 +540,27 @@ __asm void __rt_entry (void) {
#elif defined (__GNUC__) #elif defined (__GNUC__)
#ifdef __CS3__ __attribute__((naked)) void pre_main (void) {
__asm (
/* CS3 start_c routine. ".syntax unified\n"
* ".thumb\n"
* Copyright (c) 2006, 2007 CodeSourcery Inc /* Save link register (keep 8 byte alignment with dummy r4) */
* "push {r4, lr}\n"
* The authors hereby grant permission to use, copy, modify, distribute, "ldr r0,= __libc_fini_array\n"
* and license this software and its documentation for any purpose, provided "bl atexit\n"
* that existing copyright notices are retained in all copies and that this "bl __libc_init_array\n"
* notice is included verbatim in any distributions. No written agreement, /* Restore link register and branch so when main returns it
* license, or royalty fee is required for any of the authorized uses. * goes to the thread destroy function.
* Modifications to this software may be copyrighted by their authors
* and need not follow the licensing terms described here, provided that
* the new terms are clearly indicated on the first page of each file where
* they apply.
*/ */
"pop {r4, lr}\n"
#include "cs3.h" "b main\n"
);
extern void __libc_init_array (void);
__attribute ((noreturn)) void __cs3_start_c (void){
unsigned regions = __cs3_region_num;
const struct __cs3_region *rptr = __cs3_regions;
/* Initialize memory */
for (regions = __cs3_region_num, rptr = __cs3_regions; regions--; rptr++) {
long long *src = (long long *)rptr->init;
long long *dst = (long long *)rptr->data;
unsigned limit = rptr->init_size;
unsigned count;
if (src != dst)
for (count = 0; count != limit; count += sizeof (long long))
*dst++ = *src++;
else
dst = (long long *)((char *)dst + limit);
limit = rptr->zero_size;
for (count = 0; count != limit; count += sizeof (long long))
*dst++ = 0;
} }
/* Run initializers. */
__libc_init_array ();
osKernelInitialize();
#ifdef __MBED_CMSIS_RTOS_CM
set_main_stack();
#endif
osThreadCreate(&os_thread_def_main, NULL);
osKernelStart();
for (;;);
}
#else
__attribute__((naked)) void software_init_hook (void) { __attribute__((naked)) void software_init_hook (void) {
__asm ( __asm (
".syntax unified\n" ".syntax unified\n"
".thumb\n" ".thumb\n"
"movs r0,#0\n"
"movs r1,#0\n"
"mov r4,r0\n"
"mov r5,r1\n"
"ldr r0,= __libc_fini_array\n"
"bl atexit\n"
"bl __libc_init_array\n"
"mov r0,r4\n"
"mov r1,r5\n"
"bl osKernelInitialize\n" "bl osKernelInitialize\n"
#ifdef __MBED_CMSIS_RTOS_CM #ifdef __MBED_CMSIS_RTOS_CM
"bl set_main_stack\n" "bl set_main_stack\n"
@ -563,12 +569,11 @@ __attribute__((naked)) void software_init_hook (void) {
"movs r1,#0\n" "movs r1,#0\n"
"bl osThreadCreate\n" "bl osThreadCreate\n"
"bl osKernelStart\n" "bl osKernelStart\n"
"bl exit\n" /* osKernelStart should not return */
"B .\n"
); );
} }
#endif
#elif defined (__ICCARM__) #elif defined (__ICCARM__)
extern void* __vector_table; extern void* __vector_table;
@ -580,6 +585,15 @@ extern void __iar_dynamic_initialization(void);
extern void mbed_sdk_init(void); extern void mbed_sdk_init(void);
extern void exit(int arg); extern void exit(int arg);
static uint8_t low_level_init_needed;
void pre_main(void) {
if (low_level_init_needed) {
__iar_dynamic_initialization();
}
main();
}
#pragma required=__vector_table #pragma required=__vector_table
void __iar_program_start( void ) void __iar_program_start( void )
{ {
@ -587,22 +601,24 @@ void __iar_program_start( void )
__iar_init_core(); __iar_init_core();
__iar_init_vfp(); __iar_init_vfp();
int a; uint8_t low_level_init_needed_local;
if (__low_level_init() != 0) { low_level_init_needed_local = __low_level_init();
if (low_level_init_needed_local) {
__iar_data_init3(); __iar_data_init3();
mbed_sdk_init(); mbed_sdk_init();
__iar_dynamic_initialization();
} }
/* Store in a global variable after RAM has been initialized */
low_level_init_needed = low_level_init_needed_local;
#endif #endif
osKernelInitialize(); osKernelInitialize();
#ifdef __MBED_CMSIS_RTOS_CM #ifdef __MBED_CMSIS_RTOS_CM
set_main_stack(); set_main_stack();
#endif #endif
osThreadCreate(&os_thread_def_main, NULL); osThreadCreate(&os_thread_def_main, NULL);
a = osKernelStart(); osKernelStart();
exit(a); /* osKernelStart should not return */
while (1);
} }
#endif #endif