gd32f10x_pmu.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. /*!
  2. \file gd32f10x_pmu.c
  3. \brief PMU driver
  4. \version 2014-12-26, V1.0.0, firmware for GD32F10x
  5. \version 2017-06-20, V2.0.0, firmware for GD32F10x
  6. \version 2018-07-31, V2.1.0, firmware for GD32F10x
  7. \version 2019-11-26, V2.1.1, firmware for GD32F10x
  8. \version 2020-09-30, V2.2.0, firmware for GD32F10x
  9. */
  10. /*
  11. Copyright (c) 2020, GigaDevice Semiconductor Inc.
  12. Redistribution and use in source and binary forms, with or without modification,
  13. are permitted provided that the following conditions are met:
  14. 1. Redistributions of source code must retain the above copyright notice, this
  15. list of conditions and the following disclaimer.
  16. 2. Redistributions in binary form must reproduce the above copyright notice,
  17. this list of conditions and the following disclaimer in the documentation
  18. and/or other materials provided with the distribution.
  19. 3. Neither the name of the copyright holder nor the names of its contributors
  20. may be used to endorse or promote products derived from this software without
  21. specific prior written permission.
  22. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  23. AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  24. WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  25. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
  26. INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  27. NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  28. PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  29. WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  30. ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
  31. OF SUCH DAMAGE.
  32. */
  33. #include "gd32f10x_pmu.h"
  34. /*!
  35. \brief reset PMU register
  36. \param[in] none
  37. \param[out] none
  38. \retval none
  39. */
  40. void pmu_deinit(void)
  41. {
  42. /* reset PMU */
  43. rcu_periph_reset_enable(RCU_PMURST);
  44. rcu_periph_reset_disable(RCU_PMURST);
  45. }
  46. /*!
  47. \brief select low voltage detector threshold
  48. \param[in] lvdt_n:
  49. only one parameter can be selected which is shown as below:
  50. \arg PMU_LVDT_0: voltage threshold is 2.2V
  51. \arg PMU_LVDT_1: voltage threshold is 2.3V
  52. \arg PMU_LVDT_2: voltage threshold is 2.4V
  53. \arg PMU_LVDT_3: voltage threshold is 2.5V
  54. \arg PMU_LVDT_4: voltage threshold is 2.6V
  55. \arg PMU_LVDT_5: voltage threshold is 2.7V
  56. \arg PMU_LVDT_6: voltage threshold is 2.8V
  57. \arg PMU_LVDT_7: voltage threshold is 2.9V
  58. \param[out] none
  59. \retval none
  60. */
  61. void pmu_lvd_select(uint32_t lvdt_n)
  62. {
  63. /* disable LVD */
  64. PMU_CTL &= ~PMU_CTL_LVDEN;
  65. /* clear LVDT bits */
  66. PMU_CTL &= ~PMU_CTL_LVDT;
  67. /* set LVDT bits according to lvdt_n */
  68. PMU_CTL |= lvdt_n;
  69. /* enable LVD */
  70. PMU_CTL |= PMU_CTL_LVDEN;
  71. }
  72. /*!
  73. \brief disable PMU lvd
  74. \param[in] none
  75. \param[out] none
  76. \retval none
  77. */
  78. void pmu_lvd_disable(void)
  79. {
  80. /* disable LVD */
  81. PMU_CTL &= ~PMU_CTL_LVDEN;
  82. }
  83. /*!
  84. \brief PMU work in sleep mode
  85. \param[in] sleepmodecmd:
  86. only one parameter can be selected which is shown as below:
  87. \arg WFI_CMD: use WFI command
  88. \arg WFE_CMD: use WFE command
  89. \param[out] none
  90. \retval none
  91. */
  92. void pmu_to_sleepmode(uint8_t sleepmodecmd)
  93. {
  94. /* clear sleepdeep bit of Cortex-M3 system control register */
  95. SCB->SCR &= ~((uint32_t)SCB_SCR_SLEEPDEEP_Msk);
  96. /* select WFI or WFE command to enter sleep mode */
  97. if(WFI_CMD == sleepmodecmd){
  98. __WFI();
  99. }else{
  100. __WFE();
  101. }
  102. }
  103. /*!
  104. \brief PMU work in deepsleep mode
  105. \param[in] ldo:
  106. only one parameter can be selected which is shown as below:
  107. \arg PMU_LDO_NORMAL: LDO work in normal power mode when pmu enter deepsleep mode
  108. \arg PMU_LDO_LOWPOWER: LDO work in low power mode when pmu enter deepsleep mode
  109. \param[in] deepsleepmodecmd:
  110. only one parameter can be selected which is shown as below:
  111. \arg WFI_CMD: use WFI command
  112. \arg WFE_CMD: use WFE command
  113. \param[out] none
  114. \retval none
  115. */
  116. void pmu_to_deepsleepmode(uint32_t ldo,uint8_t deepsleepmodecmd)
  117. {
  118. static uint32_t reg_snap[ 4 ];
  119. /* clear stbmod and ldolp bits */
  120. PMU_CTL &= ~((uint32_t)(PMU_CTL_STBMOD | PMU_CTL_LDOLP));
  121. /* set ldolp bit according to pmu_ldo */
  122. PMU_CTL |= ldo;
  123. /* set sleepdeep bit of Cortex-M3 system control register */
  124. SCB->SCR |= SCB_SCR_SLEEPDEEP_Msk;
  125. reg_snap[0] = REG32(0xE000E010U);
  126. reg_snap[1] = REG32(0xE000E100U);
  127. reg_snap[2] = REG32(0xE000E104U);
  128. reg_snap[3] = REG32(0xE000E108U);
  129. REG32(0xE000E010U) &= 0x00010004U;
  130. REG32(0xE000E180U) = 0XFF7FF83DU;
  131. REG32(0xE000E184U) = 0XBFFFF8FFU;
  132. REG32(0xE000E188U) = 0xFFFFFFFFU;
  133. /* select WFI or WFE command to enter deepsleep mode */
  134. if(WFI_CMD == deepsleepmodecmd){
  135. __WFI();
  136. }else{
  137. __SEV();
  138. __WFE();
  139. __WFE();
  140. }
  141. REG32(0xE000E010U) = reg_snap[0] ;
  142. REG32(0xE000E100U) = reg_snap[1] ;
  143. REG32(0xE000E104U) = reg_snap[2] ;
  144. REG32(0xE000E108U) = reg_snap[3] ;
  145. /* reset sleepdeep bit of Cortex-M3 system control register */
  146. SCB->SCR &= ~((uint32_t)SCB_SCR_SLEEPDEEP_Msk);
  147. }
  148. /*!
  149. \brief pmu work in standby mode
  150. \param[in] none
  151. \param[out] none
  152. \retval none
  153. */
  154. void pmu_to_standbymode(void)
  155. {
  156. /* set stbmod bit */
  157. PMU_CTL |= PMU_CTL_STBMOD;
  158. /* reset wakeup flag */
  159. PMU_CTL |= PMU_CTL_WURST;
  160. /* set sleepdeep bit of Cortex-M3 system control register */
  161. SCB->SCR |= SCB_SCR_SLEEPDEEP_Msk;
  162. REG32(0xE000E010U) &= 0x00010004U;
  163. REG32(0xE000E180U) = 0XFFFFFFF7U;
  164. REG32(0xE000E184U) = 0XFFFFFDFFU;
  165. REG32(0xE000E188U) = 0xFFFFFFFFU;
  166. /* select WFI command to enter standby mode */
  167. __WFI();
  168. }
  169. /*!
  170. \brief enable wakeup pin
  171. \param[in] none
  172. \param[out] none
  173. \retval none
  174. */
  175. void pmu_wakeup_pin_enable(void)
  176. {
  177. PMU_CS |= PMU_CS_WUPEN;
  178. }
  179. /*!
  180. \brief disable wakeup pin
  181. \param[in] none
  182. \param[out] none
  183. \retval none
  184. */
  185. void pmu_wakeup_pin_disable(void)
  186. {
  187. PMU_CS &= ~PMU_CS_WUPEN;
  188. }
  189. /*!
  190. \brief enable write access to the registers in backup domain
  191. \param[in] none
  192. \param[out] none
  193. \retval none
  194. */
  195. void pmu_backup_write_enable(void)
  196. {
  197. PMU_CTL |= PMU_CTL_BKPWEN;
  198. }
  199. /*!
  200. \brief disable write access to the registers in backup domain
  201. \param[in] none
  202. \param[out] none
  203. \retval none
  204. */
  205. void pmu_backup_write_disable(void)
  206. {
  207. PMU_CTL &= ~PMU_CTL_BKPWEN;
  208. }
  209. /*!
  210. \brief get flag state
  211. \param[in] flag:
  212. only one parameter can be selected which is shown as below:
  213. \arg PMU_FLAG_WAKEUP: wakeup flag
  214. \arg PMU_FLAG_STANDBY: standby flag
  215. \arg PMU_FLAG_LVD: lvd flag
  216. \param[out] none
  217. \retval FlagStatus SET or RESET
  218. */
  219. FlagStatus pmu_flag_get(uint32_t flag)
  220. {
  221. if(PMU_CS & flag){
  222. return SET;
  223. }else{
  224. return RESET;
  225. }
  226. }
  227. /*!
  228. \brief clear flag bit
  229. \param[in] flag:
  230. only one parameter can be selected which is shown as below:
  231. \arg PMU_FLAG_RESET_WAKEUP: reset wakeup flag
  232. \arg PMU_FLAG_RESET_STANDBY: reset standby flag
  233. \param[out] none
  234. \retval none
  235. */
  236. void pmu_flag_clear(uint32_t flag)
  237. {
  238. switch(flag){
  239. case PMU_FLAG_RESET_WAKEUP:
  240. /* reset wakeup flag */
  241. PMU_CTL |= PMU_CTL_WURST;
  242. break;
  243. case PMU_FLAG_RESET_STANDBY:
  244. /* reset standby flag */
  245. PMU_CTL |= PMU_CTL_STBRST;
  246. break;
  247. default :
  248. break;
  249. }
  250. }