system_M051Series.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. /**************************************************************************//**
  2. * @file system_M051Series.c
  3. * @version V2.00
  4. * $Revision: 13 $
  5. * $Date: 14/01/28 10:49a $
  6. * @brief M051 Series System Setting Source File
  7. *
  8. * @note
  9. * Copyright (C) 2011 Nuvoton Technology Corp. All rights reserved.
  10. *
  11. ******************************************************************************/
  12. #include <stdio.h>
  13. #include <stdint.h>
  14. #include "M051Series.h"
  15. /*----------------------------------------------------------------------------
  16. Clock Variable definitions
  17. *----------------------------------------------------------------------------*/
  18. uint32_t SystemCoreClock = __HSI; /*!< System Clock Frequency (Core Clock) */
  19. uint32_t CyclesPerUs = (__HSI / 1000000); /*!< Cycles per micro second */
  20. uint32_t PllClock = __HSI; /*!< PLL Output Clock Frequency */
  21. const uint32_t gau32ClkSrcTbl[] = {__HXT, NULL, __HSI, __LIRC, NULL, NULL, NULL, __HIRC};
  22. /**
  23. * @brief Update the Variable SystemCoreClock
  24. *
  25. * @param None
  26. *
  27. * @return None
  28. *
  29. * @details This function is used to update the variable SystemCoreClock
  30. * and must be called whenever the core clock is changed.
  31. */
  32. void SystemCoreClockUpdate(void)
  33. {
  34. uint32_t u32Freq, u32ClkSrc;
  35. uint32_t u32HclkDiv;
  36. u32ClkSrc = CLK->CLKSEL0 & CLK_CLKSEL0_HCLK_S_Msk;
  37. /* Update PLL Clock */
  38. PllClock = CLK_GetPLLClockFreq();
  39. if(u32ClkSrc != CLK_CLKSEL0_HCLK_S_PLL)
  40. {
  41. /* Use the clock sources directly */
  42. u32Freq = gau32ClkSrcTbl[u32ClkSrc];
  43. }
  44. else
  45. {
  46. /* Use PLL clock */
  47. u32Freq = PllClock;
  48. }
  49. u32HclkDiv = (CLK->CLKDIV & CLK_CLKDIV_HCLK_N_Msk) + 1;
  50. /* Update System Core Clock */
  51. SystemCoreClock = u32Freq / u32HclkDiv;
  52. CyclesPerUs = (SystemCoreClock + 500000) / 1000000;
  53. }
  54. /**
  55. * @brief System Initialization
  56. *
  57. * @param None
  58. *
  59. * @return None
  60. *
  61. * @details The necessary initialization of system. Global variables are forbidden here.
  62. */
  63. void SystemInit(void)
  64. {
  65. #ifdef INIT_SYSCLK_AT_BOOTING
  66. int32_t i32TimeoutCnt;
  67. uint32_t u32HclkSelect;
  68. int8_t i8IsPllEn;
  69. PllClock = 0;
  70. i8IsPllEn = 0;
  71. u32HclkSelect = CLK->CLKSEL0 & CLK_CLKSEL0_HCLK_S_Msk;
  72. if(u32HclkSelect == CLK_CLKSEL0_HCLK_S_HXT)
  73. {
  74. /* Set to 50MHz system clock frequency when clock source is from external 12MHz */
  75. CLK->PLLCON = CLK_PLLCON_50MHz_HXT;
  76. /* Waiting for PLL ready */
  77. i32TimeoutCnt = (__HXT / 1000); /* Timeout is about 1ms */
  78. while((CLK->CLKSTATUS & CLK_CLKSTATUS_PLL_STB_Msk) == 0)
  79. {
  80. if(i32TimeoutCnt-- <= 0)
  81. break;
  82. }
  83. i8IsPllEn = 1;
  84. }
  85. else if(u32HclkSelect == CLK_CLKSEL0_HCLK_S_HIRC)
  86. {
  87. /* Set to 50.1918MHz system clock frequency when clock source is from internal 22.1184MHz RC clock */
  88. CLK->PLLCON = CLK_PLLCON_50MHz_HIRC;
  89. /* Waiting for PLL ready */
  90. i32TimeoutCnt = (__HIRC / 1000); /* Timeout is about 1ms */
  91. while((CLK->CLKSTATUS & CLK_CLKSTATUS_PLL_STB_Msk) == 0)
  92. {
  93. if(i32TimeoutCnt-- <= 0)
  94. break;
  95. }
  96. i8IsPllEn = 1;
  97. }
  98. if(i8IsPllEn)
  99. {
  100. /* Set PLL as HCLK clock source (HCLK_S is locked setting)*/
  101. SYS_UnlockReg();
  102. CLK->CLKSEL0 = CLK_CLKSEL0_HCLK_S_PLL;
  103. SYS_LockReg();
  104. }
  105. #endif
  106. }
  107. #if USE_ASSERT
  108. /**
  109. * @brief Assert Error Message
  110. *
  111. * @param[in] file the source file name
  112. * @param[in] line line number
  113. *
  114. * @return None
  115. *
  116. * @details The function prints the source file name and line number where
  117. * the ASSERT_PARAM() error occurs, and then stops in an infinite loop.
  118. */
  119. void AssertError(uint8_t * file, uint32_t line)
  120. {
  121. printf("[%s] line %d : wrong parameters.\r\n", file, line);
  122. /* Infinite loop */
  123. while(1) ;
  124. }
  125. #endif