drv_common.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. /*
  2. * Copyright (c) 2006-2023, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2020-08-20 Abbcc first version
  9. * 2022-03-04 stevetong459 FINSH_FUNCTION_EXPORT_ALIAS change to MSH_CMD_EXPORT for reboot function.
  10. * 2023-03-08 luobeihai modified hw_board_init function
  11. */
  12. #include "drv_common.h"
  13. #include "board.h"
  14. #ifdef RT_USING_FINSH
  15. #include <finsh.h>
  16. static void reboot(uint8_t argc, char **argv)
  17. {
  18. rt_hw_cpu_reset();
  19. }
  20. MSH_CMD_EXPORT(reboot, Reboot System);
  21. #endif /* RT_USING_FINSH */
  22. /* SysTick configuration */
  23. void rt_hw_systick_init(void)
  24. {
  25. SysTick_Config(RCM_ReadHCLKFreq() / RT_TICK_PER_SECOND);
  26. /* AHB clock selected as SysTick clock source. */
  27. SysTick->CTRL |= 0x00000004U;
  28. NVIC_SetPriority(SysTick_IRQn, 0xFF);
  29. }
  30. /**
  31. * This is the timer interrupt service routine.
  32. *
  33. */
  34. void SysTick_Handler(void)
  35. {
  36. /* enter interrupt */
  37. rt_interrupt_enter();
  38. rt_tick_increase();
  39. /* leave interrupt */
  40. rt_interrupt_leave();
  41. }
  42. /**
  43. * This function will delay for some us.
  44. *
  45. * @param us the delay time of us
  46. */
  47. void rt_hw_us_delay(rt_uint32_t us)
  48. {
  49. rt_uint32_t ticks;
  50. rt_uint32_t told, tnow, tcnt = 0;
  51. rt_uint32_t reload = SysTick->LOAD;
  52. ticks = us * reload / (1000000 / RT_TICK_PER_SECOND);
  53. told = SysTick->VAL;
  54. while (1)
  55. {
  56. tnow = SysTick->VAL;
  57. if (tnow != told)
  58. {
  59. if (tnow < told)
  60. {
  61. tcnt += told - tnow;
  62. }
  63. else
  64. {
  65. tcnt += reload - tnow + told;
  66. }
  67. told = tnow;
  68. if (tcnt >= ticks)
  69. {
  70. break;
  71. }
  72. }
  73. }
  74. }
  75. /**
  76. * This function will initial APM32 board.
  77. */
  78. void hw_board_init(char *clock_src, int32_t clock_src_freq, int32_t clock_target_freq)
  79. {
  80. extern void clk_init(char *clk_source, int source_freq, int target_freq);
  81. /* Systick initialization */
  82. rt_hw_systick_init();
  83. /* Pin driver initialization is open by default */
  84. #ifdef RT_USING_PIN
  85. extern int rt_hw_pin_init(void);
  86. rt_hw_pin_init();
  87. #endif
  88. /* USART driver initialization is open by default */
  89. #ifdef RT_USING_SERIAL
  90. extern int rt_hw_usart_init(void);
  91. rt_hw_usart_init();
  92. #endif
  93. }