system_stm32h7xx.c 16 KB

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