components.c 6.5 KB

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