]> git.sur5r.net Git - freertos/blob - Demo/_Cortex_STM32L152_IAR/system_and_ST_code/system_stm32l1xx.c
First phase of changing the directory that starts Cortex, with one that starts CORTEX.
[freertos] / Demo / _Cortex_STM32L152_IAR / system_and_ST_code / system_stm32l1xx.c
1 /**\r
2   ******************************************************************************\r
3   * @file    system_stm32l1xx.c\r
4   * @author  MCD Application Team\r
5   * @version V1.0.0RC1\r
6   * @date    07/02/2010\r
7   * @brief   CMSIS Cortex-M3 Device Peripheral Access Layer System Source File.\r
8   ******************************************************************************\r
9   *\r
10   * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS\r
11   * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE\r
12   * TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY\r
13   * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING\r
14   * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE\r
15   * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.\r
16   *\r
17   * <h2><center>&copy; COPYRIGHT 2010 STMicroelectronics</center></h2>\r
18   ******************************************************************************\r
19   */\r
20 \r
21 /** @addtogroup CMSIS\r
22   * @{\r
23   */\r
24 \r
25 /** @addtogroup stm32l1xx_system\r
26   * @{\r
27   */\r
28 \r
29 /** @addtogroup STM32L1xx_System_Private_Includes\r
30   * @{\r
31   */\r
32 \r
33 #include "stm32l1xx.h"\r
34 \r
35 /**\r
36   * @}\r
37   */\r
38 \r
39 /** @addtogroup STM32L1xx_System_Private_TypesDefinitions\r
40   * @{\r
41   */\r
42 \r
43 /**\r
44   * @}\r
45   */\r
46 \r
47 /** @addtogroup STM32L1xx_System_Private_Defines\r
48   * @{\r
49   */\r
50 \r
51 /*!< Uncomment the line corresponding to the desired System clock (SYSCLK)\r
52    frequency (after reset the MSI is used as SYSCLK source)\r
53 \r
54    IMPORTANT NOTE:\r
55    ==============\r
56    1. After each device reset the MSI is used as System clock source.\r
57 \r
58    2. Please make sure that the selected System clock doesn't exceed your device's\r
59       maximum frequency.\r
60 \r
61    3. If none of the define below is enabled, the MSI (2MHz default) is used as\r
62       System clock source.\r
63 \r
64    4. The System clock configuration functions provided within this file assume that:\r
65         - For Ultra Low Power Medium Mensity devices an external 8MHz crystal is\r
66           used to drive the System clock.\r
67      If you are using different crystal you have to adapt those functions accordingly.\r
68     */\r
69 \r
70 /* #define SYSCLK_FREQ_MSI */\r
71 \r
72 #ifndef SYSCLK_FREQ_MSI\r
73 /* #define SYSCLK_FREQ_HSI    HSI_VALUE */\r
74 /* #define SYSCLK_FREQ_HSE    HSE_VALUE */\r
75 /* #define SYSCLK_FREQ_4MHz   4000000 */\r
76 /* #define SYSCLK_FREQ_8MHz   8000000 */\r
77 /* #define SYSCLK_FREQ_16MHz  16000000 */\r
78 #define SYSCLK_FREQ_32MHz  32000000\r
79 #else\r
80 /* #define SYSCLK_FREQ_MSI_64KHz     64000 */\r
81 /* #define SYSCLK_FREQ_MSI_128KHz    128000 */\r
82 /* #define SYSCLK_FREQ_MSI_256KHz    256000 */\r
83 /* #define SYSCLK_FREQ_MSI_512KHz    512000 */\r
84 /* #define SYSCLK_FREQ_MSI_1MHz      1000000 */\r
85 /* #define SYSCLK_FREQ_MSI_2MHz      2000000 */\r
86 /* #define SYSCLK_FREQ_MSI_4MHz      4000000 */\r
87 #endif\r
88 \r
89 /**\r
90   * @}\r
91   */\r
92 \r
93 /** @addtogroup STM32L1xx_System_Private_Macros\r
94   * @{\r
95   */\r
96 \r
97 /**\r
98   * @}\r
99   */\r
100 \r
101 /** @addtogroup STM32L1xx_System_Private_Variables\r
102   * @{\r
103   */\r
104 \r
105 /*******************************************************************************\r
106 *  Clock Definitions\r
107 *******************************************************************************/\r
108 #ifndef SYSCLK_FREQ_MSI\r
109 #ifdef SYSCLK_FREQ_HSI\r
110   uint32_t SystemCoreClock         = SYSCLK_FREQ_HSI;        /*!< System Clock Frequency (Core Clock) */\r
111 #elif defined SYSCLK_FREQ_HSE\r
112   uint32_t SystemCoreClock         = SYSCLK_FREQ_HSE;        /*!< System Clock Frequency (Core Clock) */\r
113 #elif defined SYSCLK_FREQ_4MHz\r
114   uint32_t SystemCoreClock         = SYSCLK_FREQ_4MHz;       /*!< System Clock Frequency (Core Clock) */\r
115 #elif defined SYSCLK_FREQ_8MHz\r
116   uint32_t SystemCoreClock         = SYSCLK_FREQ_8MHz;       /*!< System Clock Frequency (Core Clock) */\r
117 #elif defined SYSCLK_FREQ_16MHz\r
118   uint32_t SystemCoreClock         = SYSCLK_FREQ_16MHz;      /*!< System Clock Frequency (Core Clock) */\r
119 #elif defined SYSCLK_FREQ_32MHz\r
120   uint32_t SystemCoreClock         = SYSCLK_FREQ_32MHz;      /*!< System Clock Frequency (Core Clock) */\r
121 #else /*!< MSI Selected as System Clock source */\r
122   uint32_t SystemCoreClock         = MSI_VALUE;              /*!< System Clock Frequency (Core Clock) */\r
123 #endif\r
124 #else\r
125 #ifdef SYSCLK_FREQ_MSI_64KHz\r
126   uint32_t SystemCoreClock         = SYSCLK_FREQ_MSI_64KHz;  /*!< System Clock Frequency (Core Clock) */\r
127 #elif defined SYSCLK_FREQ_MSI_128KHz\r
128   uint32_t SystemCoreClock         = SYSCLK_FREQ_MSI_128KHz; /*!< System Clock Frequency (Core Clock) */\r
129 #elif defined SYSCLK_FREQ_MSI_256KHz\r
130   uint32_t SystemCoreClock         = SYSCLK_FREQ_MSI_256KHz; /*!< System Clock Frequency (Core Clock) */\r
131 #elif defined SYSCLK_FREQ_MSI_512KHz\r
132   uint32_t SystemCoreClock         = SYSCLK_FREQ_MSI_512KHz; /*!< System Clock Frequency (Core Clock) */\r
133 #elif defined SYSCLK_FREQ_MSI_1MHz\r
134   uint32_t SystemCoreClock         = SYSCLK_FREQ_MSI_1MHz;   /*!< System Clock Frequency (Core Clock) */\r
135 #elif defined SYSCLK_FREQ_MSI_2MHz\r
136   uint32_t SystemCoreClock         = SYSCLK_FREQ_MSI_2MHz;   /*!< System Clock Frequency (Core Clock) */\r
137 #elif defined SYSCLK_FREQ_MSI_4MHz\r
138   uint32_t SystemCoreClock         = SYSCLK_FREQ_MSI_4MHz;   /*!< System Clock Frequency (Core Clock) */\r
139 #else\r
140   uint32_t SystemCoreClock         = MSI_VALUE;              /*!< System Clock Frequency (Core Clock) */\r
141 #endif\r
142 #endif\r
143 \r
144 __I uint8_t PLLMulTable[9] = {3, 4, 6, 8, 12, 16, 24, 32, 48};\r
145 __I uint8_t MSITable[7] = {0, 0, 0, 0, 1, 2, 4};\r
146 __I uint8_t AHBPrescTable[16] = {0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 4, 6, 7, 8, 9};\r
147 \r
148 /**\r
149   * @}\r
150   */\r
151 \r
152 /** @addtogroup STM32L1xx_System_Private_FunctionPrototypes\r
153   * @{\r
154   */\r
155 \r
156 static void SetSysClock(void);\r
157 \r
158 #ifdef SYSCLK_FREQ_HSI\r
159   static void SetSysClockToHSI(void);\r
160 #elif defined SYSCLK_FREQ_HSE\r
161   static void SetSysClockToHSE(void);\r
162 #elif defined SYSCLK_FREQ_4MHz\r
163   static void SetSysClockTo4(void);\r
164 #elif defined SYSCLK_FREQ_8MHz\r
165   static void SetSysClockTo8(void);\r
166 #elif defined SYSCLK_FREQ_16MHz\r
167   static void SetSysClockTo16(void);\r
168 #elif defined SYSCLK_FREQ_32MHz\r
169   static void SetSysClockTo32(void);\r
170 #else\r
171   static void SetSysClockToMSI(void);\r
172 #endif\r
173 \r
174 /**\r
175   * @}\r
176   */\r
177 \r
178 /** @addtogroup STM32L1xx_System_Private_Functions\r
179   * @{\r
180   */\r
181 \r
182 /**\r
183   * @brief  Setup the microcontroller system\r
184   *         Initialize the Embedded Flash Interface, the PLL and update the\r
185   *         SystemCoreClock variable\r
186   * @note   This function should be used only after reset.\r
187   * @param  None\r
188   * @retval None\r
189   */\r
190 void SystemInit (void)\r
191 {\r
192   /*!< Set MSION bit */\r
193   RCC->CR |= (uint32_t)0x00000100;\r
194 \r
195   /*!< Reset SW[1:0], HPRE[3:0], PPRE1[2:0], PPRE2[2:0], MCOSEL[2:0] and MCOPRE[2:0] bits */\r
196   RCC->CFGR &= (uint32_t)0x88FFC00C;\r
197 \r
198   /*!< Reset HSION, HSEON, CSSON and PLLON bits */\r
199   RCC->CR &= (uint32_t)0xEEFEFFFE;\r
200 \r
201   /*!< Reset HSEBYP bit */\r
202   RCC->CR &= (uint32_t)0xFFFBFFFF;\r
203 \r
204   /*!< Reset PLLSRC, PLLMUL[3:0] and PLLDIV[1:0] bits */\r
205   RCC->CFGR &= (uint32_t)0xFF02FFFF;\r
206 \r
207   /*!< Disable all interrupts */\r
208   RCC->CIR = 0x00000000;\r
209 \r
210   /*!< Configure the System clock frequency, HCLK, PCLK2 and PCLK1 prescalers */\r
211   /*!< Configure the Flash Latency cycles and enable prefetch buffer */\r
212   SetSysClock();\r
213 \r
214 }\r
215 \r
216 /**\r
217   * @brief  Update SystemCoreClock according to Clock Register Values\r
218   * @note   None\r
219   * @param  None\r
220   * @retval None\r
221   */\r
222 void SystemCoreClockUpdate (void)\r
223 {\r
224   uint32_t tmp = 0, pllmul = 0, plldiv = 0, pllsource = 0, msirange = 0;\r
225 \r
226   /* Get SYSCLK source -------------------------------------------------------*/\r
227   tmp = RCC->CFGR & RCC_CFGR_SWS;\r
228 \r
229   switch (tmp)\r
230   {\r
231     case 0x00:  /* MSI used as system clock */\r
232       msirange = (RCC->ICSCR & RCC_ICSCR_MSIRANGE) >> 13;\r
233       SystemCoreClock = (((1 << msirange) * 64000) - (MSITable[msirange] * 24000));\r
234       break;\r
235     case 0x04:  /* HSI used as system clock */\r
236       SystemCoreClock = HSI_VALUE;\r
237       break;\r
238     case 0x08:  /* HSE used as system clock */\r
239       SystemCoreClock = HSE_VALUE;\r
240       break;\r
241     case 0x0C:  /* PLL used as system clock */\r
242       /* Get PLL clock source and multiplication factor ----------------------*/\r
243       pllmul = RCC->CFGR & RCC_CFGR_PLLMUL;\r
244       plldiv = RCC->CFGR & RCC_CFGR_PLLDIV;\r
245       pllmul = PLLMulTable[(pllmul >> 18)];\r
246       plldiv = (plldiv >> 22) + 1;\r
247 \r
248       pllsource = RCC->CFGR & RCC_CFGR_PLLSRC;\r
249 \r
250       if (pllsource == 0x00)\r
251       {\r
252         /* HSI oscillator clock selected as PLL clock entry */\r
253         SystemCoreClock = (((HSI_VALUE) * pllmul) / plldiv);\r
254       }\r
255       else\r
256       {\r
257         /* HSE selected as PLL clock entry */\r
258         SystemCoreClock = (((HSE_VALUE) * pllmul) / plldiv);\r
259       }\r
260       break;\r
261     default:\r
262       SystemCoreClock = MSI_VALUE;\r
263       break;\r
264   }\r
265   /* Compute HCLK clock frequency --------------------------------------------*/\r
266   /* Get HCLK prescaler */\r
267   tmp = AHBPrescTable[((RCC->CFGR & RCC_CFGR_HPRE) >> 4)];\r
268   /* HCLK clock frequency */\r
269   SystemCoreClock >>= tmp;\r
270 }\r
271 \r
272 /**\r
273   * @brief  Configures the System clock frequency, HCLK, PCLK2 and PCLK1 prescalers.\r
274   * @param  None\r
275   * @retval None\r
276   */\r
277 static void SetSysClock(void)\r
278 {\r
279 #ifdef SYSCLK_FREQ_HSI\r
280   SetSysClockToHSI();\r
281 #elif defined SYSCLK_FREQ_HSE\r
282   SetSysClockToHSE();\r
283 #elif defined SYSCLK_FREQ_4MHz\r
284   SetSysClockTo4();\r
285 #elif defined SYSCLK_FREQ_8MHz\r
286   SetSysClockTo8();\r
287 #elif defined SYSCLK_FREQ_16MHz\r
288   SetSysClockTo16();\r
289 #elif defined SYSCLK_FREQ_32MHz\r
290   SetSysClockTo32();\r
291 #else\r
292   SetSysClockToMSI();\r
293 #endif\r
294 \r
295  /* If none of the define above is enabled, the MSI (2MHz default) is used as\r
296     System clock source (default after reset) */\r
297 }\r
298 \r
299 #ifdef SYSCLK_FREQ_HSI\r
300 /**\r
301   * @brief  Selects HSI as System clock source and configure HCLK, PCLK2\r
302   *         and PCLK1 prescalers.\r
303   * @note   This function should be used only after reset.\r
304   * @param  None\r
305   * @retval None\r
306   */\r
307 static void SetSysClockToHSI(void)\r
308 {\r
309   __IO uint32_t StartUpCounter = 0, HSIStatus = 0;\r
310 \r
311   /* SYSCLK, HCLK, PCLK2 and PCLK1 configuration ---------------------------*/\r
312   /* Enable HSI */\r
313   RCC->CR |= ((uint32_t)RCC_CR_HSION);\r
314 \r
315   /* Wait till HSI is ready and if Time out is reached exit */\r
316   do\r
317   {\r
318     HSIStatus = RCC->CR & RCC_CR_HSIRDY;\r
319     StartUpCounter++;\r
320   } while((HSIStatus == 0) && (StartUpCounter != HSE_STARTUP_TIMEOUT));\r
321 \r
322   if ((RCC->CR & RCC_CR_HSIRDY) != RESET)\r
323   {\r
324     HSIStatus = (uint32_t)0x01;\r
325   }\r
326   else\r
327   {\r
328     HSIStatus = (uint32_t)0x00;\r
329   }\r
330 \r
331   if (HSIStatus == (uint32_t)0x01)\r
332   {\r
333     /* Enable 64-bit access */\r
334     FLASH->ACR |= FLASH_ACR_ACC64;\r
335 \r
336     /* Enable Prefetch Buffer */\r
337     FLASH->ACR |= FLASH_ACR_PRFTEN;\r
338 \r
339     /* Flash 1 wait state */\r
340     FLASH->ACR |= FLASH_ACR_LATENCY;\r
341 \r
342     /* Enable the PWR APB1 Clock */\r
343     RCC->APB1ENR |= RCC_APB1ENR_PWREN;\r
344 \r
345     /* Select the Voltage Range 1 (1.8V) */\r
346     PWR->CR = PWR_CR_VOS_0;\r
347 \r
348     /* Wait Until the Voltage Regulator is ready */\r
349     while((PWR->CSR & PWR_CSR_VOSF) != RESET)\r
350     {\r
351     }\r
352 \r
353     /* HCLK = SYSCLK */\r
354     RCC->CFGR |= (uint32_t)RCC_CFGR_HPRE_DIV1;\r
355 \r
356     /* PCLK2 = HCLK */\r
357     RCC->CFGR |= (uint32_t)RCC_CFGR_PPRE2_DIV1;\r
358 \r
359     /* PCLK1 = HCLK */\r
360     RCC->CFGR |= (uint32_t)RCC_CFGR_PPRE1_DIV1;\r
361 \r
362     /* Select HSI as system clock source */\r
363     RCC->CFGR &= (uint32_t)((uint32_t)~(RCC_CFGR_SW));\r
364     RCC->CFGR |= (uint32_t)RCC_CFGR_SW_HSI;\r
365 \r
366     /* Wait till HSI is used as system clock source */\r
367     while ((RCC->CFGR & (uint32_t)RCC_CFGR_SWS) != (uint32_t)0x04)\r
368     {\r
369     }\r
370   }\r
371   else\r
372   {\r
373     /* If HSI fails to start-up, the application will have wrong clock\r
374        configuration. User can add here some code to deal with this error */\r
375   }\r
376 }\r
377 \r
378 #elif defined SYSCLK_FREQ_HSE\r
379 /**\r
380   * @brief  Selects HSE as System clock source and configure HCLK, PCLK2\r
381   *         and PCLK1 prescalers.\r
382   * @note   This function should be used only after reset.\r
383   * @param  None\r
384   * @retval None\r
385   */\r
386 static void SetSysClockToHSE(void)\r
387 {\r
388   __IO uint32_t StartUpCounter = 0, HSEStatus = 0;\r
389 \r
390   /* SYSCLK, HCLK, PCLK2 and PCLK1 configuration ---------------------------*/\r
391   /* Enable HSE */\r
392   RCC->CR |= ((uint32_t)RCC_CR_HSEON);\r
393 \r
394   /* Wait till HSE is ready and if Time out is reached exit */\r
395   do\r
396   {\r
397     HSEStatus = RCC->CR & RCC_CR_HSERDY;\r
398     StartUpCounter++;\r
399   } while((HSEStatus == 0) && (StartUpCounter != HSE_STARTUP_TIMEOUT));\r
400 \r
401   if ((RCC->CR & RCC_CR_HSERDY) != RESET)\r
402   {\r
403     HSEStatus = (uint32_t)0x01;\r
404   }\r
405   else\r
406   {\r
407     HSEStatus = (uint32_t)0x00;\r
408   }\r
409 \r
410   if (HSEStatus == (uint32_t)0x01)\r
411   {\r
412     /* Flash 0 wait state */\r
413     FLASH->ACR &= ~FLASH_ACR_LATENCY;\r
414 \r
415     /* Disable Prefetch Buffer */\r
416     FLASH->ACR &= ~FLASH_ACR_PRFTEN;\r
417 \r
418     /* Disable 64-bit access */\r
419     FLASH->ACR &= ~FLASH_ACR_ACC64;\r
420 \r
421     /* Enable the PWR APB1 Clock */\r
422     RCC->APB1ENR |= RCC_APB1ENR_PWREN;\r
423 \r
424     /* Select the Voltage Range 2 (1.5V) */\r
425     PWR->CR = PWR_CR_VOS_1;\r
426 \r
427     /* Wait Until the Voltage Regulator is ready */\r
428     while((PWR->CSR & PWR_CSR_VOSF) != RESET)\r
429     {\r
430     }\r
431 \r
432     /* HCLK = SYSCLK */\r
433     RCC->CFGR |= (uint32_t)RCC_CFGR_HPRE_DIV1;\r
434 \r
435     /* PCLK2 = HCLK */\r
436     RCC->CFGR |= (uint32_t)RCC_CFGR_PPRE2_DIV1;\r
437 \r
438     /* PCLK1 = HCLK */\r
439     RCC->CFGR |= (uint32_t)RCC_CFGR_PPRE1_DIV1;\r
440 \r
441     /* Select HSE as system clock source */\r
442     RCC->CFGR &= (uint32_t)((uint32_t)~(RCC_CFGR_SW));\r
443     RCC->CFGR |= (uint32_t)RCC_CFGR_SW_HSE;\r
444 \r
445     /* Wait till HSE is used as system clock source */\r
446     while ((RCC->CFGR & (uint32_t)RCC_CFGR_SWS) != (uint32_t)0x08)\r
447     {\r
448     }\r
449   }\r
450   else\r
451   {\r
452     /* If HSE fails to start-up, the application will have wrong clock\r
453        configuration. User can add here some code to deal with this error */\r
454   }\r
455 }\r
456 #elif defined SYSCLK_FREQ_4MHz\r
457 /**\r
458   * @brief  Sets System clock frequency to 4MHz and configure HCLK, PCLK2\r
459   *         and PCLK1 prescalers.\r
460   * @note   This function should be used only after reset.\r
461   * @param  None\r
462   * @retval None\r
463   */\r
464 static void SetSysClockTo4(void)\r
465 {\r
466   __IO uint32_t StartUpCounter = 0, HSEStatus = 0;\r
467 \r
468   /* SYSCLK, HCLK, PCLK2 and PCLK1 configuration ---------------------------*/\r
469   /* Enable HSE */\r
470   RCC->CR |= ((uint32_t)RCC_CR_HSEON);\r
471 \r
472   /* Wait till HSE is ready and if Time out is reached exit */\r
473   do\r
474   {\r
475     HSEStatus = RCC->CR & RCC_CR_HSERDY;\r
476     StartUpCounter++;\r
477   } while((HSEStatus == 0) && (StartUpCounter != HSE_STARTUP_TIMEOUT));\r
478 \r
479   if ((RCC->CR & RCC_CR_HSERDY) != RESET)\r
480   {\r
481     HSEStatus = (uint32_t)0x01;\r
482   }\r
483   else\r
484   {\r
485     HSEStatus = (uint32_t)0x00;\r
486   }\r
487 \r
488   if (HSEStatus == (uint32_t)0x01)\r
489   {\r
490     /* Flash 0 wait state */\r
491     FLASH->ACR &= ~FLASH_ACR_LATENCY;\r
492 \r
493     /* Disable Prefetch Buffer */\r
494     FLASH->ACR &= ~FLASH_ACR_PRFTEN;\r
495 \r
496     /* Disable 64-bit access */\r
497     FLASH->ACR &= ~FLASH_ACR_ACC64;\r
498 \r
499     /* Enable the PWR APB1 Clock */\r
500     RCC->APB1ENR |= RCC_APB1ENR_PWREN;\r
501 \r
502     /* Select the Voltage Range 2 (1.5V) */\r
503     PWR->CR = PWR_CR_VOS_1;\r
504 \r
505     /* Wait Until the Voltage Regulator is ready */\r
506     while((PWR->CSR & PWR_CSR_VOSF) != RESET)\r
507     {\r
508     }\r
509 \r
510     /* HCLK = SYSCLK */\r
511     RCC->CFGR |= (uint32_t)RCC_CFGR_HPRE_DIV2;\r
512 \r
513     /* PCLK2 = HCLK */\r
514     RCC->CFGR |= (uint32_t)RCC_CFGR_PPRE2_DIV1;\r
515 \r
516     /* PCLK1 = HCLK */\r
517     RCC->CFGR |= (uint32_t)RCC_CFGR_PPRE1_DIV1;\r
518 \r
519     /* Select HSE as system clock source */\r
520     RCC->CFGR &= (uint32_t)((uint32_t)~(RCC_CFGR_SW));\r
521     RCC->CFGR |= (uint32_t)RCC_CFGR_SW_HSE;\r
522 \r
523     /* Wait till HSE is used as system clock source */\r
524     while ((RCC->CFGR & (uint32_t)RCC_CFGR_SWS) != (uint32_t)0x08)\r
525     {\r
526     }\r
527   }\r
528   else\r
529   {\r
530     /* If HSE fails to start-up, the application will have wrong clock\r
531        configuration. User can add here some code to deal with this error */\r
532   }\r
533 }\r
534 \r
535 #elif defined SYSCLK_FREQ_8MHz\r
536 /**\r
537   * @brief  Sets System clock frequency to 8MHz and configure HCLK, PCLK2\r
538   *         and PCLK1 prescalers.\r
539   * @note   This function should be used only after reset.\r
540   * @param  None\r
541   * @retval None\r
542   */\r
543 static void SetSysClockTo8(void)\r
544 {\r
545   __IO uint32_t StartUpCounter = 0, HSEStatus = 0;\r
546 \r
547   /* SYSCLK, HCLK, PCLK2 and PCLK1 configuration ---------------------------*/\r
548   /* Enable HSE */\r
549   RCC->CR |= ((uint32_t)RCC_CR_HSEON);\r
550 \r
551   /* Wait till HSE is ready and if Time out is reached exit */\r
552   do\r
553   {\r
554     HSEStatus = RCC->CR & RCC_CR_HSERDY;\r
555     StartUpCounter++;\r
556   } while((HSEStatus == 0) && (StartUpCounter != HSE_STARTUP_TIMEOUT));\r
557 \r
558   if ((RCC->CR & RCC_CR_HSERDY) != RESET)\r
559   {\r
560     HSEStatus = (uint32_t)0x01;\r
561   }\r
562   else\r
563   {\r
564     HSEStatus = (uint32_t)0x00;\r
565   }\r
566 \r
567   if (HSEStatus == (uint32_t)0x01)\r
568   {\r
569     /* Flash 0 wait state */\r
570     FLASH->ACR &= ~FLASH_ACR_LATENCY;\r
571 \r
572     /* Disable Prefetch Buffer */\r
573     FLASH->ACR &= ~FLASH_ACR_PRFTEN;\r
574 \r
575     /* Disable 64-bit access */\r
576     FLASH->ACR &= ~FLASH_ACR_ACC64;\r
577 \r
578     /* Enable the PWR APB1 Clock */\r
579     RCC->APB1ENR |= RCC_APB1ENR_PWREN;\r
580 \r
581     /* Select the Voltage Range 2 (1.5V) */\r
582     PWR->CR = PWR_CR_VOS_1;\r
583 \r
584     /* Wait Until the Voltage Regulator is ready */\r
585     while((PWR->CSR & PWR_CSR_VOSF) != RESET)\r
586     {\r
587     }\r
588 \r
589     /* HCLK = SYSCLK */\r
590     RCC->CFGR |= (uint32_t)RCC_CFGR_HPRE_DIV1;\r
591 \r
592     /* PCLK2 = HCLK */\r
593     RCC->CFGR |= (uint32_t)RCC_CFGR_PPRE2_DIV1;\r
594 \r
595     /* PCLK1 = HCLK */\r
596     RCC->CFGR |= (uint32_t)RCC_CFGR_PPRE1_DIV1;\r
597 \r
598     /* Select HSE as system clock source */\r
599     RCC->CFGR &= (uint32_t)((uint32_t)~(RCC_CFGR_SW));\r
600     RCC->CFGR |= (uint32_t)RCC_CFGR_SW_HSE;\r
601 \r
602     /* Wait till HSE is used as system clock source */\r
603     while ((RCC->CFGR & (uint32_t)RCC_CFGR_SWS) != (uint32_t)0x08)\r
604     {\r
605     }\r
606   }\r
607   else\r
608   {\r
609     /* If HSE fails to start-up, the application will have wrong clock\r
610        configuration. User can add here some code to deal with this error */\r
611   }\r
612 }\r
613 \r
614 #elif defined SYSCLK_FREQ_16MHz\r
615 /**\r
616   * @brief  Sets System clock frequency to 16MHz and configure HCLK, PCLK2\r
617   *         and PCLK1 prescalers.\r
618   * @note   This function should be used only after reset.\r
619   * @param  None\r
620   * @retval None\r
621   */\r
622 static void SetSysClockTo16(void)\r
623 {\r
624   __IO uint32_t StartUpCounter = 0, HSEStatus = 0;\r
625 \r
626   /* SYSCLK, HCLK, PCLK2 and PCLK1 configuration ---------------------------*/\r
627   /* Enable HSE */\r
628   RCC->CR |= ((uint32_t)RCC_CR_HSEON);\r
629 \r
630   /* Wait till HSE is ready and if Time out is reached exit */\r
631   do\r
632   {\r
633     HSEStatus = RCC->CR & RCC_CR_HSERDY;\r
634     StartUpCounter++;\r
635   } while((HSEStatus == 0) && (StartUpCounter != HSE_STARTUP_TIMEOUT));\r
636 \r
637   if ((RCC->CR & RCC_CR_HSERDY) != RESET)\r
638   {\r
639     HSEStatus = (uint32_t)0x01;\r
640   }\r
641   else\r
642   {\r
643     HSEStatus = (uint32_t)0x00;\r
644   }\r
645 \r
646   if (HSEStatus == (uint32_t)0x01)\r
647   {\r
648     /* Enable 64-bit access */\r
649     FLASH->ACR |= FLASH_ACR_ACC64;\r
650 \r
651     /* Enable Prefetch Buffer */\r
652     FLASH->ACR |= FLASH_ACR_PRFTEN;\r
653 \r
654     /* Flash 1 wait state */\r
655     FLASH->ACR |= FLASH_ACR_LATENCY;\r
656 \r
657     /* Enable the PWR APB1 Clock */\r
658     RCC->APB1ENR |= RCC_APB1ENR_PWREN;\r
659 \r
660     /* Select the Voltage Range 2 (1.5V) */\r
661     PWR->CR = PWR_CR_VOS_1;\r
662 \r
663     /* Wait Until the Voltage Regulator is ready */\r
664     while((PWR->CSR & PWR_CSR_VOSF) != RESET)\r
665     {\r
666     }\r
667 \r
668     /* HCLK = SYSCLK */\r
669     RCC->CFGR |= (uint32_t)RCC_CFGR_HPRE_DIV2;\r
670 \r
671     /* PCLK2 = HCLK */\r
672     RCC->CFGR |= (uint32_t)RCC_CFGR_PPRE2_DIV1;\r
673 \r
674     /* PCLK1 = HCLK */\r
675     RCC->CFGR |= (uint32_t)RCC_CFGR_PPRE1_DIV1;\r
676 \r
677     /*  PLL configuration: PLLCLK = (HSE * 12) / 3 = 32MHz */\r
678     RCC->CFGR &= (uint32_t)((uint32_t)~(RCC_CFGR_PLLSRC | RCC_CFGR_PLLMUL |\r
679                                         RCC_CFGR_PLLDIV));\r
680     RCC->CFGR |= (uint32_t)(RCC_CFGR_PLLSRC_HSE | RCC_CFGR_PLLMUL12 | RCC_CFGR_PLLDIV3);\r
681 \r
682     /* Enable PLL */\r
683     RCC->CR |= RCC_CR_PLLON;\r
684 \r
685     /* Wait till PLL is ready */\r
686     while((RCC->CR & RCC_CR_PLLRDY) == 0)\r
687     {\r
688     }\r
689 \r
690     /* Select PLL as system clock source */\r
691     RCC->CFGR &= (uint32_t)((uint32_t)~(RCC_CFGR_SW));\r
692     RCC->CFGR |= (uint32_t)RCC_CFGR_SW_PLL;\r
693 \r
694     /* Wait till PLL is used as system clock source */\r
695     while ((RCC->CFGR & (uint32_t)RCC_CFGR_SWS) != (uint32_t)0x0C)\r
696     {\r
697     }\r
698   }\r
699   else\r
700   {\r
701     /* If HSE fails to start-up, the application will have wrong clock\r
702        configuration. User can add here some code to deal with this error */\r
703   }\r
704 }\r
705 \r
706 #elif defined SYSCLK_FREQ_32MHz\r
707 /**\r
708   * @brief  Sets System clock frequency to 32MHz and configure HCLK, PCLK2\r
709   *         and PCLK1 prescalers.\r
710   * @note   This function should be used only after reset.\r
711   * @param  None\r
712   * @retval None\r
713   */\r
714 static void SetSysClockTo32(void)\r
715 {\r
716   __IO uint32_t StartUpCounter = 0, HSEStatus = 0;\r
717 \r
718   /* SYSCLK, HCLK, PCLK2 and PCLK1 configuration ---------------------------*/\r
719   /* Enable HSE */\r
720   RCC->CR |= ((uint32_t)RCC_CR_HSEON);\r
721 \r
722   /* Wait till HSE is ready and if Time out is reached exit */\r
723   do\r
724   {\r
725     HSEStatus = RCC->CR & RCC_CR_HSERDY;\r
726     StartUpCounter++;\r
727   } while((HSEStatus == 0) && (StartUpCounter != HSE_STARTUP_TIMEOUT));\r
728 \r
729   if ((RCC->CR & RCC_CR_HSERDY) != RESET)\r
730   {\r
731     HSEStatus = (uint32_t)0x01;\r
732   }\r
733   else\r
734   {\r
735     HSEStatus = (uint32_t)0x00;\r
736   }\r
737 \r
738   if (HSEStatus == (uint32_t)0x01)\r
739   {\r
740     /* Enable 64-bit access */\r
741     FLASH->ACR |= FLASH_ACR_ACC64;\r
742 \r
743     /* Enable Prefetch Buffer */\r
744     FLASH->ACR |= FLASH_ACR_PRFTEN;\r
745 \r
746     /* Flash 1 wait state */\r
747     FLASH->ACR |= FLASH_ACR_LATENCY;\r
748 \r
749     /* Enable the PWR APB1 Clock */\r
750     RCC->APB1ENR |= RCC_APB1ENR_PWREN;\r
751 \r
752     /* Select the Voltage Range 1 (1.8V) */\r
753     PWR->CR = PWR_CR_VOS_0;\r
754 \r
755     /* Wait Until the Voltage Regulator is ready */\r
756     while((PWR->CSR & PWR_CSR_VOSF) != RESET)\r
757     {\r
758     }\r
759 \r
760     /* HCLK = SYSCLK */\r
761     RCC->CFGR |= (uint32_t)RCC_CFGR_HPRE_DIV1;\r
762 \r
763     /* PCLK2 = HCLK */\r
764     RCC->CFGR |= (uint32_t)RCC_CFGR_PPRE2_DIV1;\r
765 \r
766     /* PCLK1 = HCLK */\r
767     RCC->CFGR |= (uint32_t)RCC_CFGR_PPRE1_DIV1;\r
768 \r
769     /*  PLL configuration: PLLCLK = (HSE * 12) / 3 = 32MHz */\r
770     RCC->CFGR &= (uint32_t)((uint32_t)~(RCC_CFGR_PLLSRC | RCC_CFGR_PLLMUL |\r
771                                         RCC_CFGR_PLLDIV));\r
772     RCC->CFGR |= (uint32_t)(RCC_CFGR_PLLSRC_HSE | RCC_CFGR_PLLMUL12 | RCC_CFGR_PLLDIV3);\r
773 \r
774     /* Enable PLL */\r
775     RCC->CR |= RCC_CR_PLLON;\r
776 \r
777     /* Wait till PLL is ready */\r
778     while((RCC->CR & RCC_CR_PLLRDY) == 0)\r
779     {\r
780     }\r
781 \r
782     /* Select PLL as system clock source */\r
783     RCC->CFGR &= (uint32_t)((uint32_t)~(RCC_CFGR_SW));\r
784     RCC->CFGR |= (uint32_t)RCC_CFGR_SW_PLL;\r
785 \r
786     /* Wait till PLL is used as system clock source */\r
787     while ((RCC->CFGR & (uint32_t)RCC_CFGR_SWS) != (uint32_t)0x0C)\r
788     {\r
789     }\r
790   }\r
791   else\r
792   {\r
793     /* If HSE fails to start-up, the application will have wrong clock\r
794        configuration. User can add here some code to deal with this error */\r
795   }\r
796 }\r
797 \r
798 #else\r
799 /**\r
800   * @brief  Selects MSI as System clock source and configure HCLK, PCLK2\r
801   *         and PCLK1 prescalers.\r
802   * @note   This function should be used only after reset.\r
803   * @param  None\r
804   * @retval None\r
805   */\r
806 static void SetSysClockToMSI(void)\r
807 {\r
808   __IO uint32_t StartUpCounter = 0, MSIStatus = 0;\r
809 \r
810   /* SYSCLK, HCLK, PCLK2 and PCLK1 configuration ---------------------------*/\r
811   /* Enable MSI */\r
812   RCC->CR |= ((uint32_t)RCC_CR_MSION);\r
813 \r
814   /* Wait till MSI is ready and if Time out is reached exit */\r
815   do\r
816   {\r
817     MSIStatus = RCC->CR & RCC_CR_MSIRDY;\r
818     StartUpCounter++;\r
819   } while((MSIStatus == 0) && (StartUpCounter != HSE_STARTUP_TIMEOUT));\r
820 \r
821   if ((RCC->CR & RCC_CR_MSIRDY) != RESET)\r
822   {\r
823     MSIStatus = (uint32_t)0x01;\r
824   }\r
825   else\r
826   {\r
827     MSIStatus = (uint32_t)0x00;\r
828   }\r
829 \r
830   if (MSIStatus == (uint32_t)0x01)\r
831   {\r
832 #ifdef SYSCLK_FREQ_MSI\r
833 #ifdef SYSCLK_FREQ_MSI_4MHz\r
834     /* Enable 64-bit access */\r
835     FLASH->ACR |= FLASH_ACR_ACC64;\r
836 \r
837     /* Enable Prefetch Buffer */\r
838     FLASH->ACR |= FLASH_ACR_PRFTEN;\r
839 \r
840     /* Flash 1 wait state */\r
841     FLASH->ACR |= FLASH_ACR_LATENCY;\r
842 #else\r
843     /* Flash 0 wait state */\r
844     FLASH->ACR &= ~FLASH_ACR_LATENCY;\r
845 \r
846     /* Disable Prefetch Buffer */\r
847     FLASH->ACR &= ~FLASH_ACR_PRFTEN;\r
848 \r
849     /* Disable 64-bit access */\r
850     FLASH->ACR &= ~FLASH_ACR_ACC64;\r
851 #endif\r
852 #endif\r
853     /* Enable the PWR APB1 Clock */\r
854     RCC->APB1ENR |= RCC_APB1ENR_PWREN;\r
855 \r
856     /* Select the Voltage Range 3 (1.2V) */\r
857     PWR->CR = PWR_CR_VOS;\r
858 \r
859     /* Wait Until the Voltage Regulator is ready */\r
860     while((PWR->CSR & PWR_CSR_VOSF) != RESET)\r
861     {\r
862     }\r
863 \r
864     /* HCLK = SYSCLK */\r
865     RCC->CFGR |= (uint32_t)RCC_CFGR_HPRE_DIV1;\r
866 \r
867     /* PCLK2 = HCLK */\r
868     RCC->CFGR |= (uint32_t)RCC_CFGR_PPRE2_DIV1;\r
869 \r
870     /* PCLK1 = HCLK */\r
871     RCC->CFGR |= (uint32_t)RCC_CFGR_PPRE1_DIV1;\r
872 \r
873 #ifdef SYSCLK_FREQ_MSI\r
874   #ifdef SYSCLK_FREQ_MSI_64KHz\r
875     /* Set MSI clock range */\r
876     RCC->ICSCR &= (uint32_t)((uint32_t)~(RCC_ICSCR_MSIRANGE));\r
877     RCC->ICSCR |= (uint32_t)RCC_ICSCR_MSIRANGE_64KHz;\r
878   #elif defined SYSCLK_FREQ_MSI_128KHz\r
879     /* Set MSI clock range */\r
880     RCC->ICSCR &= (uint32_t)((uint32_t)~(RCC_ICSCR_MSIRANGE));\r
881     RCC->ICSCR |= (uint32_t)RCC_ICSCR_MSIRANGE_128KHz;\r
882   #elif defined SYSCLK_FREQ_MSI_256KHz\r
883     /* Set MSI clock range */\r
884     RCC->ICSCR &= (uint32_t)((uint32_t)~(RCC_ICSCR_MSIRANGE));\r
885     RCC->ICSCR |= (uint32_t)RCC_ICSCR_MSIRANGE_256KHz;\r
886   #elif defined SYSCLK_FREQ_MSI_512KHz\r
887     /* Set MSI clock range */\r
888     RCC->ICSCR &= (uint32_t)((uint32_t)~(RCC_ICSCR_MSIRANGE));\r
889     RCC->ICSCR |= (uint32_t)RCC_ICSCR_MSIRANGE_512KHz;\r
890   #elif defined SYSCLK_FREQ_MSI_1MHz\r
891     /* Set MSI clock range */\r
892     RCC->ICSCR &= (uint32_t)((uint32_t)~(RCC_ICSCR_MSIRANGE));\r
893     RCC->ICSCR |= (uint32_t)RCC_ICSCR_MSIRANGE_1MHz;\r
894   #elif defined SYSCLK_FREQ_MSI_2MHz\r
895     /* Set MSI clock range */\r
896     RCC->ICSCR &= (uint32_t)((uint32_t)~(RCC_ICSCR_MSIRANGE));\r
897     RCC->ICSCR |= (uint32_t)RCC_ICSCR_MSIRANGE_2MHz;\r
898   #elif defined SYSCLK_FREQ_MSI_4MHz\r
899     /* Set MSI clock range */\r
900     RCC->ICSCR &= (uint32_t)((uint32_t)~(RCC_ICSCR_MSIRANGE));\r
901     RCC->ICSCR |= (uint32_t)RCC_ICSCR_MSIRANGE_4MHz;\r
902   #endif\r
903 #endif\r
904 \r
905     /* Select MSI as system clock source */\r
906     RCC->CFGR &= (uint32_t)((uint32_t)~(RCC_CFGR_SW));\r
907     RCC->CFGR |= (uint32_t)RCC_CFGR_SW_MSI;\r
908 \r
909     /* Wait till MSI is used as system clock source */\r
910     while ((RCC->CFGR & (uint32_t)RCC_CFGR_SWS) != (uint32_t)0x00)\r
911     {\r
912     }\r
913   }\r
914   else\r
915   {\r
916     /* If MSI fails to start-up, the application will have wrong clock\r
917        configuration. User can add here some code to deal with this error */\r
918   }\r
919 }\r
920 #endif\r
921 \r
922 /**\r
923   * @}\r
924   */\r
925 \r
926 /**\r
927   * @}\r
928   */\r
929 \r
930 /**\r
931   * @}\r
932   */\r
933 \r
934 /******************* (C) COPYRIGHT 2010 STMicroelectronics *****END OF FILE****/\r