system_stm32f3xx.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. /**
  2. ******************************************************************************
  3. * @file system_stm32f3xx.c
  4. * @author MCD Application Team
  5. * @brief CMSIS Cortex-M4 Device Peripheral Access Layer System Source File.
  6. *
  7. * 1. 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_stm32f3xx.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. * 2. After each device reset the HSI (8 MHz) is used as system clock source.
  22. * Then SystemInit() function is called, in "startup_stm32f3xx.s" file, to
  23. * configure the system clock before to branch to main program.
  24. *
  25. * 3. This file configures the system clock as follows:
  26. *=============================================================================
  27. * Supported STM32F3xx device
  28. *-----------------------------------------------------------------------------
  29. * System Clock source | HSI
  30. *-----------------------------------------------------------------------------
  31. * SYSCLK(Hz) | 8000000
  32. *-----------------------------------------------------------------------------
  33. * HCLK(Hz) | 8000000
  34. *-----------------------------------------------------------------------------
  35. * AHB Prescaler | 1
  36. *-----------------------------------------------------------------------------
  37. * APB2 Prescaler | 1
  38. *-----------------------------------------------------------------------------
  39. * APB1 Prescaler | 1
  40. *-----------------------------------------------------------------------------
  41. * USB Clock | DISABLE
  42. *-----------------------------------------------------------------------------
  43. *=============================================================================
  44. ******************************************************************************
  45. * @attention
  46. *
  47. * <h2><center>&copy; COPYRIGHT(c) 2016 STMicroelectronics</center></h2>
  48. *
  49. * Redistribution and use in source and binary forms, with or without modification,
  50. * are permitted provided that the following conditions are met:
  51. * 1. Redistributions of source code must retain the above copyright notice,
  52. * this list of conditions and the following disclaimer.
  53. * 2. Redistributions in binary form must reproduce the above copyright notice,
  54. * this list of conditions and the following disclaimer in the documentation
  55. * and/or other materials provided with the distribution.
  56. * 3. Neither the name of STMicroelectronics nor the names of its contributors
  57. * may be used to endorse or promote products derived from this software
  58. * without specific prior written permission.
  59. *
  60. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  61. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  62. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  63. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  64. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  65. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  66. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  67. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  68. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  69. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  70. *
  71. ******************************************************************************
  72. */
  73. /** @addtogroup CMSIS
  74. * @{
  75. */
  76. /** @addtogroup stm32f3xx_system
  77. * @{
  78. */
  79. /** @addtogroup STM32F3xx_System_Private_Includes
  80. * @{
  81. */
  82. #include "stm32f3xx.h"
  83. /**
  84. * @}
  85. */
  86. /** @addtogroup STM32F3xx_System_Private_TypesDefinitions
  87. * @{
  88. */
  89. /**
  90. * @}
  91. */
  92. /** @addtogroup STM32F3xx_System_Private_Defines
  93. * @{
  94. */
  95. #if !defined (HSE_VALUE)
  96. #define HSE_VALUE ((uint32_t)8000000) /*!< Default value of the External oscillator in Hz.
  97. This value can be provided and adapted by the user application. */
  98. #endif /* HSE_VALUE */
  99. #if !defined (HSI_VALUE)
  100. #define HSI_VALUE ((uint32_t)8000000) /*!< Default value of the Internal oscillator in Hz.
  101. This value can be provided and adapted by the user application. */
  102. #endif /* HSI_VALUE */
  103. /*!< Uncomment the following line if you need to relocate your vector Table in
  104. Internal SRAM. */
  105. /* #define VECT_TAB_SRAM */
  106. #define VECT_TAB_OFFSET 0x0 /*!< Vector Table base offset field.
  107. This value must be a multiple of 0x200. */
  108. /**
  109. * @}
  110. */
  111. /** @addtogroup STM32F3xx_System_Private_Macros
  112. * @{
  113. */
  114. /**
  115. * @}
  116. */
  117. /** @addtogroup STM32F3xx_System_Private_Variables
  118. * @{
  119. */
  120. /* This variable is updated in three ways:
  121. 1) by calling CMSIS function SystemCoreClockUpdate()
  122. 2) by calling HAL API function HAL_RCC_GetHCLKFreq()
  123. 3) each time HAL_RCC_ClockConfig() is called to configure the system clock frequency
  124. Note: If you use this function to configure the system clock there is no need to
  125. call the 2 first functions listed above, since SystemCoreClock variable is
  126. updated automatically.
  127. */
  128. uint32_t SystemCoreClock = 8000000;
  129. const uint8_t AHBPrescTable[16] = {0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 4, 6, 7, 8, 9};
  130. const uint8_t APBPrescTable[8] = {0, 0, 0, 0, 1, 2, 3, 4};
  131. /**
  132. * @}
  133. */
  134. /** @addtogroup STM32F3xx_System_Private_FunctionPrototypes
  135. * @{
  136. */
  137. /**
  138. * @}
  139. */
  140. /** @addtogroup STM32F3xx_System_Private_Functions
  141. * @{
  142. */
  143. /**
  144. * @brief Setup the microcontroller system
  145. * Initialize the FPU setting, vector table location and the PLL configuration is reset.
  146. * @param None
  147. * @retval None
  148. */
  149. void SystemInit(void)
  150. {
  151. /* FPU settings ------------------------------------------------------------*/
  152. #if (__FPU_PRESENT == 1) && (__FPU_USED == 1)
  153. SCB->CPACR |= ((3UL << 10*2)|(3UL << 11*2)); /* set CP10 and CP11 Full Access */
  154. #endif
  155. /* Reset the RCC clock configuration to the default reset state ------------*/
  156. /* Set HSION bit */
  157. RCC->CR |= 0x00000001U;
  158. /* Reset CFGR register */
  159. RCC->CFGR &= 0xF87FC00CU;
  160. /* Reset HSEON, CSSON and PLLON bits */
  161. RCC->CR &= 0xFEF6FFFFU;
  162. /* Reset HSEBYP bit */
  163. RCC->CR &= 0xFFFBFFFFU;
  164. /* Reset PLLSRC, PLLXTPRE, PLLMUL and USBPRE bits */
  165. RCC->CFGR &= 0xFF80FFFFU;
  166. /* Reset PREDIV1[3:0] bits */
  167. RCC->CFGR2 &= 0xFFFFFFF0U;
  168. /* Reset USARTSW[1:0], I2CSW and TIMs bits */
  169. RCC->CFGR3 &= 0xFF00FCCCU;
  170. /* Disable all interrupts */
  171. RCC->CIR = 0x00000000U;
  172. #ifdef VECT_TAB_SRAM
  173. SCB->VTOR = SRAM_BASE | VECT_TAB_OFFSET; /* Vector Table Relocation in Internal SRAM */
  174. #else
  175. SCB->VTOR = FLASH_BASE | VECT_TAB_OFFSET; /* Vector Table Relocation in Internal FLASH */
  176. #endif
  177. }
  178. /**
  179. * @brief Update SystemCoreClock variable according to Clock Register Values.
  180. * The SystemCoreClock variable contains the core clock (HCLK), it can
  181. * be used by the user application to setup the SysTick timer or configure
  182. * other parameters.
  183. *
  184. * @note Each time the core clock (HCLK) changes, this function must be called
  185. * to update SystemCoreClock variable value. Otherwise, any configuration
  186. * based on this variable will be incorrect.
  187. *
  188. * @note - The system frequency computed by this function is not the real
  189. * frequency in the chip. It is calculated based on the predefined
  190. * constant and the selected clock source:
  191. *
  192. * - If SYSCLK source is HSI, SystemCoreClock will contain the HSI_VALUE(*)
  193. *
  194. * - If SYSCLK source is HSE, SystemCoreClock will contain the HSE_VALUE(**)
  195. *
  196. * - If SYSCLK source is PLL, SystemCoreClock will contain the HSE_VALUE(**)
  197. * or HSI_VALUE(*) multiplied/divided by the PLL factors.
  198. *
  199. * (*) HSI_VALUE is a constant defined in stm32f3xx_hal.h file (default value
  200. * 8 MHz) but the real value may vary depending on the variations
  201. * in voltage and temperature.
  202. *
  203. * (**) HSE_VALUE is a constant defined in stm32f3xx_hal.h file (default value
  204. * 8 MHz), user has to ensure that HSE_VALUE is same as the real
  205. * frequency of the crystal used. Otherwise, this function may
  206. * have wrong result.
  207. *
  208. * - The result of this function could be not correct when using fractional
  209. * value for HSE crystal.
  210. *
  211. * @param None
  212. * @retval None
  213. */
  214. void SystemCoreClockUpdate (void)
  215. {
  216. uint32_t tmp = 0, pllmull = 0, pllsource = 0, predivfactor = 0;
  217. /* Get SYSCLK source -------------------------------------------------------*/
  218. tmp = RCC->CFGR & RCC_CFGR_SWS;
  219. switch (tmp)
  220. {
  221. case RCC_CFGR_SWS_HSI: /* HSI used as system clock */
  222. SystemCoreClock = HSI_VALUE;
  223. break;
  224. case RCC_CFGR_SWS_HSE: /* HSE used as system clock */
  225. SystemCoreClock = HSE_VALUE;
  226. break;
  227. case RCC_CFGR_SWS_PLL: /* PLL used as system clock */
  228. /* Get PLL clock source and multiplication factor ----------------------*/
  229. pllmull = RCC->CFGR & RCC_CFGR_PLLMUL;
  230. pllsource = RCC->CFGR & RCC_CFGR_PLLSRC;
  231. pllmull = ( pllmull >> 18) + 2;
  232. #if defined (STM32F302xE) || defined (STM32F303xE) || defined (STM32F398xx)
  233. predivfactor = (RCC->CFGR2 & RCC_CFGR2_PREDIV) + 1;
  234. if (pllsource == RCC_CFGR_PLLSRC_HSE_PREDIV)
  235. {
  236. /* HSE oscillator clock selected as PREDIV1 clock entry */
  237. SystemCoreClock = (HSE_VALUE / predivfactor) * pllmull;
  238. }
  239. else
  240. {
  241. /* HSI oscillator clock selected as PREDIV1 clock entry */
  242. SystemCoreClock = (HSI_VALUE / predivfactor) * pllmull;
  243. }
  244. #else
  245. if (pllsource == RCC_CFGR_PLLSRC_HSI_DIV2)
  246. {
  247. /* HSI oscillator clock divided by 2 selected as PLL clock entry */
  248. SystemCoreClock = (HSI_VALUE >> 1) * pllmull;
  249. }
  250. else
  251. {
  252. predivfactor = (RCC->CFGR2 & RCC_CFGR2_PREDIV) + 1;
  253. /* HSE oscillator clock selected as PREDIV1 clock entry */
  254. SystemCoreClock = (HSE_VALUE / predivfactor) * pllmull;
  255. }
  256. #endif /* STM32F302xE || STM32F303xE || STM32F398xx */
  257. break;
  258. default: /* HSI used as system clock */
  259. SystemCoreClock = HSI_VALUE;
  260. break;
  261. }
  262. /* Compute HCLK clock frequency ----------------*/
  263. /* Get HCLK prescaler */
  264. tmp = AHBPrescTable[((RCC->CFGR & RCC_CFGR_HPRE) >> 4)];
  265. /* HCLK clock frequency */
  266. SystemCoreClock >>= tmp;
  267. }
  268. /**
  269. * @}
  270. */
  271. /**
  272. * @}
  273. */
  274. /**
  275. * @}
  276. */
  277. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/