stm32l4xx_hal_rcc.c 64 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730
  1. /**
  2. ******************************************************************************
  3. * @file stm32l4xx_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 Multiple Speed Internal oscillator
  17. (4 MHz) 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 (AHBs) and Low speed (APBs) busses:
  21. all peripherals mapped on these busses are running at MSI speed.
  22. (+) The clock for all peripherals is switched off, except the SRAM and FLASH.
  23. (+) All GPIOs are in analog mode, 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 (SAIx, RTC, ADC, USB OTG FS/SDMMC1/RNG)
  34. @endverbatim
  35. ******************************************************************************
  36. * @attention
  37. *
  38. * <h2><center>&copy; COPYRIGHT(c) 2017 STMicroelectronics</center></h2>
  39. *
  40. * Redistribution and use in source and binary forms, with or without modification,
  41. * are permitted provided that the following conditions are met:
  42. * 1. Redistributions of source code must retain the above copyright notice,
  43. * this list of conditions and the following disclaimer.
  44. * 2. Redistributions in binary form must reproduce the above copyright notice,
  45. * this list of conditions and the following disclaimer in the documentation
  46. * and/or other materials provided with the distribution.
  47. * 3. Neither the name of STMicroelectronics nor the names of its contributors
  48. * may be used to endorse or promote products derived from this software
  49. * without specific prior written permission.
  50. *
  51. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  52. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  53. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  54. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  55. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  56. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  57. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  58. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  59. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  60. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  61. *
  62. ******************************************************************************
  63. */
  64. /* Includes ------------------------------------------------------------------*/
  65. #include "stm32l4xx_hal.h"
  66. /** @addtogroup STM32L4xx_HAL_Driver
  67. * @{
  68. */
  69. /** @defgroup RCC RCC
  70. * @brief RCC HAL module driver
  71. * @{
  72. */
  73. #ifdef HAL_RCC_MODULE_ENABLED
  74. /* Private typedef -----------------------------------------------------------*/
  75. /* Private define ------------------------------------------------------------*/
  76. /** @defgroup RCC_Private_Constants RCC Private Constants
  77. * @{
  78. */
  79. #define HSE_TIMEOUT_VALUE HSE_STARTUP_TIMEOUT
  80. #define HSI_TIMEOUT_VALUE 2U /* 2 ms (minimum Tick + 1) */
  81. #define MSI_TIMEOUT_VALUE 2U /* 2 ms (minimum Tick + 1) */
  82. #define LSI_TIMEOUT_VALUE 2U /* 2 ms (minimum Tick + 1) */
  83. #define HSI48_TIMEOUT_VALUE 2U /* 2 ms (minimum Tick + 1) */
  84. #define PLL_TIMEOUT_VALUE 2U /* 2 ms (minimum Tick + 1) */
  85. #define CLOCKSWITCH_TIMEOUT_VALUE 5000U /* 5 s */
  86. /**
  87. * @}
  88. */
  89. /* Private macro -------------------------------------------------------------*/
  90. /** @defgroup RCC_Private_Macros RCC Private Macros
  91. * @{
  92. */
  93. #define __MCO1_CLK_ENABLE() __HAL_RCC_GPIOA_CLK_ENABLE()
  94. #define MCO1_GPIO_PORT GPIOA
  95. #define MCO1_PIN GPIO_PIN_8
  96. #define RCC_PLL_OSCSOURCE_CONFIG(__HAL_RCC_PLLSOURCE__) \
  97. (MODIFY_REG(RCC->PLLCFGR, RCC_PLLCFGR_PLLSRC, (__HAL_RCC_PLLSOURCE__)))
  98. /**
  99. * @}
  100. */
  101. /* Private variables ---------------------------------------------------------*/
  102. /* Private function prototypes -----------------------------------------------*/
  103. /** @defgroup RCC_Private_Functions RCC Private Functions
  104. * @{
  105. */
  106. static HAL_StatusTypeDef RCC_SetFlashLatencyFromMSIRange(uint32_t msirange);
  107. #if defined(STM32L4R5xx) || defined(STM32L4R7xx) || defined(STM32L4R9xx) || defined(STM32L4S5xx) || defined(STM32L4S7xx) || defined(STM32L4S9xx)
  108. static uint32_t RCC_GetSysClockFreqFromPLLSource(void);
  109. #endif
  110. /**
  111. * @}
  112. */
  113. /* Exported functions --------------------------------------------------------*/
  114. /** @defgroup RCC_Exported_Functions RCC Exported Functions
  115. * @{
  116. */
  117. /** @defgroup RCC_Exported_Functions_Group1 Initialization and de-initialization functions
  118. * @brief Initialization and Configuration functions
  119. *
  120. @verbatim
  121. ===============================================================================
  122. ##### Initialization and de-initialization functions #####
  123. ===============================================================================
  124. [..]
  125. This section provides functions allowing to configure the internal and external oscillators
  126. (HSE, HSI, LSE, MSI, LSI, PLL, CSS and MCO) and the System busses clocks (SYSCLK, AHB, APB1
  127. and APB2).
  128. [..] Internal/external clock and PLL configuration
  129. (+) HSI (high-speed internal): 16 MHz factory-trimmed RC used directly or through
  130. the PLL as System clock source.
  131. (+) MSI (Mutiple Speed Internal): Its frequency is software trimmable from 100KHZ to 48MHZ.
  132. It can be used to generate the clock for the USB OTG FS (48 MHz).
  133. The number of flash wait states is automatically adjusted when MSI range is updated with
  134. HAL_RCC_OscConfig() and the MSI is used as System clock source.
  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 optionally as RTC clock source.
  139. (+) LSE (low-speed external): 32.768 KHz oscillator used optionally as RTC clock source.
  140. (+) PLL (clocked by HSI, HSE or MSI) providing up to three independent output clocks:
  141. (++) The first output is used to generate the high speed system clock (up to 80MHz).
  142. (++) The second output is used to generate the clock for the USB OTG FS (48 MHz),
  143. the random analog generator (<=48 MHz) and the SDMMC1 (<= 48 MHz).
  144. (++) The third output is used to generate an accurate clock to achieve
  145. high-quality audio performance on SAI interface.
  146. (+) PLLSAI1 (clocked by HSI, HSE or MSI) providing up to three independent output clocks:
  147. (++) The first output is used to generate SAR ADC1 clock.
  148. (++) The second output is used to generate the clock for the USB OTG FS (48 MHz),
  149. the random analog generator (<=48 MHz) and the SDMMC1 (<= 48 MHz).
  150. (++) The Third output is used to generate an accurate clock to achieve
  151. high-quality audio performance on SAI interface.
  152. (+) PLLSAI2 (clocked by HSI , HSE or MSI) providing up to two independent output clocks:
  153. (++) The first output is used to generate SAR ADC2 clock.
  154. (++) The second output is used to generate an accurate clock to achieve
  155. high-quality audio performance on SAI interface.
  156. (+) CSS (Clock security system): once enabled, if a HSE clock failure occurs
  157. (HSE used directly or through PLL as System clock source), the System clock
  158. is automatically switched to HSI and an interrupt is generated if enabled.
  159. The interrupt is linked to the Cortex-M4 NMI (Non-Maskable Interrupt)
  160. exception vector.
  161. (+) MCO (microcontroller clock output): used to output MSI, LSI, HSI, LSE, HSE or
  162. main PLL clock (through a configurable prescaler) on PA8 pin.
  163. [..] System, AHB and APB busses clocks configuration
  164. (+) Several clock sources can be used to drive the System clock (SYSCLK): MSI, HSI,
  165. HSE and main PLL.
  166. The AHB clock (HCLK) is derived from System clock through configurable
  167. prescaler and used to clock the CPU, memory and peripherals mapped
  168. on AHB bus (DMA, GPIO...). APB1 (PCLK1) and APB2 (PCLK2) clocks are derived
  169. from AHB clock through configurable prescalers and used to clock
  170. the peripherals mapped on these busses. You can use
  171. "HAL_RCC_GetSysClockFreq()" function to retrieve the frequencies of these clocks.
  172. -@- All the peripheral clocks are derived from the System clock (SYSCLK) except:
  173. (+@) SAI: the SAI clock can be derived either from a specific PLL (PLLSAI1) or (PLLSAI2) or
  174. from an external clock mapped on the SAI_CKIN pin.
  175. You have to use HAL_RCCEx_PeriphCLKConfig() function to configure this clock.
  176. (+@) RTC: the RTC clock can be derived either from the LSI, LSE or HSE clock
  177. divided by 2 to 31.
  178. You have to use __HAL_RCC_RTC_ENABLE() and HAL_RCCEx_PeriphCLKConfig() function
  179. to configure this clock.
  180. (+@) USB OTG FS, SDMMC1 and RNG: USB OTG FS requires a frequency equal to 48 MHz
  181. to work correctly, while the SDMMC1 and RNG peripherals require a frequency
  182. equal or lower than to 48 MHz. This clock is derived of the main PLL or PLLSAI1
  183. through PLLQ divider. You have to enable the peripheral clock and use
  184. HAL_RCCEx_PeriphCLKConfig() function to configure this clock.
  185. (+@) IWDG clock which is always the LSI clock.
  186. (+) The maximum frequency of the SYSCLK, HCLK, PCLK1 and PCLK2 is 80 MHz.
  187. The clock source frequency should be adapted depending on the device voltage range
  188. as listed in the Reference Manual "Clock source frequency versus voltage scaling" chapter.
  189. @endverbatim
  190. Table 1. HCLK clock frequency for STM32L4Rx/STM32L4Sx devices
  191. +--------------------------------------------------------+
  192. | Latency | HCLK clock frequency (MHz) |
  193. | |--------------------------------------|
  194. | | voltage range 1 | voltage range 2 |
  195. | | 1.2 V | 1.0 V |
  196. |-----------------|-------------------|------------------|
  197. |0WS(1 CPU cycles)| 0 < HCLK <= 20 | 0 < HCLK <= 8 |
  198. |-----------------|-------------------|------------------|
  199. |1WS(2 CPU cycles)| 20 < HCLK <= 40 | 8 < HCLK <= 16 |
  200. |-----------------|-------------------|------------------|
  201. |2WS(3 CPU cycles)| 40 < HCLK <= 60 | 16 < HCLK <= 26 |
  202. |-----------------|-------------------|------------------|
  203. |3WS(4 CPU cycles)| 60 < HCLK <= 80 | 16 < HCLK <= 26 |
  204. |-----------------|-------------------|------------------|
  205. |4WS(5 CPU cycles)| 80 < HCLK <= 100 | 16 < HCLK <= 26 |
  206. |-----------------|-------------------|------------------|
  207. |5WS(6 CPU cycles)| 100 < HCLK <= 120 | 16 < HCLK <= 26 |
  208. +--------------------------------------------------------+
  209. Table 2. HCLK clock frequency for other STM32L4 devices
  210. +-------------------------------------------------------+
  211. | Latency | HCLK clock frequency (MHz) |
  212. | |-------------------------------------|
  213. | | voltage range 1 | voltage range 2 |
  214. | | 1.2 V | 1.0 V |
  215. |-----------------|------------------|------------------|
  216. |0WS(1 CPU cycles)| 0 < HCLK <= 16 | 0 < HCLK <= 6 |
  217. |-----------------|------------------|------------------|
  218. |1WS(2 CPU cycles)| 16 < HCLK <= 32 | 6 < HCLK <= 12 |
  219. |-----------------|------------------|------------------|
  220. |2WS(3 CPU cycles)| 32 < HCLK <= 48 | 12 < HCLK <= 18 |
  221. |-----------------|------------------|------------------|
  222. |3WS(4 CPU cycles)| 48 < HCLK <= 64 | 18 < HCLK <= 26 |
  223. |-----------------|------------------|------------------|
  224. |4WS(5 CPU cycles)| 64 < HCLK <= 80 | 18 < HCLK <= 26 |
  225. +-------------------------------------------------------+
  226. * @{
  227. */
  228. /**
  229. * @brief Reset the RCC clock configuration to the default reset state.
  230. * @note The default reset state of the clock configuration is given below:
  231. * - MSI ON and used as system clock source
  232. * - HSE, HSI, PLL, PLLSAI1 and PLLISAI2 OFF
  233. * - AHB, APB1 and APB2 prescaler set to 1.
  234. * - CSS, MCO1 OFF
  235. * - All interrupts disabled
  236. * - All interrupt and reset flags cleared
  237. * @note This function doesn't modify the configuration of the
  238. * - Peripheral clocks
  239. * - LSI, LSE and RTC clocks
  240. * @retval HAL status
  241. */
  242. HAL_StatusTypeDef HAL_RCC_DeInit(void)
  243. {
  244. uint32_t tickstart = 0;
  245. /* Set MSION bit */
  246. SET_BIT(RCC->CR, RCC_CR_MSION);
  247. /* Insure MSIRDY bit is set before writing default MSIRANGE value */
  248. /* Get start tick */
  249. tickstart = HAL_GetTick();
  250. /* Wait till MSI is ready */
  251. while(READ_BIT(RCC->CR, RCC_CR_MSIRDY) == RESET)
  252. {
  253. if((HAL_GetTick() - tickstart) > MSI_TIMEOUT_VALUE)
  254. {
  255. return HAL_TIMEOUT;
  256. }
  257. }
  258. /* Set MSIRANGE default value */
  259. MODIFY_REG(RCC->CR, RCC_CR_MSIRANGE, RCC_MSIRANGE_6);
  260. /* Reset CFGR register (MSI is selected as system clock source) */
  261. CLEAR_REG(RCC->CFGR);
  262. /* Update the SystemCoreClock global variable for MSI as system clock source */
  263. SystemCoreClock = MSI_VALUE;
  264. /* Configure the source of time base considering new system clock settings */
  265. if(HAL_InitTick(TICK_INT_PRIORITY) != HAL_OK)
  266. {
  267. return HAL_ERROR;
  268. }
  269. /* Insure MSI selected as system clock source */
  270. /* Get start tick */
  271. tickstart = HAL_GetTick();
  272. /* Wait till system clock source is ready */
  273. while(READ_BIT(RCC->CFGR, RCC_CFGR_SWS) != RCC_CFGR_SWS_MSI)
  274. {
  275. if((HAL_GetTick() - tickstart) > CLOCKSWITCH_TIMEOUT_VALUE)
  276. {
  277. return HAL_TIMEOUT;
  278. }
  279. }
  280. /* Reset HSION, HSIKERON, HSIASFS, HSEON, HSECSSON, PLLON, PLLSAIxON bits */
  281. #if defined(RCC_PLLSAI2_SUPPORT)
  282. CLEAR_BIT(RCC->CR, RCC_CR_HSEON | RCC_CR_HSION | RCC_CR_HSIKERON| RCC_CR_HSIASFS | RCC_CR_PLLON | RCC_CR_PLLSAI1ON | RCC_CR_PLLSAI2ON);
  283. #else
  284. CLEAR_BIT(RCC->CR, RCC_CR_HSEON | RCC_CR_HSION | RCC_CR_HSIKERON| RCC_CR_HSIASFS | RCC_CR_PLLON | RCC_CR_PLLSAI1ON);
  285. #endif /* RCC_PLLSAI2_SUPPORT */
  286. /* Insure PLLRDY, PLLSAI1RDY and PLLSAI2RDY (if present) are reset */
  287. /* Get start tick */
  288. tickstart = HAL_GetTick();
  289. #if defined(RCC_PLLSAI2_SUPPORT)
  290. while(READ_BIT(RCC->CR, RCC_CR_PLLRDY | RCC_CR_PLLSAI1RDY | RCC_CR_PLLSAI2RDY) != 0U)
  291. #else
  292. while(READ_BIT(RCC->CR, RCC_CR_PLLRDY | RCC_CR_PLLSAI1RDY) != 0U)
  293. #endif
  294. {
  295. if((HAL_GetTick() - tickstart) > PLL_TIMEOUT_VALUE)
  296. {
  297. return HAL_TIMEOUT;
  298. }
  299. }
  300. /* Reset PLLCFGR register */
  301. CLEAR_REG(RCC->PLLCFGR);
  302. SET_BIT(RCC->PLLCFGR, RCC_PLLCFGR_PLLN_4 );
  303. /* Reset PLLSAI1CFGR register */
  304. CLEAR_REG(RCC->PLLSAI1CFGR);
  305. SET_BIT(RCC->PLLSAI1CFGR, RCC_PLLSAI1CFGR_PLLSAI1N_4 );
  306. #if defined(RCC_PLLSAI2_SUPPORT)
  307. /* Reset PLLSAI2CFGR register */
  308. CLEAR_REG(RCC->PLLSAI2CFGR);
  309. SET_BIT(RCC->PLLSAI2CFGR, RCC_PLLSAI2CFGR_PLLSAI2N_4 );
  310. #endif /* RCC_PLLSAI2_SUPPORT */
  311. /* Reset HSEBYP bit */
  312. CLEAR_BIT(RCC->CR, RCC_CR_HSEBYP);
  313. /* Disable all interrupts */
  314. CLEAR_REG(RCC->CIER);
  315. /* Clear all interrupt flags */
  316. WRITE_REG(RCC->CICR, 0xFFFFFFFFU);
  317. /* Clear all reset flags */
  318. SET_BIT(RCC->CSR, RCC_CSR_RMVF);
  319. return HAL_OK;
  320. }
  321. /**
  322. * @brief Initialize the RCC Oscillators according to the specified parameters in the
  323. * RCC_OscInitTypeDef.
  324. * @param RCC_OscInitStruct pointer to an RCC_OscInitTypeDef structure that
  325. * contains the configuration information for the RCC Oscillators.
  326. * @note The PLL is not disabled when used as system clock.
  327. * @note Transitions LSE Bypass to LSE On and LSE On to LSE Bypass are not
  328. * supported by this macro. User should request a transition to LSE Off
  329. * first and then LSE On or LSE Bypass.
  330. * @note Transition HSE Bypass to HSE On and HSE On to HSE Bypass are not
  331. * supported by this macro. User should request a transition to HSE Off
  332. * first and then HSE On or HSE Bypass.
  333. * @retval HAL status
  334. */
  335. HAL_StatusTypeDef HAL_RCC_OscConfig(RCC_OscInitTypeDef *RCC_OscInitStruct)
  336. {
  337. uint32_t tickstart = 0;
  338. /* Check the parameters */
  339. assert_param(RCC_OscInitStruct != NULL);
  340. assert_param(IS_RCC_OSCILLATORTYPE(RCC_OscInitStruct->OscillatorType));
  341. /*----------------------------- MSI Configuration --------------------------*/
  342. if(((RCC_OscInitStruct->OscillatorType) & RCC_OSCILLATORTYPE_MSI) == RCC_OSCILLATORTYPE_MSI)
  343. {
  344. /* Check the parameters */
  345. assert_param(IS_RCC_MSI(RCC_OscInitStruct->MSIState));
  346. assert_param(IS_RCC_MSICALIBRATION_VALUE(RCC_OscInitStruct->MSICalibrationValue));
  347. assert_param(IS_RCC_MSI_CLOCK_RANGE(RCC_OscInitStruct->MSIClockRange));
  348. /* When the MSI is used as system clock it will not be disabled */
  349. if((__HAL_RCC_GET_SYSCLK_SOURCE() == RCC_CFGR_SWS_MSI) )
  350. {
  351. if((READ_BIT(RCC->CR, RCC_CR_MSIRDY) != RESET) && (RCC_OscInitStruct->MSIState == RCC_MSI_OFF))
  352. {
  353. return HAL_ERROR;
  354. }
  355. /* Otherwise, just the calibration and MSI range change are allowed */
  356. else
  357. {
  358. /* To correctly read data from FLASH memory, the number of wait states (LATENCY)
  359. must be correctly programmed according to the frequency of the CPU clock
  360. (HCLK) and the supply voltage of the device. */
  361. if(RCC_OscInitStruct->MSIClockRange > __HAL_RCC_GET_MSI_RANGE())
  362. {
  363. /* First increase number of wait states update if necessary */
  364. if(RCC_SetFlashLatencyFromMSIRange(RCC_OscInitStruct->MSIClockRange) != HAL_OK)
  365. {
  366. return HAL_ERROR;
  367. }
  368. /* Selects the Multiple Speed oscillator (MSI) clock range .*/
  369. __HAL_RCC_MSI_RANGE_CONFIG(RCC_OscInitStruct->MSIClockRange);
  370. /* Adjusts the Multiple Speed oscillator (MSI) calibration value.*/
  371. __HAL_RCC_MSI_CALIBRATIONVALUE_ADJUST(RCC_OscInitStruct->MSICalibrationValue);
  372. }
  373. else
  374. {
  375. /* Else, keep current flash latency while decreasing applies */
  376. /* Selects the Multiple Speed oscillator (MSI) clock range .*/
  377. __HAL_RCC_MSI_RANGE_CONFIG(RCC_OscInitStruct->MSIClockRange);
  378. /* Adjusts the Multiple Speed oscillator (MSI) calibration value.*/
  379. __HAL_RCC_MSI_CALIBRATIONVALUE_ADJUST(RCC_OscInitStruct->MSICalibrationValue);
  380. /* Decrease number of wait states update if necessary */
  381. if(RCC_SetFlashLatencyFromMSIRange(RCC_OscInitStruct->MSIClockRange) != HAL_OK)
  382. {
  383. return HAL_ERROR;
  384. }
  385. }
  386. /* Update the SystemCoreClock global variable */
  387. SystemCoreClock = HAL_RCC_GetSysClockFreq() >> AHBPrescTable[READ_BIT(RCC->CFGR, RCC_CFGR_HPRE) >> RCC_CFGR_HPRE_Pos];
  388. /* Configure the source of time base considering new system clocks settings*/
  389. HAL_InitTick (TICK_INT_PRIORITY);
  390. }
  391. }
  392. else
  393. {
  394. /* Check the MSI State */
  395. if(RCC_OscInitStruct->MSIState != RCC_MSI_OFF)
  396. {
  397. /* Enable the Internal High Speed oscillator (MSI). */
  398. __HAL_RCC_MSI_ENABLE();
  399. /* Get timeout */
  400. tickstart = HAL_GetTick();
  401. /* Wait till MSI is ready */
  402. while(READ_BIT(RCC->CR, RCC_CR_MSIRDY) == RESET)
  403. {
  404. if((HAL_GetTick() - tickstart) > MSI_TIMEOUT_VALUE)
  405. {
  406. return HAL_TIMEOUT;
  407. }
  408. }
  409. /* Selects the Multiple Speed oscillator (MSI) clock range .*/
  410. __HAL_RCC_MSI_RANGE_CONFIG(RCC_OscInitStruct->MSIClockRange);
  411. /* Adjusts the Multiple Speed oscillator (MSI) calibration value.*/
  412. __HAL_RCC_MSI_CALIBRATIONVALUE_ADJUST(RCC_OscInitStruct->MSICalibrationValue);
  413. }
  414. else
  415. {
  416. /* Disable the Internal High Speed oscillator (MSI). */
  417. __HAL_RCC_MSI_DISABLE();
  418. /* Get timeout */
  419. tickstart = HAL_GetTick();
  420. /* Wait till MSI is ready */
  421. while(READ_BIT(RCC->CR, RCC_CR_MSIRDY) != RESET)
  422. {
  423. if((HAL_GetTick() - tickstart) > MSI_TIMEOUT_VALUE)
  424. {
  425. return HAL_TIMEOUT;
  426. }
  427. }
  428. }
  429. }
  430. }
  431. /*------------------------------- HSE Configuration ------------------------*/
  432. if(((RCC_OscInitStruct->OscillatorType) & RCC_OSCILLATORTYPE_HSE) == RCC_OSCILLATORTYPE_HSE)
  433. {
  434. /* Check the parameters */
  435. assert_param(IS_RCC_HSE(RCC_OscInitStruct->HSEState));
  436. /* When the HSE is used as system clock or clock source for PLL in these cases it is not allowed to be disabled */
  437. if((__HAL_RCC_GET_SYSCLK_SOURCE() == RCC_CFGR_SWS_HSE) ||
  438. ((__HAL_RCC_GET_SYSCLK_SOURCE() == RCC_CFGR_SWS_PLL) && (__HAL_RCC_GET_PLL_OSCSOURCE() == RCC_PLLSOURCE_HSE)))
  439. {
  440. if((READ_BIT(RCC->CR, RCC_CR_HSERDY) != RESET) && (RCC_OscInitStruct->HSEState == RCC_HSE_OFF))
  441. {
  442. return HAL_ERROR;
  443. }
  444. }
  445. else
  446. {
  447. /* Set the new HSE configuration ---------------------------------------*/
  448. __HAL_RCC_HSE_CONFIG(RCC_OscInitStruct->HSEState);
  449. /* Check the HSE State */
  450. if(RCC_OscInitStruct->HSEState != RCC_HSE_OFF)
  451. {
  452. /* Get Start Tick*/
  453. tickstart = HAL_GetTick();
  454. /* Wait till HSE is ready */
  455. while(READ_BIT(RCC->CR, RCC_CR_HSERDY) == RESET)
  456. {
  457. if((HAL_GetTick() - tickstart) > HSE_TIMEOUT_VALUE)
  458. {
  459. return HAL_TIMEOUT;
  460. }
  461. }
  462. }
  463. else
  464. {
  465. /* Get Start Tick*/
  466. tickstart = HAL_GetTick();
  467. /* Wait till HSE is disabled */
  468. while(READ_BIT(RCC->CR, RCC_CR_HSERDY) != RESET)
  469. {
  470. if((HAL_GetTick() - tickstart) > HSE_TIMEOUT_VALUE)
  471. {
  472. return HAL_TIMEOUT;
  473. }
  474. }
  475. }
  476. }
  477. }
  478. /*----------------------------- HSI Configuration --------------------------*/
  479. if(((RCC_OscInitStruct->OscillatorType) & RCC_OSCILLATORTYPE_HSI) == RCC_OSCILLATORTYPE_HSI)
  480. {
  481. /* Check the parameters */
  482. assert_param(IS_RCC_HSI(RCC_OscInitStruct->HSIState));
  483. assert_param(IS_RCC_HSI_CALIBRATION_VALUE(RCC_OscInitStruct->HSICalibrationValue));
  484. /* Check if HSI is used as system clock or as PLL source when PLL is selected as system clock */
  485. if((__HAL_RCC_GET_SYSCLK_SOURCE() == RCC_CFGR_SWS_HSI) ||
  486. ((__HAL_RCC_GET_SYSCLK_SOURCE() == RCC_CFGR_SWS_PLL) && (__HAL_RCC_GET_PLL_OSCSOURCE() == RCC_PLLSOURCE_HSI)))
  487. {
  488. /* When HSI is used as system clock it will not be disabled */
  489. if((READ_BIT(RCC->CR, RCC_CR_HSIRDY) != RESET) && (RCC_OscInitStruct->HSIState == RCC_HSI_OFF))
  490. {
  491. return HAL_ERROR;
  492. }
  493. /* Otherwise, just the calibration is allowed */
  494. else
  495. {
  496. /* Adjusts the Internal High Speed oscillator (HSI) calibration value.*/
  497. __HAL_RCC_HSI_CALIBRATIONVALUE_ADJUST(RCC_OscInitStruct->HSICalibrationValue);
  498. }
  499. }
  500. else
  501. {
  502. /* Check the HSI State */
  503. if(RCC_OscInitStruct->HSIState != RCC_HSI_OFF)
  504. {
  505. /* Enable the Internal High Speed oscillator (HSI). */
  506. __HAL_RCC_HSI_ENABLE();
  507. /* Get Start Tick*/
  508. tickstart = HAL_GetTick();
  509. /* Wait till HSI is ready */
  510. while(READ_BIT(RCC->CR, RCC_CR_HSIRDY) == RESET)
  511. {
  512. if((HAL_GetTick() - tickstart) > HSI_TIMEOUT_VALUE)
  513. {
  514. return HAL_TIMEOUT;
  515. }
  516. }
  517. /* Adjusts the Internal High Speed oscillator (HSI) calibration value.*/
  518. __HAL_RCC_HSI_CALIBRATIONVALUE_ADJUST(RCC_OscInitStruct->HSICalibrationValue);
  519. }
  520. else
  521. {
  522. /* Disable the Internal High Speed oscillator (HSI). */
  523. __HAL_RCC_HSI_DISABLE();
  524. /* Get Start Tick*/
  525. tickstart = HAL_GetTick();
  526. /* Wait till HSI is disabled */
  527. while(READ_BIT(RCC->CR, RCC_CR_HSIRDY) != RESET)
  528. {
  529. if((HAL_GetTick() - tickstart) > HSI_TIMEOUT_VALUE)
  530. {
  531. return HAL_TIMEOUT;
  532. }
  533. }
  534. }
  535. }
  536. }
  537. /*------------------------------ LSI Configuration -------------------------*/
  538. if(((RCC_OscInitStruct->OscillatorType) & RCC_OSCILLATORTYPE_LSI) == RCC_OSCILLATORTYPE_LSI)
  539. {
  540. /* Check the parameters */
  541. assert_param(IS_RCC_LSI(RCC_OscInitStruct->LSIState));
  542. /* Check the LSI State */
  543. if(RCC_OscInitStruct->LSIState != RCC_LSI_OFF)
  544. {
  545. /* Enable the Internal Low Speed oscillator (LSI). */
  546. __HAL_RCC_LSI_ENABLE();
  547. /* Get Start Tick*/
  548. tickstart = HAL_GetTick();
  549. /* Wait till LSI is ready */
  550. while(READ_BIT(RCC->CSR, RCC_CSR_LSIRDY) == RESET)
  551. {
  552. if((HAL_GetTick() - tickstart) > LSI_TIMEOUT_VALUE)
  553. {
  554. return HAL_TIMEOUT;
  555. }
  556. }
  557. }
  558. else
  559. {
  560. /* Disable the Internal Low Speed oscillator (LSI). */
  561. __HAL_RCC_LSI_DISABLE();
  562. /* Get Start Tick*/
  563. tickstart = HAL_GetTick();
  564. /* Wait till LSI is disabled */
  565. while(READ_BIT(RCC->CSR, RCC_CSR_LSIRDY) != RESET)
  566. {
  567. if((HAL_GetTick() - tickstart) > LSI_TIMEOUT_VALUE)
  568. {
  569. return HAL_TIMEOUT;
  570. }
  571. }
  572. }
  573. }
  574. /*------------------------------ LSE Configuration -------------------------*/
  575. if(((RCC_OscInitStruct->OscillatorType) & RCC_OSCILLATORTYPE_LSE) == RCC_OSCILLATORTYPE_LSE)
  576. {
  577. FlagStatus pwrclkchanged = RESET;
  578. /* Check the parameters */
  579. assert_param(IS_RCC_LSE(RCC_OscInitStruct->LSEState));
  580. /* Update LSE configuration in Backup Domain control register */
  581. /* Requires to enable write access to Backup Domain of necessary */
  582. if(HAL_IS_BIT_CLR(RCC->APB1ENR1, RCC_APB1ENR1_PWREN))
  583. {
  584. __HAL_RCC_PWR_CLK_ENABLE();
  585. pwrclkchanged = SET;
  586. }
  587. if(HAL_IS_BIT_CLR(PWR->CR1, PWR_CR1_DBP))
  588. {
  589. /* Enable write access to Backup domain */
  590. SET_BIT(PWR->CR1, PWR_CR1_DBP);
  591. /* Wait for Backup domain Write protection disable */
  592. tickstart = HAL_GetTick();
  593. while(HAL_IS_BIT_CLR(PWR->CR1, PWR_CR1_DBP))
  594. {
  595. if((HAL_GetTick() - tickstart) > RCC_DBP_TIMEOUT_VALUE)
  596. {
  597. return HAL_TIMEOUT;
  598. }
  599. }
  600. }
  601. /* Set the new LSE configuration -----------------------------------------*/
  602. __HAL_RCC_LSE_CONFIG(RCC_OscInitStruct->LSEState);
  603. /* Check the LSE State */
  604. if(RCC_OscInitStruct->LSEState != RCC_LSE_OFF)
  605. {
  606. /* Get Start Tick*/
  607. tickstart = HAL_GetTick();
  608. /* Wait till LSE is ready */
  609. while(READ_BIT(RCC->BDCR, RCC_BDCR_LSERDY) == RESET)
  610. {
  611. if((HAL_GetTick() - tickstart) > RCC_LSE_TIMEOUT_VALUE)
  612. {
  613. return HAL_TIMEOUT;
  614. }
  615. }
  616. }
  617. else
  618. {
  619. /* Get Start Tick*/
  620. tickstart = HAL_GetTick();
  621. /* Wait till LSE is disabled */
  622. while(READ_BIT(RCC->BDCR, RCC_BDCR_LSERDY) != RESET)
  623. {
  624. if((HAL_GetTick() - tickstart) > RCC_LSE_TIMEOUT_VALUE)
  625. {
  626. return HAL_TIMEOUT;
  627. }
  628. }
  629. }
  630. /* Restore clock configuration if changed */
  631. if(pwrclkchanged == SET)
  632. {
  633. __HAL_RCC_PWR_CLK_DISABLE();
  634. }
  635. }
  636. #if defined(RCC_HSI48_SUPPORT)
  637. /*------------------------------ HSI48 Configuration -----------------------*/
  638. if(((RCC_OscInitStruct->OscillatorType) & RCC_OSCILLATORTYPE_HSI48) == RCC_OSCILLATORTYPE_HSI48)
  639. {
  640. /* Check the parameters */
  641. assert_param(IS_RCC_HSI48(RCC_OscInitStruct->HSI48State));
  642. /* Check the LSI State */
  643. if(RCC_OscInitStruct->HSI48State != RCC_HSI48_OFF)
  644. {
  645. /* Enable the Internal Low Speed oscillator (HSI48). */
  646. __HAL_RCC_HSI48_ENABLE();
  647. /* Get Start Tick*/
  648. tickstart = HAL_GetTick();
  649. /* Wait till HSI48 is ready */
  650. while(READ_BIT(RCC->CRRCR, RCC_CRRCR_HSI48RDY) == RESET)
  651. {
  652. if((HAL_GetTick() - tickstart) > HSI48_TIMEOUT_VALUE)
  653. {
  654. return HAL_TIMEOUT;
  655. }
  656. }
  657. }
  658. else
  659. {
  660. /* Disable the Internal Low Speed oscillator (HSI48). */
  661. __HAL_RCC_HSI48_DISABLE();
  662. /* Get Start Tick*/
  663. tickstart = HAL_GetTick();
  664. /* Wait till HSI48 is disabled */
  665. while(READ_BIT(RCC->CRRCR, RCC_CRRCR_HSI48RDY) != RESET)
  666. {
  667. if((HAL_GetTick() - tickstart) > HSI48_TIMEOUT_VALUE)
  668. {
  669. return HAL_TIMEOUT;
  670. }
  671. }
  672. }
  673. }
  674. #endif /* RCC_HSI48_SUPPORT */
  675. /*-------------------------------- PLL Configuration -----------------------*/
  676. /* Check the parameters */
  677. assert_param(IS_RCC_PLL(RCC_OscInitStruct->PLL.PLLState));
  678. if(RCC_OscInitStruct->PLL.PLLState != RCC_PLL_NONE)
  679. {
  680. /* Check if the PLL is used as system clock or not */
  681. if(__HAL_RCC_GET_SYSCLK_SOURCE() != RCC_CFGR_SWS_PLL)
  682. {
  683. if(RCC_OscInitStruct->PLL.PLLState == RCC_PLL_ON)
  684. {
  685. /* Check the parameters */
  686. assert_param(IS_RCC_PLLSOURCE(RCC_OscInitStruct->PLL.PLLSource));
  687. assert_param(IS_RCC_PLLM_VALUE(RCC_OscInitStruct->PLL.PLLM));
  688. assert_param(IS_RCC_PLLN_VALUE(RCC_OscInitStruct->PLL.PLLN));
  689. assert_param(IS_RCC_PLLP_VALUE(RCC_OscInitStruct->PLL.PLLP));
  690. assert_param(IS_RCC_PLLQ_VALUE(RCC_OscInitStruct->PLL.PLLQ));
  691. assert_param(IS_RCC_PLLR_VALUE(RCC_OscInitStruct->PLL.PLLR));
  692. /* Disable the main PLL. */
  693. __HAL_RCC_PLL_DISABLE();
  694. /* Get Start Tick*/
  695. tickstart = HAL_GetTick();
  696. /* Wait till PLL is ready */
  697. while(READ_BIT(RCC->CR, RCC_CR_PLLRDY) != RESET)
  698. {
  699. if((HAL_GetTick() - tickstart) > PLL_TIMEOUT_VALUE)
  700. {
  701. return HAL_TIMEOUT;
  702. }
  703. }
  704. /* Configure the main PLL clock source, multiplication and division factors. */
  705. __HAL_RCC_PLL_CONFIG(RCC_OscInitStruct->PLL.PLLSource,
  706. RCC_OscInitStruct->PLL.PLLM,
  707. RCC_OscInitStruct->PLL.PLLN,
  708. RCC_OscInitStruct->PLL.PLLP,
  709. RCC_OscInitStruct->PLL.PLLQ,
  710. RCC_OscInitStruct->PLL.PLLR);
  711. /* Enable the main PLL. */
  712. __HAL_RCC_PLL_ENABLE();
  713. /* Enable PLL System Clock output. */
  714. __HAL_RCC_PLLCLKOUT_ENABLE(RCC_PLL_SYSCLK);
  715. /* Get Start Tick*/
  716. tickstart = HAL_GetTick();
  717. /* Wait till PLL is ready */
  718. while(READ_BIT(RCC->CR, RCC_CR_PLLRDY) == RESET)
  719. {
  720. if((HAL_GetTick() - tickstart) > PLL_TIMEOUT_VALUE)
  721. {
  722. return HAL_TIMEOUT;
  723. }
  724. }
  725. }
  726. else
  727. {
  728. /* Disable the main PLL. */
  729. __HAL_RCC_PLL_DISABLE();
  730. /* Disable all PLL outputs to save power if no PLLs on */
  731. if((READ_BIT(RCC->CR, RCC_CR_PLLSAI1RDY) == RESET)
  732. #if defined(RCC_PLLSAI2_SUPPORT)
  733. &&
  734. (READ_BIT(RCC->CR, RCC_CR_PLLSAI2RDY) == RESET)
  735. #endif /* RCC_PLLSAI2_SUPPORT */
  736. )
  737. {
  738. MODIFY_REG(RCC->PLLCFGR, RCC_PLLCFGR_PLLSRC, RCC_PLLSOURCE_NONE);
  739. }
  740. #if defined(RCC_PLLSAI2_SUPPORT)
  741. __HAL_RCC_PLLCLKOUT_DISABLE(RCC_PLL_SYSCLK | RCC_PLL_48M1CLK | RCC_PLL_SAI3CLK);
  742. #else
  743. __HAL_RCC_PLLCLKOUT_DISABLE(RCC_PLL_SYSCLK | RCC_PLL_48M1CLK | RCC_PLL_SAI2CLK);
  744. #endif /* RCC_PLLSAI2_SUPPORT */
  745. /* Get Start Tick*/
  746. tickstart = HAL_GetTick();
  747. /* Wait till PLL is disabled */
  748. while(READ_BIT(RCC->CR, RCC_CR_PLLRDY) != RESET)
  749. {
  750. if((HAL_GetTick() - tickstart) > PLL_TIMEOUT_VALUE)
  751. {
  752. return HAL_TIMEOUT;
  753. }
  754. }
  755. }
  756. }
  757. else
  758. {
  759. return HAL_ERROR;
  760. }
  761. }
  762. return HAL_OK;
  763. }
  764. /**
  765. * @brief Initialize the CPU, AHB and APB busses clocks according to the specified
  766. * parameters in the RCC_ClkInitStruct.
  767. * @param RCC_ClkInitStruct pointer to an RCC_OscInitTypeDef structure that
  768. * contains the configuration information for the RCC peripheral.
  769. * @param FLatency FLASH Latency
  770. * This parameter can be one of the following values:
  771. * @arg FLASH_LATENCY_0 FLASH 0 Latency cycle
  772. * @arg FLASH_LATENCY_1 FLASH 1 Latency cycle
  773. * @arg FLASH_LATENCY_2 FLASH 2 Latency cycles
  774. * @arg FLASH_LATENCY_3 FLASH 3 Latency cycles
  775. * @arg FLASH_LATENCY_4 FLASH 4 Latency cycles
  776. @if STM32L4S9xx
  777. * @arg FLASH_LATENCY_5 FLASH 5 Latency cycles
  778. * @arg FLASH_LATENCY_6 FLASH 6 Latency cycles
  779. * @arg FLASH_LATENCY_7 FLASH 7 Latency cycles
  780. * @arg FLASH_LATENCY_8 FLASH 8 Latency cycles
  781. * @arg FLASH_LATENCY_9 FLASH 9 Latency cycles
  782. * @arg FLASH_LATENCY_10 FLASH 10 Latency cycles
  783. * @arg FLASH_LATENCY_11 FLASH 11 Latency cycles
  784. * @arg FLASH_LATENCY_12 FLASH 12 Latency cycles
  785. * @arg FLASH_LATENCY_13 FLASH 13 Latency cycles
  786. * @arg FLASH_LATENCY_14 FLASH 14 Latency cycles
  787. * @arg FLASH_LATENCY_15 FLASH 15 Latency cycles
  788. @endif
  789. *
  790. * @note The SystemCoreClock CMSIS variable is used to store System Clock Frequency
  791. * and updated by HAL_RCC_GetHCLKFreq() function called within this function
  792. *
  793. * @note The MSI is used by default as system clock source after
  794. * startup from Reset, wake-up from STANDBY mode. After restart from Reset,
  795. * the MSI frequency is set to its default value 4 MHz.
  796. *
  797. * @note The HSI can be selected as system clock source after
  798. * from STOP modes or in case of failure of the HSE used directly or indirectly
  799. * as system clock (if the Clock Security System CSS is enabled).
  800. *
  801. * @note A switch from one clock source to another occurs only if the target
  802. * clock source is ready (clock stable after startup delay or PLL locked).
  803. * If a clock source which is not yet ready is selected, the switch will
  804. * occur when the clock source is ready.
  805. *
  806. * @note You can use HAL_RCC_GetClockConfig() function to know which clock is
  807. * currently used as system clock source.
  808. *
  809. * @note Depending on the device voltage range, the software has to set correctly
  810. * HPRE[3:0] bits to ensure that HCLK not exceed the maximum allowed frequency
  811. * (for more details refer to section above "Initialization/de-initialization functions")
  812. * @retval None
  813. */
  814. HAL_StatusTypeDef HAL_RCC_ClockConfig(RCC_ClkInitTypeDef *RCC_ClkInitStruct, uint32_t FLatency)
  815. {
  816. uint32_t tickstart = 0;
  817. #if defined(STM32L4R5xx) || defined(STM32L4R7xx) || defined(STM32L4R9xx) || defined(STM32L4S5xx) || defined(STM32L4S7xx) || defined(STM32L4S9xx)
  818. uint32_t pllfreq = 0;
  819. uint32_t hpre = RCC_SYSCLK_DIV1;
  820. #endif
  821. /* Check the parameters */
  822. assert_param(RCC_ClkInitStruct != NULL);
  823. assert_param(IS_RCC_CLOCKTYPE(RCC_ClkInitStruct->ClockType));
  824. assert_param(IS_FLASH_LATENCY(FLatency));
  825. /* To correctly read data from FLASH memory, the number of wait states (LATENCY)
  826. must be correctly programmed according to the frequency of the CPU clock
  827. (HCLK) and the supply voltage of the device. */
  828. /* Increasing the number of wait states because of higher CPU frequency */
  829. if(FLatency > READ_BIT(FLASH->ACR, FLASH_ACR_LATENCY))
  830. {
  831. /* Program the new number of wait states to the LATENCY bits in the FLASH_ACR register */
  832. __HAL_FLASH_SET_LATENCY(FLatency);
  833. /* Check that the new number of wait states is taken into account to access the Flash
  834. memory by reading the FLASH_ACR register */
  835. if(READ_BIT(FLASH->ACR, FLASH_ACR_LATENCY) != FLatency)
  836. {
  837. return HAL_ERROR;
  838. }
  839. }
  840. /*------------------------- SYSCLK Configuration ---------------------------*/
  841. if(((RCC_ClkInitStruct->ClockType) & RCC_CLOCKTYPE_SYSCLK) == RCC_CLOCKTYPE_SYSCLK)
  842. {
  843. assert_param(IS_RCC_SYSCLKSOURCE(RCC_ClkInitStruct->SYSCLKSource));
  844. /* PLL is selected as System Clock Source */
  845. if(RCC_ClkInitStruct->SYSCLKSource == RCC_SYSCLKSOURCE_PLLCLK)
  846. {
  847. /* Check the PLL ready flag */
  848. if(READ_BIT(RCC->CR, RCC_CR_PLLRDY) == RESET)
  849. {
  850. return HAL_ERROR;
  851. }
  852. #if defined(STM32L4R5xx) || defined(STM32L4R7xx) || defined(STM32L4R9xx) || defined(STM32L4S5xx) || defined(STM32L4S7xx) || defined(STM32L4S9xx)
  853. /* Undershoot management when selection PLL as SYSCLK source and frequency above 80Mhz */
  854. /* Compute target PLL output frequency */
  855. pllfreq = RCC_GetSysClockFreqFromPLLSource();
  856. /* Intermediate step with HCLK prescaler 2 necessary before to go over 80Mhz */
  857. if((pllfreq > 80000000U) &&
  858. (((((RCC_ClkInitStruct->ClockType) & RCC_CLOCKTYPE_HCLK) == RCC_CLOCKTYPE_HCLK) && (RCC_ClkInitStruct->AHBCLKDivider == RCC_SYSCLK_DIV1))
  859. ||
  860. ((READ_BIT(RCC->CFGR, RCC_CFGR_HPRE) == RCC_SYSCLK_DIV1))))
  861. {
  862. MODIFY_REG(RCC->CFGR, RCC_CFGR_HPRE, RCC_SYSCLK_DIV2);
  863. hpre = RCC_SYSCLK_DIV2;
  864. }
  865. #endif
  866. }
  867. else
  868. {
  869. /* HSE is selected as System Clock Source */
  870. if(RCC_ClkInitStruct->SYSCLKSource == RCC_SYSCLKSOURCE_HSE)
  871. {
  872. /* Check the HSE ready flag */
  873. if(READ_BIT(RCC->CR, RCC_CR_HSERDY) == RESET)
  874. {
  875. return HAL_ERROR;
  876. }
  877. }
  878. /* MSI is selected as System Clock Source */
  879. else if(RCC_ClkInitStruct->SYSCLKSource == RCC_SYSCLKSOURCE_MSI)
  880. {
  881. /* Check the MSI ready flag */
  882. if(READ_BIT(RCC->CR, RCC_CR_MSIRDY) == RESET)
  883. {
  884. return HAL_ERROR;
  885. }
  886. }
  887. /* HSI is selected as System Clock Source */
  888. else
  889. {
  890. /* Check the HSI ready flag */
  891. if(READ_BIT(RCC->CR, RCC_CR_HSIRDY) == RESET)
  892. {
  893. return HAL_ERROR;
  894. }
  895. }
  896. #if defined(STM32L4R5xx) || defined(STM32L4R7xx) || defined(STM32L4R9xx) || defined(STM32L4S5xx) || defined(STM32L4S7xx) || defined(STM32L4S9xx)
  897. /* Overshoot management when going down from PLL as SYSCLK source and frequency above 80Mhz */
  898. pllfreq = HAL_RCC_GetSysClockFreq();
  899. /* Intermediate step with HCLK prescaler 2 necessary before to go under 80Mhz */
  900. if(pllfreq > 80000000U)
  901. {
  902. MODIFY_REG(RCC->CFGR, RCC_CFGR_HPRE, RCC_SYSCLK_DIV2);
  903. hpre = RCC_SYSCLK_DIV2;
  904. }
  905. #endif
  906. }
  907. MODIFY_REG(RCC->CFGR, RCC_CFGR_SW, RCC_ClkInitStruct->SYSCLKSource);
  908. /* Get Start Tick*/
  909. tickstart = HAL_GetTick();
  910. if(RCC_ClkInitStruct->SYSCLKSource == RCC_SYSCLKSOURCE_PLLCLK)
  911. {
  912. while (__HAL_RCC_GET_SYSCLK_SOURCE() != RCC_CFGR_SWS_PLL)
  913. {
  914. if((HAL_GetTick() - tickstart) > CLOCKSWITCH_TIMEOUT_VALUE)
  915. {
  916. return HAL_TIMEOUT;
  917. }
  918. }
  919. }
  920. else
  921. {
  922. if(RCC_ClkInitStruct->SYSCLKSource == RCC_SYSCLKSOURCE_HSE)
  923. {
  924. while (__HAL_RCC_GET_SYSCLK_SOURCE() != RCC_CFGR_SWS_HSE)
  925. {
  926. if((HAL_GetTick() - tickstart) > CLOCKSWITCH_TIMEOUT_VALUE)
  927. {
  928. return HAL_TIMEOUT;
  929. }
  930. }
  931. }
  932. else if(RCC_ClkInitStruct->SYSCLKSource == RCC_SYSCLKSOURCE_MSI)
  933. {
  934. while (__HAL_RCC_GET_SYSCLK_SOURCE() != RCC_CFGR_SWS_MSI)
  935. {
  936. if((HAL_GetTick() - tickstart) > CLOCKSWITCH_TIMEOUT_VALUE)
  937. {
  938. return HAL_TIMEOUT;
  939. }
  940. }
  941. }
  942. else
  943. {
  944. while(__HAL_RCC_GET_SYSCLK_SOURCE() != RCC_CFGR_SWS_HSI)
  945. {
  946. if((HAL_GetTick() - tickstart) > CLOCKSWITCH_TIMEOUT_VALUE)
  947. {
  948. return HAL_TIMEOUT;
  949. }
  950. }
  951. }
  952. }
  953. }
  954. /*-------------------------- HCLK Configuration --------------------------*/
  955. if(((RCC_ClkInitStruct->ClockType) & RCC_CLOCKTYPE_HCLK) == RCC_CLOCKTYPE_HCLK)
  956. {
  957. assert_param(IS_RCC_HCLK(RCC_ClkInitStruct->AHBCLKDivider));
  958. MODIFY_REG(RCC->CFGR, RCC_CFGR_HPRE, RCC_ClkInitStruct->AHBCLKDivider);
  959. }
  960. #if defined(STM32L4R5xx) || defined(STM32L4R7xx) || defined(STM32L4R9xx) || defined(STM32L4S5xx) || defined(STM32L4S7xx) || defined(STM32L4S9xx)
  961. else
  962. {
  963. /* Is intermediate HCLK prescaler 2 applied internally, complete with HCLK prescaler 1 */
  964. if(hpre == RCC_SYSCLK_DIV2)
  965. {
  966. MODIFY_REG(RCC->CFGR, RCC_CFGR_HPRE, RCC_SYSCLK_DIV1);
  967. }
  968. }
  969. #endif
  970. /* Decreasing the number of wait states because of lower CPU frequency */
  971. if(FLatency < READ_BIT(FLASH->ACR, FLASH_ACR_LATENCY))
  972. {
  973. /* Program the new number of wait states to the LATENCY bits in the FLASH_ACR register */
  974. __HAL_FLASH_SET_LATENCY(FLatency);
  975. /* Check that the new number of wait states is taken into account to access the Flash
  976. memory by reading the FLASH_ACR register */
  977. if(READ_BIT(FLASH->ACR, FLASH_ACR_LATENCY) != FLatency)
  978. {
  979. return HAL_ERROR;
  980. }
  981. }
  982. /*-------------------------- PCLK1 Configuration ---------------------------*/
  983. if(((RCC_ClkInitStruct->ClockType) & RCC_CLOCKTYPE_PCLK1) == RCC_CLOCKTYPE_PCLK1)
  984. {
  985. assert_param(IS_RCC_PCLK(RCC_ClkInitStruct->APB1CLKDivider));
  986. MODIFY_REG(RCC->CFGR, RCC_CFGR_PPRE1, RCC_ClkInitStruct->APB1CLKDivider);
  987. }
  988. /*-------------------------- PCLK2 Configuration ---------------------------*/
  989. if(((RCC_ClkInitStruct->ClockType) & RCC_CLOCKTYPE_PCLK2) == RCC_CLOCKTYPE_PCLK2)
  990. {
  991. assert_param(IS_RCC_PCLK(RCC_ClkInitStruct->APB2CLKDivider));
  992. MODIFY_REG(RCC->CFGR, RCC_CFGR_PPRE2, ((RCC_ClkInitStruct->APB2CLKDivider) << 3U));
  993. }
  994. /* Update the SystemCoreClock global variable */
  995. SystemCoreClock = HAL_RCC_GetSysClockFreq() >> AHBPrescTable[READ_BIT(RCC->CFGR, RCC_CFGR_HPRE) >> RCC_CFGR_HPRE_Pos];
  996. /* Configure the source of time base considering new system clocks settings*/
  997. HAL_InitTick (TICK_INT_PRIORITY);
  998. return HAL_OK;
  999. }
  1000. /**
  1001. * @}
  1002. */
  1003. /** @defgroup RCC_Exported_Functions_Group2 Peripheral Control functions
  1004. * @brief RCC clocks control functions
  1005. *
  1006. @verbatim
  1007. ===============================================================================
  1008. ##### Peripheral Control functions #####
  1009. ===============================================================================
  1010. [..]
  1011. This subsection provides a set of functions allowing to:
  1012. (+) Ouput clock to MCO pin.
  1013. (+) Retrieve current clock frequencies.
  1014. (+) Enable the Clock Security System.
  1015. @endverbatim
  1016. * @{
  1017. */
  1018. /**
  1019. * @brief Select the clock source to output on MCO pin(PA8).
  1020. * @note PA8 should be configured in alternate function mode.
  1021. * @param RCC_MCOx specifies the output direction for the clock source.
  1022. * For STM32L4xx family this parameter can have only one value:
  1023. * @arg @ref RCC_MCO1 Clock source to output on MCO1 pin(PA8).
  1024. * @param RCC_MCOSource specifies the clock source to output.
  1025. * This parameter can be one of the following values:
  1026. * @arg @ref RCC_MCO1SOURCE_NOCLOCK MCO output disabled, no clock on MCO
  1027. * @arg @ref RCC_MCO1SOURCE_SYSCLK system clock selected as MCO source
  1028. * @arg @ref RCC_MCO1SOURCE_MSI MSI clock selected as MCO source
  1029. * @arg @ref RCC_MCO1SOURCE_HSI HSI clock selected as MCO source
  1030. * @arg @ref RCC_MCO1SOURCE_HSE HSE clock selected as MCO sourcee
  1031. * @arg @ref RCC_MCO1SOURCE_PLLCLK main PLL clock selected as MCO source
  1032. * @arg @ref RCC_MCO1SOURCE_LSI LSI clock selected as MCO source
  1033. * @arg @ref RCC_MCO1SOURCE_LSE LSE clock selected as MCO source
  1034. @if STM32L443xx
  1035. * @arg @ref RCC_MCO1SOURCE_HSI48 HSI48 clock selected as MCO source for devices with HSI48
  1036. @endif
  1037. * @param RCC_MCODiv specifies the MCO prescaler.
  1038. * This parameter can be one of the following values:
  1039. * @arg @ref RCC_MCODIV_1 no division applied to MCO clock
  1040. * @arg @ref RCC_MCODIV_2 division by 2 applied to MCO clock
  1041. * @arg @ref RCC_MCODIV_4 division by 4 applied to MCO clock
  1042. * @arg @ref RCC_MCODIV_8 division by 8 applied to MCO clock
  1043. * @arg @ref RCC_MCODIV_16 division by 16 applied to MCO clock
  1044. * @retval None
  1045. */
  1046. void HAL_RCC_MCOConfig( uint32_t RCC_MCOx, uint32_t RCC_MCOSource, uint32_t RCC_MCODiv)
  1047. {
  1048. GPIO_InitTypeDef GPIO_InitStruct;
  1049. /* Check the parameters */
  1050. assert_param(IS_RCC_MCO(RCC_MCOx));
  1051. assert_param(IS_RCC_MCODIV(RCC_MCODiv));
  1052. assert_param(IS_RCC_MCO1SOURCE(RCC_MCOSource));
  1053. /* MCO Clock Enable */
  1054. __MCO1_CLK_ENABLE();
  1055. /* Configue the MCO1 pin in alternate function mode */
  1056. GPIO_InitStruct.Pin = MCO1_PIN;
  1057. GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
  1058. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
  1059. GPIO_InitStruct.Pull = GPIO_NOPULL;
  1060. GPIO_InitStruct.Alternate = GPIO_AF0_MCO;
  1061. HAL_GPIO_Init(MCO1_GPIO_PORT, &GPIO_InitStruct);
  1062. /* Mask MCOSEL[] and MCOPRE[] bits then set MCO1 clock source and prescaler */
  1063. MODIFY_REG(RCC->CFGR, (RCC_CFGR_MCOSEL | RCC_CFGR_MCOPRE), (RCC_MCOSource | RCC_MCODiv ));
  1064. }
  1065. /**
  1066. * @brief Return the SYSCLK frequency.
  1067. *
  1068. * @note The system frequency computed by this function is not the real
  1069. * frequency in the chip. It is calculated based on the predefined
  1070. * constant and the selected clock source:
  1071. * @note If SYSCLK source is MSI, function returns values based on MSI
  1072. * Value as defined by the MSI range.
  1073. * @note If SYSCLK source is HSI, function returns values based on HSI_VALUE(*)
  1074. * @note If SYSCLK source is HSE, function returns values based on HSE_VALUE(**)
  1075. * @note If SYSCLK source is PLL, function returns values based on HSE_VALUE(**),
  1076. * HSI_VALUE(*) or MSI Value multiplied/divided by the PLL factors.
  1077. * @note (*) HSI_VALUE is a constant defined in stm32l4xx_hal_conf.h file (default value
  1078. * 16 MHz) but the real value may vary depending on the variations
  1079. * in voltage and temperature.
  1080. * @note (**) HSE_VALUE is a constant defined in stm32l4xx_hal_conf.h file (default value
  1081. * 8 MHz), user has to ensure that HSE_VALUE is same as the real
  1082. * frequency of the crystal used. Otherwise, this function may
  1083. * have wrong result.
  1084. *
  1085. * @note The result of this function could be not correct when using fractional
  1086. * value for HSE crystal.
  1087. *
  1088. * @note This function can be used by the user application to compute the
  1089. * baudrate for the communication peripherals or configure other parameters.
  1090. *
  1091. * @note Each time SYSCLK changes, this function must be called to update the
  1092. * right SYSCLK value. Otherwise, any configuration based on this function will be incorrect.
  1093. *
  1094. *
  1095. * @retval SYSCLK frequency
  1096. */
  1097. uint32_t HAL_RCC_GetSysClockFreq(void)
  1098. {
  1099. uint32_t msirange = 0U, pllvco = 0U, pllsource = 0U, pllr = 2U, pllm = 2U;
  1100. uint32_t sysclockfreq = 0U;
  1101. if((__HAL_RCC_GET_SYSCLK_SOURCE() == RCC_CFGR_SWS_MSI) ||
  1102. ((__HAL_RCC_GET_SYSCLK_SOURCE() == RCC_CFGR_SWS_PLL) && (__HAL_RCC_GET_PLL_OSCSOURCE() == RCC_PLLSOURCE_MSI)))
  1103. {
  1104. /* MSI or PLL with MSI source used as system clock source */
  1105. /* Get SYSCLK source */
  1106. if(READ_BIT(RCC->CR, RCC_CR_MSIRGSEL) == RESET)
  1107. { /* MSISRANGE from RCC_CSR applies */
  1108. msirange = READ_BIT(RCC->CSR, RCC_CSR_MSISRANGE) >> RCC_CSR_MSISRANGE_Pos;
  1109. }
  1110. else
  1111. { /* MSIRANGE from RCC_CR applies */
  1112. msirange = READ_BIT(RCC->CR, RCC_CR_MSIRANGE) >> RCC_CR_MSIRANGE_Pos;
  1113. }
  1114. /*MSI frequency range in HZ*/
  1115. msirange = MSIRangeTable[msirange];
  1116. if(__HAL_RCC_GET_SYSCLK_SOURCE() == RCC_CFGR_SWS_MSI)
  1117. {
  1118. /* MSI used as system clock source */
  1119. sysclockfreq = msirange;
  1120. }
  1121. }
  1122. else if(__HAL_RCC_GET_SYSCLK_SOURCE() == RCC_CFGR_SWS_HSI)
  1123. {
  1124. /* HSI used as system clock source */
  1125. sysclockfreq = HSI_VALUE;
  1126. }
  1127. else if(__HAL_RCC_GET_SYSCLK_SOURCE() == RCC_CFGR_SWS_HSE)
  1128. {
  1129. /* HSE used as system clock source */
  1130. sysclockfreq = HSE_VALUE;
  1131. }
  1132. if(__HAL_RCC_GET_SYSCLK_SOURCE() == RCC_CFGR_SWS_PLL)
  1133. {
  1134. /* PLL used as system clock source */
  1135. /* PLL_VCO = (HSE_VALUE or HSI_VALUE or MSI_VALUE/ PLLM) * PLLN
  1136. SYSCLK = PLL_VCO / PLLR
  1137. */
  1138. pllsource = READ_BIT(RCC->PLLCFGR, RCC_PLLCFGR_PLLSRC);
  1139. pllm = (READ_BIT(RCC->PLLCFGR, RCC_PLLCFGR_PLLM) >> RCC_PLLCFGR_PLLM_Pos) + 1U ;
  1140. switch (pllsource)
  1141. {
  1142. case RCC_PLLSOURCE_HSI: /* HSI used as PLL clock source */
  1143. pllvco = (HSI_VALUE / pllm) * (READ_BIT(RCC->PLLCFGR, RCC_PLLCFGR_PLLN) >> RCC_PLLCFGR_PLLN_Pos);
  1144. break;
  1145. case RCC_PLLSOURCE_HSE: /* HSE used as PLL clock source */
  1146. pllvco = (HSE_VALUE / pllm) * (READ_BIT(RCC->PLLCFGR, RCC_PLLCFGR_PLLN) >> RCC_PLLCFGR_PLLN_Pos);
  1147. break;
  1148. case RCC_PLLSOURCE_MSI: /* MSI used as PLL clock source */
  1149. default:
  1150. pllvco = (msirange / pllm) * (READ_BIT(RCC->PLLCFGR, RCC_PLLCFGR_PLLN) >> RCC_PLLCFGR_PLLN_Pos);
  1151. break;
  1152. }
  1153. pllr = ((READ_BIT(RCC->PLLCFGR, RCC_PLLCFGR_PLLR) >> RCC_PLLCFGR_PLLR_Pos) + 1U ) * 2U;
  1154. sysclockfreq = pllvco/pllr;
  1155. }
  1156. return sysclockfreq;
  1157. }
  1158. /**
  1159. * @brief Return the HCLK frequency.
  1160. * @note Each time HCLK changes, this function must be called to update the
  1161. * right HCLK value. Otherwise, any configuration based on this function will be incorrect.
  1162. *
  1163. * @note The SystemCoreClock CMSIS variable is used to store System Clock Frequency.
  1164. * @retval HCLK frequency in Hz
  1165. */
  1166. uint32_t HAL_RCC_GetHCLKFreq(void)
  1167. {
  1168. return SystemCoreClock;
  1169. }
  1170. /**
  1171. * @brief Return the PCLK1 frequency.
  1172. * @note Each time PCLK1 changes, this function must be called to update the
  1173. * right PCLK1 value. Otherwise, any configuration based on this function will be incorrect.
  1174. * @retval PCLK1 frequency in Hz
  1175. */
  1176. uint32_t HAL_RCC_GetPCLK1Freq(void)
  1177. {
  1178. /* Get HCLK source and Compute PCLK1 frequency ---------------------------*/
  1179. return (HAL_RCC_GetHCLKFreq() >> APBPrescTable[READ_BIT(RCC->CFGR, RCC_CFGR_PPRE1) >> RCC_CFGR_PPRE1_Pos]);
  1180. }
  1181. /**
  1182. * @brief Return the PCLK2 frequency.
  1183. * @note Each time PCLK2 changes, this function must be called to update the
  1184. * right PCLK2 value. Otherwise, any configuration based on this function will be incorrect.
  1185. * @retval PCLK2 frequency in Hz
  1186. */
  1187. uint32_t HAL_RCC_GetPCLK2Freq(void)
  1188. {
  1189. /* Get HCLK source and Compute PCLK2 frequency ---------------------------*/
  1190. return (HAL_RCC_GetHCLKFreq()>> APBPrescTable[READ_BIT(RCC->CFGR, RCC_CFGR_PPRE2) >> RCC_CFGR_PPRE2_Pos]);
  1191. }
  1192. /**
  1193. * @brief Configure the RCC_OscInitStruct according to the internal
  1194. * RCC configuration registers.
  1195. * @param RCC_OscInitStruct pointer to an RCC_OscInitTypeDef structure that
  1196. * will be configured.
  1197. * @retval None
  1198. */
  1199. void HAL_RCC_GetOscConfig(RCC_OscInitTypeDef *RCC_OscInitStruct)
  1200. {
  1201. /* Check the parameters */
  1202. assert_param(RCC_OscInitStruct != NULL);
  1203. /* Set all possible values for the Oscillator type parameter ---------------*/
  1204. #if defined(RCC_HSI48_SUPPORT)
  1205. RCC_OscInitStruct->OscillatorType = RCC_OSCILLATORTYPE_HSE | RCC_OSCILLATORTYPE_HSI | RCC_OSCILLATORTYPE_MSI | \
  1206. RCC_OSCILLATORTYPE_LSE | RCC_OSCILLATORTYPE_LSI | RCC_OSCILLATORTYPE_HSI48;
  1207. #else
  1208. RCC_OscInitStruct->OscillatorType = RCC_OSCILLATORTYPE_HSE | RCC_OSCILLATORTYPE_HSI | RCC_OSCILLATORTYPE_MSI | \
  1209. RCC_OSCILLATORTYPE_LSE | RCC_OSCILLATORTYPE_LSI;
  1210. #endif /* RCC_HSI48_SUPPORT */
  1211. /* Get the HSE configuration -----------------------------------------------*/
  1212. if(READ_BIT(RCC->CR, RCC_CR_HSEBYP) == RCC_CR_HSEBYP)
  1213. {
  1214. RCC_OscInitStruct->HSEState = RCC_HSE_BYPASS;
  1215. }
  1216. else if(READ_BIT(RCC->CR, RCC_CR_HSEON) == RCC_CR_HSEON)
  1217. {
  1218. RCC_OscInitStruct->HSEState = RCC_HSE_ON;
  1219. }
  1220. else
  1221. {
  1222. RCC_OscInitStruct->HSEState = RCC_HSE_OFF;
  1223. }
  1224. /* Get the MSI configuration -----------------------------------------------*/
  1225. if(READ_BIT(RCC->CR, RCC_CR_MSION) == RCC_CR_MSION)
  1226. {
  1227. RCC_OscInitStruct->MSIState = RCC_MSI_ON;
  1228. }
  1229. else
  1230. {
  1231. RCC_OscInitStruct->MSIState = RCC_MSI_OFF;
  1232. }
  1233. RCC_OscInitStruct->MSICalibrationValue = READ_BIT(RCC->ICSCR, RCC_ICSCR_MSITRIM) >> RCC_ICSCR_MSITRIM_Pos;
  1234. RCC_OscInitStruct->MSIClockRange = READ_BIT(RCC->CR, RCC_CR_MSIRANGE);
  1235. /* Get the HSI configuration -----------------------------------------------*/
  1236. if(READ_BIT(RCC->CR, RCC_CR_HSION) == RCC_CR_HSION)
  1237. {
  1238. RCC_OscInitStruct->HSIState = RCC_HSI_ON;
  1239. }
  1240. else
  1241. {
  1242. RCC_OscInitStruct->HSIState = RCC_HSI_OFF;
  1243. }
  1244. RCC_OscInitStruct->HSICalibrationValue = READ_BIT(RCC->ICSCR, RCC_ICSCR_HSITRIM) >> RCC_ICSCR_HSITRIM_Pos;
  1245. /* Get the LSE configuration -----------------------------------------------*/
  1246. if(READ_BIT(RCC->BDCR, RCC_BDCR_LSEBYP) == RCC_BDCR_LSEBYP)
  1247. {
  1248. RCC_OscInitStruct->LSEState = RCC_LSE_BYPASS;
  1249. }
  1250. else if(READ_BIT(RCC->BDCR, RCC_BDCR_LSEON) == RCC_BDCR_LSEON)
  1251. {
  1252. RCC_OscInitStruct->LSEState = RCC_LSE_ON;
  1253. }
  1254. else
  1255. {
  1256. RCC_OscInitStruct->LSEState = RCC_LSE_OFF;
  1257. }
  1258. /* Get the LSI configuration -----------------------------------------------*/
  1259. if(READ_BIT(RCC->CSR, RCC_CSR_LSION) == RCC_CSR_LSION)
  1260. {
  1261. RCC_OscInitStruct->LSIState = RCC_LSI_ON;
  1262. }
  1263. else
  1264. {
  1265. RCC_OscInitStruct->LSIState = RCC_LSI_OFF;
  1266. }
  1267. #if defined(RCC_HSI48_SUPPORT)
  1268. /* Get the HSI48 configuration ---------------------------------------------*/
  1269. if(READ_BIT(RCC->CRRCR, RCC_CRRCR_HSI48ON) == RCC_CRRCR_HSI48ON)
  1270. {
  1271. RCC_OscInitStruct->HSI48State = RCC_HSI48_ON;
  1272. }
  1273. else
  1274. {
  1275. RCC_OscInitStruct->HSI48State = RCC_HSI48_OFF;
  1276. }
  1277. #else
  1278. RCC_OscInitStruct->HSI48State = RCC_HSI48_OFF;
  1279. #endif /* RCC_HSI48_SUPPORT */
  1280. /* Get the PLL configuration -----------------------------------------------*/
  1281. if(READ_BIT(RCC->CR, RCC_CR_PLLON) == RCC_CR_PLLON)
  1282. {
  1283. RCC_OscInitStruct->PLL.PLLState = RCC_PLL_ON;
  1284. }
  1285. else
  1286. {
  1287. RCC_OscInitStruct->PLL.PLLState = RCC_PLL_OFF;
  1288. }
  1289. RCC_OscInitStruct->PLL.PLLSource = READ_BIT(RCC->PLLCFGR, RCC_PLLCFGR_PLLSRC);
  1290. RCC_OscInitStruct->PLL.PLLM = (READ_BIT(RCC->PLLCFGR, RCC_PLLCFGR_PLLM) >> RCC_PLLCFGR_PLLM_Pos) + 1U;
  1291. RCC_OscInitStruct->PLL.PLLN = READ_BIT(RCC->PLLCFGR, RCC_PLLCFGR_PLLN) >> RCC_PLLCFGR_PLLN_Pos;
  1292. RCC_OscInitStruct->PLL.PLLQ = (((READ_BIT(RCC->PLLCFGR, RCC_PLLCFGR_PLLQ) >> RCC_PLLCFGR_PLLQ_Pos) + 1U) << 1U);
  1293. RCC_OscInitStruct->PLL.PLLR = (((READ_BIT(RCC->PLLCFGR, RCC_PLLCFGR_PLLR) >> RCC_PLLCFGR_PLLR_Pos) + 1U) << 1U);
  1294. #if defined(RCC_PLLP_DIV_2_31_SUPPORT)
  1295. RCC_OscInitStruct->PLL.PLLP = READ_BIT(RCC->PLLCFGR, RCC_PLLCFGR_PLLPDIV) >> RCC_PLLCFGR_PLLPDIV_Pos;
  1296. #else
  1297. if(READ_BIT(RCC->PLLCFGR, RCC_PLLCFGR_PLLP) != RESET)
  1298. {
  1299. RCC_OscInitStruct->PLL.PLLP = RCC_PLLP_DIV17;
  1300. }
  1301. else
  1302. {
  1303. RCC_OscInitStruct->PLL.PLLP = RCC_PLLP_DIV7;
  1304. }
  1305. #endif /* RCC_PLLP_DIV_2_31_SUPPORT */
  1306. }
  1307. /**
  1308. * @brief Configure the RCC_ClkInitStruct according to the internal
  1309. * RCC configuration registers.
  1310. * @param RCC_ClkInitStruct pointer to an RCC_ClkInitTypeDef structure that
  1311. * will be configured.
  1312. * @param pFLatency Pointer on the Flash Latency.
  1313. * @retval None
  1314. */
  1315. void HAL_RCC_GetClockConfig(RCC_ClkInitTypeDef *RCC_ClkInitStruct, uint32_t *pFLatency)
  1316. {
  1317. /* Check the parameters */
  1318. assert_param(RCC_ClkInitStruct != NULL);
  1319. assert_param(pFLatency != NULL);
  1320. /* Set all possible values for the Clock type parameter --------------------*/
  1321. RCC_ClkInitStruct->ClockType = RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2;
  1322. /* Get the SYSCLK configuration --------------------------------------------*/
  1323. RCC_ClkInitStruct->SYSCLKSource = READ_BIT(RCC->CFGR, RCC_CFGR_SW);
  1324. /* Get the HCLK configuration ----------------------------------------------*/
  1325. RCC_ClkInitStruct->AHBCLKDivider = READ_BIT(RCC->CFGR, RCC_CFGR_HPRE);
  1326. /* Get the APB1 configuration ----------------------------------------------*/
  1327. RCC_ClkInitStruct->APB1CLKDivider = READ_BIT(RCC->CFGR, RCC_CFGR_PPRE1);
  1328. /* Get the APB2 configuration ----------------------------------------------*/
  1329. RCC_ClkInitStruct->APB2CLKDivider = (READ_BIT(RCC->CFGR, RCC_CFGR_PPRE2) >> 3U);
  1330. /* Get the Flash Wait State (Latency) configuration ------------------------*/
  1331. *pFLatency = READ_BIT(FLASH->ACR, FLASH_ACR_LATENCY);
  1332. }
  1333. /**
  1334. * @brief Enable the Clock Security System.
  1335. * @note If a failure is detected on the HSE oscillator clock, this oscillator
  1336. * is automatically disabled and an interrupt is generated to inform the
  1337. * software about the failure (Clock Security System Interrupt, CSSI),
  1338. * allowing the MCU to perform rescue operations. The CSSI is linked to
  1339. * the Cortex-M4 NMI (Non-Maskable Interrupt) exception vector.
  1340. * @note The Clock Security System can only be cleared by reset.
  1341. * @retval None
  1342. */
  1343. void HAL_RCC_EnableCSS(void)
  1344. {
  1345. SET_BIT(RCC->CR, RCC_CR_CSSON) ;
  1346. }
  1347. /**
  1348. * @brief Handle the RCC Clock Security System interrupt request.
  1349. * @note This API should be called under the NMI_Handler().
  1350. * @retval None
  1351. */
  1352. void HAL_RCC_NMI_IRQHandler(void)
  1353. {
  1354. /* Check RCC CSSF interrupt flag */
  1355. if(__HAL_RCC_GET_IT(RCC_IT_CSS))
  1356. {
  1357. /* RCC Clock Security System interrupt user callback */
  1358. HAL_RCC_CSSCallback();
  1359. /* Clear RCC CSS pending bit */
  1360. __HAL_RCC_CLEAR_IT(RCC_IT_CSS);
  1361. }
  1362. }
  1363. /**
  1364. * @brief RCC Clock Security System interrupt callback.
  1365. * @retval none
  1366. */
  1367. __weak void HAL_RCC_CSSCallback(void)
  1368. {
  1369. /* NOTE : This function should not be modified, when the callback is needed,
  1370. the HAL_RCC_CSSCallback should be implemented in the user file
  1371. */
  1372. }
  1373. /**
  1374. * @}
  1375. */
  1376. /**
  1377. * @}
  1378. */
  1379. /* Private function prototypes -----------------------------------------------*/
  1380. /** @addtogroup RCC_Private_Functions
  1381. * @{
  1382. */
  1383. /**
  1384. * @brief Update number of Flash wait states in line with MSI range and current
  1385. voltage range.
  1386. * @param msirange MSI range value from RCC_MSIRANGE_0 to RCC_MSIRANGE_11
  1387. * @retval HAL status
  1388. */
  1389. static HAL_StatusTypeDef RCC_SetFlashLatencyFromMSIRange(uint32_t msirange)
  1390. {
  1391. uint32_t vos = 0;
  1392. uint32_t latency = FLASH_LATENCY_0; /* default value 0WS */
  1393. if(__HAL_RCC_PWR_IS_CLK_ENABLED())
  1394. {
  1395. vos = HAL_PWREx_GetVoltageRange();
  1396. }
  1397. else
  1398. {
  1399. __HAL_RCC_PWR_CLK_ENABLE();
  1400. vos = HAL_PWREx_GetVoltageRange();
  1401. __HAL_RCC_PWR_CLK_DISABLE();
  1402. }
  1403. if(vos == PWR_REGULATOR_VOLTAGE_SCALE1)
  1404. {
  1405. if(msirange > RCC_MSIRANGE_8)
  1406. {
  1407. /* MSI > 16Mhz */
  1408. if(msirange > RCC_MSIRANGE_10)
  1409. {
  1410. /* MSI 48Mhz */
  1411. latency = FLASH_LATENCY_2; /* 2WS */
  1412. }
  1413. else
  1414. {
  1415. /* MSI 24Mhz or 32Mhz */
  1416. latency = FLASH_LATENCY_1; /* 1WS */
  1417. }
  1418. }
  1419. /* else MSI <= 16Mhz default FLASH_LATENCY_0 0WS */
  1420. }
  1421. else
  1422. {
  1423. #if defined(STM32L4R5xx) || defined(STM32L4R7xx) || defined(STM32L4R9xx) || defined(STM32L4S5xx) || defined(STM32L4S7xx) || defined(STM32L4S9xx)
  1424. if(msirange >= RCC_MSIRANGE_8)
  1425. {
  1426. /* MSI >= 16Mhz */
  1427. latency = FLASH_LATENCY_2; /* 2WS */
  1428. }
  1429. else
  1430. {
  1431. if(msirange == RCC_MSIRANGE_7)
  1432. {
  1433. /* MSI 8Mhz */
  1434. latency = FLASH_LATENCY_1; /* 1WS */
  1435. }
  1436. /* else MSI < 8Mhz default FLASH_LATENCY_0 0WS */
  1437. }
  1438. #else
  1439. if(msirange > RCC_MSIRANGE_8)
  1440. {
  1441. /* MSI > 16Mhz */
  1442. latency = FLASH_LATENCY_3; /* 3WS */
  1443. }
  1444. else
  1445. {
  1446. if(msirange == RCC_MSIRANGE_8)
  1447. {
  1448. /* MSI 16Mhz */
  1449. latency = FLASH_LATENCY_2; /* 2WS */
  1450. }
  1451. else if(msirange == RCC_MSIRANGE_7)
  1452. {
  1453. /* MSI 8Mhz */
  1454. latency = FLASH_LATENCY_1; /* 1WS */
  1455. }
  1456. /* else MSI < 8Mhz default FLASH_LATENCY_0 0WS */
  1457. }
  1458. #endif
  1459. }
  1460. __HAL_FLASH_SET_LATENCY(latency);
  1461. /* Check that the new number of wait states is taken into account to access the Flash
  1462. memory by reading the FLASH_ACR register */
  1463. if(READ_BIT(FLASH->ACR, FLASH_ACR_LATENCY) != latency)
  1464. {
  1465. return HAL_ERROR;
  1466. }
  1467. return HAL_OK;
  1468. }
  1469. #if defined(STM32L4R5xx) || defined(STM32L4R7xx) || defined(STM32L4R9xx) || defined(STM32L4S5xx) || defined(STM32L4S7xx) || defined(STM32L4S9xx)
  1470. /**
  1471. * @brief Compute SYSCLK frequency based on PLL SYSCLK source.
  1472. * @retval SYSCLK frequency
  1473. */
  1474. static uint32_t RCC_GetSysClockFreqFromPLLSource(void)
  1475. {
  1476. uint32_t msirange = 0U, pllvco = 0U, pllsource = 0U, pllr = 2U, pllm = 2U;
  1477. uint32_t sysclockfreq = 0U;
  1478. if(__HAL_RCC_GET_PLL_OSCSOURCE() == RCC_PLLSOURCE_MSI)
  1479. {
  1480. /* Get MSI range source */
  1481. if(READ_BIT(RCC->CR, RCC_CR_MSIRGSEL) == RESET)
  1482. { /* MSISRANGE from RCC_CSR applies */
  1483. msirange = READ_BIT(RCC->CSR, RCC_CSR_MSISRANGE) >> RCC_CSR_MSISRANGE_Pos;
  1484. }
  1485. else
  1486. { /* MSIRANGE from RCC_CR applies */
  1487. msirange = READ_BIT(RCC->CR, RCC_CR_MSIRANGE) >> RCC_CR_MSIRANGE_Pos;
  1488. }
  1489. /*MSI frequency range in HZ*/
  1490. msirange = MSIRangeTable[msirange];
  1491. }
  1492. /* PLL_VCO = (HSE_VALUE or HSI_VALUE or MSI_VALUE/ PLLM) * PLLN
  1493. SYSCLK = PLL_VCO / PLLR
  1494. */
  1495. pllsource = READ_BIT(RCC->PLLCFGR, RCC_PLLCFGR_PLLSRC);
  1496. pllm = (READ_BIT(RCC->PLLCFGR, RCC_PLLCFGR_PLLM) >> RCC_PLLCFGR_PLLM_Pos) + 1U ;
  1497. switch (pllsource)
  1498. {
  1499. case RCC_PLLSOURCE_HSI: /* HSI used as PLL clock source */
  1500. pllvco = (HSI_VALUE / pllm) * (READ_BIT(RCC->PLLCFGR, RCC_PLLCFGR_PLLN) >> RCC_PLLCFGR_PLLN_Pos);
  1501. break;
  1502. case RCC_PLLSOURCE_HSE: /* HSE used as PLL clock source */
  1503. pllvco = (HSE_VALUE / pllm) * (READ_BIT(RCC->PLLCFGR, RCC_PLLCFGR_PLLN) >> RCC_PLLCFGR_PLLN_Pos);
  1504. break;
  1505. case RCC_PLLSOURCE_MSI: /* MSI used as PLL clock source */
  1506. default:
  1507. pllvco = (msirange / pllm) * (READ_BIT(RCC->PLLCFGR, RCC_PLLCFGR_PLLN) >> RCC_PLLCFGR_PLLN_Pos);
  1508. break;
  1509. }
  1510. pllr = ((READ_BIT(RCC->PLLCFGR, RCC_PLLCFGR_PLLR) >> RCC_PLLCFGR_PLLR_Pos) + 1U ) * 2U;
  1511. sysclockfreq = pllvco/pllr;
  1512. return sysclockfreq;
  1513. }
  1514. #endif /* STM32L4R5xx || STM32L4R7xx || STM32L4R9xx || STM32L4S5xx || STM32L4S7xx || STM32L4S9xx */
  1515. /**
  1516. * @}
  1517. */
  1518. #endif /* HAL_RCC_MODULE_ENABLED */
  1519. /**
  1520. * @}
  1521. */
  1522. /**
  1523. * @}
  1524. */
  1525. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/