gd32f3x0_pmu.c 12 KB

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