system_stm32f0xx.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  1. /**
  2. ******************************************************************************
  3. * @file system_stm32f0xx.c
  4. * @author MCD Application Team
  5. * @version V1.0.0
  6. * @date 18-May-2012
  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 2012 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. /* Reset SW[1:0], HPRE[3:0], PPRE[2:0], ADCPRE and MCOSEL[2:0] bits */
  145. RCC->CFGR &= (uint32_t)0xF8FFB80C;
  146. /* Reset HSEON, CSSON and PLLON bits */
  147. RCC->CR &= (uint32_t)0xFEF6FFFF;
  148. /* Reset HSEBYP bit */
  149. RCC->CR &= (uint32_t)0xFFFBFFFF;
  150. /* Reset PLLSRC, PLLXTPRE and PLLMUL[3:0] bits */
  151. RCC->CFGR &= (uint32_t)0xFFC0FFFF;
  152. /* Reset PREDIV1[3:0] bits */
  153. RCC->CFGR2 &= (uint32_t)0xFFFFFFF0;
  154. /* Reset USARTSW[1:0], I2CSW, CECSW and ADCSW bits */
  155. RCC->CFGR3 &= (uint32_t)0xFFFFFEAC;
  156. /* Reset HSI14 bit */
  157. RCC->CR2 &= (uint32_t)0xFFFFFFFE;
  158. /* Disable all interrupts */
  159. RCC->CIR = 0x00000000;
  160. /* Configure the System clock frequency, AHB/APBx prescalers and Flash settings */
  161. SetSysClock();
  162. }
  163. /**
  164. * @brief Update SystemCoreClock according to Clock Register Values
  165. * The SystemCoreClock variable contains the core clock (HCLK), it can
  166. * be used by the user application to setup the SysTick timer or configure
  167. * other parameters.
  168. *
  169. * @note Each time the core clock (HCLK) changes, this function must be called
  170. * to update SystemCoreClock variable value. Otherwise, any configuration
  171. * based on this variable will be incorrect.
  172. *
  173. * @note - The system frequency computed by this function is not the real
  174. * frequency in the chip. It is calculated based on the predefined
  175. * constant and the selected clock source:
  176. *
  177. * - If SYSCLK source is HSI, SystemCoreClock will contain the HSI_VALUE(*)
  178. *
  179. * - If SYSCLK source is HSE, SystemCoreClock will contain the HSE_VALUE(**)
  180. *
  181. * - If SYSCLK source is PLL, SystemCoreClock will contain the HSE_VALUE(**)
  182. * or HSI_VALUE(*) multiplied/divided by the PLL factors.
  183. *
  184. * (*) HSI_VALUE is a constant defined in stm32f0xx.h file (default value
  185. * 8 MHz) but the real value may vary depending on the variations
  186. * in voltage and temperature.
  187. *
  188. * (**) HSE_VALUE is a constant defined in stm32f0xx.h file (default value
  189. * 8 MHz), user has to ensure that HSE_VALUE is same as the real
  190. * frequency of the crystal used. Otherwise, this function may
  191. * have wrong result.
  192. *
  193. * - The result of this function could be not correct when using fractional
  194. * value for HSE crystal.
  195. * @param None
  196. * @retval None
  197. */
  198. void SystemCoreClockUpdate (void)
  199. {
  200. uint32_t tmp = 0, pllmull = 0, pllsource = 0, prediv1factor = 0;
  201. /* Get SYSCLK source -------------------------------------------------------*/
  202. tmp = RCC->CFGR & RCC_CFGR_SWS;
  203. switch (tmp)
  204. {
  205. case 0x00: /* HSI used as system clock */
  206. SystemCoreClock = HSI_VALUE;
  207. break;
  208. case 0x04: /* HSE used as system clock */
  209. SystemCoreClock = HSE_VALUE;
  210. break;
  211. case 0x08: /* PLL used as system clock */
  212. /* Get PLL clock source and multiplication factor ----------------------*/
  213. pllmull = RCC->CFGR & RCC_CFGR_PLLMULL;
  214. pllsource = RCC->CFGR & RCC_CFGR_PLLSRC;
  215. pllmull = ( pllmull >> 18) + 2;
  216. if (pllsource == 0x00)
  217. {
  218. /* HSI oscillator clock divided by 2 selected as PLL clock entry */
  219. SystemCoreClock = (HSI_VALUE >> 1) * pllmull;
  220. }
  221. else
  222. {
  223. prediv1factor = (RCC->CFGR2 & RCC_CFGR2_PREDIV1) + 1;
  224. /* HSE oscillator clock selected as PREDIV1 clock entry */
  225. SystemCoreClock = (HSE_VALUE / prediv1factor) * pllmull;
  226. }
  227. break;
  228. default: /* HSI used as system clock */
  229. SystemCoreClock = HSI_VALUE;
  230. break;
  231. }
  232. /* Compute HCLK clock frequency ----------------*/
  233. /* Get HCLK prescaler */
  234. tmp = AHBPrescTable[((RCC->CFGR & RCC_CFGR_HPRE) >> 4)];
  235. /* HCLK clock frequency */
  236. SystemCoreClock >>= tmp;
  237. }
  238. /**
  239. * @brief Configures the System clock frequency, AHB/APBx prescalers and Flash
  240. * settings.
  241. * @note This function should be called only once the RCC clock configuration
  242. * is reset to the default reset state (done in SystemInit() function).
  243. * @param None
  244. * @retval None
  245. */
  246. static void SetSysClock(void)
  247. {
  248. __IO uint32_t StartUpCounter = 0, HSEStatus = 0;
  249. /******************************************************************************/
  250. /* PLL (clocked by HSE) used as System clock source */
  251. /******************************************************************************/
  252. /* SYSCLK, HCLK, PCLK configuration ----------------------------------------*/
  253. /* Enable HSE */
  254. RCC->CR |= ((uint32_t)RCC_CR_HSEON);
  255. /* Wait till HSE is ready and if Time out is reached exit */
  256. do
  257. {
  258. HSEStatus = RCC->CR & RCC_CR_HSERDY;
  259. StartUpCounter++;
  260. } while((HSEStatus == 0) && (StartUpCounter != HSE_STARTUP_TIMEOUT));
  261. if ((RCC->CR & RCC_CR_HSERDY) != RESET)
  262. {
  263. HSEStatus = (uint32_t)0x01;
  264. }
  265. else
  266. {
  267. HSEStatus = (uint32_t)0x00;
  268. }
  269. if (HSEStatus == (uint32_t)0x01)
  270. {
  271. /* Enable Prefetch Buffer and set Flash Latency */
  272. FLASH->ACR = FLASH_ACR_PRFTBE | FLASH_ACR_LATENCY;
  273. /* HCLK = SYSCLK */
  274. RCC->CFGR |= (uint32_t)RCC_CFGR_HPRE_DIV1;
  275. /* PCLK = HCLK */
  276. RCC->CFGR |= (uint32_t)RCC_CFGR_PPRE_DIV1;
  277. /* PLL configuration */
  278. RCC->CFGR &= (uint32_t)((uint32_t)~(RCC_CFGR_PLLSRC | RCC_CFGR_PLLXTPRE | RCC_CFGR_PLLMULL));
  279. RCC->CFGR |= (uint32_t)(RCC_CFGR_PLLSRC_PREDIV1 | RCC_CFGR_PLLXTPRE_PREDIV1 | RCC_CFGR_PLLMULL6);
  280. /* Enable PLL */
  281. RCC->CR |= RCC_CR_PLLON;
  282. /* Wait till PLL is ready */
  283. while((RCC->CR & RCC_CR_PLLRDY) == 0)
  284. {
  285. }
  286. /* Select PLL as system clock source */
  287. RCC->CFGR &= (uint32_t)((uint32_t)~(RCC_CFGR_SW));
  288. RCC->CFGR |= (uint32_t)RCC_CFGR_SW_PLL;
  289. /* Wait till PLL is used as system clock source */
  290. while ((RCC->CFGR & (uint32_t)RCC_CFGR_SWS) != (uint32_t)RCC_CFGR_SWS_PLL)
  291. {
  292. }
  293. }
  294. else
  295. { /* If HSE fails to start-up, the application will have wrong clock
  296. configuration. User can add here some code to deal with this error */
  297. }
  298. }
  299. /**
  300. * @}
  301. */
  302. /**
  303. * @}
  304. */
  305. /**
  306. * @}
  307. */
  308. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/