system_stm32f0xx.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  1. /**
  2. ******************************************************************************
  3. * @file system_stm32f0xx.c
  4. * @author MCD Application Team
  5. * @version V1.5.0
  6. * @date 05-December-2014
  7. * @brief CMSIS Cortex-M0 Device Peripheral Access Layer System Source File.
  8. * This file contains the system clock configuration for STM32F0xx devices,
  9. * and is generated by the clock configuration tool
  10. * STM32F0xx_Clock_Configuration_V1.0.1.xls
  11. *
  12. * 1. This file provides two functions and one global variable to be called from
  13. * user application:
  14. * - SystemInit(): Setups the system clock (System clock source, PLL Multiplier
  15. * and Divider factors, AHB/APBx prescalers and Flash settings),
  16. * depending on the configuration made in the clock xls tool.
  17. * This function is called at startup just after reset and
  18. * before branch to main program. This call is made inside
  19. * the "startup_stm32f0xx.s" file.
  20. *
  21. * - SystemCoreClock variable: Contains the core clock (HCLK), it can be used
  22. * by the user application to setup the SysTick
  23. * timer or configure other parameters.
  24. *
  25. * - SystemCoreClockUpdate(): Updates the variable SystemCoreClock and must
  26. * be called whenever the core clock is changed
  27. * during program execution.
  28. *
  29. * 2. After each device reset the HSI (8 MHz Range) is used as system clock source.
  30. * Then SystemInit() function is called, in "startup_stm32f0xx.s" file, to
  31. * configure the system clock before to branch to main program.
  32. *
  33. * 3. If the system clock source selected by user fails to startup, the SystemInit()
  34. * function will do nothing and HSI still used as system clock source. User can
  35. * add some code to deal with this issue inside the SetSysClock() function.
  36. *
  37. * 4. The default value of HSE crystal is set to 8MHz, refer to "HSE_VALUE" define
  38. * in "stm32f0xx.h" file. When HSE is used as system clock source, directly or
  39. * through PLL, and you are using different crystal you have to adapt the HSE
  40. * value to your own configuration.
  41. *
  42. * 5. This file configures the system clock as follows:
  43. *=============================================================================
  44. *=============================================================================
  45. * System Clock source | PLL(HSE)
  46. *-----------------------------------------------------------------------------
  47. * SYSCLK(Hz) | 48000000
  48. *-----------------------------------------------------------------------------
  49. * HCLK(Hz) | 48000000
  50. *-----------------------------------------------------------------------------
  51. * AHB Prescaler | 1
  52. *-----------------------------------------------------------------------------
  53. * APB Prescaler | 1
  54. *-----------------------------------------------------------------------------
  55. * HSE Frequency(Hz) | 8000000
  56. *----------------------------------------------------------------------------
  57. * PLLMUL | 6
  58. *-----------------------------------------------------------------------------
  59. * PREDIV | 1
  60. *-----------------------------------------------------------------------------
  61. * Flash Latency(WS) | 1
  62. *-----------------------------------------------------------------------------
  63. * Prefetch Buffer | ON
  64. *-----------------------------------------------------------------------------
  65. ******************************************************************************
  66. * @attention
  67. *
  68. * <h2><center>&copy; COPYRIGHT 2014 STMicroelectronics</center></h2>
  69. *
  70. * Licensed under MCD-ST Liberty SW License Agreement V2, (the "License");
  71. * You may not use this file except in compliance with the License.
  72. * You may obtain a copy of the License at:
  73. *
  74. * http://www.st.com/software_license_agreement_liberty_v2
  75. *
  76. * Unless required by applicable law or agreed to in writing, software
  77. * distributed under the License is distributed on an "AS IS" BASIS,
  78. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  79. * See the License for the specific language governing permissions and
  80. * limitations under the License.
  81. *
  82. ******************************************************************************
  83. */
  84. /** @addtogroup CMSIS
  85. * @{
  86. */
  87. /** @addtogroup stm32f0xx_system
  88. * @{
  89. */
  90. /** @addtogroup STM32F0xx_System_Private_Includes
  91. * @{
  92. */
  93. #include "stm32f0xx.h"
  94. /**
  95. * @}
  96. */
  97. /** @addtogroup STM32F0xx_System_Private_TypesDefinitions
  98. * @{
  99. */
  100. /**
  101. * @}
  102. */
  103. /** @addtogroup STM32F0xx_System_Private_Defines
  104. * @{
  105. */
  106. /**
  107. * @}
  108. */
  109. /** @addtogroup STM32F0xx_System_Private_Macros
  110. * @{
  111. */
  112. /**
  113. * @}
  114. */
  115. /** @addtogroup STM32F0xx_System_Private_Variables
  116. * @{
  117. */
  118. uint32_t SystemCoreClock = 48000000;
  119. __I uint8_t AHBPrescTable[16] = {0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 4, 6, 7, 8, 9};
  120. /**
  121. * @}
  122. */
  123. /** @addtogroup STM32F0xx_System_Private_FunctionPrototypes
  124. * @{
  125. */
  126. static void SetSysClock(void);
  127. /**
  128. * @}
  129. */
  130. /** @addtogroup STM32F0xx_System_Private_Functions
  131. * @{
  132. */
  133. /**
  134. * @brief Setup the microcontroller system.
  135. * Initialize the Embedded Flash Interface, the PLL and update the
  136. * SystemCoreClock variable.
  137. * @param None
  138. * @retval None
  139. */
  140. void SystemInit (void)
  141. {
  142. /* Set HSION bit */
  143. RCC->CR |= (uint32_t)0x00000001;
  144. #if defined(STM32F051)
  145. /* Reset SW[1:0], HPRE[3:0], PPRE[2:0], ADCPRE and MCOSEL[2:0] bits */
  146. RCC->CFGR &= (uint32_t)0xF8FFB80C;
  147. #else
  148. /* Reset SW[1:0], HPRE[3:0], PPRE[2:0], ADCPRE, MCOSEL[2:0], MCOPRE[2:0] and PLLNODIV bits */
  149. RCC->CFGR &= (uint32_t)0x08FFB80C;
  150. #endif /* STM32F051 */
  151. /* Reset HSEON, CSSON and PLLON bits */
  152. RCC->CR &= (uint32_t)0xFEF6FFFF;
  153. /* Reset HSEBYP bit */
  154. RCC->CR &= (uint32_t)0xFFFBFFFF;
  155. /* Reset PLLSRC, PLLXTPRE and PLLMUL[3:0] bits */
  156. RCC->CFGR &= (uint32_t)0xFFC0FFFF;
  157. /* Reset PREDIV1[3:0] bits */
  158. RCC->CFGR2 &= (uint32_t)0xFFFFFFF0;
  159. /* Reset USARTSW[1:0], I2CSW, CECSW and ADCSW bits */
  160. RCC->CFGR3 &= (uint32_t)0xFFFFFEAC;
  161. /* Reset HSI14 bit */
  162. RCC->CR2 &= (uint32_t)0xFFFFFFFE;
  163. /* Disable all interrupts */
  164. RCC->CIR = 0x00000000;
  165. /* Configure the System clock frequency, AHB/APBx prescalers and Flash settings */
  166. SetSysClock();
  167. }
  168. /**
  169. * @brief Update SystemCoreClock according to Clock Register Values
  170. * The SystemCoreClock variable contains the core clock (HCLK), it can
  171. * be used by the user application to setup the SysTick timer or configure
  172. * other parameters.
  173. *
  174. * @note Each time the core clock (HCLK) changes, this function must be called
  175. * to update SystemCoreClock variable value. Otherwise, any configuration
  176. * based on this variable will be incorrect.
  177. *
  178. * @note - The system frequency computed by this function is not the real
  179. * frequency in the chip. It is calculated based on the predefined
  180. * constant and the selected clock source:
  181. *
  182. * - If SYSCLK source is HSI, SystemCoreClock will contain the HSI_VALUE(*)
  183. *
  184. * - If SYSCLK source is HSE, SystemCoreClock will contain the HSE_VALUE(**)
  185. *
  186. * - If SYSCLK source is PLL, SystemCoreClock will contain the HSE_VALUE(**)
  187. * or HSI_VALUE(*) multiplied/divided by the PLL factors.
  188. *
  189. * (*) HSI_VALUE is a constant defined in stm32f0xx.h file (default value
  190. * 8 MHz) but the real value may vary depending on the variations
  191. * in voltage and temperature.
  192. *
  193. * (**) HSE_VALUE is a constant defined in stm32f0xx.h file (default value
  194. * 8 MHz), user has to ensure that HSE_VALUE is same as the real
  195. * frequency of the crystal used. Otherwise, this function may
  196. * have wrong result.
  197. *
  198. * - The result of this function could be not correct when using fractional
  199. * value for HSE crystal.
  200. * @param None
  201. * @retval None
  202. */
  203. void SystemCoreClockUpdate (void)
  204. {
  205. uint32_t tmp = 0, pllmull = 0, pllsource = 0, prediv1factor = 0;
  206. /* Get SYSCLK source -------------------------------------------------------*/
  207. tmp = RCC->CFGR & RCC_CFGR_SWS;
  208. switch (tmp)
  209. {
  210. case 0x00: /* HSI used as system clock */
  211. SystemCoreClock = HSI_VALUE;
  212. break;
  213. case 0x04: /* HSE used as system clock */
  214. SystemCoreClock = HSE_VALUE;
  215. break;
  216. case 0x08: /* PLL used as system clock */
  217. /* Get PLL clock source and multiplication factor ----------------------*/
  218. pllmull = RCC->CFGR & RCC_CFGR_PLLMULL;
  219. pllsource = RCC->CFGR & RCC_CFGR_PLLSRC;
  220. pllmull = ( pllmull >> 18) + 2;
  221. if (pllsource == 0x00)
  222. {
  223. /* HSI oscillator clock divided by 2 selected as PLL clock entry */
  224. SystemCoreClock = (HSI_VALUE >> 1) * pllmull;
  225. }
  226. else
  227. {
  228. prediv1factor = (RCC->CFGR2 & RCC_CFGR2_PREDIV1) + 1;
  229. /* HSE oscillator clock selected as PREDIV1 clock entry */
  230. SystemCoreClock = (HSE_VALUE / prediv1factor) * pllmull;
  231. }
  232. break;
  233. default: /* HSI used as system clock */
  234. SystemCoreClock = HSI_VALUE;
  235. break;
  236. }
  237. /* Compute HCLK clock frequency ----------------*/
  238. /* Get HCLK prescaler */
  239. tmp = AHBPrescTable[((RCC->CFGR & RCC_CFGR_HPRE) >> 4)];
  240. /* HCLK clock frequency */
  241. SystemCoreClock >>= tmp;
  242. }
  243. /**
  244. * @brief Configures the System clock frequency, AHB/APBx prescalers and Flash
  245. * settings.
  246. * @note This function should be called only once the RCC clock configuration
  247. * is reset to the default reset state (done in SystemInit() function).
  248. * @param None
  249. * @retval None
  250. */
  251. static void SetSysClock(void)
  252. {
  253. __IO uint32_t StartUpCounter = 0, HSEStatus = 0;
  254. /******************************************************************************/
  255. /* PLL (clocked by HSE) used as System clock source */
  256. /******************************************************************************/
  257. /* SYSCLK, HCLK, PCLK configuration ----------------------------------------*/
  258. /* Enable HSE */
  259. RCC->CR |= ((uint32_t)RCC_CR_HSEON);
  260. /* Wait till HSE is ready and if Time out is reached exit */
  261. do
  262. {
  263. HSEStatus = RCC->CR & RCC_CR_HSERDY;
  264. StartUpCounter++;
  265. } while((HSEStatus == 0) && (StartUpCounter != HSE_STARTUP_TIMEOUT));
  266. if ((RCC->CR & RCC_CR_HSERDY) != RESET)
  267. {
  268. HSEStatus = (uint32_t)0x01;
  269. }
  270. else
  271. {
  272. HSEStatus = (uint32_t)0x00;
  273. }
  274. if (HSEStatus == (uint32_t)0x01)
  275. {
  276. /* Enable Prefetch Buffer and set Flash Latency */
  277. FLASH->ACR = FLASH_ACR_PRFTBE | FLASH_ACR_LATENCY;
  278. /* HCLK = SYSCLK */
  279. RCC->CFGR |= (uint32_t)RCC_CFGR_HPRE_DIV1;
  280. /* PCLK = HCLK */
  281. RCC->CFGR |= (uint32_t)RCC_CFGR_PPRE_DIV1;
  282. /* PLL configuration */
  283. RCC->CFGR &= (uint32_t)((uint32_t)~(RCC_CFGR_PLLSRC | RCC_CFGR_PLLXTPRE | RCC_CFGR_PLLMULL));
  284. RCC->CFGR |= (uint32_t)(RCC_CFGR_PLLSRC_PREDIV1 | RCC_CFGR_PLLXTPRE_PREDIV1 | RCC_CFGR_PLLMULL6);
  285. /* Enable PLL */
  286. RCC->CR |= RCC_CR_PLLON;
  287. /* Wait till PLL is ready */
  288. while((RCC->CR & RCC_CR_PLLRDY) == 0)
  289. {
  290. }
  291. /* Select PLL as system clock source */
  292. RCC->CFGR &= (uint32_t)((uint32_t)~(RCC_CFGR_SW));
  293. RCC->CFGR |= (uint32_t)RCC_CFGR_SW_PLL;
  294. /* Wait till PLL is used as system clock source */
  295. while ((RCC->CFGR & (uint32_t)RCC_CFGR_SWS) != (uint32_t)RCC_CFGR_SWS_PLL)
  296. {
  297. }
  298. }
  299. else
  300. { /* If HSE fails to start-up, the application will have wrong clock
  301. configuration. User can add here some code to deal with this error */
  302. }
  303. }
  304. /**
  305. * @}
  306. */
  307. /**
  308. * @}
  309. */
  310. /**
  311. * @}
  312. */
  313. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/