gd32f30x_pmu.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  1. /*!
  2. \file gd32f30x_pmu.c
  3. \brief PMU 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_pmu.h"
  33. /*!
  34. \brief reset PMU register
  35. \param[in] none
  36. \param[out] none
  37. \retval none
  38. */
  39. void pmu_deinit(void)
  40. {
  41. /* reset PMU */
  42. rcu_periph_reset_enable(RCU_PMURST);
  43. rcu_periph_reset_disable(RCU_PMURST);
  44. }
  45. /*!
  46. \brief select low voltage detector threshold
  47. \param[in] lvdt_n:
  48. \arg PMU_LVDT_0: voltage threshold is 2.1V
  49. \arg PMU_LVDT_1: voltage threshold is 2.3V
  50. \arg PMU_LVDT_2: voltage threshold is 2.4V
  51. \arg PMU_LVDT_3: voltage threshold is 2.6V
  52. \arg PMU_LVDT_4: voltage threshold is 2.7V
  53. \arg PMU_LVDT_5: voltage threshold is 2.9V
  54. \arg PMU_LVDT_6: voltage threshold is 3.0V
  55. \arg PMU_LVDT_7: voltage threshold is 3.1V
  56. \param[out] none
  57. \retval none
  58. */
  59. void pmu_lvd_select(uint32_t lvdt_n)
  60. {
  61. /* disable LVD */
  62. PMU_CTL &= ~PMU_CTL_LVDEN;
  63. /* clear LVDT bits */
  64. PMU_CTL &= ~PMU_CTL_LVDT;
  65. /* set LVDT bits according to lvdt_n */
  66. PMU_CTL |= lvdt_n;
  67. /* enable LVD */
  68. PMU_CTL |= PMU_CTL_LVDEN;
  69. }
  70. /*!
  71. \brief select LDO output voltage
  72. this bit set by software when the main PLL closed, before closing PLL, change the system clock to IRC16M or HXTAL
  73. \param[in] ldo_output:
  74. \arg PMU_LDOVS_LOW: LDO output voltage low mode
  75. \arg PMU_LDOVS_MID: LDO output voltage mid mode
  76. \arg PMU_LDOVS_HIGH: LDO output voltage high mode
  77. \param[out] none
  78. \retval none
  79. */
  80. void pmu_ldo_output_select(uint32_t ldo_output)
  81. {
  82. PMU_CTL &= ~PMU_CTL_LDOVS;
  83. PMU_CTL |= ldo_output;
  84. }
  85. /*!
  86. \brief disable PMU lvd
  87. \param[in] none
  88. \param[out] none
  89. \retval none
  90. */
  91. void pmu_lvd_disable(void)
  92. {
  93. /* disable LVD */
  94. PMU_CTL &= ~PMU_CTL_LVDEN;
  95. }
  96. /*!
  97. \brief switch high-driver mode
  98. this bit set by software only when IRC16M or HXTAL used as system clock
  99. \param[in] highdr_switch:
  100. \arg PMU_HIGHDR_SWITCH_NONE: disable high-driver mode switch
  101. \arg PMU_HIGHDR_SWITCH_EN: enable high-driver mode switch
  102. \param[out] none
  103. \retval none
  104. */
  105. void pmu_highdriver_switch_select(uint32_t highdr_switch)
  106. {
  107. /* wait for HDRF flag set */
  108. while(SET != pmu_flag_get(PMU_FLAG_HDRF)) {
  109. }
  110. PMU_CTL &= ~PMU_CTL_HDS;
  111. PMU_CTL |= highdr_switch;
  112. }
  113. /*!
  114. \brief enable high-driver mode
  115. this bit set by software only when IRC16M or HXTAL used as system clock
  116. \param[in] none
  117. \param[out] none
  118. \retval none
  119. */
  120. void pmu_highdriver_mode_enable(void)
  121. {
  122. PMU_CTL |= PMU_CTL_HDEN;
  123. }
  124. /*!
  125. \brief disable high-driver mode
  126. \param[in] none
  127. \param[out] none
  128. \retval none
  129. */
  130. void pmu_highdriver_mode_disable(void)
  131. {
  132. PMU_CTL &= ~PMU_CTL_HDEN;
  133. }
  134. /*!
  135. \brief enable low-driver mode in deep-sleep mode
  136. \param[in] none
  137. \param[out] none
  138. \retval none
  139. */
  140. void pmu_lowdriver_mode_enable(void)
  141. {
  142. PMU_CTL |= PMU_CTL_LDEN;
  143. }
  144. /*!
  145. \brief disable low-driver mode in deep-sleep mode
  146. \param[in] none
  147. \param[out] none
  148. \retval none
  149. */
  150. void pmu_lowdriver_mode_disable(void)
  151. {
  152. PMU_CTL &= ~PMU_CTL_LDEN;
  153. }
  154. /*!
  155. \brief driver mode when use low power LDO
  156. \param[in] mode:
  157. \arg PMU_NORMALDR_LOWPWR: normal driver when use low power LDO
  158. \arg PMU_LOWDR_LOWPWR: low-driver mode enabled when LDEN is 11 and use low power LDO
  159. \param[out] none
  160. \retval none
  161. */
  162. void pmu_lowpower_driver_config(uint32_t mode)
  163. {
  164. PMU_CTL &= ~PMU_CTL_LDLP;
  165. PMU_CTL |= mode;
  166. }
  167. /*!
  168. \brief driver mode when use normal power LDO
  169. \param[in] mode:
  170. \arg PMU_NORMALDR_NORMALPWR: normal driver when use normal power LDO
  171. \arg PMU_LOWDR_NORMALPWR: low-driver mode enabled when LDEN is 11 and use normal power LDO
  172. \param[out] none
  173. \retval none
  174. */
  175. void pmu_normalpower_driver_config(uint32_t mode)
  176. {
  177. PMU_CTL &= ~PMU_CTL_LDNP;
  178. PMU_CTL |= mode;
  179. }
  180. /*!
  181. \brief PMU work in sleep mode
  182. \param[in] sleepmodecmd:
  183. \arg WFI_CMD: use WFI command
  184. \arg WFE_CMD: use WFE command
  185. \param[out] none
  186. \retval none
  187. */
  188. void pmu_to_sleepmode(uint8_t sleepmodecmd)
  189. {
  190. /* clear sleepdeep bit of Cortex-M4 system control register */
  191. SCB->SCR &= ~((uint32_t)SCB_SCR_SLEEPDEEP_Msk);
  192. /* select WFI or WFE command to enter sleep mode */
  193. if(WFI_CMD == sleepmodecmd) {
  194. __WFI();
  195. } else {
  196. __WFE();
  197. }
  198. }
  199. /*!
  200. \brief PMU work in deepsleep mode
  201. \param[in] ldo:
  202. only one parameter can be selected which is shown as below:
  203. \arg PMU_LDO_NORMAL: LDO work in normal power mode when pmu enter deepsleep mode
  204. \arg PMU_LDO_LOWPOWER: LDO work in low power mode when pmu enter deepsleep mode
  205. \param[in] lowdrive:
  206. only one parameter can be selected which is shown as below:
  207. \arg PMU_LOWDRIVER_ENABLE: low-driver mode enable in deep-sleep mode
  208. \arg PMU_LOWDRIVER_DISABLE: low-driver mode disable in deep-sleep mode
  209. \param[in] deepsleepmodecmd:
  210. only one parameter can be selected which is shown as below:
  211. \arg WFI_CMD: use WFI command
  212. \arg WFE_CMD: use WFE command
  213. \param[out] none
  214. \retval none
  215. */
  216. void pmu_to_deepsleepmode(uint32_t ldo, uint32_t lowdrive, uint8_t deepsleepmodecmd)
  217. {
  218. static uint32_t reg_snap[ 4 ];
  219. /* clear stbmod and ldolp bits */
  220. PMU_CTL &= ~((uint32_t)(PMU_CTL_STBMOD | PMU_CTL_LDOLP | PMU_CTL_LDEN | PMU_CTL_LDNP | PMU_CTL_LDLP));
  221. /* set ldolp bit according to pmu_ldo */
  222. PMU_CTL |= ldo;
  223. /* low drive mode config in deep-sleep mode */
  224. if(PMU_LOWDRIVER_ENABLE == lowdrive) {
  225. if(PMU_LDO_NORMAL == ldo) {
  226. PMU_CTL |= (uint32_t)(PMU_CTL_LDEN | PMU_CTL_LDNP);
  227. } else {
  228. PMU_CTL |= (uint32_t)(PMU_CTL_LDEN | PMU_CTL_LDLP);
  229. }
  230. }
  231. /* set sleepdeep bit of Cortex-M4 system control register */
  232. SCB->SCR |= SCB_SCR_SLEEPDEEP_Msk;
  233. reg_snap[ 0 ] = REG32(0xE000E010U);
  234. reg_snap[ 1 ] = REG32(0xE000E100U);
  235. reg_snap[ 2 ] = REG32(0xE000E104U);
  236. reg_snap[ 3 ] = REG32(0xE000E108U);
  237. REG32(0xE000E010U) &= 0x00010004U;
  238. REG32(0xE000E180U) = 0XFF7FF83DU;
  239. REG32(0xE000E184U) = 0XFFFFF8FFU;
  240. REG32(0xE000E188U) = 0xFFFFFFFFU;
  241. /* select WFI or WFE command to enter deepsleep mode */
  242. if(WFI_CMD == deepsleepmodecmd) {
  243. __WFI();
  244. } else {
  245. __SEV();
  246. __WFE();
  247. __WFE();
  248. }
  249. REG32(0xE000E010U) = reg_snap[ 0 ] ;
  250. REG32(0xE000E100U) = reg_snap[ 1 ] ;
  251. REG32(0xE000E104U) = reg_snap[ 2 ] ;
  252. REG32(0xE000E108U) = reg_snap[ 3 ] ;
  253. /* reset sleepdeep bit of Cortex-M4 system control register */
  254. SCB->SCR &= ~((uint32_t)SCB_SCR_SLEEPDEEP_Msk);
  255. }
  256. /*!
  257. \brief pmu work in standby mode
  258. \param[in] none
  259. \param[out] none
  260. \retval none
  261. */
  262. void pmu_to_standbymode(void)
  263. {
  264. /* set stbmod bit */
  265. PMU_CTL |= PMU_CTL_STBMOD;
  266. /* reset wakeup flag */
  267. PMU_CTL |= PMU_CTL_WURST;
  268. /* set sleepdeep bit of Cortex-M4 system control register */
  269. SCB->SCR |= SCB_SCR_SLEEPDEEP_Msk;
  270. REG32(0xE000E010U) &= 0x00010004U;
  271. REG32(0xE000E180U) = 0XFFFFFFF7U;
  272. REG32(0xE000E184U) = 0XFFFFFDFFU;
  273. REG32(0xE000E188U) = 0xFFFFFFFFU;
  274. /* select WFI or WFE command to enter standby mode */
  275. __WFI();
  276. }
  277. /*!
  278. \brief enable backup domain write
  279. \param[in] none
  280. \param[out] none
  281. \retval none
  282. */
  283. void pmu_backup_write_enable(void)
  284. {
  285. PMU_CTL |= PMU_CTL_BKPWEN;
  286. }
  287. /*!
  288. \brief disable backup domain write
  289. \param[in] none
  290. \param[out] none
  291. \retval none
  292. */
  293. void pmu_backup_write_disable(void)
  294. {
  295. PMU_CTL &= ~PMU_CTL_BKPWEN;
  296. }
  297. /*!
  298. \brief enable wakeup pin
  299. \param[in] none
  300. \param[out] none
  301. \retval none
  302. */
  303. void pmu_wakeup_pin_enable(void)
  304. {
  305. PMU_CS |= PMU_CS_WUPEN;
  306. }
  307. /*!
  308. \brief disable wakeup pin
  309. \param[in] none
  310. \param[out] none
  311. \retval none
  312. */
  313. void pmu_wakeup_pin_disable(void)
  314. {
  315. PMU_CS &= ~PMU_CS_WUPEN;
  316. }
  317. /*!
  318. \brief get flag state
  319. \param[in] flag:
  320. \arg PMU_FLAG_WAKEUP: wakeup flag
  321. \arg PMU_FLAG_STANDBY: standby flag
  322. \arg PMU_FLAG_LVD: lvd flag
  323. \arg PMU_FLAG_LDOVSRF: LDO voltage select ready flag
  324. \arg PMU_FLAG_HDRF: high-driver ready flag
  325. \arg PMU_FLAG_HDSRF: high-driver switch ready flag
  326. \arg PMU_FLAG_LDRF: low-driver mode ready flag
  327. \param[out] none
  328. \retval FlagStatus SET or RESET
  329. */
  330. FlagStatus pmu_flag_get(uint32_t flag)
  331. {
  332. if(PMU_CS & flag) {
  333. return SET;
  334. } else {
  335. return RESET;
  336. }
  337. }
  338. /*!
  339. \brief clear flag bit
  340. \param[in] flag:
  341. \arg PMU_FLAG_RESET_WAKEUP: reset wakeup flag
  342. \arg PMU_FLAG_RESET_STANDBY: reset standby flag
  343. \param[out] none
  344. \retval none
  345. */
  346. void pmu_flag_clear(uint32_t flag)
  347. {
  348. switch(flag) {
  349. case PMU_FLAG_RESET_WAKEUP:
  350. /* reset wakeup flag */
  351. PMU_CTL |= PMU_CTL_WURST;
  352. break;
  353. case PMU_FLAG_RESET_STANDBY:
  354. /* reset standby flag */
  355. PMU_CTL |= PMU_CTL_STBRST;
  356. break;
  357. default :
  358. break;
  359. }
  360. }