drv_common.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. /*
  2. * Copyright (c) 2006-2025, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2018-11-7 SummerGift first version
  9. */
  10. #include <drv_common.h>
  11. #include <bsp_api.h>
  12. #include "board.h"
  13. #include <hal_data.h>
  14. #ifdef RT_USING_PIN
  15. #include <drv_gpio.h>
  16. #endif
  17. #ifdef RT_USING_SERIAL
  18. #ifdef RT_USING_SERIAL_V2
  19. #include <drv_usart_v2.h>
  20. #else
  21. #error "Serial-v1 has been obsoleted, and please select serial-v2 as the default option"
  22. #endif
  23. #endif
  24. #ifdef SOC_SERIES_R9A07G0
  25. #include "gicv3.h"
  26. static uint64_t rtt_timer_delay;
  27. extern fsp_vector_t g_sgi_ppi_vector_table[BSP_CORTEX_VECTOR_TABLE_ENTRIES];
  28. static void SysTimerInterrupt(void);
  29. #endif
  30. #ifdef RT_USING_FINSH
  31. #include <finsh.h>
  32. static void reboot(uint8_t argc, char **argv)
  33. {
  34. #ifdef SOC_SERIES_R9A07G0
  35. R_BSP_RegisterProtectDisable(BSP_REG_PROTECT_LPC_RESET);
  36. R_BSP_SystemReset();
  37. #else
  38. NVIC_SystemReset();
  39. #endif
  40. }
  41. MSH_CMD_EXPORT(reboot, Reboot System);
  42. #endif /* RT_USING_FINSH */
  43. /* SysTick configuration */
  44. void rt_hw_systick_init(void)
  45. {
  46. #ifdef SOC_SERIES_R9A07G0
  47. SysTimerInterrupt();
  48. #else
  49. SysTick_Config(SystemCoreClock / RT_TICK_PER_SECOND);
  50. NVIC_SetPriority(SysTick_IRQn, 0xFF);
  51. #endif
  52. }
  53. /**
  54. * This is the timer interrupt service routine.
  55. *
  56. */
  57. void SysTick_Handler(void)
  58. {
  59. /* enter interrupt */
  60. rt_interrupt_enter();
  61. #ifdef SOC_SERIES_R9A07G0
  62. __set_CNTP_CVAL(__get_CNTP_CVAL() + rtt_timer_delay);
  63. #endif
  64. rt_tick_increase();
  65. /* leave interrupt */
  66. rt_interrupt_leave();
  67. }
  68. /**
  69. * @brief This function is executed in case of error occurrence.
  70. * @param None
  71. * @retval None
  72. */
  73. void _Error_Handler(char *s, int num)
  74. {
  75. /* USER CODE BEGIN Error_Handler */
  76. /* User can add his own implementation to report the HAL error return state */
  77. while (1)
  78. {
  79. }
  80. /* USER CODE END Error_Handler */
  81. }
  82. /**
  83. * This function will delay for some us.
  84. *
  85. * @param us the delay time of us
  86. */
  87. void rt_hw_us_delay(rt_uint32_t us)
  88. {
  89. #ifdef ARCH_ARM_CORTEX_M
  90. rt_uint32_t ticks;
  91. rt_uint32_t told, tnow, tcnt = 0;
  92. rt_uint32_t reload = SysTick->LOAD;
  93. ticks = us * reload / (1000000 / RT_TICK_PER_SECOND);
  94. told = SysTick->VAL;
  95. while (1)
  96. {
  97. tnow = SysTick->VAL;
  98. if (tnow != told)
  99. {
  100. if (tnow < told)
  101. {
  102. tcnt += told - tnow;
  103. }
  104. else
  105. {
  106. tcnt += reload - tnow + told;
  107. }
  108. told = tnow;
  109. if (tcnt >= ticks)
  110. {
  111. break;
  112. }
  113. }
  114. }
  115. #endif
  116. }
  117. #ifdef SOC_SERIES_R9A07G0
  118. static void SysTimerInterrupt(void)
  119. {
  120. uint64_t tempCNTPCT = __get_CNTPCT();
  121. /* Wait for counter supply */
  122. while (__get_CNTPCT() == tempCNTPCT)
  123. {
  124. R_BSP_SoftwareDelay(1, BSP_DELAY_UNITS_MICROSECONDS);
  125. }
  126. /* generic timer initialize */
  127. /* set interrupt handler */
  128. g_sgi_ppi_vector_table[(int32_t) BSP_VECTOR_NUM_OFFSET +
  129. NonSecurePhysicalTimerInt] = SysTick_Handler;
  130. rtt_timer_delay = R_GSC->CNTFID0 / RT_TICK_PER_SECOND;
  131. /* set timer expiration from current counter value */
  132. __set_CNTP_CVAL(__get_CNTPCT() + rtt_timer_delay);
  133. /* configure CNTP_CTL to enable timer interrupts */
  134. __set_CNTP_CTL(1);
  135. R_BSP_IrqCfgEnable(NonSecurePhysicalTimerInt, (int32_t) BSP_VECTOR_NUM_OFFSET +
  136. NonSecurePhysicalTimerInt, RT_NULL);
  137. }
  138. #endif
  139. /**
  140. * This function will initial board.
  141. */
  142. rt_weak void rt_hw_board_init()
  143. {
  144. #ifdef SOC_SERIES_R9A07G0
  145. /* initialize hardware interrupt */
  146. rt_uint32_t redis_gic_base = platform_get_gic_dist_base();
  147. rt_int32_t cpu_id = rt_hw_cpu_id();
  148. arm_gic_redist_address_set(0, redis_gic_base, cpu_id);
  149. rt_hw_interrupt_init();
  150. #endif
  151. rt_hw_systick_init();
  152. /* Heap initialization */
  153. #if defined(RT_USING_HEAP)
  154. rt_system_heap_init((void *)HEAP_BEGIN, (void *)HEAP_END);
  155. #endif
  156. /* Pin driver initialization is open by default */
  157. #ifdef RT_USING_PIN
  158. rt_hw_pin_init();
  159. #endif
  160. /* USART driver initialization is open by default */
  161. #ifdef RT_USING_SERIAL
  162. rt_hw_usart_init();
  163. #endif
  164. /* Set the shell console output device */
  165. #if defined(RT_USING_CONSOLE) && defined(RT_USING_DEVICE)
  166. rt_console_set_device(RT_CONSOLE_DEVICE_NAME);
  167. #endif
  168. #if defined(RT_USING_CONSOLE) && defined(RT_USING_NANO)
  169. extern void rt_hw_console_init(void);
  170. rt_hw_console_init();
  171. #endif
  172. /* Board underlying hardware initialization */
  173. #ifdef RT_USING_COMPONENTS_INIT
  174. rt_components_board_init();
  175. #endif
  176. }
  177. FSP_CPP_HEADER
  178. #ifdef SOC_SERIES_R9A07G0
  179. void R_BSP_WarmStart(bsp_warm_start_event_t event) BSP_PLACE_IN_SECTION(".warm_start");
  180. #else
  181. void R_BSP_WarmStart(bsp_warm_start_event_t event);
  182. #endif
  183. FSP_CPP_FOOTER
  184. /*******************************************************************************************************************//**
  185. * This function is called at various points during the startup process. This implementation uses the event that is
  186. * called right before main() to set up the pins.
  187. *
  188. * @param[in] event Where at in the start up process the code is currently at
  189. **********************************************************************************************************************/
  190. void R_BSP_WarmStart (bsp_warm_start_event_t event)
  191. {
  192. if (BSP_WARM_START_RESET == event)
  193. {
  194. #if BSP_FEATURE_FLASH_LP_VERSION != 0
  195. /* Enable reading from data flash. */
  196. R_FACI_LP->DFLCTL = 1U;
  197. /* Would normally have to wait tDSTOP(6us) for data flash recovery. Placing the enable here, before clock and
  198. * C runtime initialization, should negate the need for a delay since the initialization will typically take more than 6us. */
  199. #endif
  200. }
  201. if (BSP_WARM_START_POST_C == event)
  202. {
  203. /* C runtime environment and system clocks are setup. */
  204. /* Configure pins. */
  205. R_IOPORT_Open(&g_ioport_ctrl, g_ioport.p_cfg);
  206. }
  207. }
  208. #if BSP_TZ_SECURE_BUILD
  209. BSP_CMSE_NONSECURE_ENTRY void template_nonsecure_callable ();
  210. /* Trustzone Secure Projects require at least one nonsecure callable function in order to build (Remove this if it is not required to build). */
  211. BSP_CMSE_NONSECURE_ENTRY void template_nonsecure_callable ()
  212. {
  213. }
  214. #endif