system_stm32f7xx.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. /**
  2. ******************************************************************************
  3. * @file system_stm32f7xx.c
  4. * @author MCD Application Team
  5. * @brief CMSIS Cortex-M7 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_stm32f7xx.s" file.
  12. *
  13. * - SystemCoreClock variable: Contains the core clock (HCLK), 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) 2016 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 stm32f7xx_system
  38. * @{
  39. */
  40. /** @addtogroup STM32F7xx_System_Private_Includes
  41. * @{
  42. */
  43. #include "stm32f7xx.h"
  44. #if !defined (HSE_VALUE)
  45. #define HSE_VALUE ((uint32_t)25000000) /*!< Default value of the External oscillator in Hz */
  46. #endif /* HSE_VALUE */
  47. #if !defined (HSI_VALUE)
  48. #define HSI_VALUE ((uint32_t)16000000) /*!< Value of the Internal oscillator in Hz*/
  49. #endif /* HSI_VALUE */
  50. /**
  51. * @}
  52. */
  53. /** @addtogroup STM32F7xx_System_Private_TypesDefinitions
  54. * @{
  55. */
  56. /**
  57. * @}
  58. */
  59. /** @addtogroup STM32F7xx_System_Private_Defines
  60. * @{
  61. */
  62. /************************* Miscellaneous Configuration ************************/
  63. /* Note: Following vector table addresses must be defined in line with linker
  64. configuration. */
  65. /*!< Uncomment the following line if you need to relocate the vector table
  66. anywhere in Flash or Sram, else the vector table is kept at the automatic
  67. remap of boot address selected */
  68. /* #define USER_VECT_TAB_ADDRESS */
  69. #if defined(USER_VECT_TAB_ADDRESS)
  70. /*!< Uncomment the following line if you need to relocate your vector Table
  71. in Sram else user remap will be done in Flash. */
  72. /* #define VECT_TAB_SRAM */
  73. #if defined(VECT_TAB_SRAM)
  74. #define VECT_TAB_BASE_ADDRESS RAMDTCM_BASE /*!< Vector Table base address field.
  75. This value must be a multiple of 0x200. */
  76. #define VECT_TAB_OFFSET 0x00000000U /*!< Vector Table base offset field.
  77. This value must be a multiple of 0x200. */
  78. #else
  79. #define VECT_TAB_BASE_ADDRESS FLASH_BASE /*!< Vector Table base address field.
  80. This value must be a multiple of 0x200. */
  81. #define VECT_TAB_OFFSET 0x00000000U /*!< Vector Table base offset field.
  82. This value must be a multiple of 0x200. */
  83. #endif /* VECT_TAB_SRAM */
  84. #endif /* USER_VECT_TAB_ADDRESS */
  85. /******************************************************************************/
  86. /**
  87. * @}
  88. */
  89. /** @addtogroup STM32F7xx_System_Private_Macros
  90. * @{
  91. */
  92. /**
  93. * @}
  94. */
  95. /** @addtogroup STM32F7xx_System_Private_Variables
  96. * @{
  97. */
  98. /* This variable is updated in three ways:
  99. 1) by calling CMSIS function SystemCoreClockUpdate()
  100. 2) by calling HAL API function HAL_RCC_GetHCLKFreq()
  101. 3) each time HAL_RCC_ClockConfig() is called to configure the system clock frequency
  102. Note: If you use this function to configure the system clock; then there
  103. is no need to call the 2 first functions listed above, since SystemCoreClock
  104. variable is updated automatically.
  105. */
  106. uint32_t SystemCoreClock = 16000000;
  107. const uint8_t AHBPrescTable[16] = {0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 4, 6, 7, 8, 9};
  108. const uint8_t APBPrescTable[8] = {0, 0, 0, 0, 1, 2, 3, 4};
  109. /**
  110. * @}
  111. */
  112. /** @addtogroup STM32F7xx_System_Private_FunctionPrototypes
  113. * @{
  114. */
  115. /**
  116. * @}
  117. */
  118. /** @addtogroup STM32F7xx_System_Private_Functions
  119. * @{
  120. */
  121. /**
  122. * @brief Setup the microcontroller system
  123. * Initialize the Embedded Flash Interface, the PLL and update the
  124. * SystemFrequency variable.
  125. * @param None
  126. * @retval None
  127. */
  128. void SystemInit(void)
  129. {
  130. /* FPU settings ------------------------------------------------------------*/
  131. #if (__FPU_PRESENT == 1) && (__FPU_USED == 1)
  132. SCB->CPACR |= ((3UL << 10*2)|(3UL << 11*2)); /* set CP10 and CP11 Full Access */
  133. #endif
  134. /* Configure the Vector Table location -------------------------------------*/
  135. #if defined(USER_VECT_TAB_ADDRESS)
  136. SCB->VTOR = VECT_TAB_BASE_ADDRESS | VECT_TAB_OFFSET; /* Vector Table Relocation in Internal SRAM */
  137. #endif /* USER_VECT_TAB_ADDRESS */
  138. }
  139. /**
  140. * @brief Update SystemCoreClock variable according to Clock Register Values.
  141. * The SystemCoreClock variable contains the core clock (HCLK), it can
  142. * be used by the user application to setup the SysTick timer or configure
  143. * other parameters.
  144. *
  145. * @note Each time the core clock (HCLK) changes, this function must be called
  146. * to update SystemCoreClock variable value. Otherwise, any configuration
  147. * based on this variable will be incorrect.
  148. *
  149. * @note - The system frequency computed by this function is not the real
  150. * frequency in the chip. It is calculated based on the predefined
  151. * constant and the selected clock source:
  152. *
  153. * - If SYSCLK source is HSI, SystemCoreClock will contain the HSI_VALUE(*)
  154. *
  155. * - If SYSCLK source is HSE, SystemCoreClock will contain the HSE_VALUE(**)
  156. *
  157. * - If SYSCLK source is PLL, SystemCoreClock will contain the HSE_VALUE(**)
  158. * or HSI_VALUE(*) multiplied/divided by the PLL factors.
  159. *
  160. * (*) HSI_VALUE is a constant defined in stm32f7xx_hal_conf.h file (default value
  161. * 16 MHz) but the real value may vary depending on the variations
  162. * in voltage and temperature.
  163. *
  164. * (**) HSE_VALUE is a constant defined in stm32f7xx_hal_conf.h file (default value
  165. * 25 MHz), user has to ensure that HSE_VALUE is same as the real
  166. * frequency of the crystal used. Otherwise, this function may
  167. * have wrong result.
  168. *
  169. * - The result of this function could be not correct when using fractional
  170. * value for HSE crystal.
  171. *
  172. * @param None
  173. * @retval None
  174. */
  175. void SystemCoreClockUpdate(void)
  176. {
  177. uint32_t tmp = 0, pllvco = 0, pllp = 2, pllsource = 0, pllm = 2;
  178. /* Get SYSCLK source -------------------------------------------------------*/
  179. tmp = RCC->CFGR & RCC_CFGR_SWS;
  180. switch (tmp)
  181. {
  182. case 0x00: /* HSI used as system clock source */
  183. SystemCoreClock = HSI_VALUE;
  184. break;
  185. case 0x04: /* HSE used as system clock source */
  186. SystemCoreClock = HSE_VALUE;
  187. break;
  188. case 0x08: /* PLL used as system clock source */
  189. /* PLL_VCO = (HSE_VALUE or HSI_VALUE / PLL_M) * PLL_N
  190. SYSCLK = PLL_VCO / PLL_P
  191. */
  192. pllsource = (RCC->PLLCFGR & RCC_PLLCFGR_PLLSRC) >> 22;
  193. pllm = RCC->PLLCFGR & RCC_PLLCFGR_PLLM;
  194. if (pllsource != 0)
  195. {
  196. /* HSE used as PLL clock source */
  197. pllvco = (HSE_VALUE / pllm) * ((RCC->PLLCFGR & RCC_PLLCFGR_PLLN) >> 6);
  198. }
  199. else
  200. {
  201. /* HSI used as PLL clock source */
  202. pllvco = (HSI_VALUE / pllm) * ((RCC->PLLCFGR & RCC_PLLCFGR_PLLN) >> 6);
  203. }
  204. pllp = (((RCC->PLLCFGR & RCC_PLLCFGR_PLLP) >>16) + 1 ) *2;
  205. SystemCoreClock = pllvco/pllp;
  206. break;
  207. default:
  208. SystemCoreClock = HSI_VALUE;
  209. break;
  210. }
  211. /* Compute HCLK frequency --------------------------------------------------*/
  212. /* Get HCLK prescaler */
  213. tmp = AHBPrescTable[((RCC->CFGR & RCC_CFGR_HPRE) >> 4)];
  214. /* HCLK frequency */
  215. SystemCoreClock >>= tmp;
  216. }
  217. /**
  218. * @}
  219. */
  220. /**
  221. * @}
  222. */
  223. /**
  224. * @}
  225. */
  226. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/