]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/CORTEX_STM32L152_Discovery_IAR/ST_Code/Libraries/STM32L1xx_StdPeriph_Driver/src/stm32l1xx_rcc.c
Add STM32L Discovery board project as a starting point to adapt to an RTOS demo.
[freertos] / FreeRTOS / Demo / CORTEX_STM32L152_Discovery_IAR / ST_Code / Libraries / STM32L1xx_StdPeriph_Driver / src / stm32l1xx_rcc.c
1 /**\r
2   ******************************************************************************\r
3   * @file    stm32l1xx_rcc.c\r
4   * @author  MCD Application Team\r
5   * @version V1.1.1\r
6   * @date    05-March-2012\r
7   * @brief   This file provides firmware functions to manage the following \r
8   *          functionalities of the Reset and clock control (RCC) peripheral:           \r
9   *           + Internal/external clocks, PLL, CSS and MCO configuration\r
10   *           + System, AHB and APB busses clocks configuration\r
11   *           + Peripheral clocks configuration\r
12   *           + Interrupts and flags management\r
13   *\r
14  @verbatim\r
15 \r
16  ===============================================================================\r
17                         ##### RCC specific features #####\r
18  ===============================================================================\r
19     [..] After reset the device is running from MSI (2 MHz) with Flash 0 WS, \r
20          all peripherals are off except internal SRAM, Flash and JTAG.\r
21          (#) There is no prescaler on High speed (AHB) and Low speed (APB) busses;\r
22              all peripherals mapped on these busses are running at MSI speed.\r
23          (#) The clock for all peripherals is switched off, except the SRAM and \r
24              FLASH.\r
25          (#) All GPIOs are in input floating state, except the JTAG pins which\r
26              are assigned to be used for debug purpose.\r
27     [..] Once the device started from reset, the user application has to:\r
28          (#) Configure the clock source to be used to drive the System clock\r
29              (if the application needs higher frequency/performance)\r
30          (#) Configure the System clock frequency and Flash settings\r
31          (#) Configure the AHB and APB busses prescalers\r
32          (#) Enable the clock for the peripheral(s) to be used\r
33          (#) Configure the clock source(s) for peripherals whose clocks are not\r
34              derived from the System clock (ADC, RTC/LCD and IWDG)\r
35 \r
36  @endverbatim\r
37   \r
38   ******************************************************************************\r
39   * @attention\r
40   *\r
41   * <h2><center>&copy; COPYRIGHT 2012 STMicroelectronics</center></h2>\r
42   *\r
43   * Licensed under MCD-ST Liberty SW License Agreement V2, (the "License");\r
44   * You may not use this file except in compliance with the License.\r
45   * You may obtain a copy of the License at:\r
46   *\r
47   *        http://www.st.com/software_license_agreement_liberty_v2\r
48   *\r
49   * Unless required by applicable law or agreed to in writing, software \r
50   * distributed under the License is distributed on an "AS IS" BASIS, \r
51   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
52   * See the License for the specific language governing permissions and\r
53   * limitations under the License.\r
54   *\r
55   ******************************************************************************\r
56   */\r
57 \r
58 /* Includes ------------------------------------------------------------------*/\r
59 #include "stm32l1xx_rcc.h"\r
60 \r
61 /** @addtogroup STM32L1xx_StdPeriph_Driver\r
62   * @{\r
63   */\r
64 \r
65 /** @defgroup RCC \r
66   * @brief RCC driver modules\r
67   * @{\r
68   */ \r
69 \r
70 /* Private typedef -----------------------------------------------------------*/\r
71 /* Private define ------------------------------------------------------------*/\r
72 \r
73 /* ------------ RCC registers bit address in the alias region ----------- */\r
74 #define RCC_OFFSET                (RCC_BASE - PERIPH_BASE)\r
75 \r
76 /* --- CR Register ---*/\r
77 \r
78 /* Alias word address of HSION bit */\r
79 #define CR_OFFSET                 (RCC_OFFSET + 0x00)\r
80 #define HSION_BitNumber           0x00\r
81 #define CR_HSION_BB               (PERIPH_BB_BASE + (CR_OFFSET * 32) + (HSION_BitNumber * 4))\r
82 \r
83 /* Alias word address of MSION bit */\r
84 #define MSION_BitNumber           0x08\r
85 #define CR_MSION_BB               (PERIPH_BB_BASE + (CR_OFFSET * 32) + (MSION_BitNumber * 4))\r
86 \r
87 /* Alias word address of PLLON bit */\r
88 #define PLLON_BitNumber           0x18\r
89 #define CR_PLLON_BB               (PERIPH_BB_BASE + (CR_OFFSET * 32) + (PLLON_BitNumber * 4))\r
90 \r
91 /* Alias word address of CSSON bit */\r
92 #define CSSON_BitNumber           0x1C\r
93 #define CR_CSSON_BB               (PERIPH_BB_BASE + (CR_OFFSET * 32) + (CSSON_BitNumber * 4))\r
94 \r
95 /* --- CSR Register ---*/\r
96 \r
97 /* Alias word address of LSION bit */\r
98 #define CSR_OFFSET                (RCC_OFFSET + 0x34)\r
99 #define LSION_BitNumber           0x00\r
100 #define CSR_LSION_BB              (PERIPH_BB_BASE + (CSR_OFFSET * 32) + (LSION_BitNumber * 4))\r
101 \r
102 /* Alias word address of LSECSSON bit */\r
103 #define LSECSSON_BitNumber        0x0B\r
104 #define CSR_LSECSSON_BB          (PERIPH_BB_BASE + (CSR_OFFSET * 32) + (LSECSSON_BitNumber * 4))\r
105 \r
106 /* Alias word address of RTCEN bit */\r
107 #define RTCEN_BitNumber           0x16\r
108 #define CSR_RTCEN_BB              (PERIPH_BB_BASE + (CSR_OFFSET * 32) + (RTCEN_BitNumber * 4))\r
109 \r
110 /* Alias word address of RTCRST bit */\r
111 #define RTCRST_BitNumber          0x17\r
112 #define CSR_RTCRST_BB             (PERIPH_BB_BASE + (CSR_OFFSET * 32) + (RTCRST_BitNumber * 4))\r
113 \r
114 \r
115 /* ---------------------- RCC registers mask -------------------------------- */\r
116 /* RCC Flag Mask */\r
117 #define FLAG_MASK                 ((uint8_t)0x1F)\r
118 \r
119 /* CR register byte 3 (Bits[23:16]) base address */\r
120 #define CR_BYTE3_ADDRESS          ((uint32_t)0x40023802)\r
121 \r
122 /* ICSCR register byte 4 (Bits[31:24]) base address */\r
123 #define ICSCR_BYTE4_ADDRESS       ((uint32_t)0x40023807)\r
124 \r
125 /* CFGR register byte 3 (Bits[23:16]) base address */\r
126 #define CFGR_BYTE3_ADDRESS        ((uint32_t)0x4002380A)\r
127 \r
128 /* CFGR register byte 4 (Bits[31:24]) base address */\r
129 #define CFGR_BYTE4_ADDRESS        ((uint32_t)0x4002380B)\r
130 \r
131 /* CIR register byte 2 (Bits[15:8]) base address */\r
132 #define CIR_BYTE2_ADDRESS         ((uint32_t)0x4002380D)\r
133 \r
134 /* CIR register byte 3 (Bits[23:16]) base address */\r
135 #define CIR_BYTE3_ADDRESS         ((uint32_t)0x4002380E)\r
136 \r
137 /* CSR register byte 2 (Bits[15:8]) base address */\r
138 #define CSR_BYTE2_ADDRESS         ((uint32_t)0x40023835)\r
139 \r
140 /* Private macro -------------------------------------------------------------*/\r
141 /* Private variables ---------------------------------------------------------*/\r
142 \r
143 static __I uint8_t PLLMulTable[9] = {3, 4, 6, 8, 12, 16, 24, 32, 48};\r
144 static __I uint8_t APBAHBPrescTable[16] = {0, 0, 0, 0, 1, 2, 3, 4, 1, 2, 3, 4, 6, 7, 8, 9};\r
145 \r
146 /* Private function prototypes -----------------------------------------------*/\r
147 /* Private functions ---------------------------------------------------------*/\r
148 \r
149 /** @defgroup RCC_Private_Functions\r
150   * @{\r
151   */\r
152 \r
153 /** @defgroup RCC_Group1 Internal and external clocks, PLL, CSS and MCO configuration functions\r
154  *  @brief   Internal and external clocks, PLL, CSS and MCO configuration functions \r
155  *\r
156 @verbatim\r
157  ===============================================================================\r
158  ##### Internal-external clocks, PLL, CSS and MCO configuration functions #####\r
159  ===============================================================================\r
160     [..] This section provide functions allowing to configure the internal/external \r
161          clocks, PLL, CSS and MCO.\r
162          (#) HSI (high-speed internal), 16 MHz factory-trimmed RC used directly \r
163              or through the PLL as System clock source.\r
164          (#) MSI (multi-speed internal), multispeed low power RC \r
165              (65.536 KHz to 4.194 MHz) MHz used as System clock source.\r
166          (#) LSI (low-speed internal), 37 KHz low consumption RC used as IWDG \r
167              and/or RTC clock source.\r
168          (#) HSE (high-speed external), 1 to 24 MHz crystal oscillator used \r
169              directly or through the PLL as System clock source. Can be used \r
170              also as RTC clock source.\r
171          (#) LSE (low-speed external), 32 KHz oscillator used as RTC clock source.\r
172          (#) PLL (clocked by HSI or HSE), for System clock and USB (48 MHz).\r
173          (#) CSS (Clock security system), once enable and if a HSE clock failure \r
174              occurs (HSE used directly or through PLL as System clock source), \r
175              the System clock is automatically switched to MSI and an interrupt \r
176              is generated if enabled. \r
177              The interrupt is linked to the Cortex-M3 NMI (Non-Maskable Interrupt) \r
178              exception vector.\r
179          (#) MCO (microcontroller clock output), used to output SYSCLK, HSI, MSI, \r
180              HSE, PLL, LSI or LSE clock (through a configurable prescaler) on \r
181              PA8 pin.\r
182 \r
183 @endverbatim\r
184   * @{\r
185   */\r
186 \r
187 /**\r
188   * @brief  Resets the RCC clock configuration to the default reset state.\r
189   * @note   The default reset state of the clock configuration is given below:\r
190   * @note      MSI ON and used as system clock source (MSI range is not modified\r
191   *            by this function, it keep the value configured by user application)\r
192   * @note      HSI, HSE and PLL OFF\r
193   * @note      AHB, APB1 and APB2 prescaler set to 1.\r
194   * @note      CSS and MCO OFF\r
195   * @note      All interrupts disabled\r
196   * @note    However, this function doesn't modify the configuration of the\r
197   * @note      Peripheral clocks\r
198   * @note      LSI, LSE and RTC clocks                  \r
199   * @param  None\r
200   * @retval None\r
201   */\r
202 void RCC_DeInit(void)\r
203 {\r
204   \r
205   /* Set MSION bit */\r
206   RCC->CR |= (uint32_t)0x00000100;\r
207 \r
208   /* Reset SW[1:0], HPRE[3:0], PPRE1[2:0], PPRE2[2:0], MCOSEL[2:0] and MCOPRE[2:0] bits */\r
209   RCC->CFGR &= (uint32_t)0x88FFC00C;\r
210   \r
211   /* Reset HSION, HSEON, CSSON and PLLON bits */\r
212   RCC->CR &= (uint32_t)0xEEFEFFFE;\r
213 \r
214   /* Reset HSEBYP bit */\r
215   RCC->CR &= (uint32_t)0xFFFBFFFF;\r
216 \r
217   /* Reset PLLSRC, PLLMUL[3:0] and PLLDIV[1:0] bits */\r
218   RCC->CFGR &= (uint32_t)0xFF02FFFF;\r
219 \r
220   /* Disable all interrupts */\r
221   RCC->CIR = 0x00000000;\r
222 }\r
223 \r
224 /**\r
225   * @brief  Configures the External High Speed oscillator (HSE).\r
226   * @note    After enabling the HSE (RCC_HSE_ON or RCC_HSE_Bypass), the application\r
227   *           software should wait on HSERDY flag to be set indicating that HSE clock\r
228   *           is stable and can be used to clock the PLL and/or system clock.\r
229   *  @note    HSE state can not be changed if it is used directly or through the\r
230   *           PLL as system clock. In this case, you have to select another source\r
231   *           of the system clock then change the HSE state (ex. disable it).\r
232   *  @note    The HSE is stopped by hardware when entering STOP and STANDBY modes.         \r
233   * @note   This function reset the CSSON bit, so if the Clock security system(CSS)\r
234   *         was previously enabled you have to enable it again after calling this\r
235   *         function.\r
236   * @param RCC_HSE: specifies the new state of the HSE.\r
237   *   This parameter can be one of the following values:\r
238   *     @arg RCC_HSE_OFF: turn OFF the HSE oscillator, HSERDY flag goes low after\r
239   *                       6 HSE oscillator clock cycles.\r
240   *     @arg RCC_HSE_ON: turn ON the HSE oscillator\r
241   *     @arg RCC_HSE_Bypass: HSE oscillator bypassed with external clock\r
242   * @retval None\r
243   */\r
244 void RCC_HSEConfig(uint8_t RCC_HSE)\r
245 {\r
246   /* Check the parameters */\r
247   assert_param(IS_RCC_HSE(RCC_HSE));\r
248 \r
249   /* Reset HSEON and HSEBYP bits before configuring the HSE ------------------*/\r
250   *(__IO uint8_t *) CR_BYTE3_ADDRESS = RCC_HSE_OFF;\r
251 \r
252   /* Set the new HSE configuration -------------------------------------------*/\r
253   *(__IO uint8_t *) CR_BYTE3_ADDRESS = RCC_HSE;\r
254 \r
255 }\r
256 \r
257 /**\r
258   * @brief  Waits for HSE start-up.\r
259   * @note   This functions waits on HSERDY flag to be set and return SUCCESS if \r
260   *         this flag is set, otherwise returns ERROR if the timeout is reached \r
261   *         and this flag is not set. The timeout value is defined by the constant\r
262   *         HSE_STARTUP_TIMEOUT in stm32l1xx.h file. You can tailor it depending\r
263   *         on the HSE crystal used in your application. \r
264   * @param  None\r
265   * @retval An ErrorStatus enumeration value:\r
266   *          - SUCCESS: HSE oscillator is stable and ready to use\r
267   *          - ERROR: HSE oscillator not yet ready\r
268   */\r
269 ErrorStatus RCC_WaitForHSEStartUp(void)\r
270 {\r
271   __IO uint32_t StartUpCounter = 0;\r
272   ErrorStatus status = ERROR;\r
273   FlagStatus HSEStatus = RESET;\r
274   \r
275   /* Wait till HSE is ready and if timeout is reached exit */\r
276   do\r
277   {\r
278     HSEStatus = RCC_GetFlagStatus(RCC_FLAG_HSERDY);\r
279     StartUpCounter++;  \r
280   } while((StartUpCounter != HSE_STARTUP_TIMEOUT) && (HSEStatus == RESET));\r
281   \r
282   if (RCC_GetFlagStatus(RCC_FLAG_HSERDY) != RESET)\r
283   {\r
284     status = SUCCESS;\r
285   }\r
286   else\r
287   {\r
288     status = ERROR;\r
289   }  \r
290   return (status);\r
291 }\r
292 \r
293 /**\r
294   * @brief  Adjusts the Internal Multi Speed oscillator (MSI) calibration value.\r
295   * @note   The calibration is used to compensate for the variations in voltage\r
296   *         and temperature that influence the frequency of the internal MSI RC.\r
297   *         Refer to the Application Note AN3300 for more details on how to  \r
298   *         calibrate the MSI.\r
299   * @param  MSICalibrationValue: specifies the MSI calibration trimming value.\r
300   *   This parameter must be a number between 0 and 0xFF.\r
301   * @retval None\r
302   */\r
303 void RCC_AdjustMSICalibrationValue(uint8_t MSICalibrationValue)\r
304 {\r
305   \r
306   /* Check the parameters */\r
307   assert_param(IS_RCC_MSI_CALIBRATION_VALUE(MSICalibrationValue));\r
308 \r
309   *(__IO uint8_t *) ICSCR_BYTE4_ADDRESS = MSICalibrationValue;  \r
310 }\r
311 \r
312 /**\r
313   * @brief  Configures the Internal Multi Speed oscillator (MSI) clock range.\r
314   * @note     After restart from Reset or wakeup from STANDBY, the MSI clock is \r
315   *           around 2.097 MHz. The MSI clock does not change after wake-up from\r
316   *           STOP mode.\r
317   *  @note    The MSI clock range can be modified on the fly.     \r
318   * @param  RCC_MSIRange: specifies the MSI Clock range.\r
319   *   This parameter must be one of the following values:\r
320   *     @arg RCC_MSIRange_0: MSI clock is around 65.536 KHz\r
321   *     @arg RCC_MSIRange_1: MSI clock is around 131.072 KHz\r
322   *     @arg RCC_MSIRange_2: MSI clock is around 262.144 KHz\r
323   *     @arg RCC_MSIRange_3: MSI clock is around 524.288 KHz\r
324   *     @arg RCC_MSIRange_4: MSI clock is around 1.048 MHz\r
325   *     @arg RCC_MSIRange_5: MSI clock is around 2.097 MHz (default after Reset or wake-up from STANDBY)\r
326   *     @arg RCC_MSIRange_6: MSI clock is around 4.194 MHz\r
327   *                   \r
328   * @retval None\r
329   */\r
330 void RCC_MSIRangeConfig(uint32_t RCC_MSIRange)\r
331 {\r
332   uint32_t tmpreg = 0;\r
333   \r
334   /* Check the parameters */\r
335   assert_param(IS_RCC_MSI_CLOCK_RANGE(RCC_MSIRange));\r
336   \r
337   tmpreg = RCC->ICSCR;\r
338   \r
339   /* Clear MSIRANGE[2:0] bits */\r
340   tmpreg &= ~RCC_ICSCR_MSIRANGE;\r
341   \r
342   /* Set the MSIRANGE[2:0] bits according to RCC_MSIRange value */\r
343   tmpreg |= (uint32_t)RCC_MSIRange;\r
344 \r
345   /* Store the new value */\r
346   RCC->ICSCR = tmpreg;\r
347 }\r
348 \r
349 /**\r
350   * @brief  Enables or disables the Internal Multi Speed oscillator (MSI).\r
351   * @note     The MSI is stopped by hardware when entering STOP and STANDBY modes.\r
352   *           It is used (enabled by hardware) as system clock source after\r
353   *           startup from Reset, wakeup from STOP and STANDBY mode, or in case\r
354   *           of failure of the HSE used directly or indirectly as system clock\r
355   *           (if the Clock Security System CSS is enabled).             \r
356   * @note     MSI can not be stopped if it is used as system clock source.\r
357   *           In this case, you have to select another source of the system\r
358   *           clock then stop the MSI.  \r
359   * @note     After enabling the MSI, the application software should wait on\r
360   *           MSIRDY flag to be set indicating that MSI clock is stable and can\r
361   *           be used as system clock source.                                       \r
362   * @param  NewState: new state of the MSI.\r
363   *   This parameter can be: ENABLE or DISABLE.\r
364   * @note   When the MSI is stopped, MSIRDY flag goes low after 6 MSI oscillator\r
365   *         clock cycles.  \r
366   * @retval None\r
367   */\r
368 void RCC_MSICmd(FunctionalState NewState)\r
369 {\r
370   /* Check the parameters */\r
371   assert_param(IS_FUNCTIONAL_STATE(NewState));\r
372   \r
373   *(__IO uint32_t *) CR_MSION_BB = (uint32_t)NewState;\r
374 }\r
375 \r
376 /**\r
377   * @brief  Adjusts the Internal High Speed oscillator (HSI) calibration value.\r
378   * @note   The calibration is used to compensate for the variations in voltage\r
379   *         and temperature that influence the frequency of the internal HSI RC.\r
380   *         Refer to the Application Note AN3300 for more details on how to  \r
381   *         calibrate the HSI.\r
382   * @param  HSICalibrationValue: specifies the HSI calibration trimming value.\r
383   *   This parameter must be a number between 0 and 0x1F.\r
384   * @retval None\r
385   */\r
386 void RCC_AdjustHSICalibrationValue(uint8_t HSICalibrationValue)\r
387 {\r
388   uint32_t tmpreg = 0;\r
389   \r
390   /* Check the parameters */\r
391   assert_param(IS_RCC_HSI_CALIBRATION_VALUE(HSICalibrationValue));\r
392   \r
393   tmpreg = RCC->ICSCR;\r
394   \r
395   /* Clear HSITRIM[4:0] bits */\r
396   tmpreg &= ~RCC_ICSCR_HSITRIM;\r
397   \r
398   /* Set the HSITRIM[4:0] bits according to HSICalibrationValue value */\r
399   tmpreg |= (uint32_t)HSICalibrationValue << 8;\r
400 \r
401   /* Store the new value */\r
402   RCC->ICSCR = tmpreg;\r
403 }\r
404 \r
405 /**\r
406   * @brief  Enables or disables the Internal High Speed oscillator (HSI).\r
407   * @note     After enabling the HSI, the application software should wait on \r
408   *           HSIRDY flag to be set indicating that HSI clock is stable and can\r
409   *           be used to clock the PLL and/or system clock.\r
410   * @note     HSI can not be stopped if it is used directly or through the PLL\r
411   *           as system clock. In this case, you have to select another source \r
412   *           of the system clock then stop the HSI.\r
413   * @note     The HSI is stopped by hardware when entering STOP and STANDBY modes. \r
414   * @param  NewState: new state of the HSI.\r
415   *   This parameter can be: ENABLE or DISABLE.\r
416   * @note   When the HSI is stopped, HSIRDY flag goes low after 6 HSI oscillator\r
417   *         clock cycles.  \r
418   * @retval None\r
419   */\r
420 void RCC_HSICmd(FunctionalState NewState)\r
421 {\r
422   /* Check the parameters */\r
423   assert_param(IS_FUNCTIONAL_STATE(NewState));\r
424   \r
425   *(__IO uint32_t *) CR_HSION_BB = (uint32_t)NewState;\r
426 }\r
427 \r
428 /**\r
429   * @brief  Configures the External Low Speed oscillator (LSE).\r
430   * @note     As the LSE is in the RTC domain and write access is denied to this\r
431   *           domain after reset, you have to enable write access using \r
432   *           PWR_RTCAccessCmd(ENABLE) function before to configure the LSE\r
433   *           (to be done once after reset).  \r
434   * @note     After enabling the LSE (RCC_LSE_ON or RCC_LSE_Bypass), the application\r
435   *           software should wait on LSERDY flag to be set indicating that LSE clock\r
436   *           is stable and can be used to clock the RTC.\r
437   * @param  RCC_LSE: specifies the new state of the LSE.\r
438   *   This parameter can be one of the following values:\r
439   *     @arg RCC_LSE_OFF: turn OFF the LSE oscillator, LSERDY flag goes low after\r
440   *                       6 LSE oscillator clock cycles.\r
441   *     @arg RCC_LSE_ON: turn ON the LSE oscillator\r
442   *     @arg RCC_LSE_Bypass: LSE oscillator bypassed with external clock\r
443   * @retval None\r
444   */\r
445 void RCC_LSEConfig(uint8_t RCC_LSE)\r
446 {\r
447   /* Check the parameters */\r
448   assert_param(IS_RCC_LSE(RCC_LSE));\r
449   \r
450   /* Reset LSEON and LSEBYP bits before configuring the LSE ------------------*/\r
451   *(__IO uint8_t *) CSR_BYTE2_ADDRESS = RCC_LSE_OFF;\r
452 \r
453   /* Set the new LSE configuration -------------------------------------------*/\r
454   *(__IO uint8_t *) CSR_BYTE2_ADDRESS = RCC_LSE;  \r
455 }\r
456 \r
457 /**\r
458   * @brief  Enables or disables the Internal Low Speed oscillator (LSI).  \r
459   * @note     After enabling the LSI, the application software should wait on \r
460   *           LSIRDY flag to be set indicating that LSI clock is stable and can\r
461   *           be used to clock the IWDG and/or the RTC.\r
462   * @note     LSI can not be disabled if the IWDG is running.  \r
463   * @param  NewState: new state of the LSI.\r
464   *   This parameter can be: ENABLE or DISABLE.\r
465   * @note   When the LSI is stopped, LSIRDY flag goes low after 6 LSI oscillator\r
466   *         clock cycles. \r
467   * @retval None\r
468   */\r
469 void RCC_LSICmd(FunctionalState NewState)\r
470 {\r
471   /* Check the parameters */\r
472   assert_param(IS_FUNCTIONAL_STATE(NewState));\r
473   \r
474   *(__IO uint32_t *) CSR_LSION_BB = (uint32_t)NewState;\r
475 }\r
476 \r
477 /**\r
478   * @brief  Configures the PLL clock source and multiplication factor.\r
479   * @note   This function must be used only when the PLL is disabled.\r
480   *   \r
481   * @param  RCC_PLLSource: specifies the PLL entry clock source.\r
482   *   This parameter can be one of the following values:\r
483   *     @arg RCC_PLLSource_HSI: HSI oscillator clock selected as PLL clock source\r
484   *     @arg RCC_PLLSource_HSE: HSE oscillator clock selected as PLL clock source\r
485   * @note   The minimum input clock frequency for PLL is 2 MHz (when using HSE as\r
486   *         PLL source).\r
487   *               \r
488   * @param  RCC_PLLMul: specifies the PLL multiplication factor, which drive the PLLVCO clock\r
489   *   This parameter can be:\r
490   *     @arg RCC_PLLMul_3: PLL clock source multiplied by 3\r
491   *     @arg RCC_PLLMul_4: PLL clock source multiplied by 4\r
492   *     @arg RCC_PLLMul_6: PLL clock source multiplied by 6\r
493   *     @arg RCC_PLLMul_8: PLL clock source multiplied by 8\r
494   *     @arg RCC_PLLMul_12: PLL clock source multiplied by 12\r
495   *     @arg RCC_PLLMul_16: PLL clock source multiplied by 16  \r
496   *     @arg RCC_PLLMul_24: PLL clock source multiplied by 24\r
497   *     @arg RCC_PLLMul_32: PLL clock source multiplied by 32\r
498   *     @arg RCC_PLLMul_48: PLL clock source multiplied by 48\r
499   * @note   The application software must set correctly the PLL multiplication\r
500   *         factor to avoid exceeding:\r
501   *             - 96 MHz as PLLVCO when the product is in range 1\r
502   *             - 48 MHz as PLLVCO when the product is in range 2\r
503   *             - 24 MHz when the product is in range 3\r
504   * @note   When using the USB the PLLVCO should be 96MHz\r
505   *                                   \r
506   * @param  RCC_PLLDiv: specifies the PLL division factor.\r
507   *   This parameter can be:\r
508   *     @arg RCC_PLLDiv_2: PLL Clock output divided by 2  \r
509   *     @arg RCC_PLLDiv_3: PLL Clock output divided by 3         \r
510   *     @arg RCC_PLLDiv_4: PLL Clock output divided by 4  \r
511   * @note   The application software must set correctly the output division to avoid\r
512   *         exceeding 32 MHz as SYSCLK.\r
513   *            \r
514   * @retval None\r
515   */\r
516 void RCC_PLLConfig(uint8_t RCC_PLLSource, uint8_t RCC_PLLMul, uint8_t RCC_PLLDiv)\r
517 {\r
518   /* Check the parameters */\r
519   assert_param(IS_RCC_PLL_SOURCE(RCC_PLLSource));\r
520   assert_param(IS_RCC_PLL_MUL(RCC_PLLMul));\r
521   assert_param(IS_RCC_PLL_DIV(RCC_PLLDiv));\r
522   \r
523   *(__IO uint8_t *) CFGR_BYTE3_ADDRESS = (uint8_t)(RCC_PLLSource | ((uint8_t)(RCC_PLLMul | (uint8_t)(RCC_PLLDiv))));\r
524 }\r
525 \r
526 /**\r
527   * @brief  Enables or disables the PLL.\r
528   * @note     After enabling the PLL, the application software should wait on \r
529   *           PLLRDY flag to be set indicating that PLL clock is stable and can\r
530   *           be used as system clock source.\r
531   * @note     The PLL can not be disabled if it is used as system clock source\r
532   * @note     The PLL is disabled by hardware when entering STOP and STANDBY modes.    \r
533   * @param  NewState: new state of the PLL.\r
534   *   This parameter can be: ENABLE or DISABLE.\r
535   * @retval None\r
536   */\r
537 void RCC_PLLCmd(FunctionalState NewState)\r
538 {\r
539   /* Check the parameters */\r
540   assert_param(IS_FUNCTIONAL_STATE(NewState));\r
541   \r
542   *(__IO uint32_t *) CR_PLLON_BB = (uint32_t)NewState;\r
543 }\r
544 \r
545 /**\r
546   * @brief  Enables or disables the Clock Security System.\r
547   * @note   If a failure is detected on the HSE oscillator clock, this oscillator\r
548   *         is automatically disabled and an interrupt is generated to inform the\r
549   *         software about the failure (Clock Security System Interrupt, CSSI),\r
550   *         allowing the MCU to perform rescue operations. The CSSI is linked to \r
551   *         the Cortex-M3 NMI (Non-Maskable Interrupt) exception vector.  \r
552   * @param  NewState: new state of the Clock Security System.\r
553   *         This parameter can be: ENABLE or DISABLE.\r
554   * @retval None\r
555   */\r
556 void RCC_ClockSecuritySystemCmd(FunctionalState NewState)\r
557 {\r
558   /* Check the parameters */\r
559   assert_param(IS_FUNCTIONAL_STATE(NewState));\r
560   \r
561   *(__IO uint32_t *) CR_CSSON_BB = (uint32_t)NewState;\r
562 }\r
563 \r
564 /**\r
565   * @brief  Enables or disables the LSE Clock Security System.\r
566   * @param  NewState: new state of the Clock Security System.\r
567   *         This parameter can be: ENABLE or DISABLE.\r
568   * @retval None\r
569   */\r
570 void RCC_LSEClockSecuritySystemCmd(FunctionalState NewState)\r
571 {\r
572   /* Check the parameters */\r
573   assert_param(IS_FUNCTIONAL_STATE(NewState));\r
574   \r
575   *(__IO uint32_t *) CSR_LSECSSON_BB = (uint32_t)NewState;\r
576 }\r
577 \r
578 /**\r
579   * @brief  Selects the clock source to output on MCO pin (PA8).\r
580   * @note   PA8 should be configured in alternate function mode.   \r
581   * @param  RCC_MCOSource: specifies the clock source to output.\r
582   *   This parameter can be one of the following values:\r
583   *     @arg RCC_MCOSource_NoClock: No clock selected\r
584   *     @arg RCC_MCOSource_SYSCLK: System clock selected\r
585   *     @arg RCC_MCOSource_HSI: HSI oscillator clock selected\r
586   *     @arg RCC_MCOSource_MSI: MSI oscillator clock selected  \r
587   *     @arg RCC_MCOSource_HSE: HSE oscillator clock selected\r
588   *     @arg RCC_MCOSource_PLLCLK: PLL clock selected\r
589   *     @arg RCC_MCOSource_LSI: LSI clock selected\r
590   *     @arg RCC_MCOSource_LSE: LSE clock selected    \r
591   * @param  RCC_MCODiv: specifies the MCO prescaler.\r
592   *   This parameter can be one of the following values: \r
593   *     @arg RCC_MCODiv_1: no division applied to MCO clock \r
594   *     @arg RCC_MCODiv_2: division by 2 applied to MCO clock\r
595   *     @arg RCC_MCODiv_4: division by 4 applied to MCO clock\r
596   *     @arg RCC_MCODiv_8: division by 8 applied to MCO clock\r
597   *     @arg RCC_MCODiv_16: division by 16 applied to MCO clock             \r
598   * @retval None\r
599   */\r
600 void RCC_MCOConfig(uint8_t RCC_MCOSource, uint8_t RCC_MCODiv)\r
601 {\r
602   /* Check the parameters */\r
603   assert_param(IS_RCC_MCO_SOURCE(RCC_MCOSource));\r
604   assert_param(IS_RCC_MCO_DIV(RCC_MCODiv));\r
605     \r
606   /* Select MCO clock source and prescaler */\r
607   *(__IO uint8_t *) CFGR_BYTE4_ADDRESS =  RCC_MCOSource | RCC_MCODiv; \r
608 }\r
609 \r
610 /**\r
611   * @}\r
612   */\r
613 \r
614 /** @defgroup RCC_Group2 System AHB and APB busses clocks configuration functions\r
615  *  @brief   System, AHB and APB busses clocks configuration functions\r
616  *\r
617 @verbatim\r
618  ===============================================================================\r
619      ##### System, AHB and APB busses clocks configuration functions #####\r
620  ===============================================================================\r
621     [..] This section provide functions allowing to configure the System, AHB, \r
622          APB1 and APB2 busses clocks.\r
623          (#) Several clock sources can be used to drive the System clock (SYSCLK): \r
624              MSI, HSI, HSE and PLL.\r
625              The AHB clock (HCLK) is derived from System clock through configurable \r
626              prescaler and used to clock the CPU, memory and peripherals mapped \r
627              on AHB bus (DMA and GPIO).APB1 (PCLK1) and APB2 (PCLK2) clocks are \r
628              derived from AHB clock through configurable prescalers and used to \r
629              clock the peripherals mapped on these busses. You can use \r
630              "RCC_GetClocksFreq()" function to retrieve the frequencies of these \r
631              clocks.  \r
632 \r
633          -@- All the peripheral clocks are derived from the System clock (SYSCLK) \r
634              except:\r
635              (+@) The USB 48 MHz clock which is derived from the PLL VCO clock.\r
636              (+@) The ADC clock which is always the HSI clock. A divider by 1, 2 \r
637                   or 4 allows to adapt the clock frequency to the device operating \r
638                   conditions. \r
639              (+@) The RTC/LCD clock which is derived from the LSE, LSI or 1 MHz \r
640                   HSE_RTC (HSE divided by a programmable prescaler).\r
641                   The System clock (SYSCLK) frequency must be higher or equal to \r
642                   the RTC/LCD clock frequency.\r
643              (+@) IWDG clock which is always the LSI clock.\r
644        \r
645          (#) The maximum frequency of the SYSCLK, HCLK, PCLK1 and PCLK2 is 32 MHz.\r
646              Depending on the device voltage range, the maximum frequency should \r
647              be adapted accordingly:\r
648 \r
649         +----------------------------------------------------------------+\r
650         |  Wait states  |                HCLK clock frequency (MHz)      |\r
651         |               |------------------------------------------------|\r
652         |   (Latency)   |            voltage range       | voltage range |\r
653         |               |            1.65 V - 3.6 V      | 2.0 V - 3.6 V |\r
654         |               |----------------|---------------|---------------|\r
655         |               |  VCORE = 1.2 V | VCORE = 1.5 V | VCORE = 1.8 V |\r
656         |-------------- |----------------|---------------|---------------|\r
657         |0WS(1CPU cycle)|0 < HCLK <= 2   |0 < HCLK <= 8  |0 < HCLK <= 16 |\r
658         |---------------|----------------|---------------|---------------|\r
659         |1WS(2CPU cycle)|2 < HCLK <= 4   |8 < HCLK <= 16 |16 < HCLK <= 32|\r
660         +----------------------------------------------------------------+\r
661 \r
662          (#) After reset, the System clock source is the MSI (2 MHz) with 0 WS, \r
663              Flash 32-bit access is enabled and prefetch is disabled.\r
664     [..] It is recommended to use the following software sequences to tune the \r
665          number of wait states needed to access the Flash memory with the CPU \r
666          frequency (HCLK).\r
667          (+) Increasing the CPU frequency (in the same voltage range)\r
668          (+) Program the Flash 64-bit access, using "FLASH_ReadAccess64Cmd(ENABLE)" \r
669              function\r
670          (+) Check that 64-bit access is taken into account by reading FLASH_ACR\r
671          (+) Program Flash WS to 1, using "FLASH_SetLatency(FLASH_Latency_1)" \r
672              function\r
673          (+) Check that the new number of WS is taken into account by reading \r
674              FLASH_ACR\r
675          (+) Modify the CPU clock source, using "RCC_SYSCLKConfig()" function\r
676          (+) If needed, modify the CPU clock prescaler by using "RCC_HCLKConfig()" \r
677              function\r
678          (+) Check that the new CPU clock source is taken into account by reading \r
679            the clock source status, using "RCC_GetSYSCLKSource()" function \r
680          (+) Decreasing the CPU frequency (in the same voltage range)\r
681          (+) Modify the CPU clock source, using "RCC_SYSCLKConfig()" function\r
682          (+) If needed, modify the CPU clock prescaler by using "RCC_HCLKConfig()" \r
683              function\r
684          (+) Check that the new CPU clock source is taken into account by reading \r
685            the clock source status, using "RCC_GetSYSCLKSource()" function\r
686          (+) Program the new number of WS, using "FLASH_SetLatency()" function\r
687          (+) Check that the new number of WS is taken into account by reading \r
688              FLASH_ACR\r
689          (+) Enable the Flash 32-bit access, using "FLASH_ReadAccess64Cmd(DISABLE)" \r
690              function\r
691          (+) Check that 32-bit access is taken into account by reading FLASH_ACR\r
692 \r
693 @endverbatim\r
694   * @{\r
695   */\r
696 \r
697 /**\r
698   * @brief  Configures the system clock (SYSCLK).\r
699   * @note     The MSI is used (enabled by hardware) as system clock source after\r
700   *           startup from Reset, wake-up from STOP and STANDBY mode, or in case\r
701   *           of failure of the HSE used directly or indirectly as system clock\r
702   *           (if the Clock Security System CSS is enabled).\r
703   * @note     A switch from one clock source to another occurs only if the target\r
704   *           clock source is ready (clock stable after startup delay or PLL locked). \r
705   *           If a clock source which is not yet ready is selected, the switch will\r
706   *           occur when the clock source will be ready. \r
707   *           You can use RCC_GetSYSCLKSource() function to know which clock is\r
708   *           currently used as system clock source.  \r
709   * @param  RCC_SYSCLKSource: specifies the clock source used as system clock source \r
710   *   This parameter can be one of the following values:\r
711   *     @arg RCC_SYSCLKSource_MSI:    MSI selected as system clock source\r
712   *     @arg RCC_SYSCLKSource_HSI:    HSI selected as system clock source\r
713   *     @arg RCC_SYSCLKSource_HSE:    HSE selected as system clock source\r
714   *     @arg RCC_SYSCLKSource_PLLCLK: PLL selected as system clock source\r
715   * @retval None\r
716   */\r
717 void RCC_SYSCLKConfig(uint32_t RCC_SYSCLKSource)\r
718 {\r
719   uint32_t tmpreg = 0;\r
720   \r
721   /* Check the parameters */\r
722   assert_param(IS_RCC_SYSCLK_SOURCE(RCC_SYSCLKSource));\r
723   \r
724   tmpreg = RCC->CFGR;\r
725   \r
726   /* Clear SW[1:0] bits */\r
727   tmpreg &= ~RCC_CFGR_SW;\r
728   \r
729   /* Set SW[1:0] bits according to RCC_SYSCLKSource value */\r
730   tmpreg |= RCC_SYSCLKSource;\r
731   \r
732   /* Store the new value */\r
733   RCC->CFGR = tmpreg;\r
734 }\r
735 \r
736 /**\r
737   * @brief  Returns the clock source used as system clock.\r
738   * @param  None\r
739   * @retval The clock source used as system clock. The returned value can be one \r
740   *         of the following values:\r
741   *              - 0x00: MSI used as system clock\r
742   *              - 0x04: HSI used as system clock  \r
743   *              - 0x08: HSE used as system clock\r
744   *              - 0x0C: PLL used as system clock\r
745   */\r
746 uint8_t RCC_GetSYSCLKSource(void)\r
747 {\r
748   return ((uint8_t)(RCC->CFGR & RCC_CFGR_SWS));\r
749 }\r
750 \r
751 /**\r
752   * @brief  Configures the AHB clock (HCLK).\r
753   * @note   Depending on the device voltage range, the software has to set correctly\r
754   *         these bits to ensure that the system frequency does not exceed the\r
755   *         maximum allowed frequency (for more details refer to section above\r
756   *         "CPU, AHB and APB busses clocks configuration functions")\r
757   * @param  RCC_SYSCLK: defines the AHB clock divider. This clock is derived from \r
758   *                     the system clock (SYSCLK).\r
759   *   This parameter can be one of the following values:\r
760   *     @arg RCC_SYSCLK_Div1:   AHB clock = SYSCLK\r
761   *     @arg RCC_SYSCLK_Div2:   AHB clock = SYSCLK/2\r
762   *     @arg RCC_SYSCLK_Div4:   AHB clock = SYSCLK/4\r
763   *     @arg RCC_SYSCLK_Div8:   AHB clock = SYSCLK/8\r
764   *     @arg RCC_SYSCLK_Div16:  AHB clock = SYSCLK/16\r
765   *     @arg RCC_SYSCLK_Div64:  AHB clock = SYSCLK/64\r
766   *     @arg RCC_SYSCLK_Div128: AHB clock = SYSCLK/128\r
767   *     @arg RCC_SYSCLK_Div256: AHB clock = SYSCLK/256\r
768   *     @arg RCC_SYSCLK_Div512: AHB clock = SYSCLK/512\r
769   * @retval None\r
770   */\r
771 void RCC_HCLKConfig(uint32_t RCC_SYSCLK)\r
772 {\r
773   uint32_t tmpreg = 0;\r
774   \r
775   /* Check the parameters */\r
776   assert_param(IS_RCC_HCLK(RCC_SYSCLK));\r
777   \r
778   tmpreg = RCC->CFGR;\r
779   \r
780   /* Clear HPRE[3:0] bits */\r
781   tmpreg &= ~RCC_CFGR_HPRE;\r
782   \r
783   /* Set HPRE[3:0] bits according to RCC_SYSCLK value */\r
784   tmpreg |= RCC_SYSCLK;\r
785   \r
786   /* Store the new value */\r
787   RCC->CFGR = tmpreg;\r
788 }\r
789 \r
790 /**\r
791   * @brief  Configures the Low Speed APB clock (PCLK1).\r
792   * @param  RCC_HCLK: defines the APB1 clock divider. This clock is derived from \r
793   *                   the AHB clock (HCLK).\r
794   *   This parameter can be one of the following values:\r
795   *     @arg RCC_HCLK_Div1:  APB1 clock = HCLK\r
796   *     @arg RCC_HCLK_Div2:  APB1 clock = HCLK/2\r
797   *     @arg RCC_HCLK_Div4:  APB1 clock = HCLK/4\r
798   *     @arg RCC_HCLK_Div8:  APB1 clock = HCLK/8\r
799   *     @arg RCC_HCLK_Div16: APB1 clock = HCLK/16\r
800   * @retval None\r
801   */\r
802 void RCC_PCLK1Config(uint32_t RCC_HCLK)\r
803 {\r
804   uint32_t tmpreg = 0;\r
805   \r
806   /* Check the parameters */\r
807   assert_param(IS_RCC_PCLK(RCC_HCLK));\r
808   \r
809   tmpreg = RCC->CFGR;\r
810   \r
811   /* Clear PPRE1[2:0] bits */\r
812   tmpreg &= ~RCC_CFGR_PPRE1;\r
813   \r
814   /* Set PPRE1[2:0] bits according to RCC_HCLK value */\r
815   tmpreg |= RCC_HCLK;\r
816   \r
817   /* Store the new value */\r
818   RCC->CFGR = tmpreg;\r
819 }\r
820 \r
821 /**\r
822   * @brief  Configures the High Speed APB clock (PCLK2).\r
823   * @param  RCC_HCLK: defines the APB2 clock divider. This clock is derived from \r
824   *                   the AHB clock (HCLK).\r
825   *   This parameter can be one of the following values:\r
826   *     @arg RCC_HCLK_Div1:  APB2 clock = HCLK\r
827   *     @arg RCC_HCLK_Div2:  APB2 clock = HCLK/2\r
828   *     @arg RCC_HCLK_Div4:  APB2 clock = HCLK/4\r
829   *     @arg RCC_HCLK_Div8:  APB2 clock = HCLK/8\r
830   *     @arg RCC_HCLK_Div16: APB2 clock = HCLK/16\r
831   * @retval None\r
832   */\r
833 void RCC_PCLK2Config(uint32_t RCC_HCLK)\r
834 {\r
835   uint32_t tmpreg = 0;\r
836   \r
837   /* Check the parameters */\r
838   assert_param(IS_RCC_PCLK(RCC_HCLK));\r
839   \r
840   tmpreg = RCC->CFGR;\r
841   \r
842   /* Clear PPRE2[2:0] bits */\r
843   tmpreg &= ~RCC_CFGR_PPRE2;\r
844   \r
845   /* Set PPRE2[2:0] bits according to RCC_HCLK value */\r
846   tmpreg |= RCC_HCLK << 3;\r
847   \r
848   /* Store the new value */\r
849   RCC->CFGR = tmpreg;\r
850 }\r
851 \r
852 /**\r
853   * @brief  Returns the frequencies of the System, AHB and APB busses clocks.\r
854   * @note     The frequency returned by this function is not the real frequency\r
855   *           in the chip. It is calculated based on the predefined constant and\r
856   *           the source selected by RCC_SYSCLKConfig():\r
857   *             \r
858   * @note     If SYSCLK source is MSI, function returns values based on  MSI\r
859   *             Value as defined by the MSI range, refer to RCC_MSIRangeConfig()\r
860   *                                   \r
861   * @note     If SYSCLK source is HSI, function returns values based on HSI_VALUE(*)\r
862   *                                              \r
863   * @note     If SYSCLK source is HSE, function returns values based on HSE_VALUE(**)\r
864   *                          \r
865   * @note     If SYSCLK source is PLL, function returns values based on HSE_VALUE(**) \r
866   *             or HSI_VALUE(*) multiplied/divided by the PLL factors.\r
867   *         \r
868   *         (*) HSI_VALUE is a constant defined in stm32l1xx.h file (default value\r
869   *             16 MHz) but the real value may vary depending on the variations\r
870   *             in voltage and temperature, refer to RCC_AdjustHSICalibrationValue().   \r
871   *    \r
872   *         (**) HSE_VALUE is a constant defined in stm32l1xx.h file (default value\r
873   *              8 MHz), user has to ensure that HSE_VALUE is same as the real\r
874   *              frequency of the crystal used. Otherwise, this function may\r
875   *              return wrong result.\r
876   *                \r
877   *         - The result of this function could be not correct when using fractional\r
878   *           value for HSE crystal.   \r
879   *             \r
880   * @param  RCC_Clocks: pointer to a RCC_ClocksTypeDef structure which will hold \r
881   *         the clocks frequencies. \r
882   *     \r
883   * @note     This function can be used by the user application to compute the \r
884   *           baudrate for the communication peripherals or configure other parameters.\r
885   * @note     Each time SYSCLK, HCLK, PCLK1 and/or PCLK2 clock changes, this function\r
886   *           must be called to update the structure's field. Otherwise, any\r
887   *           configuration based on this function will be incorrect.\r
888   *    \r
889   * @retval None\r
890   */\r
891 void RCC_GetClocksFreq(RCC_ClocksTypeDef* RCC_Clocks)\r
892 {\r
893   uint32_t tmp = 0, pllmul = 0, plldiv = 0, pllsource = 0, presc = 0, msirange = 0;\r
894 \r
895   /* Get SYSCLK source -------------------------------------------------------*/\r
896   tmp = RCC->CFGR & RCC_CFGR_SWS;\r
897   \r
898   switch (tmp)\r
899   {\r
900     case 0x00:  /* MSI used as system clock */\r
901       msirange = (RCC->ICSCR & RCC_ICSCR_MSIRANGE ) >> 13;\r
902       RCC_Clocks->SYSCLK_Frequency = (32768 * (1 << (msirange + 1)));\r
903       break;\r
904     case 0x04:  /* HSI used as system clock */\r
905       RCC_Clocks->SYSCLK_Frequency = HSI_VALUE;\r
906       break;\r
907     case 0x08:  /* HSE used as system clock */\r
908       RCC_Clocks->SYSCLK_Frequency = HSE_VALUE;\r
909       break;\r
910     case 0x0C:  /* PLL used as system clock */\r
911       /* Get PLL clock source and multiplication factor ----------------------*/\r
912       pllmul = RCC->CFGR & RCC_CFGR_PLLMUL;\r
913       plldiv = RCC->CFGR & RCC_CFGR_PLLDIV;\r
914       pllmul = PLLMulTable[(pllmul >> 18)];\r
915       plldiv = (plldiv >> 22) + 1;\r
916       \r
917       pllsource = RCC->CFGR & RCC_CFGR_PLLSRC;\r
918 \r
919       if (pllsource == 0x00)\r
920       {\r
921         /* HSI oscillator clock selected as PLL clock source */\r
922         RCC_Clocks->SYSCLK_Frequency = (((HSI_VALUE) * pllmul) / plldiv);\r
923       }\r
924       else\r
925       {\r
926         /* HSE selected as PLL clock source */\r
927         RCC_Clocks->SYSCLK_Frequency = (((HSE_VALUE) * pllmul) / plldiv);\r
928       }\r
929       break;\r
930     default: /* MSI used as system clock */\r
931       msirange = (RCC->ICSCR & RCC_ICSCR_MSIRANGE ) >> 13;\r
932       RCC_Clocks->SYSCLK_Frequency = (32768 * (1 << (msirange + 1)));\r
933       break;\r
934   }\r
935   /* Compute HCLK, PCLK1, PCLK2 and ADCCLK clocks frequencies ----------------*/\r
936   /* Get HCLK prescaler */\r
937   tmp = RCC->CFGR & RCC_CFGR_HPRE;\r
938   tmp = tmp >> 4;\r
939   presc = APBAHBPrescTable[tmp]; \r
940   /* HCLK clock frequency */\r
941   RCC_Clocks->HCLK_Frequency = RCC_Clocks->SYSCLK_Frequency >> presc;\r
942 \r
943   /* Get PCLK1 prescaler */\r
944   tmp = RCC->CFGR & RCC_CFGR_PPRE1;\r
945   tmp = tmp >> 8;\r
946   presc = APBAHBPrescTable[tmp];\r
947   /* PCLK1 clock frequency */\r
948   RCC_Clocks->PCLK1_Frequency = RCC_Clocks->HCLK_Frequency >> presc;\r
949 \r
950   /* Get PCLK2 prescaler */\r
951   tmp = RCC->CFGR & RCC_CFGR_PPRE2;\r
952   tmp = tmp >> 11;\r
953   presc = APBAHBPrescTable[tmp];\r
954   /* PCLK2 clock frequency */\r
955   RCC_Clocks->PCLK2_Frequency = RCC_Clocks->HCLK_Frequency >> presc;\r
956 }\r
957 \r
958 /**\r
959   * @}\r
960   */\r
961 \r
962 /** @defgroup RCC_Group3 Peripheral clocks configuration functions\r
963  *  @brief   Peripheral clocks configuration functions \r
964  *\r
965 @verbatim\r
966  ===============================================================================\r
967              ##### Peripheral clocks configuration functions #####\r
968  ===============================================================================\r
969     [..] This section provide functions allowing to configure the Peripheral clocks. \r
970          (#) The RTC/LCD clock which is derived from the LSE, LSI or 1 MHz HSE_RTC \r
971          (HSE divided by a programmable prescaler).\r
972          (#) After restart from Reset or wakeup from STANDBY, all peripherals are \r
973              off except internal SRAM, Flash and JTAG. Before to start using a \r
974              peripheral you have to enable its interface clock. You can do this \r
975              using RCC_AHBPeriphClockCmd(), RCC_APB2PeriphClockCmd() and \r
976              RCC_APB1PeriphClockCmd() functions.\r
977 \r
978          (#) To reset the peripherals configuration (to the default state after \r
979              device reset) you can use RCC_AHBPeriphResetCmd(), \r
980              RCC_APB2PeriphResetCmd() and RCC_APB1PeriphResetCmd() functions.\r
981          (#) To further reduce power consumption in SLEEP mode the peripheral \r
982              clocks can be disabled prior to executing the WFI or WFE instructions.\r
983              You can do this using RCC_AHBPeriphClockLPModeCmd(), \r
984              RCC_APB2PeriphClockLPModeCmd() and RCC_APB1PeriphClockLPModeCmd() \r
985              functions.\r
986 \r
987 @endverbatim\r
988   * @{\r
989   */\r
990 \r
991 /**\r
992   * @brief  Configures the RTC and LCD clock (RTCCLK / LCDCLK).\r
993   * @note     As the RTC clock configuration bits are in the RTC domain and write\r
994   *           access is denied to this domain after reset, you have to enable write\r
995   *           access using PWR_RTCAccessCmd(ENABLE) function before to configure\r
996   *           the RTC clock source (to be done once after reset).    \r
997   * @note     Once the RTC clock is configured it can't be changed unless the RTC\r
998   *           is reset using RCC_RTCResetCmd function, or by a Power On Reset (POR)\r
999   * @note     The RTC clock (RTCCLK) is used also to clock the LCD (LCDCLK).\r
1000   *             \r
1001   * @param  RCC_RTCCLKSource: specifies the RTC clock source.\r
1002   *   This parameter can be one of the following values:\r
1003   *     @arg RCC_RTCCLKSource_LSE: LSE selected as RTC clock\r
1004   *     @arg RCC_RTCCLKSource_LSI: LSI selected as RTC clock\r
1005   *     @arg RCC_RTCCLKSource_HSE_Div2: HSE divided by 2 selected as RTC clock\r
1006   *     @arg RCC_RTCCLKSource_HSE_Div4: HSE divided by 4 selected as RTC clock\r
1007   *     @arg RCC_RTCCLKSource_HSE_Div8: HSE divided by 8 selected as RTC clock\r
1008   *     @arg RCC_RTCCLKSource_HSE_Div16: HSE divided by 16 selected as RTC clock\r
1009   *       \r
1010   * @note     If the LSE or LSI is used as RTC clock source, the RTC continues to\r
1011   *           work in STOP and STANDBY modes, and can be used as wakeup source.\r
1012   *           However, when the HSE clock is used as RTC clock source, the RTC\r
1013   *           cannot be used in STOP and STANDBY modes.\r
1014   *             \r
1015   * @note     The maximum input clock frequency for RTC is 1MHz (when using HSE as\r
1016   *           RTC clock source).\r
1017   *                          \r
1018   * @retval None\r
1019   */\r
1020 void RCC_RTCCLKConfig(uint32_t RCC_RTCCLKSource)\r
1021 {\r
1022   uint32_t      tmpreg = 0;\r
1023 \r
1024   /* Check the parameters */\r
1025   assert_param(IS_RCC_RTCCLK_SOURCE(RCC_RTCCLKSource));\r
1026   \r
1027   if ((RCC_RTCCLKSource & RCC_CSR_RTCSEL_HSE) == RCC_CSR_RTCSEL_HSE)\r
1028   { \r
1029     /* If HSE is selected as RTC clock source, configure HSE division factor for RTC clock */\r
1030     tmpreg = RCC->CR;\r
1031 \r
1032     /* Clear RTCPRE[1:0] bits */\r
1033     tmpreg &= ~RCC_CR_RTCPRE;\r
1034 \r
1035     /* Configure HSE division factor for RTC clock */\r
1036     tmpreg |= (RCC_RTCCLKSource & RCC_CR_RTCPRE);\r
1037 \r
1038     /* Store the new value */\r
1039     RCC->CR = tmpreg;\r
1040   }\r
1041          \r
1042   RCC->CSR &= ~RCC_CSR_RTCSEL;\r
1043   \r
1044   /* Select the RTC clock source */\r
1045   RCC->CSR |= (RCC_RTCCLKSource & RCC_CSR_RTCSEL);\r
1046 }\r
1047 \r
1048 /**\r
1049   * @brief  Enables or disables the RTC clock.\r
1050   * @note   This function must be used only after the RTC clock source was selected\r
1051   *         using the RCC_RTCCLKConfig function.\r
1052   * @param  NewState: new state of the RTC clock.\r
1053   *   This parameter can be: ENABLE or DISABLE.\r
1054   * @retval None\r
1055   */\r
1056 void RCC_RTCCLKCmd(FunctionalState NewState)\r
1057 {\r
1058   /* Check the parameters */\r
1059   assert_param(IS_FUNCTIONAL_STATE(NewState));\r
1060   \r
1061   *(__IO uint32_t *) CSR_RTCEN_BB = (uint32_t)NewState;\r
1062 }\r
1063 \r
1064 /**\r
1065   * @brief  Forces or releases the RTC peripheral and associated resources reset.\r
1066   * @note   This function resets the RTC peripheral, RTC clock source selection\r
1067   *         (in RCC_CSR) and the backup registers.\r
1068   * @param  NewState: new state of the RTC reset.\r
1069   *   This parameter can be: ENABLE or DISABLE.\r
1070   * @retval None\r
1071   */\r
1072 void RCC_RTCResetCmd(FunctionalState NewState)\r
1073 {\r
1074   /* Check the parameters */\r
1075   assert_param(IS_FUNCTIONAL_STATE(NewState));\r
1076   \r
1077   *(__IO uint32_t *) CSR_RTCRST_BB = (uint32_t)NewState;\r
1078 }\r
1079 \r
1080 /**\r
1081   * @brief  Enables or disables the AHB peripheral clock.\r
1082   * @note   After reset, the peripheral clock (used for registers read/write access)\r
1083   *         is disabled and the application software has to enable this clock before \r
1084   *         using it.    \r
1085   * @param  RCC_AHBPeriph: specifies the AHB peripheral to gates its clock.\r
1086   *   This parameter can be any combination of the following values:\r
1087   *     @arg RCC_AHBPeriph_GPIOA:         GPIOA clock\r
1088   *     @arg RCC_AHBPeriph_GPIOB:         GPIOB clock\r
1089   *     @arg RCC_AHBPeriph_GPIOC:         GPIOC clock\r
1090   *     @arg RCC_AHBPeriph_GPIOD:         GPIOD clock\r
1091   *     @arg RCC_AHBPeriph_GPIOE:         GPIOE clock\r
1092   *     @arg RCC_AHBPeriph_GPIOH:         GPIOH clock\r
1093   *     @arg RCC_AHBPeriph_GPIOF:         GPIOF clock\r
1094   *     @arg RCC_AHBPeriph_GPIOG:         GPIOG clock\r
1095   *     @arg RCC_AHBPeriph_CRC:           CRC clock\r
1096   *     @arg RCC_AHBPeriph_FLITF: (has effect only when the Flash memory is in power down mode)  \r
1097   *     @arg RCC_AHBPeriph_DMA1:          DMA1 clock\r
1098   *     @arg RCC_AHBPeriph_DMA2:          DMA2 clock\r
1099   *     @arg RCC_AHBPeriph_AES:           AES clock\r
1100   *     @arg RCC_AHBPeriph_FSMC:          FSMC clock\r
1101   * @param  NewState: new state of the specified peripheral clock.\r
1102   *         This parameter can be: ENABLE or DISABLE.\r
1103   * @retval None\r
1104   */\r
1105 void RCC_AHBPeriphClockCmd(uint32_t RCC_AHBPeriph, FunctionalState NewState)\r
1106 {\r
1107   /* Check the parameters */\r
1108   assert_param(IS_RCC_AHB_PERIPH(RCC_AHBPeriph));\r
1109   assert_param(IS_FUNCTIONAL_STATE(NewState));\r
1110   \r
1111   if (NewState != DISABLE)\r
1112   {\r
1113     RCC->AHBENR |= RCC_AHBPeriph;\r
1114   }\r
1115   else\r
1116   {\r
1117     RCC->AHBENR &= ~RCC_AHBPeriph;\r
1118   }\r
1119 }\r
1120 \r
1121 /**\r
1122   * @brief  Enables or disables the High Speed APB (APB2) peripheral clock.\r
1123   * @note   After reset, the peripheral clock (used for registers read/write access)\r
1124   *         is disabled and the application software has to enable this clock before \r
1125   *         using it.\r
1126   * @param  RCC_APB2Periph: specifies the APB2 peripheral to gates its clock.\r
1127   *   This parameter can be any combination of the following values:\r
1128   *     @arg RCC_APB2Periph_SYSCFG: SYSCFG APB2 Clock.\r
1129   *     @arg RCC_APB2Periph_TIM9: TIM9 APB2 Clock.\r
1130   *     @arg RCC_APB2Periph_TIM10: TIM10 APB2 Clock.\r
1131   *     @arg RCC_APB2Periph_TIM11: TIM11 APB2 Clock.\r
1132   *     @arg RCC_APB2Periph_ADC1: ADC1 APB2 Clock.\r
1133   *     @arg RCC_APB2Periph_SDIO: SDIO APB2 Clock.\r
1134   *     @arg RCC_APB2Periph_SPI1: SPI1 APB2 Clock.\r
1135   *     @arg RCC_APB2Periph_USART1: USART1 APB2 Clock.\r
1136   * @param  NewState: new state of the specified peripheral clock.\r
1137   *         This parameter can be: ENABLE or DISABLE.\r
1138   * @retval None\r
1139   */\r
1140 void RCC_APB2PeriphClockCmd(uint32_t RCC_APB2Periph, FunctionalState NewState)\r
1141 {\r
1142   /* Check the parameters */\r
1143   assert_param(IS_RCC_APB2_PERIPH(RCC_APB2Periph));\r
1144   assert_param(IS_FUNCTIONAL_STATE(NewState));\r
1145 \r
1146   if (NewState != DISABLE)\r
1147   {\r
1148     RCC->APB2ENR |= RCC_APB2Periph;\r
1149   }\r
1150   else\r
1151   {\r
1152     RCC->APB2ENR &= ~RCC_APB2Periph;\r
1153   }\r
1154 }\r
1155 \r
1156 /**\r
1157   * @brief  Enables or disables the Low Speed APB (APB1) peripheral clock.\r
1158   * @note   After reset, the peripheral clock (used for registers read/write access)\r
1159   *         is disabled and the application software has to enable this clock before \r
1160   *         using it.\r
1161   * @param  RCC_APB1Periph: specifies the APB1 peripheral to gates its clock.\r
1162   *   This parameter can be any combination of the following values:\r
1163   *     @arg RCC_APB1Periph_TIM2:      TIM2 clock\r
1164   *     @arg RCC_APB1Periph_TIM3:      TIM3 clock\r
1165   *     @arg RCC_APB1Periph_TIM4:      TIM4 clock\r
1166   *     @arg RCC_APB1Periph_TIM5:      TIM5 clock  \r
1167   *     @arg RCC_APB1Periph_TIM6:      TIM6 clock\r
1168   *     @arg RCC_APB1Periph_TIM7:      TIM7 clock\r
1169   *     @arg RCC_APB1Periph_LCD:       LCD clock\r
1170   *     @arg RCC_APB1Periph_WWDG:      WWDG clock\r
1171   *     @arg RCC_APB1Periph_SPI2:      SPI2 clock\r
1172   *     @arg RCC_APB1Periph_SPI3:      SPI3 clock\r
1173   *     @arg RCC_APB1Periph_USART2:    USART2 clock\r
1174   *     @arg RCC_APB1Periph_USART3:    USART3 clock\r
1175   *     @arg RCC_APB1Periph_UART4:     UART4 clock\r
1176   *     @arg RCC_APB1Periph_UART5:     UART5 clock  \r
1177   *     @arg RCC_APB1Periph_I2C1:      I2C1 clock\r
1178   *     @arg RCC_APB1Periph_I2C2:      I2C2 clock\r
1179   *     @arg RCC_APB1Periph_USB:       USB clock\r
1180   *     @arg RCC_APB1Periph_PWR:       PWR clock\r
1181   *     @arg RCC_APB1Periph_DAC:       DAC clock\r
1182   *     @arg RCC_APB1Periph_COMP       COMP  clock\r
1183   * @param  NewState: new state of the specified peripheral clock.\r
1184   *         This parameter can be: ENABLE or DISABLE.\r
1185   * @retval None\r
1186   */\r
1187 void RCC_APB1PeriphClockCmd(uint32_t RCC_APB1Periph, FunctionalState NewState)\r
1188 {\r
1189   /* Check the parameters */\r
1190   assert_param(IS_RCC_APB1_PERIPH(RCC_APB1Periph));\r
1191   assert_param(IS_FUNCTIONAL_STATE(NewState));\r
1192 \r
1193   if (NewState != DISABLE)\r
1194   {\r
1195     RCC->APB1ENR |= RCC_APB1Periph;\r
1196   }\r
1197   else\r
1198   {\r
1199     RCC->APB1ENR &= ~RCC_APB1Periph;\r
1200   }\r
1201 }\r
1202 \r
1203 /**\r
1204   * @brief  Forces or releases AHB peripheral reset.\r
1205   * @param  RCC_AHBPeriph: specifies the AHB peripheral to reset.\r
1206   *   This parameter can be any combination of the following values:\r
1207   *     @arg RCC_AHBPeriph_GPIOA:         GPIOA clock\r
1208   *     @arg RCC_AHBPeriph_GPIOB:         GPIOB clock\r
1209   *     @arg RCC_AHBPeriph_GPIOC:         GPIOC clock  \r
1210   *     @arg RCC_AHBPeriph_GPIOD:         GPIOD clock\r
1211   *     @arg RCC_AHBPeriph_GPIOE:         GPIOE clock\r
1212   *     @arg RCC_AHBPeriph_GPIOH:         GPIOH clock\r
1213   *     @arg RCC_AHBPeriph_GPIOF:         GPIOF clock\r
1214   *     @arg RCC_AHBPeriph_GPIOG:         GPIOG clock  \r
1215   *     @arg RCC_AHBPeriph_CRC:           CRC clock\r
1216   *     @arg RCC_AHBPeriph_FLITF: (has effect only when the Flash memory is in power down mode)  \r
1217   *     @arg RCC_AHBPeriph_DMA1:          DMA1 clock\r
1218   *     @arg RCC_AHBPeriph_DMA2:          DMA2 clock\r
1219   *     @arg RCC_AHBPeriph_AES:           AES clock\r
1220   *     @arg RCC_AHBPeriph_FSMC:          FSMC clock  \r
1221   * @param  NewState: new state of the specified peripheral reset.\r
1222   *         This parameter can be: ENABLE or DISABLE.\r
1223   * @retval None\r
1224   */\r
1225 void RCC_AHBPeriphResetCmd(uint32_t RCC_AHBPeriph, FunctionalState NewState)\r
1226 {\r
1227   /* Check the parameters */\r
1228   assert_param(IS_RCC_AHB_PERIPH(RCC_AHBPeriph));\r
1229   assert_param(IS_FUNCTIONAL_STATE(NewState));\r
1230 \r
1231   if (NewState != DISABLE)\r
1232   {\r
1233     RCC->AHBRSTR |= RCC_AHBPeriph;\r
1234   }\r
1235   else\r
1236   {\r
1237     RCC->AHBRSTR &= ~RCC_AHBPeriph;\r
1238   }\r
1239 }\r
1240 \r
1241 /**\r
1242   * @brief  Forces or releases High Speed APB (APB2) peripheral reset.\r
1243   * @param  RCC_APB2Periph: specifies the APB2 peripheral to reset.\r
1244   *   This parameter can be any combination of the following values:\r
1245   *     @arg RCC_APB2Periph_SYSCFG:       SYSCFG clock\r
1246   *     @arg RCC_APB2Periph_TIM9:         TIM9 clock      \r
1247   *     @arg RCC_APB2Periph_TIM10:        TIM10 clock\r
1248   *     @arg RCC_APB2Periph_TIM11:        TIM11 clock\r
1249   *     @arg RCC_APB2Periph_ADC1:         ADC1 clock\r
1250   *     @arg RCC_APB2Periph_SDIO:         SDIO clock\r
1251   *     @arg RCC_APB2Periph_SPI1:         SPI1 clock\r
1252   *     @arg RCC_APB2Periph_USART1:       USART1 clock\r
1253   * @param  NewState: new state of the specified peripheral reset.\r
1254   *         This parameter can be: ENABLE or DISABLE.\r
1255   * @retval None\r
1256   */\r
1257 void RCC_APB2PeriphResetCmd(uint32_t RCC_APB2Periph, FunctionalState NewState)\r
1258 {\r
1259   /* Check the parameters */\r
1260   assert_param(IS_RCC_APB2_PERIPH(RCC_APB2Periph));\r
1261   assert_param(IS_FUNCTIONAL_STATE(NewState));\r
1262 \r
1263   if (NewState != DISABLE)\r
1264   {\r
1265     RCC->APB2RSTR |= RCC_APB2Periph;\r
1266   }\r
1267   else\r
1268   {\r
1269     RCC->APB2RSTR &= ~RCC_APB2Periph;\r
1270   }\r
1271 }\r
1272 \r
1273 /**\r
1274   * @brief  Forces or releases Low Speed APB (APB1) peripheral reset.\r
1275   * @param  RCC_APB1Periph: specifies the APB1 peripheral to reset.\r
1276   *   This parameter can be any combination of the following values:\r
1277   *     @arg RCC_APB1Periph_TIM2:           TIM2 clock\r
1278   *     @arg RCC_APB1Periph_TIM3:           TIM3 clock\r
1279   *     @arg RCC_APB1Periph_TIM4:           TIM4 clock\r
1280   *     @arg RCC_APB1Periph_TIM5:           TIM5 clock  \r
1281   *     @arg RCC_APB1Periph_TIM6:           TIM6 clock\r
1282   *     @arg RCC_APB1Periph_TIM7:           TIM7 clock\r
1283   *     @arg RCC_APB1Periph_LCD:            LCD clock\r
1284   *     @arg RCC_APB1Periph_WWDG:           WWDG clock\r
1285   *     @arg RCC_APB1Periph_SPI2:           SPI2 clock\r
1286   *     @arg RCC_APB1Periph_SPI3:           SPI3 clock  \r
1287   *     @arg RCC_APB1Periph_USART2:         USART2 clock\r
1288   *     @arg RCC_APB1Periph_USART3:         USART3 clock\r
1289   *     @arg RCC_APB1Periph_UART4:          UART4 clock\r
1290   *     @arg RCC_APB1Periph_UART5:          UART5 clock  \r
1291   *     @arg RCC_APB1Periph_I2C1:           I2C1 clock\r
1292   *     @arg RCC_APB1Periph_I2C2:           I2C2 clock\r
1293   *     @arg RCC_APB1Periph_USB:            USB clock\r
1294   *     @arg RCC_APB1Periph_PWR:            PWR clock\r
1295   *     @arg RCC_APB1Periph_DAC:            DAC clock\r
1296   *     @arg RCC_APB1Periph_COMP    \r
1297   * @param  NewState: new state of the specified peripheral clock.\r
1298   *         This parameter can be: ENABLE or DISABLE.\r
1299   * @retval None\r
1300   */\r
1301 void RCC_APB1PeriphResetCmd(uint32_t RCC_APB1Periph, FunctionalState NewState)\r
1302 {\r
1303   /* Check the parameters */\r
1304   assert_param(IS_RCC_APB1_PERIPH(RCC_APB1Periph));\r
1305   assert_param(IS_FUNCTIONAL_STATE(NewState));\r
1306 \r
1307   if (NewState != DISABLE)\r
1308   {\r
1309     RCC->APB1RSTR |= RCC_APB1Periph;\r
1310   }\r
1311   else\r
1312   {\r
1313     RCC->APB1RSTR &= ~RCC_APB1Periph;\r
1314   }\r
1315 }\r
1316 \r
1317 /**\r
1318   * @brief  Enables or disables the AHB peripheral clock during SLEEP mode.\r
1319   * @note     Peripheral clock gating in SLEEP mode can be used to further reduce\r
1320   *           power consumption.\r
1321   *         - After wakeup from SLEEP mode, the peripheral clock is enabled again.\r
1322   *         - By default, all peripheral clocks are enabled during SLEEP mode. \r
1323   * @param  RCC_AHBPeriph: specifies the AHB peripheral to gates its clock.\r
1324   *   This parameter can be any combination of the following values:\r
1325   *     @arg RCC_AHBPeriph_GPIOA:           GPIOA clock\r
1326   *     @arg RCC_AHBPeriph_GPIOB:           GPIOB clock\r
1327   *     @arg RCC_AHBPeriph_GPIOC:           GPIOC clock  \r
1328   *     @arg RCC_AHBPeriph_GPIOD:           GPIOD clock\r
1329   *     @arg RCC_AHBPeriph_GPIOE:           GPIOE clock\r
1330   *     @arg RCC_AHBPeriph_GPIOH:           GPIOH clock\r
1331   *     @arg RCC_AHBPeriph_GPIOF:           GPIOF clock\r
1332   *     @arg RCC_AHBPeriph_GPIOG:           GPIOG clock  \r
1333   *     @arg RCC_AHBPeriph_CRC:             CRC clock\r
1334   *     @arg RCC_AHBPeriph_FLITF: (has effect only when the Flash memory is in power down mode)  \r
1335   *     @arg RCC_AHBPeriph_SRAM:            SRAM clock     \r
1336   *     @arg RCC_AHBPeriph_DMA1:            DMA1 clock\r
1337   *     @arg RCC_AHBPeriph_DMA2:            DMA2 clock\r
1338   *     @arg RCC_AHBPeriph_AES:             AES clock\r
1339   *     @arg RCC_AHBPeriph_FSMC:            FSMC clock\r
1340   * @param  NewState: new state of the specified peripheral clock.\r
1341   *         This parameter can be: ENABLE or DISABLE.\r
1342   * @retval None\r
1343   */\r
1344 void RCC_AHBPeriphClockLPModeCmd(uint32_t RCC_AHBPeriph, FunctionalState NewState)\r
1345 {\r
1346   /* Check the parameters */\r
1347   assert_param(IS_RCC_AHB_LPMODE_PERIPH(RCC_AHBPeriph));\r
1348   assert_param(IS_FUNCTIONAL_STATE(NewState));\r
1349   \r
1350   if (NewState != DISABLE)\r
1351   {\r
1352     RCC->AHBLPENR |= RCC_AHBPeriph;\r
1353   }\r
1354   else\r
1355   {\r
1356     RCC->AHBLPENR &= ~RCC_AHBPeriph;\r
1357   }\r
1358 }\r
1359 \r
1360 /**\r
1361   * @brief  Enables or disables the APB2 peripheral clock during SLEEP mode.\r
1362   * @note     Peripheral clock gating in SLEEP mode can be used to further reduce\r
1363   *           power consumption.\r
1364   * @note     After wakeup from SLEEP mode, the peripheral clock is enabled again.\r
1365   * @note     By default, all peripheral clocks are enabled during SLEEP mode. \r
1366   * @param  RCC_APB2Periph: specifies the APB2 peripheral to gates its clock.\r
1367   *   This parameter can be any combination of the following values:\r
1368   *     @arg RCC_APB2Periph_SYSCFG:          SYSCFG clock\r
1369   *     @arg RCC_APB2Periph_TIM9:            TIM9 clock\r
1370   *     @arg RCC_APB2Periph_TIM10:           TIM10 clock\r
1371   *     @arg RCC_APB2Periph_TIM11:           TIM11 clock\r
1372   *     @arg RCC_APB2Periph_ADC1:            ADC1 clock\r
1373   *     @arg RCC_APB2Periph_SDIO:            SDIO clock  \r
1374   *     @arg RCC_APB2Periph_SPI1:            SPI1 clock\r
1375   *     @arg RCC_APB2Periph_USART1:          USART1 clock\r
1376   * @param  NewState: new state of the specified peripheral clock.\r
1377   *         This parameter can be: ENABLE or DISABLE.\r
1378   * @retval None\r
1379   */\r
1380 void RCC_APB2PeriphClockLPModeCmd(uint32_t RCC_APB2Periph, FunctionalState NewState)\r
1381 {\r
1382   /* Check the parameters */\r
1383   assert_param(IS_RCC_APB2_PERIPH(RCC_APB2Periph));\r
1384   assert_param(IS_FUNCTIONAL_STATE(NewState));\r
1385   \r
1386   if (NewState != DISABLE)\r
1387   {\r
1388     RCC->APB2LPENR |= RCC_APB2Periph;\r
1389   }\r
1390   else\r
1391   {\r
1392     RCC->APB2LPENR &= ~RCC_APB2Periph;\r
1393   }\r
1394 }\r
1395 \r
1396 /**\r
1397   * @brief  Enables or disables the APB1 peripheral clock during SLEEP mode.\r
1398   * @note     Peripheral clock gating in SLEEP mode can be used to further reduce\r
1399   *           power consumption.\r
1400   * @note     After wakeup from SLEEP mode, the peripheral clock is enabled again.\r
1401   * @note     By default, all peripheral clocks are enabled during SLEEP mode.        \r
1402   * @param  RCC_APB1Periph: specifies the APB1 peripheral to gates its clock.\r
1403   *   This parameter can be any combination of the following values:\r
1404   *     @arg RCC_APB1Periph_TIM2:                 TIM2 clock\r
1405   *     @arg RCC_APB1Periph_TIM3:                 TIM3 clock\r
1406   *     @arg RCC_APB1Periph_TIM4:                 TIM4 clock\r
1407   *     @arg RCC_APB1Periph_TIM5:                 TIM5 clock\r
1408   *     @arg RCC_APB1Periph_TIM6:                 TIM6 clock\r
1409   *     @arg RCC_APB1Periph_TIM7:                 TIM7 clock\r
1410   *     @arg RCC_APB1Periph_LCD:                  LCD clock\r
1411   *     @arg RCC_APB1Periph_WWDG:                 WWDG clock\r
1412   *     @arg RCC_APB1Periph_SPI2:                 SPI2 clock\r
1413   *     @arg RCC_APB1Periph_SPI3:                 SPI3 clock\r
1414   *     @arg RCC_APB1Periph_USART2:               USART2 clock\r
1415   *     @arg RCC_APB1Periph_USART3:               USART3 clock\r
1416   *     @arg RCC_APB1Periph_UART4:                UART4 clock\r
1417   *     @arg RCC_APB1Periph_UART5:                UART5 clock  \r
1418   *     @arg RCC_APB1Periph_I2C1:                 I2C1 clock\r
1419   *     @arg RCC_APB1Periph_I2C2:                 I2C2 clock\r
1420   *     @arg RCC_APB1Periph_USB:                  USB clock\r
1421   *     @arg RCC_APB1Periph_PWR:                  PWR clock\r
1422   *     @arg RCC_APB1Periph_DAC:                  DAC clock\r
1423   *     @arg RCC_APB1Periph_COMP:                 COMP clock\r
1424   * @param  NewState: new state                                 \r
1425   * @param  NewState: new state of the specified peripheral clock.\r
1426   *         This parameter can be: ENABLE or DISABLE.\r
1427   * @retval None\r
1428   */\r
1429 void RCC_APB1PeriphClockLPModeCmd(uint32_t RCC_APB1Periph, FunctionalState NewState)\r
1430 {\r
1431   /* Check the parameters */\r
1432   assert_param(IS_RCC_APB1_PERIPH(RCC_APB1Periph));\r
1433   assert_param(IS_FUNCTIONAL_STATE(NewState));\r
1434   \r
1435   if (NewState != DISABLE)\r
1436   {\r
1437     RCC->APB1LPENR |= RCC_APB1Periph;\r
1438   }\r
1439   else\r
1440   {\r
1441     RCC->APB1LPENR &= ~RCC_APB1Periph;\r
1442   }\r
1443 }\r
1444 \r
1445 /**\r
1446   * @}\r
1447   */\r
1448 \r
1449 /** @defgroup RCC_Group4 Interrupts and flags management functions\r
1450  *  @brief   Interrupts and flags management functions \r
1451  *\r
1452 @verbatim\r
1453  ===============================================================================\r
1454              ##### Interrupts and flags management functions #####\r
1455  ===============================================================================\r
1456 \r
1457 @endverbatim\r
1458   * @{\r
1459   */\r
1460 \r
1461 /**\r
1462   * @brief  Enables or disables the specified RCC interrupts.\r
1463   * @note   The CSS interrupt doesn't have an enable bit; once the CSS is enabled\r
1464   *         and if the HSE clock fails, the CSS interrupt occurs and an NMI is\r
1465   *         automatically generated. The NMI will be executed indefinitely, and \r
1466   *         since NMI has higher priority than any other IRQ (and main program)\r
1467   *         the application will be stacked in the NMI ISR unless the CSS interrupt\r
1468   *         pending bit is cleared.\r
1469   * @param  RCC_IT: specifies the RCC interrupt sources to be enabled or disabled.\r
1470   *   This parameter can be any combination of the following values:\r
1471   *     @arg RCC_IT_LSIRDY: LSI ready interrupt\r
1472   *     @arg RCC_IT_LSERDY: LSE ready interrupt\r
1473   *     @arg RCC_IT_HSIRDY: HSI ready interrupt\r
1474   *     @arg RCC_IT_HSERDY: HSE ready interrupt\r
1475   *     @arg RCC_IT_PLLRDY: PLL ready interrupt\r
1476   *     @arg RCC_IT_MSIRDY: MSI ready interrupt\r
1477   *     @arg RCC_IT_LSECSS: LSE CSS interrupt  \r
1478   * @param  NewState: new state of the specified RCC interrupts.\r
1479   *   This parameter can be: ENABLE or DISABLE.\r
1480   * @retval None\r
1481   */\r
1482 void RCC_ITConfig(uint8_t RCC_IT, FunctionalState NewState)\r
1483 {\r
1484   /* Check the parameters */\r
1485   assert_param(IS_RCC_IT(RCC_IT));\r
1486   assert_param(IS_FUNCTIONAL_STATE(NewState));\r
1487   \r
1488   if (NewState != DISABLE)\r
1489   {\r
1490     /* Perform Byte access to RCC_CIR[12:8] bits to enable the selected interrupts */\r
1491     *(__IO uint8_t *) CIR_BYTE2_ADDRESS |= RCC_IT;\r
1492   }\r
1493   else\r
1494   {\r
1495     /* Perform Byte access to RCC_CIR[12:8] bits to disable the selected interrupts */\r
1496     *(__IO uint8_t *) CIR_BYTE2_ADDRESS &= (uint8_t)~RCC_IT;\r
1497   }\r
1498 }\r
1499 \r
1500 /**\r
1501   * @brief  Checks whether the specified RCC flag is set or not.\r
1502   * @param  RCC_FLAG: specifies the flag to check.\r
1503   *   This parameter can be one of the following values:\r
1504   *     @arg RCC_FLAG_HSIRDY: HSI oscillator clock ready\r
1505   *     @arg RCC_FLAG_MSIRDY: MSI oscillator clock ready  \r
1506   *     @arg RCC_FLAG_HSERDY: HSE oscillator clock ready\r
1507   *     @arg RCC_FLAG_PLLRDY: PLL clock ready\r
1508   *     @arg RCC_FLAG_LSECSS: LSE oscillator clock CSS detected  \r
1509   *     @arg RCC_FLAG_LSERDY: LSE oscillator clock ready\r
1510   *     @arg RCC_FLAG_LSIRDY: LSI oscillator clock ready\r
1511   *     @arg RCC_FLAG_OBLRST: Option Byte Loader (OBL) reset \r
1512   *     @arg RCC_FLAG_PINRST: Pin reset\r
1513   *     @arg RCC_FLAG_PORRST: POR/PDR reset\r
1514   *     @arg RCC_FLAG_SFTRST: Software reset\r
1515   *     @arg RCC_FLAG_IWDGRST: Independent Watchdog reset\r
1516   *     @arg RCC_FLAG_WWDGRST: Window Watchdog reset\r
1517   *     @arg RCC_FLAG_LPWRRST: Low Power reset\r
1518   * @retval The new state of RCC_FLAG (SET or RESET).\r
1519   */\r
1520 FlagStatus RCC_GetFlagStatus(uint8_t RCC_FLAG)\r
1521 {\r
1522   uint32_t tmp = 0;\r
1523   uint32_t statusreg = 0;\r
1524   FlagStatus bitstatus = RESET;\r
1525 \r
1526   /* Check the parameters */\r
1527   assert_param(IS_RCC_FLAG(RCC_FLAG));\r
1528 \r
1529   /* Get the RCC register index */\r
1530   tmp = RCC_FLAG >> 5;\r
1531 \r
1532   if (tmp == 1)               /* The flag to check is in CR register */\r
1533   {\r
1534     statusreg = RCC->CR;\r
1535   }\r
1536   else          /* The flag to check is in CSR register (tmp == 2) */\r
1537   {\r
1538     statusreg = RCC->CSR;\r
1539   }\r
1540 \r
1541   /* Get the flag position */\r
1542   tmp = RCC_FLAG & FLAG_MASK;\r
1543 \r
1544   if ((statusreg & ((uint32_t)1 << tmp)) != (uint32_t)RESET)\r
1545   {\r
1546     bitstatus = SET;\r
1547   }\r
1548   else\r
1549   {\r
1550     bitstatus = RESET;\r
1551   }\r
1552   /* Return the flag status */\r
1553   return bitstatus;\r
1554 }\r
1555 \r
1556 /**\r
1557   * @brief  Clears the RCC reset flags.\r
1558   *         The reset flags are: RCC_FLAG_OBLRST, RCC_FLAG_PINRST, RCC_FLAG_PORRST, \r
1559   *         RCC_FLAG_SFTRST, RCC_FLAG_IWDGRST, RCC_FLAG_WWDGRST, RCC_FLAG_LPWRRST.\r
1560   * @param  None\r
1561   * @retval None\r
1562   */\r
1563 void RCC_ClearFlag(void)\r
1564 {\r
1565   /* Set RMVF bit to clear the reset flags */\r
1566   RCC->CSR |= RCC_CSR_RMVF;\r
1567 }\r
1568 \r
1569 /**\r
1570   * @brief  Checks whether the specified RCC interrupt has occurred or not.\r
1571   * @param  RCC_IT: specifies the RCC interrupt source to check.\r
1572   *   This parameter can be one of the following values:\r
1573   *     @arg RCC_IT_LSIRDY: LSI ready interrupt\r
1574   *     @arg RCC_IT_LSERDY: LSE ready interrupt  \r
1575   *     @arg RCC_IT_HSIRDY: HSI ready interrupt\r
1576   *     @arg RCC_IT_HSERDY: HSE ready interrupt\r
1577   *     @arg RCC_IT_PLLRDY: PLL ready interrupt\r
1578   *     @arg RCC_IT_MSIRDY: MSI ready interrupt\r
1579   *     @arg RCC_IT_LSECSS: LSE CSS interrupt \r
1580   *     @arg RCC_IT_CSS: Clock Security System interrupt\r
1581   * @retval The new state of RCC_IT (SET or RESET).\r
1582   */\r
1583 ITStatus RCC_GetITStatus(uint8_t RCC_IT)\r
1584 {\r
1585   ITStatus bitstatus = RESET;\r
1586   /* Check the parameters */\r
1587   assert_param(IS_RCC_GET_IT(RCC_IT));\r
1588   \r
1589   /* Check the status of the specified RCC interrupt */\r
1590   if ((RCC->CIR & RCC_IT) != (uint32_t)RESET)\r
1591   {\r
1592     bitstatus = SET;\r
1593   }\r
1594   else\r
1595   {\r
1596     bitstatus = RESET;\r
1597   }\r
1598   /* Return the RCC_IT status */\r
1599   return  bitstatus;\r
1600 }\r
1601 \r
1602 /**\r
1603   * @brief  Clears the RCC's interrupt pending bits.\r
1604   * @param  RCC_IT: specifies the interrupt pending bit to clear.\r
1605   *   This parameter can be any combination of the following values:\r
1606   *     @arg RCC_IT_LSIRDY: LSI ready interrupt\r
1607   *     @arg RCC_IT_LSERDY: LSE ready interrupt\r
1608   *     @arg RCC_IT_HSIRDY: HSI ready interrupt\r
1609   *     @arg RCC_IT_HSERDY: HSE ready interrupt\r
1610   *     @arg RCC_IT_PLLRDY: PLL ready interrupt\r
1611   *     @arg RCC_IT_MSIRDY: MSI ready interrupt \r
1612   *     @arg RCC_IT_LSECSS: LSE CSS interrupt\r
1613   *     @arg RCC_IT_CSS: Clock Security System interrupt\r
1614   * @retval None\r
1615   */\r
1616 void RCC_ClearITPendingBit(uint8_t RCC_IT)\r
1617 {\r
1618   /* Check the parameters */\r
1619   assert_param(IS_RCC_CLEAR_IT(RCC_IT));\r
1620   \r
1621   /* Perform Byte access to RCC_CIR[23:16] bits to clear the selected interrupt\r
1622      pending bits */\r
1623   *(__IO uint8_t *) CIR_BYTE3_ADDRESS = RCC_IT;\r
1624 }\r
1625 \r
1626 /**\r
1627   * @}\r
1628   */\r
1629 \r
1630 /**\r
1631   * @}\r
1632   */\r
1633 \r
1634 /**\r
1635   * @}\r
1636   */\r
1637 \r
1638 /**\r
1639   * @}\r
1640   */\r
1641 \r
1642 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/\r