2015-02-25 10:50:21 +08:00
|
|
|
/*
|
2022-08-30 03:35:23 +08:00
|
|
|
* Copyright (c) 2006-2022, RT-Thread Development Team
|
2015-02-25 10:50:21 +08:00
|
|
|
*
|
2018-09-14 22:37:43 +08:00
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
2015-02-25 10:50:21 +08:00
|
|
|
*
|
|
|
|
* Change Logs:
|
|
|
|
* Date Author Notes
|
|
|
|
* 2012-09-20 Bernard Change the name to components.c
|
|
|
|
* And all components related header files.
|
|
|
|
* 2012-12-23 Bernard fix the pthread initialization issue.
|
|
|
|
* 2013-06-23 Bernard Add the init_call for components initialization.
|
|
|
|
* 2013-07-05 Bernard Remove initialization feature for MS VC++ compiler
|
|
|
|
* 2015-02-06 Bernard Remove the MS VC++ support and move to the kernel
|
2015-07-29 09:58:26 +08:00
|
|
|
* 2015-05-04 Bernard Rename it to components.c because compiling issue
|
2015-05-04 20:38:22 +08:00
|
|
|
* in some IDEs.
|
2015-07-29 09:58:26 +08:00
|
|
|
* 2015-07-29 Arda.Fu Add support to use RT_USING_USER_MAIN with IAR
|
2018-11-22 14:40:43 +08:00
|
|
|
* 2018-11-22 Jesven Add secondary cpu boot up
|
2015-02-25 10:50:21 +08:00
|
|
|
*/
|
|
|
|
|
2015-05-18 18:09:16 +08:00
|
|
|
#include <rthw.h>
|
2015-02-25 10:50:21 +08:00
|
|
|
#include <rtthread.h>
|
|
|
|
|
2017-07-24 00:23:35 +08:00
|
|
|
#ifdef RT_USING_USER_MAIN
|
|
|
|
#ifndef RT_MAIN_THREAD_STACK_SIZE
|
2017-07-24 08:56:49 +08:00
|
|
|
#define RT_MAIN_THREAD_STACK_SIZE 2048
|
2021-06-09 19:50:03 +08:00
|
|
|
#endif /* RT_MAIN_THREAD_STACK_SIZE */
|
2018-07-31 17:05:01 +08:00
|
|
|
#ifndef RT_MAIN_THREAD_PRIORITY
|
|
|
|
#define RT_MAIN_THREAD_PRIORITY (RT_THREAD_PRIORITY_MAX / 3)
|
2021-06-09 19:50:03 +08:00
|
|
|
#endif /* RT_MAIN_THREAD_PRIORITY */
|
|
|
|
#endif /* RT_USING_USER_MAIN */
|
2017-07-24 00:23:35 +08:00
|
|
|
|
2015-02-25 10:50:21 +08:00
|
|
|
#ifdef RT_USING_COMPONENTS_INIT
|
|
|
|
/*
|
2017-09-15 11:02:24 +08:00
|
|
|
* Components Initialization will initialize some driver and components as following
|
2015-02-25 10:50:21 +08:00
|
|
|
* order:
|
|
|
|
* rti_start --> 0
|
|
|
|
* BOARD_EXPORT --> 1
|
|
|
|
* rti_board_end --> 1.end
|
|
|
|
*
|
|
|
|
* DEVICE_EXPORT --> 2
|
|
|
|
* COMPONENT_EXPORT --> 3
|
|
|
|
* FS_EXPORT --> 4
|
|
|
|
* ENV_EXPORT --> 5
|
2017-09-15 11:02:24 +08:00
|
|
|
* APP_EXPORT --> 6
|
|
|
|
*
|
2015-02-25 10:50:21 +08:00
|
|
|
* rti_end --> 6.end
|
|
|
|
*
|
2018-11-09 09:17:43 +08:00
|
|
|
* These automatically initialization, the driver or component initial function must
|
2015-02-25 10:50:21 +08:00
|
|
|
* be defined with:
|
|
|
|
* INIT_BOARD_EXPORT(fn);
|
|
|
|
* INIT_DEVICE_EXPORT(fn);
|
|
|
|
* ...
|
|
|
|
* INIT_APP_EXPORT(fn);
|
2017-09-15 11:02:24 +08:00
|
|
|
* etc.
|
2015-02-25 10:50:21 +08:00
|
|
|
*/
|
|
|
|
static int rti_start(void)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
INIT_EXPORT(rti_start, "0");
|
|
|
|
|
2015-11-09 15:25:48 +08:00
|
|
|
static int rti_board_start(void)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
INIT_EXPORT(rti_board_start, "0.end");
|
|
|
|
|
2015-02-25 10:50:21 +08:00
|
|
|
static int rti_board_end(void)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
INIT_EXPORT(rti_board_end, "1.end");
|
|
|
|
|
|
|
|
static int rti_end(void)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
INIT_EXPORT(rti_end, "6.end");
|
|
|
|
|
|
|
|
/**
|
2021-09-11 18:29:37 +08:00
|
|
|
* @brief Onboard components initialization. In this function, the board-level
|
|
|
|
* initialization function will be called to complete the initialization
|
|
|
|
* of the on-board peripherals.
|
2015-02-25 10:50:21 +08:00
|
|
|
*/
|
|
|
|
void rt_components_board_init(void)
|
|
|
|
{
|
|
|
|
#if RT_DEBUG_INIT
|
|
|
|
int result;
|
|
|
|
const struct rt_init_desc *desc;
|
2015-11-09 15:25:48 +08:00
|
|
|
for (desc = &__rt_init_desc_rti_board_start; desc < &__rt_init_desc_rti_board_end; desc ++)
|
2015-02-25 10:50:21 +08:00
|
|
|
{
|
|
|
|
rt_kprintf("initialize %s", desc->fn_name);
|
|
|
|
result = desc->fn();
|
|
|
|
rt_kprintf(":%d done\n", result);
|
|
|
|
}
|
|
|
|
#else
|
2020-01-02 18:35:22 +08:00
|
|
|
volatile const init_fn_t *fn_ptr;
|
2015-02-25 10:50:21 +08:00
|
|
|
|
2015-11-09 15:25:48 +08:00
|
|
|
for (fn_ptr = &__rt_init_rti_board_start; fn_ptr < &__rt_init_rti_board_end; fn_ptr++)
|
2015-02-25 10:50:21 +08:00
|
|
|
{
|
|
|
|
(*fn_ptr)();
|
|
|
|
}
|
2021-06-09 19:50:03 +08:00
|
|
|
#endif /* RT_DEBUG_INIT */
|
2015-02-25 10:50:21 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-09-10 15:52:37 +08:00
|
|
|
* @brief RT-Thread Components Initialization.
|
2015-02-25 10:50:21 +08:00
|
|
|
*/
|
|
|
|
void rt_components_init(void)
|
|
|
|
{
|
|
|
|
#if RT_DEBUG_INIT
|
|
|
|
int result;
|
|
|
|
const struct rt_init_desc *desc;
|
|
|
|
|
2018-11-09 09:17:43 +08:00
|
|
|
rt_kprintf("do components initialization.\n");
|
2015-02-25 10:50:21 +08:00
|
|
|
for (desc = &__rt_init_desc_rti_board_end; desc < &__rt_init_desc_rti_end; desc ++)
|
|
|
|
{
|
|
|
|
rt_kprintf("initialize %s", desc->fn_name);
|
|
|
|
result = desc->fn();
|
|
|
|
rt_kprintf(":%d done\n", result);
|
|
|
|
}
|
|
|
|
#else
|
2020-01-02 18:35:22 +08:00
|
|
|
volatile const init_fn_t *fn_ptr;
|
2015-02-25 10:50:21 +08:00
|
|
|
|
|
|
|
for (fn_ptr = &__rt_init_rti_board_end; fn_ptr < &__rt_init_rti_end; fn_ptr ++)
|
|
|
|
{
|
|
|
|
(*fn_ptr)();
|
|
|
|
}
|
2021-06-09 19:50:03 +08:00
|
|
|
#endif /* RT_DEBUG_INIT */
|
2015-02-25 10:50:21 +08:00
|
|
|
}
|
2021-06-09 19:50:03 +08:00
|
|
|
#endif /* RT_USING_COMPONENTS_INIT */
|
2015-02-25 10:50:21 +08:00
|
|
|
|
|
|
|
#ifdef RT_USING_USER_MAIN
|
2015-04-03 22:27:56 +08:00
|
|
|
|
2015-02-25 10:50:21 +08:00
|
|
|
void rt_application_init(void);
|
|
|
|
void rt_hw_board_init(void);
|
2015-05-18 18:09:16 +08:00
|
|
|
int rtthread_startup(void);
|
|
|
|
|
2021-06-13 17:36:42 +08:00
|
|
|
#ifdef __ARMCC_VERSION
|
2015-07-29 09:58:26 +08:00
|
|
|
extern int $Super$$main(void);
|
2015-04-03 22:27:56 +08:00
|
|
|
/* re-define main function */
|
|
|
|
int $Sub$$main(void)
|
|
|
|
{
|
|
|
|
rtthread_startup();
|
2015-07-29 09:58:26 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#elif defined(__ICCARM__)
|
|
|
|
/* __low_level_init will auto called by IAR cstartup */
|
2017-09-15 11:02:24 +08:00
|
|
|
extern void __iar_data_init3(void);
|
2015-07-29 09:58:26 +08:00
|
|
|
int __low_level_init(void)
|
|
|
|
{
|
2017-09-15 11:02:24 +08:00
|
|
|
// call IAR table copy function.
|
|
|
|
__iar_data_init3();
|
2015-07-29 09:58:26 +08:00
|
|
|
rtthread_startup();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#elif defined(__GNUC__)
|
|
|
|
/* Add -eentry to arm-none-eabi-gcc argument */
|
|
|
|
int entry(void)
|
|
|
|
{
|
|
|
|
rtthread_startup();
|
2015-04-03 22:27:56 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2015-02-25 10:50:21 +08:00
|
|
|
#ifndef RT_USING_HEAP
|
2015-07-29 09:58:26 +08:00
|
|
|
/* if there is not enable heap, we should use static thread and stack. */
|
2022-12-12 02:12:03 +08:00
|
|
|
rt_align(RT_ALIGN_SIZE)
|
2022-08-25 03:38:02 +08:00
|
|
|
static rt_uint8_t main_thread_stack[RT_MAIN_THREAD_STACK_SIZE];
|
2015-02-25 10:50:21 +08:00
|
|
|
struct rt_thread main_thread;
|
2021-06-09 19:50:03 +08:00
|
|
|
#endif /* RT_USING_HEAP */
|
2015-02-25 10:50:21 +08:00
|
|
|
|
2021-09-10 15:52:37 +08:00
|
|
|
/**
|
|
|
|
* @brief The system main thread. In this thread will call the rt_components_init()
|
|
|
|
* for initialization of RT-Thread Components and call the user's programming
|
|
|
|
* entry main().
|
|
|
|
*/
|
2015-02-25 10:50:21 +08:00
|
|
|
void main_thread_entry(void *parameter)
|
|
|
|
{
|
|
|
|
extern int main(void);
|
2020-01-15 09:14:58 +08:00
|
|
|
|
2019-09-11 12:01:24 +08:00
|
|
|
#ifdef RT_USING_COMPONENTS_INIT
|
2015-02-25 10:50:21 +08:00
|
|
|
/* RT-Thread components initialization */
|
|
|
|
rt_components_init();
|
2021-06-09 19:50:03 +08:00
|
|
|
#endif /* RT_USING_COMPONENTS_INIT */
|
2021-02-05 18:27:41 +08:00
|
|
|
|
2018-11-22 14:40:43 +08:00
|
|
|
#ifdef RT_USING_SMP
|
|
|
|
rt_hw_secondary_cpu_up();
|
2021-06-09 19:50:03 +08:00
|
|
|
#endif /* RT_USING_SMP */
|
2015-02-25 10:50:21 +08:00
|
|
|
/* invoke system main function */
|
2021-06-13 17:36:42 +08:00
|
|
|
#ifdef __ARMCC_VERSION
|
2021-02-05 18:27:41 +08:00
|
|
|
{
|
|
|
|
extern int $Super$$main(void);
|
|
|
|
$Super$$main(); /* for ARMCC. */
|
|
|
|
}
|
2022-08-30 03:35:23 +08:00
|
|
|
#elif defined(__ICCARM__) || defined(__GNUC__) || defined(__TASKING__) || defined(__TI_COMPILER_VERSION__)
|
2015-02-25 10:50:21 +08:00
|
|
|
main();
|
2015-04-03 22:27:56 +08:00
|
|
|
#endif
|
2015-02-25 10:50:21 +08:00
|
|
|
}
|
|
|
|
|
2021-09-10 15:52:37 +08:00
|
|
|
/**
|
|
|
|
* @brief This function will create and start the main thread, but this thread
|
2021-09-10 16:31:29 +08:00
|
|
|
* will not run until the scheduler starts.
|
2021-09-10 15:52:37 +08:00
|
|
|
*/
|
2015-02-25 10:50:21 +08:00
|
|
|
void rt_application_init(void)
|
|
|
|
{
|
|
|
|
rt_thread_t tid;
|
|
|
|
|
|
|
|
#ifdef RT_USING_HEAP
|
|
|
|
tid = rt_thread_create("main", main_thread_entry, RT_NULL,
|
2018-07-31 17:05:01 +08:00
|
|
|
RT_MAIN_THREAD_STACK_SIZE, RT_MAIN_THREAD_PRIORITY, 20);
|
2015-02-25 10:50:21 +08:00
|
|
|
RT_ASSERT(tid != RT_NULL);
|
|
|
|
#else
|
|
|
|
rt_err_t result;
|
|
|
|
|
|
|
|
tid = &main_thread;
|
|
|
|
result = rt_thread_init(tid, "main", main_thread_entry, RT_NULL,
|
2022-08-25 03:38:02 +08:00
|
|
|
main_thread_stack, sizeof(main_thread_stack), RT_MAIN_THREAD_PRIORITY, 20);
|
2017-01-02 19:52:00 +08:00
|
|
|
RT_ASSERT(result == RT_EOK);
|
2019-07-27 17:33:36 +08:00
|
|
|
|
2018-02-28 22:53:47 +08:00
|
|
|
/* if not define RT_USING_HEAP, using to eliminate the warning */
|
|
|
|
(void)result;
|
2021-06-09 19:50:03 +08:00
|
|
|
#endif /* RT_USING_HEAP */
|
2015-02-25 10:50:21 +08:00
|
|
|
|
|
|
|
rt_thread_startup(tid);
|
|
|
|
}
|
|
|
|
|
2021-09-10 15:52:37 +08:00
|
|
|
/**
|
2021-09-11 18:29:37 +08:00
|
|
|
* @brief This function will call all levels of initialization functions to complete
|
|
|
|
* the initialization of the system, and finally start the scheduler.
|
2021-09-10 15:52:37 +08:00
|
|
|
*/
|
2015-02-25 10:50:21 +08:00
|
|
|
int rtthread_startup(void)
|
|
|
|
{
|
2017-09-15 11:02:24 +08:00
|
|
|
rt_hw_interrupt_disable();
|
2015-04-16 16:47:48 +08:00
|
|
|
|
2018-11-09 09:17:43 +08:00
|
|
|
/* board level initialization
|
2015-02-25 10:50:21 +08:00
|
|
|
* NOTE: please initialize heap inside board initialization.
|
|
|
|
*/
|
|
|
|
rt_hw_board_init();
|
|
|
|
|
|
|
|
/* show RT-Thread version */
|
|
|
|
rt_show_version();
|
|
|
|
|
|
|
|
/* timer system initialization */
|
|
|
|
rt_system_timer_init();
|
|
|
|
|
|
|
|
/* scheduler system initialization */
|
|
|
|
rt_system_scheduler_init();
|
|
|
|
|
2017-10-15 22:31:53 +08:00
|
|
|
#ifdef RT_USING_SIGNALS
|
|
|
|
/* signal system initialization */
|
|
|
|
rt_system_signal_init();
|
2021-06-09 19:50:03 +08:00
|
|
|
#endif /* RT_USING_SIGNALS */
|
2017-10-15 22:31:53 +08:00
|
|
|
|
2015-02-25 10:50:21 +08:00
|
|
|
/* create init_thread */
|
|
|
|
rt_application_init();
|
|
|
|
|
|
|
|
/* timer thread initialization */
|
|
|
|
rt_system_timer_thread_init();
|
|
|
|
|
|
|
|
/* idle thread initialization */
|
|
|
|
rt_thread_idle_init();
|
|
|
|
|
2018-11-22 14:40:43 +08:00
|
|
|
#ifdef RT_USING_SMP
|
|
|
|
rt_hw_spin_lock(&_cpus_lock);
|
2021-06-09 19:50:03 +08:00
|
|
|
#endif /* RT_USING_SMP */
|
2018-11-22 14:40:43 +08:00
|
|
|
|
2015-02-25 10:50:21 +08:00
|
|
|
/* start scheduler */
|
|
|
|
rt_system_scheduler_start();
|
|
|
|
|
|
|
|
/* never reach here */
|
2015-05-18 18:09:16 +08:00
|
|
|
return 0;
|
2015-02-25 10:50:21 +08:00
|
|
|
}
|
2021-06-09 19:50:03 +08:00
|
|
|
#endif /* RT_USING_USER_MAIN */
|