init.c 5.2 KB

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