at32f413_pwc.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. /**
  2. **************************************************************************
  3. * @file at32f413_pwc.c
  4. * @version v2.0.5
  5. * @date 2022-05-20
  6. * @brief contains all the functions for the pwc firmware library
  7. **************************************************************************
  8. * Copyright notice & Disclaimer
  9. *
  10. * The software Board Support Package (BSP) that is made available to
  11. * download from Artery official website is the copyrighted work of Artery.
  12. * Artery authorizes customers to use, copy, and distribute the BSP
  13. * software and its related documentation for the purpose of design and
  14. * development in conjunction with Artery microcontrollers. Use of the
  15. * software is governed by this copyright notice and the following disclaimer.
  16. *
  17. * THIS SOFTWARE IS PROVIDED ON "AS IS" BASIS WITHOUT WARRANTIES,
  18. * GUARANTEES OR REPRESENTATIONS OF ANY KIND. ARTERY EXPRESSLY DISCLAIMS,
  19. * TO THE FULLEST EXTENT PERMITTED BY LAW, ALL EXPRESS, IMPLIED OR
  20. * STATUTORY OR OTHER WARRANTIES, GUARANTEES OR REPRESENTATIONS,
  21. * INCLUDING BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY,
  22. * FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT.
  23. *
  24. **************************************************************************
  25. */
  26. #include "at32f413_conf.h"
  27. /** @addtogroup AT32F413_periph_driver
  28. * @{
  29. */
  30. /** @defgroup PWC
  31. * @brief PWC driver modules
  32. * @{
  33. */
  34. #ifdef PWC_MODULE_ENABLED
  35. /** @defgroup PWC_private_functions
  36. * @{
  37. */
  38. /**
  39. * @brief deinitialize the pwc peripheral registers to their default reset values.
  40. * @param none
  41. * @retval none
  42. */
  43. void pwc_reset(void)
  44. {
  45. crm_periph_reset(CRM_PWC_PERIPH_RESET, TRUE);
  46. crm_periph_reset(CRM_PWC_PERIPH_RESET, FALSE);
  47. }
  48. /**
  49. * @brief enable or disable access to the battery powered domain.
  50. * @param new_state: new state of battery powered domain access.
  51. * this parameter can be: TRUE or FALSE.
  52. * @retval none
  53. */
  54. void pwc_battery_powered_domain_access(confirm_state new_state)
  55. {
  56. PWC->ctrl_bit.bpwen = new_state;
  57. }
  58. /**
  59. * @brief select the voltage threshold detected by the power voltage detector.
  60. * @param pvm_voltage: select pwc pvm voltage
  61. * this parameter can be one of the following values:
  62. * - PWC_PVM_VOLTAGE_2V3
  63. * - PWC_PVM_VOLTAGE_2V4
  64. * - PWC_PVM_VOLTAGE_2V5
  65. * - PWC_PVM_VOLTAGE_2V6
  66. * - PWC_PVM_VOLTAGE_2V7
  67. * - PWC_PVM_VOLTAGE_2V8
  68. * - PWC_PVM_VOLTAGE_2V9
  69. * @retval none
  70. */
  71. void pwc_pvm_level_select(pwc_pvm_voltage_type pvm_voltage)
  72. {
  73. PWC->ctrl_bit.pvmsel = pvm_voltage;
  74. }
  75. /**
  76. * @brief enable or disable pwc power voltage monitor (pvm)
  77. * @param new_state: new state of pvm.
  78. * this parameter can be: TRUE or FALSE.
  79. * @retval none
  80. */
  81. void pwc_power_voltage_monitor_enable(confirm_state new_state)
  82. {
  83. PWC->ctrl_bit.pvmen = new_state;
  84. }
  85. /**
  86. * @brief enable or disable pwc standby wakeup pin
  87. * @param pin_num: choose the wakeup pin.
  88. * this parameter can be be any combination of the following values:
  89. * - PWC_WAKEUP_PIN_1
  90. * @param new_state: new state of the standby wakeup pin.
  91. * this parameter can be one of the following values:
  92. * - TRUE <wakeup pin is used for wake up cpu from standby mode>
  93. * - FALSE <wakeup pin is used for general purpose I/O>
  94. * @retval none
  95. */
  96. void pwc_wakeup_pin_enable(uint32_t pin_num, confirm_state new_state)
  97. {
  98. if(new_state == TRUE)
  99. {
  100. PWC->ctrlsts |= pin_num;
  101. }
  102. else
  103. {
  104. PWC->ctrlsts &= ~pin_num;
  105. }
  106. }
  107. /**
  108. * @brief clear flag of pwc
  109. * @param pwc_flag: select the pwc flag.
  110. * this parameter can be any combination of the following values:
  111. * - PWC_WAKEUP_FLAG
  112. * - PWC_STANDBY_FLAG
  113. * - note:"PWC_PVM_OUTPUT_FLAG" cannot be choose!this bit is readonly bit,it means the voltage monitoring output state
  114. * @retval none
  115. */
  116. void pwc_flag_clear(uint32_t pwc_flag)
  117. {
  118. if(pwc_flag & PWC_STANDBY_FLAG)
  119. PWC->ctrl_bit.clsef = TRUE;
  120. if(pwc_flag & PWC_WAKEUP_FLAG)
  121. PWC->ctrl_bit.clswef = TRUE;
  122. }
  123. /**
  124. * @brief get flag of pwc
  125. * @param pwc_flag: select the pwc flag.
  126. * this parameter can be one of the following values:
  127. * - PWC_WAKEUP_FLAG
  128. * - PWC_STANDBY_FLAG
  129. * - PWC_PVM_OUTPUT_FLAG
  130. * @retval state of select flag(SET or RESET).
  131. */
  132. flag_status pwc_flag_get(uint32_t pwc_flag)
  133. {
  134. flag_status status = RESET;
  135. if ((PWC->ctrlsts & pwc_flag) == RESET)
  136. {
  137. status = RESET;
  138. }
  139. else
  140. {
  141. status = SET;
  142. }
  143. return status;
  144. }
  145. /**
  146. * @brief enter pwc sleep mode
  147. * @param sleep_mode_enter: choose the instruction to enter sleep mode.
  148. * this parameter can be one of the following values:
  149. * - PWC_SLEEP_ENTER_WFI
  150. * - PWC_SLEEP_ENTER_WFE
  151. * @retval none
  152. */
  153. void pwc_sleep_mode_enter(pwc_sleep_enter_type pwc_sleep_enter)
  154. {
  155. SCB->SCR &= (uint32_t)~0x4;
  156. if(pwc_sleep_enter == PWC_SLEEP_ENTER_WFE)
  157. {
  158. __SEV();
  159. __WFE();
  160. __WFE();
  161. }
  162. else if(pwc_sleep_enter == PWC_SLEEP_ENTER_WFI)
  163. {
  164. __WFI();
  165. }
  166. }
  167. /**
  168. * @brief enter pwc deep-sleep mode
  169. * @param pwc_deep_sleep_enter: choose the instruction to enter deep sleep mode.
  170. * this parameter can be one of the following values:
  171. * - PWC_DEEP_SLEEP_ENTER_WFI
  172. * - PWC_DEEP_SLEEP_ENTER_WFE
  173. * @retval none
  174. */
  175. void pwc_deep_sleep_mode_enter(pwc_deep_sleep_enter_type pwc_deep_sleep_enter)
  176. {
  177. SCB->SCR |= 0x04;
  178. if(pwc_deep_sleep_enter == PWC_DEEP_SLEEP_ENTER_WFE)
  179. {
  180. __SEV();
  181. __WFE();
  182. __WFE();
  183. }
  184. else if(pwc_deep_sleep_enter == PWC_DEEP_SLEEP_ENTER_WFI)
  185. {
  186. __WFI();
  187. }
  188. SCB->SCR &= (uint32_t)~0x4;
  189. }
  190. /**
  191. * @brief regulate low power consumption in the deep sleep mode
  192. * @param pwc_regulator: set the regulator state.
  193. * this parameter can be one of the following values:
  194. * - PWC_REGULATOR_ON
  195. * - PWC_REGULATOR_LOW_POWER
  196. * @retval none
  197. */
  198. void pwc_voltage_regulate_set(pwc_regulator_type pwc_regulator)
  199. {
  200. PWC->ctrl_bit.vrsel = pwc_regulator;
  201. }
  202. /**
  203. * @brief enter pwc standby mode
  204. * @param none
  205. * @retval none
  206. */
  207. void pwc_standby_mode_enter(void)
  208. {
  209. PWC->ctrl_bit.clswef = TRUE;
  210. PWC->ctrl_bit.lpsel = TRUE;
  211. SCB->SCR |= 0x04;
  212. #if defined (__CC_ARM)
  213. __force_stores();
  214. #endif
  215. __WFI();
  216. }
  217. /**
  218. * @}
  219. */
  220. #endif
  221. /**
  222. * @}
  223. */
  224. /**
  225. * @}
  226. */