system_stm32f0xx.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  1. /**
  2. ******************************************************************************
  3. * @file system_stm32f0xx.c
  4. * @author MCD Application Team
  5. * @brief CMSIS Cortex-M0 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_stm32f0xx.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_stm32f0xx.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 STM32F0xx device
  28. *-----------------------------------------------------------------------------
  29. * System Clock source | HSI
  30. *-----------------------------------------------------------------------------
  31. * SYSCLK(Hz) | 8000000
  32. *-----------------------------------------------------------------------------
  33. * HCLK(Hz) | 8000000
  34. *-----------------------------------------------------------------------------
  35. * AHB Prescaler | 1
  36. *-----------------------------------------------------------------------------
  37. * APB1 Prescaler | 1
  38. *-----------------------------------------------------------------------------
  39. *=============================================================================
  40. ******************************************************************************
  41. * @attention
  42. *
  43. * <h2><center>&copy; COPYRIGHT(c) 2016 STMicroelectronics</center></h2>
  44. *
  45. * Redistribution and use in source and binary forms, with or without modification,
  46. * are permitted provided that the following conditions are met:
  47. * 1. Redistributions of source code must retain the above copyright notice,
  48. * this list of conditions and the following disclaimer.
  49. * 2. Redistributions in binary form must reproduce the above copyright notice,
  50. * this list of conditions and the following disclaimer in the documentation
  51. * and/or other materials provided with the distribution.
  52. * 3. Neither the name of STMicroelectronics nor the names of its contributors
  53. * may be used to endorse or promote products derived from this software
  54. * without specific prior written permission.
  55. *
  56. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  57. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  58. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  59. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  60. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  61. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  62. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  63. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  64. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  65. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  66. *
  67. ******************************************************************************
  68. */
  69. /** @addtogroup CMSIS
  70. * @{
  71. */
  72. /** @addtogroup stm32f0xx_system
  73. * @{
  74. */
  75. /** @addtogroup STM32F0xx_System_Private_Includes
  76. * @{
  77. */
  78. #include "stm32f0xx.h"
  79. /**
  80. * @}
  81. */
  82. /** @addtogroup STM32F0xx_System_Private_TypesDefinitions
  83. * @{
  84. */
  85. /**
  86. * @}
  87. */
  88. /** @addtogroup STM32F0xx_System_Private_Defines
  89. * @{
  90. */
  91. #if !defined (HSE_VALUE)
  92. #define HSE_VALUE ((uint32_t)8000000) /*!< Default value of the External oscillator in Hz.
  93. This value can be provided and adapted by the user application. */
  94. #endif /* HSE_VALUE */
  95. #if !defined (HSI_VALUE)
  96. #define HSI_VALUE ((uint32_t)8000000) /*!< Default value of the Internal oscillator in Hz.
  97. This value can be provided and adapted by the user application. */
  98. #endif /* HSI_VALUE */
  99. #if !defined (HSI48_VALUE)
  100. #define HSI48_VALUE ((uint32_t)48000000) /*!< Default value of the HSI48 Internal oscillator in Hz.
  101. This value can be provided and adapted by the user application. */
  102. #endif /* HSI48_VALUE */
  103. /**
  104. * @}
  105. */
  106. /** @addtogroup STM32F0xx_System_Private_Macros
  107. * @{
  108. */
  109. /**
  110. * @}
  111. */
  112. /** @addtogroup STM32F0xx_System_Private_Variables
  113. * @{
  114. */
  115. /* This variable is updated in three ways:
  116. 1) by calling CMSIS function SystemCoreClockUpdate()
  117. 2) by calling HAL API function HAL_RCC_GetHCLKFreq()
  118. 3) each time HAL_RCC_ClockConfig() is called to configure the system clock frequency
  119. Note: If you use this function to configure the system clock there is no need to
  120. call the 2 first functions listed above, since SystemCoreClock variable is
  121. updated automatically.
  122. */
  123. uint32_t SystemCoreClock = 8000000;
  124. const uint8_t AHBPrescTable[16] = {0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 4, 6, 7, 8, 9};
  125. const uint8_t APBPrescTable[8] = {0, 0, 0, 0, 1, 2, 3, 4};
  126. /**
  127. * @}
  128. */
  129. /** @addtogroup STM32F0xx_System_Private_FunctionPrototypes
  130. * @{
  131. */
  132. /**
  133. * @}
  134. */
  135. /** @addtogroup STM32F0xx_System_Private_Functions
  136. * @{
  137. */
  138. /**
  139. * @brief Setup the microcontroller system.
  140. * Initialize the default HSI clock source, vector table location and the PLL configuration is reset.
  141. * @param None
  142. * @retval None
  143. */
  144. void SystemInit(void)
  145. {
  146. /* Reset the RCC clock configuration to the default reset state ------------*/
  147. /* Set HSION bit */
  148. RCC->CR |= (uint32_t)0x00000001U;
  149. #if defined (STM32F051x8) || defined (STM32F058x8)
  150. /* Reset SW[1:0], HPRE[3:0], PPRE[2:0], ADCPRE and MCOSEL[2:0] bits */
  151. RCC->CFGR &= (uint32_t)0xF8FFB80CU;
  152. #else
  153. /* Reset SW[1:0], HPRE[3:0], PPRE[2:0], ADCPRE, MCOSEL[2:0], MCOPRE[2:0] and PLLNODIV bits */
  154. RCC->CFGR &= (uint32_t)0x08FFB80CU;
  155. #endif /* STM32F051x8 or STM32F058x8 */
  156. /* Reset HSEON, CSSON and PLLON bits */
  157. RCC->CR &= (uint32_t)0xFEF6FFFFU;
  158. /* Reset HSEBYP bit */
  159. RCC->CR &= (uint32_t)0xFFFBFFFFU;
  160. /* Reset PLLSRC, PLLXTPRE and PLLMUL[3:0] bits */
  161. RCC->CFGR &= (uint32_t)0xFFC0FFFFU;
  162. /* Reset PREDIV[3:0] bits */
  163. RCC->CFGR2 &= (uint32_t)0xFFFFFFF0U;
  164. #if defined (STM32F072xB) || defined (STM32F078xx)
  165. /* Reset USART2SW[1:0], USART1SW[1:0], I2C1SW, CECSW, USBSW and ADCSW bits */
  166. RCC->CFGR3 &= (uint32_t)0xFFFCFE2CU;
  167. #elif defined (STM32F071xB)
  168. /* Reset USART2SW[1:0], USART1SW[1:0], I2C1SW, CECSW and ADCSW bits */
  169. RCC->CFGR3 &= (uint32_t)0xFFFFCEACU;
  170. #elif defined (STM32F091xC) || defined (STM32F098xx)
  171. /* Reset USART3SW[1:0], USART2SW[1:0], USART1SW[1:0], I2C1SW, CECSW and ADCSW bits */
  172. RCC->CFGR3 &= (uint32_t)0xFFF0FEACU;
  173. #elif defined (STM32F030x6) || defined (STM32F030x8) || defined (STM32F031x6) || defined (STM32F038xx) || defined (STM32F030xC)
  174. /* Reset USART1SW[1:0], I2C1SW and ADCSW bits */
  175. RCC->CFGR3 &= (uint32_t)0xFFFFFEECU;
  176. #elif defined (STM32F051x8) || defined (STM32F058xx)
  177. /* Reset USART1SW[1:0], I2C1SW, CECSW and ADCSW bits */
  178. RCC->CFGR3 &= (uint32_t)0xFFFFFEACU;
  179. #elif defined (STM32F042x6) || defined (STM32F048xx)
  180. /* Reset USART1SW[1:0], I2C1SW, CECSW, USBSW and ADCSW bits */
  181. RCC->CFGR3 &= (uint32_t)0xFFFFFE2CU;
  182. #elif defined (STM32F070x6) || defined (STM32F070xB)
  183. /* Reset USART1SW[1:0], I2C1SW, USBSW and ADCSW bits */
  184. RCC->CFGR3 &= (uint32_t)0xFFFFFE6CU;
  185. /* Set default USB clock to PLLCLK, since there is no HSI48 */
  186. RCC->CFGR3 |= (uint32_t)0x00000080U;
  187. #else
  188. #warning "No target selected"
  189. #endif
  190. /* Reset HSI14 bit */
  191. RCC->CR2 &= (uint32_t)0xFFFFFFFEU;
  192. /* Disable all interrupts */
  193. RCC->CIR = 0x00000000U;
  194. }
  195. /**
  196. * @brief Update SystemCoreClock variable according to Clock Register Values.
  197. * The SystemCoreClock variable contains the core clock (HCLK), it can
  198. * be used by the user application to setup the SysTick timer or configure
  199. * other parameters.
  200. *
  201. * @note Each time the core clock (HCLK) changes, this function must be called
  202. * to update SystemCoreClock variable value. Otherwise, any configuration
  203. * based on this variable will be incorrect.
  204. *
  205. * @note - The system frequency computed by this function is not the real
  206. * frequency in the chip. It is calculated based on the predefined
  207. * constant and the selected clock source:
  208. *
  209. * - If SYSCLK source is HSI, SystemCoreClock will contain the HSI_VALUE(*)
  210. *
  211. * - If SYSCLK source is HSE, SystemCoreClock will contain the HSE_VALUE(**)
  212. *
  213. * - If SYSCLK source is PLL, SystemCoreClock will contain the HSE_VALUE(**)
  214. * or HSI_VALUE(*) multiplied/divided by the PLL factors.
  215. *
  216. * (*) HSI_VALUE is a constant defined in stm32f0xx_hal.h file (default value
  217. * 8 MHz) but the real value may vary depending on the variations
  218. * in voltage and temperature.
  219. *
  220. * (**) HSE_VALUE is a constant defined in stm32f0xx_hal.h file (default value
  221. * 8 MHz), user has to ensure that HSE_VALUE is same as the real
  222. * frequency of the crystal used. Otherwise, this function may
  223. * have wrong result.
  224. *
  225. * - The result of this function could be not correct when using fractional
  226. * value for HSE crystal.
  227. *
  228. * @param None
  229. * @retval None
  230. */
  231. void SystemCoreClockUpdate (void)
  232. {
  233. uint32_t tmp = 0, pllmull = 0, pllsource = 0, predivfactor = 0;
  234. /* Get SYSCLK source -------------------------------------------------------*/
  235. tmp = RCC->CFGR & RCC_CFGR_SWS;
  236. switch (tmp)
  237. {
  238. case RCC_CFGR_SWS_HSI: /* HSI used as system clock */
  239. SystemCoreClock = HSI_VALUE;
  240. break;
  241. case RCC_CFGR_SWS_HSE: /* HSE used as system clock */
  242. SystemCoreClock = HSE_VALUE;
  243. break;
  244. case RCC_CFGR_SWS_PLL: /* PLL used as system clock */
  245. /* Get PLL clock source and multiplication factor ----------------------*/
  246. pllmull = RCC->CFGR & RCC_CFGR_PLLMUL;
  247. pllsource = RCC->CFGR & RCC_CFGR_PLLSRC;
  248. pllmull = ( pllmull >> 18) + 2;
  249. predivfactor = (RCC->CFGR2 & RCC_CFGR2_PREDIV) + 1;
  250. if (pllsource == RCC_CFGR_PLLSRC_HSE_PREDIV)
  251. {
  252. /* HSE used as PLL clock source : SystemCoreClock = HSE/PREDIV * PLLMUL */
  253. SystemCoreClock = (HSE_VALUE/predivfactor) * pllmull;
  254. }
  255. #if defined(STM32F042x6) || defined(STM32F048xx) || defined(STM32F072xB) || defined(STM32F078xx) || defined(STM32F091xC) || defined(STM32F098xx)
  256. else if (pllsource == RCC_CFGR_PLLSRC_HSI48_PREDIV)
  257. {
  258. /* HSI48 used as PLL clock source : SystemCoreClock = HSI48/PREDIV * PLLMUL */
  259. SystemCoreClock = (HSI48_VALUE/predivfactor) * pllmull;
  260. }
  261. #endif /* STM32F042x6 || STM32F048xx || STM32F072xB || STM32F078xx || STM32F091xC || STM32F098xx */
  262. else
  263. {
  264. #if defined(STM32F042x6) || defined(STM32F048xx) || defined(STM32F070x6) \
  265. || defined(STM32F078xx) || defined(STM32F071xB) || defined(STM32F072xB) \
  266. || defined(STM32F070xB) || defined(STM32F091xC) || defined(STM32F098xx) || defined(STM32F030xC)
  267. /* HSI used as PLL clock source : SystemCoreClock = HSI/PREDIV * PLLMUL */
  268. SystemCoreClock = (HSI_VALUE/predivfactor) * pllmull;
  269. #else
  270. /* HSI used as PLL clock source : SystemCoreClock = HSI/2 * PLLMUL */
  271. SystemCoreClock = (HSI_VALUE >> 1) * pllmull;
  272. #endif /* STM32F042x6 || STM32F048xx || STM32F070x6 ||
  273. STM32F071xB || STM32F072xB || STM32F078xx || STM32F070xB ||
  274. STM32F091xC || STM32F098xx || STM32F030xC */
  275. }
  276. break;
  277. default: /* HSI used as system clock */
  278. SystemCoreClock = HSI_VALUE;
  279. break;
  280. }
  281. /* Compute HCLK clock frequency ----------------*/
  282. /* Get HCLK prescaler */
  283. tmp = AHBPrescTable[((RCC->CFGR & RCC_CFGR_HPRE) >> 4)];
  284. /* HCLK clock frequency */
  285. SystemCoreClock >>= tmp;
  286. }
  287. /**
  288. * @}
  289. */
  290. /**
  291. * @}
  292. */
  293. /**
  294. * @}
  295. */
  296. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/