gd32e23x_rcu.c 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059
  1. /*!
  2. \file gd32e23x_rcu.c
  3. \brief RCU driver
  4. \version 2019-02-19, V1.0.0, firmware for GD32E23x
  5. \version 2020-12-12, V1.1.0, firmware for GD32E23x
  6. */
  7. /*
  8. Copyright (c) 2020, GigaDevice Semiconductor Inc.
  9. Redistribution and use in source and binary forms, with or without modification,
  10. are permitted provided that the following conditions are met:
  11. 1. Redistributions of source code must retain the above copyright notice, this
  12. list of conditions and the following disclaimer.
  13. 2. Redistributions in binary form must reproduce the above copyright notice,
  14. this list of conditions and the following disclaimer in the documentation
  15. and/or other materials provided with the distribution.
  16. 3. Neither the name of the copyright holder nor the names of its contributors
  17. may be used to endorse or promote products derived from this software without
  18. specific prior written permission.
  19. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  20. AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  21. WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  22. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
  23. INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  24. NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  25. PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  26. WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  27. ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
  28. OF SUCH DAMAGE.
  29. */
  30. #include "gd32e23x_rcu.h"
  31. /* define clock source */
  32. #define SEL_IRC8M 0x00U
  33. #define SEL_HXTAL 0x01U
  34. #define SEL_PLL 0x02U
  35. /* define startup timeout count */
  36. #define OSC_STARTUP_TIMEOUT ((uint32_t)0x000FFFFFU)
  37. #define LXTAL_STARTUP_TIMEOUT ((uint32_t)0x03FFFFFFU)
  38. /*!
  39. \brief deinitialize the RCU
  40. \param[in] none
  41. \param[out] none
  42. \retval none
  43. */
  44. void rcu_deinit(void)
  45. {
  46. /* enable IRC8M */
  47. RCU_CTL0 |= RCU_CTL0_IRC8MEN;
  48. while(0U == (RCU_CTL0 & RCU_CTL0_IRC8MSTB)){
  49. }
  50. RCU_CFG0 &= ~RCU_CFG0_SCS;
  51. RCU_CTL0 &= ~(RCU_CTL0_HXTALEN | RCU_CTL0_CKMEN | RCU_CTL0_PLLEN | RCU_CTL0_HXTALBPS);
  52. /* reset RCU */
  53. RCU_CFG0 &= ~(RCU_CFG0_SCS | RCU_CFG0_AHBPSC | RCU_CFG0_APB1PSC | RCU_CFG0_APB2PSC |\
  54. RCU_CFG0_ADCPSC | RCU_CFG0_CKOUTSEL | RCU_CFG0_CKOUTDIV | RCU_CFG0_PLLDV);
  55. RCU_CFG0 &= ~(RCU_CFG0_PLLSEL | RCU_CFG0_PLLMF | RCU_CFG0_PLLMF4 | RCU_CFG0_PLLDV);
  56. RCU_CFG1 &= ~(RCU_CFG1_PREDV);
  57. RCU_CFG2 &= ~(RCU_CFG2_USART0SEL | RCU_CFG2_ADCSEL);
  58. RCU_CFG2 &= ~RCU_CFG2_IRC28MDIV;
  59. RCU_CFG2 &= ~RCU_CFG2_ADCPSC2;
  60. RCU_CTL1 &= ~RCU_CTL1_IRC28MEN;
  61. RCU_INT = 0x00000000U;
  62. }
  63. /*!
  64. \brief enable the peripherals clock
  65. \param[in] periph: RCU peripherals, refer to rcu_periph_enum
  66. only one parameter can be selected which is shown as below:
  67. \arg RCU_GPIOx (x=A,B,C,F): GPIO ports clock
  68. \arg RCU_DMA: DMA clock
  69. \arg RCU_CRC: CRC clock
  70. \arg RCU_CFGCMP: CFGCMP clock
  71. \arg RCU_ADC: ADC clock
  72. \arg RCU_TIMERx (x=0,2,5,13,14,15,16): TIMER clock
  73. \arg RCU_SPIx (x=0,1): SPI clock
  74. \arg RCU_USARTx (x=0,1): USART clock
  75. \arg RCU_WWDGT: WWDGT clock
  76. \arg RCU_I2Cx (x=0,1): I2C clock
  77. \arg RCU_PMU: PMU clock
  78. \arg RCU_RTC: RTC clock
  79. \arg RCU_DBGMCU: DBGMCU clock
  80. \param[out] none
  81. \retval none
  82. */
  83. void rcu_periph_clock_enable(rcu_periph_enum periph)
  84. {
  85. RCU_REG_VAL(periph) |= BIT(RCU_BIT_POS(periph));
  86. }
  87. /*!
  88. \brief disable the peripherals clock
  89. \param[in] periph: RCU peripherals, refer to rcu_periph_enum
  90. only one parameter can be selected which is shown as below:
  91. \arg RCU_GPIOx (x=A,B,C,F): GPIO ports clock
  92. \arg RCU_DMA: DMA clock
  93. \arg RCU_CRC: CRC clock
  94. \arg RCU_CFGCMP: CFGCMP clock
  95. \arg RCU_ADC: ADC clock
  96. \arg RCU_TIMERx (x=0,2,5,13,14,15,16): TIMER clock
  97. \arg RCU_SPIx (x=0,1): SPI clock
  98. \arg RCU_USARTx (x=0,1): USART clock
  99. \arg RCU_WWDGT: WWDGT clock
  100. \arg RCU_I2Cx (x=0,1): I2C clock
  101. \arg RCU_PMU: PMU clock
  102. \arg RCU_RTC: RTC clock
  103. \arg RCU_DBGMCU: DBGMCU clock
  104. \param[out] none
  105. \retval none
  106. */
  107. void rcu_periph_clock_disable(rcu_periph_enum periph)
  108. {
  109. RCU_REG_VAL(periph) &= ~BIT(RCU_BIT_POS(periph));
  110. }
  111. /*!
  112. \brief enable the peripherals clock when sleep mode
  113. \param[in] periph: RCU peripherals, refer to rcu_periph_sleep_enum
  114. only one parameter can be selected which is shown as below:
  115. \arg RCU_FMC_SLP: FMC clock
  116. \arg RCU_SRAM_SLP: SRAM clock
  117. \param[out] none
  118. \retval none
  119. */
  120. void rcu_periph_clock_sleep_enable(rcu_periph_sleep_enum periph)
  121. {
  122. RCU_REG_VAL(periph) |= BIT(RCU_BIT_POS(periph));
  123. }
  124. /*!
  125. \brief disable the peripherals clock when sleep mode
  126. \param[in] periph: RCU peripherals, refer to rcu_periph_sleep_enum
  127. only one parameter can be selected which is shown as below:
  128. \arg RCU_FMC_SLP: FMC clock
  129. \arg RCU_SRAM_SLP: SRAM clock
  130. \param[out] none
  131. \retval none
  132. */
  133. void rcu_periph_clock_sleep_disable(rcu_periph_sleep_enum periph)
  134. {
  135. RCU_REG_VAL(periph) &= ~BIT(RCU_BIT_POS(periph));
  136. }
  137. /*!
  138. \brief reset the peripherals
  139. \param[in] periph_reset: RCU peripherals reset, refer to rcu_periph_reset_enum
  140. only one parameter can be selected which is shown as below:
  141. \arg RCU_GPIOxRST (x=A,B,C,F): reset GPIO ports
  142. \arg RCU_CFGCMPRST: reset CFGCMP
  143. \arg RCU_ADCRST: reset ADC
  144. \arg RCU_TIMERxRST (x=0,2,5,13,14,15,16): reset TIMER
  145. \arg RCU_SPIxRST (x=0,1): reset SPI
  146. \arg RCU_USARTxRST (x=0,1): reset USART
  147. \arg RCU_WWDGTRST: reset WWDGT
  148. \arg RCU_I2CxRST (x=0,1): reset I2C
  149. \arg RCU_PMURST: reset PMU
  150. \param[out] none
  151. \retval none
  152. */
  153. void rcu_periph_reset_enable(rcu_periph_reset_enum periph_reset)
  154. {
  155. RCU_REG_VAL(periph_reset) |= BIT(RCU_BIT_POS(periph_reset));
  156. }
  157. /*!
  158. \brief disable reset the peripheral
  159. \param[in] periph_reset: RCU peripherals reset, refer to rcu_periph_reset_enum
  160. only one parameter can be selected which is shown as below:
  161. \arg RCU_GPIOxRST (x=A,B,C,F): reset GPIO ports
  162. \arg RCU_CFGCMPRST: reset CFGCMP
  163. \arg RCU_ADCRST: reset ADC
  164. \arg RCU_TIMERxRST (x=0,2,5,13,14,15,16): reset TIMER
  165. \arg RCU_SPIxRST (x=0,1): reset SPI
  166. \arg RCU_USARTxRST (x=0,1): reset USART
  167. \arg RCU_WWDGTRST: reset WWDGT
  168. \arg RCU_I2CxRST (x=0,1): reset I2C
  169. \arg RCU_PMURST: reset PMU
  170. \param[out] none
  171. \retval none
  172. */
  173. void rcu_periph_reset_disable(rcu_periph_reset_enum periph_reset)
  174. {
  175. RCU_REG_VAL(periph_reset) &= ~BIT(RCU_BIT_POS(periph_reset));
  176. }
  177. /*!
  178. \brief reset the BKP
  179. \param[in] none
  180. \param[out] none
  181. \retval none
  182. */
  183. void rcu_bkp_reset_enable(void)
  184. {
  185. RCU_BDCTL |= RCU_BDCTL_BKPRST;
  186. }
  187. /*!
  188. \brief disable the BKP reset
  189. \param[in] none
  190. \param[out] none
  191. \retval none
  192. */
  193. void rcu_bkp_reset_disable(void)
  194. {
  195. RCU_BDCTL &= ~RCU_BDCTL_BKPRST;
  196. }
  197. /*!
  198. \brief configure the system clock source
  199. \param[in] ck_sys: system clock source select
  200. only one parameter can be selected which is shown as below:
  201. \arg RCU_CKSYSSRC_IRC8M: select CK_IRC8M as the CK_SYS source
  202. \arg RCU_CKSYSSRC_HXTAL: select CK_HXTAL as the CK_SYS source
  203. \arg RCU_CKSYSSRC_PLL: select CK_PLL as the CK_SYS source
  204. \param[out] none
  205. \retval none
  206. */
  207. void rcu_system_clock_source_config(uint32_t ck_sys)
  208. {
  209. uint32_t cksys_source = 0U;
  210. cksys_source = RCU_CFG0;
  211. /* reset the SCS bits and set according to ck_sys */
  212. cksys_source &= ~RCU_CFG0_SCS;
  213. RCU_CFG0 = (ck_sys | cksys_source);
  214. }
  215. /*!
  216. \brief get the system clock source
  217. \param[in] none
  218. \param[out] none
  219. \retval which clock is selected as CK_SYS source
  220. \arg RCU_SCSS_IRC8M: select CK_IRC8M as the CK_SYS source
  221. \arg RCU_SCSS_HXTAL: select CK_HXTAL as the CK_SYS source
  222. \arg RCU_SCSS_PLL: select CK_PLL as the CK_SYS source
  223. */
  224. uint32_t rcu_system_clock_source_get(void)
  225. {
  226. return (RCU_CFG0 & RCU_CFG0_SCSS);
  227. }
  228. /*!
  229. \brief configure the AHB clock prescaler selection
  230. \param[in] ck_ahb: AHB clock prescaler selection
  231. only one parameter can be selected which is shown as below:
  232. \arg RCU_AHB_CKSYS_DIVx(x=1, 2, 4, 8, 16, 64, 128, 256, 512): AHB clock is divided by x
  233. \param[out] none
  234. \retval none
  235. */
  236. void rcu_ahb_clock_config(uint32_t ck_ahb)
  237. {
  238. uint32_t ahbpsc = 0U;
  239. ahbpsc = RCU_CFG0;
  240. /* reset the AHBPSC bits and set according to ck_ahb */
  241. ahbpsc &= ~RCU_CFG0_AHBPSC;
  242. RCU_CFG0 = (ck_ahb | ahbpsc);
  243. }
  244. /*!
  245. \brief configure the APB1 clock prescaler selection
  246. \param[in] ck_apb1: APB1 clock prescaler selection
  247. only one parameter can be selected which is shown as below:
  248. \arg RCU_APB1_CKAHB_DIV1: select CK_AHB as CK_APB1
  249. \arg RCU_APB1_CKAHB_DIV2: select CK_AHB/2 as CK_APB1
  250. \arg RCU_APB1_CKAHB_DIV4: select CK_AHB/4 as CK_APB1
  251. \arg RCU_APB1_CKAHB_DIV8: select CK_AHB/8 as CK_APB1
  252. \arg RCU_APB1_CKAHB_DIV16: select CK_AHB/16 as CK_APB1
  253. \param[out] none
  254. \retval none
  255. */
  256. void rcu_apb1_clock_config(uint32_t ck_apb1)
  257. {
  258. uint32_t apb1psc = 0U;
  259. apb1psc = RCU_CFG0;
  260. /* reset the APB1PSC and set according to ck_apb1 */
  261. apb1psc &= ~RCU_CFG0_APB1PSC;
  262. RCU_CFG0 = (ck_apb1 | apb1psc);
  263. }
  264. /*!
  265. \brief configure the APB2 clock prescaler selection
  266. \param[in] ck_apb2: APB2 clock prescaler selection
  267. only one parameter can be selected which is shown as below:
  268. \arg RCU_APB2_CKAHB_DIV1: select CK_AHB as CK_APB2
  269. \arg RCU_APB2_CKAHB_DIV2: select CK_AHB/2 as CK_APB2
  270. \arg RCU_APB2_CKAHB_DIV4: select CK_AHB/4 as CK_APB2
  271. \arg RCU_APB2_CKAHB_DIV8: select CK_AHB/8 as CK_APB2
  272. \arg RCU_APB2_CKAHB_DIV16: select CK_AHB/16 as CK_APB2
  273. \param[out] none
  274. \retval none
  275. */
  276. void rcu_apb2_clock_config(uint32_t ck_apb2)
  277. {
  278. uint32_t apb2psc = 0U;
  279. apb2psc = RCU_CFG0;
  280. /* reset the APB2PSC and set according to ck_apb2 */
  281. apb2psc &= ~RCU_CFG0_APB2PSC;
  282. RCU_CFG0 = (ck_apb2 | apb2psc);
  283. }
  284. /*!
  285. \brief configure the ADC clock prescaler selection
  286. \param[in] ck_adc: ADC clock prescaler selection, refer to rcu_adc_clock_enum
  287. only one parameter can be selected which is shown as below:
  288. \arg RCU_ADCCK_IRC28M_DIV2: select CK_IRC28M/2 as CK_ADC
  289. \arg RCU_ADCCK_IRC28M: select CK_IRC28M as CK_ADC
  290. \arg RCU_ADCCK_APB2_DIV2: select CK_APB2/2 as CK_ADC
  291. \arg RCU_ADCCK_AHB_DIV3: select CK_AHB/3 as CK_ADC
  292. \arg RCU_ADCCK_APB2_DIV4: select CK_APB2/4 as CK_ADC
  293. \arg RCU_ADCCK_AHB_DIV5: select CK_AHB/5 as CK_ADC
  294. \arg RCU_ADCCK_APB2_DIV6: select CK_APB2/6 as CK_ADC
  295. \arg RCU_ADCCK_AHB_DIV7: select CK_AHB/7 as CK_ADC
  296. \arg RCU_ADCCK_APB2_DIV8: select CK_APB2/8 as CK_ADC
  297. \arg RCU_ADCCK_AHB_DIV9: select CK_AHB/9 as CK_ADC
  298. \param[out] none
  299. \retval none
  300. */
  301. void rcu_adc_clock_config(rcu_adc_clock_enum ck_adc)
  302. {
  303. /* reset the ADCPSC, ADCSEL, IRC28MDIV bits */
  304. RCU_CFG0 &= ~RCU_CFG0_ADCPSC;
  305. RCU_CFG2 &= ~(RCU_CFG2_ADCSEL | RCU_CFG2_IRC28MDIV | RCU_CFG2_ADCPSC2);
  306. /* set the ADC clock according to ck_adc */
  307. switch(ck_adc){
  308. case RCU_ADCCK_IRC28M_DIV2:
  309. RCU_CFG2 &= ~RCU_CFG2_IRC28MDIV;
  310. RCU_CFG2 &= ~RCU_CFG2_ADCSEL;
  311. break;
  312. case RCU_ADCCK_IRC28M:
  313. RCU_CFG2 |= RCU_CFG2_IRC28MDIV;
  314. RCU_CFG2 &= ~RCU_CFG2_ADCSEL;
  315. break;
  316. case RCU_ADCCK_APB2_DIV2:
  317. RCU_CFG0 |= RCU_ADC_CKAPB2_DIV2;
  318. RCU_CFG2 |= RCU_CFG2_ADCSEL;
  319. break;
  320. case RCU_ADCCK_AHB_DIV3:
  321. RCU_CFG0 |= RCU_ADC_CKAPB2_DIV2;
  322. RCU_CFG2 |= RCU_CFG2_ADCPSC2;
  323. RCU_CFG2 |= RCU_CFG2_ADCSEL;
  324. break;
  325. case RCU_ADCCK_APB2_DIV4:
  326. RCU_CFG0 |= RCU_ADC_CKAPB2_DIV4;
  327. RCU_CFG2 |= RCU_CFG2_ADCSEL;
  328. break;
  329. case RCU_ADCCK_AHB_DIV5:
  330. RCU_CFG0 |= RCU_ADC_CKAPB2_DIV4;
  331. RCU_CFG2 |= RCU_CFG2_ADCPSC2;
  332. RCU_CFG2 |= RCU_CFG2_ADCSEL;
  333. break;
  334. case RCU_ADCCK_APB2_DIV6:
  335. RCU_CFG0 |= RCU_ADC_CKAPB2_DIV6;
  336. RCU_CFG2 |= RCU_CFG2_ADCSEL;
  337. break;
  338. case RCU_ADCCK_AHB_DIV7:
  339. RCU_CFG0 |= RCU_ADC_CKAPB2_DIV6;
  340. RCU_CFG2 |= RCU_CFG2_ADCPSC2;
  341. RCU_CFG2 |= RCU_CFG2_ADCSEL;
  342. break;
  343. case RCU_ADCCK_APB2_DIV8:
  344. RCU_CFG0 |= RCU_ADC_CKAPB2_DIV8;
  345. RCU_CFG2 |= RCU_CFG2_ADCSEL;
  346. break;
  347. case RCU_ADCCK_AHB_DIV9:
  348. RCU_CFG0 |= RCU_ADC_CKAPB2_DIV8;
  349. RCU_CFG2 |= RCU_CFG2_ADCPSC2;
  350. RCU_CFG2 |= RCU_CFG2_ADCSEL;
  351. break;
  352. default:
  353. break;
  354. }
  355. }
  356. /*!
  357. \brief configure the CK_OUT clock source and divider
  358. \param[in] ckout_src: CK_OUT clock source selection
  359. only one parameter can be selected which is shown as below:
  360. \arg RCU_CKOUTSRC_NONE: no clock selected
  361. \arg RCU_CKOUTSRC_IRC28M: IRC28M selected
  362. \arg RCU_CKOUTSRC_IRC40K: IRC40K selected
  363. \arg RCU_CKOUTSRC_LXTAL: LXTAL selected
  364. \arg RCU_CKOUTSRC_CKSYS: CKSYS selected
  365. \arg RCU_CKOUTSRC_IRC8M: IRC8M selected
  366. \arg RCU_CKOUTSRC_HXTAL: HXTAL selected
  367. \arg RCU_CKOUTSRC_CKPLL_DIV1: CK_PLL selected
  368. \arg RCU_CKOUTSRC_CKPLL_DIV2: CK_PLL/2 selected
  369. \param[in] ckout_div: CK_OUT divider
  370. \arg RCU_CKOUT_DIVx(x=1,2,4,8,16,32,64,128): CK_OUT is divided by x
  371. \param[out] none
  372. \retval none
  373. */
  374. void rcu_ckout_config(uint32_t ckout_src, uint32_t ckout_div)
  375. {
  376. uint32_t ckout = 0U;
  377. ckout = RCU_CFG0;
  378. /* reset the CKOUTSEL, CKOUTDIV and PLLDV bits and set according to ckout_src and ckout_div */
  379. ckout &= ~(RCU_CFG0_CKOUTSEL | RCU_CFG0_CKOUTDIV | RCU_CFG0_PLLDV);
  380. RCU_CFG0 = (ckout | ckout_src | ckout_div);
  381. }
  382. /*!
  383. \brief configure the PLL clock source selection and PLL multiply factor
  384. \param[in] pll_src: PLL clock source selection
  385. only one parameter can be selected which is shown as below:
  386. \arg RCU_PLLSRC_IRC8M_DIV2: select CK_IRC8M/2 as PLL source clock
  387. \arg RCU_PLLSRC_HXTAL: select HXTAL as PLL source clock
  388. \param[in] pll_mul: PLL multiply factor
  389. only one parameter can be selected which is shown as below:
  390. \arg RCU_PLL_MULx(x=2..32): PLL source clock * x
  391. \param[out] none
  392. \retval none
  393. */
  394. void rcu_pll_config(uint32_t pll_src, uint32_t pll_mul)
  395. {
  396. RCU_CFG0 &= ~(RCU_CFG0_PLLSEL | RCU_CFG0_PLLMF);
  397. RCU_CFG0 |= (pll_src | pll_mul);
  398. }
  399. /*!
  400. \brief configure the USART clock source selection
  401. \param[in] ck_usart: USART clock source selection
  402. only one parameter can be selected which is shown as below:
  403. \arg RCU_USART0SRC_CKAPB2: CK_USART0 select CK_APB2
  404. \arg RCU_USART0SRC_CKSYS: CK_USART0 select CK_SYS
  405. \arg RCU_USART0SRC_LXTAL: CK_USART0 select CK_LXTAL
  406. \arg RCU_USART0SRC_IRC8M: CK_USART0 select CK_IRC8M
  407. \param[out] none
  408. \retval none
  409. */
  410. void rcu_usart_clock_config(uint32_t ck_usart)
  411. {
  412. /* reset the USART0SEL bits and set according to ck_usart */
  413. RCU_CFG2 &= ~RCU_CFG2_USART0SEL;
  414. RCU_CFG2 |= ck_usart;
  415. }
  416. /*!
  417. \brief configure the RTC clock source selection
  418. \param[in] rtc_clock_source: RTC clock source selection
  419. only one parameter can be selected which is shown as below:
  420. \arg RCU_RTCSRC_NONE: no clock selected
  421. \arg RCU_RTCSRC_LXTAL: CK_LXTAL selected as RTC source clock
  422. \arg RCU_RTCSRC_IRC40K: CK_IRC40K selected as RTC source clock
  423. \arg RCU_RTCSRC_HXTAL_DIV32: CK_HXTAL/32 selected as RTC source clock
  424. \param[out] none
  425. \retval none
  426. */
  427. void rcu_rtc_clock_config(uint32_t rtc_clock_source)
  428. {
  429. /* reset the RTCSRC bits and set according to rtc_clock_source */
  430. RCU_BDCTL &= ~RCU_BDCTL_RTCSRC;
  431. RCU_BDCTL |= rtc_clock_source;
  432. }
  433. /*!
  434. \brief configure the HXTAL divider used as input of PLL
  435. \param[in] hxtal_prediv: HXTAL divider used as input of PLL
  436. only one parameter can be selected which is shown as below:
  437. \arg RCU_PLL_PREDVx(x=1..16): HXTAL divided x used as input of PLL
  438. \param[out] none
  439. \retval none
  440. */
  441. void rcu_hxtal_prediv_config(uint32_t hxtal_prediv)
  442. {
  443. uint32_t prediv = 0U;
  444. prediv = RCU_CFG1;
  445. /* reset the PREDV bits and set according to hxtal_prediv */
  446. prediv &= ~RCU_CFG1_PREDV;
  447. RCU_CFG1 = (prediv | hxtal_prediv);
  448. }
  449. /*!
  450. \brief configure the LXTAL drive capability
  451. \param[in] lxtal_dricap: drive capability of LXTAL
  452. only one parameter can be selected which is shown as below:
  453. \arg RCU_LXTAL_LOWDRI: lower driving capability
  454. \arg RCU_LXTAL_MED_LOWDRI: medium low driving capability
  455. \arg RCU_LXTAL_MED_HIGHDRI: medium high driving capability
  456. \arg RCU_LXTAL_HIGHDRI: higher driving capability
  457. \param[out] none
  458. \retval none
  459. */
  460. void rcu_lxtal_drive_capability_config(uint32_t lxtal_dricap)
  461. {
  462. /* reset the LXTALDRI bits and set according to lxtal_dricap */
  463. RCU_BDCTL &= ~RCU_BDCTL_LXTALDRI;
  464. RCU_BDCTL |= lxtal_dricap;
  465. }
  466. /*!
  467. \brief wait until oscillator stabilization flags is SET
  468. \param[in] osci: oscillator types, refer to rcu_osci_type_enum
  469. only one parameter can be selected which is shown as below:
  470. \arg RCU_HXTAL: HXTAL
  471. \arg RCU_LXTAL: LXTAL
  472. \arg RCU_IRC8M: IRC8M
  473. \arg RCU_IRC28M: IRC28M
  474. \arg RCU_IRC40K: IRC40K
  475. \arg RCU_PLL_CK: PLL
  476. \param[out] none
  477. \retval ErrStatus: SUCCESS or ERROR
  478. */
  479. ErrStatus rcu_osci_stab_wait(rcu_osci_type_enum osci)
  480. {
  481. uint32_t stb_cnt = 0U;
  482. ErrStatus reval = ERROR;
  483. FlagStatus osci_stat = RESET;
  484. switch(osci){
  485. case RCU_HXTAL:
  486. /* wait until HXTAL is stabilization and osci_stat is not more than timeout */
  487. while((RESET == osci_stat) && (HXTAL_STARTUP_TIMEOUT != stb_cnt)){
  488. osci_stat = rcu_flag_get(RCU_FLAG_HXTALSTB);
  489. stb_cnt++;
  490. }
  491. /* check whether flag is set or not */
  492. if(RESET != rcu_flag_get(RCU_FLAG_HXTALSTB)){
  493. reval = SUCCESS;
  494. }
  495. break;
  496. /* wait LXTAL stable */
  497. case RCU_LXTAL:
  498. while((RESET == osci_stat) && (LXTAL_STARTUP_TIMEOUT != stb_cnt)){
  499. osci_stat = rcu_flag_get(RCU_FLAG_LXTALSTB);
  500. stb_cnt++;
  501. }
  502. /* check whether flag is set or not */
  503. if(RESET != rcu_flag_get(RCU_FLAG_LXTALSTB)){
  504. reval = SUCCESS;
  505. }
  506. break;
  507. /* wait IRC8M stable */
  508. case RCU_IRC8M:
  509. while((RESET == osci_stat) && (IRC8M_STARTUP_TIMEOUT != stb_cnt)){
  510. osci_stat = rcu_flag_get(RCU_FLAG_IRC8MSTB);
  511. stb_cnt++;
  512. }
  513. /* check whether flag is set or not */
  514. if(RESET != rcu_flag_get(RCU_FLAG_IRC8MSTB)){
  515. reval = SUCCESS;
  516. }
  517. break;
  518. /* wait IRC28M stable */
  519. case RCU_IRC28M:
  520. while((RESET == osci_stat) && (OSC_STARTUP_TIMEOUT != stb_cnt)){
  521. osci_stat = rcu_flag_get(RCU_FLAG_IRC28MSTB);
  522. stb_cnt++;
  523. }
  524. /* check whether flag is set or not */
  525. if(RESET != rcu_flag_get(RCU_FLAG_IRC28MSTB)){
  526. reval = SUCCESS;
  527. }
  528. break;
  529. /* wait IRC40K stable */
  530. case RCU_IRC40K:
  531. while((RESET == osci_stat) && (OSC_STARTUP_TIMEOUT != stb_cnt)){
  532. osci_stat = rcu_flag_get(RCU_FLAG_IRC40KSTB);
  533. stb_cnt++;
  534. }
  535. /* check whether flag is set or not */
  536. if(RESET != rcu_flag_get(RCU_FLAG_IRC40KSTB)){
  537. reval = SUCCESS;
  538. }
  539. break;
  540. /* wait PLL stable */
  541. case RCU_PLL_CK:
  542. while((RESET == osci_stat) && (OSC_STARTUP_TIMEOUT != stb_cnt)){
  543. osci_stat = rcu_flag_get(RCU_FLAG_PLLSTB);
  544. stb_cnt++;
  545. }
  546. /* check whether flag is set or not */
  547. if(RESET != rcu_flag_get(RCU_FLAG_PLLSTB)){
  548. reval = SUCCESS;
  549. }
  550. break;
  551. default:
  552. break;
  553. }
  554. /* return value */
  555. return reval;
  556. }
  557. /*!
  558. \brief turn on the oscillator
  559. \param[in] osci: oscillator types, refer to rcu_osci_type_enum
  560. only one parameter can be selected which is shown as below:
  561. \arg RCU_HXTAL: HXTAL
  562. \arg RCU_LXTAL: LXTAL
  563. \arg RCU_IRC8M: IRC8M
  564. \arg RCU_IRC28M: IRC28M
  565. \arg RCU_IRC40K: IRC40K
  566. \arg RCU_PLL_CK: PLL
  567. \param[out] none
  568. \retval none
  569. */
  570. void rcu_osci_on(rcu_osci_type_enum osci)
  571. {
  572. RCU_REG_VAL(osci) |= BIT(RCU_BIT_POS(osci));
  573. }
  574. /*!
  575. \brief turn off the oscillator
  576. \param[in] osci: oscillator types, refer to rcu_osci_type_enum
  577. only one parameter can be selected which is shown as below:
  578. \arg RCU_HXTAL: HXTAL
  579. \arg RCU_LXTAL: LXTAL
  580. \arg RCU_IRC8M: IRC8M
  581. \arg RCU_IRC28M: IRC28M
  582. \arg RCU_IRC40K: IRC40K
  583. \arg RCU_PLL_CK: PLL
  584. \param[out] none
  585. \retval none
  586. */
  587. void rcu_osci_off(rcu_osci_type_enum osci)
  588. {
  589. RCU_REG_VAL(osci) &= ~BIT(RCU_BIT_POS(osci));
  590. }
  591. /*!
  592. \brief enable the oscillator bypass mode, HXTALEN or LXTALEN must be reset before it
  593. \param[in] osci: oscillator types, refer to rcu_osci_type_enum
  594. only one parameter can be selected which is shown as below:
  595. \arg RCU_HXTAL: HXTAL
  596. \arg RCU_LXTAL: LXTAL
  597. \param[out] none
  598. \retval none
  599. */
  600. void rcu_osci_bypass_mode_enable(rcu_osci_type_enum osci)
  601. {
  602. uint32_t reg;
  603. switch(osci){
  604. case RCU_HXTAL:
  605. /* HXTALEN must be reset before enable the oscillator bypass mode */
  606. reg = RCU_CTL0;
  607. RCU_CTL0 &= ~RCU_CTL0_HXTALEN;
  608. RCU_CTL0 = (reg | RCU_CTL0_HXTALBPS);
  609. break;
  610. case RCU_LXTAL:
  611. /* LXTALEN must be reset before enable the oscillator bypass mode */
  612. reg = RCU_BDCTL;
  613. RCU_BDCTL &= ~RCU_BDCTL_LXTALEN;
  614. RCU_BDCTL = (reg | RCU_BDCTL_LXTALBPS);
  615. break;
  616. case RCU_IRC8M:
  617. case RCU_IRC28M:
  618. case RCU_IRC40K:
  619. case RCU_PLL_CK:
  620. break;
  621. default:
  622. break;
  623. }
  624. }
  625. /*!
  626. \brief disable the oscillator bypass mode, HXTALEN or LXTALEN must be reset before it
  627. \param[in] osci: oscillator types, refer to rcu_osci_type_enum
  628. only one parameter can be selected which is shown as below:
  629. \arg RCU_HXTAL: HXTAL
  630. \arg RCU_LXTAL: LXTAL
  631. \param[out] none
  632. \retval none
  633. */
  634. void rcu_osci_bypass_mode_disable(rcu_osci_type_enum osci)
  635. {
  636. uint32_t reg;
  637. switch(osci){
  638. case RCU_HXTAL:
  639. /* HXTALEN must be reset before disable the oscillator bypass mode */
  640. reg = RCU_CTL0;
  641. RCU_CTL0 &= ~RCU_CTL0_HXTALEN;
  642. RCU_CTL0 = (reg & (~RCU_CTL0_HXTALBPS));
  643. break;
  644. case RCU_LXTAL:
  645. /* LXTALEN must be reset before disable the oscillator bypass mode */
  646. reg = RCU_BDCTL;
  647. RCU_BDCTL &= ~RCU_BDCTL_LXTALEN;
  648. RCU_BDCTL = (reg & (~RCU_BDCTL_LXTALBPS));
  649. break;
  650. case RCU_IRC8M:
  651. case RCU_IRC28M:
  652. case RCU_IRC40K:
  653. case RCU_PLL_CK:
  654. break;
  655. default:
  656. break;
  657. }
  658. }
  659. /*!
  660. \brief enable the HXTAL clock monitor
  661. \param[in] none
  662. \param[out] none
  663. \retval none
  664. */
  665. void rcu_hxtal_clock_monitor_enable(void)
  666. {
  667. RCU_CTL0 |= RCU_CTL0_CKMEN;
  668. }
  669. /*!
  670. \brief disable the HXTAL clock monitor
  671. \param[in] none
  672. \param[out] none
  673. \retval none
  674. */
  675. void rcu_hxtal_clock_monitor_disable(void)
  676. {
  677. RCU_CTL0 &= ~RCU_CTL0_CKMEN;
  678. }
  679. /*!
  680. \brief set the IRC8M adjust value
  681. \param[in] irc8m_adjval: IRC8M adjust value, must be between 0 and 0x1F
  682. \param[out] none
  683. \retval none
  684. */
  685. void rcu_irc8m_adjust_value_set(uint8_t irc8m_adjval)
  686. {
  687. uint32_t adjust = 0U;
  688. adjust = RCU_CTL0;
  689. /* reset the IRC8MADJ bits and set according to irc8m_adjval */
  690. adjust &= ~RCU_CTL0_IRC8MADJ;
  691. RCU_CTL0 = (adjust | (((uint32_t)irc8m_adjval)<<3));
  692. }
  693. /*!
  694. \brief set the IRC28M adjust value
  695. \param[in] irc28m_adjval: IRC28M adjust value, must be between 0 and 0x1F
  696. \param[out] none
  697. \retval none
  698. */
  699. void rcu_irc28m_adjust_value_set(uint8_t irc28m_adjval)
  700. {
  701. uint32_t adjust = 0U;
  702. adjust = RCU_CTL1;
  703. /* reset the IRC28MADJ bits and set according to irc28m_adjval */
  704. adjust &= ~RCU_CTL1_IRC28MADJ;
  705. RCU_CTL1 = (adjust | (((uint32_t)irc28m_adjval)<<3));
  706. }
  707. /*!
  708. \brief unlock the voltage key
  709. \param[in] none
  710. \param[out] none
  711. \retval none
  712. */
  713. void rcu_voltage_key_unlock(void)
  714. {
  715. /* reset the KEY bits and set 0x1A2B3C4D */
  716. RCU_VKEY &= ~RCU_VKEY_KEY;
  717. RCU_VKEY |= RCU_VKEY_UNLOCK;
  718. }
  719. /*!
  720. \brief set voltage in deep sleep mode
  721. \param[in] dsvol: deep sleep mode voltage
  722. only one parameter can be selected which is shown as below:
  723. \arg RCU_DEEPSLEEP_V_1_0: the core voltage is 1.0V
  724. \arg RCU_DEEPSLEEP_V_0_9: the core voltage is 0.9V
  725. \arg RCU_DEEPSLEEP_V_0_8: the core voltage is 0.8V
  726. \arg RCU_DEEPSLEEP_V_1_2: the core voltage is 1.2V
  727. \param[out] none
  728. \retval none
  729. */
  730. void rcu_deepsleep_voltage_set(uint32_t dsvol)
  731. {
  732. /* reset the DSLPVS bits and set according to dsvol */
  733. RCU_DSV &= ~RCU_DSV_DSLPVS;
  734. RCU_DSV |= dsvol;
  735. }
  736. /*!
  737. \brief get the system clock, bus and peripheral clock frequency
  738. \param[in] clock: the clock frequency which to get
  739. only one parameter can be selected which is shown as below:
  740. \arg CK_SYS: system clock frequency
  741. \arg CK_AHB: AHB clock frequency
  742. \arg CK_APB1: APB1 clock frequency
  743. \arg CK_APB2: APB2 clock frequency
  744. \arg CK_ADC: ADC clock frequency
  745. \arg CK_USART: USART0 clock frequency
  746. \param[out] none
  747. \retval clock frequency of system, AHB, APB1, APB2, ADC or USRAT0
  748. */
  749. uint32_t rcu_clock_freq_get(rcu_clock_freq_enum clock)
  750. {
  751. uint32_t sws = 0U, adcps = 0U, adcps2 = 0U, ck_freq = 0U;
  752. uint32_t cksys_freq = 0U, ahb_freq = 0U, apb1_freq = 0U, apb2_freq = 0U;
  753. uint32_t adc_freq = 0U, usart_freq = 0U;
  754. uint32_t pllmf = 0U, pllmf4 = 0U, pllsel = 0U, prediv = 0U, idx = 0U, clk_exp = 0U;
  755. /* exponent of AHB, APB1 and APB2 clock divider */
  756. const uint8_t ahb_exp[16] = {0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 4, 6, 7, 8, 9};
  757. const uint8_t apb1_exp[8] = {0, 0, 0, 0, 1, 2, 3, 4};
  758. const uint8_t apb2_exp[8] = {0, 0, 0, 0, 1, 2, 3, 4};
  759. sws = GET_BITS(RCU_CFG0, 2, 3);
  760. switch(sws){
  761. /* IRC8M is selected as CK_SYS */
  762. case SEL_IRC8M:
  763. cksys_freq = IRC8M_VALUE;
  764. break;
  765. /* HXTAL is selected as CK_SYS */
  766. case SEL_HXTAL:
  767. cksys_freq = HXTAL_VALUE;
  768. break;
  769. /* PLL is selected as CK_SYS */
  770. case SEL_PLL:
  771. /* get the value of PLLMF[3:0] */
  772. pllmf = GET_BITS(RCU_CFG0, 18, 21);
  773. pllmf4 = GET_BITS(RCU_CFG0, 27, 27);
  774. /* high 16 bits */
  775. if(1U == pllmf4){
  776. pllmf += 17U;
  777. }else if(15U == pllmf){
  778. pllmf = 16U;
  779. }else{
  780. pllmf += 2U;
  781. }
  782. /* PLL clock source selection, HXTAL or IRC8M/2 */
  783. pllsel = GET_BITS(RCU_CFG0, 16, 16);
  784. if(0U != pllsel){
  785. prediv = (GET_BITS(RCU_CFG1, 0, 3) + 1U);
  786. cksys_freq = (HXTAL_VALUE / prediv) * pllmf;
  787. }else{
  788. cksys_freq = (IRC8M_VALUE >> 1) * pllmf;
  789. }
  790. break;
  791. /* IRC8M is selected as CK_SYS */
  792. default:
  793. cksys_freq = IRC8M_VALUE;
  794. break;
  795. }
  796. /* calculate AHB clock frequency */
  797. idx = GET_BITS(RCU_CFG0, 4, 7);
  798. clk_exp = ahb_exp[idx];
  799. ahb_freq = cksys_freq >> clk_exp;
  800. /* calculate APB1 clock frequency */
  801. idx = GET_BITS(RCU_CFG0, 8, 10);
  802. clk_exp = apb1_exp[idx];
  803. apb1_freq = ahb_freq >> clk_exp;
  804. /* calculate APB2 clock frequency */
  805. idx = GET_BITS(RCU_CFG0, 11, 13);
  806. clk_exp = apb2_exp[idx];
  807. apb2_freq = ahb_freq >> clk_exp;
  808. /* return the clocks frequency */
  809. switch(clock){
  810. case CK_SYS:
  811. ck_freq = cksys_freq;
  812. break;
  813. case CK_AHB:
  814. ck_freq = ahb_freq;
  815. break;
  816. case CK_APB1:
  817. ck_freq = apb1_freq;
  818. break;
  819. case CK_APB2:
  820. ck_freq = apb2_freq;
  821. break;
  822. case CK_ADC:
  823. /* calculate ADC clock frequency */
  824. if(RCU_ADCSRC_AHB_APB2DIV != (RCU_CFG2 & RCU_CFG2_ADCSEL)){
  825. if(RCU_ADC_IRC28M_DIV1 != (RCU_CFG2 & RCU_CFG2_IRC28MDIV)){
  826. adc_freq = IRC28M_VALUE >> 1;
  827. }else{
  828. adc_freq = IRC28M_VALUE;
  829. }
  830. }else{
  831. /* ADC clock select CK_APB2 divided by 2/4/6/8 or CK_AHB divided by 3/5/7/9 */
  832. adcps = GET_BITS(RCU_CFG0, 14, 15);
  833. adcps2 = GET_BITS(RCU_CFG2, 31, 31);
  834. switch(adcps){
  835. case 0:
  836. if(0U == adcps2){
  837. adc_freq = apb2_freq / 2U;
  838. }else{
  839. adc_freq = ahb_freq / 3U;
  840. }
  841. break;
  842. case 1:
  843. if(0U == adcps2){
  844. adc_freq = apb2_freq / 4U;
  845. }else{
  846. adc_freq = ahb_freq / 5U;
  847. }
  848. break;
  849. case 2:
  850. if(0U == adcps2){
  851. adc_freq = apb2_freq / 6U;
  852. }else{
  853. adc_freq = ahb_freq / 7U;
  854. }
  855. break;
  856. case 3:
  857. if(0U == adcps2){
  858. adc_freq = apb2_freq / 8U;
  859. }else{
  860. adc_freq = ahb_freq / 9U;
  861. }
  862. break;
  863. default:
  864. break;
  865. }
  866. }
  867. ck_freq = adc_freq;
  868. break;
  869. case CK_USART:
  870. /* calculate USART0 clock frequency */
  871. if(RCU_USART0SRC_CKAPB2 == (RCU_CFG2 & RCU_CFG2_USART0SEL)){
  872. usart_freq = apb2_freq;
  873. }else if(RCU_USART0SRC_CKSYS == (RCU_CFG2 & RCU_CFG2_USART0SEL)){
  874. usart_freq = cksys_freq;
  875. }else if(RCU_USART0SRC_LXTAL == (RCU_CFG2 & RCU_CFG2_USART0SEL)){
  876. usart_freq = LXTAL_VALUE;
  877. }else if(RCU_USART0SRC_IRC8M == (RCU_CFG2 & RCU_CFG2_USART0SEL)){
  878. usart_freq = IRC8M_VALUE;
  879. }else{
  880. }
  881. ck_freq = usart_freq;
  882. break;
  883. default:
  884. break;
  885. }
  886. return ck_freq;
  887. }
  888. /*!
  889. \brief get the clock stabilization and periphral reset flags
  890. \param[in] flag: the clock stabilization and periphral reset flags, refer to rcu_flag_enum
  891. only one parameter can be selected which is shown as below:
  892. \arg RCU_FLAG_IRC40KSTB: IRC40K stabilization flag
  893. \arg RCU_FLAG_LXTALSTB: LXTAL stabilization flag
  894. \arg RCU_FLAG_IRC8MSTB: IRC8M stabilization flag
  895. \arg RCU_FLAG_HXTALSTB: HXTAL stabilization flag
  896. \arg RCU_FLAG_PLLSTB: PLL stabilization flag
  897. \arg RCU_FLAG_IRC28MSTB: IRC28M stabilization flag
  898. \arg RCU_FLAG_V12RST: V12 domain power reset flag
  899. \arg RCU_FLAG_OBLRST: option byte loader reset flag
  900. \arg RCU_FLAG_EPRST: external pin reset flag
  901. \arg RCU_FLAG_PORRST: power reset flag
  902. \arg RCU_FLAG_SWRST: software reset flag
  903. \arg RCU_FLAG_FWDGTRST: free watchdog timer reset flag
  904. \arg RCU_FLAG_WWDGTRST: window watchdog timer reset flag
  905. \arg RCU_FLAG_LPRST: low-power reset flag
  906. \param[out] none
  907. \retval FlagStatus: SET or RESET
  908. */
  909. FlagStatus rcu_flag_get(rcu_flag_enum flag)
  910. {
  911. if(RESET != (RCU_REG_VAL(flag) & BIT(RCU_BIT_POS(flag)))){
  912. return SET;
  913. }else{
  914. return RESET;
  915. }
  916. }
  917. /*!
  918. \brief clear the reset flag
  919. \param[in] none
  920. \param[out] none
  921. \retval none
  922. */
  923. void rcu_all_reset_flag_clear(void)
  924. {
  925. RCU_RSTSCK |= RCU_RSTSCK_RSTFC;
  926. }
  927. /*!
  928. \brief get the clock stabilization interrupt and ckm flags
  929. \param[in] int_flag: interrupt and ckm flags, refer to rcu_int_flag_enum
  930. only one parameter can be selected which is shown as below:
  931. \arg RCU_INT_FLAG_IRC40KSTB: IRC40K stabilization interrupt flag
  932. \arg RCU_INT_FLAG_LXTALSTB: LXTAL stabilization interrupt flag
  933. \arg RCU_INT_FLAG_IRC8MSTB: IRC8M stabilization interrupt flag
  934. \arg RCU_INT_FLAG_HXTALSTB: HXTAL stabilization interrupt flag
  935. \arg RCU_INT_FLAG_PLLSTB: PLL stabilization interrupt flag
  936. \arg RCU_INT_FLAG_IRC28MSTB: IRC28M stabilization interrupt flag
  937. \arg RCU_INT_FLAG_CKM: HXTAL clock stuck interrupt flag
  938. \param[out] none
  939. \retval FlagStatus: SET or RESET
  940. */
  941. FlagStatus rcu_interrupt_flag_get(rcu_int_flag_enum int_flag)
  942. {
  943. if(RESET != (RCU_REG_VAL(int_flag) & BIT(RCU_BIT_POS(int_flag)))){
  944. return SET;
  945. }else{
  946. return RESET;
  947. }
  948. }
  949. /*!
  950. \brief clear the interrupt flags
  951. \param[in] int_flag_clear: clock stabilization and stuck interrupt flags clear, refer to rcu_int_flag_clear_enum
  952. only one parameter can be selected which is shown as below:
  953. \arg RCU_INT_FLAG_IRC40KSTB_CLR: IRC40K stabilization interrupt flag clear
  954. \arg RCU_INT_FLAG_LXTALSTB_CLR: LXTAL stabilization interrupt flag clear
  955. \arg RCU_INT_FLAG_IRC8MSTB_CLR: IRC8M stabilization interrupt flag clear
  956. \arg RCU_INT_FLAG_HXTALSTB_CLR: HXTAL stabilization interrupt flag clear
  957. \arg RCU_INT_FLAG_PLLSTB_CLR: PLL stabilization interrupt flag clear
  958. \arg RCU_INT_FLAG_IRC28MSTB_CLR: IRC28M stabilization interrupt flag clear
  959. \arg RCU_INT_FLAG_CKM_CLR: clock stuck interrupt flag clear
  960. \param[out] none
  961. \retval none
  962. */
  963. void rcu_interrupt_flag_clear(rcu_int_flag_clear_enum int_flag_clear)
  964. {
  965. RCU_REG_VAL(int_flag_clear) |= BIT(RCU_BIT_POS(int_flag_clear));
  966. }
  967. /*!
  968. \brief enable the stabilization interrupt
  969. \param[in] stab_int: clock stabilization interrupt, refer to rcu_int_enum
  970. only one parameter can be selected which is shown as below:
  971. \arg RCU_INT_IRC40KSTB: IRC40K stabilization interrupt enable
  972. \arg RCU_INT_LXTALSTB: LXTAL stabilization interrupt enable
  973. \arg RCU_INT_IRC8MSTB: IRC8M stabilization interrupt enable
  974. \arg RCU_INT_HXTALSTB: HXTAL stabilization interrupt enable
  975. \arg RCU_INT_PLLSTB: PLL stabilization interrupt enable
  976. \arg RCU_INT_IRC28MSTB: IRC28M stabilization interrupt enable
  977. \param[out] none
  978. \retval none
  979. */
  980. void rcu_interrupt_enable(rcu_int_enum stab_int)
  981. {
  982. RCU_REG_VAL(stab_int) |= BIT(RCU_BIT_POS(stab_int));
  983. }
  984. /*!
  985. \brief disable the stabilization interrupt
  986. \param[in] stab_int: clock stabilization interrupt, refer to rcu_int_enum
  987. only one parameter can be selected which is shown as below:
  988. \arg RCU_INT_IRC40KSTB: IRC40K stabilization interrupt disable
  989. \arg RCU_INT_LXTALSTB: LXTAL stabilization interrupt disable
  990. \arg RCU_INT_IRC8MSTB: IRC8M stabilization interrupt disable
  991. \arg RCU_INT_HXTALSTB: HXTAL stabilization interrupt disable
  992. \arg RCU_INT_PLLSTB: PLL stabilization interrupt disable
  993. \arg RCU_INT_IRC28MSTB: IRC28M stabilization interrupt disable
  994. \param[out] none
  995. \retval none
  996. */
  997. void rcu_interrupt_disable(rcu_int_enum stab_int)
  998. {
  999. RCU_REG_VAL(stab_int) &= ~BIT(RCU_BIT_POS(stab_int));
  1000. }