drv_common.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /*
  2. * Copyright (C) 2022-2024, Xiaohua Semiconductor Co., Ltd.
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2023-12-16 CDT first version
  9. */
  10. #include "board.h"
  11. #define DBG_TAG "drv_common"
  12. #define DBG_LVL DBG_INFO
  13. #include <rtdbg.h>
  14. #ifdef RT_USING_PIN
  15. #include <drv_gpio.h>
  16. #endif
  17. #ifdef RT_USING_SERIAL
  18. #ifdef RT_USING_SERIAL_V2
  19. #include <drv_usart_v2.h>
  20. #else
  21. #include <drv_usart.h>
  22. #endif /* RT_USING_SERIAL */
  23. #endif /* RT_USING_SERIAL_V2 */
  24. #ifdef RT_USING_FINSH
  25. #include <finsh.h>
  26. static void reboot(uint8_t argc, char **argv)
  27. {
  28. rt_hw_cpu_reset();
  29. }
  30. MSH_CMD_EXPORT(reboot, Reboot System);
  31. #endif /* RT_USING_FINSH */
  32. /**
  33. * This function is executed in case of error occurrence.
  34. */
  35. void _Error_Handler(char *s, int num)
  36. {
  37. LOG_E("Error_Handler at file:%s num:%d", s, num);
  38. while (1)
  39. {
  40. }
  41. }
  42. /**
  43. * This is the timer interrupt service routine.
  44. */
  45. void SysTick_Handler(void)
  46. {
  47. /* enter interrupt */
  48. rt_interrupt_enter();
  49. rt_tick_increase();
  50. /* leave interrupt */
  51. rt_interrupt_leave();
  52. }
  53. /**
  54. * Configures the SysTick for OS tick.
  55. */
  56. void SysTick_Configuration(void)
  57. {
  58. rt_uint32_t cnts;
  59. stc_clock_freq_t stcClkFreq;
  60. CLK_GetClockFreq(&stcClkFreq);
  61. cnts = (rt_uint32_t)stcClkFreq.u32HclkFreq / RT_TICK_PER_SECOND;
  62. SysTick_Config(cnts);
  63. }
  64. /**
  65. * This function will initial HC32 board.
  66. */
  67. void rt_hw_board_init()
  68. {
  69. PeripheralRegister_Unlock();
  70. SystemBase_Config();
  71. SystemClock_Config();
  72. PeripheralClock_Config();
  73. /* Configure the SysTick */
  74. SysTick_Configuration();
  75. /* Heap initialization */
  76. #if defined(RT_USING_HEAP)
  77. rt_system_heap_init((void *)HEAP_BEGIN, (void *)HEAP_END);
  78. #endif
  79. #ifdef RT_USING_PIN
  80. rt_hw_pin_init();
  81. #endif
  82. #ifdef RT_USING_SERIAL
  83. rt_hw_usart_init();
  84. #endif
  85. #if defined(RT_USING_CONSOLE) && defined(RT_USING_DEVICE)
  86. rt_console_set_device(RT_CONSOLE_DEVICE_NAME);
  87. #endif
  88. /* Board underlying hardware initialization */
  89. #ifdef RT_USING_COMPONENTS_INIT
  90. rt_components_board_init();
  91. #endif
  92. }
  93. void rt_hw_us_delay(rt_uint32_t us)
  94. {
  95. uint32_t start, now, delta, reload, us_tick;
  96. start = SysTick->VAL;
  97. reload = SysTick->LOAD;
  98. us_tick = SystemCoreClock / 1000000UL;
  99. do
  100. {
  101. now = SysTick->VAL;
  102. delta = start > now ? start - now : reload + start - now;
  103. }
  104. while (delta < us_tick * us);
  105. }