Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.
 
 
 
 

1873 righe
74 KiB

  1. /**
  2. ******************************************************************************
  3. * @file stm32f4xx_rcc.c
  4. * @author MCD Application Team
  5. * @version V1.1.0
  6. * @date 11-January-2013
  7. * @brief This file provides firmware functions to manage the following
  8. * functionalities of the Reset and clock control (RCC) peripheral:
  9. * + Internal/external clocks, PLL, CSS and MCO configuration
  10. * + System, AHB and APB busses clocks configuration
  11. * + Peripheral clocks configuration
  12. * + Interrupts and flags management
  13. *
  14. @verbatim
  15. ===============================================================================
  16. ##### RCC specific features #####
  17. ===============================================================================
  18. [..]
  19. After reset the device is running from Internal High Speed oscillator
  20. (HSI 16MHz) with Flash 0 wait state, Flash prefetch buffer, D-Cache
  21. and I-Cache are disabled, and all peripherals are off except internal
  22. SRAM, Flash and JTAG.
  23. (+) There is no prescaler on High speed (AHB) and Low speed (APB) busses;
  24. all peripherals mapped on these busses are running at HSI speed.
  25. (+) The clock for all peripherals is switched off, except the SRAM and FLASH.
  26. (+) All GPIOs are in input floating state, except the JTAG pins which
  27. are assigned to be used for debug purpose.
  28. [..]
  29. Once the device started from reset, the user application has to:
  30. (+) Configure the clock source to be used to drive the System clock
  31. (if the application needs higher frequency/performance)
  32. (+) Configure the System clock frequency and Flash settings
  33. (+) Configure the AHB and APB busses prescalers
  34. (+) Enable the clock for the peripheral(s) to be used
  35. (+) Configure the clock source(s) for peripherals which clocks are not
  36. derived from the System clock (I2S, RTC, ADC, USB OTG FS/SDIO/RNG)
  37. @endverbatim
  38. ******************************************************************************
  39. * @attention
  40. *
  41. * <h2><center>&copy; COPYRIGHT 2013 STMicroelectronics</center></h2>
  42. *
  43. * Licensed under MCD-ST Liberty SW License Agreement V2, (the "License");
  44. * You may not use this file except in compliance with the License.
  45. * You may obtain a copy of the License at:
  46. *
  47. * http://www.st.com/software_license_agreement_liberty_v2
  48. *
  49. * Unless required by applicable law or agreed to in writing, software
  50. * distributed under the License is distributed on an "AS IS" BASIS,
  51. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  52. * See the License for the specific language governing permissions and
  53. * limitations under the License.
  54. *
  55. ******************************************************************************
  56. */
  57. /* Includes ------------------------------------------------------------------*/
  58. #include "stm32f4xx_rcc.h"
  59. /** @addtogroup STM32F4xx_StdPeriph_Driver
  60. * @{
  61. */
  62. /** @defgroup RCC
  63. * @brief RCC driver modules
  64. * @{
  65. */
  66. /* Private typedef -----------------------------------------------------------*/
  67. /* Private define ------------------------------------------------------------*/
  68. /* ------------ RCC registers bit address in the alias region ----------- */
  69. #define RCC_OFFSET (RCC_BASE - PERIPH_BASE)
  70. /* --- CR Register ---*/
  71. /* Alias word address of HSION bit */
  72. #define CR_OFFSET (RCC_OFFSET + 0x00)
  73. #define HSION_BitNumber 0x00
  74. #define CR_HSION_BB (PERIPH_BB_BASE + (CR_OFFSET * 32) + (HSION_BitNumber * 4))
  75. /* Alias word address of CSSON bit */
  76. #define CSSON_BitNumber 0x13
  77. #define CR_CSSON_BB (PERIPH_BB_BASE + (CR_OFFSET * 32) + (CSSON_BitNumber * 4))
  78. /* Alias word address of PLLON bit */
  79. #define PLLON_BitNumber 0x18
  80. #define CR_PLLON_BB (PERIPH_BB_BASE + (CR_OFFSET * 32) + (PLLON_BitNumber * 4))
  81. /* Alias word address of PLLI2SON bit */
  82. #define PLLI2SON_BitNumber 0x1A
  83. #define CR_PLLI2SON_BB (PERIPH_BB_BASE + (CR_OFFSET * 32) + (PLLI2SON_BitNumber * 4))
  84. /* --- CFGR Register ---*/
  85. /* Alias word address of I2SSRC bit */
  86. #define CFGR_OFFSET (RCC_OFFSET + 0x08)
  87. #define I2SSRC_BitNumber 0x17
  88. #define CFGR_I2SSRC_BB (PERIPH_BB_BASE + (CFGR_OFFSET * 32) + (I2SSRC_BitNumber * 4))
  89. /* --- BDCR Register ---*/
  90. /* Alias word address of RTCEN bit */
  91. #define BDCR_OFFSET (RCC_OFFSET + 0x70)
  92. #define RTCEN_BitNumber 0x0F
  93. #define BDCR_RTCEN_BB (PERIPH_BB_BASE + (BDCR_OFFSET * 32) + (RTCEN_BitNumber * 4))
  94. /* Alias word address of BDRST bit */
  95. #define BDRST_BitNumber 0x10
  96. #define BDCR_BDRST_BB (PERIPH_BB_BASE + (BDCR_OFFSET * 32) + (BDRST_BitNumber * 4))
  97. /* --- CSR Register ---*/
  98. /* Alias word address of LSION bit */
  99. #define CSR_OFFSET (RCC_OFFSET + 0x74)
  100. #define LSION_BitNumber 0x00
  101. #define CSR_LSION_BB (PERIPH_BB_BASE + (CSR_OFFSET * 32) + (LSION_BitNumber * 4))
  102. /* --- DCKCFGR Register ---*/
  103. /* Alias word address of TIMPRE bit */
  104. #define DCKCFGR_OFFSET (RCC_OFFSET + 0x8C)
  105. #define TIMPRE_BitNumber 0x18
  106. #define DCKCFGR_TIMPRE_BB (PERIPH_BB_BASE + (DCKCFGR_OFFSET * 32) + (TIMPRE_BitNumber * 4))
  107. /* ---------------------- RCC registers bit mask ------------------------ */
  108. /* CFGR register bit mask */
  109. #define CFGR_MCO2_RESET_MASK ((uint32_t)0x07FFFFFF)
  110. #define CFGR_MCO1_RESET_MASK ((uint32_t)0xF89FFFFF)
  111. /* RCC Flag Mask */
  112. #define FLAG_MASK ((uint8_t)0x1F)
  113. /* CR register byte 3 (Bits[23:16]) base address */
  114. #define CR_BYTE3_ADDRESS ((uint32_t)0x40023802)
  115. /* CIR register byte 2 (Bits[15:8]) base address */
  116. #define CIR_BYTE2_ADDRESS ((uint32_t)(RCC_BASE + 0x0C + 0x01))
  117. /* CIR register byte 3 (Bits[23:16]) base address */
  118. #define CIR_BYTE3_ADDRESS ((uint32_t)(RCC_BASE + 0x0C + 0x02))
  119. /* BDCR register base address */
  120. #define BDCR_ADDRESS (PERIPH_BASE + BDCR_OFFSET)
  121. /* Private macro -------------------------------------------------------------*/
  122. /* Private variables ---------------------------------------------------------*/
  123. static __I uint8_t APBAHBPrescTable[16] = {0, 0, 0, 0, 1, 2, 3, 4, 1, 2, 3, 4, 6, 7, 8, 9};
  124. /* Private function prototypes -----------------------------------------------*/
  125. /* Private functions ---------------------------------------------------------*/
  126. /** @defgroup RCC_Private_Functions
  127. * @{
  128. */
  129. /** @defgroup RCC_Group1 Internal and external clocks, PLL, CSS and MCO configuration functions
  130. * @brief Internal and external clocks, PLL, CSS and MCO configuration functions
  131. *
  132. @verbatim
  133. ===================================================================================
  134. ##### Internal and external clocks, PLL, CSS and MCO configuration functions #####
  135. ===================================================================================
  136. [..]
  137. This section provide functions allowing to configure the internal/external clocks,
  138. PLLs, CSS and MCO pins.
  139. (#) HSI (high-speed internal), 16 MHz factory-trimmed RC used directly or through
  140. the PLL as System clock source.
  141. (#) LSI (low-speed internal), 32 KHz low consumption RC used as IWDG and/or RTC
  142. clock source.
  143. (#) HSE (high-speed external), 4 to 26 MHz crystal oscillator used directly or
  144. through the PLL as System clock source. Can be used also as RTC clock source.
  145. (#) LSE (low-speed external), 32 KHz oscillator used as RTC clock source.
  146. (#) PLL (clocked by HSI or HSE), featuring two different output clocks:
  147. (++) The first output is used to generate the high speed system clock (up to 168 MHz)
  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 SDIO (<= 48 MHz).
  150. (#) PLLI2S (clocked by HSI or HSE), used to generate an accurate clock to achieve
  151. high-quality audio performance on the I2S interface.
  152. (#) CSS (Clock security system), once enable and if a HSE clock failure occurs
  153. (HSE used directly or through PLL as System clock source), the System clock
  154. is automatically switched to HSI and an interrupt is generated if enabled.
  155. The interrupt is linked to the Cortex-M4 NMI (Non-Maskable Interrupt)
  156. exception vector.
  157. (#) MCO1 (microcontroller clock output), used to output HSI, LSE, HSE or PLL
  158. clock (through a configurable prescaler) on PA8 pin.
  159. (#) MCO2 (microcontroller clock output), used to output HSE, PLL, SYSCLK or PLLI2S
  160. clock (through a configurable prescaler) on PC9 pin.
  161. @endverbatim
  162. * @{
  163. */
  164. /**
  165. * @brief Resets the RCC clock configuration to the default reset state.
  166. * @note The default reset state of the clock configuration is given below:
  167. * - HSI ON and used as system clock source
  168. * - HSE, PLL and PLLI2S OFF
  169. * - AHB, APB1 and APB2 prescaler set to 1.
  170. * - CSS, MCO1 and MCO2 OFF
  171. * - All interrupts disabled
  172. * @note This function doesn't modify the configuration of the
  173. * - Peripheral clocks
  174. * - LSI, LSE and RTC clocks
  175. * @param None
  176. * @retval None
  177. */
  178. void RCC_DeInit(void)
  179. {
  180. /* Set HSION bit */
  181. RCC->CR |= (uint32_t)0x00000001;
  182. /* Reset CFGR register */
  183. RCC->CFGR = 0x00000000;
  184. /* Reset HSEON, CSSON, PLLON and PLLI2S bits */
  185. RCC->CR &= (uint32_t)0xFAF6FFFF;
  186. /* Reset PLLCFGR register */
  187. RCC->PLLCFGR = 0x24003010;
  188. /* Reset PLLI2SCFGR register */
  189. RCC->PLLI2SCFGR = 0x20003000;
  190. /* Reset HSEBYP bit */
  191. RCC->CR &= (uint32_t)0xFFFBFFFF;
  192. /* Disable all interrupts */
  193. RCC->CIR = 0x00000000;
  194. #ifdef STM32F427X
  195. /* Disable Timers clock prescalers selection */
  196. RCC->DCKCFGR = 0x00000000;
  197. #endif /* STM32F427X */
  198. }
  199. /**
  200. * @brief Configures the External High Speed oscillator (HSE).
  201. * @note After enabling the HSE (RCC_HSE_ON or RCC_HSE_Bypass), the application
  202. * software should wait on HSERDY flag to be set indicating that HSE clock
  203. * is stable and can be used to clock the PLL and/or system clock.
  204. * @note HSE state can not be changed if it is used directly or through the
  205. * PLL as system clock. In this case, you have to select another source
  206. * of the system clock then change the HSE state (ex. disable it).
  207. * @note The HSE is stopped by hardware when entering STOP and STANDBY modes.
  208. * @note This function reset the CSSON bit, so if the Clock security system(CSS)
  209. * was previously enabled you have to enable it again after calling this
  210. * function.
  211. * @param RCC_HSE: specifies the new state of the HSE.
  212. * This parameter can be one of the following values:
  213. * @arg RCC_HSE_OFF: turn OFF the HSE oscillator, HSERDY flag goes low after
  214. * 6 HSE oscillator clock cycles.
  215. * @arg RCC_HSE_ON: turn ON the HSE oscillator
  216. * @arg RCC_HSE_Bypass: HSE oscillator bypassed with external clock
  217. * @retval None
  218. */
  219. void RCC_HSEConfig(uint8_t RCC_HSE)
  220. {
  221. /* Check the parameters */
  222. assert_param(IS_RCC_HSE(RCC_HSE));
  223. /* Reset HSEON and HSEBYP bits before configuring the HSE ------------------*/
  224. *(__IO uint8_t *) CR_BYTE3_ADDRESS = RCC_HSE_OFF;
  225. /* Set the new HSE configuration -------------------------------------------*/
  226. *(__IO uint8_t *) CR_BYTE3_ADDRESS = RCC_HSE;
  227. }
  228. /**
  229. * @brief Waits for HSE start-up.
  230. * @note This functions waits on HSERDY flag to be set and return SUCCESS if
  231. * this flag is set, otherwise returns ERROR if the timeout is reached
  232. * and this flag is not set. The timeout value is defined by the constant
  233. * HSE_STARTUP_TIMEOUT in stm32f4xx.h file. You can tailor it depending
  234. * on the HSE crystal used in your application.
  235. * @param None
  236. * @retval An ErrorStatus enumeration value:
  237. * - SUCCESS: HSE oscillator is stable and ready to use
  238. * - ERROR: HSE oscillator not yet ready
  239. */
  240. ErrorStatus RCC_WaitForHSEStartUp(void)
  241. {
  242. __IO uint32_t startupcounter = 0;
  243. ErrorStatus status = ERROR;
  244. FlagStatus hsestatus = RESET;
  245. /* Wait till HSE is ready and if Time out is reached exit */
  246. do
  247. {
  248. hsestatus = RCC_GetFlagStatus(RCC_FLAG_HSERDY);
  249. startupcounter++;
  250. } while((startupcounter != HSE_STARTUP_TIMEOUT) && (hsestatus == RESET));
  251. if (RCC_GetFlagStatus(RCC_FLAG_HSERDY) != RESET)
  252. {
  253. status = SUCCESS;
  254. }
  255. else
  256. {
  257. status = ERROR;
  258. }
  259. return (status);
  260. }
  261. /**
  262. * @brief Adjusts the Internal High Speed oscillator (HSI) calibration value.
  263. * @note The calibration is used to compensate for the variations in voltage
  264. * and temperature that influence the frequency of the internal HSI RC.
  265. * @param HSICalibrationValue: specifies the calibration trimming value.
  266. * This parameter must be a number between 0 and 0x1F.
  267. * @retval None
  268. */
  269. void RCC_AdjustHSICalibrationValue(uint8_t HSICalibrationValue)
  270. {
  271. uint32_t tmpreg = 0;
  272. /* Check the parameters */
  273. assert_param(IS_RCC_CALIBRATION_VALUE(HSICalibrationValue));
  274. tmpreg = RCC->CR;
  275. /* Clear HSITRIM[4:0] bits */
  276. tmpreg &= ~RCC_CR_HSITRIM;
  277. /* Set the HSITRIM[4:0] bits according to HSICalibrationValue value */
  278. tmpreg |= (uint32_t)HSICalibrationValue << 3;
  279. /* Store the new value */
  280. RCC->CR = tmpreg;
  281. }
  282. /**
  283. * @brief Enables or disables the Internal High Speed oscillator (HSI).
  284. * @note The HSI is stopped by hardware when entering STOP and STANDBY modes.
  285. * It is used (enabled by hardware) as system clock source after startup
  286. * from Reset, wakeup from STOP and STANDBY mode, or in case of failure
  287. * of the HSE used directly or indirectly as system clock (if the Clock
  288. * Security System CSS is enabled).
  289. * @note HSI can not be stopped if it is used as system clock source. In this case,
  290. * you have to select another source of the system clock then stop the HSI.
  291. * @note After enabling the HSI, the application software should wait on HSIRDY
  292. * flag to be set indicating that HSI clock is stable and can be used as
  293. * system clock source.
  294. * @param NewState: new state of the HSI.
  295. * This parameter can be: ENABLE or DISABLE.
  296. * @note When the HSI is stopped, HSIRDY flag goes low after 6 HSI oscillator
  297. * clock cycles.
  298. * @retval None
  299. */
  300. void RCC_HSICmd(FunctionalState NewState)
  301. {
  302. /* Check the parameters */
  303. assert_param(IS_FUNCTIONAL_STATE(NewState));
  304. *(__IO uint32_t *) CR_HSION_BB = (uint32_t)NewState;
  305. }
  306. /**
  307. * @brief Configures the External Low Speed oscillator (LSE).
  308. * @note As the LSE is in the Backup domain and write access is denied to
  309. * this domain after reset, you have to enable write access using
  310. * PWR_BackupAccessCmd(ENABLE) function before to configure the LSE
  311. * (to be done once after reset).
  312. * @note After enabling the LSE (RCC_LSE_ON or RCC_LSE_Bypass), the application
  313. * software should wait on LSERDY flag to be set indicating that LSE clock
  314. * is stable and can be used to clock the RTC.
  315. * @param RCC_LSE: specifies the new state of the LSE.
  316. * This parameter can be one of the following values:
  317. * @arg RCC_LSE_OFF: turn OFF the LSE oscillator, LSERDY flag goes low after
  318. * 6 LSE oscillator clock cycles.
  319. * @arg RCC_LSE_ON: turn ON the LSE oscillator
  320. * @arg RCC_LSE_Bypass: LSE oscillator bypassed with external clock
  321. * @retval None
  322. */
  323. void RCC_LSEConfig(uint8_t RCC_LSE)
  324. {
  325. /* Check the parameters */
  326. assert_param(IS_RCC_LSE(RCC_LSE));
  327. /* Reset LSEON and LSEBYP bits before configuring the LSE ------------------*/
  328. /* Reset LSEON bit */
  329. *(__IO uint8_t *) BDCR_ADDRESS = RCC_LSE_OFF;
  330. /* Reset LSEBYP bit */
  331. *(__IO uint8_t *) BDCR_ADDRESS = RCC_LSE_OFF;
  332. /* Configure LSE (RCC_LSE_OFF is already covered by the code section above) */
  333. switch (RCC_LSE)
  334. {
  335. case RCC_LSE_ON:
  336. /* Set LSEON bit */
  337. *(__IO uint8_t *) BDCR_ADDRESS = RCC_LSE_ON;
  338. break;
  339. case RCC_LSE_Bypass:
  340. /* Set LSEBYP and LSEON bits */
  341. *(__IO uint8_t *) BDCR_ADDRESS = RCC_LSE_Bypass | RCC_LSE_ON;
  342. break;
  343. default:
  344. break;
  345. }
  346. }
  347. /**
  348. * @brief Enables or disables the Internal Low Speed oscillator (LSI).
  349. * @note After enabling the LSI, the application software should wait on
  350. * LSIRDY flag to be set indicating that LSI clock is stable and can
  351. * be used to clock the IWDG and/or the RTC.
  352. * @note LSI can not be disabled if the IWDG is running.
  353. * @param NewState: new state of the LSI.
  354. * This parameter can be: ENABLE or DISABLE.
  355. * @note When the LSI is stopped, LSIRDY flag goes low after 6 LSI oscillator
  356. * clock cycles.
  357. * @retval None
  358. */
  359. void RCC_LSICmd(FunctionalState NewState)
  360. {
  361. /* Check the parameters */
  362. assert_param(IS_FUNCTIONAL_STATE(NewState));
  363. *(__IO uint32_t *) CSR_LSION_BB = (uint32_t)NewState;
  364. }
  365. /**
  366. * @brief Configures the main PLL clock source, multiplication and division factors.
  367. * @note This function must be used only when the main PLL is disabled.
  368. *
  369. * @param RCC_PLLSource: specifies the PLL entry clock source.
  370. * This parameter can be one of the following values:
  371. * @arg RCC_PLLSource_HSI: HSI oscillator clock selected as PLL clock entry
  372. * @arg RCC_PLLSource_HSE: HSE oscillator clock selected as PLL clock entry
  373. * @note This clock source (RCC_PLLSource) is common for the main PLL and PLLI2S.
  374. *
  375. * @param PLLM: specifies the division factor for PLL VCO input clock
  376. * This parameter must be a number between 0 and 63.
  377. * @note You have to set the PLLM parameter correctly to ensure that the VCO input
  378. * frequency ranges from 1 to 2 MHz. It is recommended to select a frequency
  379. * of 2 MHz to limit PLL jitter.
  380. *
  381. * @param PLLN: specifies the multiplication factor for PLL VCO output clock
  382. * This parameter must be a number between 192 and 432.
  383. * @note You have to set the PLLN parameter correctly to ensure that the VCO
  384. * output frequency is between 192 and 432 MHz.
  385. *
  386. * @param PLLP: specifies the division factor for main system clock (SYSCLK)
  387. * This parameter must be a number in the range {2, 4, 6, or 8}.
  388. * @note You have to set the PLLP parameter correctly to not exceed 168 MHz on
  389. * the System clock frequency.
  390. *
  391. * @param PLLQ: specifies the division factor for OTG FS, SDIO and RNG clocks
  392. * This parameter must be a number between 4 and 15.
  393. * @note If the USB OTG FS is used in your application, you have to set the
  394. * PLLQ parameter correctly to have 48 MHz clock for the USB. However,
  395. * the SDIO and RNG need a frequency lower than or equal to 48 MHz to work
  396. * correctly.
  397. *
  398. * @retval None
  399. */
  400. void RCC_PLLConfig(uint32_t RCC_PLLSource, uint32_t PLLM, uint32_t PLLN, uint32_t PLLP, uint32_t PLLQ)
  401. {
  402. /* Check the parameters */
  403. assert_param(IS_RCC_PLL_SOURCE(RCC_PLLSource));
  404. assert_param(IS_RCC_PLLM_VALUE(PLLM));
  405. assert_param(IS_RCC_PLLN_VALUE(PLLN));
  406. assert_param(IS_RCC_PLLP_VALUE(PLLP));
  407. assert_param(IS_RCC_PLLQ_VALUE(PLLQ));
  408. RCC->PLLCFGR = PLLM | (PLLN << 6) | (((PLLP >> 1) -1) << 16) | (RCC_PLLSource) |
  409. (PLLQ << 24);
  410. }
  411. /**
  412. * @brief Enables or disables the main PLL.
  413. * @note After enabling the main PLL, the application software should wait on
  414. * PLLRDY flag to be set indicating that PLL clock is stable and can
  415. * be used as system clock source.
  416. * @note The main PLL can not be disabled if it is used as system clock source
  417. * @note The main PLL is disabled by hardware when entering STOP and STANDBY modes.
  418. * @param NewState: new state of the main PLL. This parameter can be: ENABLE or DISABLE.
  419. * @retval None
  420. */
  421. void RCC_PLLCmd(FunctionalState NewState)
  422. {
  423. /* Check the parameters */
  424. assert_param(IS_FUNCTIONAL_STATE(NewState));
  425. *(__IO uint32_t *) CR_PLLON_BB = (uint32_t)NewState;
  426. }
  427. /**
  428. * @brief Configures the PLLI2S clock multiplication and division factors.
  429. *
  430. * @note This function must be used only when the PLLI2S is disabled.
  431. * @note PLLI2S clock source is common with the main PLL (configured in
  432. * RCC_PLLConfig function )
  433. *
  434. * @param PLLI2SN: specifies the multiplication factor for PLLI2S VCO output clock
  435. * This parameter must be a number between 192 and 432.
  436. * @note You have to set the PLLI2SN parameter correctly to ensure that the VCO
  437. * output frequency is between 192 and 432 MHz.
  438. *
  439. * @param PLLI2SR: specifies the division factor for I2S clock
  440. * This parameter must be a number between 2 and 7.
  441. * @note You have to set the PLLI2SR parameter correctly to not exceed 192 MHz
  442. * on the I2S clock frequency.
  443. *
  444. * @retval None
  445. */
  446. void RCC_PLLI2SConfig(uint32_t PLLI2SN, uint32_t PLLI2SR)
  447. {
  448. /* Check the parameters */
  449. assert_param(IS_RCC_PLLI2SN_VALUE(PLLI2SN));
  450. assert_param(IS_RCC_PLLI2SR_VALUE(PLLI2SR));
  451. RCC->PLLI2SCFGR = (PLLI2SN << 6) | (PLLI2SR << 28);
  452. }
  453. /**
  454. * @brief Enables or disables the PLLI2S.
  455. * @note The PLLI2S is disabled by hardware when entering STOP and STANDBY modes.
  456. * @param NewState: new state of the PLLI2S. This parameter can be: ENABLE or DISABLE.
  457. * @retval None
  458. */
  459. void RCC_PLLI2SCmd(FunctionalState NewState)
  460. {
  461. /* Check the parameters */
  462. assert_param(IS_FUNCTIONAL_STATE(NewState));
  463. *(__IO uint32_t *) CR_PLLI2SON_BB = (uint32_t)NewState;
  464. }
  465. /**
  466. * @brief Enables or disables the Clock Security System.
  467. * @note If a failure is detected on the HSE oscillator clock, this oscillator
  468. * is automatically disabled and an interrupt is generated to inform the
  469. * software about the failure (Clock Security System Interrupt, CSSI),
  470. * allowing the MCU to perform rescue operations. The CSSI is linked to
  471. * the Cortex-M4 NMI (Non-Maskable Interrupt) exception vector.
  472. * @param NewState: new state of the Clock Security System.
  473. * This parameter can be: ENABLE or DISABLE.
  474. * @retval None
  475. */
  476. void RCC_ClockSecuritySystemCmd(FunctionalState NewState)
  477. {
  478. /* Check the parameters */
  479. assert_param(IS_FUNCTIONAL_STATE(NewState));
  480. *(__IO uint32_t *) CR_CSSON_BB = (uint32_t)NewState;
  481. }
  482. /**
  483. * @brief Selects the clock source to output on MCO1 pin(PA8).
  484. * @note PA8 should be configured in alternate function mode.
  485. * @param RCC_MCO1Source: specifies the clock source to output.
  486. * This parameter can be one of the following values:
  487. * @arg RCC_MCO1Source_HSI: HSI clock selected as MCO1 source
  488. * @arg RCC_MCO1Source_LSE: LSE clock selected as MCO1 source
  489. * @arg RCC_MCO1Source_HSE: HSE clock selected as MCO1 source
  490. * @arg RCC_MCO1Source_PLLCLK: main PLL clock selected as MCO1 source
  491. * @param RCC_MCO1Div: specifies the MCO1 prescaler.
  492. * This parameter can be one of the following values:
  493. * @arg RCC_MCO1Div_1: no division applied to MCO1 clock
  494. * @arg RCC_MCO1Div_2: division by 2 applied to MCO1 clock
  495. * @arg RCC_MCO1Div_3: division by 3 applied to MCO1 clock
  496. * @arg RCC_MCO1Div_4: division by 4 applied to MCO1 clock
  497. * @arg RCC_MCO1Div_5: division by 5 applied to MCO1 clock
  498. * @retval None
  499. */
  500. void RCC_MCO1Config(uint32_t RCC_MCO1Source, uint32_t RCC_MCO1Div)
  501. {
  502. uint32_t tmpreg = 0;
  503. /* Check the parameters */
  504. assert_param(IS_RCC_MCO1SOURCE(RCC_MCO1Source));
  505. assert_param(IS_RCC_MCO1DIV(RCC_MCO1Div));
  506. tmpreg = RCC->CFGR;
  507. /* Clear MCO1[1:0] and MCO1PRE[2:0] bits */
  508. tmpreg &= CFGR_MCO1_RESET_MASK;
  509. /* Select MCO1 clock source and prescaler */
  510. tmpreg |= RCC_MCO1Source | RCC_MCO1Div;
  511. /* Store the new value */
  512. RCC->CFGR = tmpreg;
  513. }
  514. /**
  515. * @brief Selects the clock source to output on MCO2 pin(PC9).
  516. * @note PC9 should be configured in alternate function mode.
  517. * @param RCC_MCO2Source: specifies the clock source to output.
  518. * This parameter can be one of the following values:
  519. * @arg RCC_MCO2Source_SYSCLK: System clock (SYSCLK) selected as MCO2 source
  520. * @arg RCC_MCO2Source_PLLI2SCLK: PLLI2S clock selected as MCO2 source
  521. * @arg RCC_MCO2Source_HSE: HSE clock selected as MCO2 source
  522. * @arg RCC_MCO2Source_PLLCLK: main PLL clock selected as MCO2 source
  523. * @param RCC_MCO2Div: specifies the MCO2 prescaler.
  524. * This parameter can be one of the following values:
  525. * @arg RCC_MCO2Div_1: no division applied to MCO2 clock
  526. * @arg RCC_MCO2Div_2: division by 2 applied to MCO2 clock
  527. * @arg RCC_MCO2Div_3: division by 3 applied to MCO2 clock
  528. * @arg RCC_MCO2Div_4: division by 4 applied to MCO2 clock
  529. * @arg RCC_MCO2Div_5: division by 5 applied to MCO2 clock
  530. * @retval None
  531. */
  532. void RCC_MCO2Config(uint32_t RCC_MCO2Source, uint32_t RCC_MCO2Div)
  533. {
  534. uint32_t tmpreg = 0;
  535. /* Check the parameters */
  536. assert_param(IS_RCC_MCO2SOURCE(RCC_MCO2Source));
  537. assert_param(IS_RCC_MCO2DIV(RCC_MCO2Div));
  538. tmpreg = RCC->CFGR;
  539. /* Clear MCO2 and MCO2PRE[2:0] bits */
  540. tmpreg &= CFGR_MCO2_RESET_MASK;
  541. /* Select MCO2 clock source and prescaler */
  542. tmpreg |= RCC_MCO2Source | RCC_MCO2Div;
  543. /* Store the new value */
  544. RCC->CFGR = tmpreg;
  545. }
  546. /**
  547. * @}
  548. */
  549. /** @defgroup RCC_Group2 System AHB and APB busses clocks configuration functions
  550. * @brief System, AHB and APB busses clocks configuration functions
  551. *
  552. @verbatim
  553. ===============================================================================
  554. ##### System, AHB and APB busses clocks configuration functions #####
  555. ===============================================================================
  556. [..]
  557. This section provide functions allowing to configure the System, AHB, APB1 and
  558. APB2 busses clocks.
  559. (#) Several clock sources can be used to drive the System clock (SYSCLK): HSI,
  560. HSE and PLL.
  561. The AHB clock (HCLK) is derived from System clock through configurable
  562. prescaler and used to clock the CPU, memory and peripherals mapped
  563. on AHB bus (DMA, GPIO...). APB1 (PCLK1) and APB2 (PCLK2) clocks are derived
  564. from AHB clock through configurable prescalers and used to clock
  565. the peripherals mapped on these busses. You can use
  566. "RCC_GetClocksFreq()" function to retrieve the frequencies of these clocks.
  567. -@- All the peripheral clocks are derived from the System clock (SYSCLK) except:
  568. (+@) I2S: the I2S clock can be derived either from a specific PLL (PLLI2S) or
  569. from an external clock mapped on the I2S_CKIN pin.
  570. You have to use RCC_I2SCLKConfig() function to configure this clock.
  571. (+@) RTC: the RTC clock can be derived either from the LSI, LSE or HSE clock
  572. divided by 2 to 31. You have to use RCC_RTCCLKConfig() and RCC_RTCCLKCmd()
  573. functions to configure this clock.
  574. (+@) USB OTG FS, SDIO and RTC: USB OTG FS require a frequency equal to 48 MHz
  575. to work correctly, while the SDIO require a frequency equal or lower than
  576. to 48. This clock is derived of the main PLL through PLLQ divider.
  577. (+@) IWDG clock which is always the LSI clock.
  578. (#) The maximum frequency of the SYSCLK and HCLK is 168 MHz, PCLK2 84 MHz
  579. and PCLK1 42 MHz. Depending on the device voltage range, the maximum
  580. frequency should be adapted accordingly:
  581. +-------------------------------------------------------------------------------------+
  582. | Latency | HCLK clock frequency (MHz) |
  583. | |---------------------------------------------------------------------|
  584. | | voltage range | voltage range | voltage range | voltage range |
  585. | | 2.7 V - 3.6 V | 2.4 V - 2.7 V | 2.1 V - 2.4 V | 1.8 V - 2.1 V |
  586. |---------------|----------------|----------------|-----------------|-----------------|
  587. |0WS(1CPU cycle)|0 < HCLK <= 30 |0 < HCLK <= 24 |0 < HCLK <= 18 |0 < HCLK <= 16 |
  588. |---------------|----------------|----------------|-----------------|-----------------|
  589. |1WS(2CPU cycle)|30 < HCLK <= 60 |24 < HCLK <= 48 |18 < HCLK <= 36 |16 < HCLK <= 32 |
  590. |---------------|----------------|----------------|-----------------|-----------------|
  591. |2WS(3CPU cycle)|60 < HCLK <= 90 |48 < HCLK <= 72 |36 < HCLK <= 54 |32 < HCLK <= 48 |
  592. |---------------|----------------|----------------|-----------------|-----------------|
  593. |3WS(4CPU cycle)|90 < HCLK <= 120|72 < HCLK <= 96 |54 < HCLK <= 72 |48 < HCLK <= 64 |
  594. |---------------|----------------|----------------|-----------------|-----------------|
  595. |4WS(5CPU cycle)|120< HCLK <= 150|96 < HCLK <= 120|72 < HCLK <= 90 |64 < HCLK <= 80 |
  596. |---------------|----------------|----------------|-----------------|-----------------|
  597. |5WS(6CPU cycle)|120< HCLK <= 168|120< HCLK <= 144|90 < HCLK <= 108 |80 < HCLK <= 96 |
  598. |---------------|----------------|----------------|-----------------|-----------------|
  599. |6WS(7CPU cycle)| NA |144< HCLK <= 168|108 < HCLK <= 120|96 < HCLK <= 112 |
  600. |---------------|----------------|----------------|-----------------|-----------------|
  601. |7WS(8CPU cycle)| NA | NA |120 < HCLK <= 138|112 < HCLK <= 120|
  602. +-------------------------------------------------------------------------------------+
  603. -@- When VOS bits (in PWR_CR register) is reset to 0 , the maximum value of HCLK is 144 MHz.
  604. You can use PWR_MainRegulatorModeConfig() function to set or reset this bit.
  605. @endverbatim
  606. * @{
  607. */
  608. /**
  609. * @brief Configures the system clock (SYSCLK).
  610. * @note The HSI is used (enabled by hardware) as system clock source after
  611. * startup from Reset, wake-up from STOP and STANDBY mode, or in case
  612. * of failure of the HSE used directly or indirectly as system clock
  613. * (if the Clock Security System CSS is enabled).
  614. * @note A switch from one clock source to another occurs only if the target
  615. * clock source is ready (clock stable after startup delay or PLL locked).
  616. * If a clock source which is not yet ready is selected, the switch will
  617. * occur when the clock source will be ready.
  618. * You can use RCC_GetSYSCLKSource() function to know which clock is
  619. * currently used as system clock source.
  620. * @param RCC_SYSCLKSource: specifies the clock source used as system clock.
  621. * This parameter can be one of the following values:
  622. * @arg RCC_SYSCLKSource_HSI: HSI selected as system clock source
  623. * @arg RCC_SYSCLKSource_HSE: HSE selected as system clock source
  624. * @arg RCC_SYSCLKSource_PLLCLK: PLL selected as system clock source
  625. * @retval None
  626. */
  627. void RCC_SYSCLKConfig(uint32_t RCC_SYSCLKSource)
  628. {
  629. uint32_t tmpreg = 0;
  630. /* Check the parameters */
  631. assert_param(IS_RCC_SYSCLK_SOURCE(RCC_SYSCLKSource));
  632. tmpreg = RCC->CFGR;
  633. /* Clear SW[1:0] bits */
  634. tmpreg &= ~RCC_CFGR_SW;
  635. /* Set SW[1:0] bits according to RCC_SYSCLKSource value */
  636. tmpreg |= RCC_SYSCLKSource;
  637. /* Store the new value */
  638. RCC->CFGR = tmpreg;
  639. }
  640. /**
  641. * @brief Returns the clock source used as system clock.
  642. * @param None
  643. * @retval The clock source used as system clock. The returned value can be one
  644. * of the following:
  645. * - 0x00: HSI used as system clock
  646. * - 0x04: HSE used as system clock
  647. * - 0x08: PLL used as system clock
  648. */
  649. uint8_t RCC_GetSYSCLKSource(void)
  650. {
  651. return ((uint8_t)(RCC->CFGR & RCC_CFGR_SWS));
  652. }
  653. /**
  654. * @brief Configures the AHB clock (HCLK).
  655. * @note Depending on the device voltage range, the software has to set correctly
  656. * these bits to ensure that HCLK not exceed the maximum allowed frequency
  657. * (for more details refer to section above
  658. * "CPU, AHB and APB busses clocks configuration functions")
  659. * @param RCC_SYSCLK: defines the AHB clock divider. This clock is derived from
  660. * the system clock (SYSCLK).
  661. * This parameter can be one of the following values:
  662. * @arg RCC_SYSCLK_Div1: AHB clock = SYSCLK
  663. * @arg RCC_SYSCLK_Div2: AHB clock = SYSCLK/2
  664. * @arg RCC_SYSCLK_Div4: AHB clock = SYSCLK/4
  665. * @arg RCC_SYSCLK_Div8: AHB clock = SYSCLK/8
  666. * @arg RCC_SYSCLK_Div16: AHB clock = SYSCLK/16
  667. * @arg RCC_SYSCLK_Div64: AHB clock = SYSCLK/64
  668. * @arg RCC_SYSCLK_Div128: AHB clock = SYSCLK/128
  669. * @arg RCC_SYSCLK_Div256: AHB clock = SYSCLK/256
  670. * @arg RCC_SYSCLK_Div512: AHB clock = SYSCLK/512
  671. * @retval None
  672. */
  673. void RCC_HCLKConfig(uint32_t RCC_SYSCLK)
  674. {
  675. uint32_t tmpreg = 0;
  676. /* Check the parameters */
  677. assert_param(IS_RCC_HCLK(RCC_SYSCLK));
  678. tmpreg = RCC->CFGR;
  679. /* Clear HPRE[3:0] bits */
  680. tmpreg &= ~RCC_CFGR_HPRE;
  681. /* Set HPRE[3:0] bits according to RCC_SYSCLK value */
  682. tmpreg |= RCC_SYSCLK;
  683. /* Store the new value */
  684. RCC->CFGR = tmpreg;
  685. }
  686. /**
  687. * @brief Configures the Low Speed APB clock (PCLK1).
  688. * @param RCC_HCLK: defines the APB1 clock divider. This clock is derived from
  689. * the AHB clock (HCLK).
  690. * This parameter can be one of the following values:
  691. * @arg RCC_HCLK_Div1: APB1 clock = HCLK
  692. * @arg RCC_HCLK_Div2: APB1 clock = HCLK/2
  693. * @arg RCC_HCLK_Div4: APB1 clock = HCLK/4
  694. * @arg RCC_HCLK_Div8: APB1 clock = HCLK/8
  695. * @arg RCC_HCLK_Div16: APB1 clock = HCLK/16
  696. * @retval None
  697. */
  698. void RCC_PCLK1Config(uint32_t RCC_HCLK)
  699. {
  700. uint32_t tmpreg = 0;
  701. /* Check the parameters */
  702. assert_param(IS_RCC_PCLK(RCC_HCLK));
  703. tmpreg = RCC->CFGR;
  704. /* Clear PPRE1[2:0] bits */
  705. tmpreg &= ~RCC_CFGR_PPRE1;
  706. /* Set PPRE1[2:0] bits according to RCC_HCLK value */
  707. tmpreg |= RCC_HCLK;
  708. /* Store the new value */
  709. RCC->CFGR = tmpreg;
  710. }
  711. /**
  712. * @brief Configures the High Speed APB clock (PCLK2).
  713. * @param RCC_HCLK: defines the APB2 clock divider. This clock is derived from
  714. * the AHB clock (HCLK).
  715. * This parameter can be one of the following values:
  716. * @arg RCC_HCLK_Div1: APB2 clock = HCLK
  717. * @arg RCC_HCLK_Div2: APB2 clock = HCLK/2
  718. * @arg RCC_HCLK_Div4: APB2 clock = HCLK/4
  719. * @arg RCC_HCLK_Div8: APB2 clock = HCLK/8
  720. * @arg RCC_HCLK_Div16: APB2 clock = HCLK/16
  721. * @retval None
  722. */
  723. void RCC_PCLK2Config(uint32_t RCC_HCLK)
  724. {
  725. uint32_t tmpreg = 0;
  726. /* Check the parameters */
  727. assert_param(IS_RCC_PCLK(RCC_HCLK));
  728. tmpreg = RCC->CFGR;
  729. /* Clear PPRE2[2:0] bits */
  730. tmpreg &= ~RCC_CFGR_PPRE2;
  731. /* Set PPRE2[2:0] bits according to RCC_HCLK value */
  732. tmpreg |= RCC_HCLK << 3;
  733. /* Store the new value */
  734. RCC->CFGR = tmpreg;
  735. }
  736. /**
  737. * @brief Returns the frequencies of different on chip clocks; SYSCLK, HCLK,
  738. * PCLK1 and PCLK2.
  739. *
  740. * @note The system frequency computed by this function is not the real
  741. * frequency in the chip. It is calculated based on the predefined
  742. * constant and the selected clock source:
  743. * @note If SYSCLK source is HSI, function returns values based on HSI_VALUE(*)
  744. * @note If SYSCLK source is HSE, function returns values based on HSE_VALUE(**)
  745. * @note If SYSCLK source is PLL, function returns values based on HSE_VALUE(**)
  746. * or HSI_VALUE(*) multiplied/divided by the PLL factors.
  747. * @note (*) HSI_VALUE is a constant defined in stm32f4xx.h file (default value
  748. * 16 MHz) but the real value may vary depending on the variations
  749. * in voltage and temperature.
  750. * @note (**) HSE_VALUE is a constant defined in stm32f4xx.h file (default value
  751. * 25 MHz), user has to ensure that HSE_VALUE is same as the real
  752. * frequency of the crystal used. Otherwise, this function may
  753. * have wrong result.
  754. *
  755. * @note The result of this function could be not correct when using fractional
  756. * value for HSE crystal.
  757. *
  758. * @param RCC_Clocks: pointer to a RCC_ClocksTypeDef structure which will hold
  759. * the clocks frequencies.
  760. *
  761. * @note This function can be used by the user application to compute the
  762. * baudrate for the communication peripherals or configure other parameters.
  763. * @note Each time SYSCLK, HCLK, PCLK1 and/or PCLK2 clock changes, this function
  764. * must be called to update the structure's field. Otherwise, any
  765. * configuration based on this function will be incorrect.
  766. *
  767. * @retval None
  768. */
  769. void RCC_GetClocksFreq(RCC_ClocksTypeDef* RCC_Clocks)
  770. {
  771. uint32_t tmp = 0, presc = 0, pllvco = 0, pllp = 2, pllsource = 0, pllm = 2;
  772. /* Get SYSCLK source -------------------------------------------------------*/
  773. tmp = RCC->CFGR & RCC_CFGR_SWS;
  774. switch (tmp)
  775. {
  776. case 0x00: /* HSI used as system clock source */
  777. RCC_Clocks->SYSCLK_Frequency = HSI_VALUE;
  778. break;
  779. case 0x04: /* HSE used as system clock source */
  780. RCC_Clocks->SYSCLK_Frequency = HSE_VALUE;
  781. break;
  782. case 0x08: /* PLL used as system clock source */
  783. /* PLL_VCO = (HSE_VALUE or HSI_VALUE / PLLM) * PLLN
  784. SYSCLK = PLL_VCO / PLLP
  785. */
  786. pllsource = (RCC->PLLCFGR & RCC_PLLCFGR_PLLSRC) >> 22;
  787. pllm = RCC->PLLCFGR & RCC_PLLCFGR_PLLM;
  788. if (pllsource != 0)
  789. {
  790. /* HSE used as PLL clock source */
  791. pllvco = (HSE_VALUE / pllm) * ((RCC->PLLCFGR & RCC_PLLCFGR_PLLN) >> 6);
  792. }
  793. else
  794. {
  795. /* HSI used as PLL clock source */
  796. pllvco = (HSI_VALUE / pllm) * ((RCC->PLLCFGR & RCC_PLLCFGR_PLLN) >> 6);
  797. }
  798. pllp = (((RCC->PLLCFGR & RCC_PLLCFGR_PLLP) >>16) + 1 ) *2;
  799. RCC_Clocks->SYSCLK_Frequency = pllvco/pllp;
  800. break;
  801. default:
  802. RCC_Clocks->SYSCLK_Frequency = HSI_VALUE;
  803. break;
  804. }
  805. /* Compute HCLK, PCLK1 and PCLK2 clocks frequencies ------------------------*/
  806. /* Get HCLK prescaler */
  807. tmp = RCC->CFGR & RCC_CFGR_HPRE;
  808. tmp = tmp >> 4;
  809. presc = APBAHBPrescTable[tmp];
  810. /* HCLK clock frequency */
  811. RCC_Clocks->HCLK_Frequency = RCC_Clocks->SYSCLK_Frequency >> presc;
  812. /* Get PCLK1 prescaler */
  813. tmp = RCC->CFGR & RCC_CFGR_PPRE1;
  814. tmp = tmp >> 10;
  815. presc = APBAHBPrescTable[tmp];
  816. /* PCLK1 clock frequency */
  817. RCC_Clocks->PCLK1_Frequency = RCC_Clocks->HCLK_Frequency >> presc;
  818. /* Get PCLK2 prescaler */
  819. tmp = RCC->CFGR & RCC_CFGR_PPRE2;
  820. tmp = tmp >> 13;
  821. presc = APBAHBPrescTable[tmp];
  822. /* PCLK2 clock frequency */
  823. RCC_Clocks->PCLK2_Frequency = RCC_Clocks->HCLK_Frequency >> presc;
  824. }
  825. /**
  826. * @}
  827. */
  828. /** @defgroup RCC_Group3 Peripheral clocks configuration functions
  829. * @brief Peripheral clocks configuration functions
  830. *
  831. @verbatim
  832. ===============================================================================
  833. ##### Peripheral clocks configuration functions #####
  834. ===============================================================================
  835. [..] This section provide functions allowing to configure the Peripheral clocks.
  836. (#) The RTC clock which is derived from the LSI, LSE or HSE clock divided
  837. by 2 to 31.
  838. (#) After restart from Reset or wakeup from STANDBY, all peripherals are off
  839. except internal SRAM, Flash and JTAG. Before to start using a peripheral
  840. you have to enable its interface clock. You can do this using
  841. RCC_AHBPeriphClockCmd(), RCC_APB2PeriphClockCmd() and RCC_APB1PeriphClockCmd() functions.
  842. (#) To reset the peripherals configuration (to the default state after device reset)
  843. you can use RCC_AHBPeriphResetCmd(), RCC_APB2PeriphResetCmd() and
  844. RCC_APB1PeriphResetCmd() functions.
  845. (#) To further reduce power consumption in SLEEP mode the peripheral clocks
  846. can be disabled prior to executing the WFI or WFE instructions.
  847. You can do this using RCC_AHBPeriphClockLPModeCmd(),
  848. RCC_APB2PeriphClockLPModeCmd() and RCC_APB1PeriphClockLPModeCmd() functions.
  849. @endverbatim
  850. * @{
  851. */
  852. /**
  853. * @brief Configures the RTC clock (RTCCLK).
  854. * @note As the RTC clock configuration bits are in the Backup domain and write
  855. * access is denied to this domain after reset, you have to enable write
  856. * access using PWR_BackupAccessCmd(ENABLE) function before to configure
  857. * the RTC clock source (to be done once after reset).
  858. * @note Once the RTC clock is configured it can't be changed unless the
  859. * Backup domain is reset using RCC_BackupResetCmd() function, or by
  860. * a Power On Reset (POR).
  861. *
  862. * @param RCC_RTCCLKSource: specifies the RTC clock source.
  863. * This parameter can be one of the following values:
  864. * @arg RCC_RTCCLKSource_LSE: LSE selected as RTC clock
  865. * @arg RCC_RTCCLKSource_LSI: LSI selected as RTC clock
  866. * @arg RCC_RTCCLKSource_HSE_Divx: HSE clock divided by x selected
  867. * as RTC clock, where x:[2,31]
  868. *
  869. * @note If the LSE or LSI is used as RTC clock source, the RTC continues to
  870. * work in STOP and STANDBY modes, and can be used as wakeup source.
  871. * However, when the HSE clock is used as RTC clock source, the RTC
  872. * cannot be used in STOP and STANDBY modes.
  873. * @note The maximum input clock frequency for RTC is 1MHz (when using HSE as
  874. * RTC clock source).
  875. *
  876. * @retval None
  877. */
  878. void RCC_RTCCLKConfig(uint32_t RCC_RTCCLKSource)
  879. {
  880. uint32_t tmpreg = 0;
  881. /* Check the parameters */
  882. assert_param(IS_RCC_RTCCLK_SOURCE(RCC_RTCCLKSource));
  883. if ((RCC_RTCCLKSource & 0x00000300) == 0x00000300)
  884. { /* If HSE is selected as RTC clock source, configure HSE division factor for RTC clock */
  885. tmpreg = RCC->CFGR;
  886. /* Clear RTCPRE[4:0] bits */
  887. tmpreg &= ~RCC_CFGR_RTCPRE;
  888. /* Configure HSE division factor for RTC clock */
  889. tmpreg |= (RCC_RTCCLKSource & 0xFFFFCFF);
  890. /* Store the new value */
  891. RCC->CFGR = tmpreg;
  892. }
  893. /* Select the RTC clock source */
  894. RCC->BDCR |= (RCC_RTCCLKSource & 0x00000FFF);
  895. }
  896. /**
  897. * @brief Enables or disables the RTC clock.
  898. * @note This function must be used only after the RTC clock source was selected
  899. * using the RCC_RTCCLKConfig function.
  900. * @param NewState: new state of the RTC clock. This parameter can be: ENABLE or DISABLE.
  901. * @retval None
  902. */
  903. void RCC_RTCCLKCmd(FunctionalState NewState)
  904. {
  905. /* Check the parameters */
  906. assert_param(IS_FUNCTIONAL_STATE(NewState));
  907. *(__IO uint32_t *) BDCR_RTCEN_BB = (uint32_t)NewState;
  908. }
  909. /**
  910. * @brief Forces or releases the Backup domain reset.
  911. * @note This function resets the RTC peripheral (including the backup registers)
  912. * and the RTC clock source selection in RCC_CSR register.
  913. * @note The BKPSRAM is not affected by this reset.
  914. * @param NewState: new state of the Backup domain reset.
  915. * This parameter can be: ENABLE or DISABLE.
  916. * @retval None
  917. */
  918. void RCC_BackupResetCmd(FunctionalState NewState)
  919. {
  920. /* Check the parameters */
  921. assert_param(IS_FUNCTIONAL_STATE(NewState));
  922. *(__IO uint32_t *) BDCR_BDRST_BB = (uint32_t)NewState;
  923. }
  924. /**
  925. * @brief Configures the I2S clock source (I2SCLK).
  926. * @note This function must be called before enabling the I2S APB clock.
  927. * @param RCC_I2SCLKSource: specifies the I2S clock source.
  928. * This parameter can be one of the following values:
  929. * @arg RCC_I2S2CLKSource_PLLI2S: PLLI2S clock used as I2S clock source
  930. * @arg RCC_I2S2CLKSource_Ext: External clock mapped on the I2S_CKIN pin
  931. * used as I2S clock source
  932. * @retval None
  933. */
  934. void RCC_I2SCLKConfig(uint32_t RCC_I2SCLKSource)
  935. {
  936. /* Check the parameters */
  937. assert_param(IS_RCC_I2SCLK_SOURCE(RCC_I2SCLKSource));
  938. *(__IO uint32_t *) CFGR_I2SSRC_BB = RCC_I2SCLKSource;
  939. }
  940. /**
  941. * @brief Configures the Timers clocks prescalers selection.
  942. *
  943. * @note This feature is only available with STM32F427x/437x Devices.
  944. * @param RCC_TIMCLKPrescaler : specifies the Timers clocks prescalers selection
  945. * This parameter can be one of the following values:
  946. * @arg RCC_TIMPrescDesactivated: The Timers kernels clocks prescaler is
  947. * equal to HPRE if PPREx is corresponding to division by 1 or 2,
  948. * else it is equal to [(HPRE * PPREx) / 2] if PPREx is corresponding to
  949. * division by 4 or more.
  950. *
  951. * @arg RCC_TIMPrescActivated: The Timers kernels clocks prescaler is
  952. * equal to HPRE if PPREx is corresponding to division by 1, 2 or 4,
  953. * else it is equal to [(HPRE * PPREx) / 4] if PPREx is corresponding
  954. * to division by 8 or more.
  955. * @retval None
  956. */
  957. void RCC_TIMCLKPresConfig(uint32_t RCC_TIMCLKPrescaler)
  958. {
  959. /* Check the parameters */
  960. assert_param(IS_RCC_TIMCLK_PRESCALER(RCC_TIMCLKPrescaler));
  961. *(__IO uint32_t *) DCKCFGR_TIMPRE_BB = RCC_TIMCLKPrescaler;
  962. }
  963. /**
  964. * @brief Enables or disables the AHB1 peripheral clock.
  965. * @note After reset, the peripheral clock (used for registers read/write access)
  966. * is disabled and the application software has to enable this clock before
  967. * using it.
  968. * @param RCC_AHBPeriph: specifies the AHB1 peripheral to gates its clock.
  969. * This parameter can be any combination of the following values:
  970. * @arg RCC_AHB1Periph_GPIOA: GPIOA clock
  971. * @arg RCC_AHB1Periph_GPIOB: GPIOB clock
  972. * @arg RCC_AHB1Periph_GPIOC: GPIOC clock
  973. * @arg RCC_AHB1Periph_GPIOD: GPIOD clock
  974. * @arg RCC_AHB1Periph_GPIOE: GPIOE clock
  975. * @arg RCC_AHB1Periph_GPIOF: GPIOF clock
  976. * @arg RCC_AHB1Periph_GPIOG: GPIOG clock
  977. * @arg RCC_AHB1Periph_GPIOG: GPIOG clock
  978. * @arg RCC_AHB1Periph_GPIOI: GPIOI clock
  979. * @arg RCC_AHB1Periph_CRC: CRC clock
  980. * @arg RCC_AHB1Periph_BKPSRAM: BKPSRAM interface clock
  981. * @arg RCC_AHB1Periph_CCMDATARAMEN CCM data RAM interface clock
  982. * @arg RCC_AHB1Periph_DMA1: DMA1 clock
  983. * @arg RCC_AHB1Periph_DMA2: DMA2 clock
  984. * @arg RCC_AHB1Periph_ETH_MAC: Ethernet MAC clock
  985. * @arg RCC_AHB1Periph_ETH_MAC_Tx: Ethernet Transmission clock
  986. * @arg RCC_AHB1Periph_ETH_MAC_Rx: Ethernet Reception clock
  987. * @arg RCC_AHB1Periph_ETH_MAC_PTP: Ethernet PTP clock
  988. * @arg RCC_AHB1Periph_OTG_HS: USB OTG HS clock
  989. * @arg RCC_AHB1Periph_OTG_HS_ULPI: USB OTG HS ULPI clock
  990. * @param NewState: new state of the specified peripheral clock.
  991. * This parameter can be: ENABLE or DISABLE.
  992. * @retval None
  993. */
  994. void RCC_AHB1PeriphClockCmd(uint32_t RCC_AHB1Periph, FunctionalState NewState)
  995. {
  996. /* Check the parameters */
  997. assert_param(IS_RCC_AHB1_CLOCK_PERIPH(RCC_AHB1Periph));
  998. assert_param(IS_FUNCTIONAL_STATE(NewState));
  999. if (NewState != DISABLE)
  1000. {
  1001. RCC->AHB1ENR |= RCC_AHB1Periph;
  1002. }
  1003. else
  1004. {
  1005. RCC->AHB1ENR &= ~RCC_AHB1Periph;
  1006. }
  1007. }
  1008. /**
  1009. * @brief Enables or disables the AHB2 peripheral clock.
  1010. * @note After reset, the peripheral clock (used for registers read/write access)
  1011. * is disabled and the application software has to enable this clock before
  1012. * using it.
  1013. * @param RCC_AHBPeriph: specifies the AHB2 peripheral to gates its clock.
  1014. * This parameter can be any combination of the following values:
  1015. * @arg RCC_AHB2Periph_DCMI: DCMI clock
  1016. * @arg RCC_AHB2Periph_CRYP: CRYP clock
  1017. * @arg RCC_AHB2Periph_HASH: HASH clock
  1018. * @arg RCC_AHB2Periph_RNG: RNG clock
  1019. * @arg RCC_AHB2Periph_OTG_FS: USB OTG FS clock
  1020. * @param NewState: new state of the specified peripheral clock.
  1021. * This parameter can be: ENABLE or DISABLE.
  1022. * @retval None
  1023. */
  1024. void RCC_AHB2PeriphClockCmd(uint32_t RCC_AHB2Periph, FunctionalState NewState)
  1025. {
  1026. /* Check the parameters */
  1027. assert_param(IS_RCC_AHB2_PERIPH(RCC_AHB2Periph));
  1028. assert_param(IS_FUNCTIONAL_STATE(NewState));
  1029. if (NewState != DISABLE)
  1030. {
  1031. RCC->AHB2ENR |= RCC_AHB2Periph;
  1032. }
  1033. else
  1034. {
  1035. RCC->AHB2ENR &= ~RCC_AHB2Periph;
  1036. }
  1037. }
  1038. /**
  1039. * @brief Enables or disables the AHB3 peripheral clock.
  1040. * @note After reset, the peripheral clock (used for registers read/write access)
  1041. * is disabled and the application software has to enable this clock before
  1042. * using it.
  1043. * @param RCC_AHBPeriph: specifies the AHB3 peripheral to gates its clock.
  1044. * This parameter must be: RCC_AHB3Periph_FSMC
  1045. *
  1046. * @param NewState: new state of the specified peripheral clock.
  1047. * This parameter can be: ENABLE or DISABLE.
  1048. * @retval None
  1049. */
  1050. void RCC_AHB3PeriphClockCmd(uint32_t RCC_AHB3Periph, FunctionalState NewState)
  1051. {
  1052. /* Check the parameters */
  1053. assert_param(IS_RCC_AHB3_PERIPH(RCC_AHB3Periph));
  1054. assert_param(IS_FUNCTIONAL_STATE(NewState));
  1055. if (NewState != DISABLE)
  1056. {
  1057. RCC->AHB3ENR |= RCC_AHB3Periph;
  1058. }
  1059. else
  1060. {
  1061. RCC->AHB3ENR &= ~RCC_AHB3Periph;
  1062. }
  1063. }
  1064. /**
  1065. * @brief Enables or disables the Low Speed APB (APB1) peripheral clock.
  1066. * @note After reset, the peripheral clock (used for registers read/write access)
  1067. * is disabled and the application software has to enable this clock before
  1068. * using it.
  1069. * @param RCC_APB1Periph: specifies the APB1 peripheral to gates its clock.
  1070. * This parameter can be any combination of the following values:
  1071. * @arg RCC_APB1Periph_TIM2: TIM2 clock
  1072. * @arg RCC_APB1Periph_TIM3: TIM3 clock
  1073. * @arg RCC_APB1Periph_TIM4: TIM4 clock
  1074. * @arg RCC_APB1Periph_TIM5: TIM5 clock
  1075. * @arg RCC_APB1Periph_TIM6: TIM6 clock
  1076. * @arg RCC_APB1Periph_TIM7: TIM7 clock
  1077. * @arg RCC_APB1Periph_TIM12: TIM12 clock
  1078. * @arg RCC_APB1Periph_TIM13: TIM13 clock
  1079. * @arg RCC_APB1Periph_TIM14: TIM14 clock
  1080. * @arg RCC_APB1Periph_WWDG: WWDG clock
  1081. * @arg RCC_APB1Periph_SPI2: SPI2 clock
  1082. * @arg RCC_APB1Periph_SPI3: SPI3 clock
  1083. * @arg RCC_APB1Periph_USART2: USART2 clock
  1084. * @arg RCC_APB1Periph_USART3: USART3 clock
  1085. * @arg RCC_APB1Periph_UART4: UART4 clock
  1086. * @arg RCC_APB1Periph_UART5: UART5 clock
  1087. * @arg RCC_APB1Periph_I2C1: I2C1 clock
  1088. * @arg RCC_APB1Periph_I2C2: I2C2 clock
  1089. * @arg RCC_APB1Periph_I2C3: I2C3 clock
  1090. * @arg RCC_APB1Periph_CAN1: CAN1 clock
  1091. * @arg RCC_APB1Periph_CAN2: CAN2 clock
  1092. * @arg RCC_APB1Periph_PWR: PWR clock
  1093. * @arg RCC_APB1Periph_DAC: DAC clock
  1094. * @arg RCC_APB1Periph_UART7: UART7 clock
  1095. * @arg RCC_APB1Periph_UART8: UART8 clock
  1096. * @param NewState: new state of the specified peripheral clock.
  1097. * This parameter can be: ENABLE or DISABLE.
  1098. * @retval None
  1099. */
  1100. void RCC_APB1PeriphClockCmd(uint32_t RCC_APB1Periph, FunctionalState NewState)
  1101. {
  1102. /* Check the parameters */
  1103. assert_param(IS_RCC_APB1_PERIPH(RCC_APB1Periph));
  1104. assert_param(IS_FUNCTIONAL_STATE(NewState));
  1105. if (NewState != DISABLE)
  1106. {
  1107. RCC->APB1ENR |= RCC_APB1Periph;
  1108. }
  1109. else
  1110. {
  1111. RCC->APB1ENR &= ~RCC_APB1Periph;
  1112. }
  1113. }
  1114. /**
  1115. * @brief Enables or disables the High Speed APB (APB2) peripheral clock.
  1116. * @note After reset, the peripheral clock (used for registers read/write access)
  1117. * is disabled and the application software has to enable this clock before
  1118. * using it.
  1119. * @param RCC_APB2Periph: specifies the APB2 peripheral to gates its clock.
  1120. * This parameter can be any combination of the following values:
  1121. * @arg RCC_APB2Periph_TIM1: TIM1 clock
  1122. * @arg RCC_APB2Periph_TIM8: TIM8 clock
  1123. * @arg RCC_APB2Periph_USART1: USART1 clock
  1124. * @arg RCC_APB2Periph_USART6: USART6 clock
  1125. * @arg RCC_APB2Periph_ADC1: ADC1 clock
  1126. * @arg RCC_APB2Periph_ADC2: ADC2 clock
  1127. * @arg RCC_APB2Periph_ADC3: ADC3 clock
  1128. * @arg RCC_APB2Periph_SDIO: SDIO clock
  1129. * @arg RCC_APB2Periph_SPI1: SPI1 clock
  1130. * @arg RCC_APB2Periph_SPI4: SPI4 clock
  1131. * @arg RCC_APB2Periph_SYSCFG: SYSCFG clock
  1132. * @arg RCC_APB2Periph_TIM9: TIM9 clock
  1133. * @arg RCC_APB2Periph_TIM10: TIM10 clock
  1134. * @arg RCC_APB2Periph_TIM11: TIM11 clock
  1135. * @arg RCC_APB2Periph_SPI5: SPI5 clock
  1136. * @arg RCC_APB2Periph_SPI6: SPI6 clock
  1137. * @param NewState: new state of the specified peripheral clock.
  1138. * This parameter can be: ENABLE or DISABLE.
  1139. * @retval None
  1140. */
  1141. void RCC_APB2PeriphClockCmd(uint32_t RCC_APB2Periph, FunctionalState NewState)
  1142. {
  1143. /* Check the parameters */
  1144. assert_param(IS_RCC_APB2_PERIPH(RCC_APB2Periph));
  1145. assert_param(IS_FUNCTIONAL_STATE(NewState));
  1146. if (NewState != DISABLE)
  1147. {
  1148. RCC->APB2ENR |= RCC_APB2Periph;
  1149. }
  1150. else
  1151. {
  1152. RCC->APB2ENR &= ~RCC_APB2Periph;
  1153. }
  1154. }
  1155. /**
  1156. * @brief Forces or releases AHB1 peripheral reset.
  1157. * @param RCC_AHB1Periph: specifies the AHB1 peripheral to reset.
  1158. * This parameter can be any combination of the following values:
  1159. * @arg RCC_AHB1Periph_GPIOA: GPIOA clock
  1160. * @arg RCC_AHB1Periph_GPIOB: GPIOB clock
  1161. * @arg RCC_AHB1Periph_GPIOC: GPIOC clock
  1162. * @arg RCC_AHB1Periph_GPIOD: GPIOD clock
  1163. * @arg RCC_AHB1Periph_GPIOE: GPIOE clock
  1164. * @arg RCC_AHB1Periph_GPIOF: GPIOF clock
  1165. * @arg RCC_AHB1Periph_GPIOG: GPIOG clock
  1166. * @arg RCC_AHB1Periph_GPIOG: GPIOG clock
  1167. * @arg RCC_AHB1Periph_GPIOI: GPIOI clock
  1168. * @arg RCC_AHB1Periph_CRC: CRC clock
  1169. * @arg RCC_AHB1Periph_DMA1: DMA1 clock
  1170. * @arg RCC_AHB1Periph_DMA2: DMA2 clock
  1171. * @arg RCC_AHB1Periph_ETH_MAC: Ethernet MAC clock
  1172. * @arg RCC_AHB1Periph_OTG_HS: USB OTG HS clock
  1173. *
  1174. * @param NewState: new state of the specified peripheral reset.
  1175. * This parameter can be: ENABLE or DISABLE.
  1176. * @retval None
  1177. */
  1178. void RCC_AHB1PeriphResetCmd(uint32_t RCC_AHB1Periph, FunctionalState NewState)
  1179. {
  1180. /* Check the parameters */
  1181. assert_param(IS_RCC_AHB1_RESET_PERIPH(RCC_AHB1Periph));
  1182. assert_param(IS_FUNCTIONAL_STATE(NewState));
  1183. if (NewState != DISABLE)
  1184. {
  1185. RCC->AHB1RSTR |= RCC_AHB1Periph;
  1186. }
  1187. else
  1188. {
  1189. RCC->AHB1RSTR &= ~RCC_AHB1Periph;
  1190. }
  1191. }
  1192. /**
  1193. * @brief Forces or releases AHB2 peripheral reset.
  1194. * @param RCC_AHB2Periph: specifies the AHB2 peripheral to reset.
  1195. * This parameter can be any combination of the following values:
  1196. * @arg RCC_AHB2Periph_DCMI: DCMI clock
  1197. * @arg RCC_AHB2Periph_CRYP: CRYP clock
  1198. * @arg RCC_AHB2Periph_HASH: HASH clock
  1199. * @arg RCC_AHB2Periph_RNG: RNG clock
  1200. * @arg RCC_AHB2Periph_OTG_FS: USB OTG FS clock
  1201. * @param NewState: new state of the specified peripheral reset.
  1202. * This parameter can be: ENABLE or DISABLE.
  1203. * @retval None
  1204. */
  1205. void RCC_AHB2PeriphResetCmd(uint32_t RCC_AHB2Periph, FunctionalState NewState)
  1206. {
  1207. /* Check the parameters */
  1208. assert_param(IS_RCC_AHB2_PERIPH(RCC_AHB2Periph));
  1209. assert_param(IS_FUNCTIONAL_STATE(NewState));
  1210. if (NewState != DISABLE)
  1211. {
  1212. RCC->AHB2RSTR |= RCC_AHB2Periph;
  1213. }
  1214. else
  1215. {
  1216. RCC->AHB2RSTR &= ~RCC_AHB2Periph;
  1217. }
  1218. }
  1219. /**
  1220. * @brief Forces or releases AHB3 peripheral reset.
  1221. * @param RCC_AHB3Periph: specifies the AHB3 peripheral to reset.
  1222. * This parameter must be: RCC_AHB3Periph_FSMC
  1223. *
  1224. * @param NewState: new state of the specified peripheral reset.
  1225. * This parameter can be: ENABLE or DISABLE.
  1226. * @retval None
  1227. */
  1228. void RCC_AHB3PeriphResetCmd(uint32_t RCC_AHB3Periph, FunctionalState NewState)
  1229. {
  1230. /* Check the parameters */
  1231. assert_param(IS_RCC_AHB3_PERIPH(RCC_AHB3Periph));
  1232. assert_param(IS_FUNCTIONAL_STATE(NewState));
  1233. if (NewState != DISABLE)
  1234. {
  1235. RCC->AHB3RSTR |= RCC_AHB3Periph;
  1236. }
  1237. else
  1238. {
  1239. RCC->AHB3RSTR &= ~RCC_AHB3Periph;
  1240. }
  1241. }
  1242. /**
  1243. * @brief Forces or releases Low Speed APB (APB1) peripheral reset.
  1244. * @param RCC_APB1Periph: specifies the APB1 peripheral to reset.
  1245. * This parameter can be any combination of the following values:
  1246. * @arg RCC_APB1Periph_TIM2: TIM2 clock
  1247. * @arg RCC_APB1Periph_TIM3: TIM3 clock
  1248. * @arg RCC_APB1Periph_TIM4: TIM4 clock
  1249. * @arg RCC_APB1Periph_TIM5: TIM5 clock
  1250. * @arg RCC_APB1Periph_TIM6: TIM6 clock
  1251. * @arg RCC_APB1Periph_TIM7: TIM7 clock
  1252. * @arg RCC_APB1Periph_TIM12: TIM12 clock
  1253. * @arg RCC_APB1Periph_TIM13: TIM13 clock
  1254. * @arg RCC_APB1Periph_TIM14: TIM14 clock
  1255. * @arg RCC_APB1Periph_WWDG: WWDG clock
  1256. * @arg RCC_APB1Periph_SPI2: SPI2 clock
  1257. * @arg RCC_APB1Periph_SPI3: SPI3 clock
  1258. * @arg RCC_APB1Periph_USART2: USART2 clock
  1259. * @arg RCC_APB1Periph_USART3: USART3 clock
  1260. * @arg RCC_APB1Periph_UART4: UART4 clock
  1261. * @arg RCC_APB1Periph_UART5: UART5 clock
  1262. * @arg RCC_APB1Periph_I2C1: I2C1 clock
  1263. * @arg RCC_APB1Periph_I2C2: I2C2 clock
  1264. * @arg RCC_APB1Periph_I2C3: I2C3 clock
  1265. * @arg RCC_APB1Periph_CAN1: CAN1 clock
  1266. * @arg RCC_APB1Periph_CAN2: CAN2 clock
  1267. * @arg RCC_APB1Periph_PWR: PWR clock
  1268. * @arg RCC_APB1Periph_DAC: DAC clock
  1269. * @arg RCC_APB1Periph_UART7: UART7 clock
  1270. * @arg RCC_APB1Periph_UART8: UART8 clock
  1271. * @param NewState: new state of the specified peripheral reset.
  1272. * This parameter can be: ENABLE or DISABLE.
  1273. * @retval None
  1274. */
  1275. void RCC_APB1PeriphResetCmd(uint32_t RCC_APB1Periph, FunctionalState NewState)
  1276. {
  1277. /* Check the parameters */
  1278. assert_param(IS_RCC_APB1_PERIPH(RCC_APB1Periph));
  1279. assert_param(IS_FUNCTIONAL_STATE(NewState));
  1280. if (NewState != DISABLE)
  1281. {
  1282. RCC->APB1RSTR |= RCC_APB1Periph;
  1283. }
  1284. else
  1285. {
  1286. RCC->APB1RSTR &= ~RCC_APB1Periph;
  1287. }
  1288. }
  1289. /**
  1290. * @brief Forces or releases High Speed APB (APB2) peripheral reset.
  1291. * @param RCC_APB2Periph: specifies the APB2 peripheral to reset.
  1292. * This parameter can be any combination of the following values:
  1293. * @arg RCC_APB2Periph_TIM1: TIM1 clock
  1294. * @arg RCC_APB2Periph_TIM8: TIM8 clock
  1295. * @arg RCC_APB2Periph_USART1: USART1 clock
  1296. * @arg RCC_APB2Periph_USART6: USART6 clock
  1297. * @arg RCC_APB2Periph_ADC1: ADC1 clock
  1298. * @arg RCC_APB2Periph_ADC2: ADC2 clock
  1299. * @arg RCC_APB2Periph_ADC3: ADC3 clock
  1300. * @arg RCC_APB2Periph_SDIO: SDIO clock
  1301. * @arg RCC_APB2Periph_SPI1: SPI1 clock
  1302. * @arg RCC_APB2Periph_SPI4: SPI4 clock
  1303. * @arg RCC_APB2Periph_SYSCFG: SYSCFG clock
  1304. * @arg RCC_APB2Periph_TIM9: TIM9 clock
  1305. * @arg RCC_APB2Periph_TIM10: TIM10 clock
  1306. * @arg RCC_APB2Periph_TIM11: TIM11 clock
  1307. * @arg RCC_APB2Periph_SPI5: SPI5 clock
  1308. * @arg RCC_APB2Periph_SPI6: SPI6 clock
  1309. * @param NewState: new state of the specified peripheral reset.
  1310. * This parameter can be: ENABLE or DISABLE.
  1311. * @retval None
  1312. */
  1313. void RCC_APB2PeriphResetCmd(uint32_t RCC_APB2Periph, FunctionalState NewState)
  1314. {
  1315. /* Check the parameters */
  1316. assert_param(IS_RCC_APB2_RESET_PERIPH(RCC_APB2Periph));
  1317. assert_param(IS_FUNCTIONAL_STATE(NewState));
  1318. if (NewState != DISABLE)
  1319. {
  1320. RCC->APB2RSTR |= RCC_APB2Periph;
  1321. }
  1322. else
  1323. {
  1324. RCC->APB2RSTR &= ~RCC_APB2Periph;
  1325. }
  1326. }
  1327. /**
  1328. * @brief Enables or disables the AHB1 peripheral clock during Low Power (Sleep) mode.
  1329. * @note Peripheral clock gating in SLEEP mode can be used to further reduce
  1330. * power consumption.
  1331. * @note After wakeup from SLEEP mode, the peripheral clock is enabled again.
  1332. * @note By default, all peripheral clocks are enabled during SLEEP mode.
  1333. * @param RCC_AHBPeriph: specifies the AHB1 peripheral to gates its clock.
  1334. * This parameter can be any combination of the following values:
  1335. * @arg RCC_AHB1Periph_GPIOA: GPIOA clock
  1336. * @arg RCC_AHB1Periph_GPIOB: GPIOB clock
  1337. * @arg RCC_AHB1Periph_GPIOC: GPIOC clock
  1338. * @arg RCC_AHB1Periph_GPIOD: GPIOD clock
  1339. * @arg RCC_AHB1Periph_GPIOE: GPIOE clock
  1340. * @arg RCC_AHB1Periph_GPIOF: GPIOF clock
  1341. * @arg RCC_AHB1Periph_GPIOG: GPIOG clock
  1342. * @arg RCC_AHB1Periph_GPIOG: GPIOG clock
  1343. * @arg RCC_AHB1Periph_GPIOI: GPIOI clock
  1344. * @arg RCC_AHB1Periph_CRC: CRC clock
  1345. * @arg RCC_AHB1Periph_BKPSRAM: BKPSRAM interface clock
  1346. * @arg RCC_AHB1Periph_DMA1: DMA1 clock
  1347. * @arg RCC_AHB1Periph_DMA2: DMA2 clock
  1348. * @arg RCC_AHB1Periph_ETH_MAC: Ethernet MAC clock
  1349. * @arg RCC_AHB1Periph_ETH_MAC_Tx: Ethernet Transmission clock
  1350. * @arg RCC_AHB1Periph_ETH_MAC_Rx: Ethernet Reception clock
  1351. * @arg RCC_AHB1Periph_ETH_MAC_PTP: Ethernet PTP clock
  1352. * @arg RCC_AHB1Periph_OTG_HS: USB OTG HS clock
  1353. * @arg RCC_AHB1Periph_OTG_HS_ULPI: USB OTG HS ULPI clock
  1354. * @param NewState: new state of the specified peripheral clock.
  1355. * This parameter can be: ENABLE or DISABLE.
  1356. * @retval None
  1357. */
  1358. void RCC_AHB1PeriphClockLPModeCmd(uint32_t RCC_AHB1Periph, FunctionalState NewState)
  1359. {
  1360. /* Check the parameters */
  1361. assert_param(IS_RCC_AHB1_LPMODE_PERIPH(RCC_AHB1Periph));
  1362. assert_param(IS_FUNCTIONAL_STATE(NewState));
  1363. if (NewState != DISABLE)
  1364. {
  1365. RCC->AHB1LPENR |= RCC_AHB1Periph;
  1366. }
  1367. else
  1368. {
  1369. RCC->AHB1LPENR &= ~RCC_AHB1Periph;
  1370. }
  1371. }
  1372. /**
  1373. * @brief Enables or disables the AHB2 peripheral clock during Low Power (Sleep) mode.
  1374. * @note Peripheral clock gating in SLEEP mode can be used to further reduce
  1375. * power consumption.
  1376. * @note After wakeup from SLEEP mode, the peripheral clock is enabled again.
  1377. * @note By default, all peripheral clocks are enabled during SLEEP mode.
  1378. * @param RCC_AHBPeriph: specifies the AHB2 peripheral to gates its clock.
  1379. * This parameter can be any combination of the following values:
  1380. * @arg RCC_AHB2Periph_DCMI: DCMI clock
  1381. * @arg RCC_AHB2Periph_CRYP: CRYP clock
  1382. * @arg RCC_AHB2Periph_HASH: HASH clock
  1383. * @arg RCC_AHB2Periph_RNG: RNG clock
  1384. * @arg RCC_AHB2Periph_OTG_FS: USB OTG FS clock
  1385. * @param NewState: new state of the specified peripheral clock.
  1386. * This parameter can be: ENABLE or DISABLE.
  1387. * @retval None
  1388. */
  1389. void RCC_AHB2PeriphClockLPModeCmd(uint32_t RCC_AHB2Periph, FunctionalState NewState)
  1390. {
  1391. /* Check the parameters */
  1392. assert_param(IS_RCC_AHB2_PERIPH(RCC_AHB2Periph));
  1393. assert_param(IS_FUNCTIONAL_STATE(NewState));
  1394. if (NewState != DISABLE)
  1395. {
  1396. RCC->AHB2LPENR |= RCC_AHB2Periph;
  1397. }
  1398. else
  1399. {
  1400. RCC->AHB2LPENR &= ~RCC_AHB2Periph;
  1401. }
  1402. }
  1403. /**
  1404. * @brief Enables or disables the AHB3 peripheral clock during Low Power (Sleep) mode.
  1405. * @note Peripheral clock gating in SLEEP mode can be used to further reduce
  1406. * power consumption.
  1407. * @note After wakeup from SLEEP mode, the peripheral clock is enabled again.
  1408. * @note By default, all peripheral clocks are enabled during SLEEP mode.
  1409. * @param RCC_AHBPeriph: specifies the AHB3 peripheral to gates its clock.
  1410. * This parameter must be: RCC_AHB3Periph_FSMC
  1411. *
  1412. * @param NewState: new state of the specified peripheral clock.
  1413. * This parameter can be: ENABLE or DISABLE.
  1414. * @retval None
  1415. */
  1416. void RCC_AHB3PeriphClockLPModeCmd(uint32_t RCC_AHB3Periph, FunctionalState NewState)
  1417. {
  1418. /* Check the parameters */
  1419. assert_param(IS_RCC_AHB3_PERIPH(RCC_AHB3Periph));
  1420. assert_param(IS_FUNCTIONAL_STATE(NewState));
  1421. if (NewState != DISABLE)
  1422. {
  1423. RCC->AHB3LPENR |= RCC_AHB3Periph;
  1424. }
  1425. else
  1426. {
  1427. RCC->AHB3LPENR &= ~RCC_AHB3Periph;
  1428. }
  1429. }
  1430. /**
  1431. * @brief Enables or disables the APB1 peripheral clock during Low Power (Sleep) mode.
  1432. * @note Peripheral clock gating in SLEEP mode can be used to further reduce
  1433. * power consumption.
  1434. * @note After wakeup from SLEEP mode, the peripheral clock is enabled again.
  1435. * @note By default, all peripheral clocks are enabled during SLEEP mode.
  1436. * @param RCC_APB1Periph: specifies the APB1 peripheral to gates its clock.
  1437. * This parameter can be any combination of the following values:
  1438. * @arg RCC_APB1Periph_TIM2: TIM2 clock
  1439. * @arg RCC_APB1Periph_TIM3: TIM3 clock
  1440. * @arg RCC_APB1Periph_TIM4: TIM4 clock
  1441. * @arg RCC_APB1Periph_TIM5: TIM5 clock
  1442. * @arg RCC_APB1Periph_TIM6: TIM6 clock
  1443. * @arg RCC_APB1Periph_TIM7: TIM7 clock
  1444. * @arg RCC_APB1Periph_TIM12: TIM12 clock
  1445. * @arg RCC_APB1Periph_TIM13: TIM13 clock
  1446. * @arg RCC_APB1Periph_TIM14: TIM14 clock
  1447. * @arg RCC_APB1Periph_WWDG: WWDG clock
  1448. * @arg RCC_APB1Periph_SPI2: SPI2 clock
  1449. * @arg RCC_APB1Periph_SPI3: SPI3 clock
  1450. * @arg RCC_APB1Periph_USART2: USART2 clock
  1451. * @arg RCC_APB1Periph_USART3: USART3 clock
  1452. * @arg RCC_APB1Periph_UART4: UART4 clock
  1453. * @arg RCC_APB1Periph_UART5: UART5 clock
  1454. * @arg RCC_APB1Periph_I2C1: I2C1 clock
  1455. * @arg RCC_APB1Periph_I2C2: I2C2 clock
  1456. * @arg RCC_APB1Periph_I2C3: I2C3 clock
  1457. * @arg RCC_APB1Periph_CAN1: CAN1 clock
  1458. * @arg RCC_APB1Periph_CAN2: CAN2 clock
  1459. * @arg RCC_APB1Periph_PWR: PWR clock
  1460. * @arg RCC_APB1Periph_DAC: DAC clock
  1461. * @arg RCC_APB1Periph_UART7: UART7 clock
  1462. * @arg RCC_APB1Periph_UART8: UART8 clock
  1463. * @param NewState: new state of the specified peripheral clock.
  1464. * This parameter can be: ENABLE or DISABLE.
  1465. * @retval None
  1466. */
  1467. void RCC_APB1PeriphClockLPModeCmd(uint32_t RCC_APB1Periph, FunctionalState NewState)
  1468. {
  1469. /* Check the parameters */
  1470. assert_param(IS_RCC_APB1_PERIPH(RCC_APB1Periph));
  1471. assert_param(IS_FUNCTIONAL_STATE(NewState));
  1472. if (NewState != DISABLE)
  1473. {
  1474. RCC->APB1LPENR |= RCC_APB1Periph;
  1475. }
  1476. else
  1477. {
  1478. RCC->APB1LPENR &= ~RCC_APB1Periph;
  1479. }
  1480. }
  1481. /**
  1482. * @brief Enables or disables the APB2 peripheral clock during Low Power (Sleep) mode.
  1483. * @note Peripheral clock gating in SLEEP mode can be used to further reduce
  1484. * power consumption.
  1485. * @note After wakeup from SLEEP mode, the peripheral clock is enabled again.
  1486. * @note By default, all peripheral clocks are enabled during SLEEP mode.
  1487. * @param RCC_APB2Periph: specifies the APB2 peripheral to gates its clock.
  1488. * This parameter can be any combination of the following values:
  1489. * @arg RCC_APB2Periph_TIM1: TIM1 clock
  1490. * @arg RCC_APB2Periph_TIM8: TIM8 clock
  1491. * @arg RCC_APB2Periph_USART1: USART1 clock
  1492. * @arg RCC_APB2Periph_USART6: USART6 clock
  1493. * @arg RCC_APB2Periph_ADC1: ADC1 clock
  1494. * @arg RCC_APB2Periph_ADC2: ADC2 clock
  1495. * @arg RCC_APB2Periph_ADC3: ADC3 clock
  1496. * @arg RCC_APB2Periph_SDIO: SDIO clock
  1497. * @arg RCC_APB2Periph_SPI1: SPI1 clock
  1498. * @arg RCC_APB2Periph_SPI4: SPI4 clock
  1499. * @arg RCC_APB2Periph_SYSCFG: SYSCFG clock
  1500. * @arg RCC_APB2Periph_TIM9: TIM9 clock
  1501. * @arg RCC_APB2Periph_TIM10: TIM10 clock
  1502. * @arg RCC_APB2Periph_TIM11: TIM11 clock
  1503. * @arg RCC_APB2Periph_SPI5: SPI5 clock
  1504. * @arg RCC_APB2Periph_SPI6: SPI6 clock
  1505. * @param NewState: new state of the specified peripheral clock.
  1506. * This parameter can be: ENABLE or DISABLE.
  1507. * @retval None
  1508. */
  1509. void RCC_APB2PeriphClockLPModeCmd(uint32_t RCC_APB2Periph, FunctionalState NewState)
  1510. {
  1511. /* Check the parameters */
  1512. assert_param(IS_RCC_APB2_PERIPH(RCC_APB2Periph));
  1513. assert_param(IS_FUNCTIONAL_STATE(NewState));
  1514. if (NewState != DISABLE)
  1515. {
  1516. RCC->APB2LPENR |= RCC_APB2Periph;
  1517. }
  1518. else
  1519. {
  1520. RCC->APB2LPENR &= ~RCC_APB2Periph;
  1521. }
  1522. }
  1523. /**
  1524. * @}
  1525. */
  1526. /** @defgroup RCC_Group4 Interrupts and flags management functions
  1527. * @brief Interrupts and flags management functions
  1528. *
  1529. @verbatim
  1530. ===============================================================================
  1531. ##### Interrupts and flags management functions #####
  1532. ===============================================================================
  1533. @endverbatim
  1534. * @{
  1535. */
  1536. /**
  1537. * @brief Enables or disables the specified RCC interrupts.
  1538. * @param RCC_IT: specifies the RCC interrupt sources to be enabled or disabled.
  1539. * This parameter can be any combination of the following values:
  1540. * @arg RCC_IT_LSIRDY: LSI ready interrupt
  1541. * @arg RCC_IT_LSERDY: LSE ready interrupt
  1542. * @arg RCC_IT_HSIRDY: HSI ready interrupt
  1543. * @arg RCC_IT_HSERDY: HSE ready interrupt
  1544. * @arg RCC_IT_PLLRDY: main PLL ready interrupt
  1545. * @arg RCC_IT_PLLI2SRDY: PLLI2S ready interrupt
  1546. *
  1547. * @param NewState: new state of the specified RCC interrupts.
  1548. * This parameter can be: ENABLE or DISABLE.
  1549. * @retval None
  1550. */
  1551. void RCC_ITConfig(uint8_t RCC_IT, FunctionalState NewState)
  1552. {
  1553. /* Check the parameters */
  1554. assert_param(IS_RCC_IT(RCC_IT));
  1555. assert_param(IS_FUNCTIONAL_STATE(NewState));
  1556. if (NewState != DISABLE)
  1557. {
  1558. /* Perform Byte access to RCC_CIR[14:8] bits to enable the selected interrupts */
  1559. *(__IO uint8_t *) CIR_BYTE2_ADDRESS |= RCC_IT;
  1560. }
  1561. else
  1562. {
  1563. /* Perform Byte access to RCC_CIR[14:8] bits to disable the selected interrupts */
  1564. *(__IO uint8_t *) CIR_BYTE2_ADDRESS &= (uint8_t)~RCC_IT;
  1565. }
  1566. }
  1567. /**
  1568. * @brief Checks whether the specified RCC flag is set or not.
  1569. * @param RCC_FLAG: specifies the flag to check.
  1570. * This parameter can be one of the following values:
  1571. * @arg RCC_FLAG_HSIRDY: HSI oscillator clock ready
  1572. * @arg RCC_FLAG_HSERDY: HSE oscillator clock ready
  1573. * @arg RCC_FLAG_PLLRDY: main PLL clock ready
  1574. * @arg RCC_FLAG_PLLI2SRDY: PLLI2S clock ready
  1575. * @arg RCC_FLAG_LSERDY: LSE oscillator clock ready
  1576. * @arg RCC_FLAG_LSIRDY: LSI oscillator clock ready
  1577. * @arg RCC_FLAG_BORRST: POR/PDR or BOR reset
  1578. * @arg RCC_FLAG_PINRST: Pin reset
  1579. * @arg RCC_FLAG_PORRST: POR/PDR reset
  1580. * @arg RCC_FLAG_SFTRST: Software reset
  1581. * @arg RCC_FLAG_IWDGRST: Independent Watchdog reset
  1582. * @arg RCC_FLAG_WWDGRST: Window Watchdog reset
  1583. * @arg RCC_FLAG_LPWRRST: Low Power reset
  1584. * @retval The new state of RCC_FLAG (SET or RESET).
  1585. */
  1586. FlagStatus RCC_GetFlagStatus(uint8_t RCC_FLAG)
  1587. {
  1588. uint32_t tmp = 0;
  1589. uint32_t statusreg = 0;
  1590. FlagStatus bitstatus = RESET;
  1591. /* Check the parameters */
  1592. assert_param(IS_RCC_FLAG(RCC_FLAG));
  1593. /* Get the RCC register index */
  1594. tmp = RCC_FLAG >> 5;
  1595. if (tmp == 1) /* The flag to check is in CR register */
  1596. {
  1597. statusreg = RCC->CR;
  1598. }
  1599. else if (tmp == 2) /* The flag to check is in BDCR register */
  1600. {
  1601. statusreg = RCC->BDCR;
  1602. }
  1603. else /* The flag to check is in CSR register */
  1604. {
  1605. statusreg = RCC->CSR;
  1606. }
  1607. /* Get the flag position */
  1608. tmp = RCC_FLAG & FLAG_MASK;
  1609. if ((statusreg & ((uint32_t)1 << tmp)) != (uint32_t)RESET)
  1610. {
  1611. bitstatus = SET;
  1612. }
  1613. else
  1614. {
  1615. bitstatus = RESET;
  1616. }
  1617. /* Return the flag status */
  1618. return bitstatus;
  1619. }
  1620. /**
  1621. * @brief Clears the RCC reset flags.
  1622. * The reset flags are: RCC_FLAG_PINRST, RCC_FLAG_PORRST, RCC_FLAG_SFTRST,
  1623. * RCC_FLAG_IWDGRST, RCC_FLAG_WWDGRST, RCC_FLAG_LPWRRST
  1624. * @param None
  1625. * @retval None
  1626. */
  1627. void RCC_ClearFlag(void)
  1628. {
  1629. /* Set RMVF bit to clear the reset flags */
  1630. RCC->CSR |= RCC_CSR_RMVF;
  1631. }
  1632. /**
  1633. * @brief Checks whether the specified RCC interrupt has occurred or not.
  1634. * @param RCC_IT: specifies the RCC interrupt source to check.
  1635. * This parameter can be one of the following values:
  1636. * @arg RCC_IT_LSIRDY: LSI ready interrupt
  1637. * @arg RCC_IT_LSERDY: LSE ready interrupt
  1638. * @arg RCC_IT_HSIRDY: HSI ready interrupt
  1639. * @arg RCC_IT_HSERDY: HSE ready interrupt
  1640. * @arg RCC_IT_PLLRDY: main PLL ready interrupt
  1641. * @arg RCC_IT_PLLI2SRDY: PLLI2S ready interrupt
  1642. * @arg RCC_IT_CSS: Clock Security System interrupt
  1643. * @retval The new state of RCC_IT (SET or RESET).
  1644. */
  1645. ITStatus RCC_GetITStatus(uint8_t RCC_IT)
  1646. {
  1647. ITStatus bitstatus = RESET;
  1648. /* Check the parameters */
  1649. assert_param(IS_RCC_GET_IT(RCC_IT));
  1650. /* Check the status of the specified RCC interrupt */
  1651. if ((RCC->CIR & RCC_IT) != (uint32_t)RESET)
  1652. {
  1653. bitstatus = SET;
  1654. }
  1655. else
  1656. {
  1657. bitstatus = RESET;
  1658. }
  1659. /* Return the RCC_IT status */
  1660. return bitstatus;
  1661. }
  1662. /**
  1663. * @brief Clears the RCC's interrupt pending bits.
  1664. * @param RCC_IT: specifies the interrupt pending bit to clear.
  1665. * This parameter can be any combination of the following values:
  1666. * @arg RCC_IT_LSIRDY: LSI ready interrupt
  1667. * @arg RCC_IT_LSERDY: LSE ready interrupt
  1668. * @arg RCC_IT_HSIRDY: HSI ready interrupt
  1669. * @arg RCC_IT_HSERDY: HSE ready interrupt
  1670. * @arg RCC_IT_PLLRDY: main PLL ready interrupt
  1671. * @arg RCC_IT_PLLI2SRDY: PLLI2S ready interrupt
  1672. * @arg RCC_IT_CSS: Clock Security System interrupt
  1673. * @retval None
  1674. */
  1675. void RCC_ClearITPendingBit(uint8_t RCC_IT)
  1676. {
  1677. /* Check the parameters */
  1678. assert_param(IS_RCC_CLEAR_IT(RCC_IT));
  1679. /* Perform Byte access to RCC_CIR[23:16] bits to clear the selected interrupt
  1680. pending bits */
  1681. *(__IO uint8_t *) CIR_BYTE3_ADDRESS = RCC_IT;
  1682. }
  1683. /**
  1684. * @}
  1685. */
  1686. /**
  1687. * @}
  1688. */
  1689. /**
  1690. * @}
  1691. */
  1692. /**
  1693. * @}
  1694. */
  1695. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/