board.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. /*
  2. * Copyright (c) 2021-2022, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2022-06-02 supperthomas first version
  9. */
  10. #include <stdio.h>
  11. #include "esp_private/panic_internal.h"
  12. #include "hal/uart_hal.h"
  13. #include "driver/timer.h"
  14. #include "soc/periph_defs.h"
  15. #include "hal/systimer_hal.h"
  16. #include "hal/systimer_ll.h"
  17. #include "esp_intr_alloc.h"
  18. #include "rtthread.h"
  19. #include "rthw.h"
  20. #include "drv_gpio.h"
  21. #define rt_kprintf printf
  22. #ifdef RT_USING_COMPONENTS_INIT
  23. /*
  24. * Components Initialization will initialize some driver and components as following
  25. * order:
  26. * rti_start --> 0
  27. * BOARD_EXPORT --> 1
  28. * rti_board_end --> 1.end
  29. *
  30. * DEVICE_EXPORT --> 2
  31. * COMPONENT_EXPORT --> 3
  32. * FS_EXPORT --> 4
  33. * ENV_EXPORT --> 5
  34. * APP_EXPORT --> 6
  35. *
  36. * rti_end --> 6.end
  37. *
  38. * These automatically initialization, the driver or component initial function must
  39. * be defined with:
  40. * INIT_BOARD_EXPORT(fn);
  41. * INIT_DEVICE_EXPORT(fn);
  42. * ...
  43. * INIT_APP_EXPORT(fn);
  44. * etc.
  45. */
  46. static int rti_start(void)
  47. {
  48. return 0;
  49. }
  50. INIT_EXPORT(rti_start, "0");
  51. static int rti_board_start(void)
  52. {
  53. return 0;
  54. }
  55. INIT_EXPORT(rti_board_start, "0.end");
  56. static int rti_board_end(void)
  57. {
  58. return 0;
  59. }
  60. INIT_EXPORT(rti_board_end, "1.end");
  61. static int rti_end(void)
  62. {
  63. return 0;
  64. }
  65. INIT_EXPORT(rti_end, "6.end");
  66. /**
  67. * @brief Onboard components initialization. In this function, the board-level
  68. * initialization function will be called to complete the initialization
  69. * of the on-board peripherals.
  70. */
  71. void rt_components_board_init(void)
  72. {
  73. #if RT_DEBUG_INIT
  74. int result;
  75. const struct rt_init_desc *desc;
  76. for (desc = &__rt_init_desc_rti_board_start; desc < &__rt_init_desc_rti_board_end; desc ++)
  77. {
  78. rt_kprintf("rt_components_board_init initialize %s", desc->fn_name);
  79. result = desc->fn();
  80. rt_kprintf(":%d done\n", result);
  81. }
  82. #else
  83. volatile const init_fn_t *fn_ptr;
  84. for (fn_ptr = &__rt_init_rti_board_start; fn_ptr < &__rt_init_rti_board_end; fn_ptr++)
  85. {
  86. (*fn_ptr)();
  87. }
  88. #endif /* RT_DEBUG_INIT */
  89. }
  90. /**
  91. * @brief RT-Thread Components Initialization.
  92. */
  93. void rt_components_init(void)
  94. {
  95. #if RT_DEBUG_INIT
  96. int result;
  97. const struct rt_init_desc *desc;
  98. rt_kprintf("do components initialization.\n");
  99. for (desc = &__rt_init_desc_rti_board_end; desc < &__rt_init_desc_rti_end; desc ++)
  100. {
  101. rt_kprintf("rt_components_init initialize %s", desc->fn_name);
  102. result = desc->fn();
  103. rt_kprintf(":%d done\n", result);
  104. }
  105. #else
  106. volatile const init_fn_t *fn_ptr;
  107. for (fn_ptr = &__rt_init_rti_board_end; fn_ptr < &__rt_init_rti_end; fn_ptr ++)
  108. {
  109. (*fn_ptr)();
  110. }
  111. #endif /* RT_DEBUG_INIT */
  112. }
  113. #endif /* RT_USING_COMPONENTS_INIT */
  114. void main_thread_entry(void *parameter)
  115. {
  116. #ifdef RT_USING_COMPONENTS_INIT
  117. /* RT-Thread components initialization */
  118. rt_components_init();
  119. #endif /* RT_USING_COMPONENTS_INIT */
  120. extern int rtt_main();
  121. rtt_main();
  122. }
  123. void rt_application_init(void)
  124. {
  125. rt_thread_t tid;
  126. #define RT_MAIN_THREAD_STACK_SIZE 2048
  127. #define RT_MAIN_THREAD_PRIORITY 10
  128. tid = rt_thread_create("main", main_thread_entry, RT_NULL,
  129. RT_MAIN_THREAD_STACK_SIZE, RT_MAIN_THREAD_PRIORITY, 20);
  130. RT_ASSERT(tid != RT_NULL);
  131. rt_thread_startup(tid);
  132. }
  133. //component
  134. static uint32_t uwTick = 0;
  135. static systimer_hal_context_t systimer_hal;
  136. IRAM_ATTR void rt_SysTickIsrHandler(void *arg)
  137. {
  138. systimer_ll_clear_alarm_int(systimer_hal.dev, 1);
  139. rt_interrupt_enter();
  140. rt_tick_increase();
  141. uwTick++;
  142. /* leave interrupt */
  143. rt_interrupt_leave();
  144. systimer_ll_is_alarm_int_fired(systimer_hal.dev, 1);
  145. }
  146. void rt_hw_systick_init(void)
  147. {
  148. uint8_t system_timer_counter=1;
  149. //rt_hw_interrupt_enable(0);
  150. esp_intr_alloc(ETS_SYSTIMER_TARGET1_EDGE_INTR_SOURCE, ESP_INTR_FLAG_IRAM | ESP_INTR_FLAG_LEVEL1, rt_SysTickIsrHandler, &systimer_hal, NULL);
  151. systimer_hal_init(&systimer_hal);
  152. systimer_ll_set_counter_value(systimer_hal.dev, system_timer_counter, 0);
  153. systimer_ll_apply_counter_value(systimer_hal.dev, system_timer_counter);
  154. uint32_t alarm_id = 1 ;
  155. systimer_hal_connect_alarm_counter(&systimer_hal, alarm_id, system_timer_counter);
  156. systimer_hal_set_alarm_period(&systimer_hal, alarm_id, 1000000UL / 1000);
  157. systimer_hal_select_alarm_mode(&systimer_hal, alarm_id, SYSTIMER_ALARM_MODE_PERIOD);
  158. systimer_hal_counter_can_stall_by_cpu(&systimer_hal, 1, 0, true);
  159. systimer_hal_enable_alarm_int(&systimer_hal, alarm_id);
  160. systimer_hal_enable_counter(&systimer_hal, SYSTIMER_LL_COUNTER_OS_TICK);
  161. }
  162. void rt_hw_board_init(void)
  163. {
  164. rt_hw_systick_init();
  165. #if defined(RT_USING_HEAP)
  166. extern int __heap_start__;
  167. extern int __heap_end__;
  168. printf("%s:%d__heap_start__:%p,__heap_end__:%p\n",__func__,__LINE__,&__heap_start__,&__heap_end__);
  169. rt_system_heap_init((void *)&__heap_start__, (void *)&__heap_end__);
  170. #endif
  171. /* Board underlying hardware initialization */
  172. #ifdef RT_USING_COMPONENTS_INIT
  173. rt_components_board_init();
  174. #endif
  175. }
  176. static void rtthread_startup(void)
  177. {
  178. rt_hw_interrupt_disable();
  179. /* init board */
  180. rt_hw_board_init();
  181. /* show RT-Thread version */
  182. rt_show_version();
  183. /* timer system initialization */
  184. rt_system_timer_init();
  185. /* scheduler system initialization */
  186. rt_system_scheduler_init();
  187. /* create init_thread */
  188. rt_application_init();
  189. /* timer thread initialization */
  190. rt_system_timer_thread_init();
  191. /* idle thread initialization */
  192. rt_thread_idle_init();
  193. /* start scheduler */
  194. rt_system_scheduler_start();
  195. /* init scheduler system */
  196. rt_hw_pin_init();
  197. /* never reach here */
  198. return ;
  199. }
  200. void app_main(void)
  201. {
  202. /* startup RT-Thread RTOS */
  203. rtthread_startup();
  204. return;
  205. }