components.c 5.8 KB

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