components.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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_end(void)
  64. {
  65. return 0;
  66. }
  67. INIT_EXPORT(rti_board_end, "1.end");
  68. static int rti_end(void)
  69. {
  70. return 0;
  71. }
  72. INIT_EXPORT(rti_end, "6.end");
  73. /**
  74. * RT-Thread Components Initialization for board
  75. */
  76. void rt_components_board_init(void)
  77. {
  78. #if RT_DEBUG_INIT
  79. int result;
  80. const struct rt_init_desc *desc;
  81. for (desc = &__rt_init_desc_rti_start; desc < &__rt_init_desc_rti_board_end; desc ++)
  82. {
  83. rt_kprintf("initialize %s", desc->fn_name);
  84. result = desc->fn();
  85. rt_kprintf(":%d done\n", result);
  86. }
  87. #else
  88. const init_fn_t *fn_ptr;
  89. for (fn_ptr = &__rt_init_rti_start; fn_ptr < &__rt_init_rti_board_end; fn_ptr++)
  90. {
  91. (*fn_ptr)();
  92. }
  93. #endif
  94. }
  95. /**
  96. * RT-Thread Components Initialization
  97. */
  98. void rt_components_init(void)
  99. {
  100. #if RT_DEBUG_INIT
  101. int result;
  102. const struct rt_init_desc *desc;
  103. rt_kprintf("do components intialization.\n");
  104. for (desc = &__rt_init_desc_rti_board_end; desc < &__rt_init_desc_rti_end; desc ++)
  105. {
  106. rt_kprintf("initialize %s", desc->fn_name);
  107. result = desc->fn();
  108. rt_kprintf(":%d done\n", result);
  109. }
  110. #else
  111. const init_fn_t *fn_ptr;
  112. for (fn_ptr = &__rt_init_rti_board_end; fn_ptr < &__rt_init_rti_end; fn_ptr ++)
  113. {
  114. (*fn_ptr)();
  115. }
  116. #endif
  117. }
  118. #ifdef RT_USING_USER_MAIN
  119. void rt_application_init(void);
  120. void rt_hw_board_init(void);
  121. int rtthread_startup(void);
  122. #if defined (__CC_ARM)
  123. extern int $Super$$main(void);
  124. /* re-define main function */
  125. int $Sub$$main(void)
  126. {
  127. rt_hw_interrupt_disable();
  128. rtthread_startup();
  129. return 0;
  130. }
  131. #elif defined(__ICCARM__)
  132. extern int main(void);
  133. /* __low_level_init will auto called by IAR cstartup */
  134. extern void __iar_data_init3( void );
  135. int __low_level_init(void)
  136. {
  137. // call IAR table copy function.
  138. __iar_data_init3();
  139. rt_hw_interrupt_disable();
  140. rtthread_startup();
  141. return 0;
  142. }
  143. #elif defined(__GNUC__)
  144. extern int main(void);
  145. /* Add -eentry to arm-none-eabi-gcc argument */
  146. int entry(void)
  147. {
  148. rt_hw_interrupt_disable();
  149. rtthread_startup();
  150. return 0;
  151. }
  152. #endif
  153. #ifndef RT_USING_HEAP
  154. /* if there is not enable heap, we should use static thread and stack. */
  155. ALIGN(8)
  156. static rt_uint8_t main_stack[2048];
  157. struct rt_thread main_thread;
  158. #endif
  159. /* the system main thread */
  160. void main_thread_entry(void *parameter)
  161. {
  162. extern int main(void);
  163. extern int $Super$$main(void);
  164. /* RT-Thread components initialization */
  165. rt_components_init();
  166. /* invoke system main function */
  167. #if defined (__CC_ARM)
  168. $Super$$main(); /* for ARMCC. */
  169. #elif defined(__ICCARM__) || defined(__GNUC__)
  170. main();
  171. #endif
  172. }
  173. void rt_application_init(void)
  174. {
  175. rt_thread_t tid;
  176. #ifdef RT_USING_HEAP
  177. tid = rt_thread_create("main", main_thread_entry, RT_NULL,
  178. 2048, RT_THREAD_PRIORITY_MAX / 3, 20);
  179. RT_ASSERT(tid != RT_NULL);
  180. #else
  181. rt_err_t result;
  182. tid = &main_thread;
  183. result = rt_thread_init(tid, "main", main_thread_entry, RT_NULL,
  184. 2048, RT_THREAD_PRIORITY_MAX / 3, 20);
  185. RT_ASSERT(result != RT_EOK);
  186. #endif
  187. rt_thread_startup(tid);
  188. }
  189. int rtthread_startup(void)
  190. {
  191. rt_hw_interrupt_disable();
  192. /* board level initalization
  193. * NOTE: please initialize heap inside board initialization.
  194. */
  195. rt_hw_board_init();
  196. /* show RT-Thread version */
  197. rt_show_version();
  198. /* timer system initialization */
  199. rt_system_timer_init();
  200. /* scheduler system initialization */
  201. rt_system_scheduler_init();
  202. /* create init_thread */
  203. rt_application_init();
  204. /* timer thread initialization */
  205. rt_system_timer_thread_init();
  206. /* idle thread initialization */
  207. rt_thread_idle_init();
  208. /* start scheduler */
  209. rt_system_scheduler_start();
  210. /* never reach here */
  211. return 0;
  212. }
  213. #endif
  214. #endif