stm32h7xx_hal_rcc.c 49 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358
  1. /**
  2. ******************************************************************************
  3. * @file stm32h7xx_hal_rcc.c
  4. * @author MCD Application Team
  5. * @version V1.0.0
  6. * @date 21-April-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 64MHz) with Flash 0 wait state,and all peripherals are off except
  20. internal SRAM, Flash, JTAG and PWR
  21. (+) There is no pre-scaler 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 analogue mode , except the JTAG pins which
  25. are assigned to be used for debug purpose.
  26. [..]
  27. Once the device started from reset, the user application has to:
  28. (+) Configure the clock source to be used to drive the System clock
  29. (if the application needs higher frequency/performance)
  30. (+) Configure the System clock frequency and Flash settings
  31. (+) Configure the AHB and APB buses pre-scalers
  32. (+) Enable the clock for the peripheral(s) to be used
  33. (+) Configure the clock kernel source(s) for peripherals which clocks are not
  34. derived from the System clock through :RCC_D1CCIPR,RCC_D2CCIP1R,RCC_D2CCIP2R
  35. and RCC_D3CCIPR registers
  36. ##### RCC Limitations #####
  37. ==============================================================================
  38. [..]
  39. A delay between an RCC peripheral clock enable and the effective peripheral
  40. enabling should be taken into account in order to manage the peripheral read/write
  41. from/to registers.
  42. (+) This delay depends on the peripheral mapping.
  43. (+) If peripheral is mapped on AHB: the delay is 2 AHB clock cycle
  44. after the clock enable bit is set on the hardware register
  45. (+) If peripheral is mapped on APB: the delay is 2 APB clock cycle
  46. after the clock enable bit is set on the hardware register
  47. [..]
  48. Implemented Workaround:
  49. (+) For AHB & APB peripherals, a dummy read to the peripheral register has been
  50. inserted in each __HAL_RCC_PPP_CLK_ENABLE() macro.
  51. @endverbatim
  52. ******************************************************************************
  53. * @attention
  54. *
  55. * <h2><center>&copy; COPYRIGHT(c) 2017 STMicroelectronics</center></h2>
  56. *
  57. * Redistribution and use in source and binary forms, with or without modification,
  58. * are permitted provided that the following conditions are met:
  59. * 1. Redistributions of source code must retain the above copyright notice,
  60. * this list of conditions and the following disclaimer.
  61. * 2. Redistributions in binary form must reproduce the above copyright notice,
  62. * this list of conditions and the following disclaimer in the documentation
  63. * and/or other materials provided with the distribution.
  64. * 3. Neither the name of STMicroelectronics nor the names of its contributors
  65. * may be used to endorse or promote products derived from this software
  66. * without specific prior written permission.
  67. *
  68. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  69. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  70. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  71. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  72. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  73. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  74. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  75. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  76. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  77. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  78. *
  79. ******************************************************************************
  80. */
  81. /* Includes ------------------------------------------------------------------*/
  82. #include "stm32h7xx_hal.h"
  83. /** @addtogroup STM32H7xx_HAL_Driver
  84. * @{
  85. */
  86. /** @defgroup RCC RCC
  87. * @brief RCC HAL module driver
  88. * @{
  89. */
  90. #ifdef HAL_RCC_MODULE_ENABLED
  91. /* Private typedef -----------------------------------------------------------*/
  92. /* Private define ------------------------------------------------------------*/
  93. /* Private macro -------------------------------------------------------------*/
  94. /** @defgroup RCC_Private_Macros RCC Private Macros
  95. * @{
  96. */
  97. #define __MCO1_CLK_ENABLE() __HAL_RCC_GPIOA_CLK_ENABLE()
  98. #define MCO1_GPIO_PORT GPIOA
  99. #define MCO1_PIN GPIO_PIN_8
  100. #define __MCO2_CLK_ENABLE() __HAL_RCC_GPIOC_CLK_ENABLE()
  101. #define MCO2_GPIO_PORT GPIOC
  102. #define MCO2_PIN GPIO_PIN_9
  103. /**
  104. * @}
  105. */
  106. /* Private variables ---------------------------------------------------------*/
  107. /** @defgroup RCC_Private_Variables RCC Private Variables
  108. * @{
  109. */
  110. /**
  111. * @}
  112. */
  113. /* Private function prototypes -----------------------------------------------*/
  114. /* Exported functions --------------------------------------------------------*/
  115. /** @defgroup RCC_Exported_Functions RCC Exported Functions
  116. * @{
  117. */
  118. /** @defgroup RCC_Exported_Functions_Group1 Initialization and de-initialization functions
  119. * @brief Initialization and Configuration functions
  120. *
  121. @verbatim
  122. ===============================================================================
  123. ##### Initialization and de-initialization functions #####
  124. ===============================================================================
  125. [..]
  126. This section provides functions allowing to configure the internal/external oscillators
  127. (HSE, HSI, LSE,CSI, LSI,HSI48, PLL, CSS and MCO) and the System buses clocks (SYSCLK, AHB3, AHB1
  128. AHB2,AHB4,APB3, APB1L, APB1H, APB2, and APB4).
  129. [..] Internal/external clock and PLL configuration
  130. (#) HSI (high-speed internal), 64 MHz factory-trimmed RC used directly or through
  131. the PLL as System clock source.
  132. (#) CSI is a low-power RC oscillator which can be used directly as system clock, peripheral
  133. clock, or PLL input.But even with frequency calibration, is less accurate than an
  134. external crystal oscillator or ceramic resonator.
  135. (#) LSI (low-speed internal), 32 KHz low consumption RC used as IWDG and/or RTC
  136. clock source.
  137. (#) HSE (high-speed external), 4 to 48 MHz crystal oscillator used directly or
  138. through the PLL as System clock source. Can be used also as RTC clock source.
  139. (#) LSE (low-speed external), 32 KHz oscillator used as RTC clock source.
  140. (#) PLL , The RCC features three independent PLLs (clocked by HSI , HSE or CSI),
  141. featuring three different output clocks and able to work either in integer or Fractional mode.
  142. (++) A main PLL, PLL1, which is generally used to provide clocks to the CPU
  143. and to some peripherals.
  144. (++) Two dedicated PLLs, PLL2 and PLL3, which are used to generate the kernel clock for peripherals.
  145. (#) CSS (Clock security system), once enabled and if a HSE clock failure occurs
  146. (HSE used directly or through PLL as System clock source), the System clock
  147. is automatically switched to HSI and an interrupt is generated if enabled.
  148. The interrupt is linked to the Cortex-M NMI (Non-Mask-able Interrupt)
  149. exception vector.
  150. (#) MCO1 (micro controller clock output), used to output HSI, LSE, HSE, PLL1(PLL1_Q)
  151. or HSI48 clock (through a configurable pre-scaler) on PA8 pin.
  152. (#) MCO2 (micro controller clock output), used to output HSE, PLL2(PLL2_P), SYSCLK,
  153. LSI, CSI, or PLL1(PLL1_P) clock (through a configurable pre-scaler) on PC9 pin.
  154. [..] System, AHB and APB buses clocks configuration
  155. (#) Several clock sources can be used to drive the System clock (SYSCLK): CSI,HSI,
  156. HSE and PLL.
  157. The AHB clock (HCLK) is derived from System core clock through configurable
  158. pre-scaler and used to clock the CPU, memory and peripherals mapped
  159. on AHB and APB bus of the 3 Domains (D1, D2, D3) through configurable pre-scalers
  160. and used to clock the peripherals mapped on these buses. You can use
  161. "HAL_RCC_GetSysClockFreq()" function to retrieve system clock frequency.
  162. -@- All the peripheral clocks are derived from the System clock (SYSCLK) except those
  163. with dual clock domain where kernel source clock could be selected through
  164. RCC_D1CCIPR,RCC_D2CCIP1R,RCC_D2CCIP2R and RCC_D3CCIPR registers.
  165. @endverbatim
  166. * @{
  167. */
  168. /**
  169. * @brief Resets the RCC clock configuration to the default reset state.
  170. * @note The default reset state of the clock configuration is given below:
  171. * - HSI ON and used as system clock source
  172. * - HSE, PLL1, PLL2 and PLL3 OFF
  173. * - AHB, APB Bus pre-scaler set to 1.
  174. * - CSS, MCO1 and MCO2 OFF
  175. * - All interrupts disabled
  176. * @note This function doesn't modify the configuration of the
  177. * - Peripheral clocks
  178. * - LSI, LSE and RTC clocks
  179. * @retval None
  180. */
  181. void HAL_RCC_DeInit(void)
  182. {
  183. /* Set HSION bit */
  184. SET_BIT(RCC->CR, RCC_CR_HSION);
  185. /* Reset CFGR register */
  186. CLEAR_REG(RCC->CFGR);
  187. /* Reset CSION , CSIKERON, HSEON, HSI48ON, HSECSSON,HSIDIV, PLL1ON, PLL2ON, PLL3ON bits */
  188. CLEAR_BIT(RCC->CR, RCC_CR_HSEON | RCC_CR_HSIKERON| RCC_CR_HSIDIV| RCC_CR_HSIDIVF| RCC_CR_CSION | RCC_CR_CSIKERON | RCC_CR_HSI48ON \
  189. |RCC_CR_CSSHSEON | RCC_CR_PLL1ON | RCC_CR_PLL1ON | RCC_CR_PLL2ON | RCC_CR_PLL3ON);
  190. /* Reset D1CFGR register */
  191. CLEAR_REG(RCC->D1CFGR);
  192. /* Reset D2CFGR register */
  193. CLEAR_REG(RCC->D2CFGR);
  194. /* Reset D3CFGR register */
  195. CLEAR_REG(RCC->D3CFGR);
  196. /* Reset PLLCKSELR register */
  197. CLEAR_REG(RCC->PLLCKSELR);
  198. /* Reset PLLCFGR register */
  199. CLEAR_REG(RCC->PLLCFGR);
  200. /* Reset PLL1DIVR register */
  201. CLEAR_REG(RCC->PLL1DIVR);
  202. /* Reset PLL1FRACR register */
  203. CLEAR_REG(RCC->PLL1FRACR);
  204. /* Reset PLL2DIVR register */
  205. CLEAR_REG(RCC->PLL2DIVR);
  206. /* Reset PLL2FRACR register */
  207. CLEAR_REG(RCC->PLL2FRACR);
  208. /* Reset PLL3DIVR register */
  209. CLEAR_REG(RCC->PLL3DIVR);
  210. /* Reset PLL3FRACR register */
  211. CLEAR_REG(RCC->PLL3FRACR);
  212. /* Reset HSEBYP bit */
  213. CLEAR_BIT(RCC->CR, RCC_CR_HSEBYP);
  214. /* Disable all interrupts */
  215. CLEAR_REG(RCC->CICR);
  216. }
  217. /**
  218. * @brief Initializes the RCC Oscillators according to the specified parameters in the
  219. * RCC_OscInitTypeDef.
  220. * @param RCC_OscInitStruct: pointer to an RCC_OscInitTypeDef structure that
  221. * contains the configuration information for the RCC Oscillators.
  222. * @note The PLL is not disabled when used as system clock.
  223. * @note Transitions LSE Bypass to LSE On and LSE On to LSE Bypass are not
  224. * supported by this function. User should request a transition to LSE Off
  225. * first and then LSE On or LSE Bypass.
  226. * @note Transition HSE Bypass to HSE On and HSE On to HSE Bypass are not
  227. * supported by this function. User should request a transition to HSE Off
  228. * first and then HSE On or HSE Bypass.
  229. * @retval HAL status
  230. */
  231. __weak HAL_StatusTypeDef HAL_RCC_OscConfig(RCC_OscInitTypeDef *RCC_OscInitStruct)
  232. {
  233. uint32_t tickstart = 0;
  234. /* Check the parameters */
  235. assert_param(IS_RCC_OSCILLATORTYPE(RCC_OscInitStruct->OscillatorType));
  236. /*------------------------------- HSE Configuration ------------------------*/
  237. if(((RCC_OscInitStruct->OscillatorType) & RCC_OSCILLATORTYPE_HSE) == RCC_OSCILLATORTYPE_HSE)
  238. {
  239. /* Check the parameters */
  240. assert_param(IS_RCC_HSE(RCC_OscInitStruct->HSEState));
  241. /* When the HSE is used as system clock or clock source for PLL in these cases HSE will not disabled */
  242. if((__HAL_RCC_GET_SYSCLK_SOURCE() == RCC_CFGR_SWS_HSE) || ((__HAL_RCC_GET_SYSCLK_SOURCE() == RCC_CFGR_SWS_PLL1) && ((RCC->PLLCKSELR & RCC_PLLCKSELR_PLLSRC) == RCC_PLLCKSELR_PLLSRC_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((int32_t) (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 bypassed or disabled */
  272. while(__HAL_RCC_GET_FLAG(RCC_FLAG_HSERDY) != RESET)
  273. {
  274. if((int32_t) (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. /* When the HSI is used as system clock it will not disabled */
  289. if((__HAL_RCC_GET_SYSCLK_SOURCE() == RCC_CFGR_SWS_HSI) || ((__HAL_RCC_GET_SYSCLK_SOURCE() == RCC_CFGR_SWS_PLL1) && ((RCC->PLLCKSELR & RCC_PLLCKSELR_PLLSRC) == RCC_PLLCKSELR_PLLSRC_HSI)))
  290. {
  291. /* When HSI is used as system clock it will not disabled */
  292. if((__HAL_RCC_GET_FLAG(RCC_FLAG_HSIRDY) != RESET) && (RCC_OscInitStruct->HSIState == RCC_HSI_OFF))
  293. {
  294. return HAL_ERROR;
  295. }
  296. /* Otherwise, just the calibration is allowed */
  297. else
  298. {
  299. /* Enable the Internal High Speed oscillator (HSI, HSIDIV2,HSIDIV4, or HSIDIV8) */
  300. __HAL_RCC_HSI_CONFIG(RCC_OscInitStruct->HSIState);
  301. /* Get Start Tick*/
  302. tickstart = HAL_GetTick();
  303. /* Wait till HSI is ready */
  304. while(__HAL_RCC_GET_FLAG(RCC_FLAG_HSIRDY) == RESET)
  305. {
  306. if((int32_t) (HAL_GetTick() - tickstart ) > HSI_TIMEOUT_VALUE)
  307. {
  308. return HAL_TIMEOUT;
  309. }
  310. }
  311. /* Adjusts the Internal High Speed oscillator (HSI) calibration value.*/
  312. __HAL_RCC_HSI_CALIBRATIONVALUE_ADJUST(RCC_OscInitStruct->HSICalibrationValue);
  313. }
  314. }
  315. else
  316. {
  317. /* Check the HSI State */
  318. if((RCC_OscInitStruct->HSIState)!= RCC_HSI_OFF)
  319. {
  320. /* Enable the Internal High Speed oscillator (HSI, HSIDIV2,HSIDIV4, or HSIDIV8) */
  321. __HAL_RCC_HSI_CONFIG(RCC_OscInitStruct->HSIState);
  322. /* Get Start Tick*/
  323. tickstart = HAL_GetTick();
  324. /* Wait till HSI is ready */
  325. while(__HAL_RCC_GET_FLAG(RCC_FLAG_HSIRDY) == RESET)
  326. {
  327. if((int32_t) (HAL_GetTick() - tickstart ) > HSI_TIMEOUT_VALUE)
  328. {
  329. return HAL_TIMEOUT;
  330. }
  331. }
  332. /* Adjusts the Internal High Speed oscillator (HSI) calibration value.*/
  333. __HAL_RCC_HSI_CALIBRATIONVALUE_ADJUST(RCC_OscInitStruct->HSICalibrationValue);
  334. }
  335. else
  336. {
  337. /* Disable the Internal High Speed oscillator (HSI). */
  338. __HAL_RCC_HSI_DISABLE();
  339. /* Get Start Tick*/
  340. tickstart = HAL_GetTick();
  341. /* Wait till HSI is ready */
  342. while(__HAL_RCC_GET_FLAG(RCC_FLAG_HSIRDY) != RESET)
  343. {
  344. if((int32_t) (HAL_GetTick() - tickstart ) > HSI_TIMEOUT_VALUE)
  345. {
  346. return HAL_TIMEOUT;
  347. }
  348. }
  349. }
  350. }
  351. }
  352. /*----------------------------- CSI Configuration --------------------------*/
  353. if(((RCC_OscInitStruct->OscillatorType) & RCC_OSCILLATORTYPE_CSI) == RCC_OSCILLATORTYPE_CSI)
  354. {
  355. /* Check the parameters */
  356. assert_param(IS_RCC_CSI(RCC_OscInitStruct->CSIState));
  357. assert_param(IS_RCC_CALIBRATION_VALUE(RCC_OscInitStruct->CSICalibrationValue));
  358. /* When the CSI is used as system clock it will not disabled */
  359. if((__HAL_RCC_GET_SYSCLK_SOURCE() == RCC_CFGR_SWS_CSI) || ((__HAL_RCC_GET_SYSCLK_SOURCE() == RCC_CFGR_SWS_PLL1) && ((RCC->PLLCKSELR & RCC_PLLCKSELR_PLLSRC) == RCC_PLLCKSELR_PLLSRC_CSI)))
  360. {
  361. /* When CSI is used as system clock it will not disabled */
  362. if((__HAL_RCC_GET_FLAG(RCC_FLAG_CSIRDY) != RESET) && (RCC_OscInitStruct->CSIState != RCC_CSI_ON))
  363. {
  364. return HAL_ERROR;
  365. }
  366. /* Otherwise, just the calibration is allowed */
  367. else
  368. {
  369. /* Adjusts the Internal High Speed oscillator (CSI) calibration value.*/
  370. __HAL_RCC_CSI_CALIBRATIONVALUE_ADJUST(RCC_OscInitStruct->CSICalibrationValue);
  371. }
  372. }
  373. else
  374. {
  375. /* Check the CSI State */
  376. if((RCC_OscInitStruct->CSIState)!= RCC_CSI_OFF)
  377. {
  378. /* Enable the Internal High Speed oscillator (CSI). */
  379. __HAL_RCC_CSI_ENABLE();
  380. /* Get Start Tick*/
  381. tickstart = HAL_GetTick();
  382. /* Wait till CSI is ready */
  383. while(__HAL_RCC_GET_FLAG(RCC_FLAG_CSIRDY) == RESET)
  384. {
  385. if((int32_t) (HAL_GetTick() - tickstart ) > CSI_TIMEOUT_VALUE)
  386. {
  387. return HAL_TIMEOUT;
  388. }
  389. }
  390. /* Adjusts the Internal High Speed oscillator (CSI) calibration value.*/
  391. __HAL_RCC_CSI_CALIBRATIONVALUE_ADJUST(RCC_OscInitStruct->CSICalibrationValue);
  392. }
  393. else
  394. {
  395. /* Disable the Internal High Speed oscillator (CSI). */
  396. __HAL_RCC_CSI_DISABLE();
  397. /* Get Start Tick*/
  398. tickstart = HAL_GetTick();
  399. /* Wait till CSI is ready */
  400. while(__HAL_RCC_GET_FLAG(RCC_FLAG_CSIRDY) != RESET)
  401. {
  402. if((int32_t) (HAL_GetTick() - tickstart ) > CSI_TIMEOUT_VALUE)
  403. {
  404. return HAL_TIMEOUT;
  405. }
  406. }
  407. }
  408. }
  409. }
  410. /*------------------------------ LSI Configuration -------------------------*/
  411. if(((RCC_OscInitStruct->OscillatorType) & RCC_OSCILLATORTYPE_LSI) == RCC_OSCILLATORTYPE_LSI)
  412. {
  413. /* Check the parameters */
  414. assert_param(IS_RCC_LSI(RCC_OscInitStruct->LSIState));
  415. /* Check the LSI State */
  416. if((RCC_OscInitStruct->LSIState)!= RCC_LSI_OFF)
  417. {
  418. /* Enable the Internal Low Speed oscillator (LSI). */
  419. __HAL_RCC_LSI_ENABLE();
  420. /* Get Start Tick*/
  421. tickstart = HAL_GetTick();
  422. /* Wait till LSI is ready */
  423. while(__HAL_RCC_GET_FLAG(RCC_FLAG_LSIRDY) == RESET)
  424. {
  425. if((int32_t) (HAL_GetTick() - tickstart ) > LSI_TIMEOUT_VALUE)
  426. {
  427. return HAL_TIMEOUT;
  428. }
  429. }
  430. }
  431. else
  432. {
  433. /* Disable the Internal Low Speed oscillator (LSI). */
  434. __HAL_RCC_LSI_DISABLE();
  435. /* Get Start Tick*/
  436. tickstart = HAL_GetTick();
  437. /* Wait till LSI is ready */
  438. while(__HAL_RCC_GET_FLAG(RCC_FLAG_LSIRDY) != RESET)
  439. {
  440. if((int32_t) (HAL_GetTick() - tickstart ) > LSI_TIMEOUT_VALUE)
  441. {
  442. return HAL_TIMEOUT;
  443. }
  444. }
  445. }
  446. }
  447. /*------------------------------ HSI48 Configuration -------------------------*/
  448. if(((RCC_OscInitStruct->OscillatorType) & RCC_OSCILLATORTYPE_HSI48) == RCC_OSCILLATORTYPE_HSI48)
  449. {
  450. /* Check the parameters */
  451. assert_param(IS_RCC_HSI48(RCC_OscInitStruct->HSI48State));
  452. /* Check the HSI48 State */
  453. if((RCC_OscInitStruct->HSI48State)!= RCC_HSI48_OFF)
  454. {
  455. /* Enable the Internal Low Speed oscillator (HSI48). */
  456. __HAL_RCC_HSI48_ENABLE();
  457. /* Get time-out */
  458. tickstart = HAL_GetTick();
  459. /* Wait till HSI48 is ready */
  460. while(__HAL_RCC_GET_FLAG(RCC_FLAG_HSI48RDY) == RESET)
  461. {
  462. if((HAL_GetTick() - tickstart ) > HSI48_TIMEOUT_VALUE)
  463. {
  464. return HAL_TIMEOUT;
  465. }
  466. }
  467. }
  468. else
  469. {
  470. /* Disable the Internal Low Speed oscillator (HSI48). */
  471. __HAL_RCC_HSI48_DISABLE();
  472. /* Get time-out */
  473. tickstart = HAL_GetTick();
  474. /* Wait till HSI48 is ready */
  475. while(__HAL_RCC_GET_FLAG(RCC_FLAG_HSI48RDY) != RESET)
  476. {
  477. if((HAL_GetTick() - tickstart ) > HSI48_TIMEOUT_VALUE)
  478. {
  479. return HAL_TIMEOUT;
  480. }
  481. }
  482. }
  483. }
  484. /*------------------------------ LSE Configuration -------------------------*/
  485. if(((RCC_OscInitStruct->OscillatorType) & RCC_OSCILLATORTYPE_LSE) == RCC_OSCILLATORTYPE_LSE)
  486. {
  487. /* Check the parameters */
  488. assert_param(IS_RCC_LSE(RCC_OscInitStruct->LSEState));
  489. /* Enable write access to Backup domain */
  490. PWR->CR1 |= PWR_CR1_DBP;
  491. /* Wait for Backup domain Write protection disable */
  492. tickstart = HAL_GetTick();
  493. while((PWR->CR1 & PWR_CR1_DBP) == RESET)
  494. {
  495. if((int32_t) (HAL_GetTick() - tickstart ) > RCC_DBP_TIMEOUT_VALUE)
  496. {
  497. return HAL_TIMEOUT;
  498. }
  499. }
  500. /* Set the new LSE configuration -----------------------------------------*/
  501. __HAL_RCC_LSE_CONFIG(RCC_OscInitStruct->LSEState);
  502. /* Check the LSE State */
  503. if((RCC_OscInitStruct->LSEState) == RCC_LSE_ON)
  504. {
  505. /* Get Start Tick*/
  506. tickstart = HAL_GetTick();
  507. /* Wait till LSE is ready */
  508. while(__HAL_RCC_GET_FLAG(RCC_FLAG_LSERDY) == RESET)
  509. {
  510. if((int32_t) (HAL_GetTick() - tickstart ) > RCC_LSE_TIMEOUT_VALUE)
  511. {
  512. return HAL_TIMEOUT;
  513. }
  514. }
  515. }
  516. else
  517. {
  518. /* Get Start Tick*/
  519. tickstart = HAL_GetTick();
  520. /* Wait till LSE is ready */
  521. while(__HAL_RCC_GET_FLAG(RCC_FLAG_LSERDY) != RESET)
  522. {
  523. if((int32_t) (HAL_GetTick() - tickstart ) > RCC_LSE_TIMEOUT_VALUE)
  524. {
  525. return HAL_TIMEOUT;
  526. }
  527. }
  528. }
  529. }
  530. /*-------------------------------- PLL Configuration -----------------------*/
  531. /* Check the parameters */
  532. assert_param(IS_RCC_PLL(RCC_OscInitStruct->PLL.PLLState));
  533. if ((RCC_OscInitStruct->PLL.PLLState) != RCC_PLL_NONE)
  534. {
  535. /* Check if the PLL is used as system clock or not */
  536. if(__HAL_RCC_GET_SYSCLK_SOURCE() != RCC_CFGR_SWS_PLL1)
  537. {
  538. if((RCC_OscInitStruct->PLL.PLLState) == RCC_PLL_ON)
  539. {
  540. /* Check the parameters */
  541. assert_param(IS_RCC_PLLSOURCE(RCC_OscInitStruct->PLL.PLLSource));
  542. assert_param(IS_RCC_PLLM_VALUE(RCC_OscInitStruct->PLL.PLLM));
  543. assert_param(IS_RCC_PLLN_VALUE(RCC_OscInitStruct->PLL.PLLN));
  544. assert_param(IS_RCC_PLLP_VALUE(RCC_OscInitStruct->PLL.PLLP));
  545. assert_param(IS_RCC_PLLQ_VALUE(RCC_OscInitStruct->PLL.PLLQ));
  546. assert_param(IS_RCC_PLLQ_VALUE(RCC_OscInitStruct->PLL.PLLR));
  547. /* Disable the main PLL. */
  548. __HAL_RCC_PLL_DISABLE();
  549. /* Get Start Tick*/
  550. tickstart = HAL_GetTick();
  551. /* Wait till PLL is ready */
  552. while(__HAL_RCC_GET_FLAG(RCC_FLAG_PLLRDY) != RESET)
  553. {
  554. if((int32_t) (HAL_GetTick() - tickstart ) > PLL_TIMEOUT_VALUE)
  555. {
  556. return HAL_TIMEOUT;
  557. }
  558. }
  559. /* Configure the main PLL clock source, multiplication and division factors. */
  560. __HAL_RCC_PLL_CONFIG(RCC_OscInitStruct->PLL.PLLSource,
  561. RCC_OscInitStruct->PLL.PLLM,
  562. RCC_OscInitStruct->PLL.PLLN,
  563. RCC_OscInitStruct->PLL.PLLP,
  564. RCC_OscInitStruct->PLL.PLLQ,
  565. RCC_OscInitStruct->PLL.PLLR);
  566. /* Configure PLL PLL1FRACN */
  567. __HAL_RCC_PLLFRACN_CONFIG(RCC_OscInitStruct->PLL.PLLFRACN);
  568. /* Select PLL1 input reference frequency range: VCI */
  569. __HAL_RCC_PLL_VCIRANGE(RCC_OscInitStruct->PLL.PLLRGE) ;
  570. /* Select PLL1 output frequency range : VCO */
  571. __HAL_RCC_PLL_VCORANGE(RCC_OscInitStruct->PLL.PLLVCOSEL) ;
  572. /* Enable PLL System Clock output. */
  573. __HAL_RCC_PLLCLKOUT_ENABLE(RCC_PLL1_DIVP);
  574. /* Enable PLL1Q Clock output. */
  575. __HAL_RCC_PLLCLKOUT_ENABLE(RCC_PLL1_DIVQ);
  576. /* Enable PLL1R Clock output. */
  577. __HAL_RCC_PLLCLKOUT_ENABLE(RCC_PLL1_DIVR);
  578. /* Enable PLL1FRACN . */
  579. __HAL_RCC_PLLFRACN_ENABLE();
  580. /* Enable the main PLL. */
  581. __HAL_RCC_PLL_ENABLE();
  582. /* Get Start Tick*/
  583. tickstart = HAL_GetTick();
  584. /* Wait till PLL is ready */
  585. while(__HAL_RCC_GET_FLAG(RCC_FLAG_PLLRDY) == RESET)
  586. {
  587. if((int32_t) (HAL_GetTick() - tickstart ) > PLL_TIMEOUT_VALUE)
  588. {
  589. return HAL_TIMEOUT;
  590. }
  591. }
  592. }
  593. else
  594. {
  595. /* Disable the main PLL. */
  596. __HAL_RCC_PLL_DISABLE();
  597. /* Get Start Tick*/
  598. tickstart = HAL_GetTick();
  599. /* Wait till PLL is ready */
  600. while(__HAL_RCC_GET_FLAG(RCC_FLAG_PLLRDY) != RESET)
  601. {
  602. if((int32_t) (HAL_GetTick() - tickstart ) > PLL_TIMEOUT_VALUE)
  603. {
  604. return HAL_TIMEOUT;
  605. }
  606. }
  607. }
  608. }
  609. else
  610. {
  611. return HAL_ERROR;
  612. }
  613. }
  614. return HAL_OK;
  615. }
  616. /**
  617. * @brief Initializes the CPU, AHB and APB buses clocks according to the specified
  618. * parameters in the RCC_ClkInitStruct.
  619. * @param RCC_ClkInitStruct: pointer to an RCC_OscInitTypeDef structure that
  620. * contains the configuration information for the RCC peripheral.
  621. * @param FLatency: FLASH Latency, this parameter depend on device selected
  622. *
  623. * @note The SystemCoreClock CMSIS variable is used to store System Clock Frequency
  624. * and updated by HAL_InitTick() function called within this function
  625. *
  626. * @note The HSI is used (enabled by hardware) as system clock source after
  627. * start-up from Reset, wake-up from STOP and STANDBY mode, or in case
  628. * of failure of the HSE used directly or indirectly as system clock
  629. * (if the Clock Security System CSS is enabled).
  630. *
  631. * @note A switch from one clock source to another occurs only if the target
  632. * clock source is ready (clock stable after start-up delay or PLL locked).
  633. * If a clock source which is not yet ready is selected, the switch will
  634. * occur when the clock source will be ready.
  635. * You can use HAL_RCC_GetClockConfig() function to know which clock is
  636. * currently used as system clock source.
  637. * @note Depending on the device voltage range, the software has to set correctly
  638. * D1CPRE[3:0] bits to ensure that Domain1 core clock not exceed the maximum allowed frequency
  639. * (for more details refer to section above "Initialization/de-initialization functions")
  640. * @retval None
  641. */
  642. HAL_StatusTypeDef HAL_RCC_ClockConfig(RCC_ClkInitTypeDef *RCC_ClkInitStruct, uint32_t FLatency)
  643. {
  644. uint32_t tickstart = 0;
  645. /* Check the parameters */
  646. assert_param(IS_RCC_CLOCKTYPE(RCC_ClkInitStruct->ClockType));
  647. assert_param(IS_FLASH_LATENCY(FLatency));
  648. /* To correctly read data from FLASH memory, the number of wait states (LATENCY)
  649. must be correctly programmed according to the frequency of the CPU clock
  650. (HCLK) and the supply voltage of the device. */
  651. /* Increasing the CPU frequency */
  652. if(FLatency > (FLASH->ACR & FLASH_ACR_LATENCY))
  653. {
  654. /* Program the new number of wait states to the LATENCY bits in the FLASH_ACR register */
  655. __HAL_FLASH_SET_LATENCY(FLatency);
  656. /* Check that the new number of wait states is taken into account to access the Flash
  657. memory by reading the FLASH_ACR register */
  658. if((FLASH->ACR & FLASH_ACR_LATENCY) != FLatency)
  659. {
  660. return HAL_ERROR;
  661. }
  662. }
  663. /*-------------------------- HCLK Configuration --------------------------*/
  664. if(((RCC_ClkInitStruct->ClockType) & RCC_CLOCKTYPE_HCLK) == RCC_CLOCKTYPE_HCLK)
  665. {
  666. assert_param(IS_RCC_HCLK(RCC_ClkInitStruct->AHBCLKDivider));
  667. MODIFY_REG(RCC->D1CFGR, RCC_D1CFGR_HPRE, RCC_ClkInitStruct->AHBCLKDivider);
  668. }
  669. /*------------------------- SYSCLK Configuration -------------------------*/
  670. if(((RCC_ClkInitStruct->ClockType) & RCC_CLOCKTYPE_SYSCLK) == RCC_CLOCKTYPE_SYSCLK)
  671. {
  672. assert_param(IS_RCC_SYSCLK(RCC_ClkInitStruct->SYSCLKDivider));
  673. assert_param(IS_RCC_SYSCLKSOURCE(RCC_ClkInitStruct->SYSCLKSource));
  674. MODIFY_REG(RCC->D1CFGR, RCC_D1CFGR_D1CPRE, RCC_ClkInitStruct->SYSCLKDivider);
  675. /* HSE is selected as System Clock Source */
  676. if(RCC_ClkInitStruct->SYSCLKSource == RCC_SYSCLKSOURCE_HSE)
  677. {
  678. /* Check the HSE ready flag */
  679. if(__HAL_RCC_GET_FLAG(RCC_FLAG_HSERDY) == RESET)
  680. {
  681. return HAL_ERROR;
  682. }
  683. }
  684. /* PLL is selected as System Clock Source */
  685. else if(RCC_ClkInitStruct->SYSCLKSource == RCC_SYSCLKSOURCE_PLLCLK)
  686. {
  687. /* Check the PLL ready flag */
  688. if(__HAL_RCC_GET_FLAG(RCC_FLAG_PLLRDY) == RESET)
  689. {
  690. return HAL_ERROR;
  691. }
  692. }
  693. /* CSI is selected as System Clock Source */
  694. else if(RCC_ClkInitStruct->SYSCLKSource == RCC_SYSCLKSOURCE_CSI)
  695. {
  696. /* Check the PLL ready flag */
  697. if(__HAL_RCC_GET_FLAG(RCC_FLAG_CSIRDY) == RESET)
  698. {
  699. return HAL_ERROR;
  700. }
  701. }
  702. /* HSI is selected as System Clock Source */
  703. else
  704. {
  705. /* Check the HSI ready flag */
  706. if(__HAL_RCC_GET_FLAG(RCC_FLAG_HSIRDY) == RESET)
  707. {
  708. return HAL_ERROR;
  709. }
  710. }
  711. MODIFY_REG(RCC->CFGR, RCC_CFGR_SW, RCC_ClkInitStruct->SYSCLKSource);
  712. /* Get Start Tick*/
  713. tickstart = HAL_GetTick();
  714. if(RCC_ClkInitStruct->SYSCLKSource == RCC_SYSCLKSOURCE_HSE)
  715. {
  716. while (__HAL_RCC_GET_SYSCLK_SOURCE() != RCC_CFGR_SWS_HSE)
  717. {
  718. if((int32_t) (HAL_GetTick() - tickstart ) > CLOCKSWITCH_TIMEOUT_VALUE)
  719. {
  720. return HAL_TIMEOUT;
  721. }
  722. }
  723. }
  724. else if(RCC_ClkInitStruct->SYSCLKSource == RCC_SYSCLKSOURCE_PLLCLK)
  725. {
  726. while (__HAL_RCC_GET_SYSCLK_SOURCE() != RCC_CFGR_SWS_PLL1)
  727. {
  728. if((int32_t) (HAL_GetTick() - tickstart ) > CLOCKSWITCH_TIMEOUT_VALUE)
  729. {
  730. return HAL_TIMEOUT;
  731. }
  732. }
  733. }
  734. else if(RCC_ClkInitStruct->SYSCLKSource == RCC_SYSCLKSOURCE_CSI)
  735. {
  736. while (__HAL_RCC_GET_SYSCLK_SOURCE() != RCC_CFGR_SWS_CSI)
  737. {
  738. if((int32_t) (HAL_GetTick() - tickstart ) > CLOCKSWITCH_TIMEOUT_VALUE)
  739. {
  740. return HAL_TIMEOUT;
  741. }
  742. }
  743. }
  744. else
  745. {
  746. while(__HAL_RCC_GET_SYSCLK_SOURCE() != RCC_CFGR_SWS_HSI)
  747. {
  748. if((int32_t) (HAL_GetTick() - tickstart ) > CLOCKSWITCH_TIMEOUT_VALUE)
  749. {
  750. return HAL_TIMEOUT;
  751. }
  752. }
  753. }
  754. }
  755. /* Decreasing the number of wait states because of lower CPU frequency */
  756. if(FLatency < (FLASH->ACR & FLASH_ACR_LATENCY))
  757. {
  758. /* Program the new number of wait states to the LATENCY bits in the FLASH_ACR register */
  759. __HAL_FLASH_SET_LATENCY(FLatency);
  760. /* Check that the new number of wait states is taken into account to access the Flash
  761. memory by reading the FLASH_ACR register */
  762. if((FLASH->ACR & FLASH_ACR_LATENCY) != FLatency)
  763. {
  764. return HAL_ERROR;
  765. }
  766. }
  767. /*-------------------------- D1PCLK1 Configuration ---------------------------*/
  768. if(((RCC_ClkInitStruct->ClockType) & RCC_CLOCKTYPE_D1PCLK1) == RCC_CLOCKTYPE_D1PCLK1)
  769. {
  770. assert_param(IS_RCC_D1PCLK1(RCC_ClkInitStruct->APB3CLKDivider));
  771. MODIFY_REG(RCC->D1CFGR, RCC_D1CFGR_D1PPRE, RCC_ClkInitStruct->APB3CLKDivider);
  772. }
  773. /*-------------------------- PCLK1 Configuration ---------------------------*/
  774. if(((RCC_ClkInitStruct->ClockType) & RCC_CLOCKTYPE_PCLK1) == RCC_CLOCKTYPE_PCLK1)
  775. {
  776. assert_param(IS_RCC_PCLK1(RCC_ClkInitStruct->APB1CLKDivider));
  777. MODIFY_REG(RCC->D2CFGR, RCC_D2CFGR_D2PPRE1, (RCC_ClkInitStruct->APB1CLKDivider));
  778. }
  779. /*-------------------------- PCLK2 Configuration ---------------------------*/
  780. if(((RCC_ClkInitStruct->ClockType) & RCC_CLOCKTYPE_PCLK2) == RCC_CLOCKTYPE_PCLK2)
  781. {
  782. assert_param(IS_RCC_PCLK2(RCC_ClkInitStruct->APB2CLKDivider));
  783. MODIFY_REG(RCC->D2CFGR, RCC_D2CFGR_D2PPRE2, (RCC_ClkInitStruct->APB2CLKDivider));
  784. }
  785. /*-------------------------- D3PCLK1 Configuration ---------------------------*/
  786. if(((RCC_ClkInitStruct->ClockType) & RCC_CLOCKTYPE_D3PCLK1) == RCC_CLOCKTYPE_D3PCLK1)
  787. {
  788. assert_param(IS_RCC_D3PCLK1(RCC_ClkInitStruct->APB4CLKDivider));
  789. MODIFY_REG(RCC->D3CFGR, RCC_D2CFGR_D2PPRE1, (RCC_ClkInitStruct->APB4CLKDivider) );
  790. }
  791. /* Update the SystemCoreClock global variable */
  792. SystemCoreClock = HAL_RCC_GetSysClockFreq() >> D1CorePrescTable[(RCC->D1CFGR & RCC_D1CFGR_D1CPRE)>> POSITION_VAL(RCC_D1CFGR_D1CPRE_0)];
  793. /* Configure the source of time base considering new system clocks settings*/
  794. HAL_InitTick (TICK_INT_PRIORITY);
  795. return HAL_OK;
  796. }
  797. /**
  798. * @}
  799. */
  800. /** @defgroup RCC_Group2 Peripheral Control functions
  801. * @brief RCC clocks control functions
  802. *
  803. @verbatim
  804. ===============================================================================
  805. ##### Peripheral Control functions #####
  806. ===============================================================================
  807. [..]
  808. This subsection provides a set of functions allowing to control the RCC Clocks
  809. frequencies.
  810. @endverbatim
  811. * @{
  812. */
  813. /**
  814. * @brief Selects the clock source to output on MCO1 pin(PA8) or on MCO2 pin(PC9).
  815. * @note PA8/PC9 should be configured in alternate function mode.
  816. * @param RCC_MCOx: specifies the output direction for the clock source.
  817. * This parameter can be one of the following values:
  818. * @arg RCC_MCO1: Clock source to output on MCO1 pin(PA8).
  819. * @arg RCC_MCO2: Clock source to output on MCO2 pin(PC9).
  820. * @param RCC_MCOSource: specifies the clock source to output.
  821. * This parameter can be one of the following values:
  822. * @arg RCC_MCO1SOURCE_HSI: HSI clock selected as MCO1 source
  823. * @arg RCC_MCO1SOURCE_LSE: LSE clock selected as MCO1 source
  824. * @arg RCC_MCO1SOURCE_HSE: HSE clock selected as MCO1 source
  825. * @arg RCC_MCO1SOURCE_PLL1QCLK: PLL1Q clock selected as MCO1 source
  826. * @arg RCC_MCO1SOURCE_HSI48: HSI48 (48MHZ) selected as MCO1 source
  827. * @arg RCC_MCO2SOURCE_SYSCLK: System clock (SYSCLK) selected as MCO2 source
  828. * @arg RCC_MCO2SOURCE_PLL2PCLK: PLL2P clock selected as MCO2 source
  829. * @arg RCC_MCO2SOURCE_HSE: HSE clock selected as MCO2 source
  830. * @arg RCC_MCO2SOURCE_PLLCLK: PLL1P clock selected as MCO2 source
  831. * @arg RCC_MCO2SOURCE_CSICLK: CSI clock selected as MCO2 source
  832. * @arg RCC_MCO2SOURCE_LSICLK: LSI clock selected as MCO2 source
  833. * @param RCC_MCODiv: specifies the MCOx pre-scaler.
  834. * This parameter can be one of the following values:
  835. * @arg RCC_MCODIV_1 up to RCC_MCODIV_15 : divider applied to MCOx clock
  836. * @retval None
  837. */
  838. void HAL_RCC_MCOConfig(uint32_t RCC_MCOx, uint32_t RCC_MCOSource, uint32_t RCC_MCODiv)
  839. {
  840. GPIO_InitTypeDef GPIO_InitStruct;
  841. /* Check the parameters */
  842. assert_param(IS_RCC_MCO(RCC_MCOx));
  843. assert_param(IS_RCC_MCODIV(RCC_MCODiv));
  844. /* RCC_MCO1 */
  845. if(RCC_MCOx == RCC_MCO1)
  846. {
  847. assert_param(IS_RCC_MCO1SOURCE(RCC_MCOSource));
  848. /* MCO1 Clock Enable */
  849. __MCO1_CLK_ENABLE();
  850. /* Configure the MCO1 pin in alternate function mode */
  851. GPIO_InitStruct.Pin = MCO1_PIN;
  852. GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
  853. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
  854. GPIO_InitStruct.Pull = GPIO_NOPULL;
  855. GPIO_InitStruct.Alternate = GPIO_AF0_MCO;
  856. HAL_GPIO_Init(MCO1_GPIO_PORT, &GPIO_InitStruct);
  857. /* Mask MCO1 and MCO1PRE[3:0] bits then Select MCO1 clock source and pre-scaler */
  858. MODIFY_REG(RCC->CFGR, (RCC_CFGR_MCO1 | RCC_CFGR_MCO1PRE), (RCC_MCOSource | RCC_MCODiv));
  859. }
  860. else
  861. {
  862. assert_param(IS_RCC_MCO2SOURCE(RCC_MCOSource));
  863. /* MCO2 Clock Enable */
  864. __MCO2_CLK_ENABLE();
  865. /* Configure the MCO2 pin in alternate function mode */
  866. GPIO_InitStruct.Pin = MCO2_PIN;
  867. GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
  868. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
  869. GPIO_InitStruct.Pull = GPIO_NOPULL;
  870. GPIO_InitStruct.Alternate = GPIO_AF0_MCO;
  871. HAL_GPIO_Init(MCO2_GPIO_PORT, &GPIO_InitStruct);
  872. /* Mask MCO2 and MCO2PRE[3:0] bits then Select MCO2 clock source and pre-scaler */
  873. MODIFY_REG(RCC->CFGR, (RCC_CFGR_MCO2 | RCC_CFGR_MCO2PRE), (RCC_MCOSource | (RCC_MCODiv << 7)));
  874. }
  875. }
  876. /**
  877. * @brief Enables the Clock Security System.
  878. * @note If a failure is detected on the HSE oscillator clock, this oscillator
  879. * is automatically disabled and an interrupt is generated to inform the
  880. * software about the failure (Clock Security System Interrupt, CSSI),
  881. * allowing the MCU to perform rescue operations. The CSSI is linked to
  882. * the Cortex-M NMI (Non-Mask-able Interrupt) exception vector.
  883. * @retval None
  884. */
  885. void HAL_RCC_EnableCSS(void)
  886. {
  887. SET_BIT(RCC->CR, RCC_CR_CSSHSEON) ;
  888. }
  889. /**
  890. * @brief Returns the SYSCLK frequency
  891. *
  892. * @note The system frequency computed by this function is not the real
  893. * frequency in the chip. It is calculated based on the predefined
  894. * constant and the selected clock source:
  895. * @note If SYSCLK source is CSI, function returns values based on CSI_VALUE(*)
  896. * @note If SYSCLK source is HSI, function returns values based on HSI_VALUE(**)
  897. * @note If SYSCLK source is HSE, function returns values based on HSE_VALUE(***)
  898. * @note If SYSCLK source is PLL, function returns values based on CSI_VALUE(*),
  899. * HSI_VALUE(**) or HSE_VALUE(***) multiplied/divided by the PLL factors.
  900. * @note (*) CSI_VALUE is a constant defined in stm32h7xx_hal_conf.h file (default value
  901. * 4 MHz) but the real value may vary depending on the variations
  902. * in voltage and temperature.
  903. * @note (**) HSI_VALUE is a constant defined in stm32h7xx_hal_conf.h file (default value
  904. * 64 MHz) but the real value may vary depending on the variations
  905. * in voltage and temperature.
  906. * @note (***) HSE_VALUE is a constant defined in stm32h7xx_hal_conf.h file (default value
  907. * 25 MHz), user has to ensure that HSE_VALUE is same as the real
  908. * frequency of the crystal used. Otherwise, this function may
  909. * have wrong result.
  910. *
  911. * @note The result of this function could be not correct when using fractional
  912. * value for HSE crystal.
  913. *
  914. * @note This function can be used by the user application to compute the
  915. * baud rate for the communication peripherals or configure other parameters.
  916. *
  917. * @note Each time SYSCLK changes, this function must be called to update the
  918. * right SYSCLK value. Otherwise, any configuration based on this function will be incorrect.
  919. *
  920. *
  921. * @retval SYSCLK frequency
  922. */
  923. uint32_t HAL_RCC_GetSysClockFreq(void)
  924. {
  925. uint32_t pllp = 1, pllsource = 0, pllm = 1 ,pllfracen =0 , hsivalue = 0;
  926. float fracn1=0, pllvco = 0;
  927. uint32_t sysclockfreq = 0;
  928. /* Get SYSCLK source -------------------------------------------------------*/
  929. switch (RCC->CFGR & RCC_CFGR_SWS)
  930. {
  931. case 0x00: /* HSI used as system clock source */
  932. if (__HAL_RCC_GET_FLAG(RCC_FLAG_HSIDIV) != 0U)
  933. {
  934. sysclockfreq = (uint32_t) (HSI_VALUE >> (__HAL_RCC_GET_HSI_DIVIDER()>> 3));
  935. }
  936. else
  937. {
  938. sysclockfreq = (uint32_t) HSI_VALUE;
  939. }
  940. break;
  941. case 0x08: /* CSI used as system clock source */
  942. sysclockfreq = CSI_VALUE;
  943. break;
  944. case 0x10: /* HSE used as system clock source */
  945. sysclockfreq = HSE_VALUE;
  946. break;
  947. case 0x18: /* PLL1 used as system clock source */
  948. /* PLL_VCO = (HSE_VALUE or HSI_VALUE or CSI_VALUE/ PLLM) * PLLN
  949. SYSCLK = PLL_VCO / PLLR
  950. */
  951. pllsource = (RCC->PLLCKSELR & RCC_PLLCKSELR_PLLSRC);
  952. pllm = ((RCC->PLLCKSELR & RCC_PLLCKSELR_DIVM1)>> 4) ;
  953. pllfracen = RCC->PLLCFGR & RCC_PLLCFGR_PLL1FRACEN;
  954. fracn1 = (pllfracen* ((RCC->PLL1FRACR & RCC_PLL1FRACR_FRACN1)>> 3));
  955. switch (pllsource)
  956. {
  957. case 0x00: /* HSI used as PLL clock source */
  958. if (__HAL_RCC_GET_FLAG(RCC_FLAG_HSIDIV) != 0U)
  959. {
  960. hsivalue= (HSI_VALUE >> (__HAL_RCC_GET_HSI_DIVIDER()>> 3));
  961. pllvco = ( hsivalue / pllm) * ((RCC->PLL1DIVR & RCC_PLL1DIVR_N1) + (fracn1/0x1FFF) +1 );
  962. }
  963. else
  964. {
  965. pllvco = (HSI_VALUE / pllm) * ((RCC->PLL1DIVR & RCC_PLL1DIVR_N1) + (fracn1/0x1FFF) +1 );
  966. }
  967. break;
  968. case 0x01: /* CSI used as PLL clock source */
  969. pllvco = (CSI_VALUE / pllm) * ((RCC->PLL1DIVR & RCC_PLL1DIVR_N1) + (fracn1/0x1FFF) +1 );
  970. break;
  971. case 0x02: /* HSE used as PLL clock source */
  972. pllvco = (HSE_VALUE / pllm) * ((RCC->PLL1DIVR & RCC_PLL1DIVR_N1) + (fracn1/0x1FFF) +1 );
  973. break;
  974. default:
  975. pllvco = (CSI_VALUE / pllm) * ((RCC->PLL1DIVR & RCC_PLL1DIVR_N1) + (fracn1/0x1FFF) +1 );
  976. break;
  977. }
  978. pllp = (((RCC->PLL1DIVR & RCC_PLL1DIVR_P1) >>9) + 1 ) ;
  979. sysclockfreq = (uint32_t)(pllvco/pllp);
  980. break;
  981. default:
  982. sysclockfreq = CSI_VALUE;
  983. break;
  984. }
  985. return sysclockfreq;
  986. }
  987. /**
  988. * @brief Returns the HCLK frequency
  989. * @note Each time HCLK changes, this function must be called to update the
  990. * right HCLK value. Otherwise, any configuration based on this function will be incorrect.
  991. *
  992. * @note The SystemD2Clock CMSIS variable is used to store System domain2 Clock Frequency
  993. * and updated within this function
  994. * @retval HCLK frequency
  995. */
  996. uint32_t HAL_RCC_GetHCLKFreq(void)
  997. {
  998. SystemD2Clock = (HAL_RCCEx_GetD1SysClockFreq() >> D1CorePrescTable[(RCC->D1CFGR & RCC_D1CFGR_HPRE)>> POSITION_VAL(RCC_D1CFGR_HPRE_0)]);
  999. return SystemD2Clock;
  1000. }
  1001. /**
  1002. * @brief Returns the PCLK1 frequency
  1003. * @note Each time PCLK1 changes, this function must be called to update the
  1004. * right PCLK1 value. Otherwise, any configuration based on this function will be incorrect.
  1005. * @retval PCLK1 frequency
  1006. */
  1007. uint32_t HAL_RCC_GetPCLK1Freq(void)
  1008. {
  1009. /* Get HCLK source and Compute PCLK1 frequency ---------------------------*/
  1010. return (HAL_RCC_GetHCLKFreq() >> D1CorePrescTable[(RCC->D2CFGR & RCC_D2CFGR_D2PPRE1)>> POSITION_VAL(RCC_D2CFGR_D2PPRE1_0)]);
  1011. }
  1012. /**
  1013. * @brief Returns the PCLK2 frequency
  1014. * @note Each time PCLK2 changes, this function must be called to update the
  1015. * right PCLK2 value. Otherwise, any configuration based on this function will be incorrect.
  1016. * @retval PCLK1 frequency
  1017. */
  1018. uint32_t HAL_RCC_GetPCLK2Freq(void)
  1019. {
  1020. /* Get HCLK source and Compute PCLK1 frequency ---------------------------*/
  1021. return (HAL_RCC_GetHCLKFreq() >> D1CorePrescTable[(RCC->D2CFGR & RCC_D2CFGR_D2PPRE2)>> POSITION_VAL(RCC_D2CFGR_D2PPRE2_0)]);
  1022. }
  1023. /**
  1024. * @brief Configures the RCC_OscInitStruct according to the internal
  1025. * RCC configuration registers.
  1026. * @param RCC_OscInitStruct: pointer to an RCC_OscInitTypeDef structure that
  1027. * will be configured.
  1028. * @retval None
  1029. */
  1030. void HAL_RCC_GetOscConfig(RCC_OscInitTypeDef *RCC_OscInitStruct)
  1031. {
  1032. /* Set all possible values for the Oscillator type parameter ---------------*/
  1033. RCC_OscInitStruct->OscillatorType = RCC_OSCILLATORTYPE_HSE | RCC_OSCILLATORTYPE_HSI | RCC_OSCILLATORTYPE_CSI | \
  1034. RCC_OSCILLATORTYPE_LSE | RCC_OSCILLATORTYPE_LSI| RCC_OSCILLATORTYPE_HSI48;
  1035. /* Get the HSE configuration -----------------------------------------------*/
  1036. if((RCC->CR &RCC_CR_HSEBYP) == RCC_CR_HSEBYP)
  1037. {
  1038. RCC_OscInitStruct->HSEState = RCC_HSE_BYPASS;
  1039. }
  1040. else if((RCC->CR &RCC_CR_HSEON) == RCC_CR_HSEON)
  1041. {
  1042. RCC_OscInitStruct->HSEState = RCC_HSE_ON;
  1043. }
  1044. else
  1045. {
  1046. RCC_OscInitStruct->HSEState = RCC_HSE_OFF;
  1047. }
  1048. /* Get the CSI configuration -----------------------------------------------*/
  1049. if((RCC->CR &RCC_CR_CSION) == RCC_CR_CSION)
  1050. {
  1051. RCC_OscInitStruct->CSIState = RCC_CSI_ON;
  1052. }
  1053. else
  1054. {
  1055. RCC_OscInitStruct->CSIState = RCC_CSI_OFF;
  1056. }
  1057. RCC_OscInitStruct->CSICalibrationValue = (uint32_t)((RCC->ICSCR &RCC_ICSCR_CSITRIM) >> POSITION_VAL(RCC_ICSCR_CSITRIM));
  1058. /* Get the HSI configuration -----------------------------------------------*/
  1059. if((RCC->CR &RCC_CR_HSION) == RCC_CR_HSION)
  1060. {
  1061. RCC_OscInitStruct->HSIState = RCC_HSI_ON;
  1062. }
  1063. else
  1064. {
  1065. RCC_OscInitStruct->HSIState = RCC_HSI_OFF;
  1066. }
  1067. RCC_OscInitStruct->HSICalibrationValue = (uint32_t)((RCC->ICSCR &RCC_ICSCR_HSITRIM) >> POSITION_VAL(RCC_ICSCR_HSITRIM));
  1068. /* Get the LSE configuration -----------------------------------------------*/
  1069. if((RCC->BDCR &RCC_BDCR_LSEBYP) == RCC_BDCR_LSEBYP)
  1070. {
  1071. RCC_OscInitStruct->LSEState = RCC_LSE_BYPASS;
  1072. }
  1073. else if((RCC->BDCR &RCC_BDCR_LSEON) == RCC_BDCR_LSEON)
  1074. {
  1075. RCC_OscInitStruct->LSEState = RCC_LSE_ON;
  1076. }
  1077. else
  1078. {
  1079. RCC_OscInitStruct->LSEState = RCC_LSE_OFF;
  1080. }
  1081. /* Get the LSI configuration -----------------------------------------------*/
  1082. if((RCC->CSR &RCC_CSR_LSION) == RCC_CSR_LSION)
  1083. {
  1084. RCC_OscInitStruct->LSIState = RCC_LSI_ON;
  1085. }
  1086. else
  1087. {
  1088. RCC_OscInitStruct->LSIState = RCC_LSI_OFF;
  1089. }
  1090. /* Get the HSI48 configuration ---------------------------------------------*/
  1091. if((RCC->CR & RCC_CR_HSI48ON) == RCC_CR_HSI48ON)
  1092. {
  1093. RCC_OscInitStruct->HSI48State = RCC_HSI48_ON;
  1094. }
  1095. else
  1096. {
  1097. RCC_OscInitStruct->HSI48State = RCC_HSI48_OFF;
  1098. }
  1099. /* Get the PLL configuration -----------------------------------------------*/
  1100. if((RCC->CR &RCC_CR_PLLON) == RCC_CR_PLLON)
  1101. {
  1102. RCC_OscInitStruct->PLL.PLLState = RCC_PLL_ON;
  1103. }
  1104. else
  1105. {
  1106. RCC_OscInitStruct->PLL.PLLState = RCC_PLL_OFF;
  1107. }
  1108. RCC_OscInitStruct->PLL.PLLSource = (uint32_t)(RCC->PLLCKSELR & RCC_PLLCKSELR_PLLSRC);
  1109. RCC_OscInitStruct->PLL.PLLM = (uint32_t)((RCC->PLLCKSELR & RCC_PLLCKSELR_DIVM1)>> POSITION_VAL(RCC_PLLCKSELR_DIVM1));
  1110. RCC_OscInitStruct->PLL.PLLN = (uint32_t)((RCC->PLL1DIVR & RCC_PLL1DIVR_N1) >> POSITION_VAL(RCC_PLL1DIVR_N1))+ 1;
  1111. RCC_OscInitStruct->PLL.PLLR = (uint32_t)((RCC->PLL1DIVR & RCC_PLL1DIVR_R1) >> POSITION_VAL(RCC_PLL1DIVR_R1))+ 1;
  1112. RCC_OscInitStruct->PLL.PLLP = (uint32_t)((RCC->PLL1DIVR & RCC_PLL1DIVR_P1) >> POSITION_VAL(RCC_PLL1DIVR_P1))+ 1;
  1113. RCC_OscInitStruct->PLL.PLLQ = (uint32_t)((RCC->PLL1DIVR & RCC_PLL1DIVR_Q1) >> POSITION_VAL(RCC_PLL1DIVR_Q1))+ 1;
  1114. RCC_OscInitStruct->PLL.PLLRGE = (uint32_t)((RCC->PLLCFGR & RCC_PLLCFGR_PLL1RGE) >> POSITION_VAL(RCC_PLLCFGR_PLL1RGE_1));
  1115. RCC_OscInitStruct->PLL.PLLVCOSEL = (uint32_t)((RCC->PLLCFGR & RCC_PLLCFGR_PLL1VCOSEL) >> POSITION_VAL(RCC_PLLCFGR_PLL1VCOSEL));
  1116. }
  1117. /**
  1118. * @brief Configures the RCC_ClkInitStruct according to the internal
  1119. * RCC configuration registers.
  1120. * @param RCC_ClkInitStruct: pointer to an RCC_ClkInitTypeDef structure that
  1121. * will be configured.
  1122. * @param pFLatency: Pointer on the Flash Latency.
  1123. * @retval None
  1124. */
  1125. void HAL_RCC_GetClockConfig(RCC_ClkInitTypeDef *RCC_ClkInitStruct, uint32_t *pFLatency)
  1126. {
  1127. /* Set all possible values for the Clock type parameter --------------------*/
  1128. RCC_ClkInitStruct->ClockType = RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_D1PCLK1 | RCC_CLOCKTYPE_PCLK1 |
  1129. RCC_CLOCKTYPE_PCLK2 | RCC_CLOCKTYPE_D3PCLK1 ;
  1130. /* Get the SYSCLK configuration --------------------------------------------*/
  1131. RCC_ClkInitStruct->SYSCLKSource = (uint32_t)(RCC->CFGR & RCC_CFGR_SW);
  1132. /* Get the SYSCLK configuration ----------------------------------------------*/
  1133. RCC_ClkInitStruct->SYSCLKDivider = (uint32_t)(RCC->D1CFGR & RCC_D1CFGR_D1CPRE);
  1134. /* Get the D1HCLK configuration ----------------------------------------------*/
  1135. RCC_ClkInitStruct->AHBCLKDivider = (uint32_t)(RCC->D1CFGR & RCC_D1CFGR_HPRE);
  1136. /* Get the APB3 configuration ----------------------------------------------*/
  1137. RCC_ClkInitStruct->APB3CLKDivider = (uint32_t)(RCC->D1CFGR & RCC_D1CFGR_D1PPRE);
  1138. /* Get the APB1 configuration ----------------------------------------------*/
  1139. RCC_ClkInitStruct->APB1CLKDivider = (uint32_t)(RCC->D2CFGR & RCC_D2CFGR_D2PPRE1);
  1140. /* Get the APB2 configuration ----------------------------------------------*/
  1141. RCC_ClkInitStruct->APB2CLKDivider = (uint32_t)(RCC->D2CFGR & RCC_D2CFGR_D2PPRE2);
  1142. /* Get the APB4 configuration ----------------------------------------------*/
  1143. RCC_ClkInitStruct->APB4CLKDivider = (uint32_t)(RCC->D3CFGR & RCC_D3CFGR_D3PPRE);
  1144. /* Get the Flash Wait State (Latency) configuration ------------------------*/
  1145. *pFLatency = (uint32_t)(FLASH->ACR & FLASH_ACR_LATENCY);
  1146. }
  1147. /**
  1148. * @brief This function handles the RCC CSS interrupt request.
  1149. * @note This API should be called under the NMI_Handler().
  1150. * @retval None
  1151. */
  1152. void HAL_RCC_NMI_IRQHandler(void)
  1153. {
  1154. /* Check RCC CSSF flag */
  1155. if(__HAL_RCC_GET_IT(RCC_IT_CSS))
  1156. {
  1157. /* RCC Clock Security System interrupt user callback */
  1158. HAL_RCC_CCSCallback();
  1159. /* Clear RCC CSS pending bit */
  1160. __HAL_RCC_CLEAR_IT(RCC_IT_CSS);
  1161. }
  1162. }
  1163. /**
  1164. * @brief RCC Clock Security System interrupt callback
  1165. * @retval none
  1166. */
  1167. __weak void HAL_RCC_CCSCallback(void)
  1168. {
  1169. /* NOTE : This function Should not be modified, when the callback is needed,
  1170. the HAL_RCC_CCSCallback could be implemented in the user file
  1171. */
  1172. }
  1173. /**
  1174. * @}
  1175. */
  1176. /**
  1177. * @}
  1178. */
  1179. #endif /* HAL_RCC_MODULE_ENABLED */
  1180. /**
  1181. * @}
  1182. */
  1183. /**
  1184. * @}
  1185. */
  1186. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/