drv_common.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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. * 2021-08-10 charlown first version
  9. */
  10. #include <rtthread.h>
  11. #include <rtdevice.h>
  12. #include <rthw.h>
  13. #include "board.h"
  14. #include "drv_common.h"
  15. #ifdef RT_USING_PIN
  16. #include "drv_gpio.h"
  17. #endif
  18. #ifdef RT_USING_SERIAL
  19. #include "drv_uart.h"
  20. #endif
  21. #ifdef RT_USING_FINSH
  22. #include <finsh.h>
  23. static void reboot(uint8_t argc, char **argv)
  24. {
  25. rt_hw_cpu_reset();
  26. }
  27. MSH_CMD_EXPORT(reboot, Reboot System)
  28. #endif /* RT_USING_FINSH */
  29. void rt_hw_us_delay(rt_uint32_t us)
  30. {
  31. rt_uint32_t start, now, delta, reload, us_tick;
  32. start = SysTick->VAL;
  33. reload = SysTick->LOAD;
  34. us_tick = SystemCoreClock / 1000000UL;
  35. do
  36. {
  37. now = SysTick->VAL;
  38. delta = start > now ? start - now : reload + start - now;
  39. } while (delta < us_tick * us);
  40. }
  41. void SystemClock_Config(void)
  42. {
  43. SysTick_Config(ch32_get_sysclock_frequency() / RT_TICK_PER_SECOND);
  44. NVIC_SetPriority(SysTick_IRQn, 0);
  45. }
  46. /**
  47. * This is the timer interrupt service routine.
  48. *
  49. */
  50. void SysTick_Handler(void)
  51. {
  52. /* enter interrupt */
  53. rt_interrupt_enter();
  54. rt_tick_increase();
  55. /* leave interrupt */
  56. rt_interrupt_leave();
  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. while (1)
  68. {
  69. }
  70. /* USER CODE END Error_Handler */
  71. }
  72. void rt_hw_board_init()
  73. {
  74. /* System clock initialization */
  75. SystemClock_Config();
  76. /* Heap initialization */
  77. #if defined(RT_USING_HEAP)
  78. rt_system_heap_init((void *)HEAP_BEGIN, (void *)HEAP_END);
  79. #endif
  80. /* Pin driver initialization is open by default */
  81. #ifdef RT_USING_PIN
  82. rt_hw_pin_init();
  83. #endif
  84. /* USART driver initialization is open by default */
  85. #ifdef RT_USING_SERIAL
  86. rt_hw_uart_init();
  87. #endif
  88. /* Set the shell console output device */
  89. #if defined(RT_USING_CONSOLE) && defined(RT_USING_DEVICE)
  90. rt_console_set_device(RT_CONSOLE_DEVICE_NAME);
  91. #endif
  92. /* Board underlying hardware initialization */
  93. #ifdef RT_USING_COMPONENTS_INIT
  94. rt_components_board_init();
  95. #endif
  96. }