components.c 7.3 KB

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