components.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. /*
  2. * Copyright (c) 2006-2022, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2012-09-20 Bernard Change the name to components.c
  9. * And all components related header files.
  10. * 2012-12-23 Bernard fix the pthread initialization issue.
  11. * 2013-06-23 Bernard Add the init_call for components initialization.
  12. * 2013-07-05 Bernard Remove initialization feature for MS VC++ compiler
  13. * 2015-02-06 Bernard Remove the MS VC++ support and move to the kernel
  14. * 2015-05-04 Bernard Rename it to components.c because compiling issue
  15. * in some IDEs.
  16. * 2015-07-29 Arda.Fu Add support to use RT_USING_USER_MAIN with IAR
  17. * 2018-11-22 Jesven Add secondary cpu boot up
  18. * 2023-09-15 xqyjlj perf rt_hw_interrupt_disable/enable
  19. */
  20. #include <rthw.h>
  21. #include <rtthread.h>
  22. #ifdef RT_USING_USER_MAIN
  23. #ifndef RT_MAIN_THREAD_STACK_SIZE
  24. #define RT_MAIN_THREAD_STACK_SIZE 2048
  25. #endif /* RT_MAIN_THREAD_STACK_SIZE */
  26. #ifndef RT_MAIN_THREAD_PRIORITY
  27. #define RT_MAIN_THREAD_PRIORITY (RT_THREAD_PRIORITY_MAX / 3)
  28. #endif /* RT_MAIN_THREAD_PRIORITY */
  29. #if (RT_MAIN_THREAD_PRIORITY >= RT_THREAD_PRIORITY_MAX)
  30. #error "RT_MAIN_THREAD_PRIORITY must be < RT_THREAD_PRIORITY_MAX"
  31. #elif (RT_MAIN_THREAD_PRIORITY < 0)
  32. #error "RT_MAIN_THREAD_PRIORITY must be non-negative"
  33. #endif /* RT_MAIN_THREAD_PRIORITY range check */
  34. #endif /* RT_USING_USER_MAIN */
  35. #ifdef RT_USING_COMPONENTS_INIT
  36. /*
  37. * Components Initialization will initialize some driver and components as following
  38. * order:
  39. * rti_start --> 0
  40. * BOARD_EXPORT --> 1
  41. * rti_board_end --> 1.end
  42. *
  43. * DEVICE_EXPORT --> 2
  44. * COMPONENT_EXPORT --> 3
  45. * FS_EXPORT --> 4
  46. * ENV_EXPORT --> 5
  47. * APP_EXPORT --> 6
  48. *
  49. * rti_end --> 6.end
  50. *
  51. * These automatically initialization, the driver or component initial function must
  52. * be defined with:
  53. * INIT_BOARD_EXPORT(fn);
  54. * INIT_DEVICE_EXPORT(fn);
  55. * ...
  56. * INIT_APP_EXPORT(fn);
  57. * etc.
  58. */
  59. static int rti_start(void)
  60. {
  61. return 0;
  62. }
  63. INIT_EXPORT(rti_start, "0");
  64. static int rti_board_start(void)
  65. {
  66. return 0;
  67. }
  68. INIT_EXPORT(rti_board_start, "0.end");
  69. static int rti_board_end(void)
  70. {
  71. return 0;
  72. }
  73. INIT_EXPORT(rti_board_end, "1.end");
  74. static int rti_end(void)
  75. {
  76. return 0;
  77. }
  78. INIT_EXPORT(rti_end, "6.end");
  79. /**
  80. * @brief Onboard components initialization. In this function, the board-level
  81. * initialization function will be called to complete the initialization
  82. * of the on-board peripherals.
  83. */
  84. void rt_components_board_init(void)
  85. {
  86. #ifdef RT_DEBUGING_AUTO_INIT
  87. int result;
  88. const struct rt_init_desc *desc;
  89. for (desc = &__rt_init_desc_rti_board_start; desc < &__rt_init_desc_rti_board_end; desc ++)
  90. {
  91. rt_kprintf("initialize %s\n", desc->fn_name);
  92. result = desc->fn();
  93. rt_kprintf(":%d done\n", result);
  94. }
  95. #else
  96. volatile const init_fn_t *fn_ptr;
  97. for (fn_ptr = &__rt_init_rti_board_start; fn_ptr < &__rt_init_rti_board_end; fn_ptr++)
  98. {
  99. (*fn_ptr)();
  100. }
  101. #endif /* RT_DEBUGING_AUTO_INIT */
  102. }
  103. /**
  104. * @brief RT-Thread Components Initialization.
  105. */
  106. void rt_components_init(void)
  107. {
  108. #ifdef RT_DEBUGING_AUTO_INIT
  109. int result;
  110. const struct rt_init_desc *desc;
  111. rt_kprintf("do components initialization.\n");
  112. for (desc = &__rt_init_desc_rti_board_end; desc < &__rt_init_desc_rti_end; desc ++)
  113. {
  114. rt_kprintf("initialize %s\n", desc->fn_name);
  115. result = desc->fn();
  116. rt_kprintf(":%d done\n", result);
  117. }
  118. #else
  119. volatile const init_fn_t *fn_ptr;
  120. for (fn_ptr = &__rt_init_rti_board_end; fn_ptr < &__rt_init_rti_end; fn_ptr ++)
  121. {
  122. (*fn_ptr)();
  123. }
  124. #endif /* RT_DEBUGING_AUTO_INIT */
  125. }
  126. #endif /* RT_USING_COMPONENTS_INIT */
  127. #ifdef RT_USING_USER_MAIN
  128. void rt_application_init(void);
  129. void rt_hw_board_init(void);
  130. int rtthread_startup(void);
  131. #ifdef __ARMCC_VERSION
  132. extern int $Super$$main(void);
  133. /* re-define main function */
  134. int $Sub$$main(void)
  135. {
  136. rtthread_startup();
  137. return 0;
  138. }
  139. #elif defined(__ICCARM__)
  140. /* __low_level_init will auto called by IAR cstartup */
  141. extern void __iar_data_init3(void);
  142. int __low_level_init(void)
  143. {
  144. // call IAR table copy function.
  145. __iar_data_init3();
  146. rtthread_startup();
  147. return 0;
  148. }
  149. #elif defined(__GNUC__)
  150. /* Add -eentry to arm-none-eabi-gcc argument */
  151. int entry(void)
  152. {
  153. rtthread_startup();
  154. return 0;
  155. }
  156. #endif
  157. #ifndef RT_USING_HEAP
  158. /* if there is not enable heap, we should use static thread and stack. */
  159. rt_align(RT_ALIGN_SIZE)
  160. static rt_uint8_t main_thread_stack[RT_MAIN_THREAD_STACK_SIZE];
  161. struct rt_thread main_thread;
  162. #endif /* RT_USING_HEAP */
  163. /**
  164. * @brief The system main thread. In this thread will call the rt_components_init()
  165. * for initialization of RT-Thread Components and call the user's programming
  166. * entry main().
  167. *
  168. * @param parameter is the arg of the thread.
  169. */
  170. static void main_thread_entry(void *parameter)
  171. {
  172. extern int main(void);
  173. RT_UNUSED(parameter);
  174. #ifdef RT_USING_COMPONENTS_INIT
  175. /* RT-Thread components initialization */
  176. rt_components_init();
  177. #endif /* RT_USING_COMPONENTS_INIT */
  178. #ifdef RT_USING_SMP
  179. rt_hw_secondary_cpu_up();
  180. #endif /* RT_USING_SMP */
  181. /* invoke system main function */
  182. #ifdef __ARMCC_VERSION
  183. {
  184. extern int $Super$$main(void);
  185. $Super$$main(); /* for ARMCC. */
  186. }
  187. #elif defined(__ICCARM__) || defined(__GNUC__) || defined(__TASKING__) || defined(__TI_COMPILER_VERSION__)
  188. main();
  189. #endif /* __ARMCC_VERSION */
  190. }
  191. /**
  192. * @brief This function will create and start the main thread, but this thread
  193. * will not run until the scheduler starts.
  194. */
  195. void rt_application_init(void)
  196. {
  197. rt_thread_t tid;
  198. #ifdef RT_USING_HEAP
  199. tid = rt_thread_create("main", main_thread_entry, RT_NULL,
  200. RT_MAIN_THREAD_STACK_SIZE, RT_MAIN_THREAD_PRIORITY, 20);
  201. RT_ASSERT(tid != RT_NULL);
  202. #else
  203. rt_err_t result;
  204. tid = &main_thread;
  205. result = rt_thread_init(tid, "main", main_thread_entry, RT_NULL,
  206. main_thread_stack, sizeof(main_thread_stack), RT_MAIN_THREAD_PRIORITY, 20);
  207. RT_ASSERT(result == RT_EOK);
  208. /* if not define RT_USING_HEAP, using to eliminate the warning */
  209. (void)result;
  210. #endif /* RT_USING_HEAP */
  211. rt_thread_startup(tid);
  212. }
  213. /**
  214. * @brief This function will call all levels of initialization functions to complete
  215. * the initialization of the system, and finally start the scheduler.
  216. *
  217. * @return Normally never returns. If 0 is returned, the scheduler failed.
  218. */
  219. int rtthread_startup(void)
  220. {
  221. #ifdef RT_USING_SMP
  222. rt_hw_spin_lock_init(&_cpus_lock);
  223. #endif
  224. rt_hw_local_irq_disable();
  225. /* board level initialization
  226. * NOTE: please initialize heap inside board initialization.
  227. */
  228. rt_hw_board_init();
  229. /* show RT-Thread version */
  230. rt_show_version();
  231. /* timer system initialization */
  232. rt_system_timer_init();
  233. /* scheduler system initialization */
  234. rt_system_scheduler_init();
  235. #ifdef RT_USING_SIGNALS
  236. /* signal system initialization */
  237. rt_system_signal_init();
  238. #endif /* RT_USING_SIGNALS */
  239. /* create init_thread */
  240. rt_application_init();
  241. /* timer thread initialization */
  242. rt_system_timer_thread_init();
  243. /* idle thread initialization */
  244. rt_thread_idle_init();
  245. /* defunct thread initialization */
  246. rt_thread_defunct_init();
  247. #ifdef RT_USING_SMP
  248. rt_hw_spin_lock(&_cpus_lock);
  249. #endif /* RT_USING_SMP */
  250. /* start scheduler */
  251. rt_system_scheduler_start();
  252. /* never reach here */
  253. return 0;
  254. }
  255. #endif /* RT_USING_USER_MAIN */