stm32l1xx_hal_rcc.c 50 KB

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