components.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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-0504 Bernard Rename it to components.c because compiling issue
  29. * in some IDEs.
  30. */
  31. #include <rtthread.h>
  32. #ifdef RT_USING_COMPONENTS_INIT
  33. /*
  34. * Components Initialization will initialize some driver and components as following
  35. * order:
  36. * rti_start --> 0
  37. * BOARD_EXPORT --> 1
  38. * rti_board_end --> 1.end
  39. *
  40. * DEVICE_EXPORT --> 2
  41. * COMPONENT_EXPORT --> 3
  42. * FS_EXPORT --> 4
  43. * ENV_EXPORT --> 5
  44. * APP_EXPORT --> 6
  45. *
  46. * rti_end --> 6.end
  47. *
  48. * These automatically initializaiton, the driver or component initial function must
  49. * be defined with:
  50. * INIT_BOARD_EXPORT(fn);
  51. * INIT_DEVICE_EXPORT(fn);
  52. * ...
  53. * INIT_APP_EXPORT(fn);
  54. * etc.
  55. */
  56. static int rti_start(void)
  57. {
  58. return 0;
  59. }
  60. INIT_EXPORT(rti_start, "0");
  61. static int rti_board_end(void)
  62. {
  63. return 0;
  64. }
  65. INIT_EXPORT(rti_board_end, "1.end");
  66. static int rti_end(void)
  67. {
  68. return 0;
  69. }
  70. INIT_EXPORT(rti_end, "6.end");
  71. /**
  72. * RT-Thread Components Initialization for board
  73. */
  74. void rt_components_board_init(void)
  75. {
  76. #if RT_DEBUG_INIT
  77. int result;
  78. const struct rt_init_desc *desc;
  79. for (desc = &__rt_init_desc_rti_start; desc < &__rt_init_desc_rti_board_end; desc ++)
  80. {
  81. rt_kprintf("initialize %s", desc->fn_name);
  82. result = desc->fn();
  83. rt_kprintf(":%d done\n", result);
  84. }
  85. #else
  86. const init_fn_t *fn_ptr;
  87. for (fn_ptr = &__rt_init_rti_start; fn_ptr < &__rt_init_rti_board_end; fn_ptr++)
  88. {
  89. (*fn_ptr)();
  90. }
  91. #endif
  92. }
  93. /**
  94. * RT-Thread Components Initialization
  95. */
  96. void rt_components_init(void)
  97. {
  98. #if RT_DEBUG_INIT
  99. int result;
  100. const struct rt_init_desc *desc;
  101. rt_kprintf("do components intialization.\n");
  102. for (desc = &__rt_init_desc_rti_board_end; desc < &__rt_init_desc_rti_end; desc ++)
  103. {
  104. rt_kprintf("initialize %s", desc->fn_name);
  105. result = desc->fn();
  106. rt_kprintf(":%d done\n", result);
  107. }
  108. #else
  109. const init_fn_t *fn_ptr;
  110. for (fn_ptr = &__rt_init_rti_board_end; fn_ptr < &__rt_init_rti_end; fn_ptr ++)
  111. {
  112. (*fn_ptr)();
  113. }
  114. #endif
  115. }
  116. #ifdef RT_USING_USER_MAIN
  117. void rt_application_init(void);
  118. void rt_hw_board_init(void);
  119. #ifdef __CC_ARM
  120. extern int $Super$$main(void);
  121. /* re-define main function */
  122. int $Sub$$main(void)
  123. {
  124. rt_hw_interrupt_disable();
  125. rtthread_startup();
  126. return 0;
  127. }
  128. #endif
  129. #ifndef RT_USING_HEAP
  130. /* if there is not enble heap, we should use static thread and stack. */
  131. ALIGN(8)
  132. static rt_uint8_t main_stack[2048];
  133. struct rt_thread main_thread;
  134. #endif
  135. /* the system main thread */
  136. void main_thread_entry(void *parameter)
  137. {
  138. extern int main(void);
  139. extern int $Super$$main(void);
  140. /* RT-Thread components initialization */
  141. rt_components_init();
  142. /* invoke system main function */
  143. #ifdef __CC_ARM
  144. $Super$$main(); /* for ARMCC. */
  145. #else
  146. main();
  147. #endif
  148. }
  149. void rt_application_init(void)
  150. {
  151. rt_thread_t tid;
  152. #ifdef RT_USING_HEAP
  153. tid = rt_thread_create("main", main_thread_entry, RT_NULL,
  154. 2048, RT_THREAD_PRIORITY_MAX / 3, 20);
  155. RT_ASSERT(tid != RT_NULL);
  156. #else
  157. rt_err_t result;
  158. tid = &main_thread;
  159. result = rt_thread_init(tid, "main", main_thread_entry, RT_NULL,
  160. 2048, RT_THREAD_PRIORITY_MAX / 3, 20);
  161. RT_ASSERT(result != RT_EOK);
  162. #endif
  163. rt_thread_startup(tid);
  164. }
  165. int rtthread_startup(void)
  166. {
  167. rt_hw_interrupt_disable();
  168. /* board level initalization
  169. * NOTE: please initialize heap inside board initialization.
  170. */
  171. rt_hw_board_init();
  172. /* show RT-Thread version */
  173. rt_show_version();
  174. /* timer system initialization */
  175. rt_system_timer_init();
  176. /* scheduler system initialization */
  177. rt_system_scheduler_init();
  178. /* create init_thread */
  179. rt_application_init();
  180. /* timer thread initialization */
  181. rt_system_timer_thread_init();
  182. /* idle thread initialization */
  183. rt_thread_idle_init();
  184. /* start scheduler */
  185. rt_system_scheduler_start();
  186. /* never reach here */
  187. return ;
  188. }
  189. #endif
  190. #endif