stm32f0xx_rcc.c 69 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782
  1. /**
  2. ******************************************************************************
  3. * @file stm32f0xx_rcc.c
  4. * @author MCD Application Team
  5. * @version V1.5.0
  6. * @date 05-December-2014
  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. [..] After reset the device is running from HSI (8 MHz) with Flash 0 WS,
  19. all peripherals are off except internal SRAM, Flash and SWD.
  20. (#) There is no prescaler on High speed (AHB) and Low speed (APB) busses;
  21. all peripherals mapped on these busses are running at HSI speed.
  22. (#) The clock for all peripherals is switched off, except the SRAM and FLASH.
  23. (#) All GPIOs are in input floating state, except the SWD pins which
  24. are assigned to be used for debug purpose.
  25. [..] Once the device started from reset, the user application has to:
  26. (#) Configure the clock source to be used to drive the System clock
  27. (if the application needs higher frequency/performance)
  28. (#) Configure the System clock frequency and Flash settings
  29. (#) Configure the AHB and APB busses prescalers
  30. (#) Enable the clock for the peripheral(s) to be used
  31. (#) Configure the clock source(s) for peripherals which clocks are not
  32. derived from the System clock (ADC, CEC, I2C, USART, RTC and IWDG)
  33. @endverbatim
  34. ******************************************************************************
  35. * @attention
  36. *
  37. * <h2><center>&copy; COPYRIGHT 2014 STMicroelectronics</center></h2>
  38. *
  39. * Licensed under MCD-ST Liberty SW License Agreement V2, (the "License");
  40. * You may not use this file except in compliance with the License.
  41. * You may obtain a copy of the License at:
  42. *
  43. * http://www.st.com/software_license_agreement_liberty_v2
  44. *
  45. * Unless required by applicable law or agreed to in writing, software
  46. * distributed under the License is distributed on an "AS IS" BASIS,
  47. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  48. * See the License for the specific language governing permissions and
  49. * limitations under the License.
  50. *
  51. ******************************************************************************
  52. */
  53. /* Includes ------------------------------------------------------------------*/
  54. #include "stm32f0xx_rcc.h"
  55. /** @addtogroup STM32F0xx_StdPeriph_Driver
  56. * @{
  57. */
  58. /** @defgroup RCC
  59. * @brief RCC driver modules
  60. * @{
  61. */
  62. /* Private typedef -----------------------------------------------------------*/
  63. /* Private define ------------------------------------------------------------*/
  64. /* ---------------------- RCC registers mask -------------------------------- */
  65. /* RCC Flag Mask */
  66. #define FLAG_MASK ((uint8_t)0x1F)
  67. /* CR register byte 2 (Bits[23:16]) base address */
  68. #define CR_BYTE2_ADDRESS ((uint32_t)0x40021002)
  69. /* CFGR register byte 3 (Bits[31:23]) base address */
  70. #define CFGR_BYTE3_ADDRESS ((uint32_t)0x40021007)
  71. /* CIR register byte 1 (Bits[15:8]) base address */
  72. #define CIR_BYTE1_ADDRESS ((uint32_t)0x40021009)
  73. /* CIR register byte 2 (Bits[23:16]) base address */
  74. #define CIR_BYTE2_ADDRESS ((uint32_t)0x4002100A)
  75. /* Private macro -------------------------------------------------------------*/
  76. /* Private variables ---------------------------------------------------------*/
  77. static __I uint8_t APBAHBPrescTable[16] = {0, 0, 0, 0, 1, 2, 3, 4, 1, 2, 3, 4, 6, 7, 8, 9};
  78. /* Private function prototypes -----------------------------------------------*/
  79. /* Private functions ---------------------------------------------------------*/
  80. /** @defgroup RCC_Private_Functions
  81. * @{
  82. */
  83. /** @defgroup RCC_Group1 Internal and external clocks, PLL, CSS and MCO configuration functions
  84. * @brief Internal and external clocks, PLL, CSS and MCO configuration functions
  85. *
  86. @verbatim
  87. ===============================================================================
  88. ##### Internal-external clocks, PLL, CSS and MCO configuration functions #####
  89. ===============================================================================
  90. [..] This section provides functions allowing to configure the internal/external clocks,
  91. PLL, CSS and MCO.
  92. (#) HSI (high-speed internal), 8 MHz factory-trimmed RC used directly
  93. or through the PLL as System clock source.
  94. The HSI clock can be used also to clock the USART, I2C and CEC peripherals.
  95. (#) HSI14 (high-speed internal for ADC), 14 MHz factory-trimmed RC used to clock
  96. the ADC peripheral.
  97. (#) LSI (low-speed internal), 40 KHz low consumption RC used as IWDG and/or RTC
  98. clock source.
  99. (#) HSE (high-speed external), 4 to 32 MHz crystal oscillator used directly or
  100. through the PLL as System clock source. Can be used also as RTC clock source.
  101. (#) LSE (low-speed external), 32 KHz oscillator used as RTC clock source.
  102. LSE can be used also to clock the USART and CEC peripherals.
  103. (#) PLL (clocked by HSI or HSE), for System clock.
  104. (#) CSS (Clock security system), once enabled and if a HSE clock failure occurs
  105. (HSE used directly or through PLL as System clock source), the System clock
  106. is automatically switched to HSI and an interrupt is generated if enabled.
  107. The interrupt is linked to the Cortex-M0 NMI (Non-Maskable Interrupt)
  108. exception vector.
  109. (#) MCO (microcontroller clock output), used to output SYSCLK, HSI, HSI14, LSI,
  110. HSE, LSE or PLL (divided by 2) clock on PA8 pin.
  111. @endverbatim
  112. * @{
  113. */
  114. /**
  115. * @brief Resets the RCC clock configuration to the default reset state.
  116. * @note The default reset state of the clock configuration is given below:
  117. * @note HSI ON and used as system clock source
  118. * @note HSI14, HSE and PLL OFF
  119. * @note AHB, APB prescaler set to 1.
  120. * @note CSS and MCO OFF
  121. * @note All interrupts disabled
  122. * @note However, this function doesn't modify the configuration of the
  123. * @note Peripheral clocks
  124. * @note LSI, LSE and RTC clocks
  125. * @param None
  126. * @retval None
  127. */
  128. void RCC_DeInit(void)
  129. {
  130. /* Set HSION bit */
  131. RCC->CR |= (uint32_t)0x00000001;
  132. #if defined (STM32F051)
  133. /* Reset SW[1:0], HPRE[3:0], PPRE[2:0] and MCOSEL[2:0] bits */
  134. RCC->CFGR &= (uint32_t)0xF8FFB80C;
  135. #else
  136. /* Reset SW[1:0], HPRE[3:0], PPRE[2:0], ADCPRE, MCOSEL[2:0], MCOPRE[2:0] and PLLNODIV bits */
  137. RCC->CFGR &= (uint32_t)0x08FFB80C;
  138. #endif /* STM32F051 */
  139. /* Reset HSEON, CSSON and PLLON bits */
  140. RCC->CR &= (uint32_t)0xFEF6FFFF;
  141. /* Reset HSEBYP bit */
  142. RCC->CR &= (uint32_t)0xFFFBFFFF;
  143. /* Reset PLLSRC, PLLXTPRE and PLLMUL[3:0] bits */
  144. RCC->CFGR &= (uint32_t)0xFFC0FFFF;
  145. /* Reset PREDIV1[3:0] bits */
  146. RCC->CFGR2 &= (uint32_t)0xFFFFFFF0;
  147. /* Reset USARTSW[1:0], I2CSW, CECSW and ADCSW bits */
  148. RCC->CFGR3 &= (uint32_t)0xFFF0FEAC;
  149. /* Reset HSI14 bit */
  150. RCC->CR2 &= (uint32_t)0xFFFFFFFE;
  151. /* Disable all interrupts */
  152. RCC->CIR = 0x00000000;
  153. }
  154. /**
  155. * @brief Configures the External High Speed oscillator (HSE).
  156. * @note After enabling the HSE (RCC_HSE_ON or RCC_HSE_Bypass), the application
  157. * software should wait on HSERDY flag to be set indicating that HSE clock
  158. * is stable and can be used to clock the PLL and/or system clock.
  159. * @note HSE state can not be changed if it is used directly or through the
  160. * PLL as system clock. In this case, you have to select another source
  161. * of the system clock then change the HSE state (ex. disable it).
  162. * @note The HSE is stopped by hardware when entering STOP and STANDBY modes.
  163. * @note This function resets the CSSON bit, so if the Clock security system(CSS)
  164. * was previously enabled you have to enable it again after calling this
  165. * function.
  166. * @param RCC_HSE: specifies the new state of the HSE.
  167. * This parameter can be one of the following values:
  168. * @arg RCC_HSE_OFF: turn OFF the HSE oscillator, HSERDY flag goes low after
  169. * 6 HSE oscillator clock cycles.
  170. * @arg RCC_HSE_ON: turn ON the HSE oscillator
  171. * @arg RCC_HSE_Bypass: HSE oscillator bypassed with external clock
  172. * @retval None
  173. */
  174. void RCC_HSEConfig(uint8_t RCC_HSE)
  175. {
  176. /* Check the parameters */
  177. assert_param(IS_RCC_HSE(RCC_HSE));
  178. /* Reset HSEON and HSEBYP bits before configuring the HSE ------------------*/
  179. *(__IO uint8_t *) CR_BYTE2_ADDRESS = RCC_HSE_OFF;
  180. /* Set the new HSE configuration -------------------------------------------*/
  181. *(__IO uint8_t *) CR_BYTE2_ADDRESS = RCC_HSE;
  182. }
  183. /**
  184. * @brief Waits for HSE start-up.
  185. * @note This function waits on HSERDY flag to be set and return SUCCESS if
  186. * this flag is set, otherwise returns ERROR if the timeout is reached
  187. * and this flag is not set. The timeout value is defined by the constant
  188. * HSE_STARTUP_TIMEOUT in stm32f0xx.h file. You can tailor it depending
  189. * on the HSE crystal used in your application.
  190. * @note The HSE is stopped by hardware when entering STOP and STANDBY modes.
  191. * @param None
  192. * @retval An ErrorStatus enumeration value:
  193. * - SUCCESS: HSE oscillator is stable and ready to use
  194. * - ERROR: HSE oscillator not yet ready
  195. */
  196. ErrorStatus RCC_WaitForHSEStartUp(void)
  197. {
  198. __IO uint32_t StartUpCounter = 0;
  199. ErrorStatus status = ERROR;
  200. FlagStatus HSEStatus = RESET;
  201. /* Wait till HSE is ready and if timeout is reached exit */
  202. do
  203. {
  204. HSEStatus = RCC_GetFlagStatus(RCC_FLAG_HSERDY);
  205. StartUpCounter++;
  206. } while((StartUpCounter != HSE_STARTUP_TIMEOUT) && (HSEStatus == RESET));
  207. if (RCC_GetFlagStatus(RCC_FLAG_HSERDY) != RESET)
  208. {
  209. status = SUCCESS;
  210. }
  211. else
  212. {
  213. status = ERROR;
  214. }
  215. return (status);
  216. }
  217. /**
  218. * @brief Adjusts the Internal High Speed oscillator (HSI) calibration value.
  219. * @note The calibration is used to compensate for the variations in voltage
  220. * and temperature that influence the frequency of the internal HSI RC.
  221. * Refer to the Application Note AN4067 for more details on how to
  222. * calibrate the HSI.
  223. * @param HSICalibrationValue: specifies the HSI calibration trimming value.
  224. * This parameter must be a number between 0 and 0x1F.
  225. * @retval None
  226. */
  227. void RCC_AdjustHSICalibrationValue(uint8_t HSICalibrationValue)
  228. {
  229. uint32_t tmpreg = 0;
  230. /* Check the parameters */
  231. assert_param(IS_RCC_HSI_CALIBRATION_VALUE(HSICalibrationValue));
  232. tmpreg = RCC->CR;
  233. /* Clear HSITRIM[4:0] bits */
  234. tmpreg &= ~RCC_CR_HSITRIM;
  235. /* Set the HSITRIM[4:0] bits according to HSICalibrationValue value */
  236. tmpreg |= (uint32_t)HSICalibrationValue << 3;
  237. /* Store the new value */
  238. RCC->CR = tmpreg;
  239. }
  240. /**
  241. * @brief Enables or disables the Internal High Speed oscillator (HSI).
  242. * @note After enabling the HSI, the application software should wait on
  243. * HSIRDY flag to be set indicating that HSI clock is stable and can
  244. * be used to clock the PLL and/or system clock.
  245. * @note HSI can not be stopped if it is used directly or through the PLL
  246. * as system clock. In this case, you have to select another source
  247. * of the system clock then stop the HSI.
  248. * @note The HSI is stopped by hardware when entering STOP and STANDBY modes.
  249. * @param NewState: new state of the HSI.
  250. * This parameter can be: ENABLE or DISABLE.
  251. * @note When the HSI is stopped, HSIRDY flag goes low after 6 HSI oscillator
  252. * clock cycles.
  253. * @retval None
  254. */
  255. void RCC_HSICmd(FunctionalState NewState)
  256. {
  257. /* Check the parameters */
  258. assert_param(IS_FUNCTIONAL_STATE(NewState));
  259. if (NewState != DISABLE)
  260. {
  261. RCC->CR |= RCC_CR_HSION;
  262. }
  263. else
  264. {
  265. RCC->CR &= ~RCC_CR_HSION;
  266. }
  267. }
  268. /**
  269. * @brief Adjusts the Internal High Speed oscillator for ADC (HSI14)
  270. * calibration value.
  271. * @note The calibration is used to compensate for the variations in voltage
  272. * and temperature that influence the frequency of the internal HSI RC.
  273. * Refer to the Application Note AN4067 for more details on how to
  274. * calibrate the HSI14.
  275. * @param HSI14CalibrationValue: specifies the HSI14 calibration trimming value.
  276. * This parameter must be a number between 0 and 0x1F.
  277. * @retval None
  278. */
  279. void RCC_AdjustHSI14CalibrationValue(uint8_t HSI14CalibrationValue)
  280. {
  281. uint32_t tmpreg = 0;
  282. /* Check the parameters */
  283. assert_param(IS_RCC_HSI14_CALIBRATION_VALUE(HSI14CalibrationValue));
  284. tmpreg = RCC->CR2;
  285. /* Clear HSI14TRIM[4:0] bits */
  286. tmpreg &= ~RCC_CR2_HSI14TRIM;
  287. /* Set the HSITRIM14[4:0] bits according to HSI14CalibrationValue value */
  288. tmpreg |= (uint32_t)HSI14CalibrationValue << 3;
  289. /* Store the new value */
  290. RCC->CR2 = tmpreg;
  291. }
  292. /**
  293. * @brief Enables or disables the Internal High Speed oscillator for ADC (HSI14).
  294. * @note After enabling the HSI14, the application software should wait on
  295. * HSIRDY flag to be set indicating that HSI clock is stable and can
  296. * be used to clock the ADC.
  297. * @note The HSI14 is stopped by hardware when entering STOP and STANDBY modes.
  298. * @param NewState: new state of the HSI14.
  299. * This parameter can be: ENABLE or DISABLE.
  300. * @note When the HSI14 is stopped, HSI14RDY flag goes low after 6 HSI14 oscillator
  301. * clock cycles.
  302. * @retval None
  303. */
  304. void RCC_HSI14Cmd(FunctionalState NewState)
  305. {
  306. /* Check the parameters */
  307. assert_param(IS_FUNCTIONAL_STATE(NewState));
  308. if (NewState != DISABLE)
  309. {
  310. RCC->CR2 |= RCC_CR2_HSI14ON;
  311. }
  312. else
  313. {
  314. RCC->CR2 &= ~RCC_CR2_HSI14ON;
  315. }
  316. }
  317. /**
  318. * @brief Enables or disables the Internal High Speed oscillator request from ADC.
  319. * @param NewState: new state of the HSI14 ADC request.
  320. * This parameter can be: ENABLE or DISABLE.
  321. * @retval None
  322. */
  323. void RCC_HSI14ADCRequestCmd(FunctionalState NewState)
  324. {
  325. /* Check the parameters */
  326. assert_param(IS_FUNCTIONAL_STATE(NewState));
  327. if (NewState != DISABLE)
  328. {
  329. RCC->CR2 &= ~RCC_CR2_HSI14DIS;
  330. }
  331. else
  332. {
  333. RCC->CR2 |= RCC_CR2_HSI14DIS;
  334. }
  335. }
  336. /**
  337. * @brief Configures the External Low Speed oscillator (LSE).
  338. * @note As the LSE is in the Backup domain and write access is denied to this
  339. * domain after reset, you have to enable write access using
  340. * PWR_BackupAccessCmd(ENABLE) function before to configure the LSE
  341. * (to be done once after reset).
  342. * @note After enabling the LSE (RCC_LSE_ON or RCC_LSE_Bypass), the application
  343. * software should wait on LSERDY flag to be set indicating that LSE clock
  344. * is stable and can be used to clock the RTC.
  345. * @param RCC_LSE: specifies the new state of the LSE.
  346. * This parameter can be one of the following values:
  347. * @arg RCC_LSE_OFF: turn OFF the LSE oscillator, LSERDY flag goes low after
  348. * 6 LSE oscillator clock cycles.
  349. * @arg RCC_LSE_ON: turn ON the LSE oscillator
  350. * @arg RCC_LSE_Bypass: LSE oscillator bypassed with external clock
  351. * @retval None
  352. */
  353. void RCC_LSEConfig(uint32_t RCC_LSE)
  354. {
  355. /* Check the parameters */
  356. assert_param(IS_RCC_LSE(RCC_LSE));
  357. /* Reset LSEON and LSEBYP bits before configuring the LSE ------------------*/
  358. /* Reset LSEON bit */
  359. RCC->BDCR &= ~(RCC_BDCR_LSEON);
  360. /* Reset LSEBYP bit */
  361. RCC->BDCR &= ~(RCC_BDCR_LSEBYP);
  362. /* Configure LSE */
  363. RCC->BDCR |= RCC_LSE;
  364. }
  365. /**
  366. * @brief Configures the External Low Speed oscillator (LSE) drive capability.
  367. * @param RCC_LSEDrive: specifies the new state of the LSE drive capability.
  368. * This parameter can be one of the following values:
  369. * @arg RCC_LSEDrive_Low: LSE oscillator low drive capability.
  370. * @arg RCC_LSEDrive_MediumLow: LSE oscillator medium low drive capability.
  371. * @arg RCC_LSEDrive_MediumHigh: LSE oscillator medium high drive capability.
  372. * @arg RCC_LSEDrive_High: LSE oscillator high drive capability.
  373. * @retval None
  374. */
  375. void RCC_LSEDriveConfig(uint32_t RCC_LSEDrive)
  376. {
  377. /* Check the parameters */
  378. assert_param(IS_RCC_LSE_DRIVE(RCC_LSEDrive));
  379. /* Clear LSEDRV[1:0] bits */
  380. RCC->BDCR &= ~(RCC_BDCR_LSEDRV);
  381. /* Set the LSE Drive */
  382. RCC->BDCR |= RCC_LSEDrive;
  383. }
  384. /**
  385. * @brief Enables or disables the Internal Low Speed oscillator (LSI).
  386. * @note After enabling the LSI, the application software should wait on
  387. * LSIRDY flag to be set indicating that LSI clock is stable and can
  388. * be used to clock the IWDG and/or the RTC.
  389. * @note LSI can not be disabled if the IWDG is running.
  390. * @param NewState: new state of the LSI.
  391. * This parameter can be: ENABLE or DISABLE.
  392. * @note When the LSI is stopped, LSIRDY flag goes low after 6 LSI oscillator
  393. * clock cycles.
  394. * @retval None
  395. */
  396. void RCC_LSICmd(FunctionalState NewState)
  397. {
  398. /* Check the parameters */
  399. assert_param(IS_FUNCTIONAL_STATE(NewState));
  400. if (NewState != DISABLE)
  401. {
  402. RCC->CSR |= RCC_CSR_LSION;
  403. }
  404. else
  405. {
  406. RCC->CSR &= ~RCC_CSR_LSION;
  407. }
  408. }
  409. /**
  410. * @brief Configures the PLL clock source and multiplication factor.
  411. * @note This function must be used only when the PLL is disabled.
  412. *
  413. * @param RCC_PLLSource: specifies the PLL entry clock source.
  414. * This parameter can be one of the following values:
  415. * @arg RCC_PLLSource_HSI_Div2: HSI oscillator clock selected as PLL clock source
  416. * @arg RCC_PLLSource_PREDIV1: PREDIV1 clock selected as PLL clock entry
  417. * @arg RCC_PLLSource_HSI48 HSI48 oscillator clock selected as PLL clock source, applicable only for STM32F072 devices
  418. * @arg RCC_PLLSource_HSI: HSI clock selected as PLL clock entry, applicable only for STM32F072 devices
  419. * @note The minimum input clock frequency for PLL is 2 MHz (when using HSE as
  420. * PLL source).
  421. *
  422. * @param RCC_PLLMul: specifies the PLL multiplication factor, which drive the PLLVCO clock
  423. * This parameter can be RCC_PLLMul_x where x:[2,16]
  424. *
  425. * @retval None
  426. */
  427. void RCC_PLLConfig(uint32_t RCC_PLLSource, uint32_t RCC_PLLMul)
  428. {
  429. /* Check the parameters */
  430. assert_param(IS_RCC_PLL_SOURCE(RCC_PLLSource));
  431. assert_param(IS_RCC_PLL_MUL(RCC_PLLMul));
  432. /* Clear PLL Source [16] and Multiplier [21:18] bits */
  433. RCC->CFGR &= ~(RCC_CFGR_PLLMULL | RCC_CFGR_PLLSRC);
  434. /* Set the PLL Source and Multiplier */
  435. RCC->CFGR |= (uint32_t)(RCC_PLLSource | RCC_PLLMul);
  436. }
  437. /**
  438. * @brief Enables or disables the PLL.
  439. * @note After enabling the PLL, the application software should wait on
  440. * PLLRDY flag to be set indicating that PLL clock is stable and can
  441. * be used as system clock source.
  442. * @note The PLL can not be disabled if it is used as system clock source
  443. * @note The PLL is disabled by hardware when entering STOP and STANDBY modes.
  444. * @param NewState: new state of the PLL.
  445. * This parameter can be: ENABLE or DISABLE.
  446. * @retval None
  447. */
  448. void RCC_PLLCmd(FunctionalState NewState)
  449. {
  450. /* Check the parameters */
  451. assert_param(IS_FUNCTIONAL_STATE(NewState));
  452. if (NewState != DISABLE)
  453. {
  454. RCC->CR |= RCC_CR_PLLON;
  455. }
  456. else
  457. {
  458. RCC->CR &= ~RCC_CR_PLLON;
  459. }
  460. }
  461. /**
  462. * @brief Enables or disables the Internal High Speed oscillator for USB (HSI48).
  463. * This function is only applicable for STM32F072 devices.
  464. * @note After enabling the HSI48, the application software should wait on
  465. * HSI48RDY flag to be set indicating that HSI48 clock is stable and can
  466. * be used to clock the USB.
  467. * @note The HSI48 is stopped by hardware when entering STOP and STANDBY modes.
  468. * @param NewState: new state of the HSI48.
  469. * This parameter can be: ENABLE or DISABLE.
  470. * @retval None
  471. */
  472. void RCC_HSI48Cmd(FunctionalState NewState)
  473. {
  474. /* Check the parameters */
  475. assert_param(IS_FUNCTIONAL_STATE(NewState));
  476. if (NewState != DISABLE)
  477. {
  478. RCC->CR2 |= RCC_CR2_HSI48ON;
  479. }
  480. else
  481. {
  482. RCC->CR2 &= ~RCC_CR2_HSI48ON;
  483. }
  484. }
  485. /**
  486. * @brief Configures the PREDIV1 division factor.
  487. * @note This function must be used only when the PLL is disabled.
  488. * @param RCC_PREDIV1_Div: specifies the PREDIV1 clock division factor.
  489. * This parameter can be RCC_PREDIV1_Divx where x:[1,16]
  490. * @retval None
  491. */
  492. void RCC_PREDIV1Config(uint32_t RCC_PREDIV1_Div)
  493. {
  494. uint32_t tmpreg = 0;
  495. /* Check the parameters */
  496. assert_param(IS_RCC_PREDIV1(RCC_PREDIV1_Div));
  497. tmpreg = RCC->CFGR2;
  498. /* Clear PREDIV1[3:0] bits */
  499. tmpreg &= ~(RCC_CFGR2_PREDIV1);
  500. /* Set the PREDIV1 division factor */
  501. tmpreg |= RCC_PREDIV1_Div;
  502. /* Store the new value */
  503. RCC->CFGR2 = tmpreg;
  504. }
  505. /**
  506. * @brief Enables or disables the Clock Security System.
  507. * @note If a failure is detected on the HSE oscillator clock, this oscillator
  508. * is automatically disabled and an interrupt is generated to inform the
  509. * software about the failure (Clock Security System Interrupt, CSSI),
  510. * allowing the MCU to perform rescue operations. The CSSI is linked to
  511. * the Cortex-M0 NMI (Non-Maskable Interrupt) exception vector.
  512. * @param NewState: new state of the Clock Security System.
  513. * This parameter can be: ENABLE or DISABLE.
  514. * @retval None
  515. */
  516. void RCC_ClockSecuritySystemCmd(FunctionalState NewState)
  517. {
  518. /* Check the parameters */
  519. assert_param(IS_FUNCTIONAL_STATE(NewState));
  520. if (NewState != DISABLE)
  521. {
  522. RCC->CR |= RCC_CR_CSSON;
  523. }
  524. else
  525. {
  526. RCC->CR &= ~RCC_CR_CSSON;
  527. }
  528. }
  529. #ifdef STM32F051
  530. /**
  531. * @brief Selects the clock source to output on MCO pin (PA8).
  532. * @note PA8 should be configured in alternate function mode.
  533. * @param RCC_MCOSource: specifies the clock source to output.
  534. * This parameter can be one of the following values:
  535. * @arg RCC_MCOSource_NoClock: No clock selected.
  536. * @arg RCC_MCOSource_HSI14: HSI14 oscillator clock selected.
  537. * @arg RCC_MCOSource_LSI: LSI oscillator clock selected.
  538. * @arg RCC_MCOSource_LSE: LSE oscillator clock selected.
  539. * @arg RCC_MCOSource_SYSCLK: System clock selected.
  540. * @arg RCC_MCOSource_HSI: HSI oscillator clock selected.
  541. * @arg RCC_MCOSource_HSE: HSE oscillator clock selected.
  542. * @arg RCC_MCOSource_PLLCLK_Div2: PLL clock divided by 2 selected.
  543. * @retval None
  544. */
  545. void RCC_MCOConfig(uint8_t RCC_MCOSource)
  546. {
  547. /* Check the parameters */
  548. assert_param(IS_RCC_MCO_SOURCE(RCC_MCOSource));
  549. /* Select MCO clock source and prescaler */
  550. *(__IO uint8_t *) CFGR_BYTE3_ADDRESS = RCC_MCOSource;
  551. }
  552. #else
  553. /**
  554. * @brief Selects the clock source to output on MCO pin (PA8) and the corresponding
  555. * prescsaler.
  556. * @note PA8 should be configured in alternate function mode.
  557. * @param RCC_MCOSource: specifies the clock source to output.
  558. * This parameter can be one of the following values:
  559. * @arg RCC_MCOSource_NoClock: No clock selected.
  560. * @arg RCC_MCOSource_HSI14: HSI14 oscillator clock selected.
  561. * @arg RCC_MCOSource_LSI: LSI oscillator clock selected.
  562. * @arg RCC_MCOSource_LSE: LSE oscillator clock selected.
  563. * @arg RCC_MCOSource_SYSCLK: System clock selected.
  564. * @arg RCC_MCOSource_HSI: HSI oscillator clock selected.
  565. * @arg RCC_MCOSource_HSE: HSE oscillator clock selected.
  566. * @arg RCC_MCOSource_PLLCLK_Div2: PLL clock divided by 2 selected.
  567. * @arg RCC_MCOSource_PLLCLK: PLL clock selected.
  568. * @arg RCC_MCOSource_HSI48: HSI48 clock selected.
  569. * @param RCC_MCOPrescaler: specifies the prescaler on MCO pin.
  570. * This parameter can be one of the following values:
  571. * @arg RCC_MCOPrescaler_1: MCO clock is divided by 1.
  572. * @arg RCC_MCOPrescaler_2: MCO clock is divided by 2.
  573. * @arg RCC_MCOPrescaler_4: MCO clock is divided by 4.
  574. * @arg RCC_MCOPrescaler_8: MCO clock is divided by 8.
  575. * @arg RCC_MCOPrescaler_16: MCO clock is divided by 16.
  576. * @arg RCC_MCOPrescaler_32: MCO clock is divided by 32.
  577. * @arg RCC_MCOPrescaler_64: MCO clock is divided by 64.
  578. * @arg RCC_MCOPrescaler_128: MCO clock is divided by 128.
  579. * @retval None
  580. */
  581. void RCC_MCOConfig(uint8_t RCC_MCOSource, uint32_t RCC_MCOPrescaler)
  582. {
  583. uint32_t tmpreg = 0;
  584. /* Check the parameters */
  585. assert_param(IS_RCC_MCO_SOURCE(RCC_MCOSource));
  586. assert_param(IS_RCC_MCO_PRESCALER(RCC_MCOPrescaler));
  587. /* Get CFGR value */
  588. tmpreg = RCC->CFGR;
  589. /* Clear MCOPRE[2:0] bits */
  590. tmpreg &= ~(RCC_CFGR_MCO_PRE | RCC_CFGR_MCO | RCC_CFGR_PLLNODIV);
  591. /* Set the RCC_MCOSource and RCC_MCOPrescaler */
  592. tmpreg |= (RCC_MCOPrescaler | ((uint32_t)RCC_MCOSource<<24));
  593. /* Store the new value */
  594. RCC->CFGR = tmpreg;
  595. }
  596. #endif /* STM32F072 */
  597. /**
  598. * @}
  599. */
  600. /** @defgroup RCC_Group2 System AHB and APB busses clocks configuration functions
  601. * @brief System, AHB and APB busses clocks configuration functions
  602. *
  603. @verbatim
  604. ===============================================================================
  605. ##### System, AHB and APB busses clocks configuration functions #####
  606. ===============================================================================
  607. [..] This section provide functions allowing to configure the System, AHB and
  608. APB busses clocks.
  609. (#) Several clock sources can be used to drive the System clock (SYSCLK): HSI,
  610. HSE and PLL.
  611. The AHB clock (HCLK) is derived from System clock through configurable prescaler
  612. and used to clock the CPU, memory and peripherals mapped on AHB bus (DMA and GPIO).
  613. and APB (PCLK) clocks are derived from AHB clock through
  614. configurable prescalers and used to clock the peripherals mapped on these busses.
  615. You can use "RCC_GetClocksFreq()" function to retrieve the frequencies of these clocks.
  616. -@- All the peripheral clocks are derived from the System clock (SYSCLK) except:
  617. (+@) The ADC clock which is derived from HSI14 or APB (APB divided by a
  618. programmable prescaler: 2 or 4).
  619. (+@) The CEC clock which is derived from LSE or HSI divided by 244.
  620. (+@) The I2C clock which is derived from HSI or system clock (SYSCLK).
  621. (+@) The USART clock which is derived from HSI, system clock (SYSCLK), APB or LSE.
  622. (+@) The RTC/LCD clock which is derived from the LSE, LSI or 2 MHz HSE_RTC (HSE
  623. divided by a programmable prescaler).
  624. The System clock (SYSCLK) frequency must be higher or equal to the RTC/LCD
  625. clock frequency.
  626. (+@) IWDG clock which is always the LSI clock.
  627. (#) The maximum frequency of the SYSCLK, HCLK and PCLK is 48 MHz.
  628. Depending on the maximum frequency, the FLASH wait states (WS) should be
  629. adapted accordingly:
  630. +--------------------------------------------- +
  631. | Wait states | HCLK clock frequency (MHz) |
  632. |---------------|------------------------------|
  633. |0WS(1CPU cycle)| 0 < HCLK <= 24 |
  634. |---------------|------------------------------|
  635. |1WS(2CPU cycle)| 24 < HCLK <= 48 |
  636. +----------------------------------------------+
  637. (#) After reset, the System clock source is the HSI (8 MHz) with 0 WS and
  638. prefetch is disabled.
  639. [..] It is recommended to use the following software sequences to tune the number
  640. of wait states needed to access the Flash memory with the CPU frequency (HCLK).
  641. (+) Increasing the CPU frequency
  642. (++) Program the Flash Prefetch buffer, using "FLASH_PrefetchBufferCmd(ENABLE)"
  643. function
  644. (++) Check that Flash Prefetch buffer activation is taken into account by
  645. reading FLASH_ACR using the FLASH_GetPrefetchBufferStatus() function
  646. (++) Program Flash WS to 1, using "FLASH_SetLatency(FLASH_Latency_1)" function
  647. (++) Check that the new number of WS is taken into account by reading FLASH_ACR
  648. (++) Modify the CPU clock source, using "RCC_SYSCLKConfig()" function
  649. (++) If needed, modify the CPU clock prescaler by using "RCC_HCLKConfig()" function
  650. (++) Check that the new CPU clock source is taken into account by reading
  651. the clock source status, using "RCC_GetSYSCLKSource()" function
  652. (+) Decreasing the CPU frequency
  653. (++) Modify the CPU clock source, using "RCC_SYSCLKConfig()" function
  654. (++) If needed, modify the CPU clock prescaler by using "RCC_HCLKConfig()" function
  655. (++) Check that the new CPU clock source is taken into account by reading
  656. the clock source status, using "RCC_GetSYSCLKSource()" function
  657. (++) Program the new number of WS, using "FLASH_SetLatency()" function
  658. (++) Check that the new number of WS is taken into account by reading FLASH_ACR
  659. (++) Disable the Flash Prefetch buffer using "FLASH_PrefetchBufferCmd(DISABLE)"
  660. function
  661. (++) Check that Flash Prefetch buffer deactivation is taken into account by reading FLASH_ACR
  662. using the FLASH_GetPrefetchBufferStatus() function.
  663. @endverbatim
  664. * @{
  665. */
  666. /**
  667. * @brief Configures the system clock (SYSCLK).
  668. * @note The HSI is used (enabled by hardware) as system clock source after
  669. * startup from Reset, wake-up from STOP and STANDBY mode, or in case
  670. * of failure of the HSE used directly or indirectly as system clock
  671. * (if the Clock Security System CSS is enabled).
  672. * @note A switch from one clock source to another occurs only if the target
  673. * clock source is ready (clock stable after startup delay or PLL locked).
  674. * If a clock source which is not yet ready is selected, the switch will
  675. * occur when the clock source will be ready.
  676. * You can use RCC_GetSYSCLKSource() function to know which clock is
  677. * currently used as system clock source.
  678. * @param RCC_SYSCLKSource: specifies the clock source used as system clock source
  679. * This parameter can be one of the following values:
  680. * @arg RCC_SYSCLKSource_HSI: HSI selected as system clock source
  681. * @arg RCC_SYSCLKSource_HSE: HSE selected as system clock source
  682. * @arg RCC_SYSCLKSource_PLLCLK: PLL selected as system clock source
  683. * @arg RCC_SYSCLKSource_HSI48: HSI48 selected as system clock source, applicable only for STM32F072 devices
  684. * @retval None
  685. */
  686. void RCC_SYSCLKConfig(uint32_t RCC_SYSCLKSource)
  687. {
  688. uint32_t tmpreg = 0;
  689. /* Check the parameters */
  690. assert_param(IS_RCC_SYSCLK_SOURCE(RCC_SYSCLKSource));
  691. tmpreg = RCC->CFGR;
  692. /* Clear SW[1:0] bits */
  693. tmpreg &= ~RCC_CFGR_SW;
  694. /* Set SW[1:0] bits according to RCC_SYSCLKSource value */
  695. tmpreg |= RCC_SYSCLKSource;
  696. /* Store the new value */
  697. RCC->CFGR = tmpreg;
  698. }
  699. /**
  700. * @brief Returns the clock source used as system clock.
  701. * @param None
  702. * @retval The clock source used as system clock. The returned value can be one
  703. * of the following values:
  704. * - 0x00: HSI used as system clock
  705. * - 0x04: HSE used as system clock
  706. * - 0x08: PLL used as system clock
  707. * - 0x0C: HSI48 used as system clock, applicable only for STM32F072 devices
  708. */
  709. uint8_t RCC_GetSYSCLKSource(void)
  710. {
  711. return ((uint8_t)(RCC->CFGR & RCC_CFGR_SWS));
  712. }
  713. /**
  714. * @brief Configures the AHB clock (HCLK).
  715. * @param RCC_SYSCLK: defines the AHB clock divider. This clock is derived from
  716. * the system clock (SYSCLK).
  717. * This parameter can be one of the following values:
  718. * @arg RCC_SYSCLK_Div1: AHB clock = SYSCLK
  719. * @arg RCC_SYSCLK_Div2: AHB clock = SYSCLK/2
  720. * @arg RCC_SYSCLK_Div4: AHB clock = SYSCLK/4
  721. * @arg RCC_SYSCLK_Div8: AHB clock = SYSCLK/8
  722. * @arg RCC_SYSCLK_Div16: AHB clock = SYSCLK/16
  723. * @arg RCC_SYSCLK_Div64: AHB clock = SYSCLK/64
  724. * @arg RCC_SYSCLK_Div128: AHB clock = SYSCLK/128
  725. * @arg RCC_SYSCLK_Div256: AHB clock = SYSCLK/256
  726. * @arg RCC_SYSCLK_Div512: AHB clock = SYSCLK/512
  727. * @retval None
  728. */
  729. void RCC_HCLKConfig(uint32_t RCC_SYSCLK)
  730. {
  731. uint32_t tmpreg = 0;
  732. /* Check the parameters */
  733. assert_param(IS_RCC_HCLK(RCC_SYSCLK));
  734. tmpreg = RCC->CFGR;
  735. /* Clear HPRE[3:0] bits */
  736. tmpreg &= ~RCC_CFGR_HPRE;
  737. /* Set HPRE[3:0] bits according to RCC_SYSCLK value */
  738. tmpreg |= RCC_SYSCLK;
  739. /* Store the new value */
  740. RCC->CFGR = tmpreg;
  741. }
  742. /**
  743. * @brief Configures the APB clock (PCLK).
  744. * @param RCC_HCLK: defines the APB clock divider. This clock is derived from
  745. * the AHB clock (HCLK).
  746. * This parameter can be one of the following values:
  747. * @arg RCC_HCLK_Div1: APB clock = HCLK
  748. * @arg RCC_HCLK_Div2: APB clock = HCLK/2
  749. * @arg RCC_HCLK_Div4: APB clock = HCLK/4
  750. * @arg RCC_HCLK_Div8: APB clock = HCLK/8
  751. * @arg RCC_HCLK_Div16: APB clock = HCLK/16
  752. * @retval None
  753. */
  754. void RCC_PCLKConfig(uint32_t RCC_HCLK)
  755. {
  756. uint32_t tmpreg = 0;
  757. /* Check the parameters */
  758. assert_param(IS_RCC_PCLK(RCC_HCLK));
  759. tmpreg = RCC->CFGR;
  760. /* Clear PPRE[2:0] bits */
  761. tmpreg &= ~RCC_CFGR_PPRE;
  762. /* Set PPRE[2:0] bits according to RCC_HCLK value */
  763. tmpreg |= RCC_HCLK;
  764. /* Store the new value */
  765. RCC->CFGR = tmpreg;
  766. }
  767. /**
  768. * @brief Configures the ADC clock (ADCCLK).
  769. * @note This function is obsolete.
  770. * For proper ADC clock selection, refer to ADC_ClockModeConfig() in the ADC driver
  771. * @param RCC_ADCCLK: defines the ADC clock source. This clock is derived
  772. * from the HSI14 or APB clock (PCLK).
  773. * This parameter can be one of the following values:
  774. * @arg RCC_ADCCLK_HSI14: ADC clock = HSI14 (14MHz)
  775. * @arg RCC_ADCCLK_PCLK_Div2: ADC clock = PCLK/2
  776. * @arg RCC_ADCCLK_PCLK_Div4: ADC clock = PCLK/4
  777. * @retval None
  778. */
  779. void RCC_ADCCLKConfig(uint32_t RCC_ADCCLK)
  780. {
  781. /* Check the parameters */
  782. assert_param(IS_RCC_ADCCLK(RCC_ADCCLK));
  783. /* Clear ADCPRE bit */
  784. RCC->CFGR &= ~RCC_CFGR_ADCPRE;
  785. /* Set ADCPRE bits according to RCC_PCLK value */
  786. RCC->CFGR |= RCC_ADCCLK & 0xFFFF;
  787. /* Clear ADCSW bit */
  788. RCC->CFGR3 &= ~RCC_CFGR3_ADCSW;
  789. /* Set ADCSW bits according to RCC_ADCCLK value */
  790. RCC->CFGR3 |= RCC_ADCCLK >> 16;
  791. }
  792. /**
  793. * @brief Configures the CEC clock (CECCLK).
  794. * @param RCC_CECCLK: defines the CEC clock source. This clock is derived
  795. * from the HSI or LSE clock.
  796. * This parameter can be one of the following values:
  797. * @arg RCC_CECCLK_HSI_Div244: CEC clock = HSI/244 (32768Hz)
  798. * @arg RCC_CECCLK_LSE: CEC clock = LSE
  799. * @retval None
  800. */
  801. void RCC_CECCLKConfig(uint32_t RCC_CECCLK)
  802. {
  803. /* Check the parameters */
  804. assert_param(IS_RCC_CECCLK(RCC_CECCLK));
  805. /* Clear CECSW bit */
  806. RCC->CFGR3 &= ~RCC_CFGR3_CECSW;
  807. /* Set CECSW bits according to RCC_CECCLK value */
  808. RCC->CFGR3 |= RCC_CECCLK;
  809. }
  810. /**
  811. * @brief Configures the I2C1 clock (I2C1CLK).
  812. * @param RCC_I2CCLK: defines the I2C1 clock source. This clock is derived
  813. * from the HSI or System clock.
  814. * This parameter can be one of the following values:
  815. * @arg RCC_I2C1CLK_HSI: I2C1 clock = HSI
  816. * @arg RCC_I2C1CLK_SYSCLK: I2C1 clock = System Clock
  817. * @retval None
  818. */
  819. void RCC_I2CCLKConfig(uint32_t RCC_I2CCLK)
  820. {
  821. /* Check the parameters */
  822. assert_param(IS_RCC_I2CCLK(RCC_I2CCLK));
  823. /* Clear I2CSW bit */
  824. RCC->CFGR3 &= ~RCC_CFGR3_I2C1SW;
  825. /* Set I2CSW bits according to RCC_I2CCLK value */
  826. RCC->CFGR3 |= RCC_I2CCLK;
  827. }
  828. /**
  829. * @brief Configures the USART1 clock (USART1CLK).
  830. * @param RCC_USARTCLK: defines the USART clock source. This clock is derived
  831. * from the HSI or System clock.
  832. * This parameter can be one of the following values:
  833. * @arg RCC_USART1CLK_PCLK: USART1 clock = APB Clock (PCLK)
  834. * @arg RCC_USART1CLK_SYSCLK: USART1 clock = System Clock
  835. * @arg RCC_USART1CLK_LSE: USART1 clock = LSE Clock
  836. * @arg RCC_USART1CLK_HSI: USART1 clock = HSI Clock
  837. * @arg RCC_USART2CLK_PCLK: USART2 clock = APB Clock (PCLK), applicable only for STM32F072 and STM32F091 devices
  838. * @arg RCC_USART2CLK_SYSCLK: USART2 clock = System Clock, applicable only for STM32F072 and STM32F091 devices
  839. * @arg RCC_USART2CLK_LSE: USART2 clock = LSE Clock, applicable only for STM32F072 and STM32F091 devices
  840. * @arg RCC_USART2CLK_HSI: USART2 clock = HSI Clock, applicable only for STM32F072 and STM32F091 devices
  841. * @arg RCC_USART3CLK_PCLK: USART3 clock = APB Clock (PCLK), applicable only for STM32F091 devices
  842. * @arg RCC_USART3CLK_SYSCLK: USART3 clock = System Clock, applicable only for STM32F091 devices
  843. * @arg RCC_USART3CLK_LSE: USART3 clock = LSE Clock, applicable only for STM32F091 devices
  844. * @arg RCC_USART3CLK_HSI: USART3 clock = HSI Clock, applicable only for STM32F091 devices
  845. * @retval None
  846. */
  847. void RCC_USARTCLKConfig(uint32_t RCC_USARTCLK)
  848. {
  849. uint32_t tmp = 0;
  850. /* Check the parameters */
  851. assert_param(IS_RCC_USARTCLK(RCC_USARTCLK));
  852. /* Get USART index */
  853. tmp = (RCC_USARTCLK >> 28);
  854. /* Clear USARTSW[1:0] bit */
  855. if (tmp == (uint32_t)0x00000001)
  856. {
  857. /* Clear USART1SW[1:0] bit */
  858. RCC->CFGR3 &= ~RCC_CFGR3_USART1SW;
  859. }
  860. else if (tmp == (uint32_t)0x00000002)
  861. {
  862. /* Clear USART2SW[1:0] bit */
  863. RCC->CFGR3 &= ~RCC_CFGR3_USART2SW;
  864. }
  865. else
  866. {
  867. /* Clear USART3SW[1:0] bit */
  868. RCC->CFGR3 &= ~RCC_CFGR3_USART3SW;
  869. }
  870. /* Set USARTxSW bits according to RCC_USARTCLK value */
  871. RCC->CFGR3 |= RCC_USARTCLK;
  872. }
  873. /**
  874. * @brief Configures the USB clock (USBCLK).
  875. * This function is only applicable for STM32F072 devices.
  876. * @param RCC_USBCLK: defines the USB clock source. This clock is derived
  877. * from the HSI48 or system clock.
  878. * This parameter can be one of the following values:
  879. * @arg RCC_USBCLK_HSI48: USB clock = HSI48
  880. * @arg RCC_USBCLK_PLLCLK: USB clock = PLL clock
  881. * @retval None
  882. */
  883. void RCC_USBCLKConfig(uint32_t RCC_USBCLK)
  884. {
  885. /* Check the parameters */
  886. assert_param(IS_RCC_USBCLK(RCC_USBCLK));
  887. /* Clear USBSW bit */
  888. RCC->CFGR3 &= ~RCC_CFGR3_USBSW;
  889. /* Set USBSW bits according to RCC_USBCLK value */
  890. RCC->CFGR3 |= RCC_USBCLK;
  891. }
  892. /**
  893. * @brief Returns the frequencies of the System, AHB and APB busses clocks.
  894. * @note The frequency returned by this function is not the real frequency
  895. * in the chip. It is calculated based on the predefined constant and
  896. * the source selected by RCC_SYSCLKConfig():
  897. *
  898. * @note If SYSCLK source is HSI, function returns constant HSI_VALUE(*)
  899. *
  900. * @note If SYSCLK source is HSE, function returns constant HSE_VALUE(**)
  901. *
  902. * @note If SYSCLK source is PLL, function returns constant HSE_VALUE(**)
  903. * or HSI_VALUE(*) multiplied by the PLL factors.
  904. *
  905. * @note If SYSCLK source is HSI48, function returns constant HSI48_VALUE(***)
  906. *
  907. * @note (*) HSI_VALUE is a constant defined in stm32f0xx.h file (default value
  908. * 8 MHz) but the real value may vary depending on the variations
  909. * in voltage and temperature, refer to RCC_AdjustHSICalibrationValue().
  910. *
  911. * @note (**) HSE_VALUE is a constant defined in stm32f0xx.h file (default value
  912. * 8 MHz), user has to ensure that HSE_VALUE is same as the real
  913. * frequency of the crystal used. Otherwise, this function may
  914. * return wrong result.
  915. *
  916. * @note (***) HSI48_VALUE is a constant defined in stm32f0xx.h file (default value
  917. * 48 MHz) but the real value may vary depending on the variations
  918. * in voltage and temperature.
  919. *
  920. * @note The result of this function could be not correct when using fractional
  921. * value for HSE crystal.
  922. *
  923. * @param RCC_Clocks: pointer to a RCC_ClocksTypeDef structure which will hold
  924. * the clocks frequencies.
  925. *
  926. * @note This function can be used by the user application to compute the
  927. * baudrate for the communication peripherals or configure other parameters.
  928. * @note Each time SYSCLK, HCLK and/or PCLK clock changes, this function
  929. * must be called to update the structure's field. Otherwise, any
  930. * configuration based on this function will be incorrect.
  931. *
  932. * @retval None
  933. */
  934. void RCC_GetClocksFreq(RCC_ClocksTypeDef* RCC_Clocks)
  935. {
  936. uint32_t tmp = 0, pllmull = 0, pllsource = 0, prediv1factor = 0, presc = 0, pllclk = 0;
  937. /* Get SYSCLK source -------------------------------------------------------*/
  938. tmp = RCC->CFGR & RCC_CFGR_SWS;
  939. switch (tmp)
  940. {
  941. case 0x00: /* HSI used as system clock */
  942. RCC_Clocks->SYSCLK_Frequency = HSI_VALUE;
  943. break;
  944. case 0x04: /* HSE used as system clock */
  945. RCC_Clocks->SYSCLK_Frequency = HSE_VALUE;
  946. break;
  947. case 0x08: /* PLL used as system clock */
  948. /* Get PLL clock source and multiplication factor ----------------------*/
  949. pllmull = RCC->CFGR & RCC_CFGR_PLLMULL;
  950. pllsource = RCC->CFGR & RCC_CFGR_PLLSRC;
  951. pllmull = ( pllmull >> 18) + 2;
  952. if (pllsource == 0x00)
  953. {
  954. /* HSI oscillator clock divided by 2 selected as PLL clock entry */
  955. pllclk = (HSI_VALUE >> 1) * pllmull;
  956. }
  957. else
  958. {
  959. prediv1factor = (RCC->CFGR2 & RCC_CFGR2_PREDIV1) + 1;
  960. /* HSE oscillator clock selected as PREDIV1 clock entry */
  961. pllclk = (HSE_VALUE / prediv1factor) * pllmull;
  962. }
  963. RCC_Clocks->SYSCLK_Frequency = pllclk;
  964. break;
  965. case 0x0C: /* HSI48 used as system clock */
  966. RCC_Clocks->SYSCLK_Frequency = HSI48_VALUE;
  967. break;
  968. default: /* HSI used as system clock */
  969. RCC_Clocks->SYSCLK_Frequency = HSI_VALUE;
  970. break;
  971. }
  972. /* Compute HCLK, PCLK clocks frequencies -----------------------------------*/
  973. /* Get HCLK prescaler */
  974. tmp = RCC->CFGR & RCC_CFGR_HPRE;
  975. tmp = tmp >> 4;
  976. presc = APBAHBPrescTable[tmp];
  977. /* HCLK clock frequency */
  978. RCC_Clocks->HCLK_Frequency = RCC_Clocks->SYSCLK_Frequency >> presc;
  979. /* Get PCLK prescaler */
  980. tmp = RCC->CFGR & RCC_CFGR_PPRE;
  981. tmp = tmp >> 8;
  982. presc = APBAHBPrescTable[tmp];
  983. /* PCLK clock frequency */
  984. RCC_Clocks->PCLK_Frequency = RCC_Clocks->HCLK_Frequency >> presc;
  985. /* ADCCLK clock frequency */
  986. if((RCC->CFGR3 & RCC_CFGR3_ADCSW) != RCC_CFGR3_ADCSW)
  987. {
  988. /* ADC Clock is HSI14 Osc. */
  989. RCC_Clocks->ADCCLK_Frequency = HSI14_VALUE;
  990. }
  991. else
  992. {
  993. if((RCC->CFGR & RCC_CFGR_ADCPRE) != RCC_CFGR_ADCPRE)
  994. {
  995. /* ADC Clock is derived from PCLK/2 */
  996. RCC_Clocks->ADCCLK_Frequency = RCC_Clocks->PCLK_Frequency >> 1;
  997. }
  998. else
  999. {
  1000. /* ADC Clock is derived from PCLK/4 */
  1001. RCC_Clocks->ADCCLK_Frequency = RCC_Clocks->PCLK_Frequency >> 2;
  1002. }
  1003. }
  1004. /* CECCLK clock frequency */
  1005. if((RCC->CFGR3 & RCC_CFGR3_CECSW) != RCC_CFGR3_CECSW)
  1006. {
  1007. /* CEC Clock is HSI/244 */
  1008. RCC_Clocks->CECCLK_Frequency = HSI_VALUE / 244;
  1009. }
  1010. else
  1011. {
  1012. /* CECC Clock is LSE Osc. */
  1013. RCC_Clocks->CECCLK_Frequency = LSE_VALUE;
  1014. }
  1015. /* I2C1CLK clock frequency */
  1016. if((RCC->CFGR3 & RCC_CFGR3_I2C1SW) != RCC_CFGR3_I2C1SW)
  1017. {
  1018. /* I2C1 Clock is HSI Osc. */
  1019. RCC_Clocks->I2C1CLK_Frequency = HSI_VALUE;
  1020. }
  1021. else
  1022. {
  1023. /* I2C1 Clock is System Clock */
  1024. RCC_Clocks->I2C1CLK_Frequency = RCC_Clocks->SYSCLK_Frequency;
  1025. }
  1026. /* USART1CLK clock frequency */
  1027. if((RCC->CFGR3 & RCC_CFGR3_USART1SW) == 0x0)
  1028. {
  1029. /* USART1 Clock is PCLK */
  1030. RCC_Clocks->USART1CLK_Frequency = RCC_Clocks->PCLK_Frequency;
  1031. }
  1032. else if((RCC->CFGR3 & RCC_CFGR3_USART1SW) == RCC_CFGR3_USART1SW_0)
  1033. {
  1034. /* USART1 Clock is System Clock */
  1035. RCC_Clocks->USART1CLK_Frequency = RCC_Clocks->SYSCLK_Frequency;
  1036. }
  1037. else if((RCC->CFGR3 & RCC_CFGR3_USART1SW) == RCC_CFGR3_USART1SW_1)
  1038. {
  1039. /* USART1 Clock is LSE Osc. */
  1040. RCC_Clocks->USART1CLK_Frequency = LSE_VALUE;
  1041. }
  1042. else if((RCC->CFGR3 & RCC_CFGR3_USART1SW) == RCC_CFGR3_USART1SW)
  1043. {
  1044. /* USART1 Clock is HSI Osc. */
  1045. RCC_Clocks->USART1CLK_Frequency = HSI_VALUE;
  1046. }
  1047. /* USART2CLK clock frequency */
  1048. if((RCC->CFGR3 & RCC_CFGR3_USART2SW) == 0x0)
  1049. {
  1050. /* USART Clock is PCLK */
  1051. RCC_Clocks->USART2CLK_Frequency = RCC_Clocks->PCLK_Frequency;
  1052. }
  1053. else if((RCC->CFGR3 & RCC_CFGR3_USART2SW) == RCC_CFGR3_USART2SW_0)
  1054. {
  1055. /* USART Clock is System Clock */
  1056. RCC_Clocks->USART2CLK_Frequency = RCC_Clocks->SYSCLK_Frequency;
  1057. }
  1058. else if((RCC->CFGR3 & RCC_CFGR3_USART2SW) == RCC_CFGR3_USART2SW_1)
  1059. {
  1060. /* USART Clock is LSE Osc. */
  1061. RCC_Clocks->USART2CLK_Frequency = LSE_VALUE;
  1062. }
  1063. else if((RCC->CFGR3 & RCC_CFGR3_USART2SW) == RCC_CFGR3_USART2SW)
  1064. {
  1065. /* USART Clock is HSI Osc. */
  1066. RCC_Clocks->USART2CLK_Frequency = HSI_VALUE;
  1067. }
  1068. /* USART3CLK clock frequency */
  1069. if((RCC->CFGR3 & RCC_CFGR3_USART3SW) == 0x0)
  1070. {
  1071. /* USART Clock is PCLK */
  1072. RCC_Clocks->USART3CLK_Frequency = RCC_Clocks->PCLK_Frequency;
  1073. }
  1074. else if((RCC->CFGR3 & RCC_CFGR3_USART3SW) == RCC_CFGR3_USART3SW_0)
  1075. {
  1076. /* USART Clock is System Clock */
  1077. RCC_Clocks->USART3CLK_Frequency = RCC_Clocks->SYSCLK_Frequency;
  1078. }
  1079. else if((RCC->CFGR3 & RCC_CFGR3_USART3SW) == RCC_CFGR3_USART3SW_1)
  1080. {
  1081. /* USART Clock is LSE Osc. */
  1082. RCC_Clocks->USART3CLK_Frequency = LSE_VALUE;
  1083. }
  1084. else if((RCC->CFGR3 & RCC_CFGR3_USART3SW) == RCC_CFGR3_USART3SW)
  1085. {
  1086. /* USART Clock is HSI Osc. */
  1087. RCC_Clocks->USART3CLK_Frequency = HSI_VALUE;
  1088. }
  1089. /* USBCLK clock frequency */
  1090. if((RCC->CFGR3 & RCC_CFGR3_USBSW) != RCC_CFGR3_USBSW)
  1091. {
  1092. /* USB Clock is HSI48 */
  1093. RCC_Clocks->USBCLK_Frequency = HSI48_VALUE;
  1094. }
  1095. else
  1096. {
  1097. /* USB Clock is PLL clock */
  1098. RCC_Clocks->USBCLK_Frequency = pllclk;
  1099. }
  1100. }
  1101. /**
  1102. * @}
  1103. */
  1104. /** @defgroup RCC_Group3 Peripheral clocks configuration functions
  1105. * @brief Peripheral clocks configuration functions
  1106. *
  1107. @verbatim
  1108. ===============================================================================
  1109. #####Peripheral clocks configuration functions #####
  1110. ===============================================================================
  1111. [..] This section provide functions allowing to configure the Peripheral clocks.
  1112. (#) The RTC clock which is derived from the LSE, LSI or HSE_Div32 (HSE
  1113. divided by 32).
  1114. (#) After restart from Reset or wakeup from STANDBY, all peripherals are off
  1115. except internal SRAM, Flash and SWD. Before to start using a peripheral you
  1116. have to enable its interface clock. You can do this using RCC_AHBPeriphClockCmd(),
  1117. RCC_APB2PeriphClockCmd() and RCC_APB1PeriphClockCmd() functions.
  1118. (#) To reset the peripherals configuration (to the default state after device reset)
  1119. you can use RCC_AHBPeriphResetCmd(), RCC_APB2PeriphResetCmd() and
  1120. RCC_APB1PeriphResetCmd() functions.
  1121. @endverbatim
  1122. * @{
  1123. */
  1124. /**
  1125. * @brief Configures the RTC clock (RTCCLK).
  1126. * @note As the RTC clock configuration bits are in the Backup domain and write
  1127. * access is denied to this domain after reset, you have to enable write
  1128. * access using PWR_BackupAccessCmd(ENABLE) function before to configure
  1129. * the RTC clock source (to be done once after reset).
  1130. * @note Once the RTC clock is configured it can't be changed unless the RTC
  1131. * is reset using RCC_BackupResetCmd function, or by a Power On Reset (POR)
  1132. *
  1133. * @param RCC_RTCCLKSource: specifies the RTC clock source.
  1134. * This parameter can be one of the following values:
  1135. * @arg RCC_RTCCLKSource_LSE: LSE selected as RTC clock
  1136. * @arg RCC_RTCCLKSource_LSI: LSI selected as RTC clock
  1137. * @arg RCC_RTCCLKSource_HSE_Div32: HSE divided by 32 selected as RTC clock
  1138. *
  1139. * @note If the LSE or LSI is used as RTC clock source, the RTC continues to
  1140. * work in STOP and STANDBY modes, and can be used as wakeup source.
  1141. * However, when the HSE clock is used as RTC clock source, the RTC
  1142. * cannot be used in STOP and STANDBY modes.
  1143. *
  1144. * @note The maximum input clock frequency for RTC is 2MHz (when using HSE as
  1145. * RTC clock source).
  1146. *
  1147. * @retval None
  1148. */
  1149. void RCC_RTCCLKConfig(uint32_t RCC_RTCCLKSource)
  1150. {
  1151. /* Check the parameters */
  1152. assert_param(IS_RCC_RTCCLK_SOURCE(RCC_RTCCLKSource));
  1153. /* Select the RTC clock source */
  1154. RCC->BDCR |= RCC_RTCCLKSource;
  1155. }
  1156. /**
  1157. * @brief Enables or disables the RTC clock.
  1158. * @note This function must be used only after the RTC clock source was selected
  1159. * using the RCC_RTCCLKConfig function.
  1160. * @param NewState: new state of the RTC clock.
  1161. * This parameter can be: ENABLE or DISABLE.
  1162. * @retval None
  1163. */
  1164. void RCC_RTCCLKCmd(FunctionalState NewState)
  1165. {
  1166. /* Check the parameters */
  1167. assert_param(IS_FUNCTIONAL_STATE(NewState));
  1168. if (NewState != DISABLE)
  1169. {
  1170. RCC->BDCR |= RCC_BDCR_RTCEN;
  1171. }
  1172. else
  1173. {
  1174. RCC->BDCR &= ~RCC_BDCR_RTCEN;
  1175. }
  1176. }
  1177. /**
  1178. * @brief Forces or releases the Backup domain reset.
  1179. * @note This function resets the RTC peripheral (including the backup registers)
  1180. * and the RTC clock source selection in RCC_BDCR register.
  1181. * @param NewState: new state of the Backup domain reset.
  1182. * This parameter can be: ENABLE or DISABLE.
  1183. * @retval None
  1184. */
  1185. void RCC_BackupResetCmd(FunctionalState NewState)
  1186. {
  1187. /* Check the parameters */
  1188. assert_param(IS_FUNCTIONAL_STATE(NewState));
  1189. if (NewState != DISABLE)
  1190. {
  1191. RCC->BDCR |= RCC_BDCR_BDRST;
  1192. }
  1193. else
  1194. {
  1195. RCC->BDCR &= ~RCC_BDCR_BDRST;
  1196. }
  1197. }
  1198. /**
  1199. * @brief Enables or disables the AHB peripheral clock.
  1200. * @note After reset, the peripheral clock (used for registers read/write access)
  1201. * is disabled and the application software has to enable this clock before
  1202. * using it.
  1203. * @param RCC_AHBPeriph: specifies the AHB peripheral to gates its clock.
  1204. * This parameter can be any combination of the following values:
  1205. * @arg RCC_AHBPeriph_GPIOA: GPIOA clock
  1206. * @arg RCC_AHBPeriph_GPIOB: GPIOB clock
  1207. * @arg RCC_AHBPeriph_GPIOC: GPIOC clock
  1208. * @arg RCC_AHBPeriph_GPIOD: GPIOD clock
  1209. * @arg RCC_AHBPeriph_GPIOE: GPIOE clock, applicable only for STM32F072 devices
  1210. * @arg RCC_AHBPeriph_GPIOF: GPIOF clock
  1211. * @arg RCC_AHBPeriph_TS: TS clock
  1212. * @arg RCC_AHBPeriph_CRC: CRC clock
  1213. * @arg RCC_AHBPeriph_FLITF: (has effect only when the Flash memory is in power down mode)
  1214. * @arg RCC_AHBPeriph_SRAM: SRAM clock
  1215. * @arg RCC_AHBPeriph_DMA1: DMA1 clock
  1216. * @arg RCC_AHBPeriph_DMA2: DMA2 clock
  1217. * @param NewState: new state of the specified peripheral clock.
  1218. * This parameter can be: ENABLE or DISABLE.
  1219. * @retval None
  1220. */
  1221. void RCC_AHBPeriphClockCmd(uint32_t RCC_AHBPeriph, FunctionalState NewState)
  1222. {
  1223. /* Check the parameters */
  1224. assert_param(IS_RCC_AHB_PERIPH(RCC_AHBPeriph));
  1225. assert_param(IS_FUNCTIONAL_STATE(NewState));
  1226. if (NewState != DISABLE)
  1227. {
  1228. RCC->AHBENR |= RCC_AHBPeriph;
  1229. }
  1230. else
  1231. {
  1232. RCC->AHBENR &= ~RCC_AHBPeriph;
  1233. }
  1234. }
  1235. /**
  1236. * @brief Enables or disables the High Speed APB (APB2) peripheral clock.
  1237. * @note After reset, the peripheral clock (used for registers read/write access)
  1238. * is disabled and the application software has to enable this clock before
  1239. * using it.
  1240. * @param RCC_APB2Periph: specifies the APB2 peripheral to gates its clock.
  1241. * This parameter can be any combination of the following values:
  1242. * @arg RCC_APB2Periph_SYSCFG: SYSCFG clock
  1243. * @arg RCC_APB2Periph_USART6: USART6 clock
  1244. * @arg RCC_APB2Periph_USART7: USART7 clock
  1245. * @arg RCC_APB2Periph_USART8: USART8 clock
  1246. * @arg RCC_APB2Periph_ADC1: ADC1 clock
  1247. * @arg RCC_APB2Periph_TIM1: TIM1 clock
  1248. * @arg RCC_APB2Periph_SPI1: SPI1 clock
  1249. * @arg RCC_APB2Periph_USART1: USART1 clock
  1250. * @arg RCC_APB2Periph_TIM15: TIM15 clock
  1251. * @arg RCC_APB2Periph_TIM16: TIM16 clock
  1252. * @arg RCC_APB2Periph_TIM17: TIM17 clock
  1253. * @arg RCC_APB2Periph_DBGMCU: DBGMCU clock
  1254. * @param NewState: new state of the specified peripheral clock.
  1255. * This parameter can be: ENABLE or DISABLE.
  1256. * @retval None
  1257. */
  1258. void RCC_APB2PeriphClockCmd(uint32_t RCC_APB2Periph, FunctionalState NewState)
  1259. {
  1260. /* Check the parameters */
  1261. assert_param(IS_RCC_APB2_PERIPH(RCC_APB2Periph));
  1262. assert_param(IS_FUNCTIONAL_STATE(NewState));
  1263. if (NewState != DISABLE)
  1264. {
  1265. RCC->APB2ENR |= RCC_APB2Periph;
  1266. }
  1267. else
  1268. {
  1269. RCC->APB2ENR &= ~RCC_APB2Periph;
  1270. }
  1271. }
  1272. /**
  1273. * @brief Enables or disables the Low Speed APB (APB1) peripheral clock.
  1274. * @note After reset, the peripheral clock (used for registers read/write access)
  1275. * is disabled and the application software has to enable this clock before
  1276. * using it.
  1277. * @param RCC_APB1Periph: specifies the APB1 peripheral to gates its clock.
  1278. * This parameter can be any combination of the following values:
  1279. * @arg RCC_APB1Periph_TIM2: TIM2 clock, applicable only for STM32F051 and STM32F072 devices
  1280. * @arg RCC_APB1Periph_TIM3: TIM3 clock
  1281. * @arg RCC_APB1Periph_TIM6: TIM6 clock
  1282. * @arg RCC_APB1Periph_TIM7: TIM7 clock, applicable only for STM32F072 devices
  1283. * @arg RCC_APB1Periph_TIM14: TIM14 clock
  1284. * @arg RCC_APB1Periph_WWDG: WWDG clock
  1285. * @arg RCC_APB1Periph_SPI2: SPI2 clock
  1286. * @arg RCC_APB1Periph_USART2: USART2 clock
  1287. * @arg RCC_APB1Periph_USART3: USART3 clock, applicable only for STM32F072 and STM32F091 devices
  1288. * @arg RCC_APB1Periph_USART4: USART4 clock, applicable only for STM32F072 and STM32F091 devices
  1289. * @arg RCC_APB1Periph_USART5: USART5 clock, applicable only for STM32F091 devices
  1290. * @arg RCC_APB1Periph_I2C1: I2C1 clock
  1291. * @arg RCC_APB1Periph_I2C2: I2C2 clock
  1292. * @arg RCC_APB1Periph_USB: USB clock, applicable only for STM32F042 and STM32F072 devices
  1293. * @arg RCC_APB1Periph_CAN: CAN clock, applicable only for STM32F042 and STM32F072 devices
  1294. * @arg RCC_APB1Periph_CRS: CRS clock , applicable only for STM32F042 and STM32F072 devices
  1295. * @arg RCC_APB1Periph_PWR: PWR clock
  1296. * @arg RCC_APB1Periph_DAC: DAC clock, applicable only for STM32F051 and STM32F072 devices
  1297. * @arg RCC_APB1Periph_CEC: CEC clock, applicable only for STM32F051, STM32F042 and STM32F072 devices
  1298. * @param NewState: new state of the specified peripheral clock.
  1299. * This parameter can be: ENABLE or DISABLE.
  1300. * @retval None
  1301. */
  1302. void RCC_APB1PeriphClockCmd(uint32_t RCC_APB1Periph, FunctionalState NewState)
  1303. {
  1304. /* Check the parameters */
  1305. assert_param(IS_RCC_APB1_PERIPH(RCC_APB1Periph));
  1306. assert_param(IS_FUNCTIONAL_STATE(NewState));
  1307. if (NewState != DISABLE)
  1308. {
  1309. RCC->APB1ENR |= RCC_APB1Periph;
  1310. }
  1311. else
  1312. {
  1313. RCC->APB1ENR &= ~RCC_APB1Periph;
  1314. }
  1315. }
  1316. /**
  1317. * @brief Forces or releases AHB peripheral reset.
  1318. * @param RCC_AHBPeriph: specifies the AHB peripheral to reset.
  1319. * This parameter can be any combination of the following values:
  1320. * @arg RCC_AHBPeriph_GPIOA: GPIOA clock
  1321. * @arg RCC_AHBPeriph_GPIOB: GPIOB clock
  1322. * @arg RCC_AHBPeriph_GPIOC: GPIOC clock
  1323. * @arg RCC_AHBPeriph_GPIOD: GPIOD clock
  1324. * @arg RCC_AHBPeriph_GPIOE: GPIOE clock, applicable only for STM32F072 devices
  1325. * @arg RCC_AHBPeriph_GPIOF: GPIOF clock
  1326. * @arg RCC_AHBPeriph_TS: TS clock
  1327. * @param NewState: new state of the specified peripheral reset.
  1328. * This parameter can be: ENABLE or DISABLE.
  1329. * @retval None
  1330. */
  1331. void RCC_AHBPeriphResetCmd(uint32_t RCC_AHBPeriph, FunctionalState NewState)
  1332. {
  1333. /* Check the parameters */
  1334. assert_param(IS_RCC_AHB_RST_PERIPH(RCC_AHBPeriph));
  1335. assert_param(IS_FUNCTIONAL_STATE(NewState));
  1336. if (NewState != DISABLE)
  1337. {
  1338. RCC->AHBRSTR |= RCC_AHBPeriph;
  1339. }
  1340. else
  1341. {
  1342. RCC->AHBRSTR &= ~RCC_AHBPeriph;
  1343. }
  1344. }
  1345. /**
  1346. * @brief Forces or releases High Speed APB (APB2) peripheral reset.
  1347. * @param RCC_APB2Periph: specifies the APB2 peripheral to reset.
  1348. * This parameter can be any combination of the following values:
  1349. * @arg RCC_APB2Periph_SYSCFG: SYSCFG clock
  1350. * @arg RCC_APB2Periph_USART6: USART6 clock
  1351. * @arg RCC_APB2Periph_USART7: USART7 clock
  1352. * @arg RCC_APB2Periph_USART8: USART8 clock
  1353. * @arg RCC_APB2Periph_ADC1: ADC1 clock
  1354. * @arg RCC_APB2Periph_TIM1: TIM1 clock
  1355. * @arg RCC_APB2Periph_SPI1: SPI1 clock
  1356. * @arg RCC_APB2Periph_USART1: USART1 clock
  1357. * @arg RCC_APB2Periph_TIM15: TIM15 clock
  1358. * @arg RCC_APB2Periph_TIM16: TIM16 clock
  1359. * @arg RCC_APB2Periph_TIM17: TIM17 clock
  1360. * @arg RCC_APB2Periph_DBGMCU: DBGMCU clock
  1361. * @param NewState: new state of the specified peripheral reset.
  1362. * This parameter can be: ENABLE or DISABLE.
  1363. * @retval None
  1364. */
  1365. void RCC_APB2PeriphResetCmd(uint32_t RCC_APB2Periph, FunctionalState NewState)
  1366. {
  1367. /* Check the parameters */
  1368. assert_param(IS_RCC_APB2_PERIPH(RCC_APB2Periph));
  1369. assert_param(IS_FUNCTIONAL_STATE(NewState));
  1370. if (NewState != DISABLE)
  1371. {
  1372. RCC->APB2RSTR |= RCC_APB2Periph;
  1373. }
  1374. else
  1375. {
  1376. RCC->APB2RSTR &= ~RCC_APB2Periph;
  1377. }
  1378. }
  1379. /**
  1380. * @brief Forces or releases Low Speed APB (APB1) peripheral reset.
  1381. * @param RCC_APB1Periph: specifies the APB1 peripheral to reset.
  1382. * This parameter can be any combination of the following values:
  1383. * @arg RCC_APB1Periph_TIM2: TIM2 clock, applicable only for STM32F051 and STM32F072 devices
  1384. * @arg RCC_APB1Periph_TIM3: TIM3 clock
  1385. * @arg RCC_APB1Periph_TIM6: TIM6 clock
  1386. * @arg RCC_APB1Periph_TIM7: TIM7 clock, applicable only for STM32F072 devices
  1387. * @arg RCC_APB1Periph_TIM14: TIM14 clock
  1388. * @arg RCC_APB1Periph_WWDG: WWDG clock
  1389. * @arg RCC_APB1Periph_SPI2: SPI2 clock
  1390. * @arg RCC_APB1Periph_USART2: USART2 clock
  1391. * @arg RCC_APB1Periph_USART3: USART3 clock, applicable only for STM32F072 and STM32F091 devices
  1392. * @arg RCC_APB1Periph_USART4: USART4 clock, applicable only for STM32F072 and STM32F091 devices
  1393. * @arg RCC_APB1Periph_USART5: USART5 clock, applicable only for STM32F091 devices
  1394. * @arg RCC_APB1Periph_I2C1: I2C1 clock
  1395. * @arg RCC_APB1Periph_I2C2: I2C2 clock
  1396. * @arg RCC_APB1Periph_USB: USB clock, applicable only for STM32F042 and STM32F072 devices
  1397. * @arg RCC_APB1Periph_CAN: CAN clock, applicable only for STM32F042 and STM32F072 devices
  1398. * @arg RCC_APB1Periph_CRS: CRS clock , applicable only for STM32F042 and STM32F072 devices
  1399. * @arg RCC_APB1Periph_PWR: PWR clock
  1400. * @arg RCC_APB1Periph_DAC: DAC clock, applicable only for STM32F051 and STM32F072 devices
  1401. * @arg RCC_APB1Periph_CEC: CEC clock, applicable only for STM32F051, STM32F042 and STM32F072 devices
  1402. * @param NewState: new state of the specified peripheral clock.
  1403. * This parameter can be: ENABLE or DISABLE.
  1404. * @retval None
  1405. */
  1406. void RCC_APB1PeriphResetCmd(uint32_t RCC_APB1Periph, FunctionalState NewState)
  1407. {
  1408. /* Check the parameters */
  1409. assert_param(IS_RCC_APB1_PERIPH(RCC_APB1Periph));
  1410. assert_param(IS_FUNCTIONAL_STATE(NewState));
  1411. if (NewState != DISABLE)
  1412. {
  1413. RCC->APB1RSTR |= RCC_APB1Periph;
  1414. }
  1415. else
  1416. {
  1417. RCC->APB1RSTR &= ~RCC_APB1Periph;
  1418. }
  1419. }
  1420. /**
  1421. * @}
  1422. */
  1423. /** @defgroup RCC_Group4 Interrupts and flags management functions
  1424. * @brief Interrupts and flags management functions
  1425. *
  1426. @verbatim
  1427. ===============================================================================
  1428. ##### Interrupts and flags management functions #####
  1429. ===============================================================================
  1430. @endverbatim
  1431. * @{
  1432. */
  1433. /**
  1434. * @brief Enables or disables the specified RCC interrupts.
  1435. * @note The CSS interrupt doesn't have an enable bit; once the CSS is enabled
  1436. * and if the HSE clock fails, the CSS interrupt occurs and an NMI is
  1437. * automatically generated. The NMI will be executed indefinitely, and
  1438. * since NMI has higher priority than any other IRQ (and main program)
  1439. * the application will be stacked in the NMI ISR unless the CSS interrupt
  1440. * pending bit is cleared.
  1441. * @param RCC_IT: specifies the RCC interrupt sources to be enabled or disabled.
  1442. * This parameter can be any combination of the following values:
  1443. * @arg RCC_IT_LSIRDY: LSI ready interrupt
  1444. * @arg RCC_IT_LSERDY: LSE ready interrupt
  1445. * @arg RCC_IT_HSIRDY: HSI ready interrupt
  1446. * @arg RCC_IT_HSERDY: HSE ready interrupt
  1447. * @arg RCC_IT_PLLRDY: PLL ready interrupt
  1448. * @arg RCC_IT_HSI14RDY: HSI14 ready interrupt
  1449. * @arg RCC_IT_HSI48RDY: HSI48 ready interrupt, applicable only for STM32F072 devices
  1450. * @param NewState: new state of the specified RCC interrupts.
  1451. * This parameter can be: ENABLE or DISABLE.
  1452. * @retval None
  1453. */
  1454. void RCC_ITConfig(uint8_t RCC_IT, FunctionalState NewState)
  1455. {
  1456. /* Check the parameters */
  1457. assert_param(IS_RCC_IT(RCC_IT));
  1458. assert_param(IS_FUNCTIONAL_STATE(NewState));
  1459. if (NewState != DISABLE)
  1460. {
  1461. /* Perform Byte access to RCC_CIR[13:8] bits to enable the selected interrupts */
  1462. *(__IO uint8_t *) CIR_BYTE1_ADDRESS |= RCC_IT;
  1463. }
  1464. else
  1465. {
  1466. /* Perform Byte access to RCC_CIR[13:8] bits to disable the selected interrupts */
  1467. *(__IO uint8_t *) CIR_BYTE1_ADDRESS &= (uint8_t)~RCC_IT;
  1468. }
  1469. }
  1470. /**
  1471. * @brief Checks whether the specified RCC flag is set or not.
  1472. * @param RCC_FLAG: specifies the flag to check.
  1473. * This parameter can be one of the following values:
  1474. * @arg RCC_FLAG_HSIRDY: HSI oscillator clock ready
  1475. * @arg RCC_FLAG_HSERDY: HSE oscillator clock ready
  1476. * @arg RCC_FLAG_PLLRDY: PLL clock ready
  1477. * @arg RCC_FLAG_LSERDY: LSE oscillator clock ready
  1478. * @arg RCC_FLAG_LSIRDY: LSI oscillator clock ready
  1479. * @arg RCC_FLAG_OBLRST: Option Byte Loader (OBL) reset
  1480. * @arg RCC_FLAG_PINRST: Pin reset
  1481. * @arg RCC_FLAG_V18PWRRSTF: V1.8 power domain reset
  1482. * @arg RCC_FLAG_PORRST: POR/PDR reset
  1483. * @arg RCC_FLAG_SFTRST: Software reset
  1484. * @arg RCC_FLAG_IWDGRST: Independent Watchdog reset
  1485. * @arg RCC_FLAG_WWDGRST: Window Watchdog reset
  1486. * @arg RCC_FLAG_LPWRRST: Low Power reset
  1487. * @arg RCC_FLAG_HSI14RDY: HSI14 oscillator clock ready
  1488. * @arg RCC_FLAG_HSI48RDY: HSI48 oscillator clock ready, applicable only for STM32F072 devices
  1489. * @retval The new state of RCC_FLAG (SET or RESET).
  1490. */
  1491. FlagStatus RCC_GetFlagStatus(uint8_t RCC_FLAG)
  1492. {
  1493. uint32_t tmp = 0;
  1494. uint32_t statusreg = 0;
  1495. FlagStatus bitstatus = RESET;
  1496. /* Check the parameters */
  1497. assert_param(IS_RCC_FLAG(RCC_FLAG));
  1498. /* Get the RCC register index */
  1499. tmp = RCC_FLAG >> 5;
  1500. if (tmp == 0) /* The flag to check is in CR register */
  1501. {
  1502. statusreg = RCC->CR;
  1503. }
  1504. else if (tmp == 1) /* The flag to check is in BDCR register */
  1505. {
  1506. statusreg = RCC->BDCR;
  1507. }
  1508. else if (tmp == 2) /* The flag to check is in CSR register */
  1509. {
  1510. statusreg = RCC->CSR;
  1511. }
  1512. else /* The flag to check is in CR2 register */
  1513. {
  1514. statusreg = RCC->CR2;
  1515. }
  1516. /* Get the flag position */
  1517. tmp = RCC_FLAG & FLAG_MASK;
  1518. if ((statusreg & ((uint32_t)1 << tmp)) != (uint32_t)RESET)
  1519. {
  1520. bitstatus = SET;
  1521. }
  1522. else
  1523. {
  1524. bitstatus = RESET;
  1525. }
  1526. /* Return the flag status */
  1527. return bitstatus;
  1528. }
  1529. /**
  1530. * @brief Clears the RCC reset flags.
  1531. * The reset flags are: RCC_FLAG_OBLRST, RCC_FLAG_PINRST, RCC_FLAG_V18PWRRSTF,
  1532. * RCC_FLAG_PORRST, RCC_FLAG_SFTRST, RCC_FLAG_IWDGRST, RCC_FLAG_WWDGRST,
  1533. * RCC_FLAG_LPWRRST.
  1534. * @param None
  1535. * @retval None
  1536. */
  1537. void RCC_ClearFlag(void)
  1538. {
  1539. /* Set RMVF bit to clear the reset flags */
  1540. RCC->CSR |= RCC_CSR_RMVF;
  1541. }
  1542. /**
  1543. * @brief Checks whether the specified RCC interrupt has occurred or not.
  1544. * @param RCC_IT: specifies the RCC interrupt source to check.
  1545. * This parameter can be one of the following values:
  1546. * @arg RCC_IT_LSIRDY: LSI ready interrupt
  1547. * @arg RCC_IT_LSERDY: LSE ready interrupt
  1548. * @arg RCC_IT_HSIRDY: HSI ready interrupt
  1549. * @arg RCC_IT_HSERDY: HSE ready interrupt
  1550. * @arg RCC_IT_PLLRDY: PLL ready interrupt
  1551. * @arg RCC_IT_HSI14RDY: HSI14 ready interrupt
  1552. * @arg RCC_IT_HSI48RDY: HSI48 ready interrupt, applicable only for STM32F072 devices
  1553. * @arg RCC_IT_CSS: Clock Security System interrupt
  1554. * @retval The new state of RCC_IT (SET or RESET).
  1555. */
  1556. ITStatus RCC_GetITStatus(uint8_t RCC_IT)
  1557. {
  1558. ITStatus bitstatus = RESET;
  1559. /* Check the parameters */
  1560. assert_param(IS_RCC_GET_IT(RCC_IT));
  1561. /* Check the status of the specified RCC interrupt */
  1562. if ((RCC->CIR & RCC_IT) != (uint32_t)RESET)
  1563. {
  1564. bitstatus = SET;
  1565. }
  1566. else
  1567. {
  1568. bitstatus = RESET;
  1569. }
  1570. /* Return the RCC_IT status */
  1571. return bitstatus;
  1572. }
  1573. /**
  1574. * @brief Clears the RCC's interrupt pending bits.
  1575. * @param RCC_IT: specifies the interrupt pending bit to clear.
  1576. * This parameter can be any combination of the following values:
  1577. * @arg RCC_IT_LSIRDY: LSI ready interrupt
  1578. * @arg RCC_IT_LSERDY: LSE ready interrupt
  1579. * @arg RCC_IT_HSIRDY: HSI ready interrupt
  1580. * @arg RCC_IT_HSERDY: HSE ready interrupt
  1581. * @arg RCC_IT_PLLRDY: PLL ready interrupt
  1582. * @arg RCC_IT_HSI48RDY: HSI48 ready interrupt, applicable only for STM32F072 devices
  1583. * @arg RCC_IT_HSI14RDY: HSI14 ready interrupt
  1584. * @arg RCC_IT_CSS: Clock Security System interrupt
  1585. * @retval None
  1586. */
  1587. void RCC_ClearITPendingBit(uint8_t RCC_IT)
  1588. {
  1589. /* Check the parameters */
  1590. assert_param(IS_RCC_CLEAR_IT(RCC_IT));
  1591. /* Perform Byte access to RCC_CIR[23:16] bits to clear the selected interrupt
  1592. pending bits */
  1593. *(__IO uint8_t *) CIR_BYTE2_ADDRESS = RCC_IT;
  1594. }
  1595. /**
  1596. * @}
  1597. */
  1598. /**
  1599. * @}
  1600. */
  1601. /**
  1602. * @}
  1603. */
  1604. /**
  1605. * @}
  1606. */
  1607. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/