gd32vf103_pmu.c 7.5 KB

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