components.c 5.4 KB

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