apm32e10x_pmu.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. /*!
  2. * @file apm32e10x_pmu.c
  3. *
  4. * @brief This file provides all the PMU firmware functions.
  5. *
  6. * @version V1.0.2
  7. *
  8. * @date 2022-12-31
  9. *
  10. * @attention
  11. *
  12. * Copyright (C) 2021-2023 Geehy Semiconductor
  13. *
  14. * You may not use this file except in compliance with the
  15. * GEEHY COPYRIGHT NOTICE (GEEHY SOFTWARE PACKAGE LICENSE).
  16. *
  17. * The program is only for reference, which is distributed in the hope
  18. * that it will be useful and instructional for customers to develop
  19. * their software. Unless required by applicable law or agreed to in
  20. * writing, the program is distributed on an "AS IS" BASIS, WITHOUT
  21. * ANY WARRANTY OR CONDITIONS OF ANY KIND, either express or implied.
  22. * See the GEEHY SOFTWARE PACKAGE LICENSE for the governing permissions
  23. * and limitations under the License.
  24. */
  25. #include "apm32e10x_pmu.h"
  26. #include "apm32e10x_rcm.h"
  27. /** @addtogroup APM32E10x_StdPeriphDriver
  28. @{
  29. */
  30. /** @addtogroup PMU_Driver
  31. * @brief PMU driver modules
  32. @{
  33. */
  34. /** @defgroup PMU_Functions Functions
  35. @{
  36. */
  37. /*!
  38. * @brief Reset the PMU peripheral register.
  39. *
  40. * @param None
  41. *
  42. * @retval None
  43. */
  44. void PMU_Reset(void)
  45. {
  46. RCM_EnableAPB1PeriphReset(RCM_APB1_PERIPH_PMU);
  47. RCM_DisableAPB1PeriphReset(RCM_APB1_PERIPH_PMU);
  48. }
  49. /*!
  50. * @brief Enables access to the RTC and backup registers.
  51. *
  52. * @param None
  53. *
  54. * @retval None
  55. */
  56. void PMU_EnableBackupAccess(void)
  57. {
  58. PMU->CTRL_B.BPWEN = ENABLE ;
  59. }
  60. /*!
  61. * @brief Disables access to the RTC and backup registers.
  62. *
  63. * @param None
  64. *
  65. * @retval None
  66. */
  67. void PMU_DisableBackupAccess(void)
  68. {
  69. PMU->CTRL_B.BPWEN = DISABLE;
  70. }
  71. /*!
  72. * @brief Enables the Power Voltage Detector(PVD).
  73. *
  74. * @param None
  75. *
  76. * @retval None
  77. */
  78. void PMU_EnablePVD(void)
  79. {
  80. PMU->CTRL_B.PVDEN = ENABLE;
  81. }
  82. /*!
  83. * @brief Disables the Power Voltage Detector(PVD).
  84. *
  85. * @param None
  86. *
  87. * @retval None
  88. */
  89. void PMU_DisablePVD(void)
  90. {
  91. PMU->CTRL_B.PVDEN = DISABLE;
  92. }
  93. /*!
  94. * @brief Configure a voltage threshold detected by a power supply voltage detector (PVD).
  95. *
  96. * @param level: specifies the PVD detection level
  97. * This parameter can be one of the following values:
  98. * @arg PMU_PVD_LEVEL_2V2 : Config PVD detection level to 2.2V
  99. * @arg PMU_PVD_LEVEL_2V3 : Config PVD detection level to 2.3V
  100. * @arg PMU_PVD_LEVEL_2V4 : Config PVD detection level to 2.4V
  101. * @arg PMU_PVD_LEVEL_2V5 : Config PVD detection level to 2.5V
  102. * @arg PMU_PVD_LEVEL_2V6 : Config PVD detection level to 2.6V
  103. * @arg PMU_PVD_LEVEL_2V7 : Config PVD detection level to 2.7V
  104. * @arg PMU_PVD_LEVEL_2V8 : Config PVD detection level to 2.8V
  105. * @arg PMU_PVD_LEVEL_2V9 : Config PVD detection level to 2.9V
  106. *
  107. * @retval None
  108. */
  109. void PMU_ConfigPVDLevel(PMU_PVD_LEVEL_T level)
  110. {
  111. /* Clear PLS[7:5] bits */
  112. PMU->CTRL_B.PLSEL = 0x0000;
  113. /* Store the new value */
  114. PMU->CTRL_B.PLSEL = level;
  115. }
  116. /*!
  117. * @brief Enables the WakeUp Pin functionality.
  118. *
  119. * @param None
  120. *
  121. * @retval None
  122. */
  123. void PMU_EnableWakeUpPin(void)
  124. {
  125. PMU->CSTS_B.WKUPCFG = ENABLE ;
  126. }
  127. /*!
  128. * @brief Diaables the WakeUp Pin functionality.
  129. *
  130. * @param None
  131. *
  132. * @retval None
  133. */
  134. void PMU_DisableWakeUpPin(void)
  135. {
  136. PMU->CSTS_B.WKUPCFG = DISABLE ;
  137. }
  138. /*!
  139. * @brief Enters STOP mode.
  140. *
  141. * @param regulator: specifies the regulator state in STOP mode.
  142. * This parameter can be one of the following values:
  143. * @arg PMU_REGULATOR_ON : STOP mode with regulator ON
  144. * @arg PMU_REGULATOR_LOWPOWER: STOP mode with regulator in low power mode
  145. *
  146. * @param entry: specifies if STOP mode in entered with WFI or WFE instruction.
  147. * This parameter can be one of the following values:
  148. * @arg PMU_STOP_ENTRY_WFI: Enter STOP mode with WFI instruction
  149. * @arg PMU_STOP_ENTRY_WFE: Enter STOP mode with WFE instruction
  150. *
  151. * @retval None
  152. */
  153. void PMU_EnterSTOPMode(PMU_REGULATOR_T regulator, PMU_STOP_ENTRY_T entry)
  154. {
  155. /** Clear PDDSCFG and LPDSCFG bits */
  156. PMU->CTRL_B.PDDSCFG = 0x00;
  157. PMU->CTRL_B.LPDSCFG = 0x00;
  158. /** Set LPDSCFG bit according to regulator value */
  159. PMU->CTRL_B.LPDSCFG = regulator;
  160. /** Set Cortex System Control Register */
  161. SCB->SCR |= (uint32_t )0x04;
  162. /** Select STOP mode entry*/
  163. if(entry == PMU_STOP_ENTRY_WFI)
  164. {
  165. /** Request Wait For Interrupt */
  166. __WFI();
  167. }
  168. else
  169. {
  170. /** Request Wait For Event */
  171. __WFE();
  172. }
  173. /** Reset SLEEPDEEP bit of Cortex System Control Register */
  174. SCB->SCR &= (uint32_t)~((uint32_t)0x04);
  175. }
  176. /*!
  177. * @brief Enters STANDBY mode.
  178. *
  179. * @param None
  180. *
  181. * @retval None
  182. */
  183. void PMU_EnterSTANDBYMode(void)
  184. {
  185. /* Clear Wake-up flag */
  186. PMU->CTRL_B.WUFLGCLR = BIT_SET;
  187. /* Select STANDBY mode */
  188. PMU->CTRL_B.PDDSCFG = BIT_SET;
  189. /* Set Cortex System Control Register */
  190. SCB->SCR |= (uint32_t )0x04;
  191. #if defined ( __CC_ARM )
  192. __force_stores();
  193. #endif
  194. /* Request Wait For Interrupt */
  195. __WFI();
  196. }
  197. /*!
  198. * @brief Read the specified PWR flag is set or not.
  199. *
  200. * @param flag: Reads the status of specifies the flag.
  201. * This parameter can be one of the following values:
  202. * @arg PMU_FLAG_WUE : Wake Up flag
  203. * @arg PMU_FLAG_SB : StandBy flag
  204. * @arg PMU_FLAG_PVDO: PVD Output flag
  205. *
  206. * @retval The new state of PMU_FLAG (SET or RESET).
  207. */
  208. uint8_t PMU_ReadStatusFlag(PMU_FLAG_T flag)
  209. {
  210. uint8_t BitStatus = BIT_RESET;
  211. if(flag == PMU_FLAG_WUE)
  212. {
  213. BitStatus = PMU->CSTS_B.WUEFLG;
  214. }
  215. else if(flag == PMU_FLAG_SB)
  216. {
  217. BitStatus = PMU->CSTS_B.SBFLG;
  218. }
  219. else if(flag == PMU_FLAG_PVDO)
  220. {
  221. BitStatus = PMU->CSTS_B.PVDOFLG;
  222. }
  223. return BitStatus;
  224. }
  225. /*!
  226. * @brief Clears the PWR's pending flags.
  227. *
  228. * @param flag: Clears the status of specifies the flag.
  229. * This parameter can be one of the following values:
  230. * @arg PMU_FLAG_WUE : Wake Up flag
  231. * @arg PMU_FLAG_SB : StandBy flag
  232. *
  233. * @retval None
  234. */
  235. void PMU_ClearStatusFlag(PMU_FLAG_T flag)
  236. {
  237. if(flag == PMU_FLAG_WUE)
  238. {
  239. PMU->CTRL_B.WUFLGCLR = BIT_SET;
  240. }
  241. else if(flag == PMU_FLAG_SB)
  242. {
  243. PMU->CTRL_B.SBFLGCLR = BIT_SET;
  244. }
  245. }
  246. /**@} end of group PMU_Functions */
  247. /**@} end of group PMU_Driver */
  248. /**@} end of group APM32E10x_StdPeriphDriver */