Determine the device parameters explicit after running
[fw/stlink] / example / 32l_dac / system_stm32l1xx.c
1 /**\r
2   ******************************************************************************\r
3   * @file    system_stm32l1xx.c\r
4   * @author  MCD Application Team\r
5   * @version V1.0.0\r
6   * @date    2-June-2011\r
7   * @brief   CMSIS Cortex-M3 Device Peripheral Access Layer System Source File.\r
8   *          This file contains the system clock configuration for STM32L1xx Ultra\r
9   *          Low Medium-density devices, and is generated by the clock configuration\r
10   *          tool "STM32L1xx_Clock_Configuration_V1.0.0.xls".\r
11   *             \r
12   * 1.  This file provides two functions and one global variable to be called from \r
13   *     user application:\r
14   *      - SystemInit(): Setups the system clock (System clock source, PLL Multiplier\r
15   *                      and Divider factors, AHB/APBx prescalers and Flash settings),\r
16   *                      depending on the configuration made in the clock xls tool. \r
17   *                      This function is called at startup just after reset and \r
18   *                      before branch to main program. This call is made inside\r
19   *                      the "startup_stm32l1xx_md.s" file.\r
20   *                        \r
21   *      - SystemCoreClock variable: Contains the core clock (HCLK), it can be used\r
22   *                                  by the user application to setup the SysTick \r
23   *                                  timer or configure other parameters.\r
24   *                                     \r
25   *      - SystemCoreClockUpdate(): Updates the variable SystemCoreClock and must\r
26   *                                 be called whenever the core clock is changed\r
27   *                                 during program execution.   \r
28   *      \r
29   * 2. After each device reset the MSI (2.1 MHz Range) is used as system clock source.\r
30   *    Then SystemInit() function is called, in "startup_stm32l1xx_md.s" file, to\r
31   *    configure the system clock before to branch to main program.    \r
32   *    \r
33   * 3. If the system clock source selected by user fails to startup, the SystemInit()\r
34   *    function will do nothing and MSI still used as system clock source. User can \r
35   *    add some code to deal with this issue inside the SetSysClock() function.       \r
36   * \r
37   * 4. The default value of HSE crystal is set to 8MHz, refer to "HSE_VALUE" define\r
38   *    in "stm32l1xx.h" file. When HSE is used as system clock source, directly or\r
39   *    through PLL, and you are using different crystal you have to adapt the HSE\r
40   *    value to your own configuration.\r
41   * \r
42   * 5. This file configures the system clock as follows:  \r
43   *=============================================================================\r
44   *                         System Clock Configuration\r
45   *=============================================================================\r
46   *        System clock source          | HSI\r
47   *----------------------------------------------------------------------------- \r
48   *        SYSCLK                       | 16000000 Hz\r
49   *----------------------------------------------------------------------------- \r
50   *        HCLK                         | 16000000 Hz\r
51   *----------------------------------------------------------------------------- \r
52   *        AHB Prescaler                | 1\r
53   *----------------------------------------------------------------------------- \r
54   *        APB1 Prescaler               | 1\r
55   *----------------------------------------------------------------------------- \r
56   *        APB2 Prescaler               | 1\r
57   *----------------------------------------------------------------------------- \r
58   *        HSE Frequency                | 8000000 Hz\r
59   *----------------------------------------------------------------------------- \r
60   *        PLL DIV                      | Not Used\r
61   *----------------------------------------------------------------------------- \r
62   *        PLL MUL                      | Not Used\r
63   *----------------------------------------------------------------------------- \r
64   *        VDD                          | 3.3 V\r
65   *----------------------------------------------------------------------------- \r
66   *        Vcore                        | 1.8 V (Range 1)\r
67   *----------------------------------------------------------------------------- \r
68   *        Flash Latency                | 0 WS\r
69   *----------------------------------------------------------------------------- \r
70   *        Require 48MHz for USB clock  | Disabled\r
71   *----------------------------------------------------------------------------- \r
72   *=============================================================================\r
73   ****************************************************************************** \r
74   * @attention\r
75   *\r
76   * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS\r
77   * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE\r
78   * TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY\r
79   * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING\r
80   * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE\r
81   * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.\r
82   *\r
83   * <h2><center>&copy; COPYRIGHT 2010 STMicroelectronics</center></h2>\r
84   ******************************************************************************  \r
85   */\r
86 \r
87 /** @addtogroup CMSIS\r
88   * @{\r
89   */\r
90 \r
91 /** @addtogroup stm32l1xx_system\r
92   * @{\r
93   */  \r
94   \r
95 /** @addtogroup STM32L1xx_System_Private_Includes\r
96   * @{\r
97   */\r
98 \r
99 #include "stm32l1xx.h"\r
100 \r
101 /**\r
102   * @}\r
103   */\r
104 \r
105 /** @addtogroup STM32L1xx_System_Private_TypesDefinitions\r
106   * @{\r
107   */\r
108 \r
109 /**\r
110   * @}\r
111   */\r
112 \r
113 /** @addtogroup STM32L1xx_System_Private_Defines\r
114   * @{\r
115   */\r
116 /*!< Uncomment the following line if you need to relocate your vector Table in\r
117      Internal SRAM. */ \r
118 /* #define VECT_TAB_SRAM */\r
119 #define VECT_TAB_OFFSET  0x0 /*!< Vector Table base offset field. \r
120                                   This value must be a multiple of 0x200. */\r
121 /**\r
122   * @}\r
123   */\r
124 \r
125 /** @addtogroup STM32L1xx_System_Private_Macros\r
126   * @{\r
127   */\r
128 \r
129 /**\r
130   * @}\r
131   */\r
132 \r
133 /** @addtogroup STM32L1xx_System_Private_Variables\r
134   * @{\r
135   */\r
136 uint32_t SystemCoreClock    = 16000000;\r
137 __I uint8_t PLLMulTable[9] = {3, 4, 6, 8, 12, 16, 24, 32, 48};\r
138 __I uint8_t AHBPrescTable[16] = {0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 4, 6, 7, 8, 9};\r
139 \r
140 /**\r
141   * @}\r
142   */\r
143 \r
144 /** @addtogroup STM32L1xx_System_Private_FunctionPrototypes\r
145   * @{\r
146   */\r
147 \r
148 static void SetSysClock(void);\r
149 \r
150 /**\r
151   * @}\r
152   */\r
153 \r
154 /** @addtogroup STM32L1xx_System_Private_Functions\r
155   * @{\r
156   */\r
157 \r
158 /**\r
159   * @brief  Setup the microcontroller system.\r
160   *         Initialize the Embedded Flash Interface, the PLL and update the \r
161   *         SystemCoreClock variable.\r
162   * @param  None\r
163   * @retval None\r
164   */\r
165 void SystemInit (void)\r
166 {\r
167   /*!< Set MSION bit */\r
168   RCC->CR |= (uint32_t)0x00000100;\r
169 \r
170   /*!< Reset SW[1:0], HPRE[3:0], PPRE1[2:0], PPRE2[2:0], MCOSEL[2:0] and MCOPRE[2:0] bits */\r
171   RCC->CFGR &= (uint32_t)0x88FFC00C;\r
172   \r
173   /*!< Reset HSION, HSEON, CSSON and PLLON bits */\r
174   RCC->CR &= (uint32_t)0xEEFEFFFE;\r
175 \r
176   /*!< Reset HSEBYP bit */\r
177   RCC->CR &= (uint32_t)0xFFFBFFFF;\r
178 \r
179   /*!< Reset PLLSRC, PLLMUL[3:0] and PLLDIV[1:0] bits */\r
180   RCC->CFGR &= (uint32_t)0xFF02FFFF;\r
181 \r
182   /*!< Disable all interrupts */\r
183   RCC->CIR = 0x00000000;\r
184     \r
185   /* Configure the System clock frequency, AHB/APBx prescalers and Flash settings */\r
186   SetSysClock();\r
187 \r
188 #ifdef VECT_TAB_SRAM\r
189   SCB->VTOR = SRAM_BASE | VECT_TAB_OFFSET; /* Vector Table Relocation in Internal SRAM. */\r
190 #else\r
191   SCB->VTOR = FLASH_BASE | VECT_TAB_OFFSET; /* Vector Table Relocation in Internal FLASH. */\r
192 #endif\r
193 }\r
194 \r
195 /**\r
196   * @brief  Update SystemCoreClock according to Clock Register Values\r
197   * @note   - The system frequency computed by this function is not the real \r
198   *           frequency in the chip. It is calculated based on the predefined \r
199   *           constant and the selected clock source:\r
200   *             \r
201   *           - If SYSCLK source is MSI, SystemCoreClock will contain the MSI \r
202   *             value as defined by the MSI range.\r
203   *                                   \r
204   *           - If SYSCLK source is HSI, SystemCoreClock will contain the HSI_VALUE(*)\r
205   *                                              \r
206   *           - If SYSCLK source is HSE, SystemCoreClock will contain the HSE_VALUE(**)\r
207   *                          \r
208   *           - If SYSCLK source is PLL, SystemCoreClock will contain the HSE_VALUE(**) \r
209   *             or HSI_VALUE(*) multiplied/divided by the PLL factors.\r
210   *         \r
211   *         (*) HSI_VALUE is a constant defined in stm32l1xx.h file (default value\r
212   *             16 MHz) but the real value may vary depending on the variations\r
213   *             in voltage and temperature.   \r
214   *    \r
215   *         (**) HSE_VALUE is a constant defined in stm32l1xx.h file (default value\r
216   *              8 MHz), user has to ensure that HSE_VALUE is same as the real\r
217   *              frequency of the crystal used. Otherwise, this function may\r
218   *              have wrong result.\r
219   *                \r
220   *         - The result of this function could be not correct when using fractional\r
221   *           value for HSE crystal.  \r
222   * @param  None\r
223   * @retval None\r
224   */\r
225 void SystemCoreClockUpdate (void)\r
226 {\r
227   uint32_t tmp = 0, pllmul = 0, plldiv = 0, pllsource = 0, msirange = 0;\r
228 \r
229   /* Get SYSCLK source -------------------------------------------------------*/\r
230   tmp = RCC->CFGR & RCC_CFGR_SWS;\r
231   \r
232   switch (tmp)\r
233   {\r
234     case 0x00:  /* MSI used as system clock */\r
235       msirange = (RCC->ICSCR & RCC_ICSCR_MSIRANGE) >> 13;\r
236       SystemCoreClock = (32768 * (1 << (msirange + 1)));\r
237       break;\r
238     case 0x04:  /* HSI used as system clock */\r
239       SystemCoreClock = HSI_VALUE;\r
240       break;\r
241     case 0x08:  /* HSE used as system clock */\r
242       SystemCoreClock = HSE_VALUE;\r
243       break;\r
244     case 0x0C:  /* PLL used as system clock */\r
245       /* Get PLL clock source and multiplication factor ----------------------*/\r
246       pllmul = RCC->CFGR & RCC_CFGR_PLLMUL;\r
247       plldiv = RCC->CFGR & RCC_CFGR_PLLDIV;\r
248       pllmul = PLLMulTable[(pllmul >> 18)];\r
249       plldiv = (plldiv >> 22) + 1;\r
250       \r
251       pllsource = RCC->CFGR & RCC_CFGR_PLLSRC;\r
252 \r
253       if (pllsource == 0x00)\r
254       {\r
255         /* HSI oscillator clock selected as PLL clock entry */\r
256         SystemCoreClock = (((HSI_VALUE) * pllmul) / plldiv);\r
257       }\r
258       else\r
259       {\r
260         /* HSE selected as PLL clock entry */\r
261         SystemCoreClock = (((HSE_VALUE) * pllmul) / plldiv);\r
262       }\r
263       break;\r
264     default: /* MSI used as system clock */\r
265       msirange = (RCC->ICSCR & RCC_ICSCR_MSIRANGE) >> 13;\r
266       SystemCoreClock = (32768 * (1 << (msirange + 1)));\r
267       break;\r
268   }\r
269   /* Compute HCLK clock frequency --------------------------------------------*/\r
270   /* Get HCLK prescaler */\r
271   tmp = AHBPrescTable[((RCC->CFGR & RCC_CFGR_HPRE) >> 4)];\r
272   /* HCLK clock frequency */\r
273   SystemCoreClock >>= tmp;\r
274 }\r
275 \r
276 /**\r
277   * @brief  Configures the System clock frequency, AHB/APBx prescalers and Flash \r
278   *         settings.\r
279   * @note   This function should be called only once the RCC clock configuration  \r
280   *         is reset to the default reset state (done in SystemInit() function).             \r
281   * @param  None\r
282   * @retval None\r
283   */\r
284 static void SetSysClock(void)\r
285 {\r
286   __IO uint32_t StartUpCounter = 0, HSIStatus = 0;\r
287   \r
288   /* SYSCLK, HCLK, PCLK2 and PCLK1 configuration ---------------------------*/\r
289   /* Enable HSI */\r
290   RCC->CR |= ((uint32_t)RCC_CR_HSION);\r
291  \r
292   /* Wait till HSI is ready and if Time out is reached exit */\r
293   do\r
294   {\r
295     HSIStatus = RCC->CR & RCC_CR_HSIRDY;\r
296   } while((HSIStatus == 0) && (StartUpCounter != HSI_STARTUP_TIMEOUT));\r
297 \r
298   if ((RCC->CR & RCC_CR_HSIRDY) != RESET)\r
299   {\r
300     HSIStatus = (uint32_t)0x01;\r
301   }\r
302   else\r
303   {\r
304     HSIStatus = (uint32_t)0x00;\r
305   }\r
306     \r
307   if (HSIStatus == (uint32_t)0x01)\r
308   {\r
309     /* Flash 0 wait state */\r
310     FLASH->ACR &= ~FLASH_ACR_LATENCY;\r
311     \r
312     /* Disable Prefetch Buffer */\r
313     FLASH->ACR &= ~FLASH_ACR_PRFTEN;\r
314 \r
315     /* Disable 64-bit access */\r
316     FLASH->ACR &= ~FLASH_ACR_ACC64;\r
317     \r
318 \r
319     /* Power enable */\r
320     RCC->APB1ENR |= RCC_APB1ENR_PWREN;\r
321   \r
322     /* Select the Voltage Range 1 (1.8 V) */\r
323     PWR->CR = PWR_CR_VOS_0;\r
324   \r
325   \r
326     /* Wait Until the Voltage Regulator is ready */\r
327     while((PWR->CSR & PWR_CSR_VOSF) != RESET)\r
328     {\r
329     }\r
330       \r
331     /* HCLK = SYSCLK /1*/\r
332     RCC->CFGR |= (uint32_t)RCC_CFGR_HPRE_DIV1;\r
333     /* PCLK2 = HCLK /1*/\r
334     RCC->CFGR |= (uint32_t)RCC_CFGR_PPRE2_DIV1;\r
335     \r
336     /* PCLK1 = HCLK /1*/\r
337     RCC->CFGR |= (uint32_t)RCC_CFGR_PPRE1_DIV1;\r
338     \r
339     /* Select HSI as system clock source */\r
340     RCC->CFGR &= (uint32_t)((uint32_t)~(RCC_CFGR_SW));\r
341     RCC->CFGR |= (uint32_t)RCC_CFGR_SW_HSI;\r
342 \r
343     /* Wait till HSI is used as system clock source */\r
344     while ((RCC->CFGR & (uint32_t)RCC_CFGR_SWS) != (uint32_t)RCC_CFGR_SWS_HSI)\r
345     {\r
346     }\r
347   }\r
348   else\r
349   {\r
350     /* If HSI fails to start-up, the application will have wrong clock\r
351        configuration. User can add here some code to deal with this error */\r
352   }\r
353 }\r
354 \r
355 /**\r
356   * @}\r
357   */\r
358 \r
359 /**\r
360   * @}\r
361   */\r
362 \r
363 /**\r
364   * @}\r
365   */\r
366 \r
367 /******************* (C) COPYRIGHT 2010 STMicroelectronics *****END OF FILE****/\r