drv_common.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. /**************************************************************************//**
  2. *
  3. * @copyright (C) 2019 Nuvoton Technology Corp. All rights reserved.
  4. *
  5. * SPDX-License-Identifier: Apache-2.0
  6. *
  7. * Change Logs:
  8. * Date Author Notes
  9. * 2020-1-16 Wayne First version
  10. *
  11. ******************************************************************************/
  12. #include <rtconfig.h>
  13. #include <rtthread.h>
  14. #include "NuMicro.h"
  15. #include "drv_uart.h"
  16. #include "board.h"
  17. #include "nutool_pincfg.h"
  18. #include "nutool_modclkcfg.h"
  19. /**
  20. * This function will initial.
  21. */
  22. rt_weak void rt_hw_board_init(void)
  23. {
  24. /* Init System/modules clock */
  25. nutool_modclkcfg_init();
  26. /* Unlock protected registers */
  27. SYS_UnlockReg();
  28. /* Init all pin function setting */
  29. nutool_pincfg_init();
  30. /* Configure SysTick */
  31. SysTick_Config(SystemCoreClock / RT_TICK_PER_SECOND);
  32. /* Update System Core Clock */
  33. /* User can use SystemCoreClockUpdate() to calculate SystemCoreClock. */
  34. SystemCoreClockUpdate();
  35. #if defined(BSP_USING_EADC)
  36. /* Vref connect to internal */
  37. SYS->VREFCTL = (SYS->VREFCTL & ~SYS_VREFCTL_VREFCTL_Msk) | SYS_VREFCTL_VREF_3_0V;
  38. #endif
  39. /* Lock protected registers */
  40. SYS_LockReg();
  41. #ifdef RT_USING_HEAP
  42. rt_system_heap_init(HEAP_BEGIN, HEAP_END);
  43. #endif /* RT_USING_HEAP */
  44. #if defined(BSP_USING_UART)
  45. rt_hw_uart_init();
  46. #endif
  47. #if defined(RT_USING_CONSOLE) && defined(RT_USING_DEVICE)
  48. rt_console_set_device(RT_CONSOLE_DEVICE_NAME);
  49. #endif
  50. NVIC_SetPriorityGrouping(7);
  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. int reboot(int argc, char **argv)
  106. {
  107. rt_hw_cpu_reset();
  108. return 0;
  109. }
  110. MSH_CMD_EXPORT(reboot, Reboot System);