at32f403a_407_pwc.c 6.5 KB

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