ft32f0xx_rcc.c 52 KB

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