components.c 5.9 KB

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