drv_common.c 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. /*
  2. * Copyright (c) 2006-2022, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2022-02-22 airm2m first version
  9. */
  10. #include "drv_common.h"
  11. #include "board.h"
  12. #ifdef RT_USING_SERIAL
  13. #ifdef RT_USING_SERIAL_V2
  14. #include "drv_usart_v2.h"
  15. #else
  16. #include "drv_usart.h"
  17. #endif /* RT_USING_SERIAL */
  18. #endif /* RT_USING_SERIAL_V2 */
  19. #define DBG_TAG "drv_common"
  20. #define DBG_LVL DBG_INFO
  21. #include <rtdbg.h>
  22. #ifdef RT_USING_FINSH
  23. #include <finsh.h>
  24. static void reboot(uint8_t argc, char **argv)
  25. {
  26. rt_hw_cpu_reset();
  27. }
  28. MSH_CMD_EXPORT(reboot, Reboot System);
  29. #endif /* RT_USING_FINSH */
  30. /* SysTick configuration */
  31. void rt_hw_systick_init(void)
  32. {
  33. SysTick_Config(SystemCoreClock / RT_TICK_PER_SECOND);
  34. NVIC_SetPriority(SysTick_IRQn, 0xFF);
  35. }
  36. /**
  37. * This is the timer interrupt service routine.
  38. *
  39. */
  40. void SysTick_Handler(void)
  41. {
  42. /* enter interrupt */
  43. rt_interrupt_enter();
  44. rt_tick_increase();
  45. /* leave interrupt */
  46. rt_interrupt_leave();
  47. }
  48. /**
  49. * @brief This function is executed in case of error occurrence.
  50. * @param None
  51. * @retval None
  52. */
  53. void _Error_Handler(char *s, int num)
  54. {
  55. /* USER CODE BEGIN Error_Handler */
  56. /* User can add his own implementation to report the HAL error return state */
  57. LOG_E("Error_Handler at file:%s num:%d", s, num);
  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. }