system_stm32f0xx.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. /**
  2. ******************************************************************************
  3. * @file system_stm32f0xx.c
  4. * @author MCD Application Team
  5. * @version V1.0.0RC1
  6. * @date 27-January-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_VX.Y.Z.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. * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
  71. * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE
  72. * TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY
  73. * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING
  74. * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE
  75. * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
  76. *
  77. * FOR MORE INFORMATION PLEASE READ CAREFULLY THE LICENSE AGREEMENT FILE
  78. * LOCATED IN THE ROOT DIRECTORY OF THIS FIRMWARE PACKAGE.
  79. *
  80. * <h2><center>&copy; COPYRIGHT 2012 STMicroelectronics</center></h2>
  81. ******************************************************************************
  82. */
  83. /** @addtogroup CMSIS
  84. * @{
  85. */
  86. /** @addtogroup stm32f0xx_system
  87. * @{
  88. */
  89. /** @addtogroup STM32F0xx_System_Private_Includes
  90. * @{
  91. */
  92. #include "stm32f0xx.h"
  93. /**
  94. * @}
  95. */
  96. /** @addtogroup STM32F0xx_System_Private_TypesDefinitions
  97. * @{
  98. */
  99. /**
  100. * @}
  101. */
  102. /** @addtogroup STM32F0xx_System_Private_Defines
  103. * @{
  104. */
  105. /**
  106. * @}
  107. */
  108. /** @addtogroup STM32F0xx_System_Private_Macros
  109. * @{
  110. */
  111. /**
  112. * @}
  113. */
  114. /** @addtogroup STM32F0xx_System_Private_Variables
  115. * @{
  116. */
  117. uint32_t SystemCoreClock = 36000000;
  118. __I uint8_t AHBPrescTable[16] = {0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 4, 6, 7, 8, 9};
  119. /**
  120. * @}
  121. */
  122. /** @addtogroup STM32F0xx_System_Private_FunctionPrototypes
  123. * @{
  124. */
  125. //static void SetSysClock(void);
  126. /**
  127. * @}
  128. */
  129. /** @addtogroup STM32F0xx_System_Private_Functions
  130. * @{
  131. */
  132. /**
  133. * @brief Setup the microcontroller system.
  134. * Initialize the Embedded Flash Interface, the PLL and update the
  135. * SystemCoreClock variable.
  136. * @param None
  137. * @retval None
  138. */
  139. void SystemInit (void)
  140. {
  141. /* Set HSION bit */
  142. RCC->CR |= (uint32_t)0x00000001;
  143. // select HSI as PLL source
  144. RCC->CFGR |= (uint32_t)RCC_CFGR_PLLSRC_HSI_Div2;
  145. //PLLCLK=8/2*12=48M
  146. RCC->CFGR |= (uint32_t)RCC_CFGR_PLLMULL9;
  147. /* HCLK = SYSCLK/1 */
  148. RCC->CFGR |= (uint32_t)RCC_CFGR_HPRE_DIV1;
  149. /* Enable PLL */
  150. RCC->CR |= RCC_CR_PLLON;
  151. /* Wait till PLL is ready */
  152. while((RCC->CR & RCC_CR_PLLRDY) == 0)
  153. {
  154. }
  155. /* Select PLL as system clock source */
  156. RCC->CFGR &= (uint32_t)((uint32_t)~(RCC_CFGR_SW));
  157. RCC->CFGR |= (uint32_t)RCC_CFGR_SW_PLL;
  158. /* Wait till PLL is used as system clock source */
  159. while ((RCC->CFGR & (uint32_t)RCC_CFGR_SWS) != (uint32_t)0x08)
  160. {
  161. }
  162. }
  163. /**
  164. * @brief Update SystemCoreClock according to Clock Register Values
  165. * @note - The system frequency computed by this function is not the real
  166. * frequency in the chip. It is calculated based on the predefined
  167. * constant and the selected clock source:
  168. *
  169. * - If SYSCLK source is HSI, SystemCoreClock will contain the HSI_VALUE(*)
  170. *
  171. * - If SYSCLK source is HSE, SystemCoreClock will contain the HSE_VALUE(**)
  172. *
  173. * - If SYSCLK source is PLL, SystemCoreClock will contain the HSE_VALUE(**)
  174. * or HSI_VALUE(*) multiplied/divided by the PLL factors.
  175. *
  176. * (*) HSI_VALUE is a constant defined in stm32f0xx.h file (default value
  177. * 8 MHz) but the real value may vary depending on the variations
  178. * in voltage and temperature.
  179. *
  180. * (**) HSE_VALUE is a constant defined in stm32f0xx.h file (default value
  181. * 8 MHz), user has to ensure that HSE_VALUE is same as the real
  182. * frequency of the crystal used. Otherwise, this function may
  183. * have wrong result.
  184. *
  185. * - The result of this function could be not correct when using fractional
  186. * value for HSE crystal.
  187. * @param None
  188. * @retval None
  189. */
  190. void SystemCoreClockUpdate (void)
  191. {
  192. uint32_t tmp = 0, pllmull = 0, pllsource = 0, prediv1factor = 0;
  193. /* Get SYSCLK source -------------------------------------------------------*/
  194. tmp = RCC->CFGR & RCC_CFGR_SWS;
  195. switch (tmp)
  196. {
  197. case 0x00: /* HSI used as system clock */
  198. SystemCoreClock = HSI_VALUE;
  199. break;
  200. case 0x04: /* HSE used as system clock */
  201. SystemCoreClock = HSE_VALUE;
  202. break;
  203. case 0x08: /* PLL used as system clock */
  204. /* Get PLL clock source and multiplication factor ----------------------*/
  205. pllmull = RCC->CFGR & RCC_CFGR_PLLMULL;
  206. pllsource = RCC->CFGR & RCC_CFGR_PLLSRC;
  207. pllmull = ( pllmull >> 18) + 2;
  208. if (pllsource == 0x00)
  209. {
  210. /* HSI oscillator clock divided by 2 selected as PLL clock entry */
  211. SystemCoreClock = (HSI_VALUE >> 1) * pllmull;
  212. }
  213. else
  214. {
  215. prediv1factor = (RCC->CFGR2 & RCC_CFGR2_PREDIV1) + 1;
  216. /* HSE oscillator clock selected as PREDIV1 clock entry */
  217. SystemCoreClock = (HSE_VALUE / prediv1factor) * pllmull;
  218. }
  219. break;
  220. default: /* HSI used as system clock */
  221. SystemCoreClock = HSI_VALUE;
  222. break;
  223. }
  224. /* Compute HCLK clock frequency ----------------*/
  225. /* Get HCLK prescaler */
  226. tmp = AHBPrescTable[((RCC->CFGR & RCC_CFGR_HPRE) >> 4)];
  227. /* HCLK clock frequency */
  228. SystemCoreClock >>= tmp;
  229. }
  230. /**
  231. * @brief Configures the System clock frequency, AHB/APBx prescalers and Flash
  232. * settings.
  233. * @note This function should be called only once the RCC clock configuration
  234. * is reset to the default reset state (done in SystemInit() function).
  235. * @param None
  236. * @retval None
  237. */
  238. //static void SetSysClock(void)
  239. //{
  240. // __IO uint32_t StartUpCounter = 0, HSEStatus = 0;
  241. //
  242. // /* SYSCLK, HCLK, PCLK configuration ----------------------------------------*/
  243. // /* Enable HSE */
  244. // RCC->CR |= ((uint32_t)RCC_CR_HSEON);
  245. //
  246. // /* Wait till HSE is ready and if Time out is reached exit */
  247. // do
  248. // {
  249. // HSEStatus = RCC->CR & RCC_CR_HSERDY;
  250. // StartUpCounter++;
  251. // } while((HSEStatus == 0) && (StartUpCounter != HSE_STARTUP_TIMEOUT));
  252. // if ((RCC->CR & RCC_CR_HSERDY) != RESET)
  253. // {
  254. // HSEStatus = (uint32_t)0x01;
  255. // }
  256. // else
  257. // {
  258. // HSEStatus = (uint32_t)0x00;
  259. // }
  260. // if (HSEStatus == (uint32_t)0x01)
  261. // {
  262. // /* Enable Prefetch Buffer */
  263. // FLASH->ACR |= FLASH_ACR_PRFTBE;
  264. // FLASH->ACR |= (uint32_t)FLASH_ACR_LATENCY;
  265. //
  266. // /* HCLK = SYSCLK */
  267. // RCC->CFGR |= (uint32_t)RCC_CFGR_HPRE_DIV1;
  268. //
  269. // /* PCLK = HCLK */
  270. // RCC->CFGR |= (uint32_t)RCC_CFGR_PPRE_DIV1;
  271. // /* PLL configuration: = HSE * 6 = 48 MHz */
  272. // RCC->CFGR &= (uint32_t)((uint32_t)~(RCC_CFGR_PLLSRC | RCC_CFGR_PLLXTPRE | RCC_CFGR_PLLMULL));
  273. // RCC->CFGR |= (uint32_t)(RCC_CFGR_PLLSRC_PREDIV1 | RCC_CFGR_PLLXTPRE_PREDIV1 | RCC_CFGR_PLLMULL6);
  274. //
  275. // /* Enable PLL */
  276. // RCC->CR |= RCC_CR_PLLON;
  277. // /* Wait till PLL is ready */
  278. // while((RCC->CR & RCC_CR_PLLRDY) == 0)
  279. // {
  280. // }
  281. // /* Select PLL as system clock source */
  282. // RCC->CFGR &= (uint32_t)((uint32_t)~(RCC_CFGR_SW));
  283. // RCC->CFGR |= (uint32_t)RCC_CFGR_SW_PLL;
  284. // /* Wait till PLL is used as system clock source */
  285. // while ((RCC->CFGR & (uint32_t)RCC_CFGR_SWS) != (uint32_t)0x08)
  286. // {
  287. // }
  288. // }
  289. // else
  290. // { /* If HSE fails to start-up, the application will have wrong clock
  291. // configuration. User can add here some code to deal with this error */
  292. // }
  293. //}
  294. /**
  295. * @}
  296. */
  297. /**
  298. * @}
  299. */
  300. /**
  301. * @}
  302. */
  303. /******************* (C) COPYRIGHT 2012 STMicroelectronics *****END OF FILE****/