system_hc32f448.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. /**
  2. *******************************************************************************
  3. * @file system_hc32f448.c
  4. * @brief This file provides two functions and two global variables to be called
  5. * from user application.
  6. @verbatim
  7. Change Logs:
  8. Date Author Notes
  9. 2022-12-31 CDT First version
  10. @endverbatim
  11. *******************************************************************************
  12. * Copyright (C) 2022, Xiaohua Semiconductor Co., Ltd. All rights reserved.
  13. *
  14. * This software component is licensed by XHSC under BSD 3-Clause license
  15. * (the "License"); You may not use this file except in compliance with the
  16. * License. You may obtain a copy of the License at:
  17. * opensource.org/licenses/BSD-3-Clause
  18. *
  19. *******************************************************************************
  20. */
  21. /*******************************************************************************
  22. * Include files
  23. ******************************************************************************/
  24. #include "system_hc32f448.h"
  25. /**
  26. * @addtogroup CMSIS
  27. * @{
  28. */
  29. /**
  30. * @addtogroup HC32F448_System
  31. * @{
  32. */
  33. /*******************************************************************************
  34. * Global pre-processor symbols/macros ('define')
  35. ******************************************************************************/
  36. /**
  37. * @defgroup HC32F448_System_Local_Macros HC32F448 System Local Macros
  38. * @{
  39. */
  40. #define HRC_16MHz_VALUE (16000000UL) /*!< Internal high speed RC freq. */
  41. #define HRC_20MHz_VALUE (20000000UL) /*!< Internal high speed RC freq. */
  42. /* HRC select */
  43. #define HRC_FREQ_MON() (*((volatile uint32_t *)(0x40010684UL)))
  44. /* Vector Table base offset field */
  45. #ifndef VECT_TAB_OFFSET
  46. #define VECT_TAB_OFFSET (0x0UL) /*!< This value must be a multiple of 0x400. */
  47. #endif
  48. /**
  49. * @}
  50. */
  51. /*******************************************************************************
  52. * Global variable definitions (declared in header file with 'extern')
  53. ******************************************************************************/
  54. /**
  55. * @addtogroup HC32F448_System_Global_Variable
  56. * @{
  57. */
  58. /*!< System clock frequency (Core clock) */
  59. __NO_INIT uint32_t SystemCoreClock;
  60. /*!< High speed RC frequency (HCR clock) */
  61. __NO_INIT uint32_t HRC_VALUE;
  62. /**
  63. * @}
  64. */
  65. /*******************************************************************************
  66. * Local variable definitions ('static')
  67. ******************************************************************************/
  68. /*******************************************************************************
  69. * Function implementation - global ('extern') and local ('static')
  70. ******************************************************************************/
  71. /**
  72. * @addtogroup HC32F448_System_Global_Functions
  73. * @{
  74. */
  75. /**
  76. * @brief Setup the microcontroller system. Initialize the System and update
  77. * the SystemCoreClock variable.
  78. * @param None
  79. * @retval None
  80. */
  81. void SystemInit(void)
  82. {
  83. /* FPU settings */
  84. #if (__FPU_PRESENT == 1) && (__FPU_USED == 1)
  85. SCB->CPACR |= ((3UL << 20) | (3UL << 22)); /* set CP10 and CP11 Full Access */
  86. #endif
  87. SystemCoreClockUpdate();
  88. #if defined (ROM_EXT_QSPI)
  89. SystemInit_QspiMem();
  90. #endif /* ROM_EXT_QSPI */
  91. /* Configure the Vector Table relocation */
  92. SCB->VTOR = VECT_TAB_OFFSET; /* Vector Table Relocation */
  93. }
  94. /**
  95. * @brief Update SystemCoreClock variable according to Clock Register Values.
  96. * @param None
  97. * @retval None
  98. */
  99. void SystemCoreClockUpdate(void)
  100. {
  101. uint8_t u8SysClkSrc;
  102. uint32_t plln;
  103. uint32_t pllp;
  104. uint32_t pllm;
  105. uint32_t u32PllSrcFreq;
  106. /* Select proper HRC_VALUE according to ICG1.HRCFREQSEL bit */
  107. if (1UL == (HRC_FREQ_MON() & 1UL)) {
  108. HRC_VALUE = HRC_16MHz_VALUE;
  109. } else {
  110. HRC_VALUE = HRC_20MHz_VALUE;
  111. }
  112. u8SysClkSrc = CM_CMU->CKSWR & CMU_CKSWR_CKSW;
  113. switch (u8SysClkSrc) {
  114. case 0x00U: /* use internal high speed RC */
  115. SystemCoreClock = HRC_VALUE;
  116. break;
  117. case 0x01U: /* use internal middle speed RC */
  118. SystemCoreClock = MRC_VALUE;
  119. break;
  120. case 0x02U: /* use internal low speed RC */
  121. SystemCoreClock = LRC_VALUE;
  122. break;
  123. case 0x03U: /* use external high speed OSC */
  124. SystemCoreClock = XTAL_VALUE;
  125. break;
  126. case 0x04U: /* use external low speed OSC */
  127. SystemCoreClock = XTAL32_VALUE;
  128. break;
  129. case 0x05U: /* use PLLH */
  130. /* PLLCLK = ((pllsrc / pllm) * plln) / pllp */
  131. plln = (CM_CMU->PLLHCFGR & CMU_PLLHCFGR_PLLHN) >> CMU_PLLHCFGR_PLLHN_POS;
  132. pllp = (CM_CMU->PLLHCFGR & CMU_PLLHCFGR_PLLHP) >> CMU_PLLHCFGR_PLLHP_POS;
  133. pllm = (CM_CMU->PLLHCFGR & CMU_PLLHCFGR_PLLHM) >> CMU_PLLHCFGR_PLLHM_POS;
  134. if (0UL == bCM_CMU->PLLHCFGR_b.PLLSRC) { /* use external high speed OSC as PLL source */
  135. u32PllSrcFreq = XTAL_VALUE;
  136. } else { /* use internal high RC as PLL source */
  137. u32PllSrcFreq = HRC_VALUE;
  138. }
  139. SystemCoreClock = u32PllSrcFreq / (pllm + 1UL) * (plln + 1UL) / (pllp + 1UL);
  140. break;
  141. default:
  142. break;
  143. }
  144. }
  145. #if defined (ROM_EXT_QSPI)
  146. /**
  147. * @brief Initialize the QSPI memory.
  148. * @param None
  149. * @retval None
  150. */
  151. __WEAKDEF void SystemInit_QspiMem(void)
  152. {
  153. /* QSPI configure */
  154. CM_GPIO->PWPR = 0xA501U;
  155. /* High driver */
  156. CM_GPIO->PCRC7 = 0x0120U;
  157. CM_GPIO->PCRB14 = 0x0120U;
  158. CM_GPIO->PCRB13 = 0x0120U;
  159. CM_GPIO->PCRD9 = 0x0120U;
  160. CM_GPIO->PCRD10 = 0x0120U;
  161. CM_GPIO->PCRD11 = 0x0120U;
  162. /* Set function */
  163. CM_GPIO->PFSRC7 = 0x07U;
  164. CM_GPIO->PFSRB14 = 0x07U;
  165. CM_GPIO->PFSRB13 = 0x07U;
  166. CM_GPIO->PFSRD9 = 0x07U;
  167. CM_GPIO->PFSRD10 = 0x07U;
  168. CM_GPIO->PFSRD11 = 0x07U;
  169. /* qspi configure */
  170. CM_PWC->FCG1 &= ~0x00000008UL;
  171. CM_QSPI->CR = 0x0002000D;
  172. CM_QSPI->CSCR = 0x00000001;
  173. CM_QSPI->FCR = 0x00008332;
  174. }
  175. #endif /* ROM_EXT_QSPI */
  176. /**
  177. * @}
  178. */
  179. /**
  180. * @}
  181. */
  182. /**
  183. * @}
  184. */
  185. /*******************************************************************************
  186. * EOF (not truncated)
  187. ******************************************************************************/