board.c 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /*
  2. * Copyright (C) 2021, Huada Semiconductor Co., Ltd.
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2021-08-19 pjq first version
  9. */
  10. #include <rthw.h>
  11. #include <rtthread.h>
  12. #include "board.h"
  13. /**
  14. * @addtogroup HC32
  15. */
  16. /*@{*/
  17. /**
  18. * @brief BSP clock initialize.
  19. * Set board system clock 24Mhz
  20. * @param None
  21. * @retval None
  22. */
  23. void rt_hw_board_clock_init(void)
  24. {
  25. Sysctrl_SetRCHTrim(SysctrlRchFreq24MHz);
  26. Sysctrl_ClkSourceEnable(SysctrlClkRCH, TRUE);
  27. }
  28. /*******************************************************************************
  29. * Function Name : SysTick_Configuration
  30. * Description : Configures the SysTick for OS tick.
  31. * Input : None
  32. * Output : None
  33. * Return : None
  34. *******************************************************************************/
  35. void SysTick_Configuration(void)
  36. {
  37. SystemCoreClockUpdate();
  38. SysTick_Config(SystemCoreClock/RT_TICK_PER_SECOND);
  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. rt_tick_increase();
  49. /* leave interrupt */
  50. rt_interrupt_leave();
  51. }
  52. /**
  53. * This function will initialize HC32 board.
  54. */
  55. void rt_hw_board_init()
  56. {
  57. /* Configure the System clock */
  58. rt_hw_board_clock_init();
  59. /* Configure the SysTick */
  60. SysTick_Configuration();
  61. #ifdef RT_USING_HEAP
  62. rt_system_heap_init((void *)HEAP_BEGIN, (void *)HEAP_END);
  63. #endif
  64. #ifdef RT_USING_COMPONENTS_INIT
  65. rt_components_board_init();
  66. #endif
  67. #if defined(RT_USING_CONSOLE) && defined(RT_USING_DEVICE)
  68. rt_console_set_device(RT_CONSOLE_DEVICE_NAME);
  69. #endif
  70. }
  71. void rt_hw_us_delay(rt_uint32_t us)
  72. {
  73. uint32_t start, now, delta, reload, us_tick;
  74. start = SysTick->VAL;
  75. reload = SysTick->LOAD;
  76. us_tick = SystemCoreClock / 1000000UL;
  77. do{
  78. now = SysTick->VAL;
  79. delta = start > now ? start - now : reload + start - now;
  80. }
  81. while(delta < us_tick * us);
  82. }
  83. /*@}*/