components.c 6.4 KB

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