xref: /btstack/port/stm32-l073rz-nucleo-em9304/Src/system_stm32l0xx.c (revision e838079242074edcbcbb400962776e15fe6ca6cb)
1*e8380792SMatthias Ringwald /**
2*e8380792SMatthias Ringwald   ******************************************************************************
3*e8380792SMatthias Ringwald   * @file    system_stm32l0xx.c
4*e8380792SMatthias Ringwald   * @author  MCD Application Team
5*e8380792SMatthias Ringwald   * @brief   CMSIS Cortex-M0+ Device Peripheral Access Layer System Source File.
6*e8380792SMatthias Ringwald   *
7*e8380792SMatthias Ringwald   *   This file provides two functions and one global variable to be called from
8*e8380792SMatthias Ringwald   *   user application:
9*e8380792SMatthias Ringwald   *      - SystemInit(): This function is called at startup just after reset and
10*e8380792SMatthias Ringwald   *                      before branch to main program. This call is made inside
11*e8380792SMatthias Ringwald   *                      the "startup_stm32l0xx.s" file.
12*e8380792SMatthias Ringwald   *
13*e8380792SMatthias Ringwald   *      - SystemCoreClock variable: Contains the core clock (HCLK), it can be used
14*e8380792SMatthias Ringwald   *                                  by the user application to setup the SysTick
15*e8380792SMatthias Ringwald   *                                  timer or configure other parameters.
16*e8380792SMatthias Ringwald   *
17*e8380792SMatthias Ringwald   *      - SystemCoreClockUpdate(): Updates the variable SystemCoreClock and must
18*e8380792SMatthias Ringwald   *                                 be called whenever the core clock is changed
19*e8380792SMatthias Ringwald   *                                 during program execution.
20*e8380792SMatthias Ringwald   *
21*e8380792SMatthias Ringwald   *
22*e8380792SMatthias Ringwald   ******************************************************************************
23*e8380792SMatthias Ringwald   * @attention
24*e8380792SMatthias Ringwald   *
25*e8380792SMatthias Ringwald   * <h2><center>&copy; Copyright(c) 2016 STMicroelectronics.
26*e8380792SMatthias Ringwald   * All rights reserved.</center></h2>
27*e8380792SMatthias Ringwald   *
28*e8380792SMatthias Ringwald   * This software component is licensed by ST under BSD 3-Clause license,
29*e8380792SMatthias Ringwald   * the "License"; You may not use this file except in compliance with the
30*e8380792SMatthias Ringwald   * License. You may obtain a copy of the License at:
31*e8380792SMatthias Ringwald   *                        opensource.org/licenses/BSD-3-Clause
32*e8380792SMatthias Ringwald   *
33*e8380792SMatthias Ringwald   ******************************************************************************
34*e8380792SMatthias Ringwald   */
35*e8380792SMatthias Ringwald 
36*e8380792SMatthias Ringwald /** @addtogroup CMSIS
37*e8380792SMatthias Ringwald   * @{
38*e8380792SMatthias Ringwald   */
39*e8380792SMatthias Ringwald 
40*e8380792SMatthias Ringwald /** @addtogroup stm32l0xx_system
41*e8380792SMatthias Ringwald   * @{
42*e8380792SMatthias Ringwald   */
43*e8380792SMatthias Ringwald 
44*e8380792SMatthias Ringwald /** @addtogroup STM32L0xx_System_Private_Includes
45*e8380792SMatthias Ringwald   * @{
46*e8380792SMatthias Ringwald   */
47*e8380792SMatthias Ringwald 
48*e8380792SMatthias Ringwald #include "stm32l0xx.h"
49*e8380792SMatthias Ringwald 
50*e8380792SMatthias Ringwald #if !defined  (HSE_VALUE)
51*e8380792SMatthias Ringwald   #define HSE_VALUE    ((uint32_t)8000000U) /*!< Value of the External oscillator in Hz */
52*e8380792SMatthias Ringwald #endif /* HSE_VALUE */
53*e8380792SMatthias Ringwald 
54*e8380792SMatthias Ringwald #if !defined  (MSI_VALUE)
55*e8380792SMatthias Ringwald   #define MSI_VALUE    ((uint32_t)2097152U) /*!< Value of the Internal oscillator in Hz*/
56*e8380792SMatthias Ringwald #endif /* MSI_VALUE */
57*e8380792SMatthias Ringwald 
58*e8380792SMatthias Ringwald #if !defined  (HSI_VALUE)
59*e8380792SMatthias Ringwald   #define HSI_VALUE    ((uint32_t)16000000U) /*!< Value of the Internal oscillator in Hz*/
60*e8380792SMatthias Ringwald #endif /* HSI_VALUE */
61*e8380792SMatthias Ringwald 
62*e8380792SMatthias Ringwald 
63*e8380792SMatthias Ringwald /**
64*e8380792SMatthias Ringwald   * @}
65*e8380792SMatthias Ringwald   */
66*e8380792SMatthias Ringwald 
67*e8380792SMatthias Ringwald /** @addtogroup STM32L0xx_System_Private_TypesDefinitions
68*e8380792SMatthias Ringwald   * @{
69*e8380792SMatthias Ringwald   */
70*e8380792SMatthias Ringwald 
71*e8380792SMatthias Ringwald /**
72*e8380792SMatthias Ringwald   * @}
73*e8380792SMatthias Ringwald   */
74*e8380792SMatthias Ringwald 
75*e8380792SMatthias Ringwald /** @addtogroup STM32L0xx_System_Private_Defines
76*e8380792SMatthias Ringwald   * @{
77*e8380792SMatthias Ringwald   */
78*e8380792SMatthias Ringwald /************************* Miscellaneous Configuration ************************/
79*e8380792SMatthias Ringwald 
80*e8380792SMatthias Ringwald /*!< Uncomment the following line if you need to relocate your vector Table in
81*e8380792SMatthias Ringwald      Internal SRAM. */
82*e8380792SMatthias Ringwald /* #define VECT_TAB_SRAM */
83*e8380792SMatthias Ringwald #define VECT_TAB_OFFSET  0x00U /*!< Vector Table base offset field.
84*e8380792SMatthias Ringwald                                    This value must be a multiple of 0x100. */
85*e8380792SMatthias Ringwald /******************************************************************************/
86*e8380792SMatthias Ringwald /**
87*e8380792SMatthias Ringwald   * @}
88*e8380792SMatthias Ringwald   */
89*e8380792SMatthias Ringwald 
90*e8380792SMatthias Ringwald /** @addtogroup STM32L0xx_System_Private_Macros
91*e8380792SMatthias Ringwald   * @{
92*e8380792SMatthias Ringwald   */
93*e8380792SMatthias Ringwald 
94*e8380792SMatthias Ringwald /**
95*e8380792SMatthias Ringwald   * @}
96*e8380792SMatthias Ringwald   */
97*e8380792SMatthias Ringwald 
98*e8380792SMatthias Ringwald /** @addtogroup STM32L0xx_System_Private_Variables
99*e8380792SMatthias Ringwald   * @{
100*e8380792SMatthias Ringwald   */
101*e8380792SMatthias Ringwald   /* This variable is updated in three ways:
102*e8380792SMatthias Ringwald       1) by calling CMSIS function SystemCoreClockUpdate()
103*e8380792SMatthias Ringwald       2) by calling HAL API function HAL_RCC_GetHCLKFreq()
104*e8380792SMatthias Ringwald       3) each time HAL_RCC_ClockConfig() is called to configure the system clock frequency
105*e8380792SMatthias Ringwald          Note: If you use this function to configure the system clock; then there
106*e8380792SMatthias Ringwald                is no need to call the 2 first functions listed above, since SystemCoreClock
107*e8380792SMatthias Ringwald                variable is updated automatically.
108*e8380792SMatthias Ringwald   */
109*e8380792SMatthias Ringwald   uint32_t SystemCoreClock = 2097152U; /* 32.768 kHz * 2^6 */
110*e8380792SMatthias Ringwald   const uint8_t AHBPrescTable[16] = {0U, 0U, 0U, 0U, 0U, 0U, 0U, 0U, 1U, 2U, 3U, 4U, 6U, 7U, 8U, 9U};
111*e8380792SMatthias Ringwald   const uint8_t APBPrescTable[8] = {0U, 0U, 0U, 0U, 1U, 2U, 3U, 4U};
112*e8380792SMatthias Ringwald   const uint8_t PLLMulTable[9] = {3U, 4U, 6U, 8U, 12U, 16U, 24U, 32U, 48U};
113*e8380792SMatthias Ringwald 
114*e8380792SMatthias Ringwald /**
115*e8380792SMatthias Ringwald   * @}
116*e8380792SMatthias Ringwald   */
117*e8380792SMatthias Ringwald 
118*e8380792SMatthias Ringwald /** @addtogroup STM32L0xx_System_Private_FunctionPrototypes
119*e8380792SMatthias Ringwald   * @{
120*e8380792SMatthias Ringwald   */
121*e8380792SMatthias Ringwald 
122*e8380792SMatthias Ringwald /**
123*e8380792SMatthias Ringwald   * @}
124*e8380792SMatthias Ringwald   */
125*e8380792SMatthias Ringwald 
126*e8380792SMatthias Ringwald /** @addtogroup STM32L0xx_System_Private_Functions
127*e8380792SMatthias Ringwald   * @{
128*e8380792SMatthias Ringwald   */
129*e8380792SMatthias Ringwald 
130*e8380792SMatthias Ringwald /**
131*e8380792SMatthias Ringwald   * @brief  Setup the microcontroller system.
132*e8380792SMatthias Ringwald   * @param  None
133*e8380792SMatthias Ringwald   * @retval None
134*e8380792SMatthias Ringwald   */
SystemInit(void)135*e8380792SMatthias Ringwald void SystemInit (void)
136*e8380792SMatthias Ringwald {
137*e8380792SMatthias Ringwald /*!< Set MSION bit */
138*e8380792SMatthias Ringwald   RCC->CR |= (uint32_t)0x00000100U;
139*e8380792SMatthias Ringwald 
140*e8380792SMatthias Ringwald   /*!< Reset SW[1:0], HPRE[3:0], PPRE1[2:0], PPRE2[2:0], MCOSEL[2:0] and MCOPRE[2:0] bits */
141*e8380792SMatthias Ringwald   RCC->CFGR &= (uint32_t) 0x88FF400CU;
142*e8380792SMatthias Ringwald 
143*e8380792SMatthias Ringwald   /*!< Reset HSION, HSIDIVEN, HSEON, CSSON and PLLON bits */
144*e8380792SMatthias Ringwald   RCC->CR &= (uint32_t)0xFEF6FFF6U;
145*e8380792SMatthias Ringwald 
146*e8380792SMatthias Ringwald   /*!< Reset HSI48ON  bit */
147*e8380792SMatthias Ringwald   RCC->CRRCR &= (uint32_t)0xFFFFFFFEU;
148*e8380792SMatthias Ringwald 
149*e8380792SMatthias Ringwald   /*!< Reset HSEBYP bit */
150*e8380792SMatthias Ringwald   RCC->CR &= (uint32_t)0xFFFBFFFFU;
151*e8380792SMatthias Ringwald 
152*e8380792SMatthias Ringwald   /*!< Reset PLLSRC, PLLMUL[3:0] and PLLDIV[1:0] bits */
153*e8380792SMatthias Ringwald   RCC->CFGR &= (uint32_t)0xFF02FFFFU;
154*e8380792SMatthias Ringwald 
155*e8380792SMatthias Ringwald   /*!< Disable all interrupts */
156*e8380792SMatthias Ringwald   RCC->CIER = 0x00000000U;
157*e8380792SMatthias Ringwald 
158*e8380792SMatthias Ringwald   /* Configure the Vector Table location add offset address ------------------*/
159*e8380792SMatthias Ringwald #ifdef VECT_TAB_SRAM
160*e8380792SMatthias Ringwald   SCB->VTOR = SRAM_BASE | VECT_TAB_OFFSET; /* Vector Table Relocation in Internal SRAM */
161*e8380792SMatthias Ringwald #else
162*e8380792SMatthias Ringwald   SCB->VTOR = FLASH_BASE | VECT_TAB_OFFSET; /* Vector Table Relocation in Internal FLASH */
163*e8380792SMatthias Ringwald #endif
164*e8380792SMatthias Ringwald }
165*e8380792SMatthias Ringwald 
166*e8380792SMatthias Ringwald /**
167*e8380792SMatthias Ringwald   * @brief  Update SystemCoreClock according to Clock Register Values
168*e8380792SMatthias Ringwald   *         The SystemCoreClock variable contains the core clock (HCLK), it can
169*e8380792SMatthias Ringwald   *         be used by the user application to setup the SysTick timer or configure
170*e8380792SMatthias Ringwald   *         other parameters.
171*e8380792SMatthias Ringwald   *
172*e8380792SMatthias Ringwald   * @note   Each time the core clock (HCLK) changes, this function must be called
173*e8380792SMatthias Ringwald   *         to update SystemCoreClock variable value. Otherwise, any configuration
174*e8380792SMatthias Ringwald   *         based on this variable will be incorrect.
175*e8380792SMatthias Ringwald   *
176*e8380792SMatthias Ringwald   * @note   - The system frequency computed by this function is not the real
177*e8380792SMatthias Ringwald   *           frequency in the chip. It is calculated based on the predefined
178*e8380792SMatthias Ringwald   *           constant and the selected clock source:
179*e8380792SMatthias Ringwald   *
180*e8380792SMatthias Ringwald   *           - If SYSCLK source is MSI, SystemCoreClock will contain the MSI
181*e8380792SMatthias Ringwald   *             value as defined by the MSI range.
182*e8380792SMatthias Ringwald   *
183*e8380792SMatthias Ringwald   *           - If SYSCLK source is HSI, SystemCoreClock will contain the HSI_VALUE(*)
184*e8380792SMatthias Ringwald   *
185*e8380792SMatthias Ringwald   *           - If SYSCLK source is HSE, SystemCoreClock will contain the HSE_VALUE(**)
186*e8380792SMatthias Ringwald   *
187*e8380792SMatthias Ringwald   *           - If SYSCLK source is PLL, SystemCoreClock will contain the HSE_VALUE(**)
188*e8380792SMatthias Ringwald   *             or HSI_VALUE(*) multiplied/divided by the PLL factors.
189*e8380792SMatthias Ringwald   *
190*e8380792SMatthias Ringwald   *         (*) HSI_VALUE is a constant defined in stm32l0xx_hal.h file (default value
191*e8380792SMatthias Ringwald   *             16 MHz) but the real value may vary depending on the variations
192*e8380792SMatthias Ringwald   *             in voltage and temperature.
193*e8380792SMatthias Ringwald   *
194*e8380792SMatthias Ringwald   *         (**) HSE_VALUE is a constant defined in stm32l0xx_hal.h file (default value
195*e8380792SMatthias Ringwald   *              8 MHz), user has to ensure that HSE_VALUE is same as the real
196*e8380792SMatthias Ringwald   *              frequency of the crystal used. Otherwise, this function may
197*e8380792SMatthias Ringwald   *              have wrong result.
198*e8380792SMatthias Ringwald   *
199*e8380792SMatthias Ringwald   *         - The result of this function could be not correct when using fractional
200*e8380792SMatthias Ringwald   *           value for HSE crystal.
201*e8380792SMatthias Ringwald   * @param  None
202*e8380792SMatthias Ringwald   * @retval None
203*e8380792SMatthias Ringwald   */
SystemCoreClockUpdate(void)204*e8380792SMatthias Ringwald void SystemCoreClockUpdate (void)
205*e8380792SMatthias Ringwald {
206*e8380792SMatthias Ringwald   uint32_t tmp = 0U, pllmul = 0U, plldiv = 0U, pllsource = 0U, msirange = 0U;
207*e8380792SMatthias Ringwald 
208*e8380792SMatthias Ringwald   /* Get SYSCLK source -------------------------------------------------------*/
209*e8380792SMatthias Ringwald   tmp = RCC->CFGR & RCC_CFGR_SWS;
210*e8380792SMatthias Ringwald 
211*e8380792SMatthias Ringwald   switch (tmp)
212*e8380792SMatthias Ringwald   {
213*e8380792SMatthias Ringwald     case 0x00U:  /* MSI used as system clock */
214*e8380792SMatthias Ringwald       msirange = (RCC->ICSCR & RCC_ICSCR_MSIRANGE) >> RCC_ICSCR_MSIRANGE_Pos;
215*e8380792SMatthias Ringwald       SystemCoreClock = (32768U * (1U << (msirange + 1U)));
216*e8380792SMatthias Ringwald       break;
217*e8380792SMatthias Ringwald     case 0x04U:  /* HSI used as system clock */
218*e8380792SMatthias Ringwald       if ((RCC->CR & RCC_CR_HSIDIVF) != 0U)
219*e8380792SMatthias Ringwald       {
220*e8380792SMatthias Ringwald         SystemCoreClock = HSI_VALUE / 4U;
221*e8380792SMatthias Ringwald       }
222*e8380792SMatthias Ringwald       else
223*e8380792SMatthias Ringwald       {
224*e8380792SMatthias Ringwald         SystemCoreClock = HSI_VALUE;
225*e8380792SMatthias Ringwald       }
226*e8380792SMatthias Ringwald       break;
227*e8380792SMatthias Ringwald     case 0x08U:  /* HSE used as system clock */
228*e8380792SMatthias Ringwald       SystemCoreClock = HSE_VALUE;
229*e8380792SMatthias Ringwald       break;
230*e8380792SMatthias Ringwald     default:  /* PLL used as system clock */
231*e8380792SMatthias Ringwald       /* Get PLL clock source and multiplication factor ----------------------*/
232*e8380792SMatthias Ringwald       pllmul = RCC->CFGR & RCC_CFGR_PLLMUL;
233*e8380792SMatthias Ringwald       plldiv = RCC->CFGR & RCC_CFGR_PLLDIV;
234*e8380792SMatthias Ringwald       pllmul = PLLMulTable[(pllmul >> RCC_CFGR_PLLMUL_Pos)];
235*e8380792SMatthias Ringwald       plldiv = (plldiv >> RCC_CFGR_PLLDIV_Pos) + 1U;
236*e8380792SMatthias Ringwald 
237*e8380792SMatthias Ringwald       pllsource = RCC->CFGR & RCC_CFGR_PLLSRC;
238*e8380792SMatthias Ringwald 
239*e8380792SMatthias Ringwald       if (pllsource == 0x00U)
240*e8380792SMatthias Ringwald       {
241*e8380792SMatthias Ringwald         /* HSI oscillator clock selected as PLL clock entry */
242*e8380792SMatthias Ringwald         if ((RCC->CR & RCC_CR_HSIDIVF) != 0U)
243*e8380792SMatthias Ringwald         {
244*e8380792SMatthias Ringwald           SystemCoreClock = (((HSI_VALUE / 4U) * pllmul) / plldiv);
245*e8380792SMatthias Ringwald         }
246*e8380792SMatthias Ringwald         else
247*e8380792SMatthias Ringwald         {
248*e8380792SMatthias Ringwald           SystemCoreClock = (((HSI_VALUE) * pllmul) / plldiv);
249*e8380792SMatthias Ringwald         }
250*e8380792SMatthias Ringwald       }
251*e8380792SMatthias Ringwald       else
252*e8380792SMatthias Ringwald       {
253*e8380792SMatthias Ringwald         /* HSE selected as PLL clock entry */
254*e8380792SMatthias Ringwald         SystemCoreClock = (((HSE_VALUE) * pllmul) / plldiv);
255*e8380792SMatthias Ringwald       }
256*e8380792SMatthias Ringwald       break;
257*e8380792SMatthias Ringwald   }
258*e8380792SMatthias Ringwald   /* Compute HCLK clock frequency --------------------------------------------*/
259*e8380792SMatthias Ringwald   /* Get HCLK prescaler */
260*e8380792SMatthias Ringwald   tmp = AHBPrescTable[((RCC->CFGR & RCC_CFGR_HPRE) >> RCC_CFGR_HPRE_Pos)];
261*e8380792SMatthias Ringwald   /* HCLK clock frequency */
262*e8380792SMatthias Ringwald   SystemCoreClock >>= tmp;
263*e8380792SMatthias Ringwald }
264*e8380792SMatthias Ringwald 
265*e8380792SMatthias Ringwald 
266*e8380792SMatthias Ringwald 
267*e8380792SMatthias Ringwald /**
268*e8380792SMatthias Ringwald   * @}
269*e8380792SMatthias Ringwald   */
270*e8380792SMatthias Ringwald 
271*e8380792SMatthias Ringwald /**
272*e8380792SMatthias Ringwald   * @}
273*e8380792SMatthias Ringwald   */
274*e8380792SMatthias Ringwald 
275*e8380792SMatthias Ringwald /**
276*e8380792SMatthias Ringwald   * @}
277*e8380792SMatthias Ringwald   */
278*e8380792SMatthias Ringwald 
279*e8380792SMatthias Ringwald /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
280