gd32f30x_pmu.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  1. /*!
  2. \file gd32f30x_pmu.c
  3. \brief PMU driver
  4. \version 2023-12-30, V2.2.0, firmware for GD32F30x
  5. */
  6. /*
  7. Copyright (c) 2020, GigaDevice Semiconductor Inc.
  8. Redistribution and use in source and binary forms, with or without modification,
  9. are permitted provided that the following conditions are met:
  10. 1. Redistributions of source code must retain the above copyright notice, this
  11. list of conditions and the following disclaimer.
  12. 2. Redistributions in binary form must reproduce the above copyright notice,
  13. this list of conditions and the following disclaimer in the documentation
  14. and/or other materials provided with the distribution.
  15. 3. Neither the name of the copyright holder nor the names of its contributors
  16. may be used to endorse or promote products derived from this software without
  17. specific prior written permission.
  18. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  19. AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  20. WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  21. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
  22. INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  23. NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  24. PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  25. WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  26. ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
  27. OF SUCH DAMAGE.
  28. */
  29. #include "gd32f30x_pmu.h"
  30. /*!
  31. \brief reset PMU register
  32. \param[in] none
  33. \param[out] none
  34. \retval none
  35. */
  36. void pmu_deinit(void)
  37. {
  38. /* reset PMU */
  39. rcu_periph_reset_enable(RCU_PMURST);
  40. rcu_periph_reset_disable(RCU_PMURST);
  41. }
  42. /*!
  43. \brief select low voltage detector threshold
  44. \param[in] lvdt_n:
  45. \arg PMU_LVDT_0: voltage threshold is 2.1V
  46. \arg PMU_LVDT_1: voltage threshold is 2.3V
  47. \arg PMU_LVDT_2: voltage threshold is 2.4V
  48. \arg PMU_LVDT_3: voltage threshold is 2.6V
  49. \arg PMU_LVDT_4: voltage threshold is 2.7V
  50. \arg PMU_LVDT_5: voltage threshold is 2.9V
  51. \arg PMU_LVDT_6: voltage threshold is 3.0V
  52. \arg PMU_LVDT_7: voltage threshold is 3.1V
  53. \param[out] none
  54. \retval none
  55. */
  56. void pmu_lvd_select(uint32_t lvdt_n)
  57. {
  58. /* disable LVD */
  59. PMU_CTL &= ~PMU_CTL_LVDEN;
  60. /* clear LVDT bits */
  61. PMU_CTL &= ~PMU_CTL_LVDT;
  62. /* set LVDT bits according to lvdt_n */
  63. PMU_CTL |= lvdt_n;
  64. /* enable LVD */
  65. PMU_CTL |= PMU_CTL_LVDEN;
  66. }
  67. /*!
  68. \brief select LDO output voltage
  69. this bit set by software when the main PLL closed, before closing PLL, change the system clock to IRC16M or HXTAL
  70. \param[in] ldo_output:
  71. \arg PMU_LDOVS_LOW: LDO output voltage low mode
  72. \arg PMU_LDOVS_MID: LDO output voltage mid mode
  73. \arg PMU_LDOVS_HIGH: LDO output voltage high mode
  74. \param[out] none
  75. \retval none
  76. */
  77. void pmu_ldo_output_select(uint32_t ldo_output)
  78. {
  79. PMU_CTL &= ~PMU_CTL_LDOVS;
  80. PMU_CTL |= ldo_output;
  81. }
  82. /*!
  83. \brief disable PMU lvd
  84. \param[in] none
  85. \param[out] none
  86. \retval none
  87. */
  88. void pmu_lvd_disable(void)
  89. {
  90. /* disable LVD */
  91. PMU_CTL &= ~PMU_CTL_LVDEN;
  92. }
  93. /*!
  94. \brief switch high-driver mode
  95. this bit set by software only when IRC16M or HXTAL used as system clock
  96. \param[in] highdr_switch:
  97. \arg PMU_HIGHDR_SWITCH_NONE: disable high-driver mode switch
  98. \arg PMU_HIGHDR_SWITCH_EN: enable high-driver mode switch
  99. \param[out] none
  100. \retval none
  101. */
  102. void pmu_highdriver_switch_select(uint32_t highdr_switch)
  103. {
  104. /* wait for HDRF flag set */
  105. while(SET != pmu_flag_get(PMU_FLAG_HDRF)) {
  106. }
  107. PMU_CTL &= ~PMU_CTL_HDS;
  108. PMU_CTL |= highdr_switch;
  109. }
  110. /*!
  111. \brief enable high-driver mode
  112. this bit set by software only when IRC16M or HXTAL used as system clock
  113. \param[in] none
  114. \param[out] none
  115. \retval none
  116. */
  117. void pmu_highdriver_mode_enable(void)
  118. {
  119. PMU_CTL |= PMU_CTL_HDEN;
  120. }
  121. /*!
  122. \brief disable high-driver mode
  123. \param[in] none
  124. \param[out] none
  125. \retval none
  126. */
  127. void pmu_highdriver_mode_disable(void)
  128. {
  129. PMU_CTL &= ~PMU_CTL_HDEN;
  130. }
  131. /*!
  132. \brief enable low-driver mode in deep-sleep mode
  133. \param[in] none
  134. \param[out] none
  135. \retval none
  136. */
  137. void pmu_lowdriver_mode_enable(void)
  138. {
  139. PMU_CTL |= PMU_CTL_LDEN;
  140. }
  141. /*!
  142. \brief disable low-driver mode in deep-sleep mode
  143. \param[in] none
  144. \param[out] none
  145. \retval none
  146. */
  147. void pmu_lowdriver_mode_disable(void)
  148. {
  149. PMU_CTL &= ~PMU_CTL_LDEN;
  150. }
  151. /*!
  152. \brief driver mode when use low power LDO
  153. \param[in] mode:
  154. \arg PMU_NORMALDR_LOWPWR: normal driver when use low power LDO
  155. \arg PMU_LOWDR_LOWPWR: low-driver mode enabled when LDEN is 11 and use low power LDO
  156. \param[out] none
  157. \retval none
  158. */
  159. void pmu_lowpower_driver_config(uint32_t mode)
  160. {
  161. PMU_CTL &= ~PMU_CTL_LDLP;
  162. PMU_CTL |= mode;
  163. }
  164. /*!
  165. \brief driver mode when use normal power LDO
  166. \param[in] mode:
  167. \arg PMU_NORMALDR_NORMALPWR: normal driver when use normal power LDO
  168. \arg PMU_LOWDR_NORMALPWR: low-driver mode enabled when LDEN is 11 and use normal power LDO
  169. \param[out] none
  170. \retval none
  171. */
  172. void pmu_normalpower_driver_config(uint32_t mode)
  173. {
  174. PMU_CTL &= ~PMU_CTL_LDNP;
  175. PMU_CTL |= mode;
  176. }
  177. /*!
  178. \brief PMU work in sleep mode
  179. \param[in] sleepmodecmd:
  180. \arg WFI_CMD: use WFI command
  181. \arg WFE_CMD: use WFE command
  182. \param[out] none
  183. \retval none
  184. */
  185. void pmu_to_sleepmode(uint8_t sleepmodecmd)
  186. {
  187. /* clear sleepdeep bit of Cortex-M4 system control register */
  188. SCB->SCR &= ~((uint32_t)SCB_SCR_SLEEPDEEP_Msk);
  189. /* select WFI or WFE command to enter sleep mode */
  190. if(WFI_CMD == sleepmodecmd) {
  191. __WFI();
  192. } else {
  193. __WFE();
  194. }
  195. }
  196. /*!
  197. \brief PMU work in deepsleep mode
  198. \param[in] ldo:
  199. only one parameter can be selected which is shown as below:
  200. \arg PMU_LDO_NORMAL: LDO work in normal power mode when pmu enter deepsleep mode
  201. \arg PMU_LDO_LOWPOWER: LDO work in low power mode when pmu enter deepsleep mode
  202. \param[in] lowdrive:
  203. only one parameter can be selected which is shown as below:
  204. \arg PMU_LOWDRIVER_ENABLE: low-driver mode enable in deep-sleep mode
  205. \arg PMU_LOWDRIVER_DISABLE: low-driver mode disable in deep-sleep mode
  206. \param[in] deepsleepmodecmd:
  207. only one parameter can be selected which is shown as below:
  208. \arg WFI_CMD: use WFI command
  209. \arg WFE_CMD: use WFE command
  210. \param[out] none
  211. \retval none
  212. */
  213. void pmu_to_deepsleepmode(uint32_t ldo, uint32_t lowdrive, uint8_t deepsleepmodecmd)
  214. {
  215. static uint32_t reg_snap[ 4 ];
  216. /* clear stbmod and ldolp bits */
  217. PMU_CTL &= ~((uint32_t)(PMU_CTL_STBMOD | PMU_CTL_LDOLP | PMU_CTL_LDEN | PMU_CTL_LDNP | PMU_CTL_LDLP));
  218. /* set ldolp bit according to pmu_ldo */
  219. PMU_CTL |= ldo;
  220. /* low drive mode config in deep-sleep mode */
  221. if(PMU_LOWDRIVER_ENABLE == lowdrive) {
  222. if(PMU_LDO_NORMAL == ldo) {
  223. PMU_CTL |= (uint32_t)(PMU_CTL_LDEN | PMU_CTL_LDNP);
  224. } else {
  225. PMU_CTL |= (uint32_t)(PMU_CTL_LDEN | PMU_CTL_LDLP);
  226. }
  227. }
  228. /* set sleepdeep bit of Cortex-M4 system control register */
  229. SCB->SCR |= SCB_SCR_SLEEPDEEP_Msk;
  230. reg_snap[ 0 ] = REG32(0xE000E010U);
  231. reg_snap[ 1 ] = REG32(0xE000E100U);
  232. reg_snap[ 2 ] = REG32(0xE000E104U);
  233. reg_snap[ 3 ] = REG32(0xE000E108U);
  234. REG32(0xE000E010U) &= 0x00010004U;
  235. REG32(0xE000E180U) = 0XFF7FF83DU;
  236. REG32(0xE000E184U) = 0XFFFFF8FFU;
  237. REG32(0xE000E188U) = 0xFFFFFFFFU;
  238. /* select WFI or WFE command to enter deepsleep mode */
  239. if(WFI_CMD == deepsleepmodecmd) {
  240. __WFI();
  241. } else {
  242. __SEV();
  243. __WFE();
  244. __WFE();
  245. }
  246. REG32(0xE000E010U) = reg_snap[ 0 ] ;
  247. REG32(0xE000E100U) = reg_snap[ 1 ] ;
  248. REG32(0xE000E104U) = reg_snap[ 2 ] ;
  249. REG32(0xE000E108U) = reg_snap[ 3 ] ;
  250. /* reset sleepdeep bit of Cortex-M4 system control register */
  251. SCB->SCR &= ~((uint32_t)SCB_SCR_SLEEPDEEP_Msk);
  252. }
  253. /*!
  254. \brief pmu work in standby mode
  255. \param[in] none
  256. \param[out] none
  257. \retval none
  258. */
  259. void pmu_to_standbymode(void)
  260. {
  261. /* set stbmod bit */
  262. PMU_CTL |= PMU_CTL_STBMOD;
  263. /* reset wakeup flag */
  264. PMU_CTL |= PMU_CTL_WURST;
  265. /* set sleepdeep bit of Cortex-M4 system control register */
  266. SCB->SCR |= SCB_SCR_SLEEPDEEP_Msk;
  267. REG32(0xE000E010U) &= 0x00010004U;
  268. REG32(0xE000E180U) = 0XFFFFFFF7U;
  269. REG32(0xE000E184U) = 0XFFFFFDFFU;
  270. REG32(0xE000E188U) = 0xFFFFFFFFU;
  271. /* select WFI or WFE command to enter standby mode */
  272. __WFI();
  273. }
  274. /*!
  275. \brief enable backup domain write
  276. \param[in] none
  277. \param[out] none
  278. \retval none
  279. */
  280. void pmu_backup_write_enable(void)
  281. {
  282. PMU_CTL |= PMU_CTL_BKPWEN;
  283. }
  284. /*!
  285. \brief disable backup domain write
  286. \param[in] none
  287. \param[out] none
  288. \retval none
  289. */
  290. void pmu_backup_write_disable(void)
  291. {
  292. PMU_CTL &= ~PMU_CTL_BKPWEN;
  293. }
  294. /*!
  295. \brief enable wakeup pin
  296. \param[in] none
  297. \param[out] none
  298. \retval none
  299. */
  300. void pmu_wakeup_pin_enable(void)
  301. {
  302. PMU_CS |= PMU_CS_WUPEN;
  303. }
  304. /*!
  305. \brief disable wakeup pin
  306. \param[in] none
  307. \param[out] none
  308. \retval none
  309. */
  310. void pmu_wakeup_pin_disable(void)
  311. {
  312. PMU_CS &= ~PMU_CS_WUPEN;
  313. }
  314. /*!
  315. \brief get flag state
  316. \param[in] flag:
  317. \arg PMU_FLAG_WAKEUP: wakeup flag
  318. \arg PMU_FLAG_STANDBY: standby flag
  319. \arg PMU_FLAG_LVD: lvd flag
  320. \arg PMU_FLAG_LDOVSRF: LDO voltage select ready flag
  321. \arg PMU_FLAG_HDRF: high-driver ready flag
  322. \arg PMU_FLAG_HDSRF: high-driver switch ready flag
  323. \arg PMU_FLAG_LDRF: low-driver mode ready flag
  324. \param[out] none
  325. \retval FlagStatus SET or RESET
  326. */
  327. FlagStatus pmu_flag_get(uint32_t flag)
  328. {
  329. if(PMU_CS & flag) {
  330. return SET;
  331. } else {
  332. return RESET;
  333. }
  334. }
  335. /*!
  336. \brief clear flag bit
  337. \param[in] flag:
  338. \arg PMU_FLAG_RESET_WAKEUP: reset wakeup flag
  339. \arg PMU_FLAG_RESET_STANDBY: reset standby flag
  340. \param[out] none
  341. \retval none
  342. */
  343. void pmu_flag_clear(uint32_t flag)
  344. {
  345. switch(flag) {
  346. case PMU_FLAG_RESET_WAKEUP:
  347. /* reset wakeup flag */
  348. PMU_CTL |= PMU_CTL_WURST;
  349. break;
  350. case PMU_FLAG_RESET_STANDBY:
  351. /* reset standby flag */
  352. PMU_CTL |= PMU_CTL_STBRST;
  353. break;
  354. default :
  355. break;
  356. }
  357. }