drv_common.c 6.0 KB

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