drv_common.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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. #include <drv_usart.h>
  21. #endif
  22. #endif
  23. #ifdef RT_USING_FINSH
  24. #include <finsh.h>
  25. static void reboot(uint8_t argc, char **argv)
  26. {
  27. NVIC_SystemReset();
  28. }
  29. MSH_CMD_EXPORT(reboot, Reboot System);
  30. #endif /* RT_USING_FINSH */
  31. /* SysTick configuration */
  32. void rt_hw_systick_init(void)
  33. {
  34. SysTick_Config(SystemCoreClock / RT_TICK_PER_SECOND);
  35. NVIC_SetPriority(SysTick_IRQn, 0xFF);
  36. }
  37. /**
  38. * This is the timer interrupt service routine.
  39. *
  40. */
  41. void SysTick_Handler(void)
  42. {
  43. /* enter interrupt */
  44. rt_interrupt_enter();
  45. rt_tick_increase();
  46. /* leave interrupt */
  47. rt_interrupt_leave();
  48. }
  49. /**
  50. * @brief This function is executed in case of error occurrence.
  51. * @param None
  52. * @retval None
  53. */
  54. void _Error_Handler(char *s, int num)
  55. {
  56. /* USER CODE BEGIN Error_Handler */
  57. /* User can add his own implementation to report the HAL error return state */
  58. while (1)
  59. {
  60. }
  61. /* USER CODE END Error_Handler */
  62. }
  63. /**
  64. * This function will delay for some us.
  65. *
  66. * @param us the delay time of us
  67. */
  68. void rt_hw_us_delay(rt_uint32_t us)
  69. {
  70. rt_uint32_t ticks;
  71. rt_uint32_t told, tnow, tcnt = 0;
  72. rt_uint32_t reload = SysTick->LOAD;
  73. ticks = us * reload / (1000000 / RT_TICK_PER_SECOND);
  74. told = SysTick->VAL;
  75. while (1)
  76. {
  77. tnow = SysTick->VAL;
  78. if (tnow != told)
  79. {
  80. if (tnow < told)
  81. {
  82. tcnt += told - tnow;
  83. }
  84. else
  85. {
  86. tcnt += reload - tnow + told;
  87. }
  88. told = tnow;
  89. if (tcnt >= ticks)
  90. {
  91. break;
  92. }
  93. }
  94. }
  95. }
  96. /**
  97. * This function will initial STM32 board.
  98. */
  99. RT_WEAK void rt_hw_board_init()
  100. {
  101. rt_hw_systick_init();
  102. /* Heap initialization */
  103. #if defined(RT_USING_HEAP)
  104. rt_system_heap_init((void *)HEAP_BEGIN, (void *)HEAP_END);
  105. #endif
  106. /* Pin driver initialization is open by default */
  107. #ifdef RT_USING_PIN
  108. rt_hw_pin_init();
  109. #endif
  110. /* USART driver initialization is open by default */
  111. #ifdef RT_USING_SERIAL
  112. rt_hw_usart_init();
  113. #endif
  114. /* Set the shell console output device */
  115. #if defined(RT_USING_CONSOLE) && defined(RT_USING_DEVICE)
  116. rt_console_set_device(RT_CONSOLE_DEVICE_NAME);
  117. #endif
  118. /* Board underlying hardware initialization */
  119. #ifdef RT_USING_COMPONENTS_INIT
  120. rt_components_board_init();
  121. #endif
  122. }
  123. FSP_CPP_HEADER
  124. void R_BSP_WarmStart(bsp_warm_start_event_t event);
  125. FSP_CPP_FOOTER
  126. /*******************************************************************************************************************//**
  127. * This function is called at various points during the startup process. This implementation uses the event that is
  128. * called right before main() to set up the pins.
  129. *
  130. * @param[in] event Where at in the start up process the code is currently at
  131. **********************************************************************************************************************/
  132. void R_BSP_WarmStart (bsp_warm_start_event_t event)
  133. {
  134. if (BSP_WARM_START_RESET == event)
  135. {
  136. #if BSP_FEATURE_FLASH_LP_VERSION != 0
  137. /* Enable reading from data flash. */
  138. R_FACI_LP->DFLCTL = 1U;
  139. /* Would normally have to wait tDSTOP(6us) for data flash recovery. Placing the enable here, before clock and
  140. * C runtime initialization, should negate the need for a delay since the initialization will typically take more than 6us. */
  141. #endif
  142. }
  143. if (BSP_WARM_START_POST_C == event)
  144. {
  145. /* C runtime environment and system clocks are setup. */
  146. /* Configure pins. */
  147. R_IOPORT_Open(&g_ioport_ctrl, g_ioport.p_cfg);
  148. }
  149. }
  150. #if BSP_TZ_SECURE_BUILD
  151. BSP_CMSE_NONSECURE_ENTRY void template_nonsecure_callable ();
  152. /* Trustzone Secure Projects require at least one nonsecure callable function in order to build (Remove this if it is not required to build). */
  153. BSP_CMSE_NONSECURE_ENTRY void template_nonsecure_callable ()
  154. {
  155. }
  156. #endif