stm32f1xx_hal_rcc.c 47 KB

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