system_M480.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /**************************************************************************//**
  2. * @file system_M480.c
  3. * @version V1.000
  4. * @brief CMSIS Cortex-M4 Core Peripheral Access Layer Source File for M480
  5. *
  6. * SPDX-License-Identifier: Apache-2.0
  7. * @copyright (C) 2017-2020 Nuvoton Technology Corp. All rights reserved.
  8. *****************************************************************************/
  9. #include "NuMicro.h"
  10. /*----------------------------------------------------------------------------
  11. DEFINES
  12. *----------------------------------------------------------------------------*/
  13. /*----------------------------------------------------------------------------
  14. Clock Variable definitions
  15. *----------------------------------------------------------------------------*/
  16. uint32_t SystemCoreClock = __SYSTEM_CLOCK; /*!< System Clock Frequency (Core Clock)*/
  17. uint32_t CyclesPerUs = (__HSI / 1000000UL); /* Cycles per micro second */
  18. uint32_t PllClock = __HSI; /*!< PLL Output Clock Frequency */
  19. uint32_t gau32ClkSrcTbl[] = {__HXT, __LXT, 0UL, __LIRC, 0UL, 0UL, 0UL, __HIRC};
  20. /*----------------------------------------------------------------------------
  21. Clock functions
  22. *----------------------------------------------------------------------------*/
  23. void SystemCoreClockUpdate (void) /* Get Core Clock Frequency */
  24. {
  25. uint32_t u32Freq, u32ClkSrc;
  26. uint32_t u32HclkDiv;
  27. /* Update PLL Clock */
  28. PllClock = CLK_GetPLLClockFreq();
  29. u32ClkSrc = CLK->CLKSEL0 & CLK_CLKSEL0_HCLKSEL_Msk;
  30. if(u32ClkSrc == CLK_CLKSEL0_HCLKSEL_PLL)
  31. {
  32. /* Use PLL clock */
  33. u32Freq = PllClock;
  34. }
  35. else
  36. {
  37. /* Use the clock sources directly */
  38. u32Freq = gau32ClkSrcTbl[u32ClkSrc];
  39. }
  40. u32HclkDiv = (CLK->CLKDIV0 & CLK_CLKDIV0_HCLKDIV_Msk) + 1UL;
  41. /* Update System Core Clock */
  42. SystemCoreClock = u32Freq / u32HclkDiv;
  43. //if(SystemCoreClock == 0)
  44. // __BKPT(0);
  45. CyclesPerUs = (SystemCoreClock + 500000UL) / 1000000UL;
  46. }
  47. /**
  48. * @brief Set PF.2 and PF.3 to input mode
  49. * @param None
  50. * @return None
  51. * @details GPIO default state could be configured as input or quasi through user config.
  52. * To use HXT, PF.2 and PF.3 must not set as quasi mode. This function changes
  53. * PF.2 and PF.3 to input mode no matter which mode they are working at.
  54. */
  55. static __INLINE void HXTInit(void)
  56. {
  57. PF->MODE &= ~(GPIO_MODE_MODE2_Msk | GPIO_MODE_MODE3_Msk);
  58. }
  59. /**
  60. * @brief Initialize the System
  61. *
  62. * @param none
  63. * @return none
  64. */
  65. void SystemInit (void)
  66. {
  67. /* Add your system initialize code here.
  68. Do not use global variables because this function is called before
  69. reaching pre-main. RW section maybe overwritten afterwards. */
  70. /* FPU settings ------------------------------------------------------------*/
  71. #if (__FPU_PRESENT == 1U) && (__FPU_USED == 1U)
  72. SCB->CPACR |= ((3UL << 10*2) | /* set CP10 Full Access */
  73. (3UL << 11*2) ); /* set CP11 Full Access */
  74. #endif
  75. /* Set access cycle for CPU @ 192MHz */
  76. FMC->CYCCTL = (FMC->CYCCTL & ~FMC_CYCCTL_CYCLE_Msk) | (8 << FMC_CYCCTL_CYCLE_Pos);
  77. /* Configure power down bias, must set 1 before entering power down mode.
  78. So set it at the very beginning */
  79. CLK->LDOCTL |= CLK_LDOCTL_PDBIASEN_Msk;
  80. /* Hand over the control of PF.4~11 I/O function from RTC module to GPIO module */
  81. CLK->APBCLK0 |= CLK_APBCLK0_RTCCKEN_Msk;
  82. RTC->GPIOCTL0 &= ~(RTC_GPIOCTL0_CTLSEL0_Msk | RTC_GPIOCTL0_CTLSEL1_Msk |
  83. RTC_GPIOCTL0_CTLSEL2_Msk | RTC_GPIOCTL0_CTLSEL3_Msk);
  84. RTC->GPIOCTL1 &= ~(RTC_GPIOCTL1_CTLSEL4_Msk | RTC_GPIOCTL1_CTLSEL5_Msk |
  85. RTC_GPIOCTL1_CTLSEL6_Msk | RTC_GPIOCTL1_CTLSEL7_Msk);
  86. CLK->APBCLK0 &= ~CLK_APBCLK0_RTCCKEN_Msk;
  87. HXTInit();
  88. }
  89. /*** (C) COPYRIGHT 2016 Nuvoton Technology Corp. ***/