gd32f30x_pmu.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  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. */
  8. /*
  9. Copyright (c) 2018, GigaDevice Semiconductor Inc.
  10. All rights reserved.
  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 at 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 at deepsleep mode
  201. \param[in] ldo
  202. \arg PMU_LDO_NORMAL: LDO normal work when pmu enter deepsleep mode
  203. \arg PMU_LDO_LOWPOWER: LDO work at low power mode when pmu enter deepsleep mode
  204. \param[in] deepsleepmodecmd:
  205. \arg WFI_CMD: use WFI command
  206. \arg WFE_CMD: use WFE command
  207. \param[out] none
  208. \retval none
  209. */
  210. void pmu_to_deepsleepmode(uint32_t ldo,uint8_t deepsleepmodecmd)
  211. {
  212. static uint32_t reg_snap[ 4 ];
  213. /* clear stbmod and ldolp bits */
  214. PMU_CTL &= ~((uint32_t)(PMU_CTL_STBMOD | PMU_CTL_LDOLP));
  215. /* set ldolp bit according to pmu_ldo */
  216. PMU_CTL |= ldo;
  217. /* set sleepdeep bit of Cortex-M4 system control register */
  218. SCB->SCR |= SCB_SCR_SLEEPDEEP_Msk;
  219. reg_snap[ 0 ] = REG32( 0xE000E010U );
  220. reg_snap[ 1 ] = REG32( 0xE000E100U );
  221. reg_snap[ 2 ] = REG32( 0xE000E104U );
  222. reg_snap[ 3 ] = REG32( 0xE000E108U );
  223. REG32( 0xE000E010U ) &= 0x00010004U;
  224. REG32( 0xE000E180U ) = 0XFF7FF83DU;
  225. REG32( 0xE000E184U ) = 0XBFFFF8FFU;
  226. REG32( 0xE000E188U ) = 0xFFFFFFFFU;
  227. /* select WFI or WFE command to enter deepsleep mode */
  228. if(WFI_CMD == deepsleepmodecmd){
  229. __WFI();
  230. }else{
  231. __SEV();
  232. __WFE();
  233. __WFE();
  234. }
  235. REG32( 0xE000E010U ) = reg_snap[ 0 ] ;
  236. REG32( 0xE000E100U ) = reg_snap[ 1 ] ;
  237. REG32( 0xE000E104U ) = reg_snap[ 2 ] ;
  238. REG32( 0xE000E108U ) = reg_snap[ 3 ] ;
  239. /* reset sleepdeep bit of Cortex-M4 system control register */
  240. SCB->SCR &= ~((uint32_t)SCB_SCR_SLEEPDEEP_Msk);
  241. }
  242. /*!
  243. \brief pmu work at standby mode
  244. \param[in] standbymodecmd:
  245. \arg WFI_CMD: use WFI command
  246. \arg WFE_CMD: use WFE command
  247. \param[out] none
  248. \retval none
  249. */
  250. void pmu_to_standbymode(uint8_t standbymodecmd)
  251. {
  252. /* set sleepdeep bit of Cortex-M4 system control register */
  253. SCB->SCR |= SCB_SCR_SLEEPDEEP_Msk;
  254. /* set stbmod bit */
  255. PMU_CTL |= PMU_CTL_STBMOD;
  256. /* reset wakeup flag */
  257. PMU_CTL |= PMU_CTL_WURST;
  258. /* select WFI or WFE command to enter standby mode */
  259. if(WFI_CMD == standbymodecmd){
  260. __WFI();
  261. }else{
  262. __WFE();
  263. }
  264. }
  265. /*!
  266. \brief enable backup domain write
  267. \param[in] none
  268. \param[out] none
  269. \retval none
  270. */
  271. void pmu_backup_write_enable(void)
  272. {
  273. PMU_CTL |= PMU_CTL_BKPWEN;
  274. }
  275. /*!
  276. \brief disable backup domain write
  277. \param[in] none
  278. \param[out] none
  279. \retval none
  280. */
  281. void pmu_backup_write_disable(void)
  282. {
  283. PMU_CTL &= ~PMU_CTL_BKPWEN;
  284. }
  285. /*!
  286. \brief enable wakeup pin
  287. \param[in] none
  288. \param[out] none
  289. \retval none
  290. */
  291. void pmu_wakeup_pin_enable(void)
  292. {
  293. PMU_CS |= PMU_CS_WUPEN;
  294. }
  295. /*!
  296. \brief disable wakeup pin
  297. \param[in] none
  298. \param[out] none
  299. \retval none
  300. */
  301. void pmu_wakeup_pin_disable(void)
  302. {
  303. PMU_CS &= ~PMU_CS_WUPEN;
  304. }
  305. /*!
  306. \brief clear flag bit
  307. \param[in] flag_reset:
  308. \arg PMU_FLAG_RESET_WAKEUP: reset wakeup flag
  309. \arg PMU_FLAG_RESET_STANDBY: reset standby flag
  310. \param[out] none
  311. \retval none
  312. */
  313. void pmu_flag_clear(uint32_t flag_reset)
  314. {
  315. switch(flag_reset){
  316. case PMU_FLAG_RESET_WAKEUP:
  317. /* reset wakeup flag */
  318. PMU_CTL |= PMU_CTL_WURST;
  319. break;
  320. case PMU_FLAG_RESET_STANDBY:
  321. /* reset standby flag */
  322. PMU_CTL |= PMU_CTL_STBRST;
  323. break;
  324. default :
  325. break;
  326. }
  327. }
  328. /*!
  329. \brief get flag state
  330. \param[in] flag:
  331. \arg PMU_FLAG_WAKEUP: wakeup flag
  332. \arg PMU_FLAG_STANDBY: standby flag
  333. \arg PMU_FLAG_LVD: lvd flag
  334. \arg PMU_FLAG_LDOVSRF: LDO voltage select ready flag
  335. \arg PMU_FLAG_HDRF: high-driver ready flag
  336. \arg PMU_FLAG_HDSRF: high-driver switch ready flag
  337. \arg PMU_FLAG_LDRF: low-driver mode ready flag
  338. \param[out] none
  339. \retval FlagStatus SET or RESET
  340. */
  341. FlagStatus pmu_flag_get(uint32_t flag)
  342. {
  343. if(PMU_CS & flag){
  344. return SET;
  345. }else{
  346. return RESET;
  347. }
  348. }