system_stm32f7xx.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  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. * <h2><center>&copy; COPYRIGHT 2016 STMicroelectronics</center></h2>
  26. *
  27. * Redistribution and use in source and binary forms, with or without modification,
  28. * are permitted provided that the following conditions are met:
  29. * 1. Redistributions of source code must retain the above copyright notice,
  30. * this list of conditions and the following disclaimer.
  31. * 2. Redistributions in binary form must reproduce the above copyright notice,
  32. * this list of conditions and the following disclaimer in the documentation
  33. * and/or other materials provided with the distribution.
  34. * 3. Neither the name of STMicroelectronics nor the names of its contributors
  35. * may be used to endorse or promote products derived from this software
  36. * without specific prior written permission.
  37. *
  38. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  39. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  40. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  41. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  42. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  43. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  44. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  45. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  46. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  47. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  48. *
  49. ******************************************************************************
  50. */
  51. /** @addtogroup CMSIS
  52. * @{
  53. */
  54. /** @addtogroup stm32f7xx_system
  55. * @{
  56. */
  57. /** @addtogroup STM32F7xx_System_Private_Includes
  58. * @{
  59. */
  60. #include "stm32f7xx.h"
  61. #if !defined (HSE_VALUE)
  62. #define HSE_VALUE ((uint32_t)25000000) /*!< Default value of the External oscillator in Hz */
  63. #endif /* HSE_VALUE */
  64. #if !defined (HSI_VALUE)
  65. #define HSI_VALUE ((uint32_t)16000000) /*!< Value of the Internal oscillator in Hz*/
  66. #endif /* HSI_VALUE */
  67. /**
  68. * @}
  69. */
  70. /** @addtogroup STM32F7xx_System_Private_TypesDefinitions
  71. * @{
  72. */
  73. /**
  74. * @}
  75. */
  76. /** @addtogroup STM32F7xx_System_Private_Defines
  77. * @{
  78. */
  79. /************************* Miscellaneous Configuration ************************/
  80. /*!< Uncomment the following line if you need to relocate your vector Table in
  81. Internal SRAM. */
  82. /* #define VECT_TAB_SRAM */
  83. #define VECT_TAB_OFFSET 0x00 /*!< Vector Table base offset field.
  84. This value must be a multiple of 0x200. */
  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. /* Reset the RCC clock configuration to the default reset state ------------*/
  135. /* Set HSION bit */
  136. RCC->CR |= (uint32_t)0x00000001;
  137. /* Reset CFGR register */
  138. RCC->CFGR = 0x00000000;
  139. /* Reset HSEON, CSSON and PLLON bits */
  140. RCC->CR &= (uint32_t)0xFEF6FFFF;
  141. /* Reset PLLCFGR register */
  142. RCC->PLLCFGR = 0x24003010;
  143. /* Reset HSEBYP bit */
  144. RCC->CR &= (uint32_t)0xFFFBFFFF;
  145. /* Disable all interrupts */
  146. RCC->CIR = 0x00000000;
  147. /* Configure the Vector Table location add offset address ------------------*/
  148. #ifdef VECT_TAB_SRAM
  149. SCB->VTOR = RAMDTCM_BASE | VECT_TAB_OFFSET; /* Vector Table Relocation in Internal SRAM */
  150. #else
  151. SCB->VTOR = FLASH_BASE | VECT_TAB_OFFSET; /* Vector Table Relocation in Internal FLASH */
  152. #endif
  153. }
  154. /**
  155. * @brief Update SystemCoreClock variable according to Clock Register Values.
  156. * The SystemCoreClock variable contains the core clock (HCLK), it can
  157. * be used by the user application to setup the SysTick timer or configure
  158. * other parameters.
  159. *
  160. * @note Each time the core clock (HCLK) changes, this function must be called
  161. * to update SystemCoreClock variable value. Otherwise, any configuration
  162. * based on this variable will be incorrect.
  163. *
  164. * @note - The system frequency computed by this function is not the real
  165. * frequency in the chip. It is calculated based on the predefined
  166. * constant and the selected clock source:
  167. *
  168. * - If SYSCLK source is HSI, SystemCoreClock will contain the HSI_VALUE(*)
  169. *
  170. * - If SYSCLK source is HSE, SystemCoreClock will contain the HSE_VALUE(**)
  171. *
  172. * - If SYSCLK source is PLL, SystemCoreClock will contain the HSE_VALUE(**)
  173. * or HSI_VALUE(*) multiplied/divided by the PLL factors.
  174. *
  175. * (*) HSI_VALUE is a constant defined in stm32f7xx_hal_conf.h file (default value
  176. * 16 MHz) but the real value may vary depending on the variations
  177. * in voltage and temperature.
  178. *
  179. * (**) HSE_VALUE is a constant defined in stm32f7xx_hal_conf.h file (default value
  180. * 25 MHz), user has to ensure that HSE_VALUE is same as the real
  181. * frequency of the crystal used. Otherwise, this function may
  182. * have wrong result.
  183. *
  184. * - The result of this function could be not correct when using fractional
  185. * value for HSE crystal.
  186. *
  187. * @param None
  188. * @retval None
  189. */
  190. void SystemCoreClockUpdate(void)
  191. {
  192. uint32_t tmp = 0, pllvco = 0, pllp = 2, pllsource = 0, pllm = 2;
  193. /* Get SYSCLK source -------------------------------------------------------*/
  194. tmp = RCC->CFGR & RCC_CFGR_SWS;
  195. switch (tmp)
  196. {
  197. case 0x00: /* HSI used as system clock source */
  198. SystemCoreClock = HSI_VALUE;
  199. break;
  200. case 0x04: /* HSE used as system clock source */
  201. SystemCoreClock = HSE_VALUE;
  202. break;
  203. case 0x08: /* PLL used as system clock source */
  204. /* PLL_VCO = (HSE_VALUE or HSI_VALUE / PLL_M) * PLL_N
  205. SYSCLK = PLL_VCO / PLL_P
  206. */
  207. pllsource = (RCC->PLLCFGR & RCC_PLLCFGR_PLLSRC) >> 22;
  208. pllm = RCC->PLLCFGR & RCC_PLLCFGR_PLLM;
  209. if (pllsource != 0)
  210. {
  211. /* HSE used as PLL clock source */
  212. pllvco = (HSE_VALUE / pllm) * ((RCC->PLLCFGR & RCC_PLLCFGR_PLLN) >> 6);
  213. }
  214. else
  215. {
  216. /* HSI used as PLL clock source */
  217. pllvco = (HSI_VALUE / pllm) * ((RCC->PLLCFGR & RCC_PLLCFGR_PLLN) >> 6);
  218. }
  219. pllp = (((RCC->PLLCFGR & RCC_PLLCFGR_PLLP) >>16) + 1 ) *2;
  220. SystemCoreClock = pllvco/pllp;
  221. break;
  222. default:
  223. SystemCoreClock = HSI_VALUE;
  224. break;
  225. }
  226. /* Compute HCLK frequency --------------------------------------------------*/
  227. /* Get HCLK prescaler */
  228. tmp = AHBPrescTable[((RCC->CFGR & RCC_CFGR_HPRE) >> 4)];
  229. /* HCLK frequency */
  230. SystemCoreClock >>= tmp;
  231. }
  232. /**
  233. * @}
  234. */
  235. /**
  236. * @}
  237. */
  238. /**
  239. * @}
  240. */
  241. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/