gd32f10x_pmu.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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. */
  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 "gd32f10x_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. only one parameter can be selected which is shown as below:
  49. \arg PMU_LVDT_0: voltage threshold is 2.2V
  50. \arg PMU_LVDT_1: voltage threshold is 2.3V
  51. \arg PMU_LVDT_2: voltage threshold is 2.4V
  52. \arg PMU_LVDT_3: voltage threshold is 2.5V
  53. \arg PMU_LVDT_4: voltage threshold is 2.6V
  54. \arg PMU_LVDT_5: voltage threshold is 2.7V
  55. \arg PMU_LVDT_6: voltage threshold is 2.8V
  56. \arg PMU_LVDT_7: voltage threshold is 2.9V
  57. \param[out] none
  58. \retval none
  59. */
  60. void pmu_lvd_select(uint32_t lvdt_n)
  61. {
  62. /* disable LVD */
  63. PMU_CTL &= ~PMU_CTL_LVDEN;
  64. /* clear LVDT bits */
  65. PMU_CTL &= ~PMU_CTL_LVDT;
  66. /* set LVDT bits according to lvdt_n */
  67. PMU_CTL |= lvdt_n;
  68. /* enable LVD */
  69. PMU_CTL |= PMU_CTL_LVDEN;
  70. }
  71. /*!
  72. \brief disable PMU lvd
  73. \param[in] none
  74. \param[out] none
  75. \retval none
  76. */
  77. void pmu_lvd_disable(void)
  78. {
  79. /* disable LVD */
  80. PMU_CTL &= ~PMU_CTL_LVDEN;
  81. }
  82. /*!
  83. \brief PMU work at sleep mode
  84. \param[in] sleepmodecmd:
  85. only one parameter can be selected which is shown as below:
  86. \arg WFI_CMD: use WFI command
  87. \arg WFE_CMD: use WFE command
  88. \param[out] none
  89. \retval none
  90. */
  91. void pmu_to_sleepmode(uint8_t sleepmodecmd)
  92. {
  93. /* clear sleepdeep bit of Cortex-M3 system control register */
  94. SCB->SCR &= ~((uint32_t)SCB_SCR_SLEEPDEEP_Msk);
  95. /* select WFI or WFE command to enter sleep mode */
  96. if(WFI_CMD == sleepmodecmd){
  97. __WFI();
  98. }else{
  99. __WFE();
  100. }
  101. }
  102. /*!
  103. \brief PMU work at deepsleep mode
  104. \param[in] ldo:
  105. only one parameter can be selected which is shown as below:
  106. \arg PMU_LDO_NORMAL: LDO work at normal power mode when pmu enter deepsleep mode
  107. \arg PMU_LDO_LOWPOWER: LDO work at low power mode when pmu enter deepsleep mode
  108. \param[in] deepsleepmodecmd:
  109. only one parameter can be selected which is shown as below:
  110. \arg WFI_CMD: use WFI command
  111. \arg WFE_CMD: use WFE command
  112. \param[out] none
  113. \retval none
  114. */
  115. void pmu_to_deepsleepmode(uint32_t ldo,uint8_t deepsleepmodecmd)
  116. {
  117. /* clear stbmod and ldolp bits */
  118. PMU_CTL &= ~((uint32_t)(PMU_CTL_STBMOD | PMU_CTL_LDOLP));
  119. /* set ldolp bit according to pmu_ldo */
  120. PMU_CTL |= ldo;
  121. /* set sleepdeep bit of Cortex-M3 system control register */
  122. SCB->SCR |= SCB_SCR_SLEEPDEEP_Msk;
  123. /* select WFI or WFE command to enter deepsleep mode */
  124. if(WFI_CMD == deepsleepmodecmd){
  125. __WFI();
  126. }else{
  127. __SEV();
  128. __WFE();
  129. __WFE();
  130. }
  131. /* reset sleepdeep bit of Cortex-M3 system control register */
  132. SCB->SCR &= ~((uint32_t)SCB_SCR_SLEEPDEEP_Msk);
  133. }
  134. /*!
  135. \brief pmu work at standby mode
  136. \param[in] standbymodecmd:
  137. only one parameter can be selected which is shown as below:
  138. \arg WFI_CMD: use WFI command
  139. \arg WFE_CMD: use WFE command
  140. \param[out] none
  141. \retval none
  142. */
  143. void pmu_to_standbymode(uint8_t standbymodecmd)
  144. {
  145. /* set sleepdeep bit of Cortex-M3 system control register */
  146. SCB->SCR |= SCB_SCR_SLEEPDEEP_Msk;
  147. /* set stbmod bit */
  148. PMU_CTL |= PMU_CTL_STBMOD;
  149. /* reset wakeup flag */
  150. PMU_CTL |= PMU_CTL_WURST;
  151. /* select WFI or WFE command to enter standby mode */
  152. if(WFI_CMD == standbymodecmd){
  153. __WFI();
  154. }else{
  155. __WFE();
  156. }
  157. }
  158. /*!
  159. \brief enable wakeup pin
  160. \param[in] none
  161. \param[out] none
  162. \retval none
  163. */
  164. void pmu_wakeup_pin_enable(void)
  165. {
  166. PMU_CS |= PMU_CS_WUPEN;
  167. }
  168. /*!
  169. \brief disable wakeup pin
  170. \param[in] none
  171. \param[out] none
  172. \retval none
  173. */
  174. void pmu_wakeup_pin_disable(void)
  175. {
  176. PMU_CS &= ~PMU_CS_WUPEN;
  177. }
  178. /*!
  179. \brief enable write access to the registers in backup domain
  180. \param[in] none
  181. \param[out] none
  182. \retval none
  183. */
  184. void pmu_backup_write_enable(void)
  185. {
  186. PMU_CTL |= PMU_CTL_BKPWEN;
  187. }
  188. /*!
  189. \brief disable write access to the registers in backup domain
  190. \param[in] none
  191. \param[out] none
  192. \retval none
  193. */
  194. void pmu_backup_write_disable(void)
  195. {
  196. PMU_CTL &= ~PMU_CTL_BKPWEN;
  197. }
  198. /*!
  199. \brief get flag state
  200. \param[in] flag:
  201. only one parameter can be selected which is shown as below:
  202. \arg PMU_FLAG_WAKEUP: wakeup flag
  203. \arg PMU_FLAG_STANDBY: standby flag
  204. \arg PMU_FLAG_LVD: lvd flag
  205. \param[out] none
  206. \retval FlagStatus SET or RESET
  207. */
  208. FlagStatus pmu_flag_get(uint32_t flag)
  209. {
  210. if(PMU_CS & flag){
  211. return SET;
  212. }else{
  213. return RESET;
  214. }
  215. }
  216. /*!
  217. \brief clear flag bit
  218. \param[in] flag_reset:
  219. only one parameter can be selected which is shown as below:
  220. \arg PMU_FLAG_RESET_WAKEUP: reset wakeup flag
  221. \arg PMU_FLAG_RESET_STANDBY: reset standby flag
  222. \param[out] none
  223. \retval none
  224. */
  225. void pmu_flag_clear(uint32_t flag_reset)
  226. {
  227. switch(flag_reset){
  228. case PMU_FLAG_RESET_WAKEUP:
  229. /* reset wakeup flag */
  230. PMU_CTL |= PMU_CTL_WURST;
  231. break;
  232. case PMU_FLAG_RESET_STANDBY:
  233. /* reset standby flag */
  234. PMU_CTL |= PMU_CTL_STBRST;
  235. break;
  236. default :
  237. break;
  238. }
  239. }