stm32l0xx_hal_rcc.c 48 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339
  1. /**
  2. ******************************************************************************
  3. * @file stm32l0xx_hal_rcc.c
  4. * @author MCD Application Team
  5. * @version V1.4.0
  6. * @date 01-October-2015
  7. * @brief RCC HAL module driver.
  8. * This file provides firmware functions to manage the following
  9. * functionalities of the Reset and Clock Control (RCC) peripheral:
  10. * + Initialization and de-initialization functions
  11. * + Peripheral Control functions
  12. *
  13. @verbatim
  14. ==============================================================================
  15. ##### RCC specific features #####
  16. ==============================================================================
  17. [..] After reset the device is running from MSI (2 MHz) with Flash 0 WS,
  18. all peripherals are off except internal SRAM, Flash and SW-DP.
  19. (+) There is no prescaler on High speed (AHB) and Low speed (APB) busses;
  20. all peripherals mapped on these busses are running at MSI speed.
  21. (+) The clock for all peripherals is switched off, except the SRAM and
  22. FLASH.
  23. (+) All GPIOs are in input floating state, except the SW-DP pins which
  24. are assigned to be used for debug purpose.
  25. [..] Once the device started from reset, the user application has to:
  26. (+) Configure the clock source to be used to drive the System clock
  27. (if the application needs higher frequency/performance)
  28. (+) Configure the System clock frequency and Flash settings
  29. (+) Configure the AHB and APB busses prescalers
  30. (+) Enable the clock for the peripheral(s) to be used
  31. (+) Configure the clock source(s) for peripherals whose clocks are not
  32. derived from the System clock (ADC, RTC/LCD, RNG and IWDG)
  33. ##### RCC Limitations #####
  34. ==============================================================================
  35. [..]
  36. A delay between an RCC peripheral clock enable and the effective peripheral
  37. enabling should be taken into account in order to manage the peripheral read/write
  38. from/to registeres.
  39. (+) This delay depends on the peripheral mapping.
  40. (+) If peripheral is mapped on AHB: the delay is 2 AHB clock cycle
  41. after the clock enable bit is set on the hardware register
  42. (+) If peripheral is mapped on APB: the delay is 2 APB clock cycle
  43. after the clock enable bit is set on the hardware register
  44. [..]
  45. Possible Workarounds:
  46. (#) Enable the peripheral clock sometimes before the peripheral read/write
  47. register is required.
  48. (#) For AHB peripheral, insert two dummy read to the peripheral register.
  49. (#) For APB peripheral, insert a dummy read to the peripheral register.
  50. @endverbatim
  51. ******************************************************************************
  52. * @attention
  53. *
  54. * <h2><center>&copy; COPYRIGHT(c) 2016 STMicroelectronics</center></h2>
  55. *
  56. * Redistribution and use in source and binary forms, with or without modification,
  57. * are permitted provided that the following conditions are met:
  58. * 1. Redistributions of source code must retain the above copyright notice,
  59. * this list of conditions and the following disclaimer.
  60. * 2. Redistributions in binary form must reproduce the above copyright notice,
  61. * this list of conditions and the following disclaimer in the documentation
  62. * and/or other materials provided with the distribution.
  63. * 3. Neither the name of STMicroelectronics nor the names of its contributors
  64. * may be used to endorse or promote products derived from this software
  65. * without specific prior written permission.
  66. *
  67. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  68. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  69. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  70. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  71. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  72. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  73. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  74. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  75. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  76. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  77. *
  78. ******************************************************************************
  79. */
  80. /* Includes ------------------------------------------------------------------*/
  81. #include "stm32l0xx_hal.h"
  82. /** @addtogroup STM32L0xx_HAL_Driver
  83. * @{
  84. */
  85. #ifdef HAL_RCC_MODULE_ENABLED
  86. /** @addtogroup RCC
  87. * @brief RCC HAL module driver
  88. * @{
  89. */
  90. /** @addtogroup RCC_Private
  91. * @{
  92. */
  93. #define RCC_HSE_TIMEOUT_VALUE HSE_STARTUP_TIMEOUT
  94. #define RCC_HSI_TIMEOUT_VALUE ((uint32_t)2U) /* 2 ms (minimum Tick + 1) */
  95. #define RCC_LSI_TIMEOUT_VALUE ((uint32_t)2U) /* 2 ms (minimum Tick + 1) */
  96. #define RCC_PLL_TIMEOUT_VALUE ((uint32_t)2U) /* 2 ms (minimum Tick + 1) */
  97. #define RCC_HSI48_TIMEOUT_VALUE ((uint32_t)2U) /* 2 ms (minimum Tick + 1) */
  98. #define RCC_MSI_TIMEOUT_VALUE ((uint32_t)2U) /* 2 ms (minimum Tick + 1) */
  99. #define RCC_CLOCKSWITCH_TIMEOUT_VALUE ((uint32_t)5000U) /* 5 s */
  100. #define __MCO1_CLK_ENABLE() __HAL_RCC_GPIOA_CLK_ENABLE()
  101. #define MCO1_GPIO_PORT GPIOA
  102. #define MCO1_PIN GPIO_PIN_8
  103. #define __MCO2_CLK_ENABLE() __HAL_RCC_GPIOA_CLK_ENABLE()
  104. #define MCO2_GPIO_PORT GPIOA
  105. #define MCO2_PIN GPIO_PIN_9
  106. #if defined(STM32L031xx) || defined(STM32L041xx) || defined(STM32L073xx) || defined(STM32L083xx) || \
  107. defined(STM32L072xx) || defined(STM32L082xx) || defined(STM32L071xx) || defined(STM32L081xx)
  108. #define __MCO3_CLK_ENABLE() __HAL_RCC_GPIOB_CLK_ENABLE()
  109. #define MCO3_GPIO_PORT GPIOB
  110. #define MCO3_PIN GPIO_PIN_13
  111. #endif
  112. extern const uint8_t PLLMulTable[]; /* Defined in CMSIS (system_stm32l0xx.c)*/
  113. /**
  114. * @}
  115. */
  116. /** @addtogroup RCC_Exported_Functions
  117. * @{
  118. */
  119. /** @addtogroup RCC_Exported_Functions_Group1 Initialization and de-initialization functions
  120. * @brief Initialization and Configuration functions
  121. *
  122. @verbatim
  123. ===============================================================================
  124. ##### Initialization and de-initialization functions #####
  125. ===============================================================================
  126. [..]
  127. This section provide functions allowing to configure the internal/external
  128. clocks, PLL, CSS and MCO.
  129. [..] Internal/external clock and PLL configuration
  130. (#) HSI (high-speed internal), 16 MHz factory-trimmed RC used directly
  131. or through the PLL as System clock source.
  132. (#) MSI (multi-speed internal), multispeed low power RC
  133. (65.536 KHz to 4.194 MHz) MHz used as System clock source.
  134. (#) LSI (low-speed internal), 37 KHz low consumption RC used as IWDG
  135. and/or RTC clock source.
  136. (#) HSE (high-speed external), 1 to 24 MHz crystal oscillator used
  137. directly or through the PLL as System clock source. Can be used
  138. also as RTC clock source.
  139. (#) LSE (low-speed external), 32 KHz oscillator used as RTC clock source.
  140. (#) PLL (clocked by HSI or HSE), for System clock and USB (48 MHz).
  141. (#) CSS (Clock security system), once enable and if a HSE clock failure
  142. occurs (HSE used directly or through PLL as System clock source),
  143. the System clock is automatically switched to MSI and an interrupt
  144. is generated if enabled.
  145. The interrupt is linked to the Cortex-M3 NMI (Non-Maskable Interrupt)
  146. exception vector.
  147. (#) MCO (microcontroller clock output), used to output SYSCLK, HSI, MSI,
  148. HSE, PLL, LSI or LSE clock (through a configurable prescaler) on
  149. PA8 pin.
  150. [..] System, AHB and APB busses clocks configuration
  151. (#) Several clock sources can be used to drive the System clock (SYSCLK): MSI, HSI,
  152. HSE and PLL.
  153. The AHB clock (HCLK) is derived from System clock through configurable
  154. prescaler and used to clock the CPU, memory and peripherals mapped
  155. on IOPORT, AHB bus (DMA,Flash...). APB1 (PCLK1) and APB2 (PCLK2) clocks are derived
  156. from AHB clock through configurable prescalers and used to clock
  157. the peripherals mapped on these busses. You can use
  158. "HAL_RCC_GetSysClockFreq()" function to retrieve the frequencies of these clocks.
  159. -@- All the peripheral clocks are derived from the System clock (SYSCLK) except:
  160. (+@) I2S: the I2S clock can be derived from an external clock mapped on the I2S_CKIN pin.
  161. (+@) RTC: the RTC clock can be derived either from the LSI, LSE or HSE clock
  162. divided by 2 to 16. You have to use __HAL_RCC_RTC_CONFIG() and __HAL_RCC_RTC_ENABLE()
  163. macros to configure this clock.
  164. (+@) USB FS,and RNG require a frequency equal to 48 MHz to work correctly
  165. This clock is derived from the main PLL or HSI48 RC oscillator.
  166. (+@) IWDG clock which is always the LSI clock.
  167. (#) For the STM32L0xx devices, the maximum
  168. frequency of the SYSCLK ,HCLK, APB1 and APB2 is 32 MHz.
  169. Depending on the device voltage range, the maximum frequency should
  170. be adapted accordingly. Refer to the Reference Manual for more details.
  171. @endverbatim
  172. Table 1. HCLK clock frequency.
  173. +----------------------------------------------------------------+
  174. | Wait states | HCLK clock frequency (MHz) |
  175. | |------------------------------------------------|
  176. | (Latency) | voltage range | voltage range |
  177. | | 1.65 V - 3.6 V | 2.0 V - 3.6 V |
  178. | |----------------|---------------|---------------|
  179. | | VCORE = 1.2 V | VCORE = 1.5 V | VCORE = 1.8 V |
  180. |-------------- |----------------|---------------|---------------|
  181. |0WS(1CPU cycle)|0 < HCLK <= 2 |0 < HCLK <= 8 |0 < HCLK <= 16 |
  182. |---------------|----------------|---------------|---------------|
  183. |1WS(2CPU cycle)|2 < HCLK <= 4 |8 < HCLK <= 16 |16 < HCLK <= 32|
  184. +----------------------------------------------------------------+
  185. * @{
  186. */
  187. /**
  188. * @brief Resets the RCC clock configuration to the default reset state.
  189. * @note The default reset state of the clock configuration is given below:
  190. * - MSI ON and used as system clock source (MSI range is not modified
  191. * - by this function, it keep the value configured by user application)
  192. * - HSI, HSI_OUT, HSE and PLL OFF
  193. * - AHB, APB1 and APB2 prescaler set to 1.
  194. * - CSS and MCO OFF
  195. * - All interrupts disabled
  196. * @note This function does not modify the configuration of the
  197. * @note -Peripheral clocks
  198. * @note -HSI48, LSI, LSE and RTC clocks
  199. * @retval None
  200. */
  201. void HAL_RCC_DeInit(void)
  202. {
  203. __IO uint32_t tmpreg;
  204. /* Set MSION bit */
  205. SET_BIT(RCC->CR, RCC_CR_MSION);
  206. #if defined(STM32L073xx) || defined(STM32L083xx) || \
  207. defined(STM32L072xx) || defined(STM32L082xx) || \
  208. defined(STM32L071xx) || defined(STM32L081xx) || \
  209. defined(STM32L031xx) || defined(STM32L041xx)
  210. /* Reset HSE, HSI, CSS, PLL */
  211. CLEAR_BIT(RCC->CR, RCC_CR_HSION| RCC_CR_HSIKERON| RCC_CR_HSIDIVEN | RCC_CR_HSIOUTEN | \
  212. RCC_CR_HSEON | RCC_CR_CSSHSEON | RCC_CR_PLLON);
  213. #elif defined(STM32L011xx) || defined(STM32L021xx)
  214. CLEAR_BIT(RCC->CR, RCC_CR_HSION| RCC_CR_HSIKERON| RCC_CR_HSIDIVEN | RCC_CR_HSIOUTEN | \
  215. RCC_CR_HSEON | RCC_CR_PLLON);
  216. #else
  217. CLEAR_BIT(RCC->CR, RCC_CR_HSION| RCC_CR_HSIKERON| RCC_CR_HSIDIVEN | \
  218. RCC_CR_HSEON | RCC_CR_CSSHSEON | RCC_CR_PLLON);
  219. #endif
  220. /* Delay after an RCC peripheral clock */ \
  221. tmpreg = READ_BIT(RCC->CR, RCC_CR_HSEON); \
  222. UNUSED(tmpreg);
  223. /* Reset HSEBYP bit */
  224. CLEAR_BIT(RCC->CR, RCC_CR_HSEBYP);
  225. /* Reset CFGR register */
  226. CLEAR_REG(RCC->CFGR);
  227. /* Disable all interrupts */
  228. CLEAR_REG(RCC->CIER);
  229. /* Update the SystemCoreClock global variable */
  230. SystemCoreClock = MSI_VALUE;
  231. }
  232. /**
  233. * @brief Initializes the RCC Oscillators according to the specified parameters in the
  234. * RCC_OscInitTypeDef.
  235. * @param RCC_OscInitStruct: pointer to an RCC_OscInitTypeDef structure that
  236. * contains the configuration information for the RCC Oscillators.
  237. * @note The PLL is not disabled when used as system clock.
  238. * @note Transitions LSE Bypass to LSE On and LSE On to LSE Bypass are not
  239. * supported by this macro. User should request a transition to LSE Off
  240. * first and then LSE On or LSE Bypass.
  241. * @note Transition HSE Bypass to HSE On and HSE On to HSE Bypass are not
  242. * supported by this macro. User should request a transition to HSE Off
  243. * first and then HSE On or HSE Bypass.
  244. * @retval HAL status
  245. */
  246. HAL_StatusTypeDef HAL_RCC_OscConfig(RCC_OscInitTypeDef *RCC_OscInitStruct)
  247. {
  248. uint32_t tickstart = 0U;
  249. /* Check the parameters */
  250. assert_param(RCC_OscInitStruct != NULL);
  251. assert_param(IS_RCC_OSCILLATORTYPE(RCC_OscInitStruct->OscillatorType));
  252. /*------------------------------- HSE Configuration ------------------------*/
  253. if(((RCC_OscInitStruct->OscillatorType) & RCC_OSCILLATORTYPE_HSE) == RCC_OSCILLATORTYPE_HSE)
  254. {
  255. /* Check the parameters */
  256. assert_param(IS_RCC_HSE(RCC_OscInitStruct->HSEState));
  257. /* When the HSE is used as system clock or clock source for PLL in these cases HSE will not disabled */
  258. if((__HAL_RCC_GET_SYSCLK_SOURCE() == RCC_SYSCLKSOURCE_STATUS_HSE) || ((__HAL_RCC_GET_SYSCLK_SOURCE() == RCC_SYSCLKSOURCE_STATUS_PLLCLK) && ((RCC->CFGR & RCC_CFGR_PLLSRC) == RCC_CFGR_PLLSRC_HSE)))
  259. {
  260. if((__HAL_RCC_GET_FLAG(RCC_FLAG_HSERDY) != RESET) && (RCC_OscInitStruct->HSEState == RCC_HSE_OFF))
  261. {
  262. return HAL_ERROR;
  263. }
  264. }
  265. else
  266. {
  267. /* Set the new HSE configuration ---------------------------------------*/
  268. __HAL_RCC_HSE_CONFIG(RCC_OscInitStruct->HSEState);
  269. /* Check the HSE State */
  270. if((RCC_OscInitStruct->HSEState) != RCC_HSE_OFF)
  271. {
  272. /* Get timeout */
  273. tickstart = HAL_GetTick();
  274. /* Wait till HSE is ready */
  275. while(__HAL_RCC_GET_FLAG(RCC_FLAG_HSERDY) == RESET)
  276. {
  277. if((HAL_GetTick() - tickstart ) > RCC_HSE_TIMEOUT_VALUE)
  278. {
  279. return HAL_TIMEOUT;
  280. }
  281. }
  282. }
  283. else
  284. {
  285. /* Get timeout */
  286. tickstart = HAL_GetTick();
  287. /* Wait till HSE is disabled */
  288. while(__HAL_RCC_GET_FLAG(RCC_FLAG_HSERDY) != RESET)
  289. {
  290. if((HAL_GetTick() - tickstart ) > RCC_HSE_TIMEOUT_VALUE)
  291. {
  292. return HAL_TIMEOUT;
  293. }
  294. }
  295. }
  296. }
  297. }
  298. /*----------------------------- HSI Configuration --------------------------*/
  299. if(((RCC_OscInitStruct->OscillatorType) & RCC_OSCILLATORTYPE_HSI) == RCC_OSCILLATORTYPE_HSI)
  300. {
  301. /* Check the parameters */
  302. assert_param(IS_RCC_HSI(RCC_OscInitStruct->HSIState));
  303. assert_param(IS_RCC_CALIBRATION_VALUE(RCC_OscInitStruct->HSICalibrationValue));
  304. /* When the HSI is used as system clock it will not disabled */
  305. if((__HAL_RCC_GET_SYSCLK_SOURCE() == RCC_SYSCLKSOURCE_STATUS_HSI) || ((__HAL_RCC_GET_SYSCLK_SOURCE() == RCC_SYSCLKSOURCE_STATUS_PLLCLK) && ((RCC->CFGR & RCC_CFGR_PLLSRC) == RCC_CFGR_PLLSRC_HSI)))
  306. {
  307. /* When HSI is used as system clock it will not disabled */
  308. if((__HAL_RCC_GET_FLAG(RCC_FLAG_HSIRDY) != RESET) && (RCC_OscInitStruct->HSIState != RCC_HSI_ON))
  309. {
  310. return HAL_ERROR;
  311. }
  312. /* Otherwise, just the calibration is allowed */
  313. else
  314. {
  315. /* Adjusts the Internal High Speed oscillator (HSI) calibration value.*/
  316. __HAL_RCC_HSI_CALIBRATIONVALUE_ADJUST(RCC_OscInitStruct->HSICalibrationValue);
  317. }
  318. }
  319. else
  320. {
  321. /* Check the HSI State */
  322. if((RCC_OscInitStruct->HSIState)!= RCC_HSI_OFF)
  323. {
  324. /* Enable the Internal High Speed oscillator (HSI or HSIdiv4 */
  325. __HAL_RCC_HSI_CONFIG(RCC_OscInitStruct->HSIState);
  326. /* Get Start Tick*/
  327. tickstart = HAL_GetTick();
  328. /* Wait till HSI is ready */
  329. while(__HAL_RCC_GET_FLAG(RCC_FLAG_HSIRDY) == RESET)
  330. {
  331. if((HAL_GetTick() - tickstart ) > RCC_HSI_TIMEOUT_VALUE)
  332. {
  333. return HAL_TIMEOUT;
  334. }
  335. }
  336. /* Adjusts the Internal High Speed oscillator (HSI) calibration value.*/
  337. __HAL_RCC_HSI_CALIBRATIONVALUE_ADJUST(RCC_OscInitStruct->HSICalibrationValue);
  338. }
  339. else
  340. {
  341. /* Disable the Internal High Speed oscillator (HSI). */
  342. __HAL_RCC_HSI_CONFIG(RCC_OscInitStruct->HSIState);
  343. /* Get Start Tick*/
  344. tickstart = HAL_GetTick();
  345. /* Wait till HSI is ready */
  346. while(__HAL_RCC_GET_FLAG(RCC_FLAG_HSIRDY) != RESET)
  347. {
  348. if((HAL_GetTick() - tickstart ) > RCC_HSI_TIMEOUT_VALUE)
  349. {
  350. return HAL_TIMEOUT;
  351. }
  352. }
  353. }
  354. }
  355. }
  356. /*----------------------------- MSI Configuration --------------------------*/
  357. if(((RCC_OscInitStruct->OscillatorType) & RCC_OSCILLATORTYPE_MSI) == RCC_OSCILLATORTYPE_MSI)
  358. {
  359. /* When the MSI is used as system clock it will not be disabled */
  360. if((__HAL_RCC_GET_SYSCLK_SOURCE() == RCC_CFGR_SWS_MSI) )
  361. {
  362. if((__HAL_RCC_GET_FLAG(RCC_FLAG_MSIRDY) != RESET) && (RCC_OscInitStruct->MSIState == RCC_MSI_OFF))
  363. {
  364. return HAL_ERROR;
  365. }
  366. /* Otherwise, just the calibration and MSI range change are allowed */
  367. else
  368. {
  369. /* Check MSICalibrationValue and MSIClockRange input parameters */
  370. assert_param(IS_RCC_MSICALIBRATION_VALUE(RCC_OscInitStruct->MSICalibrationValue));
  371. assert_param(IS_RCC_MSI_CLOCK_RANGE(RCC_OscInitStruct->MSIClockRange));
  372. /* Selects the Multiple Speed oscillator (MSI) clock range .*/
  373. __HAL_RCC_MSI_RANGE_CONFIG (RCC_OscInitStruct->MSIClockRange);
  374. /* Adjusts the Multiple Speed oscillator (MSI) calibration value.*/
  375. __HAL_RCC_MSI_CALIBRATIONVALUE_ADJUST(RCC_OscInitStruct->MSICalibrationValue);
  376. /* Update the SystemCoreClock global variable */
  377. SystemCoreClock = HAL_RCC_GetSysClockFreq() >> AHBPrescTable[((RCC->CFGR & RCC_CFGR_HPRE) >> 4)];
  378. /* Configure the source of time base considering new system clocks settings*/
  379. HAL_InitTick (TICK_INT_PRIORITY);
  380. }
  381. }
  382. else
  383. {
  384. /* Check the MSI State */
  385. assert_param(IS_RCC_MSI(RCC_OscInitStruct->MSIState));
  386. if((RCC_OscInitStruct->MSIState)!= RCC_MSI_OFF)
  387. {
  388. /* Enable the Internal High Speed oscillator (MSI). */
  389. __HAL_RCC_MSI_ENABLE();
  390. /* Get timeout */
  391. tickstart = HAL_GetTick();
  392. /* Wait till MSI is ready */
  393. while(__HAL_RCC_GET_FLAG(RCC_FLAG_MSIRDY) == RESET)
  394. {
  395. if((HAL_GetTick() - tickstart ) > RCC_MSI_TIMEOUT_VALUE)
  396. {
  397. return HAL_TIMEOUT;
  398. }
  399. }
  400. /* Check MSICalibrationValue and MSIClockRange input parameters */
  401. assert_param(IS_RCC_MSICALIBRATION_VALUE(RCC_OscInitStruct->MSICalibrationValue));
  402. assert_param(IS_RCC_MSI_CLOCK_RANGE(RCC_OscInitStruct->MSIClockRange));
  403. /* Selects the Multiple Speed oscillator (MSI) clock range .*/
  404. __HAL_RCC_MSI_RANGE_CONFIG (RCC_OscInitStruct->MSIClockRange);
  405. /* Adjusts the Multiple Speed oscillator (MSI) calibration value.*/
  406. __HAL_RCC_MSI_CALIBRATIONVALUE_ADJUST(RCC_OscInitStruct->MSICalibrationValue);
  407. }
  408. else
  409. {
  410. /* Disable the Internal High Speed oscillator (MSI). */
  411. __HAL_RCC_MSI_DISABLE();
  412. /* Get timeout */
  413. tickstart = HAL_GetTick();
  414. /* Wait till MSI is ready */
  415. while(__HAL_RCC_GET_FLAG(RCC_FLAG_MSIRDY) != RESET)
  416. {
  417. if((HAL_GetTick() - tickstart ) > RCC_MSI_TIMEOUT_VALUE)
  418. {
  419. return HAL_TIMEOUT;
  420. }
  421. }
  422. }
  423. }
  424. }
  425. /*------------------------------ LSI Configuration -------------------------*/
  426. if(((RCC_OscInitStruct->OscillatorType) & RCC_OSCILLATORTYPE_LSI) == RCC_OSCILLATORTYPE_LSI)
  427. {
  428. /* Check the parameters */
  429. assert_param(IS_RCC_LSI(RCC_OscInitStruct->LSIState));
  430. /* Check the LSI State */
  431. if((RCC_OscInitStruct->LSIState)!= RCC_LSI_OFF)
  432. {
  433. /* Enable the Internal Low Speed oscillator (LSI). */
  434. __HAL_RCC_LSI_ENABLE();
  435. /* Get timeout */
  436. tickstart = HAL_GetTick();
  437. /* Wait till LSI is ready */
  438. while(__HAL_RCC_GET_FLAG(RCC_FLAG_LSIRDY) == RESET)
  439. {
  440. if((HAL_GetTick() - tickstart ) > RCC_LSI_TIMEOUT_VALUE)
  441. {
  442. return HAL_TIMEOUT;
  443. }
  444. }
  445. }
  446. else
  447. {
  448. /* Disable the Internal Low Speed oscillator (LSI). */
  449. __HAL_RCC_LSI_DISABLE();
  450. /* Get timeout */
  451. tickstart = HAL_GetTick();
  452. /* Wait till LSI is ready */
  453. while(__HAL_RCC_GET_FLAG(RCC_FLAG_LSIRDY) != RESET)
  454. {
  455. if((HAL_GetTick() - tickstart ) > RCC_LSI_TIMEOUT_VALUE)
  456. {
  457. return HAL_TIMEOUT;
  458. }
  459. }
  460. }
  461. }
  462. #if !defined (STM32L011xx) && !defined (STM32L021xx) && !defined (STM32L031xx) && !defined (STM32L041xx) && !defined(STM32L051xx) && !defined(STM32L061xx) && !defined(STM32L071xx) && !defined(STM32L081xx)
  463. /*------------------------------ HSI48 Configuration -------------------------*/
  464. if(((RCC_OscInitStruct->OscillatorType) & RCC_OSCILLATORTYPE_HSI48) == RCC_OSCILLATORTYPE_HSI48)
  465. {
  466. /* Check the parameters */
  467. assert_param(IS_RCC_HSI48(RCC_OscInitStruct->HSI48State));
  468. /* Check the HSI48 State */
  469. if((RCC_OscInitStruct->HSI48State)!= RCC_HSI48_OFF)
  470. {
  471. /* Enable the Internal Low Speed oscillator (HSI48). */
  472. __HAL_RCC_HSI48_ENABLE();
  473. /* Get timeout */
  474. tickstart = HAL_GetTick();
  475. /* Wait till HSI48 is ready */
  476. while(__HAL_RCC_GET_FLAG(RCC_FLAG_HSI48RDY) == RESET)
  477. {
  478. if((HAL_GetTick() - tickstart ) > RCC_HSI48_TIMEOUT_VALUE)
  479. {
  480. return HAL_TIMEOUT;
  481. }
  482. }
  483. }
  484. else
  485. {
  486. /* Disable the Internal Low Speed oscillator (HSI48). */
  487. __HAL_RCC_HSI48_DISABLE();
  488. /* Get timeout */
  489. tickstart = HAL_GetTick();
  490. /* Wait till HSI48 is ready */
  491. while(__HAL_RCC_GET_FLAG(RCC_FLAG_HSI48RDY) != RESET)
  492. {
  493. if((HAL_GetTick() - tickstart ) > RCC_HSI48_TIMEOUT_VALUE)
  494. {
  495. return HAL_TIMEOUT;
  496. }
  497. }
  498. }
  499. }
  500. #endif /* !defined (STM32L011xx) && !defined (STM32L021xx) && !(STM32L031xx) && !(STM32L041xx) && !defined(STM32L051xx) && !defined(STM32L061xx) && !defined(STM32L071xx) && !defined(STM32L081xx)*/
  501. /*------------------------------ LSE Configuration -------------------------*/
  502. if(((RCC_OscInitStruct->OscillatorType) & RCC_OSCILLATORTYPE_LSE) == RCC_OSCILLATORTYPE_LSE)
  503. {
  504. FlagStatus pwrclkchanged = RESET;
  505. FlagStatus backupchanged = RESET;
  506. /* Check the parameters */
  507. assert_param(IS_RCC_LSE(RCC_OscInitStruct->LSEState));
  508. /* Update LSE configuration in Backup Domain control register */
  509. /* Requires to enable write access to Backup Domain of necessary */
  510. if(HAL_IS_BIT_CLR(RCC->APB1ENR, RCC_APB1ENR_PWREN))
  511. {
  512. __HAL_RCC_PWR_CLK_ENABLE();
  513. pwrclkchanged = SET;
  514. }
  515. if(HAL_IS_BIT_CLR(PWR->CR, PWR_CR_DBP))
  516. {
  517. /* Enable write access to Backup domain */
  518. SET_BIT(PWR->CR, PWR_CR_DBP);
  519. backupchanged = SET;
  520. /* Wait for Backup domain Write protection disable */
  521. tickstart = HAL_GetTick();
  522. while(HAL_IS_BIT_CLR(PWR->CR, PWR_CR_DBP))
  523. {
  524. if((HAL_GetTick() - tickstart) > RCC_DBP_TIMEOUT_VALUE)
  525. {
  526. return HAL_TIMEOUT;
  527. }
  528. }
  529. }
  530. /* Set the new LSE configuration -----------------------------------------*/
  531. __HAL_RCC_LSE_CONFIG(RCC_OscInitStruct->LSEState);
  532. /* Check the LSE State */
  533. if((RCC_OscInitStruct->LSEState) != RCC_LSE_OFF)
  534. {
  535. /* Get timeout */
  536. tickstart = HAL_GetTick();
  537. /* Wait till LSE is ready */
  538. while(__HAL_RCC_GET_FLAG(RCC_FLAG_LSERDY) == RESET)
  539. {
  540. if((HAL_GetTick() - tickstart ) > RCC_LSE_TIMEOUT_VALUE)
  541. {
  542. return HAL_TIMEOUT;
  543. }
  544. }
  545. }
  546. else
  547. {
  548. /* Get timeout */
  549. tickstart = HAL_GetTick();
  550. /* Wait till LSE is ready */
  551. while(__HAL_RCC_GET_FLAG(RCC_FLAG_LSERDY) != RESET)
  552. {
  553. if((HAL_GetTick() - tickstart ) > RCC_LSE_TIMEOUT_VALUE)
  554. {
  555. return HAL_TIMEOUT;
  556. }
  557. }
  558. }
  559. /* Requires to disable write access to Backup Domain of necessary */
  560. if(backupchanged == SET)
  561. {
  562. CLEAR_BIT(PWR->CR, PWR_CR_DBP);
  563. }
  564. if(pwrclkchanged == SET)
  565. {
  566. __HAL_RCC_PWR_CLK_DISABLE();
  567. }
  568. }
  569. /*-------------------------------- PLL Configuration -----------------------*/
  570. /* Check the parameters */
  571. assert_param(IS_RCC_PLL(RCC_OscInitStruct->PLL.PLLState));
  572. if ((RCC_OscInitStruct->PLL.PLLState) != RCC_PLL_NONE)
  573. {
  574. /* Check if the PLL is used as system clock or not */
  575. if(__HAL_RCC_GET_SYSCLK_SOURCE() != RCC_SYSCLKSOURCE_STATUS_PLLCLK)
  576. {
  577. if((RCC_OscInitStruct->PLL.PLLState) == RCC_PLL_ON)
  578. {
  579. /* Check the parameters */
  580. assert_param(IS_RCC_PLLSOURCE(RCC_OscInitStruct->PLL.PLLSource));
  581. assert_param(IS_RCC_PLL_MUL(RCC_OscInitStruct->PLL.PLLMUL));
  582. assert_param(IS_RCC_PLL_DIV(RCC_OscInitStruct->PLL.PLLDIV));
  583. /* Disable the main PLL. */
  584. __HAL_RCC_PLL_DISABLE();
  585. /* Get timeout */
  586. tickstart = HAL_GetTick();
  587. /* Wait till PLL is ready */
  588. while(__HAL_RCC_GET_FLAG(RCC_FLAG_PLLRDY) != RESET)
  589. {
  590. if((HAL_GetTick() - tickstart ) > RCC_PLL_TIMEOUT_VALUE)
  591. {
  592. return HAL_TIMEOUT;
  593. }
  594. }
  595. /* Configure the main PLL clock source, multiplication and division factors. */
  596. __HAL_RCC_PLL_CONFIG(RCC_OscInitStruct->PLL.PLLSource,
  597. RCC_OscInitStruct->PLL.PLLMUL,
  598. RCC_OscInitStruct->PLL.PLLDIV);
  599. /* Enable the main PLL. */
  600. __HAL_RCC_PLL_ENABLE();
  601. /* Get timeout */
  602. tickstart = HAL_GetTick();
  603. /* Wait till PLL is ready */
  604. while(__HAL_RCC_GET_FLAG(RCC_FLAG_PLLRDY) == RESET)
  605. {
  606. if((HAL_GetTick() - tickstart ) > RCC_PLL_TIMEOUT_VALUE)
  607. {
  608. return HAL_TIMEOUT;
  609. }
  610. }
  611. }
  612. else
  613. {
  614. /* Disable the main PLL. */
  615. __HAL_RCC_PLL_DISABLE();
  616. /* Get timeout */
  617. tickstart = HAL_GetTick();
  618. /* Wait till PLL is ready */
  619. while(__HAL_RCC_GET_FLAG(RCC_FLAG_PLLRDY) != RESET)
  620. {
  621. if((HAL_GetTick() - tickstart ) > RCC_PLL_TIMEOUT_VALUE)
  622. {
  623. return HAL_TIMEOUT;
  624. }
  625. }
  626. }
  627. }
  628. else
  629. {
  630. return HAL_ERROR;
  631. }
  632. }
  633. return HAL_OK;
  634. }
  635. /**
  636. * @brief Initializes the CPU, AHB and APB busses clocks according to the specified
  637. * parameters in the RCC_ClkInitStruct.
  638. * @param RCC_ClkInitStruct: pointer to an RCC_OscInitTypeDef structure that
  639. * contains the configuration information for the RCC peripheral.
  640. * @param FLatency: FLASH Latency, this parameter depends on System Clock Frequency
  641. *
  642. * @note The SystemCoreClock CMSIS variable is used to store System Clock Frequency
  643. * and updated by HAL_RCC_GetHCLKFreq() function called within this function
  644. *
  645. * @note The MSI is used (enabled by hardware) as system clock source after
  646. * startup from Reset, wake-up from STOP and STANDBY mode, or in case
  647. * of failure of the HSE used directly or indirectly as system clock
  648. * (if the Clock Security System CSS is enabled).
  649. *
  650. * @note A switch from one clock source to another occurs only if the target
  651. * clock source is ready (clock stable after startup delay or PLL locked).
  652. * If a clock source which is not yet ready is selected, the switch will
  653. * occur when the clock source will be ready.
  654. * @retval None
  655. */
  656. HAL_StatusTypeDef HAL_RCC_ClockConfig(RCC_ClkInitTypeDef *RCC_ClkInitStruct, uint32_t FLatency)
  657. {
  658. uint32_t tickstart = 0U;
  659. /* Check the parameters */
  660. assert_param(RCC_ClkInitStruct != NULL);
  661. assert_param(IS_RCC_CLOCKTYPE(RCC_ClkInitStruct->ClockType));
  662. assert_param(IS_FLASH_LATENCY(FLatency));
  663. /* To correctly read data from FLASH memory, the number of wait states (LATENCY)
  664. must be correctly programmed according to the frequency of the CPU clock
  665. (HCLK) and the supply voltage of the device. */
  666. /* Increasing the CPU frequency */
  667. if(FLatency > (FLASH->ACR & FLASH_ACR_LATENCY))
  668. {
  669. /* Program the new number of wait states to the LATENCY bits in the FLASH_ACR register */
  670. __HAL_FLASH_SET_LATENCY(FLatency);
  671. /* Check that the new number of wait states is taken into account to access the Flash
  672. memory by reading the FLASH_ACR register */
  673. if((FLASH->ACR & FLASH_ACR_LATENCY) != FLatency)
  674. {
  675. return HAL_ERROR;
  676. }
  677. }
  678. /*-------------------------- HCLK Configuration --------------------------*/
  679. if(((RCC_ClkInitStruct->ClockType) & RCC_CLOCKTYPE_HCLK) == RCC_CLOCKTYPE_HCLK)
  680. {
  681. assert_param(IS_RCC_HCLK(RCC_ClkInitStruct->AHBCLKDivider));
  682. MODIFY_REG(RCC->CFGR, RCC_CFGR_HPRE, RCC_ClkInitStruct->AHBCLKDivider);
  683. }
  684. /*------------------------- SYSCLK Configuration ---------------------------*/
  685. if(((RCC_ClkInitStruct->ClockType) & RCC_CLOCKTYPE_SYSCLK) == RCC_CLOCKTYPE_SYSCLK)
  686. {
  687. assert_param(IS_RCC_SYSCLKSOURCE(RCC_ClkInitStruct->SYSCLKSource));
  688. /* HSE is selected as System Clock Source */
  689. if(RCC_ClkInitStruct->SYSCLKSource == RCC_SYSCLKSOURCE_HSE)
  690. {
  691. /* Check the HSE ready flag */
  692. if(__HAL_RCC_GET_FLAG(RCC_FLAG_HSERDY) == RESET)
  693. {
  694. return HAL_ERROR;
  695. }
  696. }
  697. /* MSI is selected as System Clock Source */
  698. else if(RCC_ClkInitStruct->SYSCLKSource == RCC_SYSCLKSOURCE_MSI)
  699. {
  700. /* Check the MSI ready flag */
  701. if(__HAL_RCC_GET_FLAG(RCC_FLAG_MSIRDY) == RESET)
  702. {
  703. return HAL_ERROR;
  704. }
  705. }
  706. /* PLL is selected as System Clock Source */
  707. else if(RCC_ClkInitStruct->SYSCLKSource == RCC_SYSCLKSOURCE_PLLCLK)
  708. {
  709. /* Check the PLL ready flag */
  710. if(__HAL_RCC_GET_FLAG(RCC_FLAG_PLLRDY) == RESET)
  711. {
  712. return HAL_ERROR;
  713. }
  714. }
  715. /* HSI is selected as System Clock Source */
  716. else
  717. {
  718. /* Check the HSI ready flag */
  719. if(__HAL_RCC_GET_FLAG(RCC_FLAG_HSIRDY) == RESET)
  720. {
  721. return HAL_ERROR;
  722. }
  723. }
  724. MODIFY_REG(RCC->CFGR, RCC_CFGR_SW, RCC_ClkInitStruct->SYSCLKSource);
  725. /* Get timeout */
  726. tickstart = HAL_GetTick();
  727. if(RCC_ClkInitStruct->SYSCLKSource == RCC_SYSCLKSOURCE_HSE)
  728. {
  729. while (__HAL_RCC_GET_SYSCLK_SOURCE() != RCC_SYSCLKSOURCE_STATUS_HSE)
  730. {
  731. if((HAL_GetTick() - tickstart ) > RCC_CLOCKSWITCH_TIMEOUT_VALUE)
  732. {
  733. return HAL_TIMEOUT;
  734. }
  735. }
  736. }
  737. else if(RCC_ClkInitStruct->SYSCLKSource == RCC_SYSCLKSOURCE_PLLCLK)
  738. {
  739. while (__HAL_RCC_GET_SYSCLK_SOURCE() != RCC_SYSCLKSOURCE_STATUS_PLLCLK)
  740. {
  741. if((HAL_GetTick() - tickstart ) > RCC_CLOCKSWITCH_TIMEOUT_VALUE)
  742. {
  743. return HAL_TIMEOUT;
  744. }
  745. }
  746. }
  747. else if(RCC_ClkInitStruct->SYSCLKSource == RCC_SYSCLKSOURCE_MSI)
  748. {
  749. while (__HAL_RCC_GET_SYSCLK_SOURCE() != RCC_CFGR_SWS_MSI)
  750. {
  751. if((HAL_GetTick() - tickstart ) > RCC_CLOCKSWITCH_TIMEOUT_VALUE)
  752. {
  753. return HAL_TIMEOUT;
  754. }
  755. }
  756. }
  757. else
  758. {
  759. while(__HAL_RCC_GET_SYSCLK_SOURCE() != RCC_SYSCLKSOURCE_STATUS_HSI)
  760. {
  761. if((HAL_GetTick() - tickstart ) > RCC_CLOCKSWITCH_TIMEOUT_VALUE)
  762. {
  763. return HAL_TIMEOUT;
  764. }
  765. }
  766. }
  767. }
  768. /* Decreasing the CPU frequency */
  769. if(FLatency <= (FLASH->ACR & FLASH_ACR_LATENCY))
  770. {
  771. /* Program the new number of wait states to the LATENCY bits in the FLASH_ACR register */
  772. __HAL_FLASH_SET_LATENCY(FLatency);
  773. /* Check that the new number of wait states is taken into account to access the Flash
  774. memory by reading the FLASH_ACR register */
  775. if((FLASH->ACR & FLASH_ACR_LATENCY) != FLatency)
  776. {
  777. return HAL_ERROR;
  778. }
  779. }
  780. /*-------------------------- PCLK1 Configuration ---------------------------*/
  781. if(((RCC_ClkInitStruct->ClockType) & RCC_CLOCKTYPE_PCLK1) == RCC_CLOCKTYPE_PCLK1)
  782. {
  783. assert_param(IS_RCC_PCLK(RCC_ClkInitStruct->APB1CLKDivider));
  784. MODIFY_REG(RCC->CFGR, RCC_CFGR_PPRE1, RCC_ClkInitStruct->APB1CLKDivider);
  785. }
  786. /*-------------------------- PCLK2 Configuration ---------------------------*/
  787. if(((RCC_ClkInitStruct->ClockType) & RCC_CLOCKTYPE_PCLK2) == RCC_CLOCKTYPE_PCLK2)
  788. {
  789. assert_param(IS_RCC_PCLK(RCC_ClkInitStruct->APB2CLKDivider));
  790. MODIFY_REG(RCC->CFGR, RCC_CFGR_PPRE2, ((RCC_ClkInitStruct->APB2CLKDivider) << 3));
  791. }
  792. /* Update the SystemCoreClock global variable */
  793. SystemCoreClock = HAL_RCC_GetSysClockFreq() >> AHBPrescTable[((RCC->CFGR & RCC_CFGR_HPRE) >> 4)];
  794. /* Configure the source of time base considering new system clocks settings*/
  795. HAL_InitTick (TICK_INT_PRIORITY);
  796. return HAL_OK;
  797. }
  798. /**
  799. * @}
  800. */
  801. /** @addtogroup RCC_Exported_Functions_Group2 Peripheral Control functions
  802. * @brief RCC clocks control functions
  803. *
  804. @verbatim
  805. ===============================================================================
  806. ##### Peripheral Control functions #####
  807. ===============================================================================
  808. [..]
  809. This subsection provides a set of functions allowing to control the RCC Clocks
  810. frequencies.
  811. @endverbatim
  812. * @{
  813. */
  814. /**
  815. * @brief Selects the clock source to output on MCO pin.
  816. * @note MCO pin should be configured in alternate function mode.
  817. * @param RCC_MCOx: specifies the output direction for the clock source.
  818. * For STM32L0xx family this parameter can have only one value:
  819. * @arg RCC_MCO1: Clock source to output on MCO pin(PA8).
  820. * @arg RCC_MCO2: Clock source to output on MCO pin(PA9).
  821. * @arg RCC_MCO3: Clock source to output on MCO pin(PB13) on STM32L03x/4x/7x/8x .
  822. * @param RCC_MCOSource: specifies the clock source to output.
  823. * This parameter can be one of the following values:
  824. * @arg RCC_MCO1SOURCE_NOCLOCK: No clock selected
  825. * @arg RCC_MCO1SOURCE_SYSCLK: System clock selected
  826. * @arg RCC_MCO1SOURCE_HSI: HSI oscillator clock selected
  827. * @arg RCC_MCO1SOURCE_MSI: MSI oscillator clock selected
  828. * @arg RCC_MCO1SOURCE_HSE: HSE oscillator clock selected
  829. * @arg RCC_MCO1SOURCE_PLLCLK: PLL clock selected
  830. * @arg RCC_MCO1SOURCE_LSI: LSI clock selected
  831. * @arg RCC_MCO1SOURCE_LSE: LSE clock selected
  832. * and in STM32L052xx,STM32L053xx,STM32L062xx, STM32L063xx
  833. * STM32L072xx,STM32L073xx,STM32L082xx, STM32L083xx
  834. * @arg RCC_MCO1SOURCE_HSI48: HSI48 clock selected
  835. * @param RCC_MCODiv: specifies the MCO DIV.
  836. * This parameter can be one of the following values:
  837. * @arg RCC_MCODIV_1: no division applied to MCO clock
  838. * @arg RCC_MCODIV_2: division by 2 applied to MCO clock
  839. * @arg RCC_MCODIV_4: division by 4 applied to MCO clock
  840. * @arg RCC_MCODIV_8: division by 8 applied to MCO clock
  841. * @arg RCC_MCODIV_16: division by 16 applied to MCO clock
  842. * @retval None
  843. */
  844. void HAL_RCC_MCOConfig( uint32_t RCC_MCOx, uint32_t RCC_MCOSource, uint32_t RCC_MCODiv)
  845. {
  846. GPIO_InitTypeDef GPIO_InitStruct;
  847. /* Check the parameters */
  848. assert_param(IS_RCC_MCO(RCC_MCOx));
  849. assert_param(IS_RCC_MCODIV(RCC_MCODiv));
  850. assert_param(IS_RCC_MCO1SOURCE(RCC_MCOSource));
  851. /* Configure the MCO pin in alternate function mode */
  852. GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
  853. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
  854. GPIO_InitStruct.Pull = GPIO_NOPULL;
  855. if(RCC_MCOx == RCC_MCO1)
  856. {
  857. /* MCO Clock Enable */
  858. __MCO1_CLK_ENABLE();
  859. GPIO_InitStruct.Pin = MCO1_PIN;
  860. GPIO_InitStruct.Alternate = GPIO_AF0_MCO;
  861. HAL_GPIO_Init(MCO1_GPIO_PORT, &GPIO_InitStruct);
  862. }
  863. else if (RCC_MCOx == RCC_MCO2)
  864. {
  865. /* MCO Clock Enable */
  866. __MCO2_CLK_ENABLE();
  867. GPIO_InitStruct.Pin = MCO2_PIN;
  868. GPIO_InitStruct.Alternate = GPIO_AF0_MCO;
  869. HAL_GPIO_Init(MCO2_GPIO_PORT, &GPIO_InitStruct);
  870. }
  871. #if defined(STM32L031xx) || defined(STM32L041xx) || defined(STM32L073xx) || defined(STM32L083xx) || \
  872. defined(STM32L072xx) || defined(STM32L082xx) || defined(STM32L071xx) || defined(STM32L081xx)
  873. else
  874. {
  875. /* MCO Clock Enable */
  876. __MCO3_CLK_ENABLE();
  877. GPIO_InitStruct.Pin = MCO3_PIN;
  878. GPIO_InitStruct.Alternate = GPIO_AF2_MCO;
  879. HAL_GPIO_Init(MCO3_GPIO_PORT, &GPIO_InitStruct);
  880. }
  881. #endif
  882. /* Mask MCO and MCOPRE[2:0] bits then Select MCO clock source and prescaler */
  883. MODIFY_REG(RCC->CFGR, (RCC_CFGR_MCOSEL | RCC_CFGR_MCO_PRE), ((RCC_MCOSource | RCC_MCODiv )));
  884. }
  885. #if !defined (STM32L011xx) && !defined (STM32L021xx)
  886. /**
  887. * @brief Enables the Clock Security System.
  888. * @note If a failure is detected on the HSE oscillator clock, this oscillator
  889. * is automatically disabled and an interrupt is generated to inform the
  890. * software about the failure (Clock Security System Interrupt, CSSI),
  891. * allowing the MCU to perform rescue operations. The CSSI is linked to
  892. * the Cortex-M0+ NMI (Non-Maskable Interrupt) exception vector.
  893. * @retval None
  894. */
  895. void HAL_RCC_EnableCSS(void)
  896. {
  897. SET_BIT(RCC->CR, RCC_CR_CSSHSEON) ;
  898. }
  899. #endif
  900. /**
  901. * @brief Returns the SYSCLK frequency
  902. *
  903. * @note The system frequency computed by this function is not the real
  904. * frequency in the chip. It is calculated based on the predefined
  905. * constant and the selected clock source:
  906. * @note If SYSCLK source is MSI, function returns values based on MSI
  907. * Value as defined by the MSI range.
  908. * @note If SYSCLK source is HSI, function returns values based on HSI_VALUE(*)
  909. * @note If SYSCLK source is HSE, function returns values based on HSE_VALUE(**)
  910. * @note If SYSCLK source is PLL, function returns values based on HSE_VALUE(**)
  911. * or HSI_VALUE(*) multiplied/divided by the PLL factors.
  912. * @note (*) HSI_VALUE is a constant defined in stm32l0xx_hal_conf.h file (default value
  913. * 16 MHz) but the real value may vary depending on the variations
  914. * in voltage and temperature.
  915. * @note (**) HSE_VALUE is a constant defined in stm32l0xx_hal_conf.h file (default value
  916. * 8 MHz), user has to ensure that HSE_VALUE is same as the real
  917. * frequency of the crystal used. Otherwise, this function may
  918. * have wrong result.
  919. *
  920. * @note The result of this function could be not correct when using fractional
  921. * value for HSE crystal.
  922. *
  923. * @note This function can be used by the user application to compute the
  924. * baudrate for the communication peripherals or configure other parameters.
  925. *
  926. * @note Each time SYSCLK changes, this function must be called to update the
  927. * right SYSCLK value. Otherwise, any configuration based on this function will be incorrect.
  928. *
  929. *
  930. * @retval SYSCLK frequency
  931. */
  932. uint32_t HAL_RCC_GetSysClockFreq(void)
  933. {
  934. uint32_t pllmul = 0U, plldiv = 0U, pllsource = 0U, msirange = 0U;
  935. uint32_t sysclockfreq = 0U;
  936. /* Get SYSCLK source -------------------------------------------------------*/
  937. /*MSI frequency range in HZ*/
  938. msirange = (RCC->ICSCR & RCC_ICSCR_MSIRANGE) >> 13U;
  939. switch (RCC->CFGR & RCC_CFGR_SWS)
  940. {
  941. case RCC_CFGR_SWS_MSI: /* MSI used as system clock */
  942. {
  943. sysclockfreq = (32768U * (1U << (msirange + 1U)));
  944. break;
  945. }
  946. case RCC_SYSCLKSOURCE_STATUS_HSI: /* HSI used as system clock */
  947. {
  948. if ((RCC->CR & RCC_CR_HSIDIVF) != 0U)
  949. {
  950. sysclockfreq = (HSI_VALUE >> 2U);
  951. }
  952. else
  953. {
  954. sysclockfreq = HSI_VALUE;
  955. }
  956. break;
  957. }
  958. case RCC_SYSCLKSOURCE_STATUS_HSE: /* HSE used as system clock */
  959. {
  960. sysclockfreq = HSE_VALUE;
  961. break;
  962. }
  963. case RCC_SYSCLKSOURCE_STATUS_PLLCLK: /* PLL used as system clock */
  964. {
  965. /* Get PLL clock source and multiplication factor ----------------------*/
  966. pllmul = RCC->CFGR & RCC_CFGR_PLLMUL;
  967. plldiv = RCC->CFGR & RCC_CFGR_PLLDIV;
  968. pllmul = PLLMulTable[(pllmul >> 18U)];
  969. plldiv = (plldiv >> 22U) + 1U;
  970. pllsource = RCC->CFGR & RCC_CFGR_PLLSRC;
  971. if (pllsource == RCC_CFGR_PLLSRC_HSI)
  972. {
  973. /* HSI oscillator clock selected as PLL clock source */
  974. if ((RCC->CR & RCC_CR_HSIDIVF) != 0U)
  975. {
  976. sysclockfreq = (((HSI_VALUE >> 2U) * pllmul) / plldiv);
  977. }
  978. else
  979. {
  980. sysclockfreq =((HSI_VALUE * pllmul) / plldiv);
  981. }
  982. }
  983. else
  984. {
  985. /* HSE selected as PLL clock source */
  986. sysclockfreq = ((HSE_VALUE * pllmul) / plldiv);
  987. }
  988. break;
  989. }
  990. default: /* MSI used as system clock */
  991. {
  992. sysclockfreq = (32768U * (1U << (msirange + 1U)));
  993. break;
  994. }
  995. }
  996. return sysclockfreq;
  997. }
  998. /**
  999. * @brief Returns the HCLK frequency
  1000. * @note Each time HCLK changes, this function must be called to update the
  1001. * right HCLK value. Otherwise, any configuration based on this function will be incorrect.
  1002. *
  1003. * @note The SystemCoreClock CMSIS variable is used to store System Clock Frequency
  1004. * and updated within this function
  1005. * @retval HCLK frequency
  1006. */
  1007. uint32_t HAL_RCC_GetHCLKFreq(void)
  1008. {
  1009. return (SystemCoreClock);
  1010. }
  1011. /**
  1012. * @brief Returns the PCLK1 frequency
  1013. * @note Each time PCLK1 changes, this function must be called to update the
  1014. * right PCLK1 value. Otherwise, any configuration based on this function will be incorrect.
  1015. * @retval PCLK1 frequency
  1016. */
  1017. uint32_t HAL_RCC_GetPCLK1Freq(void)
  1018. {
  1019. return ( HAL_RCC_GetHCLKFreq() >> APBPrescTable[((RCC->CFGR & RCC_CFGR_PPRE1) >> 8U)]);
  1020. }
  1021. /**
  1022. * @brief Returns the PCLK2 frequency
  1023. * @note Each time PCLK2 changes, this function must be called to update the
  1024. * right PCLK2 value. Otherwise, any configuration based on this function will be incorrect.
  1025. * @retval PCLK2 frequency
  1026. */
  1027. uint32_t HAL_RCC_GetPCLK2Freq(void)
  1028. {
  1029. return ( HAL_RCC_GetHCLKFreq() >> APBPrescTable[((RCC->CFGR & RCC_CFGR_PPRE2) >> 11U)]);
  1030. }
  1031. /**
  1032. * @brief Configures the RCC_OscInitStruct according to the internal
  1033. * RCC configuration registers.
  1034. * @param RCC_OscInitStruct: pointer to an RCC_OscInitTypeDef structure that
  1035. * will be configured.
  1036. * @retval None
  1037. */
  1038. void HAL_RCC_GetOscConfig(RCC_OscInitTypeDef *RCC_OscInitStruct)
  1039. {
  1040. /* Set all possible values for the Oscillator type parameter ---------------*/
  1041. RCC_OscInitStruct->OscillatorType = RCC_OSCILLATORTYPE_HSE | RCC_OSCILLATORTYPE_MSI | RCC_OSCILLATORTYPE_HSI | \
  1042. RCC_OSCILLATORTYPE_LSE | RCC_OSCILLATORTYPE_LSI;
  1043. #if !defined (STM32L011xx) && !defined (STM32L021xx) && !defined (STM32L031xx) && !defined (STM32L041xx) && !defined (STM32L031xx) && !defined (STM32L041xx) && !defined(STM32L051xx) && !defined(STM32L061xx) && !defined(STM32L071xx) && !defined(STM32L081xx)
  1044. RCC_OscInitStruct->OscillatorType |= RCC_OSCILLATORTYPE_HSI48;
  1045. #endif
  1046. /* Get the HSE configuration -----------------------------------------------*/
  1047. if((RCC->CR &RCC_CR_HSEBYP) == RCC_CR_HSEBYP)
  1048. {
  1049. RCC_OscInitStruct->HSEState = RCC_HSE_BYPASS;
  1050. }
  1051. else if((RCC->CR &RCC_CR_HSEON) == RCC_CR_HSEON)
  1052. {
  1053. RCC_OscInitStruct->HSEState = RCC_HSE_ON;
  1054. }
  1055. else
  1056. {
  1057. RCC_OscInitStruct->HSEState = RCC_HSE_OFF;
  1058. }
  1059. /* Get the MSI configuration -----------------------------------------------*/
  1060. if((RCC->CR &RCC_CR_MSION) == RCC_CR_MSION)
  1061. {
  1062. RCC_OscInitStruct->MSIState = RCC_MSI_ON;
  1063. }
  1064. else
  1065. {
  1066. RCC_OscInitStruct->MSIState = RCC_MSI_OFF;
  1067. }
  1068. RCC_OscInitStruct->MSICalibrationValue = (uint32_t)((RCC->CR &RCC_ICSCR_MSITRIM) >> 24U);
  1069. RCC_OscInitStruct->MSIClockRange = (uint32_t)((RCC->ICSCR &RCC_ICSCR_MSIRANGE) >> 13U);
  1070. #if !defined (STM32L011xx) && !defined (STM32L021xx) && !defined (STM32L031xx) && !defined (STM32L041xx) && !defined(STM32L051xx) && !defined(STM32L061xx) && !defined(STM32L071xx) && !defined(STM32L081xx)
  1071. /* Get the HSI48 configuration -----------------------------------------------*/
  1072. if((RCC->CRRCR &RCC_CRRCR_HSI48ON) == RCC_CRRCR_HSI48ON)
  1073. {
  1074. RCC_OscInitStruct->HSI48State = RCC_HSI48_ON;
  1075. }
  1076. else
  1077. {
  1078. RCC_OscInitStruct->HSI48State = RCC_HSI48_OFF;
  1079. }
  1080. #endif
  1081. /* Get the HSI configuration -----------------------------------------------*/
  1082. if((RCC->CR &RCC_CR_HSION) == RCC_CR_HSION)
  1083. {
  1084. RCC_OscInitStruct->HSIState = RCC_HSI_ON;
  1085. }
  1086. else
  1087. {
  1088. RCC_OscInitStruct->HSIState = RCC_HSI_OFF;
  1089. }
  1090. RCC_OscInitStruct->HSICalibrationValue = (uint32_t)((RCC->ICSCR &RCC_ICSCR_HSITRIM) >> 8U);
  1091. /* Get the LSE configuration -----------------------------------------------*/
  1092. if((RCC->CSR &RCC_CSR_LSEBYP) == RCC_CSR_LSEBYP)
  1093. {
  1094. RCC_OscInitStruct->LSEState = RCC_LSE_BYPASS;
  1095. }
  1096. else if((RCC->CSR &RCC_CSR_LSEON) == RCC_CSR_LSEON)
  1097. {
  1098. RCC_OscInitStruct->LSEState = RCC_LSE_ON;
  1099. }
  1100. else
  1101. {
  1102. RCC_OscInitStruct->LSEState = RCC_LSE_OFF;
  1103. }
  1104. /* Get the LSI configuration -----------------------------------------------*/
  1105. if((RCC->CSR &RCC_CSR_LSION) == RCC_CSR_LSION)
  1106. {
  1107. RCC_OscInitStruct->LSIState = RCC_LSI_ON;
  1108. }
  1109. else
  1110. {
  1111. RCC_OscInitStruct->LSIState = RCC_LSI_OFF;
  1112. }
  1113. /* Get the PLL configuration -----------------------------------------------*/
  1114. if((RCC->CR &RCC_CR_PLLON) == RCC_CR_PLLON)
  1115. {
  1116. RCC_OscInitStruct->PLL.PLLState = RCC_PLL_ON;
  1117. }
  1118. else
  1119. {
  1120. RCC_OscInitStruct->PLL.PLLState = RCC_PLL_OFF;
  1121. }
  1122. RCC_OscInitStruct->PLL.PLLSource = (uint32_t)(RCC->CFGR & RCC_CFGR_PLLSRC);
  1123. RCC_OscInitStruct->PLL.PLLMUL = (uint32_t)(RCC->CFGR & RCC_CFGR_PLLMUL) >> 18U;
  1124. RCC_OscInitStruct->PLL.PLLDIV = (uint32_t)(RCC->CFGR & RCC_CFGR_PLLDIV) >> 22U;
  1125. }
  1126. /**
  1127. * @brief Configures the RCC_ClkInitStruct according to the internal
  1128. * RCC configuration registers.
  1129. * @param RCC_ClkInitStruct: pointer to an RCC_ClkInitTypeDef structure that
  1130. * will be configured.
  1131. * @param pFLatency: Pointer on the Flash Latency.
  1132. * @retval None
  1133. */
  1134. void HAL_RCC_GetClockConfig(RCC_ClkInitTypeDef *RCC_ClkInitStruct, uint32_t *pFLatency)
  1135. {
  1136. /* Set all possible values for the Clock type parameter --------------------*/
  1137. RCC_ClkInitStruct->ClockType = RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2;
  1138. /* Get the SYSCLK configuration --------------------------------------------*/
  1139. RCC_ClkInitStruct->SYSCLKSource = (uint32_t)(RCC->CFGR & RCC_CFGR_SW);
  1140. /* Get the HCLK configuration ----------------------------------------------*/
  1141. RCC_ClkInitStruct->AHBCLKDivider = (uint32_t)(RCC->CFGR & RCC_CFGR_HPRE);
  1142. /* Get the APB1 configuration ----------------------------------------------*/
  1143. RCC_ClkInitStruct->APB1CLKDivider = (uint32_t)(RCC->CFGR & RCC_CFGR_PPRE1);
  1144. /* Get the APB2 configuration ----------------------------------------------*/
  1145. RCC_ClkInitStruct->APB2CLKDivider = (uint32_t)((RCC->CFGR & RCC_CFGR_PPRE2) >> 3U);
  1146. /* Get the Flash Wait State (Latency) configuration ------------------------*/
  1147. *pFLatency = (uint32_t)(FLASH->ACR & FLASH_ACR_LATENCY);
  1148. }
  1149. #if !defined (STM32L011xx) && !defined (STM32L021xx)
  1150. /**
  1151. * @brief This function handles the RCC CSS interrupt request.
  1152. * @note This API should be called under the NMI_Handler().
  1153. * @retval None
  1154. */
  1155. void HAL_RCC_NMI_IRQHandler(void)
  1156. {
  1157. /* Check RCC CSSF flag */
  1158. if(__HAL_RCC_GET_IT(RCC_IT_CSSHSE))
  1159. {
  1160. /* RCC Clock Security System interrupt user callback */
  1161. HAL_RCC_CSSCallback();
  1162. /* Clear RCC CSS pending bit */
  1163. __HAL_RCC_CLEAR_IT(RCC_IT_CSSHSE);
  1164. }
  1165. }
  1166. /**
  1167. * @brief RCC Clock Security System interrupt callback
  1168. * @retval None
  1169. */
  1170. __weak void HAL_RCC_CSSCallback(void)
  1171. {
  1172. /* NOTE : This function Should not be modified, when the callback is needed,
  1173. the HAL_RCC_CSSCallback could be implemented in the user file
  1174. */
  1175. }
  1176. #endif
  1177. /**
  1178. * @}
  1179. */
  1180. /**
  1181. * @}
  1182. */
  1183. /**
  1184. * @}
  1185. */
  1186. #endif /* HAL_RCC_MODULE_ENABLED */
  1187. /**
  1188. * @}
  1189. */
  1190. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/