gd32f4xx_pmu.c 11 KB

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