stm32f4xx_hal_rcc.c 41 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097
  1. /**
  2. ******************************************************************************
  3. * @file stm32f4xx_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 Internal High Speed oscillator
  17. (HSI 16MHz) with Flash 0 wait state, Flash prefetch buffer, D-Cache
  18. and I-Cache are disabled, and all peripherals are off except internal
  19. SRAM, Flash and JTAG.
  20. (+) There is no prescaler on High speed (AHB) and Low speed (APB) busses;
  21. all peripherals mapped on these busses are running at HSI speed.
  22. (+) The clock for all peripherals is switched off, except the SRAM and FLASH.
  23. (+) All GPIOs are in input floating state, except the JTAG pins which
  24. are assigned to be used for debug purpose.
  25. [..]
  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 busses prescalers
  31. (+) Enable the clock for the peripheral(s) to be used
  32. (+) Configure the clock source(s) for peripherals which clocks are not
  33. derived from the System clock (I2S, RTC, ADC, USB OTG FS/SDIO/RNG)
  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. (+) If peripheral is mapped on AHB: the delay is 2 AHB clock cycle
  42. after the clock enable bit is set on the hardware register
  43. (+) If peripheral is mapped on APB: the delay is 2 APB clock cycle
  44. after the clock enable bit is set on the hardware register
  45. [..]
  46. Implemented Workaround:
  47. (+) For AHB & APB peripherals, a dummy read to the peripheral register has been
  48. inserted in each __HAL_RCC_PPP_CLK_ENABLE() macro.
  49. @endverbatim
  50. ******************************************************************************
  51. * @attention
  52. *
  53. * <h2><center>&copy; COPYRIGHT(c) 2017 STMicroelectronics</center></h2>
  54. *
  55. * Redistribution and use in source and binary forms, with or without modification,
  56. * are permitted provided that the following conditions are met:
  57. * 1. Redistributions of source code must retain the above copyright notice,
  58. * this list of conditions and the following disclaimer.
  59. * 2. Redistributions in binary form must reproduce the above copyright notice,
  60. * this list of conditions and the following disclaimer in the documentation
  61. * and/or other materials provided with the distribution.
  62. * 3. Neither the name of STMicroelectronics nor the names of its contributors
  63. * may be used to endorse or promote products derived from this software
  64. * without specific prior written permission.
  65. *
  66. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  67. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  68. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  69. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  70. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  71. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  72. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  73. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  74. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  75. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  76. *
  77. ******************************************************************************
  78. */
  79. /* Includes ------------------------------------------------------------------*/
  80. #include "stm32f4xx_hal.h"
  81. /** @addtogroup STM32F4xx_HAL_Driver
  82. * @{
  83. */
  84. /** @defgroup RCC RCC
  85. * @brief RCC HAL module driver
  86. * @{
  87. */
  88. #ifdef HAL_RCC_MODULE_ENABLED
  89. /* Private typedef -----------------------------------------------------------*/
  90. /* Private define ------------------------------------------------------------*/
  91. /** @addtogroup RCC_Private_Constants
  92. * @{
  93. */
  94. /* Private macro -------------------------------------------------------------*/
  95. #define __MCO1_CLK_ENABLE() __HAL_RCC_GPIOA_CLK_ENABLE()
  96. #define MCO1_GPIO_PORT GPIOA
  97. #define MCO1_PIN GPIO_PIN_8
  98. #define __MCO2_CLK_ENABLE() __HAL_RCC_GPIOC_CLK_ENABLE()
  99. #define MCO2_GPIO_PORT GPIOC
  100. #define MCO2_PIN GPIO_PIN_9
  101. /**
  102. * @}
  103. */
  104. /* Private variables ---------------------------------------------------------*/
  105. /** @defgroup RCC_Private_Variables RCC Private Variables
  106. * @{
  107. */
  108. /**
  109. * @}
  110. */
  111. /* Private function prototypes -----------------------------------------------*/
  112. /* Private functions ---------------------------------------------------------*/
  113. /** @defgroup RCC_Exported_Functions RCC Exported Functions
  114. * @{
  115. */
  116. /** @defgroup RCC_Exported_Functions_Group1 Initialization and de-initialization functions
  117. * @brief Initialization and Configuration functions
  118. *
  119. @verbatim
  120. ===============================================================================
  121. ##### Initialization and de-initialization functions #####
  122. ===============================================================================
  123. [..]
  124. This section provides functions allowing to configure the internal/external oscillators
  125. (HSE, HSI, LSE, LSI, PLL, CSS and MCO) and the System busses clocks (SYSCLK, AHB, APB1
  126. and APB2).
  127. [..] Internal/external clock and PLL configuration
  128. (#) HSI (high-speed internal), 16 MHz factory-trimmed RC used directly or through
  129. the PLL as System clock source.
  130. (#) LSI (low-speed internal), 32 KHz low consumption RC used as IWDG and/or RTC
  131. clock source.
  132. (#) HSE (high-speed external), 4 to 26 MHz crystal oscillator used directly or
  133. through the PLL as System clock source. Can be used also as RTC clock source.
  134. (#) LSE (low-speed external), 32 KHz oscillator used as RTC clock source.
  135. (#) PLL (clocked by HSI or HSE), featuring two different output clocks:
  136. (++) The first output is used to generate the high speed system clock (up to 168 MHz)
  137. (++) The second output is used to generate the clock for the USB OTG FS (48 MHz),
  138. the random analog generator (<=48 MHz) and the SDIO (<= 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-M4 NMI
  143. (Non-Maskable Interrupt) exception vector.
  144. (#) MCO1 (microcontroller clock output), used to output HSI, LSE, HSE or PLL
  145. clock (through a configurable prescaler) on PA8 pin.
  146. (#) MCO2 (microcontroller clock output), used to output HSE, PLL, SYSCLK or PLLI2S
  147. clock (through a configurable prescaler) on PC9 pin.
  148. [..] System, AHB and APB busses clocks configuration
  149. (#) Several clock sources can be used to drive the System clock (SYSCLK): HSI,
  150. HSE and PLL.
  151. The AHB clock (HCLK) is derived from System clock through configurable
  152. prescaler and used to clock the CPU, memory and peripherals mapped
  153. on AHB bus (DMA, GPIO...). APB1 (PCLK1) and APB2 (PCLK2) clocks are derived
  154. from AHB clock through configurable prescalers and used to clock
  155. the peripherals mapped on these busses. You can use
  156. "HAL_RCC_GetSysClockFreq()" function to retrieve the frequencies of these clocks.
  157. (#) For the STM32F405xx/07xx and STM32F415xx/17xx devices, the maximum
  158. frequency of the SYSCLK and HCLK is 168 MHz, PCLK2 84 MHz and PCLK1 42 MHz.
  159. Depending on the device voltage range, the maximum frequency should
  160. be adapted accordingly (refer to the product datasheets for more details).
  161. (#) For the STM32F42xxx, STM32F43xxx, STM32F446xx, STM32F469xx and STM32F479xx devices,
  162. the maximum frequency of the SYSCLK and HCLK is 180 MHz, PCLK2 90 MHz and PCLK1 45 MHz.
  163. Depending on the device voltage range, the maximum frequency should
  164. be adapted accordingly (refer to the product datasheets for more details).
  165. (#) For the STM32F401xx, the maximum frequency of the SYSCLK and HCLK is 84 MHz,
  166. PCLK2 84 MHz and PCLK1 42 MHz.
  167. Depending on the device voltage range, the maximum frequency should
  168. be adapted accordingly (refer to the product datasheets for more details).
  169. (#) For the STM32F41xxx, the maximum frequency of the SYSCLK and HCLK is 100 MHz,
  170. PCLK2 100 MHz and PCLK1 50 MHz.
  171. Depending on the device voltage range, the maximum frequency should
  172. be adapted accordingly (refer to the product datasheets for more details).
  173. @endverbatim
  174. * @{
  175. */
  176. /**
  177. * @brief Resets the RCC clock configuration to the default reset state.
  178. * @note The default reset state of the clock configuration is given below:
  179. * - HSI ON and used as system clock source
  180. * - HSE and PLL OFF
  181. * - AHB, APB1 and APB2 prescaler set to 1.
  182. * - CSS, MCO1 and MCO2 OFF
  183. * - All interrupts disabled
  184. * @note This function doesn't modify the configuration of the
  185. * - Peripheral clocks
  186. * - LSI, LSE and RTC clocks
  187. * @retval HAL status
  188. */
  189. __weak HAL_StatusTypeDef HAL_RCC_DeInit(void)
  190. {
  191. return HAL_OK;
  192. }
  193. /**
  194. * @brief Initializes the RCC Oscillators according to the specified parameters in the
  195. * RCC_OscInitTypeDef.
  196. * @param RCC_OscInitStruct pointer to an RCC_OscInitTypeDef structure that
  197. * contains the configuration information for the RCC Oscillators.
  198. * @note The PLL is not disabled when used as system clock.
  199. * @note Transitions LSE Bypass to LSE On and LSE On to LSE Bypass are not
  200. * supported by this API. User should request a transition to LSE Off
  201. * first and then LSE On or LSE Bypass.
  202. * @note Transition HSE Bypass to HSE On and HSE On to HSE Bypass are not
  203. * supported by this API. User should request a transition to HSE Off
  204. * first and then HSE On or HSE Bypass.
  205. * @retval HAL status
  206. */
  207. __weak HAL_StatusTypeDef HAL_RCC_OscConfig(RCC_OscInitTypeDef *RCC_OscInitStruct)
  208. {
  209. uint32_t tickstart;
  210. /* Check Null pointer */
  211. if(RCC_OscInitStruct == NULL)
  212. {
  213. return HAL_ERROR;
  214. }
  215. /* Check the parameters */
  216. assert_param(IS_RCC_OSCILLATORTYPE(RCC_OscInitStruct->OscillatorType));
  217. /*------------------------------- HSE Configuration ------------------------*/
  218. if(((RCC_OscInitStruct->OscillatorType) & RCC_OSCILLATORTYPE_HSE) == RCC_OSCILLATORTYPE_HSE)
  219. {
  220. /* Check the parameters */
  221. assert_param(IS_RCC_HSE(RCC_OscInitStruct->HSEState));
  222. /* When the HSE is used as system clock or clock source for PLL in these cases HSE will not disabled */
  223. if((__HAL_RCC_GET_SYSCLK_SOURCE() == RCC_CFGR_SWS_HSE) ||\
  224. ((__HAL_RCC_GET_SYSCLK_SOURCE() == RCC_CFGR_SWS_PLL) && ((RCC->PLLCFGR & RCC_PLLCFGR_PLLSRC) == RCC_PLLCFGR_PLLSRC_HSE)))
  225. {
  226. if((__HAL_RCC_GET_FLAG(RCC_FLAG_HSERDY) != RESET) && (RCC_OscInitStruct->HSEState == RCC_HSE_OFF))
  227. {
  228. return HAL_ERROR;
  229. }
  230. }
  231. else
  232. {
  233. /* Set the new HSE configuration ---------------------------------------*/
  234. __HAL_RCC_HSE_CONFIG(RCC_OscInitStruct->HSEState);
  235. /* Check the HSE State */
  236. if((RCC_OscInitStruct->HSEState) != RCC_HSE_OFF)
  237. {
  238. /* Get Start Tick */
  239. tickstart = HAL_GetTick();
  240. /* Wait till HSE is ready */
  241. while(__HAL_RCC_GET_FLAG(RCC_FLAG_HSERDY) == RESET)
  242. {
  243. if((HAL_GetTick() - tickstart ) > HSE_TIMEOUT_VALUE)
  244. {
  245. return HAL_TIMEOUT;
  246. }
  247. }
  248. }
  249. else
  250. {
  251. /* Get Start Tick */
  252. tickstart = HAL_GetTick();
  253. /* Wait till HSE is bypassed or disabled */
  254. while(__HAL_RCC_GET_FLAG(RCC_FLAG_HSERDY) != RESET)
  255. {
  256. if((HAL_GetTick() - tickstart ) > HSE_TIMEOUT_VALUE)
  257. {
  258. return HAL_TIMEOUT;
  259. }
  260. }
  261. }
  262. }
  263. }
  264. /*----------------------------- HSI Configuration --------------------------*/
  265. if(((RCC_OscInitStruct->OscillatorType) & RCC_OSCILLATORTYPE_HSI) == RCC_OSCILLATORTYPE_HSI)
  266. {
  267. /* Check the parameters */
  268. assert_param(IS_RCC_HSI(RCC_OscInitStruct->HSIState));
  269. assert_param(IS_RCC_CALIBRATION_VALUE(RCC_OscInitStruct->HSICalibrationValue));
  270. /* Check if HSI is used as system clock or as PLL source when PLL is selected as system clock */
  271. if((__HAL_RCC_GET_SYSCLK_SOURCE() == RCC_CFGR_SWS_HSI) ||\
  272. ((__HAL_RCC_GET_SYSCLK_SOURCE() == RCC_CFGR_SWS_PLL) && ((RCC->PLLCFGR & RCC_PLLCFGR_PLLSRC) == RCC_PLLCFGR_PLLSRC_HSI)))
  273. {
  274. /* When HSI is used as system clock it will not disabled */
  275. if((__HAL_RCC_GET_FLAG(RCC_FLAG_HSIRDY) != RESET) && (RCC_OscInitStruct->HSIState != RCC_HSI_ON))
  276. {
  277. return HAL_ERROR;
  278. }
  279. /* Otherwise, just the calibration is allowed */
  280. else
  281. {
  282. /* Adjusts the Internal High Speed oscillator (HSI) calibration value.*/
  283. __HAL_RCC_HSI_CALIBRATIONVALUE_ADJUST(RCC_OscInitStruct->HSICalibrationValue);
  284. }
  285. }
  286. else
  287. {
  288. /* Check the HSI State */
  289. if((RCC_OscInitStruct->HSIState)!= RCC_HSI_OFF)
  290. {
  291. /* Enable the Internal High Speed oscillator (HSI). */
  292. __HAL_RCC_HSI_ENABLE();
  293. /* Get Start Tick*/
  294. tickstart = HAL_GetTick();
  295. /* Wait till HSI is ready */
  296. while(__HAL_RCC_GET_FLAG(RCC_FLAG_HSIRDY) == RESET)
  297. {
  298. if((HAL_GetTick() - tickstart ) > HSI_TIMEOUT_VALUE)
  299. {
  300. return HAL_TIMEOUT;
  301. }
  302. }
  303. /* Adjusts the Internal High Speed oscillator (HSI) calibration value. */
  304. __HAL_RCC_HSI_CALIBRATIONVALUE_ADJUST(RCC_OscInitStruct->HSICalibrationValue);
  305. }
  306. else
  307. {
  308. /* Disable the Internal High Speed oscillator (HSI). */
  309. __HAL_RCC_HSI_DISABLE();
  310. /* Get Start Tick*/
  311. tickstart = HAL_GetTick();
  312. /* Wait till HSI is ready */
  313. while(__HAL_RCC_GET_FLAG(RCC_FLAG_HSIRDY) != RESET)
  314. {
  315. if((HAL_GetTick() - tickstart ) > HSI_TIMEOUT_VALUE)
  316. {
  317. return HAL_TIMEOUT;
  318. }
  319. }
  320. }
  321. }
  322. }
  323. /*------------------------------ LSI Configuration -------------------------*/
  324. if(((RCC_OscInitStruct->OscillatorType) & RCC_OSCILLATORTYPE_LSI) == RCC_OSCILLATORTYPE_LSI)
  325. {
  326. /* Check the parameters */
  327. assert_param(IS_RCC_LSI(RCC_OscInitStruct->LSIState));
  328. /* Check the LSI State */
  329. if((RCC_OscInitStruct->LSIState)!= RCC_LSI_OFF)
  330. {
  331. /* Enable the Internal Low Speed oscillator (LSI). */
  332. __HAL_RCC_LSI_ENABLE();
  333. /* Get Start Tick*/
  334. tickstart = HAL_GetTick();
  335. /* Wait till LSI is ready */
  336. while(__HAL_RCC_GET_FLAG(RCC_FLAG_LSIRDY) == RESET)
  337. {
  338. if((HAL_GetTick() - tickstart ) > LSI_TIMEOUT_VALUE)
  339. {
  340. return HAL_TIMEOUT;
  341. }
  342. }
  343. }
  344. else
  345. {
  346. /* Disable the Internal Low Speed oscillator (LSI). */
  347. __HAL_RCC_LSI_DISABLE();
  348. /* Get Start Tick */
  349. tickstart = HAL_GetTick();
  350. /* Wait till LSI is ready */
  351. while(__HAL_RCC_GET_FLAG(RCC_FLAG_LSIRDY) != RESET)
  352. {
  353. if((HAL_GetTick() - tickstart ) > LSI_TIMEOUT_VALUE)
  354. {
  355. return HAL_TIMEOUT;
  356. }
  357. }
  358. }
  359. }
  360. /*------------------------------ LSE Configuration -------------------------*/
  361. if(((RCC_OscInitStruct->OscillatorType) & RCC_OSCILLATORTYPE_LSE) == RCC_OSCILLATORTYPE_LSE)
  362. {
  363. /* Check the parameters */
  364. assert_param(IS_RCC_LSE(RCC_OscInitStruct->LSEState));
  365. /* Enable Power Clock */
  366. __HAL_RCC_PWR_CLK_ENABLE();
  367. /* Enable write access to Backup domain */
  368. PWR->CR |= PWR_CR_DBP;
  369. /* Wait for Backup domain Write protection enable */
  370. tickstart = HAL_GetTick();
  371. while((PWR->CR & PWR_CR_DBP) == RESET)
  372. {
  373. if((HAL_GetTick() - tickstart ) > RCC_DBP_TIMEOUT_VALUE)
  374. {
  375. return HAL_TIMEOUT;
  376. }
  377. }
  378. /* Set the new LSE configuration -----------------------------------------*/
  379. __HAL_RCC_LSE_CONFIG(RCC_OscInitStruct->LSEState);
  380. /* Check the LSE State */
  381. if((RCC_OscInitStruct->LSEState) != RCC_LSE_OFF)
  382. {
  383. /* Get Start Tick*/
  384. tickstart = HAL_GetTick();
  385. /* Wait till LSE is ready */
  386. while(__HAL_RCC_GET_FLAG(RCC_FLAG_LSERDY) == RESET)
  387. {
  388. if((HAL_GetTick() - tickstart ) > RCC_LSE_TIMEOUT_VALUE)
  389. {
  390. return HAL_TIMEOUT;
  391. }
  392. }
  393. }
  394. else
  395. {
  396. /* Get Start Tick */
  397. tickstart = HAL_GetTick();
  398. /* Wait till LSE is ready */
  399. while(__HAL_RCC_GET_FLAG(RCC_FLAG_LSERDY) != RESET)
  400. {
  401. if((HAL_GetTick() - tickstart ) > RCC_LSE_TIMEOUT_VALUE)
  402. {
  403. return HAL_TIMEOUT;
  404. }
  405. }
  406. }
  407. }
  408. /*-------------------------------- PLL Configuration -----------------------*/
  409. /* Check the parameters */
  410. assert_param(IS_RCC_PLL(RCC_OscInitStruct->PLL.PLLState));
  411. if ((RCC_OscInitStruct->PLL.PLLState) != RCC_PLL_NONE)
  412. {
  413. /* Check if the PLL is used as system clock or not */
  414. if(__HAL_RCC_GET_SYSCLK_SOURCE() != RCC_CFGR_SWS_PLL)
  415. {
  416. if((RCC_OscInitStruct->PLL.PLLState) == RCC_PLL_ON)
  417. {
  418. /* Check the parameters */
  419. assert_param(IS_RCC_PLLSOURCE(RCC_OscInitStruct->PLL.PLLSource));
  420. assert_param(IS_RCC_PLLM_VALUE(RCC_OscInitStruct->PLL.PLLM));
  421. assert_param(IS_RCC_PLLN_VALUE(RCC_OscInitStruct->PLL.PLLN));
  422. assert_param(IS_RCC_PLLP_VALUE(RCC_OscInitStruct->PLL.PLLP));
  423. assert_param(IS_RCC_PLLQ_VALUE(RCC_OscInitStruct->PLL.PLLQ));
  424. /* Disable the main PLL. */
  425. __HAL_RCC_PLL_DISABLE();
  426. /* Get Start Tick */
  427. tickstart = HAL_GetTick();
  428. /* Wait till PLL is ready */
  429. while(__HAL_RCC_GET_FLAG(RCC_FLAG_PLLRDY) != RESET)
  430. {
  431. if((HAL_GetTick() - tickstart ) > PLL_TIMEOUT_VALUE)
  432. {
  433. return HAL_TIMEOUT;
  434. }
  435. }
  436. /* Configure the main PLL clock source, multiplication and division factors. */
  437. WRITE_REG(RCC->PLLCFGR, (RCC_OscInitStruct->PLL.PLLSource | \
  438. RCC_OscInitStruct->PLL.PLLM | \
  439. (RCC_OscInitStruct->PLL.PLLN << RCC_PLLCFGR_PLLN_Pos) | \
  440. (((RCC_OscInitStruct->PLL.PLLP >> 1U) - 1U) << RCC_PLLCFGR_PLLP_Pos) | \
  441. (RCC_OscInitStruct->PLL.PLLQ << RCC_PLLCFGR_PLLQ_Pos)));
  442. /* Enable the main PLL. */
  443. __HAL_RCC_PLL_ENABLE();
  444. /* Get Start Tick */
  445. tickstart = HAL_GetTick();
  446. /* Wait till PLL is ready */
  447. while(__HAL_RCC_GET_FLAG(RCC_FLAG_PLLRDY) == RESET)
  448. {
  449. if((HAL_GetTick() - tickstart ) > PLL_TIMEOUT_VALUE)
  450. {
  451. return HAL_TIMEOUT;
  452. }
  453. }
  454. }
  455. else
  456. {
  457. /* Disable the main PLL. */
  458. __HAL_RCC_PLL_DISABLE();
  459. /* Get Start Tick */
  460. tickstart = HAL_GetTick();
  461. /* Wait till PLL is ready */
  462. while(__HAL_RCC_GET_FLAG(RCC_FLAG_PLLRDY) != RESET)
  463. {
  464. if((HAL_GetTick() - tickstart ) > PLL_TIMEOUT_VALUE)
  465. {
  466. return HAL_TIMEOUT;
  467. }
  468. }
  469. }
  470. }
  471. else
  472. {
  473. return HAL_ERROR;
  474. }
  475. }
  476. return HAL_OK;
  477. }
  478. /**
  479. * @brief Initializes the CPU, AHB and APB busses clocks according to the specified
  480. * parameters in the RCC_ClkInitStruct.
  481. * @param RCC_ClkInitStruct pointer to an RCC_OscInitTypeDef structure that
  482. * contains the configuration information for the RCC peripheral.
  483. * @param FLatency FLASH Latency, this parameter depend on device selected
  484. *
  485. * @note The SystemCoreClock CMSIS variable is used to store System Clock Frequency
  486. * and updated by HAL_RCC_GetHCLKFreq() function called within this function
  487. *
  488. * @note The HSI is used (enabled by hardware) as system clock source after
  489. * startup from Reset, wake-up from STOP and STANDBY mode, or in case
  490. * of failure of the HSE used directly or indirectly as system clock
  491. * (if the Clock Security System CSS is enabled).
  492. *
  493. * @note A switch from one clock source to another occurs only if the target
  494. * clock source is ready (clock stable after startup delay or PLL locked).
  495. * If a clock source which is not yet ready is selected, the switch will
  496. * occur when the clock source will be ready.
  497. *
  498. * @note Depending on the device voltage range, the software has to set correctly
  499. * HPRE[3:0] bits to ensure that HCLK not exceed the maximum allowed frequency
  500. * (for more details refer to section above "Initialization/de-initialization functions")
  501. * @retval None
  502. */
  503. HAL_StatusTypeDef HAL_RCC_ClockConfig(RCC_ClkInitTypeDef *RCC_ClkInitStruct, uint32_t FLatency)
  504. {
  505. uint32_t tickstart;
  506. /* Check Null pointer */
  507. if(RCC_ClkInitStruct == NULL)
  508. {
  509. return HAL_ERROR;
  510. }
  511. /* Check the parameters */
  512. assert_param(IS_RCC_CLOCKTYPE(RCC_ClkInitStruct->ClockType));
  513. assert_param(IS_FLASH_LATENCY(FLatency));
  514. /* To correctly read data from FLASH memory, the number of wait states (LATENCY)
  515. must be correctly programmed according to the frequency of the CPU clock
  516. (HCLK) and the supply voltage of the device. */
  517. /* Increasing the number of wait states because of higher CPU frequency */
  518. if(FLatency > __HAL_FLASH_GET_LATENCY())
  519. {
  520. /* Program the new number of wait states to the LATENCY bits in the FLASH_ACR register */
  521. __HAL_FLASH_SET_LATENCY(FLatency);
  522. /* Check that the new number of wait states is taken into account to access the Flash
  523. memory by reading the FLASH_ACR register */
  524. if(__HAL_FLASH_GET_LATENCY() != FLatency)
  525. {
  526. return HAL_ERROR;
  527. }
  528. }
  529. /*-------------------------- HCLK Configuration --------------------------*/
  530. if(((RCC_ClkInitStruct->ClockType) & RCC_CLOCKTYPE_HCLK) == RCC_CLOCKTYPE_HCLK)
  531. {
  532. /* Set the highest APBx dividers in order to ensure that we do not go through
  533. a non-spec phase whatever we decrease or increase HCLK. */
  534. if(((RCC_ClkInitStruct->ClockType) & RCC_CLOCKTYPE_PCLK1) == RCC_CLOCKTYPE_PCLK1)
  535. {
  536. MODIFY_REG(RCC->CFGR, RCC_CFGR_PPRE1, RCC_HCLK_DIV16);
  537. }
  538. if(((RCC_ClkInitStruct->ClockType) & RCC_CLOCKTYPE_PCLK2) == RCC_CLOCKTYPE_PCLK2)
  539. {
  540. MODIFY_REG(RCC->CFGR, RCC_CFGR_PPRE2, (RCC_HCLK_DIV16 << 3));
  541. }
  542. assert_param(IS_RCC_HCLK(RCC_ClkInitStruct->AHBCLKDivider));
  543. MODIFY_REG(RCC->CFGR, RCC_CFGR_HPRE, RCC_ClkInitStruct->AHBCLKDivider);
  544. }
  545. /*------------------------- SYSCLK Configuration ---------------------------*/
  546. if(((RCC_ClkInitStruct->ClockType) & RCC_CLOCKTYPE_SYSCLK) == RCC_CLOCKTYPE_SYSCLK)
  547. {
  548. assert_param(IS_RCC_SYSCLKSOURCE(RCC_ClkInitStruct->SYSCLKSource));
  549. /* HSE is selected as System Clock Source */
  550. if(RCC_ClkInitStruct->SYSCLKSource == RCC_SYSCLKSOURCE_HSE)
  551. {
  552. /* Check the HSE ready flag */
  553. if(__HAL_RCC_GET_FLAG(RCC_FLAG_HSERDY) == RESET)
  554. {
  555. return HAL_ERROR;
  556. }
  557. }
  558. /* PLL is selected as System Clock Source */
  559. else if((RCC_ClkInitStruct->SYSCLKSource == RCC_SYSCLKSOURCE_PLLCLK) ||
  560. (RCC_ClkInitStruct->SYSCLKSource == RCC_SYSCLKSOURCE_PLLRCLK))
  561. {
  562. /* Check the PLL ready flag */
  563. if(__HAL_RCC_GET_FLAG(RCC_FLAG_PLLRDY) == RESET)
  564. {
  565. return HAL_ERROR;
  566. }
  567. }
  568. /* HSI is selected as System Clock Source */
  569. else
  570. {
  571. /* Check the HSI ready flag */
  572. if(__HAL_RCC_GET_FLAG(RCC_FLAG_HSIRDY) == RESET)
  573. {
  574. return HAL_ERROR;
  575. }
  576. }
  577. __HAL_RCC_SYSCLK_CONFIG(RCC_ClkInitStruct->SYSCLKSource);
  578. /* Get Start Tick */
  579. tickstart = HAL_GetTick();
  580. while (__HAL_RCC_GET_SYSCLK_SOURCE() != (RCC_ClkInitStruct->SYSCLKSource << RCC_CFGR_SWS_Pos))
  581. {
  582. if ((HAL_GetTick() - tickstart) > CLOCKSWITCH_TIMEOUT_VALUE)
  583. {
  584. return HAL_TIMEOUT;
  585. }
  586. }
  587. }
  588. /* Decreasing the number of wait states because of lower CPU frequency */
  589. if(FLatency < __HAL_FLASH_GET_LATENCY())
  590. {
  591. /* Program the new number of wait states to the LATENCY bits in the FLASH_ACR register */
  592. __HAL_FLASH_SET_LATENCY(FLatency);
  593. /* Check that the new number of wait states is taken into account to access the Flash
  594. memory by reading the FLASH_ACR register */
  595. if(__HAL_FLASH_GET_LATENCY() != FLatency)
  596. {
  597. return HAL_ERROR;
  598. }
  599. }
  600. /*-------------------------- PCLK1 Configuration ---------------------------*/
  601. if(((RCC_ClkInitStruct->ClockType) & RCC_CLOCKTYPE_PCLK1) == RCC_CLOCKTYPE_PCLK1)
  602. {
  603. assert_param(IS_RCC_PCLK(RCC_ClkInitStruct->APB1CLKDivider));
  604. MODIFY_REG(RCC->CFGR, RCC_CFGR_PPRE1, RCC_ClkInitStruct->APB1CLKDivider);
  605. }
  606. /*-------------------------- PCLK2 Configuration ---------------------------*/
  607. if(((RCC_ClkInitStruct->ClockType) & RCC_CLOCKTYPE_PCLK2) == RCC_CLOCKTYPE_PCLK2)
  608. {
  609. assert_param(IS_RCC_PCLK(RCC_ClkInitStruct->APB2CLKDivider));
  610. MODIFY_REG(RCC->CFGR, RCC_CFGR_PPRE2, ((RCC_ClkInitStruct->APB2CLKDivider) << 3U));
  611. }
  612. /* Update the SystemCoreClock global variable */
  613. SystemCoreClock = HAL_RCC_GetSysClockFreq() >> AHBPrescTable[(RCC->CFGR & RCC_CFGR_HPRE)>> RCC_CFGR_HPRE_Pos];
  614. /* Configure the source of time base considering new system clocks settings */
  615. HAL_InitTick (TICK_INT_PRIORITY);
  616. return HAL_OK;
  617. }
  618. /**
  619. * @}
  620. */
  621. /** @defgroup RCC_Exported_Functions_Group2 Peripheral Control functions
  622. * @brief RCC clocks control functions
  623. *
  624. @verbatim
  625. ===============================================================================
  626. ##### Peripheral Control functions #####
  627. ===============================================================================
  628. [..]
  629. This subsection provides a set of functions allowing to control the RCC Clocks
  630. frequencies.
  631. @endverbatim
  632. * @{
  633. */
  634. /**
  635. * @brief Selects the clock source to output on MCO1 pin(PA8) or on MCO2 pin(PC9).
  636. * @note PA8/PC9 should be configured in alternate function mode.
  637. * @param RCC_MCOx specifies the output direction for the clock source.
  638. * This parameter can be one of the following values:
  639. * @arg RCC_MCO1: Clock source to output on MCO1 pin(PA8).
  640. * @arg RCC_MCO2: Clock source to output on MCO2 pin(PC9).
  641. * @param RCC_MCOSource specifies the clock source to output.
  642. * This parameter can be one of the following values:
  643. * @arg RCC_MCO1SOURCE_HSI: HSI clock selected as MCO1 source
  644. * @arg RCC_MCO1SOURCE_LSE: LSE clock selected as MCO1 source
  645. * @arg RCC_MCO1SOURCE_HSE: HSE clock selected as MCO1 source
  646. * @arg RCC_MCO1SOURCE_PLLCLK: main PLL clock selected as MCO1 source
  647. * @arg RCC_MCO2SOURCE_SYSCLK: System clock (SYSCLK) selected as MCO2 source
  648. * @arg RCC_MCO2SOURCE_PLLI2SCLK: PLLI2S clock selected as MCO2 source, available for all STM32F4 devices except STM32F410xx
  649. * @arg RCC_MCO2SOURCE_I2SCLK: I2SCLK clock selected as MCO2 source, available only for STM32F410Rx devices
  650. * @arg RCC_MCO2SOURCE_HSE: HSE clock selected as MCO2 source
  651. * @arg RCC_MCO2SOURCE_PLLCLK: main PLL clock selected as MCO2 source
  652. * @param RCC_MCODiv specifies the MCOx prescaler.
  653. * This parameter can be one of the following values:
  654. * @arg RCC_MCODIV_1: no division applied to MCOx clock
  655. * @arg RCC_MCODIV_2: division by 2 applied to MCOx clock
  656. * @arg RCC_MCODIV_3: division by 3 applied to MCOx clock
  657. * @arg RCC_MCODIV_4: division by 4 applied to MCOx clock
  658. * @arg RCC_MCODIV_5: division by 5 applied to MCOx clock
  659. * @note For STM32F410Rx devices to output I2SCLK clock on MCO2 you should have
  660. * at last one of the SPI clocks enabled (SPI1, SPI2 or SPI5).
  661. * @retval None
  662. */
  663. void HAL_RCC_MCOConfig(uint32_t RCC_MCOx, uint32_t RCC_MCOSource, uint32_t RCC_MCODiv)
  664. {
  665. GPIO_InitTypeDef GPIO_InitStruct;
  666. /* Check the parameters */
  667. assert_param(IS_RCC_MCO(RCC_MCOx));
  668. assert_param(IS_RCC_MCODIV(RCC_MCODiv));
  669. /* RCC_MCO1 */
  670. if(RCC_MCOx == RCC_MCO1)
  671. {
  672. assert_param(IS_RCC_MCO1SOURCE(RCC_MCOSource));
  673. /* MCO1 Clock Enable */
  674. __MCO1_CLK_ENABLE();
  675. /* Configure the MCO1 pin in alternate function mode */
  676. GPIO_InitStruct.Pin = MCO1_PIN;
  677. GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
  678. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
  679. GPIO_InitStruct.Pull = GPIO_NOPULL;
  680. GPIO_InitStruct.Alternate = GPIO_AF0_MCO;
  681. HAL_GPIO_Init(MCO1_GPIO_PORT, &GPIO_InitStruct);
  682. /* Mask MCO1 and MCO1PRE[2:0] bits then Select MCO1 clock source and prescaler */
  683. MODIFY_REG(RCC->CFGR, (RCC_CFGR_MCO1 | RCC_CFGR_MCO1PRE), (RCC_MCOSource | RCC_MCODiv));
  684. /* This RCC MCO1 enable feature is available only on STM32F410xx devices */
  685. #if defined(RCC_CFGR_MCO1EN)
  686. __HAL_RCC_MCO1_ENABLE();
  687. #endif /* RCC_CFGR_MCO1EN */
  688. }
  689. #if defined(RCC_CFGR_MCO2)
  690. else
  691. {
  692. assert_param(IS_RCC_MCO2SOURCE(RCC_MCOSource));
  693. /* MCO2 Clock Enable */
  694. __MCO2_CLK_ENABLE();
  695. /* Configure the MCO2 pin in alternate function mode */
  696. GPIO_InitStruct.Pin = MCO2_PIN;
  697. GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
  698. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
  699. GPIO_InitStruct.Pull = GPIO_NOPULL;
  700. GPIO_InitStruct.Alternate = GPIO_AF0_MCO;
  701. HAL_GPIO_Init(MCO2_GPIO_PORT, &GPIO_InitStruct);
  702. /* Mask MCO2 and MCO2PRE[2:0] bits then Select MCO2 clock source and prescaler */
  703. MODIFY_REG(RCC->CFGR, (RCC_CFGR_MCO2 | RCC_CFGR_MCO2PRE), (RCC_MCOSource | (RCC_MCODiv << 3U)));
  704. /* This RCC MCO2 enable feature is available only on STM32F410Rx devices */
  705. #if defined(RCC_CFGR_MCO2EN)
  706. __HAL_RCC_MCO2_ENABLE();
  707. #endif /* RCC_CFGR_MCO2EN */
  708. }
  709. #endif /* RCC_CFGR_MCO2 */
  710. }
  711. /**
  712. * @brief Enables the Clock Security System.
  713. * @note If a failure is detected on the HSE oscillator clock, this oscillator
  714. * is automatically disabled and an interrupt is generated to inform the
  715. * software about the failure (Clock Security System Interrupt, CSSI),
  716. * allowing the MCU to perform rescue operations. The CSSI is linked to
  717. * the Cortex-M4 NMI (Non-Maskable Interrupt) exception vector.
  718. * @retval None
  719. */
  720. void HAL_RCC_EnableCSS(void)
  721. {
  722. *(__IO uint32_t *) RCC_CR_CSSON_BB = (uint32_t)ENABLE;
  723. }
  724. /**
  725. * @brief Disables the Clock Security System.
  726. * @retval None
  727. */
  728. void HAL_RCC_DisableCSS(void)
  729. {
  730. *(__IO uint32_t *) RCC_CR_CSSON_BB = (uint32_t)DISABLE;
  731. }
  732. /**
  733. * @brief Returns the SYSCLK frequency
  734. *
  735. * @note The system frequency computed by this function is not the real
  736. * frequency in the chip. It is calculated based on the predefined
  737. * constant and the selected clock source:
  738. * @note If SYSCLK source is HSI, function returns values based on HSI_VALUE(*)
  739. * @note If SYSCLK source is HSE, function returns values based on HSE_VALUE(**)
  740. * @note If SYSCLK source is PLL, function returns values based on HSE_VALUE(**)
  741. * or HSI_VALUE(*) multiplied/divided by the PLL factors.
  742. * @note (*) HSI_VALUE is a constant defined in stm32f4xx_hal_conf.h file (default value
  743. * 16 MHz) but the real value may vary depending on the variations
  744. * in voltage and temperature.
  745. * @note (**) HSE_VALUE is a constant defined in stm32f4xx_hal_conf.h file (default value
  746. * 25 MHz), user has to ensure that HSE_VALUE is same as the real
  747. * frequency of the crystal used. Otherwise, this function may
  748. * have wrong result.
  749. *
  750. * @note The result of this function could be not correct when using fractional
  751. * value for HSE crystal.
  752. *
  753. * @note This function can be used by the user application to compute the
  754. * baudrate for the communication peripherals or configure other parameters.
  755. *
  756. * @note Each time SYSCLK changes, this function must be called to update the
  757. * right SYSCLK value. Otherwise, any configuration based on this function will be incorrect.
  758. *
  759. *
  760. * @retval SYSCLK frequency
  761. */
  762. __weak uint32_t HAL_RCC_GetSysClockFreq(void)
  763. {
  764. uint32_t pllm = 0U, pllvco = 0U, pllp = 0U;
  765. uint32_t sysclockfreq = 0U;
  766. /* Get SYSCLK source -------------------------------------------------------*/
  767. switch (RCC->CFGR & RCC_CFGR_SWS)
  768. {
  769. case RCC_CFGR_SWS_HSI: /* HSI used as system clock source */
  770. {
  771. sysclockfreq = HSI_VALUE;
  772. break;
  773. }
  774. case RCC_CFGR_SWS_HSE: /* HSE used as system clock source */
  775. {
  776. sysclockfreq = HSE_VALUE;
  777. break;
  778. }
  779. case RCC_CFGR_SWS_PLL: /* PLL used as system clock source */
  780. {
  781. /* PLL_VCO = (HSE_VALUE or HSI_VALUE / PLLM) * PLLN
  782. SYSCLK = PLL_VCO / PLLP */
  783. pllm = RCC->PLLCFGR & RCC_PLLCFGR_PLLM;
  784. if(__HAL_RCC_GET_PLL_OSCSOURCE() != RCC_PLLSOURCE_HSI)
  785. {
  786. /* HSE used as PLL clock source */
  787. pllvco = (uint32_t) ((((uint64_t) HSE_VALUE * ((uint64_t) ((RCC->PLLCFGR & RCC_PLLCFGR_PLLN) >> RCC_PLLCFGR_PLLN_Pos)))) / (uint64_t)pllm);
  788. }
  789. else
  790. {
  791. /* HSI used as PLL clock source */
  792. pllvco = (uint32_t) ((((uint64_t) HSI_VALUE * ((uint64_t) ((RCC->PLLCFGR & RCC_PLLCFGR_PLLN) >> RCC_PLLCFGR_PLLN_Pos)))) / (uint64_t)pllm);
  793. }
  794. pllp = ((((RCC->PLLCFGR & RCC_PLLCFGR_PLLP) >> RCC_PLLCFGR_PLLP_Pos) + 1U) *2U);
  795. sysclockfreq = pllvco/pllp;
  796. break;
  797. }
  798. default:
  799. {
  800. sysclockfreq = HSI_VALUE;
  801. break;
  802. }
  803. }
  804. return sysclockfreq;
  805. }
  806. /**
  807. * @brief Returns the HCLK frequency
  808. * @note Each time HCLK changes, this function must be called to update the
  809. * right HCLK value. Otherwise, any configuration based on this function will be incorrect.
  810. *
  811. * @note The SystemCoreClock CMSIS variable is used to store System Clock Frequency
  812. * and updated within this function
  813. * @retval HCLK frequency
  814. */
  815. uint32_t HAL_RCC_GetHCLKFreq(void)
  816. {
  817. return SystemCoreClock;
  818. }
  819. /**
  820. * @brief Returns the PCLK1 frequency
  821. * @note Each time PCLK1 changes, this function must be called to update the
  822. * right PCLK1 value. Otherwise, any configuration based on this function will be incorrect.
  823. * @retval PCLK1 frequency
  824. */
  825. uint32_t HAL_RCC_GetPCLK1Freq(void)
  826. {
  827. /* Get HCLK source and Compute PCLK1 frequency ---------------------------*/
  828. return (HAL_RCC_GetHCLKFreq() >> APBPrescTable[(RCC->CFGR & RCC_CFGR_PPRE1)>> RCC_CFGR_PPRE1_Pos]);
  829. }
  830. /**
  831. * @brief Returns the PCLK2 frequency
  832. * @note Each time PCLK2 changes, this function must be called to update the
  833. * right PCLK2 value. Otherwise, any configuration based on this function will be incorrect.
  834. * @retval PCLK2 frequency
  835. */
  836. uint32_t HAL_RCC_GetPCLK2Freq(void)
  837. {
  838. /* Get HCLK source and Compute PCLK2 frequency ---------------------------*/
  839. return (HAL_RCC_GetHCLKFreq()>> APBPrescTable[(RCC->CFGR & RCC_CFGR_PPRE2)>> RCC_CFGR_PPRE2_Pos]);
  840. }
  841. /**
  842. * @brief Configures the RCC_OscInitStruct according to the internal
  843. * RCC configuration registers.
  844. * @param RCC_OscInitStruct pointer to an RCC_OscInitTypeDef structure that
  845. * will be configured.
  846. * @retval None
  847. */
  848. __weak void HAL_RCC_GetOscConfig(RCC_OscInitTypeDef *RCC_OscInitStruct)
  849. {
  850. /* Set all possible values for the Oscillator type parameter ---------------*/
  851. RCC_OscInitStruct->OscillatorType = RCC_OSCILLATORTYPE_HSE | RCC_OSCILLATORTYPE_HSI | RCC_OSCILLATORTYPE_LSE | RCC_OSCILLATORTYPE_LSI;
  852. /* Get the HSE configuration -----------------------------------------------*/
  853. if((RCC->CR &RCC_CR_HSEBYP) == RCC_CR_HSEBYP)
  854. {
  855. RCC_OscInitStruct->HSEState = RCC_HSE_BYPASS;
  856. }
  857. else if((RCC->CR &RCC_CR_HSEON) == RCC_CR_HSEON)
  858. {
  859. RCC_OscInitStruct->HSEState = RCC_HSE_ON;
  860. }
  861. else
  862. {
  863. RCC_OscInitStruct->HSEState = RCC_HSE_OFF;
  864. }
  865. /* Get the HSI configuration -----------------------------------------------*/
  866. if((RCC->CR &RCC_CR_HSION) == RCC_CR_HSION)
  867. {
  868. RCC_OscInitStruct->HSIState = RCC_HSI_ON;
  869. }
  870. else
  871. {
  872. RCC_OscInitStruct->HSIState = RCC_HSI_OFF;
  873. }
  874. RCC_OscInitStruct->HSICalibrationValue = (uint32_t)((RCC->CR &RCC_CR_HSITRIM) >> RCC_CR_HSITRIM_Pos);
  875. /* Get the LSE configuration -----------------------------------------------*/
  876. if((RCC->BDCR &RCC_BDCR_LSEBYP) == RCC_BDCR_LSEBYP)
  877. {
  878. RCC_OscInitStruct->LSEState = RCC_LSE_BYPASS;
  879. }
  880. else if((RCC->BDCR &RCC_BDCR_LSEON) == RCC_BDCR_LSEON)
  881. {
  882. RCC_OscInitStruct->LSEState = RCC_LSE_ON;
  883. }
  884. else
  885. {
  886. RCC_OscInitStruct->LSEState = RCC_LSE_OFF;
  887. }
  888. /* Get the LSI configuration -----------------------------------------------*/
  889. if((RCC->CSR &RCC_CSR_LSION) == RCC_CSR_LSION)
  890. {
  891. RCC_OscInitStruct->LSIState = RCC_LSI_ON;
  892. }
  893. else
  894. {
  895. RCC_OscInitStruct->LSIState = RCC_LSI_OFF;
  896. }
  897. /* Get the PLL configuration -----------------------------------------------*/
  898. if((RCC->CR &RCC_CR_PLLON) == RCC_CR_PLLON)
  899. {
  900. RCC_OscInitStruct->PLL.PLLState = RCC_PLL_ON;
  901. }
  902. else
  903. {
  904. RCC_OscInitStruct->PLL.PLLState = RCC_PLL_OFF;
  905. }
  906. RCC_OscInitStruct->PLL.PLLSource = (uint32_t)(RCC->PLLCFGR & RCC_PLLCFGR_PLLSRC);
  907. RCC_OscInitStruct->PLL.PLLM = (uint32_t)(RCC->PLLCFGR & RCC_PLLCFGR_PLLM);
  908. RCC_OscInitStruct->PLL.PLLN = (uint32_t)((RCC->PLLCFGR & RCC_PLLCFGR_PLLN) >> RCC_PLLCFGR_PLLN_Pos);
  909. RCC_OscInitStruct->PLL.PLLP = (uint32_t)((((RCC->PLLCFGR & RCC_PLLCFGR_PLLP) + RCC_PLLCFGR_PLLP_0) << 1U) >> RCC_PLLCFGR_PLLP_Pos);
  910. RCC_OscInitStruct->PLL.PLLQ = (uint32_t)((RCC->PLLCFGR & RCC_PLLCFGR_PLLQ) >> RCC_PLLCFGR_PLLQ_Pos);
  911. }
  912. /**
  913. * @brief Configures the RCC_ClkInitStruct according to the internal
  914. * RCC configuration registers.
  915. * @param RCC_ClkInitStruct pointer to an RCC_ClkInitTypeDef structure that
  916. * will be configured.
  917. * @param pFLatency Pointer on the Flash Latency.
  918. * @retval None
  919. */
  920. void HAL_RCC_GetClockConfig(RCC_ClkInitTypeDef *RCC_ClkInitStruct, uint32_t *pFLatency)
  921. {
  922. /* Set all possible values for the Clock type parameter --------------------*/
  923. RCC_ClkInitStruct->ClockType = RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2;
  924. /* Get the SYSCLK configuration --------------------------------------------*/
  925. RCC_ClkInitStruct->SYSCLKSource = (uint32_t)(RCC->CFGR & RCC_CFGR_SW);
  926. /* Get the HCLK configuration ----------------------------------------------*/
  927. RCC_ClkInitStruct->AHBCLKDivider = (uint32_t)(RCC->CFGR & RCC_CFGR_HPRE);
  928. /* Get the APB1 configuration ----------------------------------------------*/
  929. RCC_ClkInitStruct->APB1CLKDivider = (uint32_t)(RCC->CFGR & RCC_CFGR_PPRE1);
  930. /* Get the APB2 configuration ----------------------------------------------*/
  931. RCC_ClkInitStruct->APB2CLKDivider = (uint32_t)((RCC->CFGR & RCC_CFGR_PPRE2) >> 3U);
  932. /* Get the Flash Wait State (Latency) configuration ------------------------*/
  933. *pFLatency = (uint32_t)(FLASH->ACR & FLASH_ACR_LATENCY);
  934. }
  935. /**
  936. * @brief This function handles the RCC CSS interrupt request.
  937. * @note This API should be called under the NMI_Handler().
  938. * @retval None
  939. */
  940. void HAL_RCC_NMI_IRQHandler(void)
  941. {
  942. /* Check RCC CSSF flag */
  943. if(__HAL_RCC_GET_IT(RCC_IT_CSS))
  944. {
  945. /* RCC Clock Security System interrupt user callback */
  946. HAL_RCC_CSSCallback();
  947. /* Clear RCC CSS pending bit */
  948. __HAL_RCC_CLEAR_IT(RCC_IT_CSS);
  949. }
  950. }
  951. /**
  952. * @brief RCC Clock Security System interrupt callback
  953. * @retval None
  954. */
  955. __weak void HAL_RCC_CSSCallback(void)
  956. {
  957. /* NOTE : This function Should not be modified, when the callback is needed,
  958. the HAL_RCC_CSSCallback could be implemented in the user file
  959. */
  960. }
  961. /**
  962. * @}
  963. */
  964. /**
  965. * @}
  966. */
  967. #endif /* HAL_RCC_MODULE_ENABLED */
  968. /**
  969. * @}
  970. */
  971. /**
  972. * @}
  973. */
  974. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/