components.c 6.1 KB

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