gd32f10x_rcu.c 41 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195
  1. /*!
  2. \file gd32f10x_rcu.c
  3. \brief RCU driver
  4. \version 2014-12-26, V1.0.0, firmware for GD32F10x
  5. \version 2017-06-20, V2.0.0, firmware for GD32F10x
  6. \version 2018-07-31, V2.1.0, firmware for GD32F10x
  7. \version 2020-09-30, V2.2.0, firmware for GD32F10x
  8. */
  9. /*
  10. Copyright (c) 2020, GigaDevice Semiconductor Inc.
  11. Redistribution and use in source and binary forms, with or without modification,
  12. are permitted provided that the following conditions are met:
  13. 1. Redistributions of source code must retain the above copyright notice, this
  14. list of conditions and the following disclaimer.
  15. 2. Redistributions in binary form must reproduce the above copyright notice,
  16. this list of conditions and the following disclaimer in the documentation
  17. and/or other materials provided with the distribution.
  18. 3. Neither the name of the copyright holder nor the names of its contributors
  19. may be used to endorse or promote products derived from this software without
  20. specific prior written permission.
  21. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  22. AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  23. WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  24. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
  25. INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  26. NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  27. PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  28. WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  29. ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
  30. OF SUCH DAMAGE.
  31. */
  32. #include "gd32f10x_rcu.h"
  33. /* define clock source */
  34. #define SEL_IRC8M ((uint16_t)0U)
  35. #define SEL_HXTAL ((uint16_t)1U)
  36. #define SEL_PLL ((uint16_t)2U)
  37. /* define startup timeout count */
  38. #define OSC_STARTUP_TIMEOUT ((uint32_t)0xFFFFFU)
  39. #define LXTAL_STARTUP_TIMEOUT ((uint32_t)0x3FFFFFFU)
  40. /*!
  41. \brief deinitialize the RCU
  42. \param[in] none
  43. \param[out] none
  44. \retval none
  45. */
  46. void rcu_deinit(void)
  47. {
  48. /* enable IRC8M */
  49. RCU_CTL |= RCU_CTL_IRC8MEN;
  50. rcu_osci_stab_wait(RCU_IRC8M);
  51. RCU_CFG0 &= ~RCU_CFG0_SCS;
  52. /* reset CTL register */
  53. RCU_CTL &= ~(RCU_CTL_HXTALEN | RCU_CTL_CKMEN | RCU_CTL_PLLEN);
  54. RCU_CTL &= ~RCU_CTL_HXTALBPS;
  55. #ifdef GD32F10X_CL
  56. RCU_CTL &= ~(RCU_CTL_PLL1EN | RCU_CTL_PLL2EN);
  57. #endif /* GD32F10X_CL */
  58. /* reset CFG0 register */
  59. #if (defined(GD32F10X_MD) || defined(GD32F10X_HD) || defined(GD32F10X_XD))
  60. RCU_CFG0 &= ~(RCU_CFG0_SCS | RCU_CFG0_AHBPSC | RCU_CFG0_APB1PSC | RCU_CFG0_APB2PSC |
  61. RCU_CFG0_ADCPSC | RCU_CFG0_PLLSEL | RCU_CFG0_PREDV0 | RCU_CFG0_PLLMF |
  62. RCU_CFG0_USBDPSC | RCU_CFG0_CKOUT0SEL | RCU_CFG0_PLLMF_4 | RCU_CFG0_ADCPSC_2);
  63. #elif defined(GD32F10X_CL)
  64. RCU_CFG0 &= ~(RCU_CFG0_SCS | RCU_CFG0_AHBPSC | RCU_CFG0_APB1PSC | RCU_CFG0_APB2PSC |
  65. RCU_CFG0_ADCPSC | RCU_CFG0_PLLSEL | RCU_CFG0_PREDV0_LSB | RCU_CFG0_PLLMF |
  66. RCU_CFG0_USBFSPSC | RCU_CFG0_CKOUT0SEL | RCU_CFG0_ADCPSC_2 | RCU_CFG0_PLLMF_4);
  67. #endif /* GD32F10X_MD and GD32F10X_HD and GD32F10X_XD */
  68. /* reset INT and CFG1 register */
  69. #if (defined(GD32F10X_MD) || defined(GD32F10X_HD) || defined(GD32F10X_XD))
  70. RCU_INT = 0x009f0000U;
  71. #elif defined(GD32F10X_CL)
  72. RCU_INT = 0x00ff0000U;
  73. RCU_CFG1 &= ~(RCU_CFG1_PREDV0 | RCU_CFG1_PREDV1 | RCU_CFG1_PLL1MF | RCU_CFG1_PLL2MF |
  74. RCU_CFG1_PREDV0SEL | RCU_CFG1_I2S1SEL | RCU_CFG1_I2S2SEL);
  75. #endif /* GD32F10X_MD and GD32F10X_HD and GD32F10X_XD */
  76. }
  77. /*!
  78. \brief enable the peripherals clock
  79. \param[in] periph: RCU peripherals, refer to rcu_periph_enum
  80. only one parameter can be selected which is shown as below:
  81. \arg RCU_GPIOx (x=A,B,C,D,E,F,G): GPIO ports clock
  82. \arg RCU_AF : alternate function clock
  83. \arg RCU_CRC: CRC clock
  84. \arg RCU_DMAx (x=0,1): DMA clock
  85. \arg RCU_ENET: ENET clock(CL series available)
  86. \arg RCU_ENETTX: ENETTX clock(CL series available)
  87. \arg RCU_ENETRX: ENETRX clock(CL series available)
  88. \arg RCU_USBD: USBD clock(HD,XD series available)
  89. \arg RCU_USBFS: USBFS clock(CL series available)
  90. \arg RCU_EXMC: EXMC clock
  91. \arg RCU_TIMERx (x=0,1,2,3,4,5,6,7,8,9,10,11,12,13,TIMER8..13 are only available for XD series): TIMER clock
  92. \arg RCU_WWDGT: WWDGT clock
  93. \arg RCU_SPIx (x=0,1,2): SPI clock
  94. \arg RCU_USARTx (x=0,1,2): USART clock
  95. \arg RCU_UARTx (x=3,4): UART clock
  96. \arg RCU_I2Cx (x=0,1): I2C clock
  97. \arg RCU_CANx (x=0,1,CAN1 is only available for CL series): CAN clock
  98. \arg RCU_PMU: PMU clock
  99. \arg RCU_DAC: DAC clock
  100. \arg RCU_RTC: RTC clock
  101. \arg RCU_ADCx (x=0,1,2,ADC2 is not available for CL series): ADC clock
  102. \arg RCU_SDIO: SDIO clock(not available for CL series)
  103. \arg RCU_BKPI: BKP interface clock
  104. \param[out] none
  105. \retval none
  106. */
  107. void rcu_periph_clock_enable(rcu_periph_enum periph)
  108. {
  109. RCU_REG_VAL(periph) |= BIT(RCU_BIT_POS(periph));
  110. }
  111. /*!
  112. \brief disable the peripherals clock
  113. \param[in] periph: RCU peripherals, refer to rcu_periph_enum
  114. only one parameter can be selected which is shown as below:
  115. \arg RCU_GPIOx (x=A,B,C,D,E,F,G): GPIO ports clock
  116. \arg RCU_AF: alternate function clock
  117. \arg RCU_CRC: CRC clock
  118. \arg RCU_DMAx (x=0,1): DMA clock
  119. \arg RCU_ENET: ENET clock(CL series available)
  120. \arg RCU_ENETTX: ENETTX clock(CL series available)
  121. \arg RCU_ENETRX: ENETRX clock(CL series available)
  122. \arg RCU_USBD: USBD clock(HD,XD series available)
  123. \arg RCU_USBFS: USBFS clock(CL series available)
  124. \arg RCU_EXMC: EXMC clock
  125. \arg RCU_TIMERx (x=0,1,2,3,4,5,6,7,8,9,10,11,12,13,TIMER8..13 are only available for XD series): TIMER clock
  126. \arg RCU_WWDGT: WWDGT clock
  127. \arg RCU_SPIx (x=0,1,2): SPI clock
  128. \arg RCU_USARTx (x=0,1,2): USART clock
  129. \arg RCU_UARTx (x=3,4): UART clock
  130. \arg RCU_I2Cx (x=0,1): I2C clock
  131. \arg RCU_CANx (x=0,1,CAN1 is only available for CL series): CAN clock
  132. \arg RCU_PMU: PMU clock
  133. \arg RCU_DAC: DAC clock
  134. \arg RCU_RTC: RTC clock
  135. \arg RCU_ADCx (x=0,1,2,ADC2 is not available for CL series): ADC clock
  136. \arg RCU_SDIO: SDIO clock(not available for CL series)
  137. \arg RCU_BKPI: BKP interface clock
  138. \param[out] none
  139. \retval none
  140. */
  141. void rcu_periph_clock_disable(rcu_periph_enum periph)
  142. {
  143. RCU_REG_VAL(periph) &= ~BIT(RCU_BIT_POS(periph));
  144. }
  145. /*!
  146. \brief enable the peripherals clock when sleep mode
  147. \param[in] periph: RCU peripherals, refer to rcu_periph_sleep_enum
  148. only one parameter can be selected which is shown as below:
  149. \arg RCU_FMC_SLP: FMC clock
  150. \arg RCU_SRAM_SLP: SRAM clock
  151. \param[out] none
  152. \retval none
  153. */
  154. void rcu_periph_clock_sleep_enable(rcu_periph_sleep_enum periph)
  155. {
  156. RCU_REG_VAL(periph) |= BIT(RCU_BIT_POS(periph));
  157. }
  158. /*!
  159. \brief disable the peripherals clock when sleep mode
  160. \param[in] periph: RCU peripherals, refer to rcu_periph_sleep_enum
  161. only one parameter can be selected which is shown as below:
  162. \arg RCU_FMC_SLP: FMC clock
  163. \arg RCU_SRAM_SLP: SRAM clock
  164. \param[out] none
  165. \retval none
  166. */
  167. void rcu_periph_clock_sleep_disable(rcu_periph_sleep_enum periph)
  168. {
  169. RCU_REG_VAL(periph) &= ~BIT(RCU_BIT_POS(periph));
  170. }
  171. /*!
  172. \brief reset the peripherals
  173. \param[in] periph_reset: RCU peripherals reset, refer to rcu_periph_reset_enum
  174. only one parameter can be selected which is shown as below:
  175. \arg RCU_GPIOxRST (x=A,B,C,D,E,F,G): reset GPIO ports
  176. \arg RCU_AFRST : reset alternate function clock
  177. \arg RCU_ENETRST: reset ENET(CL series available)
  178. \arg RCU_USBDRST: reset USBD(HD,XD series available)
  179. \arg RCU_USBFSRST: reset USBFS(CL series available)
  180. \arg RCU_TIMERxRST (x=0,1,2,3,4,5,6,7,8,9,10,11,12,13,TIMER8..13 are only available for XD series): reset TIMER
  181. \arg RCU_WWDGTRST: reset WWDGT
  182. \arg RCU_SPIxRST (x=0,1,2): reset SPI
  183. \arg RCU_USARTxRST (x=0,1,2): reset USART
  184. \arg RCU_UARTxRST (x=3,4): reset UART
  185. \arg RCU_I2CxRST (x=0,1): reset I2C
  186. \arg RCU_CANxRST (x=0,1,CAN1 is only available for CL series): reset CAN
  187. \arg RCU_PMURST: reset PMU
  188. \arg RCU_DACRST: reset DAC
  189. \arg RCU_ADCxRST (x=0,1,2, ADC2 is not available for CL series): reset ADC
  190. \arg RCU_BKPIRST: reset BKPI
  191. \param[out] none
  192. \retval none
  193. */
  194. void rcu_periph_reset_enable(rcu_periph_reset_enum periph_reset)
  195. {
  196. RCU_REG_VAL(periph_reset) |= BIT(RCU_BIT_POS(periph_reset));
  197. }
  198. /*!
  199. \brief disable reset the peripheral
  200. \param[in] periph_reset: RCU peripherals reset, refer to rcu_periph_reset_enum
  201. only one parameter can be selected which is shown as below:
  202. \arg RCU_GPIOxRST (x=A,B,C,D,E,F,G): reset GPIO ports
  203. \arg RCU_AFRST : reset alternate function clock
  204. \arg RCU_ENETRST: reset ENET(CL series available)
  205. \arg RCU_USBDRST: reset USBD(HD,XD series available)
  206. \arg RCU_USBFSRST: reset USBFS(CL series available)
  207. \arg RCU_TIMERxRST (x=0,1,2,3,4,5,6,7,8,9,10,11,12,13,TIMER8..13 are only available for XD series): reset TIMER
  208. \arg RCU_WWDGTRST: reset WWDGT
  209. \arg RCU_SPIxRST (x=0,1,2): reset SPI
  210. \arg RCU_USARTxRST (x=0,1,2): reset USART
  211. \arg RCU_UARTxRST (x=3,4): reset UART
  212. \arg RCU_I2CxRST (x=0,1): reset I2C
  213. \arg RCU_CANxRST (x=0,1,CAN1 is only available for CL series): reset CAN
  214. \arg RCU_PMURST: reset PMU
  215. \arg RCU_DACRST: reset DAC
  216. \arg RCU_ADCxRST (x=0,1,2, ADC2 is not available for CL series): reset ADC
  217. \arg RCU_BKPIRST: reset BKPI
  218. \param[out] none
  219. \retval none
  220. */
  221. void rcu_periph_reset_disable(rcu_periph_reset_enum periph_reset)
  222. {
  223. RCU_REG_VAL(periph_reset) &= ~BIT(RCU_BIT_POS(periph_reset));
  224. }
  225. /*!
  226. \brief reset the BKP domain
  227. \param[in] none
  228. \param[out] none
  229. \retval none
  230. */
  231. void rcu_bkp_reset_enable(void)
  232. {
  233. RCU_BDCTL |= RCU_BDCTL_BKPRST;
  234. }
  235. /*!
  236. \brief disable the BKP domain reset
  237. \param[in] none
  238. \param[out] none
  239. \retval none
  240. */
  241. void rcu_bkp_reset_disable(void)
  242. {
  243. RCU_BDCTL &= ~RCU_BDCTL_BKPRST;
  244. }
  245. /*!
  246. \brief configure the system clock source
  247. \param[in] ck_sys: system clock source select
  248. only one parameter can be selected which is shown as below:
  249. \arg RCU_CKSYSSRC_IRC8M: select CK_IRC8M as the CK_SYS source
  250. \arg RCU_CKSYSSRC_HXTAL: select CK_HXTAL as the CK_SYS source
  251. \arg RCU_CKSYSSRC_PLL: select CK_PLL as the CK_SYS source
  252. \param[out] none
  253. \retval none
  254. */
  255. void rcu_system_clock_source_config(uint32_t ck_sys)
  256. {
  257. uint32_t reg;
  258. reg = RCU_CFG0;
  259. /* reset the SCS bits and set according to ck_sys */
  260. reg &= ~RCU_CFG0_SCS;
  261. RCU_CFG0 = (reg | ck_sys);
  262. }
  263. /*!
  264. \brief get the system clock source
  265. \param[in] none
  266. \param[out] none
  267. \retval which clock is selected as CK_SYS source
  268. \arg RCU_SCSS_IRC8M: CK_IRC8M is selected as the CK_SYS source
  269. \arg RCU_SCSS_HXTAL: CK_HXTAL is selected as the CK_SYS source
  270. \arg RCU_SCSS_PLL: CK_PLL is selected as the CK_SYS source
  271. */
  272. uint32_t rcu_system_clock_source_get(void)
  273. {
  274. return (RCU_CFG0 & RCU_CFG0_SCSS);
  275. }
  276. /*!
  277. \brief configure the AHB clock prescaler selection
  278. \param[in] ck_ahb: AHB clock prescaler selection
  279. only one parameter can be selected which is shown as below:
  280. \arg RCU_AHB_CKSYS_DIVx, x=1, 2, 4, 8, 16, 64, 128, 256, 512
  281. \param[out] none
  282. \retval none
  283. */
  284. void rcu_ahb_clock_config(uint32_t ck_ahb)
  285. {
  286. uint32_t reg;
  287. reg = RCU_CFG0;
  288. /* reset the AHBPSC bits and set according to ck_ahb */
  289. reg &= ~RCU_CFG0_AHBPSC;
  290. RCU_CFG0 = (reg | ck_ahb);
  291. }
  292. /*!
  293. \brief configure the APB1 clock prescaler selection
  294. \param[in] ck_apb1: APB1 clock prescaler selection
  295. only one parameter can be selected which is shown as below:
  296. \arg RCU_APB1_CKAHB_DIV1: select CK_AHB as CK_APB1
  297. \arg RCU_APB1_CKAHB_DIV2: select CK_AHB/2 as CK_APB1
  298. \arg RCU_APB1_CKAHB_DIV4: select CK_AHB/4 as CK_APB1
  299. \arg RCU_APB1_CKAHB_DIV8: select CK_AHB/8 as CK_APB1
  300. \arg RCU_APB1_CKAHB_DIV16: select CK_AHB/16 as CK_APB1
  301. \param[out] none
  302. \retval none
  303. */
  304. void rcu_apb1_clock_config(uint32_t ck_apb1)
  305. {
  306. uint32_t reg;
  307. reg = RCU_CFG0;
  308. /* reset the APB1PSC and set according to ck_apb1 */
  309. reg &= ~RCU_CFG0_APB1PSC;
  310. RCU_CFG0 = (reg | ck_apb1);
  311. }
  312. /*!
  313. \brief configure the APB2 clock prescaler selection
  314. \param[in] ck_apb2: APB2 clock prescaler selection
  315. only one parameter can be selected which is shown as below:
  316. \arg RCU_APB2_CKAHB_DIV1: select CK_AHB as CK_APB2
  317. \arg RCU_APB2_CKAHB_DIV2: select CK_AHB/2 as CK_APB2
  318. \arg RCU_APB2_CKAHB_DIV4: select CK_AHB/4 as CK_APB2
  319. \arg RCU_APB2_CKAHB_DIV8: select CK_AHB/8 as CK_APB2
  320. \arg RCU_APB2_CKAHB_DIV16: select CK_AHB/16 as CK_APB2
  321. \param[out] none
  322. \retval none
  323. */
  324. void rcu_apb2_clock_config(uint32_t ck_apb2)
  325. {
  326. uint32_t reg;
  327. reg = RCU_CFG0;
  328. /* reset the APB2PSC and set according to ck_apb2 */
  329. reg &= ~RCU_CFG0_APB2PSC;
  330. RCU_CFG0 = (reg | ck_apb2);
  331. }
  332. /*!
  333. \brief configure the CK_OUT0 clock source
  334. \param[in] ckout0_src: CK_OUT0 clock source selection
  335. only one parameter can be selected which is shown as below:
  336. \arg RCU_CKOUT0SRC_NONE: no clock selected
  337. \arg RCU_CKOUT0SRC_CKSYS: system clock selected
  338. \arg RCU_CKOUT0SRC_IRC8M: high speed 8M internal oscillator clock selected
  339. \arg RCU_CKOUT0SRC_HXTAL: HXTAL selected
  340. \arg RCU_CKOUT0SRC_CKPLL_DIV2: CK_PLL/2 selected
  341. \arg RCU_CKOUT0SRC_CKPLL1: CK_PLL1 selected
  342. \arg RCU_CKOUT0SRC_CKPLL2_DIV2: CK_PLL2/2 selected
  343. \arg RCU_CKOUT0SRC_EXT1: EXT1 selected
  344. \arg RCU_CKOUT0SRC_CKPLL2: PLL2 selected
  345. \param[out] none
  346. \retval none
  347. */
  348. void rcu_ckout0_config(uint32_t ckout0_src)
  349. {
  350. uint32_t reg;
  351. reg = RCU_CFG0;
  352. /* reset the CKOUT0SRC, set according to ckout0_src */
  353. reg &= ~RCU_CFG0_CKOUT0SEL;
  354. RCU_CFG0 = (reg | ckout0_src);
  355. }
  356. /*!
  357. \brief configure the main PLL clock
  358. \param[in] pll_src: PLL clock source selection
  359. only one parameter can be selected which is shown as below:
  360. \arg RCU_PLLSRC_IRC8M_DIV2: IRC8M/2 clock selected as source clock of PLL
  361. \arg RCU_PLLSRC_HXTAL: HXTAL selected as source clock of PLL
  362. \param[in] pll_mul: PLL clock multiplication factor
  363. only one parameter can be selected which is shown as below:
  364. \arg RCU_PLL_MULx (XD series x = 2..32, CL series x = 2..14, 6.5, 16..32)
  365. \param[out] none
  366. \retval none
  367. */
  368. void rcu_pll_config(uint32_t pll_src, uint32_t pll_mul)
  369. {
  370. uint32_t reg = 0U;
  371. reg = RCU_CFG0;
  372. /* PLL clock source and multiplication factor configuration */
  373. reg &= ~(RCU_CFG0_PLLSEL | RCU_CFG0_PLLMF | RCU_CFG0_PLLMF_4);
  374. reg |= (pll_src | pll_mul);
  375. RCU_CFG0 = reg;
  376. }
  377. #if (defined(GD32F10X_MD) || defined(GD32F10X_HD) || defined(GD32F10X_XD))
  378. /*!
  379. \brief configure the PREDV0 division factor
  380. \param[in] predv0_div: PREDV0 division factor
  381. only one parameter can be selected which is shown as below:
  382. \arg RCU_PREDV0_DIVx, x = 1,2
  383. \param[out] none
  384. \retval none
  385. */
  386. void rcu_predv0_config(uint32_t predv0_div)
  387. {
  388. uint32_t reg = 0U;
  389. reg = RCU_CFG0;
  390. /* reset PREDV0 bit */
  391. reg &= ~RCU_CFG0_PREDV0;
  392. if(RCU_PREDV0_DIV2 == predv0_div){
  393. /* set the PREDV0 bit */
  394. reg |= RCU_CFG0_PREDV0;
  395. }
  396. RCU_CFG0 = reg;
  397. }
  398. #elif defined(GD32F10X_CL)
  399. /*!
  400. \brief configure the PREDV0 division factor and clock source
  401. \param[in] predv0_source: PREDV0 input clock source selection
  402. only one parameter can be selected which is shown as below:
  403. \arg RCU_PREDV0SRC_HXTAL: HXTAL selected as PREDV0 input source clock
  404. \arg RCU_PREDV0SRC_CKPLL1: CK_PLL1 selected as PREDV0 input source clock
  405. \param[in] predv0_div: PREDV0 division factor
  406. only one parameter can be selected which is shown as below:
  407. \arg RCU_PREDV0_DIVx, x = 1..16
  408. \param[out] none
  409. \retval none
  410. */
  411. void rcu_predv0_config(uint32_t predv0_source, uint32_t predv0_div)
  412. {
  413. uint32_t reg = 0U;
  414. reg = RCU_CFG1;
  415. /* reset PREDV0SEL and PREDV0 bits */
  416. reg &= ~(RCU_CFG1_PREDV0SEL | RCU_CFG1_PREDV0);
  417. /* set the PREDV0SEL and PREDV0 division factor */
  418. reg |= (predv0_source | predv0_div);
  419. RCU_CFG1 = reg;
  420. }
  421. /*!
  422. \brief configure the PREDV1 division factor
  423. \param[in] predv1_div: PREDV1 division factor
  424. only one parameter can be selected which is shown as below:
  425. \arg RCU_PREDV1_DIVx, x = 1..16
  426. \param[out] none
  427. \retval none
  428. */
  429. void rcu_predv1_config(uint32_t predv1_div)
  430. {
  431. uint32_t reg = 0U;
  432. reg = RCU_CFG1;
  433. /* reset the PREDV1 bits */
  434. reg &= ~RCU_CFG1_PREDV1;
  435. /* set the PREDV1 division factor */
  436. reg |= predv1_div;
  437. RCU_CFG1 = reg;
  438. }
  439. /*!
  440. \brief configure the PLL1 clock
  441. \param[in] pll_mul: PLL clock multiplication factor
  442. only one parameter can be selected which is shown as below:
  443. \arg RCU_PLL1_MULx (x = 8..16, 20)
  444. \param[out] none
  445. \retval none
  446. */
  447. void rcu_pll1_config(uint32_t pll_mul)
  448. {
  449. RCU_CFG1 &= ~RCU_CFG1_PLL1MF;
  450. RCU_CFG1 |= pll_mul;
  451. }
  452. /*!
  453. \brief configure the PLL2 clock
  454. \param[in] pll_mul: PLL clock multiplication factor
  455. only one parameter can be selected which is shown as below:
  456. \arg RCU_PLL2_MULx (x = 8..16, 20)
  457. \param[out] none
  458. \retval none
  459. */
  460. void rcu_pll2_config(uint32_t pll_mul)
  461. {
  462. RCU_CFG1 &= ~RCU_CFG1_PLL2MF;
  463. RCU_CFG1 |= pll_mul;
  464. }
  465. #endif /* GD32F10X_MD and GD32F10X_HD and GD32F10X_XD */
  466. /*!
  467. \brief configure the ADC prescaler factor
  468. \param[in] adc_psc: ADC prescaler factor
  469. only one parameter can be selected which is shown as below:
  470. \arg RCU_CKADC_CKAPB2_DIV2: ADC prescaler select CK_APB2/2
  471. \arg RCU_CKADC_CKAPB2_DIV4: ADC prescaler select CK_APB2/4
  472. \arg RCU_CKADC_CKAPB2_DIV6: ADC prescaler select CK_APB2/6
  473. \arg RCU_CKADC_CKAPB2_DIV8: ADC prescaler select CK_APB2/8
  474. \arg RCU_CKADC_CKAPB2_DIV12: ADC prescaler select CK_APB2/12
  475. \arg RCU_CKADC_CKAPB2_DIV16: ADC prescaler select CK_APB2/16
  476. \param[out] none
  477. \retval none
  478. */
  479. void rcu_adc_clock_config(uint32_t adc_psc)
  480. {
  481. uint32_t reg0;
  482. /* reset the ADCPSC bits */
  483. reg0 = RCU_CFG0;
  484. reg0 &= ~(RCU_CFG0_ADCPSC_2 | RCU_CFG0_ADCPSC);
  485. /* set the ADC prescaler factor */
  486. switch(adc_psc){
  487. case RCU_CKADC_CKAPB2_DIV2:
  488. case RCU_CKADC_CKAPB2_DIV4:
  489. case RCU_CKADC_CKAPB2_DIV6:
  490. case RCU_CKADC_CKAPB2_DIV8:
  491. reg0 |= (adc_psc << 14);
  492. break;
  493. case RCU_CKADC_CKAPB2_DIV12:
  494. case RCU_CKADC_CKAPB2_DIV16:
  495. adc_psc &= ~BIT(2);
  496. reg0 |= (adc_psc << 14 | RCU_CFG0_ADCPSC_2);
  497. break;
  498. default:
  499. break;
  500. }
  501. /* set the register */
  502. RCU_CFG0 = reg0;
  503. }
  504. /*!
  505. \brief configure the USBD/USBFS prescaler factor
  506. \param[in] usb_psc: USB prescaler factor
  507. only one parameter can be selected which is shown as below:
  508. \arg RCU_CKUSB_CKPLL_DIV1_5: USBD/USBFS prescaler select CK_PLL/1.5
  509. \arg RCU_CKUSB_CKPLL_DIV1: USBD/USBFS prescaler select CK_PLL/1
  510. \arg RCU_CKUSB_CKPLL_DIV2_5: USBD/USBFS prescaler select CK_PLL/2.5
  511. \arg RCU_CKUSB_CKPLL_DIV2: USBD/USBFS prescaler select CK_PLL/2
  512. \param[out] none
  513. \retval none
  514. */
  515. void rcu_usb_clock_config(uint32_t usb_psc)
  516. {
  517. uint32_t reg;
  518. reg = RCU_CFG0;
  519. /* configure the USBD/USBFS prescaler factor */
  520. #if (defined(GD32F10X_MD) || defined(GD32F10X_HD) || defined(GD32F10X_XD))
  521. reg &= ~RCU_CFG0_USBDPSC;
  522. #elif defined(GD32F10X_CL)
  523. reg &= ~RCU_CFG0_USBFSPSC;
  524. #endif /* GD32F10X_MD and GD32F10X_HD and GD32F10X_XD */
  525. RCU_CFG0 = (reg | usb_psc);
  526. }
  527. /*!
  528. \brief configure the RTC clock source selection
  529. \param[in] rtc_clock_source: RTC clock source selection
  530. only one parameter can be selected which is shown as below:
  531. \arg RCU_RTCSRC_NONE: no clock selected
  532. \arg RCU_RTCSRC_LXTAL: CK_LXTAL selected as RTC source clock
  533. \arg RCU_RTCSRC_IRC40K: CK_IRC40K selected as RTC source clock
  534. \arg RCU_RTCSRC_HXTAL_DIV_128: CK_HXTAL/128 selected as RTC source clock
  535. \param[out] none
  536. \retval none
  537. */
  538. void rcu_rtc_clock_config(uint32_t rtc_clock_source)
  539. {
  540. uint32_t reg;
  541. reg = RCU_BDCTL;
  542. /* reset the RTCSRC bits and set according to rtc_clock_source */
  543. reg &= ~RCU_BDCTL_RTCSRC;
  544. RCU_BDCTL = (reg | rtc_clock_source);
  545. }
  546. #ifdef GD32F10X_CL
  547. /*!
  548. \brief configure the I2S1 clock source selection
  549. \param[in] i2s_clock_source: I2S1 clock source selection
  550. only one parameter can be selected which is shown as below:
  551. \arg RCU_I2S1SRC_CKSYS: System clock selected as I2S1 source clock
  552. \arg RCU_I2S1SRC_CKPLL2_MUL2: CK_PLL2x2 selected as I2S1 source clock
  553. \param[out] none
  554. \retval none
  555. */
  556. void rcu_i2s1_clock_config(uint32_t i2s_clock_source)
  557. {
  558. uint32_t reg;
  559. reg = RCU_CFG1;
  560. /* reset the I2S1SEL bit and set according to i2s_clock_source */
  561. reg &= ~RCU_CFG1_I2S1SEL;
  562. RCU_CFG1 = (reg | i2s_clock_source);
  563. }
  564. /*!
  565. \brief configure the I2S2 clock source selection
  566. \param[in] i2s_clock_source: I2S2 clock source selection
  567. only one parameter can be selected which is shown as below:
  568. \arg RCU_I2S2SRC_CKSYS: system clock selected as I2S2 source clock
  569. \arg RCU_I2S2SRC_CKPLL2_MUL2: CK_PLL2x2 selected as I2S2 source clock
  570. \param[out] none
  571. \retval none
  572. */
  573. void rcu_i2s2_clock_config(uint32_t i2s_clock_source)
  574. {
  575. uint32_t reg;
  576. reg = RCU_CFG1;
  577. /* reset the I2S2SEL bit and set according to i2s_clock_source */
  578. reg &= ~RCU_CFG1_I2S2SEL;
  579. RCU_CFG1 = (reg | i2s_clock_source);
  580. }
  581. #endif /* GD32F10X_CL */
  582. /*!
  583. \brief get the clock stabilization and periphral reset flags
  584. \param[in] flag: the clock stabilization and periphral reset flags, refer to rcu_flag_enum
  585. only one parameter can be selected which is shown as below:
  586. \arg RCU_FLAG_IRC8MSTB: IRC8M stabilization flag
  587. \arg RCU_FLAG_HXTALSTB: HXTAL stabilization flag
  588. \arg RCU_FLAG_PLLSTB: PLL stabilization flag
  589. \arg RCU_FLAG_PLL1STB: PLL1 stabilization flag(CL series only)
  590. \arg RCU_FLAG_PLL2STB: PLL2 stabilization flag(CL series only)
  591. \arg RCU_FLAG_LXTALSTB: LXTAL stabilization flag
  592. \arg RCU_FLAG_IRC40KSTB: IRC40K stabilization flag
  593. \arg RCU_FLAG_EPRST: external PIN reset flag
  594. \arg RCU_FLAG_PORRST: power reset flag
  595. \arg RCU_FLAG_SWRST: software reset flag
  596. \arg RCU_FLAG_FWDGTRST: free watchdog timer reset flag
  597. \arg RCU_FLAG_WWDGTRST: window watchdog timer reset flag
  598. \arg RCU_FLAG_LPRST: low-power reset flag
  599. \param[out] none
  600. \retval FlagStatus: SET or RESET
  601. */
  602. FlagStatus rcu_flag_get(rcu_flag_enum flag)
  603. {
  604. /* get the rcu flag */
  605. if(RESET != (RCU_REG_VAL(flag) & BIT(RCU_BIT_POS(flag)))){
  606. return SET;
  607. }else{
  608. return RESET;
  609. }
  610. }
  611. /*!
  612. \brief clear all the reset flag
  613. \param[in] none
  614. \param[out] none
  615. \retval none
  616. */
  617. void rcu_all_reset_flag_clear(void)
  618. {
  619. RCU_RSTSCK |= RCU_RSTSCK_RSTFC;
  620. }
  621. /*!
  622. \brief get the clock stabilization interrupt and ckm flags
  623. \param[in] int_flag: interrupt and ckm flags, refer to rcu_int_flag_enum
  624. only one parameter can be selected which is shown as below:
  625. \arg RCU_INT_FLAG_IRC40KSTB: IRC40K stabilization interrupt flag
  626. \arg RCU_INT_FLAG_LXTALSTB: LXTAL stabilization interrupt flag
  627. \arg RCU_INT_FLAG_IRC8MSTB: IRC8M stabilization interrupt flag
  628. \arg RCU_INT_FLAG_HXTALSTB: HXTAL stabilization interrupt flag
  629. \arg RCU_INT_FLAG_PLLSTB: PLL stabilization interrupt flag
  630. \arg RCU_INT_FLAG_PLL1STB: PLL1 stabilization interrupt flag(CL series only)
  631. \arg RCU_INT_FLAG_PLL2STB: PLL2 stabilization interrupt flag(CL series only)
  632. \arg RCU_INT_FLAG_CKM: HXTAL clock stuck interrupt flag
  633. \param[out] none
  634. \retval FlagStatus: SET or RESET
  635. */
  636. FlagStatus rcu_interrupt_flag_get(rcu_int_flag_enum int_flag)
  637. {
  638. /* get the rcu interrupt flag */
  639. if(RESET != (RCU_REG_VAL(int_flag) & BIT(RCU_BIT_POS(int_flag)))){
  640. return SET;
  641. }else{
  642. return RESET;
  643. }
  644. }
  645. /*!
  646. \brief clear the interrupt flags
  647. \param[in] int_flag_clear: clock stabilization and stuck interrupt flags clear, refer to rcu_int_flag_clear_enum
  648. only one parameter can be selected which is shown as below:
  649. \arg RCU_INT_FLAG_IRC40KSTB_CLR: IRC40K stabilization interrupt flag clear
  650. \arg RCU_INT_FLAG_LXTALSTB_CLR: LXTAL stabilization interrupt flag clear
  651. \arg RCU_INT_FLAG_IRC8MSTB_CLR: IRC8M stabilization interrupt flag clear
  652. \arg RCU_INT_FLAG_HXTALSTB_CLR: HXTAL stabilization interrupt flag clear
  653. \arg RCU_INT_FLAG_PLLSTB_CLR: PLL stabilization interrupt flag clear
  654. \arg RCU_INT_FLAG_PLL1STB_CLR: PLL1 stabilization interrupt flag clear(CL series only)
  655. \arg RCU_INT_FLAG_PLL2STB_CLR: PLL2 stabilization interrupt flag clear(CL series only)
  656. \arg RCU_INT_FLAG_CKM_CLR: clock stuck interrupt flag clear
  657. \param[out] none
  658. \retval none
  659. */
  660. void rcu_interrupt_flag_clear(rcu_int_flag_clear_enum int_flag_clear)
  661. {
  662. RCU_REG_VAL(int_flag_clear) |= BIT(RCU_BIT_POS(int_flag_clear));
  663. }
  664. /*!
  665. \brief enable the stabilization interrupt
  666. \param[in] stab_int: clock stabilization interrupt, refer to rcu_int_enum
  667. Only one parameter can be selected which is shown as below:
  668. \arg RCU_INT_IRC40KSTB: IRC40K stabilization interrupt enable
  669. \arg RCU_INT_LXTALSTB: LXTAL stabilization interrupt enable
  670. \arg RCU_INT_IRC8MSTB: IRC8M stabilization interrupt enable
  671. \arg RCU_INT_HXTALSTB: HXTAL stabilization interrupt enable
  672. \arg RCU_INT_PLLSTB: PLL stabilization interrupt enable
  673. \arg RCU_INT_PLL1STB: PLL1 stabilization interrupt enable(CL series only)
  674. \arg RCU_INT_PLL2STB: PLL2 stabilization interrupt enable(CL series only)
  675. \param[out] none
  676. \retval none
  677. */
  678. void rcu_interrupt_enable(rcu_int_enum stab_int)
  679. {
  680. RCU_REG_VAL(stab_int) |= BIT(RCU_BIT_POS(stab_int));
  681. }
  682. /*!
  683. \brief disable the stabilization interrupt
  684. \param[in] stab_int: clock stabilization interrupt, refer to rcu_int_enum
  685. only one parameter can be selected which is shown as below:
  686. \arg RCU_INT_IRC40KSTB: IRC40K stabilization interrupt enable
  687. \arg RCU_INT_LXTALSTB: LXTAL stabilization interrupt enable
  688. \arg RCU_INT_IRC8MSTB: IRC8M stabilization interrupt enable
  689. \arg RCU_INT_HXTALSTB: HXTAL stabilization interrupt enable
  690. \arg RCU_INT_PLLSTB: PLL stabilization interrupt enable
  691. \arg RCU_INT_PLL1STB: PLL1 stabilization interrupt enable(CL series only)
  692. \arg RCU_INT_PLL2STB: PLL2 stabilization interrupt enable(CL series only)
  693. \param[out] none
  694. \retval none
  695. */
  696. void rcu_interrupt_disable(rcu_int_enum stab_int)
  697. {
  698. RCU_REG_VAL(stab_int) &= ~BIT(RCU_BIT_POS(stab_int));
  699. }
  700. /*!
  701. \brief wait for oscillator stabilization flags is SET or oscillator startup is timeout
  702. \param[in] osci: oscillator types, refer to rcu_osci_type_enum
  703. only one parameter can be selected which is shown as below:
  704. \arg RCU_HXTAL: high speed crystal oscillator(HXTAL)
  705. \arg RCU_LXTAL: low speed crystal oscillator(LXTAL)
  706. \arg RCU_IRC8M: internal 8M RC oscillators(IRC8M)
  707. \arg RCU_IRC40K: internal 40K RC oscillator(IRC40K)
  708. \arg RCU_PLL_CK: phase locked loop(PLL)
  709. \arg RCU_PLL1_CK: phase locked loop 1(CL series only)
  710. \arg RCU_PLL2_CK: phase locked loop 2(CL series only)
  711. \param[out] none
  712. \retval ErrStatus: SUCCESS or ERROR
  713. */
  714. ErrStatus rcu_osci_stab_wait(rcu_osci_type_enum osci)
  715. {
  716. uint32_t stb_cnt = 0U;
  717. ErrStatus reval = ERROR;
  718. FlagStatus osci_stat = RESET;
  719. switch(osci){
  720. /* wait HXTAL stable */
  721. case RCU_HXTAL:
  722. while((RESET == osci_stat) && (HXTAL_STARTUP_TIMEOUT != stb_cnt)){
  723. osci_stat = rcu_flag_get(RCU_FLAG_HXTALSTB);
  724. stb_cnt++;
  725. }
  726. /* check whether flag is set or not */
  727. if(RESET != rcu_flag_get(RCU_FLAG_HXTALSTB)){
  728. reval = SUCCESS;
  729. }
  730. break;
  731. /* wait LXTAL stable */
  732. case RCU_LXTAL:
  733. while((RESET == osci_stat) && (LXTAL_STARTUP_TIMEOUT != stb_cnt)){
  734. osci_stat = rcu_flag_get(RCU_FLAG_LXTALSTB);
  735. stb_cnt++;
  736. }
  737. /* check whether flag is set or not */
  738. if(RESET != rcu_flag_get(RCU_FLAG_LXTALSTB)){
  739. reval = SUCCESS;
  740. }
  741. break;
  742. /* wait IRC8M stable */
  743. case RCU_IRC8M:
  744. while((RESET == osci_stat) && (IRC8M_STARTUP_TIMEOUT != stb_cnt)){
  745. osci_stat = rcu_flag_get(RCU_FLAG_IRC8MSTB);
  746. stb_cnt++;
  747. }
  748. /* check whether flag is set or not */
  749. if(RESET != rcu_flag_get(RCU_FLAG_IRC8MSTB)){
  750. reval = SUCCESS;
  751. }
  752. break;
  753. /* wait IRC40K stable */
  754. case RCU_IRC40K:
  755. while((RESET == osci_stat) && (OSC_STARTUP_TIMEOUT != stb_cnt)){
  756. osci_stat = rcu_flag_get(RCU_FLAG_IRC40KSTB);
  757. stb_cnt++;
  758. }
  759. /* check whether flag is set or not */
  760. if(RESET != rcu_flag_get(RCU_FLAG_IRC40KSTB)){
  761. reval = SUCCESS;
  762. }
  763. break;
  764. /* wait PLL stable */
  765. case RCU_PLL_CK:
  766. while((RESET == osci_stat) && (OSC_STARTUP_TIMEOUT != stb_cnt)){
  767. osci_stat = rcu_flag_get(RCU_FLAG_PLLSTB);
  768. stb_cnt++;
  769. }
  770. /* check whether flag is set or not */
  771. if(RESET != rcu_flag_get(RCU_FLAG_PLLSTB)){
  772. reval = SUCCESS;
  773. }
  774. break;
  775. #ifdef GD32F10X_CL
  776. /* wait PLL1 stable */
  777. case RCU_PLL1_CK:
  778. while((RESET == osci_stat) && (OSC_STARTUP_TIMEOUT != stb_cnt)){
  779. osci_stat = rcu_flag_get(RCU_FLAG_PLL1STB);
  780. stb_cnt++;
  781. }
  782. /* check whether flag is set or not */
  783. if(RESET != rcu_flag_get(RCU_FLAG_PLL1STB)){
  784. reval = SUCCESS;
  785. }
  786. break;
  787. /* wait PLL2 stable */
  788. case RCU_PLL2_CK:
  789. while((RESET == osci_stat) && (OSC_STARTUP_TIMEOUT != stb_cnt)){
  790. osci_stat = rcu_flag_get(RCU_FLAG_PLL2STB);
  791. stb_cnt++;
  792. }
  793. /* check whether flag is set or not */
  794. if(RESET != rcu_flag_get(RCU_FLAG_PLL2STB)){
  795. reval = SUCCESS;
  796. }
  797. break;
  798. #endif /* GD32F10X_CL */
  799. default:
  800. break;
  801. }
  802. /* return value */
  803. return reval;
  804. }
  805. /*!
  806. \brief turn on the oscillator
  807. \param[in] osci: oscillator types, refer to rcu_osci_type_enum
  808. only one parameter can be selected which is shown as below:
  809. \arg RCU_HXTAL: high speed crystal oscillator(HXTAL)
  810. \arg RCU_LXTAL: low speed crystal oscillator(LXTAL)
  811. \arg RCU_IRC8M: internal 8M RC oscillators(IRC8M)
  812. \arg RCU_IRC40K: internal 40K RC oscillator(IRC40K)
  813. \arg RCU_PLL_CK: phase locked loop(PLL)
  814. \arg RCU_PLL1_CK: phase locked loop 1(CL series only)
  815. \arg RCU_PLL2_CK: phase locked loop 2(CL series only)
  816. \param[out] none
  817. \retval none
  818. */
  819. void rcu_osci_on(rcu_osci_type_enum osci)
  820. {
  821. RCU_REG_VAL(osci) |= BIT(RCU_BIT_POS(osci));
  822. }
  823. /*!
  824. \brief turn off the oscillator
  825. \param[in] osci: oscillator types, refer to rcu_osci_type_enum
  826. only one parameter can be selected which is shown as below:
  827. \arg RCU_HXTAL: high speed crystal oscillator(HXTAL)
  828. \arg RCU_LXTAL: low speed crystal oscillator(LXTAL)
  829. \arg RCU_IRC8M: internal 8M RC oscillators(IRC8M)
  830. \arg RCU_IRC40K: internal 40K RC oscillator(IRC40K)
  831. \arg RCU_PLL_CK: phase locked loop(PLL)
  832. \arg RCU_PLL1_CK: phase locked loop 1(CL series only)
  833. \arg RCU_PLL2_CK: phase locked loop 2(CL series only)
  834. \param[out] none
  835. \retval none
  836. */
  837. void rcu_osci_off(rcu_osci_type_enum osci)
  838. {
  839. RCU_REG_VAL(osci) &= ~BIT(RCU_BIT_POS(osci));
  840. }
  841. /*!
  842. \brief enable the oscillator bypass mode, HXTALEN or LXTALEN must be reset before it
  843. \param[in] osci: oscillator types, refer to rcu_osci_type_enum
  844. only one parameter can be selected which is shown as below:
  845. \arg RCU_HXTAL: high speed crystal oscillator(HXTAL)
  846. \arg RCU_LXTAL: low speed crystal oscillator(LXTAL)
  847. \param[out] none
  848. \retval none
  849. */
  850. void rcu_osci_bypass_mode_enable(rcu_osci_type_enum osci)
  851. {
  852. uint32_t reg;
  853. switch(osci){
  854. /* enable HXTAL to bypass mode */
  855. case RCU_HXTAL:
  856. reg = RCU_CTL;
  857. RCU_CTL &= ~RCU_CTL_HXTALEN;
  858. RCU_CTL = (reg | RCU_CTL_HXTALBPS);
  859. break;
  860. /* enable LXTAL to bypass mode */
  861. case RCU_LXTAL:
  862. reg = RCU_BDCTL;
  863. RCU_BDCTL &= ~RCU_BDCTL_LXTALEN;
  864. RCU_BDCTL = (reg | RCU_BDCTL_LXTALBPS);
  865. break;
  866. case RCU_IRC8M:
  867. case RCU_IRC40K:
  868. case RCU_PLL_CK:
  869. #ifdef GD32F10X_CL
  870. case RCU_PLL1_CK:
  871. case RCU_PLL2_CK:
  872. #endif /* GD32F10X_CL */
  873. break;
  874. default:
  875. break;
  876. }
  877. }
  878. /*!
  879. \brief disable the oscillator bypass mode, HXTALEN or LXTALEN must be reset before it
  880. \param[in] osci: oscillator types, refer to rcu_osci_type_enum
  881. only one parameter can be selected which is shown as below:
  882. \arg RCU_HXTAL: high speed crystal oscillator(HXTAL)
  883. \arg RCU_LXTAL: low speed crystal oscillator(LXTAL)
  884. \param[out] none
  885. \retval none
  886. */
  887. void rcu_osci_bypass_mode_disable(rcu_osci_type_enum osci)
  888. {
  889. uint32_t reg;
  890. switch(osci){
  891. /* disable HXTAL to bypass mode */
  892. case RCU_HXTAL:
  893. reg = RCU_CTL;
  894. RCU_CTL &= ~RCU_CTL_HXTALEN;
  895. RCU_CTL = (reg & ~RCU_CTL_HXTALBPS);
  896. break;
  897. /* disable LXTAL to bypass mode */
  898. case RCU_LXTAL:
  899. reg = RCU_BDCTL;
  900. RCU_BDCTL &= ~RCU_BDCTL_LXTALEN;
  901. RCU_BDCTL = (reg & ~RCU_BDCTL_LXTALBPS);
  902. break;
  903. case RCU_IRC8M:
  904. case RCU_IRC40K:
  905. case RCU_PLL_CK:
  906. #ifdef GD32F10X_CL
  907. case RCU_PLL1_CK:
  908. case RCU_PLL2_CK:
  909. #endif /* GD32F10X_CL */
  910. break;
  911. default:
  912. break;
  913. }
  914. }
  915. /*!
  916. \brief enable the HXTAL clock monitor
  917. \param[in] none
  918. \param[out] none
  919. \retval none
  920. */
  921. void rcu_hxtal_clock_monitor_enable(void)
  922. {
  923. RCU_CTL |= RCU_CTL_CKMEN;
  924. }
  925. /*!
  926. \brief disable the HXTAL clock monitor
  927. \param[in] none
  928. \param[out] none
  929. \retval none
  930. */
  931. void rcu_hxtal_clock_monitor_disable(void)
  932. {
  933. RCU_CTL &= ~RCU_CTL_CKMEN;
  934. }
  935. /*!
  936. \brief set the IRC8M adjust value
  937. \param[in] irc8m_adjval: IRC8M adjust value, must be between 0 and 0x1F
  938. \param[out] none
  939. \retval none
  940. */
  941. void rcu_irc8m_adjust_value_set(uint8_t irc8m_adjval)
  942. {
  943. uint32_t reg;
  944. reg = RCU_CTL;
  945. /* reset the IRC8MADJ bits and set according to irc8m_adjval */
  946. reg &= ~RCU_CTL_IRC8MADJ;
  947. RCU_CTL = (reg | ((irc8m_adjval & 0x1FU) << 3));
  948. }
  949. /*!
  950. \brief deep-sleep mode voltage select
  951. \param[in] dsvol: deep sleep mode voltage
  952. only one parameter can be selected which is shown as below:
  953. \arg RCU_DEEPSLEEP_V_1_2: the core voltage is 1.2V
  954. \arg RCU_DEEPSLEEP_V_1_1: the core voltage is 1.1V
  955. \arg RCU_DEEPSLEEP_V_1_0: the core voltage is 1.0V
  956. \arg RCU_DEEPSLEEP_V_0_9: the core voltage is 0.9V
  957. \param[out] none
  958. \retval none
  959. */
  960. void rcu_deepsleep_voltage_set(uint32_t dsvol)
  961. {
  962. dsvol &= RCU_DSV_DSLPVS;
  963. RCU_DSV = dsvol;
  964. }
  965. /*!
  966. \brief get the system clock, bus and peripheral clock frequency
  967. \param[in] clock: the clock frequency which to get
  968. only one parameter can be selected which is shown as below:
  969. \arg CK_SYS: system clock frequency
  970. \arg CK_AHB: AHB clock frequency
  971. \arg CK_APB1: APB1 clock frequency
  972. \arg CK_APB2: APB2 clock frequency
  973. \param[out] none
  974. \retval clock frequency of system, AHB, APB1, APB2
  975. */
  976. uint32_t rcu_clock_freq_get(rcu_clock_freq_enum clock)
  977. {
  978. uint32_t sws, ck_freq = 0U;
  979. uint32_t cksys_freq, ahb_freq, apb1_freq, apb2_freq;
  980. uint32_t pllsel, predv0sel, pllmf,ck_src, idx, clk_exp;
  981. #ifdef GD32F10X_CL
  982. uint32_t predv0, predv1, pll1mf;
  983. #endif /* GD32F10X_CL */
  984. /* exponent of AHB, APB1 and APB2 clock divider */
  985. uint8_t ahb_exp[16] = {0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 4, 6, 7, 8, 9};
  986. uint8_t apb1_exp[8] = {0, 0, 0, 0, 1, 2, 3, 4};
  987. uint8_t apb2_exp[8] = {0, 0, 0, 0, 1, 2, 3, 4};
  988. sws = GET_BITS(RCU_CFG0, 2, 3);
  989. switch(sws){
  990. /* IRC8M is selected as CK_SYS */
  991. case SEL_IRC8M:
  992. cksys_freq = IRC8M_VALUE;
  993. break;
  994. /* HXTAL is selected as CK_SYS */
  995. case SEL_HXTAL:
  996. cksys_freq = HXTAL_VALUE;
  997. break;
  998. /* PLL is selected as CK_SYS */
  999. case SEL_PLL:
  1000. /* PLL clock source selection, HXTAL or IRC8M/2 */
  1001. pllsel = (RCU_CFG0 & RCU_CFG0_PLLSEL);
  1002. if(RCU_PLLSRC_HXTAL == pllsel) {
  1003. /* PLL clock source is HXTAL */
  1004. ck_src = HXTAL_VALUE;
  1005. #if (defined(GD32F10X_MD) || defined(GD32F10X_HD) || defined(GD32F10X_XD))
  1006. predv0sel = (RCU_CFG0 & RCU_CFG0_PREDV0);
  1007. /* PREDV0 input source clock divided by 2 */
  1008. if(RCU_CFG0_PREDV0 == predv0sel){
  1009. ck_src = HXTAL_VALUE/2U;
  1010. }
  1011. #elif defined(GD32F10X_CL)
  1012. predv0sel = (RCU_CFG1 & RCU_CFG1_PREDV0SEL);
  1013. /* source clock use PLL1 */
  1014. if(RCU_PREDV0SRC_CKPLL1 == predv0sel){
  1015. predv1 = (uint32_t)((RCU_CFG1 & RCU_CFG1_PREDV1) >> 4) + 1U;
  1016. pll1mf = (uint32_t)((RCU_CFG1 & RCU_CFG1_PLL1MF) >> 8) + 2U;
  1017. if(17U == pll1mf){
  1018. pll1mf = 20U;
  1019. }
  1020. ck_src = (ck_src / predv1) * pll1mf;
  1021. }
  1022. predv0 = (RCU_CFG1 & RCU_CFG1_PREDV0) + 1U;
  1023. ck_src /= predv0;
  1024. #endif /* GD32F10X_HD and GD32F10X_XD */
  1025. }else{
  1026. /* PLL clock source is IRC8M/2 */
  1027. ck_src = IRC8M_VALUE/2U;
  1028. }
  1029. /* PLL multiplication factor */
  1030. pllmf = GET_BITS(RCU_CFG0, 18, 21);
  1031. if((RCU_CFG0 & RCU_CFG0_PLLMF_4)){
  1032. pllmf |= 0x10U;
  1033. }
  1034. if(pllmf < 15U){
  1035. pllmf += 2U;
  1036. }else{
  1037. pllmf += 1U;
  1038. }
  1039. cksys_freq = ck_src * pllmf;
  1040. #ifdef GD32F10X_CL
  1041. if(15U == pllmf){
  1042. /* PLL source clock multiply by 6.5 */
  1043. cksys_freq = ck_src * 6U + ck_src / 2U;
  1044. }
  1045. #endif /* GD32F10X_CL */
  1046. break;
  1047. /* IRC8M is selected as CK_SYS */
  1048. default:
  1049. cksys_freq = IRC8M_VALUE;
  1050. break;
  1051. }
  1052. /* calculate AHB clock frequency */
  1053. idx = GET_BITS(RCU_CFG0, 4, 7);
  1054. clk_exp = ahb_exp[idx];
  1055. ahb_freq = cksys_freq >> clk_exp;
  1056. /* calculate APB1 clock frequency */
  1057. idx = GET_BITS(RCU_CFG0, 8, 10);
  1058. clk_exp = apb1_exp[idx];
  1059. apb1_freq = ahb_freq >> clk_exp;
  1060. /* calculate APB2 clock frequency */
  1061. idx = GET_BITS(RCU_CFG0, 11, 13);
  1062. clk_exp = apb2_exp[idx];
  1063. apb2_freq = ahb_freq >> clk_exp;
  1064. /* return the clocks frequency */
  1065. switch(clock){
  1066. case CK_SYS:
  1067. ck_freq = cksys_freq;
  1068. break;
  1069. case CK_AHB:
  1070. ck_freq = ahb_freq;
  1071. break;
  1072. case CK_APB1:
  1073. ck_freq = apb1_freq;
  1074. break;
  1075. case CK_APB2:
  1076. ck_freq = apb2_freq;
  1077. break;
  1078. default:
  1079. break;
  1080. }
  1081. return ck_freq;
  1082. }