stm32f4xx_rcc.c 73 KB

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