Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
50 | mjames | 1 | /** |
2 | ****************************************************************************** |
||
3 | * @file system_stm32l1xx.c |
||
4 | * @author MCD Application Team |
||
5 | * @brief CMSIS Cortex-M3 Device Peripheral Access Layer System Source File. |
||
6 | * |
||
7 | * This file provides two functions and one global variable to be called from |
||
8 | * user application: |
||
9 | * - SystemInit(): This function is called at startup just after reset and |
||
10 | * before branch to main program. This call is made inside |
||
11 | * the "startup_stm32l1xx.s" file. |
||
12 | * |
||
13 | * - SystemCoreClock variable: Contains the core clock (HCLK), it can be used |
||
14 | * by the user application to setup the SysTick |
||
15 | * timer or configure other parameters. |
||
16 | * |
||
17 | * - SystemCoreClockUpdate(): Updates the variable SystemCoreClock and must |
||
18 | * be called whenever the core clock is changed |
||
19 | * during program execution. |
||
20 | * |
||
21 | ****************************************************************************** |
||
22 | * @attention |
||
23 | * |
||
24 | * <h2><center>© Copyright (c) 2017 STMicroelectronics. |
||
25 | * All rights reserved.</center></h2> |
||
26 | * |
||
27 | * This software component is licensed by ST under BSD 3-Clause license, |
||
28 | * the "License"; You may not use this file except in compliance with the |
||
29 | * License. You may obtain a copy of the License at: |
||
30 | * opensource.org/licenses/BSD-3-Clause |
||
31 | * |
||
32 | ****************************************************************************** |
||
33 | */ |
||
34 | |||
35 | /** @addtogroup CMSIS |
||
36 | * @{ |
||
37 | */ |
||
38 | |||
39 | /** @addtogroup stm32l1xx_system |
||
40 | * @{ |
||
41 | */ |
||
42 | |||
43 | /** @addtogroup STM32L1xx_System_Private_Includes |
||
44 | * @{ |
||
45 | */ |
||
46 | |||
47 | #include "stm32l1xx.h" |
||
48 | |||
49 | /** |
||
50 | * @} |
||
51 | */ |
||
52 | |||
53 | /** @addtogroup STM32L1xx_System_Private_TypesDefinitions |
||
54 | * @{ |
||
55 | */ |
||
56 | |||
57 | /** |
||
58 | * @} |
||
59 | */ |
||
60 | |||
61 | /** @addtogroup STM32L1xx_System_Private_Defines |
||
62 | * @{ |
||
63 | */ |
||
64 | #if !defined (HSE_VALUE) |
||
65 | #define HSE_VALUE ((uint32_t)8000000U) /*!< Default value of the External oscillator in Hz. |
||
66 | This value can be provided and adapted by the user application. */ |
||
67 | #endif /* HSE_VALUE */ |
||
68 | |||
69 | #if !defined (HSI_VALUE) |
||
70 | #define HSI_VALUE ((uint32_t)8000000U) /*!< Default value of the Internal oscillator in Hz. |
||
71 | This value can be provided and adapted by the user application. */ |
||
72 | #endif /* HSI_VALUE */ |
||
73 | |||
74 | /*!< Uncomment the following line if you need to use external SRAM mounted |
||
75 | on STM32L152D_EVAL board as data memory */ |
||
76 | /* #define DATA_IN_ExtSRAM */ |
||
77 | |||
78 | /*!< Uncomment the following line if you need to relocate your vector Table in |
||
79 | Internal SRAM. */ |
||
80 | /* #define VECT_TAB_SRAM */ |
||
81 | #define VECT_TAB_OFFSET 0x00U /*!< Vector Table base offset field. |
||
82 | This value must be a multiple of 0x200. */ |
||
83 | /** |
||
84 | * @} |
||
85 | */ |
||
86 | |||
87 | /** @addtogroup STM32L1xx_System_Private_Macros |
||
88 | * @{ |
||
89 | */ |
||
90 | |||
91 | /** |
||
92 | * @} |
||
93 | */ |
||
94 | |||
95 | /** @addtogroup STM32L1xx_System_Private_Variables |
||
96 | * @{ |
||
97 | */ |
||
98 | /* This variable is updated in three ways: |
||
99 | 1) by calling CMSIS function SystemCoreClockUpdate() |
||
100 | 2) by calling HAL API function HAL_RCC_GetHCLKFreq() |
||
101 | 3) each time HAL_RCC_ClockConfig() is called to configure the system clock frequency |
||
102 | Note: If you use this function to configure the system clock; then there |
||
103 | is no need to call the 2 first functions listed above, since SystemCoreClock |
||
104 | variable is updated automatically. |
||
105 | */ |
||
106 | uint32_t SystemCoreClock = 2097000U; |
||
107 | const uint8_t PLLMulTable[9] = {3U, 4U, 6U, 8U, 12U, 16U, 24U, 32U, 48U}; |
||
108 | const uint8_t AHBPrescTable[16] = {0U, 0U, 0U, 0U, 0U, 0U, 0U, 0U, 1U, 2U, 3U, 4U, 6U, 7U, 8U, 9U}; |
||
109 | const uint8_t APBPrescTable[8] = {0U, 0U, 0U, 0U, 1U, 2U, 3U, 4U}; |
||
110 | |||
111 | /** |
||
112 | * @} |
||
113 | */ |
||
114 | |||
115 | /** @addtogroup STM32L1xx_System_Private_FunctionPrototypes |
||
116 | * @{ |
||
117 | */ |
||
118 | |||
119 | #if defined (STM32L151xD) || defined (STM32L152xD) || defined (STM32L162xD) |
||
120 | #ifdef DATA_IN_ExtSRAM |
||
121 | static void SystemInit_ExtMemCtl(void); |
||
122 | #endif /* DATA_IN_ExtSRAM */ |
||
123 | #endif /* STM32L151xD || STM32L152xD || STM32L162xD */ |
||
124 | |||
125 | /** |
||
126 | * @} |
||
127 | */ |
||
128 | |||
129 | /** @addtogroup STM32L1xx_System_Private_Functions |
||
130 | * @{ |
||
131 | */ |
||
132 | |||
133 | /** |
||
134 | * @brief Setup the microcontroller system. |
||
135 | * Initialize the Embedded Flash Interface, the PLL and update the |
||
136 | * SystemCoreClock variable. |
||
137 | * @param None |
||
138 | * @retval None |
||
139 | */ |
||
140 | void SystemInit (void) |
||
141 | { |
||
142 | #ifdef DATA_IN_ExtSRAM |
||
143 | SystemInit_ExtMemCtl(); |
||
144 | #endif /* DATA_IN_ExtSRAM */ |
||
145 | |||
146 | #ifdef VECT_TAB_SRAM |
||
147 | SCB->VTOR = SRAM_BASE | VECT_TAB_OFFSET; /* Vector Table Relocation in Internal SRAM. */ |
||
148 | #else |
||
149 | SCB->VTOR = FLASH_BASE | VECT_TAB_OFFSET; /* Vector Table Relocation in Internal FLASH. */ |
||
150 | #endif |
||
151 | } |
||
152 | |||
153 | /** |
||
154 | * @brief Update SystemCoreClock according to Clock Register Values |
||
155 | * The SystemCoreClock variable contains the core clock (HCLK), it can |
||
156 | * be used by the user application to setup the SysTick timer or configure |
||
157 | * other parameters. |
||
158 | * |
||
159 | * @note Each time the core clock (HCLK) changes, this function must be called |
||
160 | * to update SystemCoreClock variable value. Otherwise, any configuration |
||
161 | * based on this variable will be incorrect. |
||
162 | * |
||
163 | * @note - The system frequency computed by this function is not the real |
||
164 | * frequency in the chip. It is calculated based on the predefined |
||
165 | * constant and the selected clock source: |
||
166 | * |
||
167 | * - If SYSCLK source is MSI, SystemCoreClock will contain the MSI |
||
168 | * value as defined by the MSI range. |
||
169 | * |
||
170 | * - If SYSCLK source is HSI, SystemCoreClock will contain the HSI_VALUE(*) |
||
171 | * |
||
172 | * - If SYSCLK source is HSE, SystemCoreClock will contain the HSE_VALUE(**) |
||
173 | * |
||
174 | * - If SYSCLK source is PLL, SystemCoreClock will contain the HSE_VALUE(**) |
||
175 | * or HSI_VALUE(*) multiplied/divided by the PLL factors. |
||
176 | * |
||
177 | * (*) HSI_VALUE is a constant defined in stm32l1xx.h file (default value |
||
178 | * 16 MHz) but the real value may vary depending on the variations |
||
179 | * in voltage and temperature. |
||
180 | * |
||
181 | * (**) HSE_VALUE is a constant defined in stm32l1xx.h file (default value |
||
182 | * 8 MHz), user has to ensure that HSE_VALUE is same as the real |
||
183 | * frequency of the crystal used. Otherwise, this function may |
||
184 | * have wrong result. |
||
185 | * |
||
186 | * - The result of this function could be not correct when using fractional |
||
187 | * value for HSE crystal. |
||
188 | * @param None |
||
189 | * @retval None |
||
190 | */ |
||
191 | void SystemCoreClockUpdate (void) |
||
192 | { |
||
193 | uint32_t tmp = 0, pllmul = 0, plldiv = 0, pllsource = 0, msirange = 0; |
||
194 | |||
195 | /* Get SYSCLK source -------------------------------------------------------*/ |
||
196 | tmp = RCC->CFGR & RCC_CFGR_SWS; |
||
197 | |||
198 | switch (tmp) |
||
199 | { |
||
200 | case 0x00: /* MSI used as system clock */ |
||
201 | msirange = (RCC->ICSCR & RCC_ICSCR_MSIRANGE) >> 13; |
||
202 | SystemCoreClock = (32768 * (1 << (msirange + 1))); |
||
203 | break; |
||
204 | case 0x04: /* HSI used as system clock */ |
||
205 | SystemCoreClock = HSI_VALUE; |
||
206 | break; |
||
207 | case 0x08: /* HSE used as system clock */ |
||
208 | SystemCoreClock = HSE_VALUE; |
||
209 | break; |
||
210 | case 0x0C: /* PLL used as system clock */ |
||
211 | /* Get PLL clock source and multiplication factor ----------------------*/ |
||
212 | pllmul = RCC->CFGR & RCC_CFGR_PLLMUL; |
||
213 | plldiv = RCC->CFGR & RCC_CFGR_PLLDIV; |
||
214 | pllmul = PLLMulTable[(pllmul >> 18)]; |
||
215 | plldiv = (plldiv >> 22) + 1; |
||
216 | |||
217 | pllsource = RCC->CFGR & RCC_CFGR_PLLSRC; |
||
218 | |||
219 | if (pllsource == 0x00) |
||
220 | { |
||
221 | /* HSI oscillator clock selected as PLL clock entry */ |
||
222 | SystemCoreClock = (((HSI_VALUE) * pllmul) / plldiv); |
||
223 | } |
||
224 | else |
||
225 | { |
||
226 | /* HSE selected as PLL clock entry */ |
||
227 | SystemCoreClock = (((HSE_VALUE) * pllmul) / plldiv); |
||
228 | } |
||
229 | break; |
||
230 | default: /* MSI used as system clock */ |
||
231 | msirange = (RCC->ICSCR & RCC_ICSCR_MSIRANGE) >> 13; |
||
232 | SystemCoreClock = (32768 * (1 << (msirange + 1))); |
||
233 | break; |
||
234 | } |
||
235 | /* Compute HCLK clock frequency --------------------------------------------*/ |
||
236 | /* Get HCLK prescaler */ |
||
237 | tmp = AHBPrescTable[((RCC->CFGR & RCC_CFGR_HPRE) >> 4)]; |
||
238 | /* HCLK clock frequency */ |
||
239 | SystemCoreClock >>= tmp; |
||
240 | } |
||
241 | |||
242 | #if defined (STM32L151xD) || defined (STM32L152xD) || defined (STM32L162xD) |
||
243 | #ifdef DATA_IN_ExtSRAM |
||
244 | /** |
||
245 | * @brief Setup the external memory controller. |
||
246 | * Called in SystemInit() function before jump to main. |
||
247 | * This function configures the external SRAM mounted on STM32L152D_EVAL board |
||
248 | * This SRAM will be used as program data memory (including heap and stack). |
||
249 | * @param None |
||
250 | * @retval None |
||
251 | */ |
||
252 | void SystemInit_ExtMemCtl(void) |
||
253 | { |
||
254 | __IO uint32_t tmpreg = 0; |
||
255 | |||
256 | /* Flash 1 wait state */ |
||
257 | FLASH->ACR |= FLASH_ACR_LATENCY; |
||
258 | |||
259 | /* Power enable */ |
||
260 | RCC->APB1ENR |= RCC_APB1ENR_PWREN; |
||
261 | |||
262 | /* Delay after an RCC peripheral clock enabling */ |
||
263 | tmpreg = READ_BIT(RCC->APB1ENR, RCC_APB1ENR_PWREN); |
||
264 | |||
265 | /* Select the Voltage Range 1 (1.8 V) */ |
||
266 | PWR->CR = PWR_CR_VOS_0; |
||
267 | |||
268 | /* Wait Until the Voltage Regulator is ready */ |
||
269 | while((PWR->CSR & PWR_CSR_VOSF) != RESET) |
||
270 | { |
||
271 | } |
||
272 | |||
273 | /*-- GPIOs Configuration -----------------------------------------------------*/ |
||
274 | /* |
||
275 | +-------------------+--------------------+------------------+------------------+ |
||
276 | + SRAM pins assignment + |
||
277 | +-------------------+--------------------+------------------+------------------+ |
||
278 | | PD0 <-> FSMC_D2 | PE0 <-> FSMC_NBL0 | PF0 <-> FSMC_A0 | PG0 <-> FSMC_A10 | |
||
279 | | PD1 <-> FSMC_D3 | PE1 <-> FSMC_NBL1 | PF1 <-> FSMC_A1 | PG1 <-> FSMC_A11 | |
||
280 | | PD4 <-> FSMC_NOE | PE7 <-> FSMC_D4 | PF2 <-> FSMC_A2 | PG2 <-> FSMC_A12 | |
||
281 | | PD5 <-> FSMC_NWE | PE8 <-> FSMC_D5 | PF3 <-> FSMC_A3 | PG3 <-> FSMC_A13 | |
||
282 | | PD8 <-> FSMC_D13 | PE9 <-> FSMC_D6 | PF4 <-> FSMC_A4 | PG4 <-> FSMC_A14 | |
||
283 | | PD9 <-> FSMC_D14 | PE10 <-> FSMC_D7 | PF5 <-> FSMC_A5 | PG5 <-> FSMC_A15 | |
||
284 | | PD10 <-> FSMC_D15 | PE11 <-> FSMC_D8 | PF12 <-> FSMC_A6 | PG10<-> FSMC_NE2 | |
||
285 | | PD11 <-> FSMC_A16 | PE12 <-> FSMC_D9 | PF13 <-> FSMC_A7 |------------------+ |
||
286 | | PD12 <-> FSMC_A17 | PE13 <-> FSMC_D10 | PF14 <-> FSMC_A8 | |
||
287 | | PD13 <-> FSMC_A18 | PE14 <-> FSMC_D11 | PF15 <-> FSMC_A9 | |
||
288 | | PD14 <-> FSMC_D0 | PE15 <-> FSMC_D12 |------------------+ |
||
289 | | PD15 <-> FSMC_D1 |--------------------+ |
||
290 | +-------------------+ |
||
291 | */ |
||
292 | |||
293 | /* Enable GPIOD, GPIOE, GPIOF and GPIOG interface clock */ |
||
294 | RCC->AHBENR = 0x000080D8; |
||
295 | |||
296 | /* Delay after an RCC peripheral clock enabling */ |
||
297 | tmpreg = READ_BIT(RCC->AHBENR, RCC_AHBENR_GPIODEN); |
||
298 | |||
299 | /* Connect PDx pins to FSMC Alternate function */ |
||
300 | GPIOD->AFR[0] = 0x00CC00CC; |
||
301 | GPIOD->AFR[1] = 0xCCCCCCCC; |
||
302 | /* Configure PDx pins in Alternate function mode */ |
||
303 | GPIOD->MODER = 0xAAAA0A0A; |
||
304 | /* Configure PDx pins speed to 40 MHz */ |
||
305 | GPIOD->OSPEEDR = 0xFFFF0F0F; |
||
306 | /* Configure PDx pins Output type to push-pull */ |
||
307 | GPIOD->OTYPER = 0x00000000; |
||
308 | /* No pull-up, pull-down for PDx pins */ |
||
309 | GPIOD->PUPDR = 0x00000000; |
||
310 | |||
311 | /* Connect PEx pins to FSMC Alternate function */ |
||
312 | GPIOE->AFR[0] = 0xC00000CC; |
||
313 | GPIOE->AFR[1] = 0xCCCCCCCC; |
||
314 | /* Configure PEx pins in Alternate function mode */ |
||
315 | GPIOE->MODER = 0xAAAA800A; |
||
316 | /* Configure PEx pins speed to 40 MHz */ |
||
317 | GPIOE->OSPEEDR = 0xFFFFC00F; |
||
318 | /* Configure PEx pins Output type to push-pull */ |
||
319 | GPIOE->OTYPER = 0x00000000; |
||
320 | /* No pull-up, pull-down for PEx pins */ |
||
321 | GPIOE->PUPDR = 0x00000000; |
||
322 | |||
323 | /* Connect PFx pins to FSMC Alternate function */ |
||
324 | GPIOF->AFR[0] = 0x00CCCCCC; |
||
325 | GPIOF->AFR[1] = 0xCCCC0000; |
||
326 | /* Configure PFx pins in Alternate function mode */ |
||
327 | GPIOF->MODER = 0xAA000AAA; |
||
328 | /* Configure PFx pins speed to 40 MHz */ |
||
329 | GPIOF->OSPEEDR = 0xFF000FFF; |
||
330 | /* Configure PFx pins Output type to push-pull */ |
||
331 | GPIOF->OTYPER = 0x00000000; |
||
332 | /* No pull-up, pull-down for PFx pins */ |
||
333 | GPIOF->PUPDR = 0x00000000; |
||
334 | |||
335 | /* Connect PGx pins to FSMC Alternate function */ |
||
336 | GPIOG->AFR[0] = 0x00CCCCCC; |
||
337 | GPIOG->AFR[1] = 0x00000C00; |
||
338 | /* Configure PGx pins in Alternate function mode */ |
||
339 | GPIOG->MODER = 0x00200AAA; |
||
340 | /* Configure PGx pins speed to 40 MHz */ |
||
341 | GPIOG->OSPEEDR = 0x00300FFF; |
||
342 | /* Configure PGx pins Output type to push-pull */ |
||
343 | GPIOG->OTYPER = 0x00000000; |
||
344 | /* No pull-up, pull-down for PGx pins */ |
||
345 | GPIOG->PUPDR = 0x00000000; |
||
346 | |||
347 | /*-- FSMC Configuration ------------------------------------------------------*/ |
||
348 | /* Enable the FSMC interface clock */ |
||
349 | RCC->AHBENR = 0x400080D8; |
||
350 | |||
351 | /* Delay after an RCC peripheral clock enabling */ |
||
352 | tmpreg = READ_BIT(RCC->AHBENR, RCC_AHBENR_FSMCEN); |
||
353 | |||
354 | (void)(tmpreg); |
||
355 | |||
356 | /* Configure and enable Bank1_SRAM3 */ |
||
357 | FSMC_Bank1->BTCR[4] = 0x00001011; |
||
358 | FSMC_Bank1->BTCR[5] = 0x00000300; |
||
359 | FSMC_Bank1E->BWTR[4] = 0x0FFFFFFF; |
||
360 | /* |
||
361 | Bank1_SRAM3 is configured as follow: |
||
362 | |||
363 | p.FSMC_AddressSetupTime = 0; |
||
364 | p.FSMC_AddressHoldTime = 0; |
||
365 | p.FSMC_DataSetupTime = 3; |
||
366 | p.FSMC_BusTurnAroundDuration = 0; |
||
367 | p.FSMC_CLKDivision = 0; |
||
368 | p.FSMC_DataLatency = 0; |
||
369 | p.FSMC_AccessMode = FSMC_AccessMode_A; |
||
370 | |||
371 | FSMC_NORSRAMInitStructure.FSMC_Bank = FSMC_Bank1_NORSRAM3; |
||
372 | FSMC_NORSRAMInitStructure.FSMC_DataAddressMux = FSMC_DataAddressMux_Disable; |
||
373 | FSMC_NORSRAMInitStructure.FSMC_MemoryType = FSMC_MemoryType_SRAM; |
||
374 | FSMC_NORSRAMInitStructure.FSMC_MemoryDataWidth = FSMC_MemoryDataWidth_16b; |
||
375 | FSMC_NORSRAMInitStructure.FSMC_BurstAccessMode = FSMC_BurstAccessMode_Disable; |
||
376 | FSMC_NORSRAMInitStructure.FSMC_AsynchronousWait = FSMC_AsynchronousWait_Disable; |
||
377 | FSMC_NORSRAMInitStructure.FSMC_WaitSignalPolarity = FSMC_WaitSignalPolarity_Low; |
||
378 | FSMC_NORSRAMInitStructure.FSMC_WrapMode = FSMC_WrapMode_Disable; |
||
379 | FSMC_NORSRAMInitStructure.FSMC_WaitSignalActive = FSMC_WaitSignalActive_BeforeWaitState; |
||
380 | FSMC_NORSRAMInitStructure.FSMC_WriteOperation = FSMC_WriteOperation_Enable; |
||
381 | FSMC_NORSRAMInitStructure.FSMC_WaitSignal = FSMC_WaitSignal_Disable; |
||
382 | FSMC_NORSRAMInitStructure.FSMC_ExtendedMode = FSMC_ExtendedMode_Disable; |
||
383 | FSMC_NORSRAMInitStructure.FSMC_WriteBurst = FSMC_WriteBurst_Disable; |
||
384 | FSMC_NORSRAMInitStructure.FSMC_ReadWriteTimingStruct = &p; |
||
385 | FSMC_NORSRAMInitStructure.FSMC_WriteTimingStruct = &p; |
||
386 | |||
387 | FSMC_NORSRAMInit(&FSMC_NORSRAMInitStructure); |
||
388 | |||
389 | FSMC_NORSRAMCmd(FSMC_Bank1_NORSRAM3, ENABLE); |
||
390 | */ |
||
391 | |||
392 | } |
||
393 | #endif /* DATA_IN_ExtSRAM */ |
||
394 | #endif /* STM32L151xD || STM32L152xD || STM32L162xD */ |
||
395 | |||
396 | /** |
||
397 | * @} |
||
398 | */ |
||
399 | |||
400 | /** |
||
401 | * @} |
||
402 | */ |
||
403 | |||
404 | /** |
||
405 | * @} |
||
406 | */ |
||
407 | |||
408 | /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ |