gd32f30x_rcu.c 47 KB

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