]> git.sur5r.net Git - freertos/blob - Demo/CORTEX_M0_STM32F0518_IAR/Libraries/CMSIS/Device/ST/STM32F0xx/Source/Templates/system_stm32f0xx.c
Start to re-arrange files to include FreeRTOS+ in main download.
[freertos] / Demo / CORTEX_M0_STM32F0518_IAR / Libraries / CMSIS / Device / ST / STM32F0xx / Source / Templates / system_stm32f0xx.c
1 /**\r
2   ******************************************************************************\r
3   * @file    system_stm32f0xx.c\r
4   * @author  MCD Application Team\r
5   * @version V1.0.0RC1\r
6   * @date    27-January-2012\r
7   * @brief   CMSIS Cortex-M0 Device Peripheral Access Layer System Source File.\r
8   *          This file contains the system clock configuration for STM32F0xx devices,\r
9   *          and is generated by the clock configuration tool  \r
10   *          STM32F0xx_Clock_Configuration_VX.Y.Z.xls\r
11   *             \r
12   * 1.  This file provides two functions and one global variable to be called from \r
13   *     user application:\r
14   *      - SystemInit(): Setups the system clock (System clock source, PLL Multiplier\r
15   *                      and Divider factors, AHB/APBx prescalers and Flash settings),\r
16   *                      depending on the configuration made in the clock xls tool.\r
17   *                      This function is called at startup just after reset and \r
18   *                      before branch to main program. This call is made inside\r
19   *                      the "startup_stm32f0xx.s" file.\r
20   *                        \r
21   *      - SystemCoreClock variable: Contains the core clock (HCLK), it can be used\r
22   *                                  by the user application to setup the SysTick \r
23   *                                  timer or configure other parameters.\r
24   *                                     \r
25   *      - SystemCoreClockUpdate(): Updates the variable SystemCoreClock and must\r
26   *                                 be called whenever the core clock is changed\r
27   *                                 during program execution.\r
28   *      \r
29   * 2. After each device reset the HSI (8 MHz Range) is used as system clock source.\r
30   *    Then SystemInit() function is called, in "startup_stm32f0xx.s" file, to\r
31   *    configure the system clock before to branch to main program.\r
32   *    \r
33   * 3. If the system clock source selected by user fails to startup, the SystemInit()\r
34   *    function will do nothing and HSI still used as system clock source. User can \r
35   *    add some code to deal with this issue inside the SetSysClock() function.\r
36   * \r
37   * 4. The default value of HSE crystal is set to 8MHz, refer to "HSE_VALUE" define\r
38   *    in "stm32f0xx.h" file. When HSE is used as system clock source, directly or\r
39   *    through PLL, and you are using different crystal you have to adapt the HSE\r
40   *    value to your own configuration.\r
41   * \r
42   * 5. This file configures the system clock as follows:\r
43   *=============================================================================\r
44   *                         System Clock Configuration\r
45   *=============================================================================\r
46   *        System Clock source          | PLL(HSE)\r
47   *-----------------------------------------------------------------------------\r
48   *        SYSCLK                       | 48000000 Hz\r
49   *-----------------------------------------------------------------------------\r
50   *        HCLK                         | 48000000 Hz\r
51   *-----------------------------------------------------------------------------\r
52   *        AHB Prescaler                | 1\r
53   *-----------------------------------------------------------------------------\r
54   *        APB1 Prescaler               | 1\r
55   *-----------------------------------------------------------------------------\r
56   *        APB2 Prescaler               | 1\r
57   *-----------------------------------------------------------------------------\r
58   *        HSE Frequency                | 8000000 Hz\r
59   *-----------------------------------------------------------------------------\r
60   *        PLL MUL                      | 6\r
61   *-----------------------------------------------------------------------------\r
62   *        VDD                          | 3.3 V\r
63   *-----------------------------------------------------------------------------\r
64   *        Flash Latency                | 1 WS\r
65   *-----------------------------------------------------------------------------\r
66   *=============================================================================\r
67   ******************************************************************************\r
68   * @attention\r
69   *\r
70   * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS\r
71   * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE\r
72   * TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY\r
73   * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING\r
74   * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE\r
75   * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.\r
76   *\r
77   * FOR MORE INFORMATION PLEASE READ CAREFULLY THE LICENSE AGREEMENT FILE\r
78   * LOCATED IN THE ROOT DIRECTORY OF THIS FIRMWARE PACKAGE.\r
79   *\r
80   * <h2><center>&copy; COPYRIGHT 2012 STMicroelectronics</center></h2>\r
81   ******************************************************************************\r
82   */\r
83 \r
84 /** @addtogroup CMSIS\r
85   * @{\r
86   */\r
87 \r
88 /** @addtogroup stm32f0xx_system\r
89   * @{\r
90   */  \r
91   \r
92 /** @addtogroup STM32F0xx_System_Private_Includes\r
93   * @{\r
94   */\r
95 \r
96 #include "stm32f0xx.h"\r
97 \r
98 /**\r
99   * @}\r
100   */\r
101 \r
102 /** @addtogroup STM32F0xx_System_Private_TypesDefinitions\r
103   * @{\r
104   */\r
105 \r
106 /**\r
107   * @}\r
108   */\r
109 \r
110 /** @addtogroup STM32F0xx_System_Private_Defines\r
111   * @{\r
112   */\r
113 /**\r
114   * @}\r
115   */\r
116 \r
117 /** @addtogroup STM32F0xx_System_Private_Macros\r
118   * @{\r
119   */\r
120 \r
121 /**\r
122   * @}\r
123   */\r
124 \r
125 /** @addtogroup STM32F0xx_System_Private_Variables\r
126   * @{\r
127   */\r
128 uint32_t SystemCoreClock    = 48000000;\r
129 __I uint8_t AHBPrescTable[16] = {0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 4, 6, 7, 8, 9};\r
130 \r
131 /**\r
132   * @}\r
133   */\r
134 \r
135 /** @addtogroup STM32F0xx_System_Private_FunctionPrototypes\r
136   * @{\r
137   */\r
138 \r
139 static void SetSysClock(void);\r
140 \r
141 /**\r
142   * @}\r
143   */\r
144 \r
145 /** @addtogroup STM32F0xx_System_Private_Functions\r
146   * @{\r
147   */\r
148 \r
149 /**\r
150   * @brief  Setup the microcontroller system.\r
151   *         Initialize the Embedded Flash Interface, the PLL and update the \r
152   *         SystemCoreClock variable.\r
153   * @param  None\r
154   * @retval None\r
155   */\r
156 void SystemInit (void)\r
157 {    \r
158   /* Set HSION bit */\r
159   RCC->CR |= (uint32_t)0x00000001;\r
160 \r
161   /* Reset SW[1:0], HPRE[3:0], PPRE[2:0], ADCPRE and MCOSEL[2:0] bits */\r
162   RCC->CFGR &= (uint32_t)0xF8FFB80C;\r
163   \r
164   /* Reset HSEON, CSSON and PLLON bits */\r
165   RCC->CR &= (uint32_t)0xFEF6FFFF;\r
166 \r
167   /* Reset HSEBYP bit */\r
168   RCC->CR &= (uint32_t)0xFFFBFFFF;\r
169 \r
170   /* Reset PLLSRC, PLLXTPRE and PLLMUL[3:0] bits */\r
171   RCC->CFGR &= (uint32_t)0xFFC0FFFF;\r
172 \r
173   /* Reset PREDIV1[3:0] bits */\r
174   RCC->CFGR2 &= (uint32_t)0xFFFFFFF0;\r
175 \r
176   /* Reset USARTSW[1:0], I2CSW, CECSW and ADCSW bits */\r
177   RCC->CFGR3 &= (uint32_t)0xFFFFFEAC;\r
178   \r
179   /* Reset HSI14 bit */\r
180   RCC->CR2 &= (uint32_t)0xFFFFFFFE;\r
181 \r
182   /* Disable all interrupts */\r
183   RCC->CIR = 0x00000000;\r
184   \r
185 /* Configure the System clock frequency, AHB/APBx prescalers and Flash settings */\r
186   SetSysClock();\r
187 }\r
188 \r
189 /**\r
190   * @brief  Update SystemCoreClock according to Clock Register Values\r
191   * @note   - The system frequency computed by this function is not the real \r
192   *           frequency in the chip. It is calculated based on the predefined \r
193   *           constant and the selected clock source:\r
194   *\r
195   *           - If SYSCLK source is HSI, SystemCoreClock will contain the HSI_VALUE(*)\r
196   *                                              \r
197   *           - If SYSCLK source is HSE, SystemCoreClock will contain the HSE_VALUE(**)\r
198   *                          \r
199   *           - If SYSCLK source is PLL, SystemCoreClock will contain the HSE_VALUE(**)\r
200   *             or HSI_VALUE(*) multiplied/divided by the PLL factors.\r
201   *         \r
202   *         (*) HSI_VALUE is a constant defined in stm32f0xx.h file (default value\r
203   *             8 MHz) but the real value may vary depending on the variations\r
204   *             in voltage and temperature.\r
205   *    \r
206   *         (**) HSE_VALUE is a constant defined in stm32f0xx.h file (default value\r
207   *              8 MHz), user has to ensure that HSE_VALUE is same as the real\r
208   *              frequency of the crystal used. Otherwise, this function may\r
209   *              have wrong result.\r
210   *                \r
211   *         - The result of this function could be not correct when using fractional\r
212   *           value for HSE crystal.\r
213   * @param  None\r
214   * @retval None\r
215   */\r
216 void SystemCoreClockUpdate (void)\r
217 {\r
218   uint32_t tmp = 0, pllmull = 0, pllsource = 0, prediv1factor = 0;\r
219 \r
220   /* Get SYSCLK source -------------------------------------------------------*/\r
221   tmp = RCC->CFGR & RCC_CFGR_SWS;\r
222   \r
223   switch (tmp)\r
224   {\r
225     case 0x00:  /* HSI used as system clock */\r
226       SystemCoreClock = HSI_VALUE;\r
227       break;\r
228     case 0x04:  /* HSE used as system clock */\r
229       SystemCoreClock = HSE_VALUE;\r
230       break;\r
231     case 0x08:  /* PLL used as system clock */\r
232       /* Get PLL clock source and multiplication factor ----------------------*/\r
233       pllmull = RCC->CFGR & RCC_CFGR_PLLMULL;\r
234       pllsource = RCC->CFGR & RCC_CFGR_PLLSRC;\r
235       pllmull = ( pllmull >> 18) + 2;\r
236       \r
237       if (pllsource == 0x00)\r
238       {\r
239         /* HSI oscillator clock divided by 2 selected as PLL clock entry */\r
240         SystemCoreClock = (HSI_VALUE >> 1) * pllmull;\r
241       }\r
242       else\r
243       {\r
244         prediv1factor = (RCC->CFGR2 & RCC_CFGR2_PREDIV1) + 1;\r
245         /* HSE oscillator clock selected as PREDIV1 clock entry */\r
246         SystemCoreClock = (HSE_VALUE / prediv1factor) * pllmull; \r
247       }      \r
248       break;\r
249     default: /* HSI used as system clock */\r
250       SystemCoreClock = HSI_VALUE;\r
251       break;\r
252   }\r
253   /* Compute HCLK clock frequency ----------------*/\r
254   /* Get HCLK prescaler */\r
255   tmp = AHBPrescTable[((RCC->CFGR & RCC_CFGR_HPRE) >> 4)];\r
256   /* HCLK clock frequency */\r
257   SystemCoreClock >>= tmp;  \r
258 }\r
259 \r
260 /**\r
261   * @brief  Configures the System clock frequency, AHB/APBx prescalers and Flash\r
262   *         settings.\r
263   * @note   This function should be called only once the RCC clock configuration\r
264   *         is reset to the default reset state (done in SystemInit() function).\r
265   * @param  None\r
266   * @retval None\r
267   */\r
268 static void SetSysClock(void)\r
269 {\r
270   __IO uint32_t StartUpCounter = 0, HSEStatus = 0;\r
271   \r
272   /* SYSCLK, HCLK, PCLK configuration ----------------------------------------*/\r
273   /* Enable HSE */    \r
274   RCC->CR |= ((uint32_t)RCC_CR_HSEON);\r
275  \r
276   /* Wait till HSE is ready and if Time out is reached exit */\r
277   do\r
278   {\r
279     HSEStatus = RCC->CR & RCC_CR_HSERDY;\r
280     StartUpCounter++;  \r
281   } while((HSEStatus == 0) && (StartUpCounter != HSE_STARTUP_TIMEOUT));\r
282 \r
283   if ((RCC->CR & RCC_CR_HSERDY) != RESET)\r
284   {\r
285     HSEStatus = (uint32_t)0x01;\r
286   }\r
287   else\r
288   {\r
289     HSEStatus = (uint32_t)0x00;\r
290   }  \r
291 \r
292   if (HSEStatus == (uint32_t)0x01)\r
293   {\r
294     /* Enable Prefetch Buffer */\r
295     FLASH->ACR |= FLASH_ACR_PRFTBE;\r
296     FLASH->ACR |= (uint32_t)FLASH_ACR_LATENCY;\r
297  \r
298     /* HCLK = SYSCLK */\r
299     RCC->CFGR |= (uint32_t)RCC_CFGR_HPRE_DIV1;\r
300       \r
301     /* PCLK = HCLK */\r
302     RCC->CFGR |= (uint32_t)RCC_CFGR_PPRE_DIV1;\r
303 \r
304     /*  PLL configuration:  = HSE *  6 = 48 MHz */\r
305     RCC->CFGR &= (uint32_t)((uint32_t)~(RCC_CFGR_PLLSRC | RCC_CFGR_PLLXTPRE | RCC_CFGR_PLLMULL));\r
306     RCC->CFGR |= (uint32_t)(RCC_CFGR_PLLSRC_PREDIV1 | RCC_CFGR_PLLXTPRE_PREDIV1 | RCC_CFGR_PLLMULL6);\r
307             \r
308     /* Enable PLL */\r
309     RCC->CR |= RCC_CR_PLLON;\r
310 \r
311     /* Wait till PLL is ready */\r
312     while((RCC->CR & RCC_CR_PLLRDY) == 0)\r
313     {\r
314     }\r
315 \r
316     /* Select PLL as system clock source */\r
317     RCC->CFGR &= (uint32_t)((uint32_t)~(RCC_CFGR_SW));\r
318     RCC->CFGR |= (uint32_t)RCC_CFGR_SW_PLL;    \r
319 \r
320     /* Wait till PLL is used as system clock source */\r
321     while ((RCC->CFGR & (uint32_t)RCC_CFGR_SWS) != (uint32_t)0x08)\r
322     {\r
323     }\r
324   }\r
325   else\r
326   { /* If HSE fails to start-up, the application will have wrong clock \r
327          configuration. User can add here some code to deal with this error */\r
328   }  \r
329 }\r
330 \r
331 /**\r
332   * @}\r
333   */\r
334 \r
335 /**\r
336   * @}\r
337   */\r
338 \r
339 /**\r
340   * @}\r
341   */\r
342 \r
343 /******************* (C) COPYRIGHT 2012 STMicroelectronics *****END OF FILE****/\r