system_stm32f0xx.c 12 KB

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