board.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /*
  2. * File : board.c
  3. * This file is part of RT-Thread RTOS
  4. * COPYRIGHT (C) 2014 RT-Thread Develop Team
  5. *
  6. * The license and distribution terms for this file may be
  7. * found in the file LICENSE in this distribution or at
  8. * http://www.rt-thread.org/license/LICENSE
  9. *
  10. * Change Logs:
  11. * Date Author Notes
  12. * 2016-12-05 Pluto first implementation
  13. */
  14. #include <rthw.h>
  15. #include <rtthread.h>
  16. #include "board.h"
  17. #include "usart.h"
  18. /**
  19. * @addtogroup NUVOTON_M451
  20. */
  21. /*@{*/
  22. /*******************************************************************************
  23. * Function Name : NVIC_Configuration
  24. * Description : Configures Vector Table base location.
  25. * Input : None
  26. * Output : None
  27. * Return : None
  28. *******************************************************************************/
  29. void NVIC_Configuration(void)
  30. {
  31. // NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
  32. }
  33. /**
  34. * @brief Inserts a delay time.
  35. * @param nCount: specifies the delay time length.
  36. * @retval None
  37. */
  38. static void delay(__IO uint32_t nCount)
  39. {
  40. /* Decrement nCount value */
  41. while (nCount != 0)
  42. {
  43. nCount--;
  44. }
  45. }
  46. static void rt_hw_system_init(void)
  47. {
  48. /*---------------------------------------------------------------------------------------------------------*/
  49. /* Init System Clock */
  50. /*---------------------------------------------------------------------------------------------------------*/
  51. /* Unlock protected registers */
  52. SYS_UnlockReg();
  53. /* Enable HIRC clock (Internal RC 22.1184MHz) */
  54. CLK_EnableXtalRC(CLK_PWRCTL_HIRCEN_Msk);
  55. /* Wait for HIRC clock ready */
  56. CLK_WaitClockReady(CLK_STATUS_HIRCSTB_Msk);
  57. /* Select HCLK clock source as HIRC and and HCLK clock divider as 1 */
  58. CLK_SetHCLK(CLK_CLKSEL0_HCLKSEL_HIRC, CLK_CLKDIV0_HCLK(1));
  59. /* Enable HXT clock (external XTAL 12MHz) */
  60. CLK_EnableXtalRC(CLK_PWRCTL_HXTEN_Msk);
  61. /* Wait for HXT clock ready */
  62. CLK_WaitClockReady(CLK_STATUS_HXTSTB_Msk);
  63. /* Set core clock as PLL_CLOCK from PLL */
  64. CLK_SetCoreClock(BOARD_PLL_CLOCK);
  65. /* Set SysTick clock source to HCLK source divide 2 */
  66. CLK_SetSysTickClockSrc(CLK_CLKSEL0_STCLKSEL_HCLK_DIV2);
  67. /* Lock protected registers */
  68. SYS_LockReg();
  69. }
  70. /**
  71. * This is the timer interrupt service routine.
  72. *
  73. */
  74. void SysTick_Handler(void)
  75. {
  76. /* enter interrupt */
  77. rt_interrupt_enter();
  78. rt_tick_increase();
  79. /* leave interrupt */
  80. rt_interrupt_leave();
  81. }
  82. /**
  83. * This function will initial Nuvoton board.
  84. */
  85. void rt_hw_board_init()
  86. {
  87. /* NVIC Configuration */
  88. NVIC_Configuration();
  89. /* Configure the system clock */
  90. rt_hw_system_init();
  91. /* Configure the SysTick */
  92. SysTick_Config(SystemCoreClock / RT_TICK_PER_SECOND);
  93. /* Initial usart deriver, and set console device */
  94. rt_hw_usart_init();
  95. #ifdef RT_USING_CONSOLE
  96. rt_console_set_device(RT_CONSOLE_DEVICE_NAME);
  97. #endif
  98. /* Call components board initial (use INIT_BOARD_EXPORT()) */
  99. #ifdef RT_USING_COMPONENTS_INIT
  100. rt_components_board_init();
  101. #endif
  102. }
  103. /*@}*/