board.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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. #include "drv_uart.h"
  22. #include "shell.h"
  23. #ifdef RT_USING_COMPONENTS_INIT
  24. /*
  25. * Components Initialization will initialize some driver and components as following
  26. * order:
  27. * rti_start --> 0
  28. * BOARD_EXPORT --> 1
  29. * rti_board_end --> 1.end
  30. *
  31. * DEVICE_EXPORT --> 2
  32. * COMPONENT_EXPORT --> 3
  33. * FS_EXPORT --> 4
  34. * ENV_EXPORT --> 5
  35. * APP_EXPORT --> 6
  36. *
  37. * rti_end --> 6.end
  38. *
  39. * These automatically initialization, the driver or component initial function must
  40. * be defined with:
  41. * INIT_BOARD_EXPORT(fn);
  42. * INIT_DEVICE_EXPORT(fn);
  43. * ...
  44. * INIT_APP_EXPORT(fn);
  45. * etc.
  46. */
  47. static int rti_start(void)
  48. {
  49. return 0;
  50. }
  51. INIT_EXPORT(rti_start, "0");
  52. static int rti_board_start(void)
  53. {
  54. return 0;
  55. }
  56. INIT_EXPORT(rti_board_start, "0.end");
  57. static int rti_board_end(void)
  58. {
  59. return 0;
  60. }
  61. INIT_EXPORT(rti_board_end, "1.end");
  62. static int rti_end(void)
  63. {
  64. return 0;
  65. }
  66. INIT_EXPORT(rti_end, "6.end");
  67. /**
  68. * @brief Onboard components initialization. In this function, the board-level
  69. * initialization function will be called to complete the initialization
  70. * of the on-board peripherals.
  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_board_start; desc < &__rt_init_desc_rti_board_end; desc ++)
  78. {
  79. rt_kprintf("rt_components_board_init initialize %s", desc->fn_name);
  80. result = desc->fn();
  81. rt_kprintf(":%d done\n", result);
  82. }
  83. #else
  84. volatile const init_fn_t *fn_ptr;
  85. for (fn_ptr = &__rt_init_rti_board_start; fn_ptr < &__rt_init_rti_board_end; fn_ptr++)
  86. {
  87. (*fn_ptr)();
  88. }
  89. #endif /* RT_DEBUG_INIT */
  90. }
  91. /**
  92. * @brief 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 initialization.\n");
  100. for (desc = &__rt_init_desc_rti_board_end; desc < &__rt_init_desc_rti_end; desc ++)
  101. {
  102. rt_kprintf("rt_components_init initialize %s", desc->fn_name);
  103. result = desc->fn();
  104. rt_kprintf(":%d done\n", result);
  105. }
  106. #else
  107. volatile 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 /* RT_DEBUG_INIT */
  113. }
  114. #endif /* RT_USING_COMPONENTS_INIT */
  115. void main_thread_entry(void *parameter)
  116. {
  117. #ifdef RT_USING_COMPONENTS_INIT
  118. /* RT-Thread components initialization */
  119. rt_components_init();
  120. #endif /* RT_USING_COMPONENTS_INIT */
  121. extern int rtt_main();
  122. rtt_main();
  123. }
  124. void rt_application_init(void)
  125. {
  126. rt_thread_t tid;
  127. #define RT_MAIN_THREAD_STACK_SIZE 2048
  128. #define RT_MAIN_THREAD_PRIORITY 10
  129. tid = rt_thread_create("main", main_thread_entry, RT_NULL,
  130. RT_MAIN_THREAD_STACK_SIZE, RT_MAIN_THREAD_PRIORITY, 20);
  131. RT_ASSERT(tid != RT_NULL);
  132. rt_thread_startup(tid);
  133. }
  134. //component
  135. static uint32_t uwTick = 0;
  136. static systimer_hal_context_t systimer_hal;
  137. IRAM_ATTR void rt_SysTickIsrHandler(void *arg)
  138. {
  139. systimer_ll_clear_alarm_int(systimer_hal.dev, 1);
  140. rt_interrupt_enter();
  141. rt_tick_increase();
  142. uwTick++;
  143. /* leave interrupt */
  144. rt_interrupt_leave();
  145. systimer_ll_is_alarm_int_fired(systimer_hal.dev, 1);
  146. }
  147. void rt_hw_systick_init(void)
  148. {
  149. uint8_t system_timer_counter=1;
  150. //rt_hw_interrupt_enable(0);
  151. esp_intr_alloc(ETS_SYSTIMER_TARGET1_EDGE_INTR_SOURCE, ESP_INTR_FLAG_IRAM | ESP_INTR_FLAG_LEVEL1, rt_SysTickIsrHandler, &systimer_hal, NULL);
  152. systimer_hal_init(&systimer_hal);
  153. systimer_ll_set_counter_value(systimer_hal.dev, system_timer_counter, 0);
  154. systimer_ll_apply_counter_value(systimer_hal.dev, system_timer_counter);
  155. uint32_t alarm_id = 1 ;
  156. systimer_hal_connect_alarm_counter(&systimer_hal, alarm_id, system_timer_counter);
  157. systimer_hal_set_alarm_period(&systimer_hal, alarm_id, 1000000UL / 1000);
  158. systimer_hal_select_alarm_mode(&systimer_hal, alarm_id, SYSTIMER_ALARM_MODE_PERIOD);
  159. systimer_hal_counter_can_stall_by_cpu(&systimer_hal, 1, 0, true);
  160. systimer_hal_enable_alarm_int(&systimer_hal, alarm_id);
  161. systimer_hal_enable_counter(&systimer_hal, SYSTIMER_LL_COUNTER_OS_TICK);
  162. }
  163. void rt_hw_board_init(void)
  164. {
  165. rt_hw_systick_init();
  166. /* Board underlying hardware initialization */
  167. #ifdef RT_USING_COMPONENTS_INIT
  168. rt_components_board_init();
  169. #endif
  170. #if defined(RT_USING_CONSOLE) && defined(RT_USING_DEVICE)
  171. rt_console_set_device(RT_CONSOLE_DEVICE_NAME);
  172. #endif
  173. }
  174. static void rtthread_startup(void)
  175. {
  176. rt_hw_interrupt_disable();
  177. /* init board */
  178. rt_hw_board_init();
  179. /* timer system initialization */
  180. rt_system_timer_init();
  181. /* create init_thread */
  182. rt_application_init();
  183. /* timer thread initialization */
  184. rt_system_timer_thread_init();
  185. /* idle thread initialization */
  186. rt_thread_idle_init();
  187. /* start scheduler */
  188. rt_system_scheduler_start();
  189. /* init scheduler system */
  190. rt_hw_pin_init();
  191. rt_hw_uart_init();
  192. finsh_system_init();
  193. /* never reach here */
  194. return ;
  195. }
  196. void app_main(void)
  197. {
  198. /* startup RT-Thread RTOS */
  199. rtthread_startup();
  200. return;
  201. }