stm32l1xx_hal_rcc.c 48 KB

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