gd32f10x_rcc.c 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323
  1. /**
  2. ******************************************************************************
  3. * @brief RCC functions of the firmware library.
  4. ******************************************************************************
  5. */
  6. /* Includes ------------------------------------------------------------------*/
  7. #include "gd32f10x_rcc.h"
  8. /** @addtogroup GD32F10x_Firmware
  9. * @{
  10. */
  11. /** @defgroup RCC
  12. * @brief RCC driver modules
  13. * @{
  14. */
  15. /** @defgroup RCC_Private_Defines
  16. * @{
  17. */
  18. /* RCC GCCR_HSIEN mask */
  19. #define GCCR_HSIEN_SET ((uint32_t)0x00000001)
  20. /* RCC GCFGR_Reset mask */
  21. #define GCFGR_RESET_CL ((uint32_t)0xE0FF0000)
  22. #define GCFGR_RESET ((uint32_t)0xE8FF0000)
  23. /* RCC GCCR_HSEEN_CKMEN_PLLEN masks */
  24. #define GCCR_HSEEN_CKMEN_PLLEN_RESET ((uint32_t)0xFEF6FFFF)
  25. /* RCC GCCR_HSEBPS mask */
  26. #define GCCR_HSEBPS_RESET ((uint32_t)0xFFFBFFFF)
  27. /* RCC GCFGR_PLLSEL_PLLPREDV_PLLMF masks */
  28. #define GCFGR_PLLSEL_PLLPREDV_PLLMF_USBPS_RESET ((uint32_t)0xF700FFFF)
  29. #define GCFGR_PLLSEL_PLLPREDV_PLLMF_OTGFSPS_RESET_CL ((uint32_t)0xDF00FFFF)
  30. /* RCC GCCR_PLL2EN_PLL3EN masks */
  31. #define GCCR_PLL2EN_PLL3EN_RESET ((uint32_t)0xEBFFFFFF)
  32. /* RCC GCFGR2 reset */
  33. #define GCFGR2_RESET ((uint32_t)0x00000000)
  34. /* RCC GCIR_INT ans FLAG masks */
  35. #define GCIR_INT_FLAG_RESET ((uint32_t)0x009F0000)
  36. #define GCIR_INT_FLAG_RESET_CL ((uint32_t)0x00FF0000)
  37. #define GCCR_HSEEN_HSEBPS_RESET ((uint32_t)0xFFFAFFFF)
  38. /* RCC GCCR_HSIADJ masks */
  39. #define GCCR_HSIADJ_OFFSET ((uint32_t)0x00000003)
  40. #define RCC_GCFGR_PLLMF_3_0 ((uint32_t)0x003C0000) /*!< PLLMF[3:0] Bits */
  41. /* RCC HSI clock divided by 2 masks */
  42. #define HSI_CLOCK_DIVIDED_2 ((uint32_t)0x00000001)
  43. /* RCC HSE clock divided by 2 masks */
  44. #define HSE_CLOCK_DIVIDED_2 ((uint32_t)0x00000001)
  45. static __I uint8_t AHBPrescTable[16] = {0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 4, 6, 7, 8, 9};
  46. static __I uint8_t APBPrescTable[8] = {0, 0, 0, 0, 1, 2, 3, 4};
  47. /* GCIR register byte 1 (Bits[15:8]) base address */
  48. #define GCIR_BYTE1_ADDRESS ((uint32_t)0x40021009)
  49. /* GCIR register byte 2 (Bits[23:16]) base address */
  50. #define GCIR_BYTE2_ADDRESS ((uint32_t)0x4002100A)
  51. /* RCC Flag Mask */
  52. #define FLAG_MASK ((uint8_t)0x1F)
  53. /**
  54. * @}
  55. */
  56. /** @defgroup RCC_Private_Functions
  57. * @{
  58. */
  59. /**
  60. * @brief Reset the RCC clock configuration.
  61. * @param None
  62. * @retval None
  63. */
  64. void RCC_DeInit(void)
  65. {
  66. /* Set RCC GCCR_HSIEN mask */
  67. RCC->GCCR |= GCCR_HSIEN_SET;
  68. /* Reset SCS[1:0], AHBPS[3:0], APB1PS[2:0],APB2PS[2:0], ADCPS[2:0],CKOTUSEL[2:0] bits */
  69. #ifdef GD32F10X_CL
  70. RCC->GCFGR &= GCFGR_RESET_CL;
  71. #else
  72. RCC->GCFGR &= GCFGR_RESET;
  73. #endif /* GD32F10X_CL */
  74. /* Reset HSEEN, CKMEN and PLLEN bits */
  75. RCC->GCCR &= GCCR_HSEEN_CKMEN_PLLEN_RESET;
  76. /* Reset HSEBPS bit */
  77. RCC->GCCR &= GCCR_HSEBPS_RESET;
  78. /* Reset PLLSEL, PLLPREDV and PLLMF[4:0] USBPS/OTGFSPS bits */
  79. #ifdef GD32F10X_CL
  80. RCC->GCFGR &= GCFGR_PLLSEL_PLLPREDV_PLLMF_OTGFSPS_RESET_CL;
  81. #else
  82. RCC->GCFGR &= GCFGR_PLLSEL_PLLPREDV_PLLMF_USBPS_RESET;
  83. #endif /* GD32F10X_CL */
  84. #ifdef GD32F10X_CL
  85. /* Reset PLL2EN and PLL3EN bits */
  86. RCC->GCCR &= GCCR_PLL2EN_PLL3EN_RESET;
  87. /* Reset GCFGR2 register */
  88. RCC->GCFGR2 = GCFGR2_RESET ;
  89. /* Disable all interrupts and clear flag bits */
  90. RCC->GCIR = GCIR_INT_FLAG_RESET_CL;
  91. #else
  92. /* Disable all interrupts and clear flag bits */
  93. RCC->GCIR = GCIR_INT_FLAG_RESET;
  94. #endif /* GD32F10X_CL */
  95. }
  96. /**
  97. * @brief Configure the External High Speed oscillator (HSE).
  98. * @param RCC_HSE: specify the new state of HSE.
  99. * This parameter can be one of the following values:
  100. * @arg RCC_HSE_OFF: turn off the HSE
  101. * @arg RCC_HSE_ON: turn on the HSE
  102. * @arg RCC_HSE_BYPASS: HSE bypassed with external clock
  103. * @retval None
  104. */
  105. void RCC_HSEConfig(uint32_t RCC_HSE)
  106. {
  107. /* Reset HSEEN and HSEBPS bits */
  108. RCC->GCCR &= GCCR_HSEEN_HSEBPS_RESET;
  109. /* Set the new state of HSE */
  110. RCC->GCCR |= RCC_HSE;
  111. }
  112. /**
  113. * @brief Wait for HSE start-up.
  114. * @param None
  115. * @retval The HSE start-up result(SUCCESS or ERROR)
  116. */
  117. TypeState RCC_WaitForHSEStartUp(void)
  118. {
  119. __IO uint32_t HSE_StartOk_Counter = 0;
  120. TypeState HSEState = RESET;
  121. /* Wait until HSE is ready and if timeout to exit */
  122. while ((HSE_StartOk_Counter != HSE_STARTUP_TIMEOUT) && (HSEState == RESET)) {
  123. HSEState = RCC_GetBitState(RCC_FLAG_HSESTB);
  124. HSE_StartOk_Counter++;
  125. }
  126. if (RCC_GetBitState(RCC_FLAG_HSESTB) != RESET) {
  127. return SUCCESS;
  128. } else {
  129. return ERROR;
  130. }
  131. }
  132. /**
  133. * @brief Adjust the Internal High Speed oscillator (HSI) calibration value.
  134. * @param HSICalibrationValue: the HSI calibration value.
  135. * This parameter must be between 0 and 0x1F.
  136. * @retval None
  137. */
  138. void RCC_AdjustHSICalibrationValue(uint8_t HSICalibrationValue)
  139. {
  140. uint32_t temp_adjust = 0;
  141. temp_adjust = RCC->GCCR;
  142. /* Clear HSIADJ[4:0] bits */
  143. temp_adjust &= ~RCC_GCCR_HSIADJ;
  144. /* Set HSIADJ[4:0] bits according to HSICalibrationValue value */
  145. temp_adjust |= (uint32_t)HSICalibrationValue << GCCR_HSIADJ_OFFSET ;
  146. /* Store the calibration value */
  147. RCC->GCCR = temp_adjust;
  148. }
  149. /**
  150. * @brief Enable or disable the Internal High Speed oscillator (HSI).
  151. * @param NewValue: new value of the HSI.
  152. * This parameter can be: ENABLE or DISABLE.
  153. * @retval None
  154. */
  155. void RCC_HSI_Enable(TypeState NewValue)
  156. {
  157. if (NewValue != DISABLE) {
  158. RCC->GCCR |= RCC_GCCR_HSIEN;
  159. } else {
  160. RCC->GCCR &= ~RCC_GCCR_HSIEN;
  161. }
  162. }
  163. /**
  164. * @brief Configure the PLL clock source and multiplication factor.
  165. * @param RCC_PLLSelect: specify the PLL clock source.
  166. * For @b GD32_Connectivity_line_devices, this parameter can be one of the following values:
  167. * @arg RCC_PLLSOURCE_HSI_DIV2: HSI divided by 2 selected as PLL clock source
  168. * @arg RCC_PLLSOURCE_PREDIV1: PREDIV1 clock selected as PLL clock source
  169. * For @b other GD32_devices, this parameter can be one of the following values:
  170. * @arg RCC_PLLSOURCE_HSI_DIV2: HSI divided by 2 selected as PLL clock source
  171. * @arg RCC_PLLSOURCE_HSE_DIV1: HSE selected as PLL clock source
  172. * @arg RCC_PLLSOURCE_HSE_DIV2: HSE divided by 2 selected as PLL clock source
  173. * @param RCC_PLLMF: specify the PLL multiplication factor.
  174. * For @b GD32_Connectivity_line_devices, this parameter can be RCC_PLLMUL_x where x:{[2,32], 6_5}
  175. * For @b other_GD32_devices, this parameter can be RCC_PLLMUL_x where x:[2,32]
  176. * @retval None
  177. */
  178. void RCC_PLLConfig(uint32_t RCC_PLLSelect, uint32_t RCC_PLLMF)
  179. {
  180. uint32_t temp = 0;
  181. temp = RCC->GCFGR;
  182. /* Clear PLLSEL [16] and PLLMF[4:0] bits */
  183. temp &= ~(RCC_GCFGR_PLLMF | RCC_GCFGR_PLLSEL);
  184. /* Set the PLLSEL and PLLMF */
  185. temp |= RCC_PLLSelect | RCC_PLLMF;
  186. RCC->GCFGR = temp;
  187. }
  188. /**
  189. * @brief Enable or disable the PLL.
  190. * @param NewValue: new value of the PLL.
  191. * This parameter can be: ENABLE or DISABLE.
  192. * @retval None
  193. */
  194. void RCC_PLL_Enable(TypeState NewValue)
  195. {
  196. if (NewValue != DISABLE) {
  197. RCC->GCCR |= RCC_GCCR_PLLEN;
  198. } else {
  199. RCC->GCCR &= ~RCC_GCCR_PLLEN;
  200. }
  201. }
  202. #ifdef GD32F10X_CL
  203. /**
  204. * @brief Configure the PREDV1 division factor.
  205. * @param RCC_PREDV1_Source: specifies the PREDV1 clock source.
  206. * This parameter can be one of the following values:
  207. * @arg RCC_PREDIV1_SOURCE_HSE: HSE selected as PREDIV1 clock
  208. * @arg RCC_PREDIV1_SOURCE_PLL2: PLL2 selected as PREDIV1 clock
  209. * @param RCC_PREDV1_DIV: specify the PREDV1 division factor.
  210. * This parameter can be RCC_PREDIV1_DIVx where x:[1,16]
  211. * @retval None
  212. */
  213. void RCC_PREDV1Config(uint32_t RCC_PREDV1_Source, uint32_t RCC_PREDV1_Div)
  214. {
  215. uint32_t temp = 0;
  216. temp = RCC->GCFGR2;
  217. /* Clear PREDV1[3:0] and PREDV1SEL bits */
  218. temp &= ~(RCC_GCFGR2_PREDV1 | RCC_GCFGR2_PREDV1SEL);
  219. /* Set the PREDV1 division factor and PREDV1SEL */
  220. temp |= (RCC_PREDV1_Div | RCC_PREDV1_Source);
  221. RCC->GCFGR2 = temp;
  222. }
  223. /**
  224. * @brief Configure the PREDV2 division factor.
  225. * @param RCC_PREDV2_Div: specify the PREDV2 clock division factor.
  226. * This parameter can be RCC_PREDIV2_DIVx where x:[1,16]
  227. * @retval None
  228. */
  229. void RCC_PREDV2Config(uint32_t RCC_PREDV2_Div)
  230. {
  231. uint32_t temp = 0;
  232. temp = RCC->GCFGR2;
  233. /* Clear PREDV2[3:0] bits */
  234. temp &= ~RCC_GCFGR2_PREDV2;
  235. /* Set the PREDV2 division factor */
  236. temp |= RCC_PREDV2_Div;
  237. RCC->GCFGR2 = temp;
  238. }
  239. /**
  240. * @brief Configure the PLL2 multiplication factor.
  241. * @param RCC_PLL2MF: specify the PLL2 multiplication factor.
  242. * This parameter can be RCC_PLL2MUL_x where x:{[8,14], 16, 20}
  243. * @retval None
  244. */
  245. void RCC_PLL2Config(uint32_t RCC_PLL2MF)
  246. {
  247. uint32_t temp = 0;
  248. temp = RCC->GCFGR2;
  249. /* Clear PLL2MF[3:0] bits */
  250. temp &= ~RCC_GCFGR2_PLL2MF;
  251. /* Set the PLL2 configuration bits */
  252. temp |= RCC_PLL2MF;
  253. RCC->GCFGR2 = temp;
  254. }
  255. /**
  256. * @brief Enable or disable the PLL2.
  257. * @param NewValue: new value of the PLL2.
  258. * This parameter can be: ENABLE or DISABLE.
  259. * @retval None
  260. */
  261. void RCC_PLL2_Enable(TypeState NewValue)
  262. {
  263. if (NewValue != DISABLE) {
  264. RCC->GCCR |= RCC_GCCR_PLL2EN;
  265. } else {
  266. RCC->GCCR &= ~RCC_GCCR_PLL2EN;
  267. }
  268. }
  269. /**
  270. * @brief Configure the PLL3 multiplication factor.
  271. * @param RCC_PLL3MF: specify the PLL3 multiplication factor.
  272. * This parameter can be RCC_PLL3MUL_x where x:{[8,14], 16, 20}
  273. * @retval None
  274. */
  275. void RCC_PLL3Config(uint32_t RCC_PLL3MF)
  276. {
  277. uint32_t temp = 0;
  278. temp = RCC->GCFGR2;
  279. /* Clear PLL2MF[3:0] bits */
  280. temp &= ~RCC_GCFGR2_PLL3MF;
  281. /* Set the PLL3 configuration bits */
  282. temp |= RCC_PLL3MF;
  283. RCC->GCFGR2 = temp;
  284. }
  285. /**
  286. * @brief Enable or disable the PLL3.
  287. * @param NewValue: new value of the PLL3.
  288. * This parameter can be: ENABLE or DISABLE.
  289. * @retval None
  290. */
  291. void RCC_PLL3_Enable(TypeState NewValue)
  292. {
  293. if (NewValue != DISABLE) {
  294. RCC->GCCR |= RCC_GCCR_PLL3EN;
  295. } else {
  296. RCC->GCCR &= ~RCC_GCCR_PLL3EN;
  297. }
  298. }
  299. #endif /* GD32F10X_CL */
  300. /**
  301. * @brief Configure the system clock (CK_SYS)
  302. * @param RCC_SYSCLKSource: specify the system clock source
  303. * This parameter can be one of the following values:
  304. * @arg RCC_SYSCLKSOURCE_HSI: selecte HSI as CK_SYS source
  305. * @arg RCC_SYSCLKSOURCE_HSE: selecte HSE as CK_SYS source
  306. * @arg RCC_SYSCLKSOURCE_PLLCLK: selecte PLL as CK_SYS source
  307. * @retval None
  308. */
  309. void RCC_CK_SYSConfig(uint32_t RCC_SYSCLKSource)
  310. {
  311. uint32_t temp = 0;
  312. temp = RCC->GCFGR;
  313. /* Clear SCS[1:0] bits */
  314. temp &= ~RCC_GCFGR_SCS;
  315. /* Set SCS[1:0] bits according to RCC_SYSCLKSource value */
  316. temp |= RCC_SYSCLKSource;
  317. /* Store the new value */
  318. RCC->GCFGR = temp;
  319. }
  320. /**
  321. * @brief Get the system clock source.
  322. * @param None
  323. * @retval Get the system clock source. The returned value can be one
  324. * of the following values:
  325. * @arg 0x00: HSI used as CK_SYS source
  326. * @arg 0x04: HSE used as CK_SYS source
  327. * @arg 0x08: PLL used as CK_SYS source
  328. */
  329. uint8_t RCC_GetCK_SYSSource(void)
  330. {
  331. return ((uint8_t)(RCC->GCFGR & RCC_GCFGR_SCSS));
  332. }
  333. /**
  334. * @brief Configure the AHB clock.
  335. * @param RCC_CK_SYSDiv: specify the AHB clock divider. This clock is derived from
  336. * the system clock (CK_SYS).
  337. * This parameter can be one of the following values:
  338. * @arg RCC_SYSCLK_DIV1: AHB clock = CK_SYS
  339. * @arg RCC_SYSCLK_DIV2: AHB clock = CK_SYS/2
  340. * @arg RCC_SYSCLK_DIV4: AHB clock = CK_SYS/4
  341. * @arg RCC_SYSCLK_DIV8: AHB clock = CK_SYS/8
  342. * @arg RCC_SYSCLK_DIV16: AHB clock = CK_SYS/16
  343. * @arg RCC_SYSCLK_DIV64: AHB clock = CK_SYS/64
  344. * @arg RCC_SYSCLK_DIV128: AHB clock = CK_SYS/128
  345. * @arg RCC_SYSCLK_DIV256: AHB clock = CK_SYS/256
  346. * @arg RCC_SYSCLK_DIV512: AHB clock = CK_SYS/512
  347. * @retval None
  348. */
  349. void RCC_AHBConfig(uint32_t RCC_CK_SYSDiv)
  350. {
  351. uint32_t temp = 0;
  352. temp = RCC->GCFGR;
  353. /* Clear AHBPS[3:0] bits */
  354. temp &= ~RCC_GCFGR_AHBPS;
  355. /* Set AHBPS[3:0] bits according to RCC_CK_SYSDiv value */
  356. temp |= RCC_CK_SYSDiv;
  357. /* Store the new value */
  358. RCC->GCFGR = temp;
  359. }
  360. /**
  361. * @brief Configure the APB1 clock.
  362. * @param RCC_APB1: specify the APB1 clock divider. This clock is derived from
  363. * the AHB clock.
  364. * This parameter can be one of the following values:
  365. * @arg RCC_APB1AHB_DIV1: APB1 clock = AHB
  366. * @arg RCC_APB1AHB_DIV2: APB1 clock = AHB/2
  367. * @arg RCC_APB1AHB_DIV4: APB1 clock = AHB/4
  368. * @arg RCC_APB1AHB_DIV8: APB1 clock = AHB/8
  369. * @arg RCC_APB1AHB_DIV16: APB1 clock = AHB/16
  370. * @retval None
  371. */
  372. void RCC_APB1Config(uint32_t RCC_APB1)
  373. {
  374. uint32_t temp = 0;
  375. temp = RCC->GCFGR;
  376. /* Clear APB1PS[2:0] bits */
  377. temp &= ~RCC_GCFGR_APB1PS;
  378. /* Set APB1PS[2:0] bits according to RCC_APB1 value */
  379. temp |= RCC_APB1;
  380. /* Store the new value */
  381. RCC->GCFGR = temp;
  382. }
  383. /**
  384. * @brief Configure the APB2 clock.
  385. * @param RCC_APB2: specify the APB2 clock divider. This clock is derived from
  386. * the AHB clock.
  387. * This parameter can be one of the following values:
  388. * @arg RCC_APB2AHB_DIV1: APB2 clock = AHB
  389. * @arg RCC_APB2AHB_DIV2: APB2 clock = AHB/2
  390. * @arg RCC_APB2AHB_DIV4: APB2 clock = AHB/4
  391. * @arg RCC_APB2AHB_DIV8: APB2 clock = AHB/8
  392. * @arg RCC_APB2AHB_DIV16: APB2 clock = AHB/16
  393. * @retval None
  394. */
  395. void RCC_APB2Config(uint32_t RCC_APB2)
  396. {
  397. uint32_t temp = 0;
  398. temp = RCC->GCFGR;
  399. /* Clear APB2PS[2:0] bits */
  400. temp &= ~RCC_GCFGR_APB2PS;
  401. /* Set APB2PS[2:0] bits according to RCC_APB2 value */
  402. temp |= RCC_APB2;
  403. /* Store the new value */
  404. RCC->GCFGR = temp;
  405. }
  406. #ifdef GD32F10X_CL
  407. /**
  408. * @brief Configure the USB_OTG clock (USB_OTG CLK).
  409. * @param RCC_OTGFSCLK: specify the USB_OTG clock source. This clock is derived
  410. * from the PLL.
  411. * This parameter can be one of the following values:
  412. * @arg RCC_OTGCLK_PLL_DIV1_5: USB_OTG clock = PLL/1.5
  413. * @arg RCC_OTGCLK_PLL_DIV1: USB_OTG clock = PLL
  414. * @arg RCC_OTGCLK_PLL_DIV2: USB_OTG clock = PLL/2
  415. * @arg RCC_OTGCLK_PLL_DIV2_5: USB-OTG clock = PLL/2.5
  416. * @retval None
  417. */
  418. void RCC_OTGFSCLKConfig(uint32_t RCC_OTGFSCLK)
  419. {
  420. /* Clear OTGFSPS bit */
  421. RCC->GCFGR &= ~RCC_GCFGR_OTGFSPS;
  422. /* Set OTGFSPS bits according to RCC_OTGFSCLK value */
  423. RCC->GCFGR |= RCC_OTGFSCLK;
  424. }
  425. #else
  426. /**
  427. * @brief Configure the USB clock (USBCLK).
  428. * @param RCC_USBCLK: specify the USB clock source. This clock is derived
  429. * from the PLL.
  430. * This parameter can be one of the following values:
  431. * @arg RCC_USBCLK_PLL_DIV1_5: USB clock = PLL/1.5
  432. * @arg RCC_USBCLK_PLL_DIV1: USB clock = PLL
  433. * @arg RCC_USBCLK_PLL_DIV2: USB clock = PLL/2
  434. * @arg RCC_USBCLK_PLL_DIV2_5: USB clock = PLL/2.5
  435. * @retval None
  436. */
  437. void RCC_USBCLKConfig(uint32_t RCC_USBCLK)
  438. {
  439. /* Clear USBPS bit */
  440. RCC->GCFGR &= ~RCC_GCFGR_USBPS;
  441. /* Set USBPS bits according to RCC_USBCLK value */
  442. RCC->GCFGR |= RCC_USBCLK;
  443. }
  444. #endif /* GD32F10X_CL */
  445. /**
  446. * @brief Configure the ADC clock (ADCCLK).
  447. * @param RCC_ADCCLK: specify the ADC clock source. This clock is derived from APB2 clock.
  448. * This parameter can be one of the following values:
  449. * @arg RCC_ADCCLK_APB2_DIV2: ADC clock = APB2/2
  450. * @arg RCC_ADCCLK_APB2_DIV4: ADC clock = APB2/4
  451. * @arg RCC_ADCCLK_APB2_DIV6: ADC clock = APB2/6
  452. * @arg RCC_ADCCLK_APB2_DIV8: ADC clock = APB2/8
  453. * @arg RCC_ADCCLK_APB2_DIV12: ADC clock = APB2/12
  454. * @arg RCC_ADCCLK_APB2_DIV16: ADC clock = APB2/16
  455. * @retval None
  456. */
  457. void RCC_ADCCLKConfig(uint32_t RCC_ADCCLK)
  458. {
  459. /* Clear ADCPS bit */
  460. RCC->GCFGR &= ~RCC_GCFGR_ADCPS;
  461. /* Set ADCPS bits according to RCC_APB2 value */
  462. RCC->GCFGR |= RCC_ADCCLK;
  463. }
  464. #ifdef GD32F10X_CL
  465. /**
  466. * @brief Configure the I2S2 clock source(I2S2CLK).
  467. * @param RCC_I2S2CLK: specify the I2S2 clock source.
  468. * This parameter can be one of the following values:
  469. * @arg RCC_I2S2CLK_SYSCLK: system clock selected as I2S2 clock entry
  470. * @arg RCC_I2S2CLK_PLL3: PLL3 clock selected as I2S2 clock entry
  471. * @note If the RCC_I2S2CLK_PLL3 selected, the I2S2 clock is (PLL3 x 2).
  472. * @retval None
  473. */
  474. void RCC_I2S2CLKConfig(uint32_t RCC_I2S2CLK)
  475. {
  476. /* Clear I2S2SEL bit */
  477. RCC->GCFGR2 &= ~RCC_GCFGR2_I2S2SEL;
  478. /* Set I2S2CLK bits according to RCC_I2S2CLK value */
  479. RCC->GCFGR2 |= RCC_I2S2CLK;
  480. }
  481. /**
  482. * @brief Configure the I2S3 clock source(I2S3CLK).
  483. * @param RCC_I2S3CLK: specify the I2S3 clock source.
  484. * This parameter can be one of the following values:
  485. * @arg RCC_I2S3CLK_SYSCLK: system clock selected as I2S3 clock entry
  486. * @arg RCC_I2S3CLK_PLL3: PLL3 clock selected as I2S3 clock entry
  487. * @note If the RCC_I2S3CLK_PLL3 selected, the I2S3 clock is (PLL3 x 2).
  488. * @retval None
  489. */
  490. void RCC_I2S3CLKConfig(uint32_t RCC_I2S3CLK)
  491. {
  492. /* Clear I2S3SEL bit */
  493. RCC->GCFGR2 &= ~RCC_GCFGR2_I2S3SEL;
  494. /* Set I2S3CLK bits according to RCC_I2S3CLK value */
  495. RCC->GCFGR2 |= RCC_I2S3CLK;
  496. }
  497. #endif /* GD32F10X_CL */
  498. /**
  499. * @brief Configure the External Low Speed oscillator (LSE).
  500. * @param RCC_LSE: specify the new state of the LSE.
  501. * This parameter can be one of the following values:
  502. * @arg RCC_LSE_OFF: turn off the LSE
  503. * @arg RCC_LSE_EN: turn on the LSE
  504. * @arg RCC_LSE_BYPASS: LSE bypassed with external clock
  505. * @retval None
  506. */
  507. void RCC_LSEConfig(uint32_t RCC_LSE)
  508. {
  509. /* Reset LSEEN and LSEBPS bits before configuring the LSE */
  510. RCC->BDCR &= ~(RCC_BDCR_LSEEN);
  511. RCC->BDCR &= ~(RCC_BDCR_LSEBPS);
  512. /* Configure LSE */
  513. RCC->BDCR |= RCC_LSE;
  514. }
  515. /**
  516. * @brief Enable or disable the Internal Low Speed oscillator (LSI).
  517. * @param NewValue: new value of the LSI.
  518. * This parameter can be: ENABLE or DISABLE.
  519. * @retval None
  520. */
  521. void RCC_LSI_Enable(TypeState NewValue)
  522. {
  523. if (NewValue != DISABLE) {
  524. RCC->GCSR |= RCC_GCSR_LSIEN;
  525. } else {
  526. RCC->GCSR &= ~RCC_GCSR_LSIEN;
  527. }
  528. }
  529. /**
  530. * @brief Configure the RTC clock (RTCCLK).
  531. * @param RCC_RTCCLKSource: specify the RTC clock source.
  532. * This parameter can be one of the following values:
  533. * @arg RCC_RTCCLKSOURCE_LSE: selecte LSE as RTC clock
  534. * @arg RCC_RTCCLKSOURCE_LSI: selecte LSI as RTC clock
  535. * @arg RCC_RTCCLKSOURCE_HSE_DIV128: selecte HSE divided by 128 as RTC clock
  536. * @note if using HSE as RTC source, the maximum clock frequency for RTC is 2 MHz.
  537. * @retval None
  538. */
  539. void RCC_RTCCLKConfig(uint32_t RCC_RTCCLKSource)
  540. {
  541. /* Clear RTCSEL bit */
  542. RCC->BDCR &= ~RCC_BDCR_RTCSEL;
  543. /* Select the RTC clock source */
  544. RCC->BDCR |= RCC_RTCCLKSource;
  545. }
  546. /**
  547. * @brief Enable or disable the RTC clock.
  548. * @param NewValue: new state of the RTC clock.
  549. * This parameter can be: ENABLE or DISABLE.
  550. * @retval None
  551. */
  552. void RCC_RTCCLK_Enable(TypeState NewValue)
  553. {
  554. if (NewValue != DISABLE) {
  555. RCC->BDCR |= RCC_BDCR_RTCEN;
  556. } else {
  557. RCC->BDCR &= ~RCC_BDCR_RTCEN;
  558. }
  559. }
  560. /**
  561. * @brief Get the frequencies of the CK_SYS, AHB, APB1, APB2, ADC clocks.
  562. * @retval None
  563. */
  564. void RCC_GetClocksFreq(RCC_ClocksPara *RCC_Clocks)
  565. {
  566. uint32_t temp = 0, pllmf = 0, pllmf4 = 0, pllselect = 0, presc = 0;
  567. #ifdef GD32F10X_CL
  568. uint32_t prediv1select = 0, prediv1factor = 0, prediv2factor = 0, pll2mf = 0;
  569. #endif /* GD32F10X_CL */
  570. /* Get CK_SYS source */
  571. temp = RCC->GCFGR & RCC_GCFGR_SCSS;
  572. switch (temp) {
  573. case 0x00: /* HSI used as CK_SYS */
  574. RCC_Clocks->CK_SYS_Frequency = HSI_VALUE;
  575. break;
  576. case 0x04: /* HSE used as CK_SYS */
  577. RCC_Clocks->CK_SYS_Frequency = HSE_VALUE;
  578. break;
  579. case 0x08: /* PLL used as CK_SYS */
  580. #ifdef GD32F10X_CL
  581. /* Get PLL clock source and multiplication factor */
  582. /* Get PLLMF[3:0] */
  583. pllmf = RCC->GCFGR & RCC_GCFGR_PLLMF_3_0;
  584. /* Get PLLMF[4] */
  585. pllmf4 = RCC->GCFGR & RCC_GCFGR_PLLMF_4;
  586. pllmf4 = ((pllmf4 >> 29) * 16);
  587. pllmf = (pllmf >> 18) + pllmf4;
  588. if (pllmf == 0x0D) {
  589. /* PLL multiplication factor = PLL input clock * 6.5*/
  590. /* Beacause of the float number 6.5, pllmf * 2 to remove the float number */
  591. pllmf = 13 * 2 / 2;
  592. } else if (pllmf >= 0x0F) {
  593. pllmf += 1;
  594. /* Here is consistent with the above that pllmf * 2 to remove the float number*/
  595. pllmf = pllmf * 2;
  596. } else {
  597. pllmf += 2;
  598. /* Here is consistent with the above that pllmf * 2 to remove the float number*/
  599. pllmf = pllmf * 2;
  600. }
  601. pllselect = RCC->GCFGR & RCC_GCFGR_PLLSEL;
  602. if (pllselect == 0x00) {
  603. /* HSI clock divided by 2 selected as PLL clock source */
  604. /* Here is consistent with the above that pllmf / 2 */
  605. RCC_Clocks->CK_SYS_Frequency = (HSI_VALUE >> HSI_CLOCK_DIVIDED_2) * pllmf / 2;
  606. } else {
  607. /* PREDIV1 selected as PLL clock entry */
  608. /* Get PREDIV1 clock source and division factor */
  609. prediv1select = RCC->GCFGR2 & RCC_GCFGR2_PREDV1SEL;
  610. prediv1factor = (RCC->GCFGR2 & RCC_GCFGR2_PREDV1) + 1;
  611. if (prediv1select == 0) {
  612. /* HSE clock selected as PREDIV1 clock entry */
  613. /* Here is consistent with the above that pllmf / 2 */
  614. RCC_Clocks->CK_SYS_Frequency = (HSE_VALUE / prediv1factor) * pllmf / 2;
  615. } else {
  616. /* PLL2 clock selected as PREDIV1 clock entry */
  617. /* Get PREDIV2 division factor and PLL2 multiplication factor */
  618. prediv2factor = ((RCC->GCFGR2 & RCC_GCFGR2_PREDV2) >> 4) + 1;
  619. pll2mf = ((RCC->GCFGR2 & RCC_GCFGR2_PLL2MF) >> 8);
  620. if (pll2mf != 15) {
  621. pll2mf += 2;
  622. } else {
  623. pll2mf += 5;
  624. }
  625. /* Here is consistent with the above that pllmf / 2 */
  626. RCC_Clocks->CK_SYS_Frequency = (((HSE_VALUE / prediv2factor) * pll2mf) / prediv1factor) * pllmf / 2;
  627. }
  628. }
  629. #else
  630. /* Get PLL clock source and multiplication factor */
  631. /* Get PLLMF[3:0] */
  632. pllmf = RCC->GCFGR & RCC_GCFGR_PLLMF_3_0;
  633. /* Get PLLMF[4] */
  634. pllmf4 = RCC->GCFGR & RCC_GCFGR_PLLMF_4;
  635. pllmf4 = ((pllmf4 >> 27) * 16);
  636. pllmf = (pllmf >> 18) + pllmf4;
  637. if (pllmf >= 0x0F)
  638. pllmf += 1;
  639. else
  640. pllmf += 2;
  641. pllselect = RCC->GCFGR & RCC_GCFGR_PLLSEL;
  642. if (pllselect == 0x00) {
  643. /* HSI clock divided by 2 selected as PLL clock source */
  644. RCC_Clocks->CK_SYS_Frequency = (HSI_VALUE >> HSI_CLOCK_DIVIDED_2) * pllmf;
  645. } else {
  646. if ((RCC->GCFGR & RCC_GCFGR_PLLPREDV) != (uint32_t)RESET) {
  647. /* HSE clock divided by 2 */
  648. RCC_Clocks->CK_SYS_Frequency = (HSE_VALUE >> HSE_CLOCK_DIVIDED_2) * pllmf;
  649. } else {
  650. RCC_Clocks->CK_SYS_Frequency = HSE_VALUE * pllmf;
  651. }
  652. }
  653. #endif/* GD32F10X_CL */
  654. break;
  655. default: /* HSI used as system clock */
  656. RCC_Clocks->CK_SYS_Frequency = HSI_VALUE;
  657. break;
  658. }
  659. /* Get AHB prescaler */
  660. temp = RCC->GCFGR & RCC_GCFGR_AHBPS;
  661. temp = temp >> 4;
  662. presc = AHBPrescTable[temp];
  663. /* Get AHB clock frequency */
  664. RCC_Clocks->AHB_Frequency = RCC_Clocks->CK_SYS_Frequency >> presc;
  665. /* Get APB1 prescaler */
  666. temp = RCC->GCFGR & RCC_GCFGR_APB1PS;
  667. temp = temp >> 8;
  668. presc = APBPrescTable[temp];
  669. /* Get APB1 clock frequency */
  670. RCC_Clocks->APB1_Frequency = RCC_Clocks->AHB_Frequency >> presc;
  671. /* Get APB2 prescaler */
  672. temp = RCC->GCFGR & RCC_GCFGR_APB2PS;
  673. temp = temp >> 11;
  674. presc = APBPrescTable[temp];
  675. /* Get APB2 clock frequency */
  676. RCC_Clocks->APB2_Frequency = RCC_Clocks->AHB_Frequency >> presc;
  677. /* Get ADCCLK clock frequency */
  678. temp = ((RCC->GCFGR & RCC_GCFGR_ADCPS_2) >> 26);
  679. temp += ((RCC->GCFGR & (RCC_GCFGR_ADCPS_0 | RCC_GCFGR_ADCPS_1)) >> 14);
  680. switch (temp) {
  681. case 0x00: /* ADC Clock is derived from APB2/2 */
  682. RCC_Clocks->ADCCLK_Frequency = (RCC_Clocks->APB2_Frequency / 2);
  683. break;
  684. case 0x01: /* ADC Clock is derived from APB2/4 */
  685. RCC_Clocks->ADCCLK_Frequency = (RCC_Clocks->APB2_Frequency / 4);
  686. break;
  687. case 0x02: /* ADC Clock is derived from APB2/6 */
  688. RCC_Clocks->ADCCLK_Frequency = (RCC_Clocks->APB2_Frequency / 6);
  689. break;
  690. case 0x03: /* ADC Clock is derived from APB2/8 */
  691. RCC_Clocks->ADCCLK_Frequency = (RCC_Clocks->APB2_Frequency / 8);
  692. break;
  693. case 0x04: /* ADC Clock is derived from APB2/2 */
  694. RCC_Clocks->ADCCLK_Frequency = (RCC_Clocks->APB2_Frequency / 2);
  695. break;
  696. case 0x05: /* ADC Clock is derived from APB2/12 */
  697. RCC_Clocks->ADCCLK_Frequency = (RCC_Clocks->APB2_Frequency / 12);
  698. break;
  699. case 0x06: /* ADC Clock is derived from APB2/8 */
  700. RCC_Clocks->ADCCLK_Frequency = (RCC_Clocks->APB2_Frequency / 8);
  701. break;
  702. case 0x07: /* ADC Clock is derived from APB2/16 */
  703. RCC_Clocks->ADCCLK_Frequency = (RCC_Clocks->APB2_Frequency / 16);
  704. break;
  705. default:
  706. break;
  707. }
  708. }
  709. /**
  710. * @brief Enable or disable the AHB peripheral clock.
  711. * @param RCC_AHBPeriph: specify the AHB peripheral clock.
  712. * For @b GD32_Connectivity_line_devices,
  713. * this parameter can be any combination of the following values:
  714. * @arg RCC_AHBPERIPH_DMA1
  715. * @arg RCC_AHBPERIPH_DMA2
  716. * @arg RCC_AHBPERIPH_SRAM
  717. * @arg RCC_AHBPERIPH_FMC
  718. * @arg RCC_AHBPERIPH_CRC
  719. * @arg RCC_AHBPERIPH_EXMC
  720. * @arg RCC_AHBPERIPH_OTG_FS
  721. * @arg RCC_AHBPERIPH_ETH_MAC
  722. * @arg RCC_AHBPERIPH_ETH_MAC_RX
  723. * @arg RCC_AHBPERIPH_ETH_MAC_TX
  724. *
  725. * For @b other_GD32_devices, this parameter can be any combination of the
  726. * following values:
  727. * @arg RCC_AHBPERIPH_DMA1
  728. * @arg RCC_AHBPERIPH_DMA2
  729. * @arg RCC_AHBPERIPH_SRAM
  730. * @arg RCC_AHBPERIPH_FMC
  731. * @arg RCC_AHBPERIPH_CRC
  732. * @arg RCC_AHBPERIPH_EXMC
  733. * @arg RCC_AHBPERIPH_SDIO
  734. * @param NewValue: new state of the peripheral clock.
  735. * This parameter can be: ENABLE or DISABLE.
  736. * @retval None
  737. */
  738. void RCC_AHBPeriphClock_Enable(uint32_t RCC_AHBPeriph, TypeState NewValue)
  739. {
  740. if (NewValue != DISABLE) {
  741. RCC->AHBCCR |= RCC_AHBPeriph;
  742. } else {
  743. RCC->AHBCCR &= ~RCC_AHBPeriph;
  744. }
  745. }
  746. /**
  747. * @brief Enable or disable the APB2 peripheral clock.
  748. * @param RCC_APB2Periph: specify the APB2 peripheral clock.
  749. * This parameter can be any combination of the following values:
  750. * @arg RCC_APB2PERIPH_AF
  751. * @arg RCC_APB2PERIPH_GPIOA
  752. * @arg RCC_APB2PERIPH_GPIOB
  753. * @arg RCC_APB2PERIPH_GPIOC
  754. * @arg RCC_APB2PERIPH_GPIOD
  755. * @arg RCC_APB2PERIPH_GPIOE
  756. * @arg RCC_APB2PERIPH_GPIOF
  757. * @arg RCC_APB2PERIPH_GPIOG
  758. * @arg RCC_APB2PERIPH_ADC1
  759. * @arg RCC_APB2PERIPH_ADC2
  760. * @arg RCC_APB2PERIPH_TIMER1
  761. * @arg RCC_APB2PERIPH_SPI1
  762. * @arg RCC_APB2PERIPH_TIMER8
  763. * @arg RCC_APB2PERIPH_USART1
  764. * @arg RCC_APB2PERIPH_ADC3
  765. * @arg RCC_APB2PERIPH_TIMER9
  766. * @arg RCC_APB2PERIPH_TIMER10
  767. * @arg RCC_APB2PERIPH_TIMER11
  768. * @param NewValue: new state of the peripheral clock.
  769. * This parameter can be: ENABLE or DISABLE.
  770. * @retval None
  771. */
  772. void RCC_APB2PeriphClock_Enable(uint32_t RCC_APB2Periph, TypeState NewValue)
  773. {
  774. if (NewValue != DISABLE) {
  775. RCC->APB2CCR |= RCC_APB2Periph;
  776. } else {
  777. RCC->APB2CCR &= ~RCC_APB2Periph;
  778. }
  779. }
  780. /**
  781. * @brief Enable or disable the APB1 peripheral clock.
  782. * @param RCC_APB1Periph: specify the APB1 peripheral clock.
  783. * This parameter can be any combination of the following values:
  784. * @arg RCC_APB1PERIPH_TIMER2
  785. * @arg RCC_APB1PERIPH_TIMER3
  786. * @arg RCC_APB1PERIPH_TIMER4
  787. * @arg RCC_APB1PERIPH_TIMER5
  788. * @arg RCC_APB1PERIPH_TIMER6
  789. * @arg RCC_APB1PERIPH_TIMER7
  790. * @arg RCC_APB1PERIPH_TIMER12
  791. * @arg RCC_APB1PERIPH_TIMER13
  792. * @arg RCC_APB1PERIPH_TIMER14
  793. * @arg RCC_APB1PERIPH_WWDG
  794. * @arg RCC_APB1PERIPH_SPI2
  795. * @arg RCC_APB1PERIPH_SPI3,
  796. * @arg RCC_APB1PERIPH_USART2
  797. * @arg RCC_APB1PERIPH_USART3
  798. * @arg RCC_APB1PERIPH_UART4
  799. * @arg RCC_APB1PERIPH_UART5
  800. * @arg RCC_APB1PERIPH_I2C1
  801. * @arg RCC_APB1PERIPH_I2C2
  802. * @arg RCC_APB1PERIPH_USB
  803. * @arg RCC_APB1PERIPH_CAN1
  804. * @arg RCC_APB1PERIPH_CAN2
  805. * @arg RCC_APB1PERIPH_BKP
  806. * @arg RCC_APB1PERIPH_PWR
  807. * @arg RCC_APB1PERIPH_DAC
  808. * @param NewState: new state of the specified peripheral clock.
  809. * This parameter can be: ENABLE or DISABLE.
  810. * @retval None
  811. */
  812. void RCC_APB1PeriphClock_Enable(uint32_t RCC_APB1Periph, TypeState NewValue)
  813. {
  814. if (NewValue != DISABLE) {
  815. RCC->APB1CCR |= RCC_APB1Periph;
  816. } else {
  817. RCC->APB1CCR &= ~RCC_APB1Periph;
  818. }
  819. }
  820. #ifdef GD32F10X_CL
  821. /**
  822. * @brief Force or release AHB peripheral reset.
  823. * @param RCC_AHBPeriphRST: specify the AHB peripheral to reset.
  824. * This parameter can be any combination of the following values:
  825. * @arg RCC_AHBPeriph_OTGFSRST
  826. * @arg RCC_AHBPeriph_ETHMACRST
  827. * @param NewState: new state of the specified peripheral reset.
  828. * This parameter can be: ENABLE or DISABLE.
  829. * @retval None
  830. */
  831. void RCC_AHBPeriphReset_Enable(uint32_t RCC_AHBPeriphRST, TypeState NewValue)
  832. {
  833. if (NewValue != DISABLE) {
  834. RCC->AHBRCR |= RCC_AHBPeriphRST;
  835. } else {
  836. RCC->AHBRCR &= ~RCC_AHBPeriphRST;
  837. }
  838. }
  839. #endif /* GD32F10X_CL */
  840. /**
  841. * @brief Force or release APB2 peripheral reset.
  842. * @param RCC_APB2PeriphRST: specify the APB2 peripheral to reset.
  843. * This parameter can be any combination of the following values:
  844. * @arg RCC_APB2PERIPH_AFRST
  845. * @arg RCC_APB2PERIPH_GPIOARST
  846. * @arg RCC_APB2PERIPH_GPIOBRST
  847. * @arg RCC_APB2PERIPH_GPIOCRST
  848. * @arg RCC_APB2PERIPH_GPIODRST
  849. * @arg RCC_APB2PERIPH_GPIOERST
  850. * @arg RCC_APB2PERIPH_GPIOFRST
  851. * @arg RCC_APB2PERIPH_GPIOGRST
  852. * @arg RCC_APB2PERIPH_ADC1RST
  853. * @arg RCC_APB2PERIPH_ADC2RST
  854. * @arg RCC_APB2PERIPH_TIMER1RST
  855. * @arg RCC_APB2PERIPH_SPI1RST
  856. * @arg RCC_APB2PERIPH_TIMER8RST
  857. * @arg RCC_APB2PERIPH_USART1RST
  858. * @arg RCC_APB2PERIPH_ADC3RST
  859. * @arg RCC_APB2PERIPH_TIMER9RST
  860. * @arg RCC_APB2PERIPH_TIMER10RST
  861. * @arg RCC_APB2PERIPH_TIMER11RST
  862. * @param NewValue: new state of the peripheral reset.
  863. * This parameter can be: ENABLE or DISABLE.
  864. * @retval None
  865. */
  866. void RCC_APB2PeriphReset_Enable(uint32_t RCC_APB2PeriphRST, TypeState NewValue)
  867. {
  868. if (NewValue != DISABLE) {
  869. RCC->APB2RCR |= RCC_APB2PeriphRST;
  870. } else {
  871. RCC->APB2RCR &= ~RCC_APB2PeriphRST;
  872. }
  873. }
  874. /**
  875. * @brief Force or release APB1 peripheral reset.
  876. * @param RCC_APB1PeriphRST: specify the APB1 peripheral to reset.
  877. * This parameter can be any combination of the following values:
  878. * @arg RCC_APB1PERIPH_TIMER2RST
  879. * @arg RCC_APB1PERIPH_TIMER3RST
  880. * @arg RCC_APB1PERIPH_TIMER4RST
  881. * @arg RCC_APB1PERIPH_TIMER5RST
  882. * @arg RCC_APB1PERIPH_TIMER6RST
  883. * @arg RCC_APB1PERIPH_TIMER7RST
  884. * @arg RCC_APB1PERIPH_TIMER12RST
  885. * @arg RCC_APB1PERIPH_TIMER13RST
  886. * @arg RCC_APB1PERIPH_TIMER14RST
  887. * @arg RCC_APB1PERIPH_WWDGRST
  888. * @arg RCC_APB1PERIPH_SPI2RST
  889. * @arg RCC_APB1PERIPH_SPI3RST
  890. * @arg RCC_APB1PERIPH_USART2RST
  891. * @arg RCC_APB1PERIPH_USART3RST
  892. * @arg RCC_APB1PERIPH_UART4RST
  893. * @arg RCC_APB1PERIPH_UART5RST
  894. * @arg RCC_APB1PERIPH_I2C1RST
  895. * @arg RCC_APB1PERIPH_I2C2RST
  896. * @arg RCC_APB1PERIPH_USBRST
  897. * @arg RCC_APB1PERIPH_CAN1RST
  898. * @arg RCC_APB1PERIPH_CAN2RST
  899. * @arg RCC_APB1PERIPH_BKPRST
  900. * @arg RCC_APB1PERIPH_PWRRST
  901. * @arg RCC_APB1PERIPH_DACRST
  902. * @param NewValue: new state of the peripheral clock (ENABLE or DISABLE).
  903. * @retval None
  904. */
  905. void RCC_APB1PeriphReset_Enable(uint32_t RCC_APB1PeriphRST, TypeState NewValue)
  906. {
  907. if (NewValue != DISABLE) {
  908. RCC->APB1RCR |= RCC_APB1PeriphRST;
  909. } else {
  910. RCC->APB1RCR &= ~RCC_APB1PeriphRST;
  911. }
  912. }
  913. /**
  914. * @brief Force or release the Backup domain reset.
  915. * @param NewValue: new state of the Backup domain reset (ENABLE or DISABLE).
  916. * @retval None
  917. */
  918. void RCC_BackupReset_Enable(TypeState NewValue)
  919. {
  920. if (NewValue != DISABLE) {
  921. RCC->BDCR |= RCC_BDCR_BKPRST;
  922. } else {
  923. RCC->BDCR &= ~RCC_BDCR_BKPRST;
  924. }
  925. }
  926. /**
  927. * @brief Enable or disable the Clock Security System.
  928. * @param NewValue: new value of the Clock Security System.
  929. * This parameter can be: ENABLE or DISABLE.
  930. * @retval None
  931. */
  932. void RCC_HSEClockMonitor_Enable(TypeState NewValue)
  933. {
  934. if (NewValue != DISABLE) {
  935. RCC->GCCR |= RCC_GCCR_CKMEN;
  936. } else {
  937. RCC->GCCR &= ~RCC_GCCR_CKMEN;
  938. }
  939. }
  940. /**
  941. * @brief Select the clock source to output on CKOUTSRC and the corresponding prescaler.
  942. * @param RCC_CKOUTSRC: specify the clock source to output.
  943. * For @b GD32_Connectivity_line_devices, this parameter can be one of the
  944. * following values:
  945. * @arg RCC_CKOUTSRC_NOCLOCK: No clock selected
  946. * @arg RCC_CKOUTSRC_SYSCLK: System clock selected
  947. * @arg RCC_CKOUTSRC_HSI: HSI oscillator clock selected
  948. * @arg RCC_CKOUTSRC_HSE: HSE oscillator clock selected
  949. * @arg RCC_CKOUTSRC_PLL2CLK: PLL2 clock selected
  950. * @arg RCC_CKOUTSRC_PLLCLK_DIV2: PLL clock divided by 2 selected
  951. * @arg RCC_CKOUTSRC_PLL3CLK: PLL3 clock selected
  952. * @arg RCC_CKOUTSRC_PLL3CLK_DIV2: PLL3 clock divided by 2 selected
  953. * @arg RCC_CKOUTSRC_EXT1: External 3-25 MHz oscillator clock selected
  954. * For @b other_GD32_devices, this parameter can be one of the following values:
  955. * @arg RCC_CKOUTSRC_NOCLOCK: No clock selected
  956. * @arg RCC_CKOUTSRC_SYSCLK: System clock selected
  957. * @arg RCC_CKOUTSRC_HSI: HSI oscillator clock selected
  958. * @arg RCC_CKOUTSRC_HSE: HSE oscillator clock selected
  959. * @arg RCC_CKOUTSRC_PLLCLK_DIV2: PLL clock divided by 2 selected
  960. * @retval None
  961. */
  962. void RCC_CKOUTSRCConfig(uint32_t RCC_CKOUTSRC)
  963. {
  964. uint32_t temp = 0;
  965. temp = RCC->GCFGR;
  966. /* Clear CKOUTSRC[2:0] bits */
  967. temp &= ~(RCC_GCFGR_CKOUTSEL);
  968. /* Set the RCC_CKOUTSRC */
  969. temp |= RCC_CKOUTSRC;
  970. /* Store the new value */
  971. RCC->GCFGR = temp;
  972. }
  973. /**
  974. * @brief Enable or disable RCC interrupts.
  975. * @param RCC_INT: specify the RCC interrupt sources.
  976. * For @b GD32_Connectivity_line_devices, this parameter can be any combination
  977. * of the following values
  978. * @arg RCC_INT_LSISTB: LSI ready interrupt
  979. * @arg RCC_INT_LSESTB LSE ready interrupt
  980. * @arg RCC_INT_HSISTB: HSI ready interrupt
  981. * @arg RCC_INT_HSESTB: HSE ready interrupt
  982. * @arg RCC_INT_PLLSTB: PLL ready interrupt
  983. * @arg RCC_INT_PLL2STB: PLL2 ready interrupt
  984. * @arg RCC_INT_PLL3STB: PLL3 ready interrupt
  985. *
  986. * For @b other_GD32_devices, this parameter can be any combination of the
  987. * following values
  988. * @arg RCC_INT_LSISTB: LSI ready interrupt
  989. * @arg RCC_INT_LSESTB: LSE ready interrupt
  990. * @arg RCC_INT_HSISTB: HSI ready interrupt
  991. * @arg RCC_INT_HSESTB: HSE ready interrupt
  992. * @arg RCC_INT_PLLSTB: PLL ready interrupt
  993. * @param NewValue: new state of the RCC interrupts (ENABLE or DISABLE).
  994. * @retval None
  995. */
  996. void RCC_INTConfig(uint8_t RCC_INT, TypeState NewValue)
  997. {
  998. if (NewValue != DISABLE) {
  999. /* Perform Byte access to RCC_GCIR[14:8] bits to enable the selected interrupts */
  1000. *(__IO uint8_t *) GCIR_BYTE1_ADDRESS |= RCC_INT;
  1001. } else {
  1002. /* Perform Byte access to RCC_GCIR bits to disable the selected interrupts */
  1003. *(__IO uint8_t *) GCIR_BYTE1_ADDRESS &= (uint8_t)~RCC_INT;
  1004. }
  1005. }
  1006. /**
  1007. * @brief Check whether the specified RCC flag is set or not.
  1008. * @param RCC_FLAG: specify the flag to check.
  1009. * For @b GD32_Connectivity_line_devices, this parameter can be one of the
  1010. * following values:
  1011. * @arg RCC_FLAG_HSISTB: HSI clock ready
  1012. * @arg RCC_FLAG_HSESTB: HSE clock ready
  1013. * @arg RCC_FLAG_PLLSTB: PLL clock ready
  1014. * @arg RCC_FLAG_PLL2STB: PLL2 clock ready
  1015. * @arg RCC_FLAG_PLL3STB: PLL3 clock ready
  1016. * @arg RCC_FLAG_LSESTB: LSE clock ready
  1017. * @arg RCC_FLAG_LSISTB: LSI clock ready
  1018. * @arg RCC_FLAG_EPRST: Pin reset
  1019. * @arg RCC_FLAG_POPDRST: POR/PDR reset
  1020. * @arg RCC_FLAG_SWRRST: Software reset
  1021. * @arg RCC_FLAG_IWDGRST: Independent Watchdog reset
  1022. * @arg RCC_FLAG_WWDGRST: Window Watchdog reset
  1023. * @arg RCC_FLAG_LPRRST: Low Power reset
  1024. * For @b other_GD32_devices, this parameter can be one of the following values:
  1025. * @arg RCC_FLAG_HSISTB: HSI oscillator clock ready
  1026. * @arg RCC_FLAG_HSESTB: HSE oscillator clock ready
  1027. * @arg RCC_FLAG_PLLSTB: PLL clock ready
  1028. * @arg RCC_FLAG_LSESTB: LSE oscillator clock ready
  1029. * @arg RCC_FLAG_LSISTB: LSI oscillator clock ready
  1030. * @arg RCC_FLAG_EPRST: Pin reset
  1031. * @arg RCC_FLAG_POPDRST: POR/PDR reset
  1032. * @arg RCC_FLAG_SWRRST: Software reset
  1033. * @arg RCC_FLAG_IWDGRST: Independent Watchdog reset
  1034. * @arg RCC_FLAG_WWDGRST: Window Watchdog reset
  1035. * @arg RCC_FLAG_LPRRST: Low Power reset
  1036. * @retval The new state of RCC_FLAG (SET or RESET).
  1037. */
  1038. TypeState RCC_GetBitState(uint8_t RCC_FLAG)
  1039. {
  1040. uint32_t temp = 0;
  1041. uint32_t statusreg = 0;
  1042. /* Get the RCC register index */
  1043. temp = RCC_FLAG >> 5;
  1044. /* The flag to check is in GCCR register */
  1045. if (temp == 1) {
  1046. statusreg = RCC->GCCR;
  1047. }
  1048. /* The flag to check is in BDCR register */
  1049. else if (temp == 2) {
  1050. statusreg = RCC->BDCR;
  1051. }
  1052. /* The flag to check is in GCSR register */
  1053. else {
  1054. statusreg = RCC->GCSR;
  1055. }
  1056. /* Get the flag position */
  1057. temp = RCC_FLAG & FLAG_MASK;
  1058. if ((statusreg & ((uint32_t)(1 << temp))) != (uint32_t)RESET) {
  1059. return SET;
  1060. } else {
  1061. return RESET;
  1062. }
  1063. }
  1064. /**
  1065. * @brief Clear the RCC all reset flags.
  1066. * @param None
  1067. * @retval None
  1068. */
  1069. void RCC_ClearBitState(void)
  1070. {
  1071. /* Set RSTFC bit to clear all reset flags */
  1072. RCC->GCSR |= RCC_GCSR_RSTFC;
  1073. }
  1074. /**
  1075. * @brief Check whether the RCC interrupt has occurred or not.
  1076. * @param RCC_INT: specify the RCC interrupt source to check.
  1077. * For @b GD32_Connectivity_line_devices, this parameter can be one of the
  1078. * following values:
  1079. * @arg RCC_INT_LSISTB: the flag of LSI ready interrupt
  1080. * @arg RCC_INT_LSESTB: the flag of LSE ready interrupt
  1081. * @arg RCC_INT_HSISTB: the flag of HSI ready interrupt
  1082. * @arg RCC_INT_HSESTB: the flag of HSE ready interrupt
  1083. * @arg RCC_INT_PLLSTB: the flag of PLL ready interrupt
  1084. * @arg RCC_INT_PLL2STB: the flag of PLL2 ready interrupt
  1085. * @arg RCC_INT_PLL3STB: the flag of PLL3 ready interrupt
  1086. * @arg RCC_INT_CKM: the flag of Clock Security System interrupt
  1087. * For @b other_GD32_devices, this parameter can be one of the following values:
  1088. * @arg RCC_INT_LSISTB: the flag of LSI ready interrupt
  1089. * @arg RCC_INT_LSESTB: the flag of LSE ready interrupt
  1090. * @arg RCC_INT_HSISTB: the flag of HSI ready interrupt
  1091. * @arg RCC_INT_HSESTB: the flag of HSE ready interrupt
  1092. * @arg RCC_INT_PLLSTB: the flag of PLL ready interrupt
  1093. * @arg RCC_INT_CKM: the flag of Clock Security System interrupt
  1094. * @retval The new state of RCC_INT (SET or RESET).
  1095. */
  1096. TypeState RCC_GetIntBitState(uint8_t RCC_INT)
  1097. {
  1098. /* Check the status of the RCC interrupt */
  1099. if ((RCC->GCIR & RCC_INT) != (uint32_t)RESET) {
  1100. return SET;
  1101. } else {
  1102. return RESET;
  1103. }
  1104. }
  1105. /**
  1106. * @brief Clear the RCC interrupt bits.
  1107. * @param RCC_INT: specify the interrupt bit to clear.
  1108. * For @b GD32_Connectivity_line_devices, this parameter can be any combination
  1109. * of the following values:
  1110. * @arg RCC_INT_LSISTB: LSI ready interrupt
  1111. * @arg RCC_INT_LSESTB LSE ready interrupt
  1112. * @arg RCC_INT_HSISTB: HSI ready interrupt
  1113. * @arg RCC_INT_HSESTB: HSE ready interrupt
  1114. * @arg RCC_INT_PLLSTB: PLL ready interrupt
  1115. * @arg RCC_INT_PLL2STB: PLL2 ready interrupt
  1116. * @arg RCC_INT_PLL3STB: PLL3 ready interrupt
  1117. * @arg RCC_INT_CKM: Clock Security System interrupt
  1118. * For @b other_GD32_devices, this parameter can be any combination of the
  1119. * following values
  1120. * @arg RCC_INT_LSISTB: LSI ready interrupt
  1121. * @arg RCC_INT_LSESTB: LSE ready interrupt
  1122. * @arg RCC_INT_HSISTB: HSI ready interrupt
  1123. * @arg RCC_INT_HSESTB: HSE ready interrupt
  1124. * @arg RCC_INT_PLLSTB: PLL ready interrupt
  1125. * @arg RCC_INT_CKM: Clock Security System interrupt
  1126. * @retval None
  1127. */
  1128. void RCC_ClearIntBitState(uint8_t RCC_INT)
  1129. {
  1130. /* Perform RCC_GCIR[23:16] bits to clear the selected interrupt bits */
  1131. *(__IO uint8_t *) GCIR_BYTE2_ADDRESS = RCC_INT;
  1132. }
  1133. /**
  1134. * @brief Configure the kernel voltage in Deep-sleep mode.
  1135. * @note Only unlock the power,this configuration is effective.
  1136. * @param RCC_KERNEL_VOL: specify the kernel voltage.
  1137. * This parameter can be one of the following values:
  1138. * @arg RCC_KERNEL_VOL1_2: The kernel voltage in Deep-sleep mode is 1.2V
  1139. * @arg RCC_KERNEL_VOL1_1: The kernel voltage in Deep-sleep mode is 1.1V
  1140. * @arg RCC_KERNEL_VOL1_0: The kernel voltage in Deep-sleep mode is 1.0V
  1141. * @arg RCC_KERNEL_VOL0_9: The kernel voltage in Deep-sleep mode is 0.9V
  1142. * @retval None
  1143. */
  1144. void RCC_KERNELVOLConfig(uint32_t RCC_KERNEL_VOL)
  1145. {
  1146. /* Clear DEEPSLEEP_VC bit */
  1147. RCC->RCC_DEEPSLEEP_VC &= ~RCC_DEEPSLEEP_VC_CLEAR;
  1148. /* Set DEEPSLEEP_VC bits according to RCC_KERNEL_VOL value */
  1149. RCC->RCC_DEEPSLEEP_VC |= RCC_KERNEL_VOL;
  1150. }
  1151. /**
  1152. * @}
  1153. */
  1154. /**
  1155. * @}
  1156. */
  1157. /**
  1158. * @}
  1159. */