components.c 6.0 KB

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