drv_common.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /**************************************************************************//**
  2. *
  3. * @copyright (C) 2020 Nuvoton Technology Corp. All rights reserved.
  4. *
  5. * SPDX-License-Identifier: Apache-2.0
  6. *
  7. * Change Logs:
  8. * Date Author Notes
  9. * 2020-9-1 Philo First version
  10. *
  11. ******************************************************************************/
  12. #include <rtconfig.h>
  13. #include <rtthread.h>
  14. #include "NuMicro.h"
  15. #include <nu_bitutil.h>
  16. #include "drv_uart.h"
  17. #include "board.h"
  18. #include "nutool_pincfg.h"
  19. #include "nutool_modclkcfg.h"
  20. /**
  21. * This function will initial M032 board.
  22. */
  23. rt_weak void rt_hw_board_init(void)
  24. {
  25. /* Init System/modules clock */
  26. nutool_modclkcfg_init();
  27. /* Unlock protected registers */
  28. SYS_UnlockReg();
  29. /* Init all pin function setting */
  30. nutool_pincfg_init();
  31. /* Configure SysTick */
  32. SysTick_Config(SystemCoreClock / RT_TICK_PER_SECOND);
  33. /* Update System Core Clock */
  34. /* User can use SystemCoreClockUpdate() to calculate SystemCoreClock. */
  35. SystemCoreClockUpdate();
  36. #if defined(BSP_USING_EADC)
  37. /* Vref connect to internal */
  38. SYS->VREFCTL = (SYS->VREFCTL & ~SYS_VREFCTL_VREFCTL_Msk) | SYS_VREFCTL_VREF_3_0V;
  39. #endif
  40. /* Lock protected registers */
  41. SYS_LockReg();
  42. #ifdef RT_USING_HEAP
  43. rt_system_heap_init(HEAP_BEGIN, HEAP_END);
  44. #endif /* RT_USING_HEAP */
  45. #if defined(BSP_USING_UART)
  46. rt_hw_uart_init();
  47. #endif
  48. #if defined(RT_USING_CONSOLE) && defined(RT_USING_DEVICE)
  49. rt_console_set_device(RT_CONSOLE_DEVICE_NAME);
  50. #endif
  51. #ifdef RT_USING_COMPONENTS_INIT
  52. rt_components_board_init();
  53. #endif
  54. }
  55. /**
  56. * The time delay function.
  57. *
  58. * @param microseconds.
  59. */
  60. void rt_hw_us_delay(rt_uint32_t us)
  61. {
  62. rt_uint32_t ticks;
  63. rt_uint32_t told, tnow, tcnt = 0;
  64. rt_uint32_t reload = SysTick->LOAD;
  65. ticks = us * reload / (1000000 / RT_TICK_PER_SECOND);
  66. told = SysTick->VAL;
  67. while (1)
  68. {
  69. tnow = SysTick->VAL;
  70. if (tnow != told)
  71. {
  72. if (tnow < told)
  73. {
  74. tcnt += told - tnow;
  75. }
  76. else
  77. {
  78. tcnt += reload - tnow + told;
  79. }
  80. told = tnow;
  81. if (tcnt >= ticks)
  82. {
  83. break;
  84. }
  85. }
  86. }
  87. }
  88. /**
  89. * This is the timer interrupt service routine.
  90. *
  91. */
  92. void SysTick_Handler(void)
  93. {
  94. /* enter interrupt */
  95. rt_interrupt_enter();
  96. rt_tick_increase();
  97. /* leave interrupt */
  98. rt_interrupt_leave();
  99. }
  100. void rt_hw_cpu_reset(void)
  101. {
  102. SYS_UnlockReg();
  103. SYS->IPRST0 |= SYS_IPRST0_CHIPRST_Msk;
  104. }
  105. #ifdef RT_USING_CPU_FFS
  106. int __rt_ffs(int value)
  107. {
  108. if (!value) return 0;
  109. return __CLZ(__RBIT(value)) + 1;
  110. }
  111. #endif
  112. #ifdef RT_USING_FINSH
  113. #include <finsh.h>
  114. static void reboot(uint8_t argc, char **argv)
  115. {
  116. rt_hw_cpu_reset();
  117. }
  118. MSH_CMD_EXPORT(reboot, Reboot System);
  119. #endif /* RT_USING_FINSH */