drv_common.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. /*
  2. * Copyright (c) 2020-2022, CQ 100ask Development Team
  3. *
  4. * Change Logs:
  5. * Date Author Notes
  6. * 2022-05-29 Alen first version
  7. */
  8. #include "drv_common.h"
  9. #define DBG_TAG "drv_common"
  10. #define DBG_LVL DBG_INFO
  11. #include <rtdbg.h>
  12. #ifdef RT_USING_FINSH
  13. #include <finsh.h>
  14. static void reboot(uint8_t argc, char **argv)
  15. {
  16. rt_hw_cpu_reset();
  17. }
  18. MSH_CMD_EXPORT(reboot, Reboot System);
  19. #endif /* RT_USING_FINSH */
  20. volatile uint32_t uwTick;
  21. static uint32_t _systick_ms = 1;
  22. void HAL_IncTick(void);
  23. /* SysTick configuration */
  24. void rt_hw_systick_init(void)
  25. {
  26. uint32_t prioritygroup = 0x00U;
  27. uint32_t SystemCoreClock = HAL_GetSysClockFreq();
  28. /* Configure the SysTick to have interrupt in 1ms time basis*/
  29. if(SysTick_Config(SystemCoreClock/1000) > 0)
  30. {
  31. return;
  32. }
  33. /* Configure the SysTick IRQ priority */
  34. prioritygroup = NVIC_GetPriorityGrouping();
  35. NVIC_SetPriority(SysTick_IRQn, NVIC_EncodePriority(prioritygroup, 15, 0));
  36. _systick_ms = 1000u / RT_TICK_PER_SECOND;
  37. if(_systick_ms == 0)
  38. _systick_ms = 1;
  39. }
  40. /**
  41. * This is the timer interrupt service routine.
  42. *
  43. */
  44. void SysTick_Handler(void)
  45. {
  46. /* enter interrupt */
  47. rt_interrupt_enter();
  48. if(SysTick->CTRL & SysTick_CTRL_COUNTFLAG_Msk)
  49. HAL_IncTick();
  50. rt_tick_increase();
  51. /* leave interrupt */
  52. rt_interrupt_leave();
  53. }
  54. void HAL_IncTick(void)
  55. {
  56. uwTick += _systick_ms;
  57. }
  58. /**
  59. * @brief This function is executed in case of error occurrence.
  60. * @param None
  61. * @retval None
  62. */
  63. void _Error_Handler(char *s, int num)
  64. {
  65. /* USER CODE BEGIN Error_Handler */
  66. /* User can add his own implementation to report the HAL error return state */
  67. LOG_E("Error_Handler at file:%s num:%d", s, num);
  68. while (1)
  69. {
  70. }
  71. /* USER CODE END Error_Handler */
  72. }
  73. /**
  74. * This function will delay for some us.
  75. *
  76. * @param us the delay time of us
  77. */
  78. void rt_hw_us_delay(rt_uint32_t us)
  79. {
  80. rt_uint32_t ticks;
  81. rt_uint32_t told, tnow, tcnt = 0;
  82. rt_uint32_t reload = SysTick->LOAD;
  83. ticks = us * reload / (1000000 / RT_TICK_PER_SECOND);
  84. told = SysTick->VAL;
  85. while (1)
  86. {
  87. tnow = SysTick->VAL;
  88. if (tnow != told)
  89. {
  90. if (tnow < told)
  91. {
  92. tcnt += told - tnow;
  93. }
  94. else
  95. {
  96. tcnt += reload - tnow + told;
  97. }
  98. told = tnow;
  99. if (tcnt >= ticks)
  100. {
  101. break;
  102. }
  103. }
  104. }
  105. }
  106. /**
  107. * This function will initial STM32 board.
  108. */
  109. rt_weak void rt_hw_board_init()
  110. {
  111. #ifdef BSP_SCB_ENABLE_I_CACHE
  112. /* Enable I-Cache---------------------------------------------------------*/
  113. SCB_EnableICache();
  114. #endif
  115. #ifdef BSP_SCB_ENABLE_D_CACHE
  116. /* Enable D-Cache---------------------------------------------------------*/
  117. SCB_EnableDCache();
  118. #endif
  119. /* System clock initialization */
  120. SystemClock_Config();
  121. rt_hw_systick_init();
  122. /* Heap initialization */
  123. #if defined(RT_USING_HEAP)
  124. rt_system_heap_init((void *)HEAP_BEGIN, (void *)HEAP_END);
  125. #endif
  126. /* Pin driver initialization is open by default */
  127. #ifdef RT_USING_PIN
  128. rt_hw_pin_init();
  129. #endif
  130. /* USART driver initialization is open by default */
  131. #ifdef RT_USING_SERIAL
  132. rt_hw_uart_init();
  133. #endif
  134. /* Set the shell console output device */
  135. #if defined(RT_USING_CONSOLE) && defined(RT_USING_DEVICE)
  136. rt_console_set_device(RT_CONSOLE_DEVICE_NAME);
  137. #endif
  138. /* Board underlying hardware initialization */
  139. #ifdef RT_USING_COMPONENTS_INIT
  140. rt_components_board_init();
  141. #endif
  142. }