system_stm32h7xx.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  1. /**
  2. ******************************************************************************
  3. * @file system_stm32h7xx.c
  4. * @author MCD Application Team
  5. * @brief CMSIS Cortex-Mx Device Peripheral Access Layer System Source File.
  6. *
  7. * This file provides two functions and one global variable to be called from
  8. * user application:
  9. * - SystemInit(): This function is called at startup just after reset and
  10. * before branch to main program. This call is made inside
  11. * the "startup_stm32h7xx.s" file.
  12. *
  13. * - SystemCoreClock variable: Contains the core clock, it can be used
  14. * by the user application to setup the SysTick
  15. * timer or configure other parameters.
  16. *
  17. * - SystemCoreClockUpdate(): Updates the variable SystemCoreClock and must
  18. * be called whenever the core clock is changed
  19. * during program execution.
  20. *
  21. *
  22. ******************************************************************************
  23. * @attention
  24. *
  25. * <h2><center>&copy; Copyright (c) 2017 STMicroelectronics.
  26. * All rights reserved.</center></h2>
  27. *
  28. * This software component is licensed by ST under BSD 3-Clause license,
  29. * the "License"; You may not use this file except in compliance with the
  30. * License. You may obtain a copy of the License at:
  31. * opensource.org/licenses/BSD-3-Clause
  32. *
  33. ******************************************************************************
  34. */
  35. /** @addtogroup CMSIS
  36. * @{
  37. */
  38. /** @addtogroup stm32h7xx_system
  39. * @{
  40. */
  41. /** @addtogroup STM32H7xx_System_Private_Includes
  42. * @{
  43. */
  44. #include "stm32h7xx.h"
  45. #include <math.h>
  46. #if !defined (HSE_VALUE)
  47. #define HSE_VALUE ((uint32_t)25000000) /*!< Value of the External oscillator in Hz */
  48. #endif /* HSE_VALUE */
  49. #if !defined (CSI_VALUE)
  50. #define CSI_VALUE ((uint32_t)4000000) /*!< Value of the Internal oscillator in Hz*/
  51. #endif /* CSI_VALUE */
  52. #if !defined (HSI_VALUE)
  53. #define HSI_VALUE ((uint32_t)64000000) /*!< Value of the Internal oscillator in Hz*/
  54. #endif /* HSI_VALUE */
  55. /**
  56. * @}
  57. */
  58. /** @addtogroup STM32H7xx_System_Private_TypesDefinitions
  59. * @{
  60. */
  61. /**
  62. * @}
  63. */
  64. /** @addtogroup STM32H7xx_System_Private_Defines
  65. * @{
  66. */
  67. /************************* Miscellaneous Configuration ************************/
  68. /*!< Uncomment the following line if you need to use initialized data in D2 domain SRAM (AHB SRAM) */
  69. /* #define DATA_IN_D2_SRAM */
  70. /*!< Uncomment the following line if you need to relocate your vector Table in
  71. Internal SRAM. */
  72. /* #define VECT_TAB_SRAM */
  73. #define VECT_TAB_OFFSET 0x00000000UL /*!< Vector Table base offset field.
  74. This value must be a multiple of 0x200. */
  75. /******************************************************************************/
  76. /**
  77. * @}
  78. */
  79. /** @addtogroup STM32H7xx_System_Private_Macros
  80. * @{
  81. */
  82. /**
  83. * @}
  84. */
  85. /** @addtogroup STM32H7xx_System_Private_Variables
  86. * @{
  87. */
  88. /* This variable is updated in three ways:
  89. 1) by calling CMSIS function SystemCoreClockUpdate()
  90. 2) by calling HAL API function HAL_RCC_GetHCLKFreq()
  91. 3) each time HAL_RCC_ClockConfig() is called to configure the system clock frequency
  92. Note: If you use this function to configure the system clock; then there
  93. is no need to call the 2 first functions listed above, since SystemCoreClock
  94. variable is updated automatically.
  95. */
  96. uint32_t SystemCoreClock = 64000000;
  97. uint32_t SystemD2Clock = 64000000;
  98. const uint8_t D1CorePrescTable[16] = {0, 0, 0, 0, 1, 2, 3, 4, 1, 2, 3, 4, 6, 7, 8, 9};
  99. /**
  100. * @}
  101. */
  102. /** @addtogroup STM32H7xx_System_Private_FunctionPrototypes
  103. * @{
  104. */
  105. /**
  106. * @}
  107. */
  108. /** @addtogroup STM32H7xx_System_Private_Functions
  109. * @{
  110. */
  111. /**
  112. * @brief Setup the microcontroller system
  113. * Initialize the FPU setting and vector table location
  114. * configuration.
  115. * @param None
  116. * @retval None
  117. */
  118. void SystemInit (void)
  119. {
  120. #if defined (DATA_IN_D2_SRAM)
  121. __IO uint32_t tmpreg;
  122. #endif /* DATA_IN_D2_SRAM */
  123. /* FPU settings ------------------------------------------------------------*/
  124. #if (__FPU_PRESENT == 1) && (__FPU_USED == 1)
  125. SCB->CPACR |= ((3UL << (10*2))|(3UL << (11*2))); /* set CP10 and CP11 Full Access */
  126. #endif
  127. /* Reset the RCC clock configuration to the default reset state ------------*/
  128. /* Increasing the CPU frequency */
  129. if(FLASH_LATENCY_DEFAULT > (READ_BIT((FLASH->ACR), FLASH_ACR_LATENCY)))
  130. {
  131. /* Program the new number of wait states to the LATENCY bits in the FLASH_ACR register */
  132. MODIFY_REG(FLASH->ACR, FLASH_ACR_LATENCY, (uint32_t)(FLASH_LATENCY_DEFAULT));
  133. }
  134. /* Set HSION bit */
  135. RCC->CR |= RCC_CR_HSION;
  136. /* Reset CFGR register */
  137. RCC->CFGR = 0x00000000;
  138. /* Reset HSEON, HSECSSON, CSION, HSI48ON, CSIKERON, PLL1ON, PLL2ON and PLL3ON bits */
  139. RCC->CR &= 0xEAF6ED7FU;
  140. /* Decreasing the number of wait states because of lower CPU frequency */
  141. if(FLASH_LATENCY_DEFAULT < (READ_BIT((FLASH->ACR), FLASH_ACR_LATENCY)))
  142. {
  143. /* Program the new number of wait states to the LATENCY bits in the FLASH_ACR register */
  144. MODIFY_REG(FLASH->ACR, FLASH_ACR_LATENCY, (uint32_t)(FLASH_LATENCY_DEFAULT));
  145. }
  146. #if defined(D3_SRAM_BASE)
  147. /* Reset D1CFGR register */
  148. RCC->D1CFGR = 0x00000000;
  149. /* Reset D2CFGR register */
  150. RCC->D2CFGR = 0x00000000;
  151. /* Reset D3CFGR register */
  152. RCC->D3CFGR = 0x00000000;
  153. #else
  154. /* Reset CDCFGR1 register */
  155. RCC->CDCFGR1 = 0x00000000;
  156. /* Reset CDCFGR2 register */
  157. RCC->CDCFGR2 = 0x00000000;
  158. /* Reset SRDCFGR register */
  159. RCC->SRDCFGR = 0x00000000;
  160. #endif
  161. /* Reset PLLCKSELR register */
  162. RCC->PLLCKSELR = 0x02020200;
  163. /* Reset PLLCFGR register */
  164. RCC->PLLCFGR = 0x01FF0000;
  165. /* Reset PLL1DIVR register */
  166. RCC->PLL1DIVR = 0x01010280;
  167. /* Reset PLL1FRACR register */
  168. RCC->PLL1FRACR = 0x00000000;
  169. /* Reset PLL2DIVR register */
  170. RCC->PLL2DIVR = 0x01010280;
  171. /* Reset PLL2FRACR register */
  172. RCC->PLL2FRACR = 0x00000000;
  173. /* Reset PLL3DIVR register */
  174. RCC->PLL3DIVR = 0x01010280;
  175. /* Reset PLL3FRACR register */
  176. RCC->PLL3FRACR = 0x00000000;
  177. /* Reset HSEBYP bit */
  178. RCC->CR &= 0xFFFBFFFFU;
  179. /* Disable all interrupts */
  180. RCC->CIER = 0x00000000;
  181. #if (STM32H7_DEV_ID == 0x450UL)
  182. /* dual core CM7 or single core line */
  183. if((DBGMCU->IDCODE & 0xFFFF0000U) < 0x20000000U)
  184. {
  185. /* if stm32h7 revY*/
  186. /* Change the switch matrix read issuing capability to 1 for the AXI SRAM target (Target 7) */
  187. *((__IO uint32_t*)0x51008108) = 0x000000001U;
  188. }
  189. #endif
  190. #if defined (DATA_IN_D2_SRAM)
  191. /* in case of initialized data in D2 SRAM (AHB SRAM) , enable the D2 SRAM clock (AHB SRAM clock) */
  192. #if defined(RCC_AHB2ENR_D2SRAM3EN)
  193. RCC->AHB2ENR |= (RCC_AHB2ENR_D2SRAM1EN | RCC_AHB2ENR_D2SRAM2EN | RCC_AHB2ENR_D2SRAM3EN);
  194. #elif defined(RCC_AHB2ENR_D2SRAM2EN)
  195. RCC->AHB2ENR |= (RCC_AHB2ENR_D2SRAM1EN | RCC_AHB2ENR_D2SRAM2EN);
  196. #else
  197. RCC->AHB2ENR |= (RCC_AHB2ENR_AHBSRAM1EN | RCC_AHB2ENR_AHBSRAM2EN);
  198. #endif /* RCC_AHB2ENR_D2SRAM3EN */
  199. tmpreg = RCC->AHB2ENR;
  200. (void) tmpreg;
  201. #endif /* DATA_IN_D2_SRAM */
  202. #if defined(DUAL_CORE) && defined(CORE_CM4)
  203. /* Configure the Vector Table location add offset address for cortex-M4 ------------------*/
  204. #ifdef VECT_TAB_SRAM
  205. SCB->VTOR = D2_AXISRAM_BASE | VECT_TAB_OFFSET; /* Vector Table Relocation in Internal SRAM */
  206. #else
  207. SCB->VTOR = FLASH_BANK2_BASE | VECT_TAB_OFFSET; /* Vector Table Relocation in Internal FLASH */
  208. #endif /* VECT_TAB_SRAM */
  209. #else
  210. /*
  211. * Disable the FMC bank1 (enabled after reset).
  212. * This, prevents CPU speculation access on this bank which blocks the use of FMC during
  213. * 24us. During this time the others FMC master (such as LTDC) cannot use it!
  214. */
  215. FMC_Bank1_R->BTCR[0] = 0x000030D2;
  216. /* Configure the Vector Table location add offset address for cortex-M7 ------------------*/
  217. #ifdef VECT_TAB_SRAM
  218. SCB->VTOR = D1_AXISRAM_BASE | VECT_TAB_OFFSET; /* Vector Table Relocation in Internal AXI-RAM */
  219. #else
  220. SCB->VTOR = FLASH_BANK1_BASE | VECT_TAB_OFFSET; /* Vector Table Relocation in Internal FLASH */
  221. #endif
  222. #endif /*DUAL_CORE && CORE_CM4*/
  223. }
  224. /**
  225. * @brief Update SystemCoreClock variable according to Clock Register Values.
  226. * The SystemCoreClock variable contains the core clock , it can
  227. * be used by the user application to setup the SysTick timer or configure
  228. * other parameters.
  229. *
  230. * @note Each time the core clock changes, this function must be called
  231. * to update SystemCoreClock variable value. Otherwise, any configuration
  232. * based on this variable will be incorrect.
  233. *
  234. * @note - The system frequency computed by this function is not the real
  235. * frequency in the chip. It is calculated based on the predefined
  236. * constant and the selected clock source:
  237. *
  238. * - If SYSCLK source is CSI, SystemCoreClock will contain the CSI_VALUE(*)
  239. * - If SYSCLK source is HSI, SystemCoreClock will contain the HSI_VALUE(**)
  240. * - If SYSCLK source is HSE, SystemCoreClock will contain the HSE_VALUE(***)
  241. * - If SYSCLK source is PLL, SystemCoreClock will contain the CSI_VALUE(*),
  242. * HSI_VALUE(**) or HSE_VALUE(***) multiplied/divided by the PLL factors.
  243. *
  244. * (*) CSI_VALUE is a constant defined in stm32h7xx_hal.h file (default value
  245. * 4 MHz) but the real value may vary depending on the variations
  246. * in voltage and temperature.
  247. * (**) HSI_VALUE is a constant defined in stm32h7xx_hal.h file (default value
  248. * 64 MHz) but the real value may vary depending on the variations
  249. * in voltage and temperature.
  250. *
  251. * (***)HSE_VALUE is a constant defined in stm32h7xx_hal.h file (default value
  252. * 25 MHz), user has to ensure that HSE_VALUE is same as the real
  253. * frequency of the crystal used. Otherwise, this function may
  254. * have wrong result.
  255. *
  256. * - The result of this function could be not correct when using fractional
  257. * value for HSE crystal.
  258. * @param None
  259. * @retval None
  260. */
  261. void SystemCoreClockUpdate (void)
  262. {
  263. uint32_t pllp, pllsource, pllm, pllfracen, hsivalue, tmp;
  264. uint32_t common_system_clock;
  265. float_t fracn1, pllvco;
  266. /* Get SYSCLK source -------------------------------------------------------*/
  267. switch (RCC->CFGR & RCC_CFGR_SWS)
  268. {
  269. case RCC_CFGR_SWS_HSI: /* HSI used as system clock source */
  270. common_system_clock = (uint32_t) (HSI_VALUE >> ((RCC->CR & RCC_CR_HSIDIV)>> 3));
  271. break;
  272. case RCC_CFGR_SWS_CSI: /* CSI used as system clock source */
  273. common_system_clock = CSI_VALUE;
  274. break;
  275. case RCC_CFGR_SWS_HSE: /* HSE used as system clock source */
  276. common_system_clock = HSE_VALUE;
  277. break;
  278. case RCC_CFGR_SWS_PLL1: /* PLL1 used as system clock source */
  279. /* PLL_VCO = (HSE_VALUE or HSI_VALUE or CSI_VALUE/ PLLM) * PLLN
  280. SYSCLK = PLL_VCO / PLLR
  281. */
  282. pllsource = (RCC->PLLCKSELR & RCC_PLLCKSELR_PLLSRC);
  283. pllm = ((RCC->PLLCKSELR & RCC_PLLCKSELR_DIVM1)>> 4) ;
  284. pllfracen = ((RCC->PLLCFGR & RCC_PLLCFGR_PLL1FRACEN)>>RCC_PLLCFGR_PLL1FRACEN_Pos);
  285. fracn1 = (float_t)(uint32_t)(pllfracen* ((RCC->PLL1FRACR & RCC_PLL1FRACR_FRACN1)>> 3));
  286. if (pllm != 0U)
  287. {
  288. switch (pllsource)
  289. {
  290. case RCC_PLLCKSELR_PLLSRC_HSI: /* HSI used as PLL clock source */
  291. hsivalue = (HSI_VALUE >> ((RCC->CR & RCC_CR_HSIDIV)>> 3)) ;
  292. pllvco = ( (float_t)hsivalue / (float_t)pllm) * ((float_t)(uint32_t)(RCC->PLL1DIVR & RCC_PLL1DIVR_N1) + (fracn1/(float_t)0x2000) +(float_t)1 );
  293. break;
  294. case RCC_PLLCKSELR_PLLSRC_CSI: /* CSI used as PLL clock source */
  295. pllvco = ((float_t)CSI_VALUE / (float_t)pllm) * ((float_t)(uint32_t)(RCC->PLL1DIVR & RCC_PLL1DIVR_N1) + (fracn1/(float_t)0x2000) +(float_t)1 );
  296. break;
  297. case RCC_PLLCKSELR_PLLSRC_HSE: /* HSE used as PLL clock source */
  298. pllvco = ((float_t)HSE_VALUE / (float_t)pllm) * ((float_t)(uint32_t)(RCC->PLL1DIVR & RCC_PLL1DIVR_N1) + (fracn1/(float_t)0x2000) +(float_t)1 );
  299. break;
  300. default:
  301. pllvco = ((float_t)CSI_VALUE / (float_t)pllm) * ((float_t)(uint32_t)(RCC->PLL1DIVR & RCC_PLL1DIVR_N1) + (fracn1/(float_t)0x2000) +(float_t)1 );
  302. break;
  303. }
  304. pllp = (((RCC->PLL1DIVR & RCC_PLL1DIVR_P1) >>9) + 1U ) ;
  305. common_system_clock = (uint32_t)(float_t)(pllvco/(float_t)pllp);
  306. }
  307. else
  308. {
  309. common_system_clock = 0U;
  310. }
  311. break;
  312. default:
  313. common_system_clock = CSI_VALUE;
  314. break;
  315. }
  316. /* Compute SystemClock frequency --------------------------------------------------*/
  317. #if defined (RCC_D1CFGR_D1CPRE)
  318. tmp = D1CorePrescTable[(RCC->D1CFGR & RCC_D1CFGR_D1CPRE)>> RCC_D1CFGR_D1CPRE_Pos];
  319. /* common_system_clock frequency : CM7 CPU frequency */
  320. common_system_clock >>= tmp;
  321. /* SystemD2Clock frequency : CM4 CPU, AXI and AHBs Clock frequency */
  322. SystemD2Clock = (common_system_clock >> ((D1CorePrescTable[(RCC->D1CFGR & RCC_D1CFGR_HPRE)>> RCC_D1CFGR_HPRE_Pos]) & 0x1FU));
  323. #else
  324. tmp = D1CorePrescTable[(RCC->CDCFGR1 & RCC_CDCFGR1_CDCPRE)>> RCC_CDCFGR1_CDCPRE_Pos];
  325. /* common_system_clock frequency : CM7 CPU frequency */
  326. common_system_clock >>= tmp;
  327. /* SystemD2Clock frequency : AXI and AHBs Clock frequency */
  328. SystemD2Clock = (common_system_clock >> ((D1CorePrescTable[(RCC->CDCFGR1 & RCC_CDCFGR1_HPRE)>> RCC_CDCFGR1_HPRE_Pos]) & 0x1FU));
  329. #endif
  330. #if defined(DUAL_CORE) && defined(CORE_CM4)
  331. SystemCoreClock = SystemD2Clock;
  332. #else
  333. SystemCoreClock = common_system_clock;
  334. #endif /* DUAL_CORE && CORE_CM4 */
  335. }
  336. /**
  337. * @}
  338. */
  339. /**
  340. * @}
  341. */
  342. /**
  343. * @}
  344. */
  345. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/