You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

281 lines
9.2 KiB

  1. /**
  2. ******************************************************************************
  3. * @file system_stm32f7xx.c
  4. * @author MCD Application Team
  5. * @version V1.2.0
  6. * @date 30-December-2016
  7. * @brief CMSIS Cortex-M7 Device Peripheral Access Layer System Source File.
  8. *
  9. * This file provides two functions and one global variable to be called from
  10. * user application:
  11. * - SystemInit(): This function is called at startup just after reset and
  12. * before branch to main program. This call is made inside
  13. * the "startup_stm32f7xx.s" file.
  14. *
  15. * - SystemCoreClock variable: Contains the core clock (HCLK), it can be used
  16. * by the user application to setup the SysTick
  17. * timer or configure other parameters.
  18. *
  19. * - SystemCoreClockUpdate(): Updates the variable SystemCoreClock and must
  20. * be called whenever the core clock is changed
  21. * during program execution.
  22. *
  23. *
  24. ******************************************************************************
  25. * @attention
  26. *
  27. * <h2><center>&copy; COPYRIGHT 2016 STMicroelectronics</center></h2>
  28. *
  29. * Redistribution and use in source and binary forms, with or without modification,
  30. * are permitted provided that the following conditions are met:
  31. * 1. Redistributions of source code must retain the above copyright notice,
  32. * this list of conditions and the following disclaimer.
  33. * 2. Redistributions in binary form must reproduce the above copyright notice,
  34. * this list of conditions and the following disclaimer in the documentation
  35. * and/or other materials provided with the distribution.
  36. * 3. Neither the name of STMicroelectronics nor the names of its contributors
  37. * may be used to endorse or promote products derived from this software
  38. * without specific prior written permission.
  39. *
  40. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  41. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  42. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  43. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  44. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  45. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  46. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  47. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  48. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  49. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  50. *
  51. ******************************************************************************
  52. */
  53. /** @addtogroup CMSIS
  54. * @{
  55. */
  56. /** @addtogroup stm32f7xx_system
  57. * @{
  58. */
  59. /** @addtogroup STM32F7xx_System_Private_Includes
  60. * @{
  61. */
  62. #include "stm32f7xx.h"
  63. #if !defined (HSE_VALUE)
  64. #define HSE_VALUE ((uint32_t)25000000) /*!< Default value of the External oscillator in Hz */
  65. #endif /* HSE_VALUE */
  66. #if !defined (HSI_VALUE)
  67. #define HSI_VALUE ((uint32_t)16000000) /*!< Value of the Internal oscillator in Hz*/
  68. #endif /* HSI_VALUE */
  69. /**
  70. * @}
  71. */
  72. /** @addtogroup STM32F7xx_System_Private_TypesDefinitions
  73. * @{
  74. */
  75. /**
  76. * @}
  77. */
  78. /** @addtogroup STM32F7xx_System_Private_Defines
  79. * @{
  80. */
  81. /************************* Miscellaneous Configuration ************************/
  82. /*!< Uncomment the following line if you need to relocate your vector Table in
  83. Internal SRAM. */
  84. /* #define VECT_TAB_SRAM */
  85. #define VECT_TAB_OFFSET 0x00 /*!< Vector Table base offset field.
  86. This value must be a multiple of 0x200. */
  87. /******************************************************************************/
  88. /**
  89. * @}
  90. */
  91. /** @addtogroup STM32F7xx_System_Private_Macros
  92. * @{
  93. */
  94. /**
  95. * @}
  96. */
  97. /** @addtogroup STM32F7xx_System_Private_Variables
  98. * @{
  99. */
  100. /* This variable is updated in three ways:
  101. 1) by calling CMSIS function SystemCoreClockUpdate()
  102. 2) by calling HAL API function HAL_RCC_GetHCLKFreq()
  103. 3) each time HAL_RCC_ClockConfig() is called to configure the system clock frequency
  104. Note: If you use this function to configure the system clock; then there
  105. is no need to call the 2 first functions listed above, since SystemCoreClock
  106. variable is updated automatically.
  107. */
  108. uint32_t SystemCoreClock = 16000000;
  109. const uint8_t AHBPrescTable[16] = {0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 4, 6, 7, 8, 9};
  110. const uint8_t APBPrescTable[8] = {0, 0, 0, 0, 1, 2, 3, 4};
  111. /**
  112. * @}
  113. */
  114. /** @addtogroup STM32F7xx_System_Private_FunctionPrototypes
  115. * @{
  116. */
  117. /**
  118. * @}
  119. */
  120. /** @addtogroup STM32F7xx_System_Private_Functions
  121. * @{
  122. */
  123. /**
  124. * @brief Setup the microcontroller system
  125. * Initialize the Embedded Flash Interface, the PLL and update the
  126. * SystemFrequency variable.
  127. * @param None
  128. * @retval None
  129. */
  130. void SystemInit(void)
  131. {
  132. /* FPU settings ------------------------------------------------------------*/
  133. #if (__FPU_PRESENT == 1) && (__FPU_USED == 1)
  134. SCB->CPACR |= ((3UL << 10*2)|(3UL << 11*2)); /* set CP10 and CP11 Full Access */
  135. #endif
  136. /* Reset the RCC clock configuration to the default reset state ------------*/
  137. /* Set HSION bit */
  138. RCC->CR |= (uint32_t)0x00000001;
  139. /* Reset CFGR register */
  140. RCC->CFGR = 0x00000000;
  141. /* Reset HSEON, CSSON and PLLON bits */
  142. RCC->CR &= (uint32_t)0xFEF6FFFF;
  143. /* Reset PLLCFGR register */
  144. RCC->PLLCFGR = 0x24003010;
  145. /* Reset HSEBYP bit */
  146. RCC->CR &= (uint32_t)0xFFFBFFFF;
  147. /* Disable all interrupts */
  148. RCC->CIR = 0x00000000;
  149. /* Configure the Vector Table location add offset address ------------------*/
  150. #ifdef VECT_TAB_SRAM
  151. SCB->VTOR = RAMDTCM_BASE | VECT_TAB_OFFSET; /* Vector Table Relocation in Internal SRAM */
  152. #else
  153. SCB->VTOR = FLASH_BASE | VECT_TAB_OFFSET; /* Vector Table Relocation in Internal FLASH */
  154. #endif
  155. }
  156. /**
  157. * @brief Update SystemCoreClock variable according to Clock Register Values.
  158. * The SystemCoreClock variable contains the core clock (HCLK), it can
  159. * be used by the user application to setup the SysTick timer or configure
  160. * other parameters.
  161. *
  162. * @note Each time the core clock (HCLK) changes, this function must be called
  163. * to update SystemCoreClock variable value. Otherwise, any configuration
  164. * based on this variable will be incorrect.
  165. *
  166. * @note - The system frequency computed by this function is not the real
  167. * frequency in the chip. It is calculated based on the predefined
  168. * constant and the selected clock source:
  169. *
  170. * - If SYSCLK source is HSI, SystemCoreClock will contain the HSI_VALUE(*)
  171. *
  172. * - If SYSCLK source is HSE, SystemCoreClock will contain the HSE_VALUE(**)
  173. *
  174. * - If SYSCLK source is PLL, SystemCoreClock will contain the HSE_VALUE(**)
  175. * or HSI_VALUE(*) multiplied/divided by the PLL factors.
  176. *
  177. * (*) HSI_VALUE is a constant defined in stm32f7xx_hal_conf.h file (default value
  178. * 16 MHz) but the real value may vary depending on the variations
  179. * in voltage and temperature.
  180. *
  181. * (**) HSE_VALUE is a constant defined in stm32f7xx_hal_conf.h file (default value
  182. * 25 MHz), user has to ensure that HSE_VALUE is same as the real
  183. * frequency of the crystal used. Otherwise, this function may
  184. * have wrong result.
  185. *
  186. * - The result of this function could be not correct when using fractional
  187. * value for HSE crystal.
  188. *
  189. * @param None
  190. * @retval None
  191. */
  192. void SystemCoreClockUpdate(void)
  193. {
  194. uint32_t tmp = 0, pllvco = 0, pllp = 2, pllsource = 0, pllm = 2;
  195. /* Get SYSCLK source -------------------------------------------------------*/
  196. tmp = RCC->CFGR & RCC_CFGR_SWS;
  197. switch (tmp)
  198. {
  199. case 0x00: /* HSI used as system clock source */
  200. SystemCoreClock = HSI_VALUE;
  201. break;
  202. case 0x04: /* HSE used as system clock source */
  203. SystemCoreClock = HSE_VALUE;
  204. break;
  205. case 0x08: /* PLL used as system clock source */
  206. /* PLL_VCO = (HSE_VALUE or HSI_VALUE / PLL_M) * PLL_N
  207. SYSCLK = PLL_VCO / PLL_P
  208. */
  209. pllsource = (RCC->PLLCFGR & RCC_PLLCFGR_PLLSRC) >> 22;
  210. pllm = RCC->PLLCFGR & RCC_PLLCFGR_PLLM;
  211. if (pllsource != 0)
  212. {
  213. /* HSE used as PLL clock source */
  214. pllvco = (HSE_VALUE / pllm) * ((RCC->PLLCFGR & RCC_PLLCFGR_PLLN) >> 6);
  215. }
  216. else
  217. {
  218. /* HSI used as PLL clock source */
  219. pllvco = (HSI_VALUE / pllm) * ((RCC->PLLCFGR & RCC_PLLCFGR_PLLN) >> 6);
  220. }
  221. pllp = (((RCC->PLLCFGR & RCC_PLLCFGR_PLLP) >>16) + 1 ) *2;
  222. SystemCoreClock = pllvco/pllp;
  223. break;
  224. default:
  225. SystemCoreClock = HSI_VALUE;
  226. break;
  227. }
  228. /* Compute HCLK frequency --------------------------------------------------*/
  229. /* Get HCLK prescaler */
  230. tmp = AHBPrescTable[((RCC->CFGR & RCC_CFGR_HPRE) >> 4)];
  231. /* HCLK frequency */
  232. SystemCoreClock >>= tmp;
  233. }
  234. /**
  235. * @}
  236. */
  237. /**
  238. * @}
  239. */
  240. /**
  241. * @}
  242. */
  243. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/