at32f423_pwc.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. /**
  2. **************************************************************************
  3. * @file at32f423_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 "at32f423_conf.h"
  25. /** @addtogroup AT32F423_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. * - PWC_WAKEUP_PIN_2
  89. * - PWC_WAKEUP_PIN_6
  90. * - PWC_WAKEUP_PIN_7
  91. * @param new_state: new state of the standby wakeup pin.
  92. * this parameter can be one of the following values:
  93. * - TRUE <wakeup pin is used for wake up cpu from standby mode>
  94. * - FALSE <wakeup pin is used for general purpose I/O>
  95. * @retval none
  96. */
  97. void pwc_wakeup_pin_enable(uint32_t pin_num, confirm_state new_state)
  98. {
  99. if(new_state == TRUE)
  100. {
  101. PWC->ctrlsts |= pin_num;
  102. }
  103. else
  104. {
  105. PWC->ctrlsts &= ~pin_num;
  106. }
  107. }
  108. /**
  109. * @brief clear flag of pwc
  110. * @param pwc_flag: select the pwc flag.
  111. * this parameter can be any combination of the following values:
  112. * - PWC_WAKEUP_FLAG
  113. * - PWC_STANDBY_FLAG
  114. * - note:"PWC_PVM_OUTPUT_FLAG" cannot be choose!this bit is readonly bit,it means the voltage monitoring output state
  115. * @retval none
  116. */
  117. void pwc_flag_clear(uint32_t pwc_flag)
  118. {
  119. if(pwc_flag & PWC_STANDBY_FLAG)
  120. PWC->ctrl_bit.clsef = TRUE;
  121. if(pwc_flag & PWC_WAKEUP_FLAG)
  122. PWC->ctrl_bit.clswef = TRUE;
  123. }
  124. /**
  125. * @brief get flag of pwc
  126. * @param pwc_flag: select the pwc flag.
  127. * this parameter can be one of the following values:
  128. * - PWC_WAKEUP_FLAG
  129. * - PWC_STANDBY_FLAG
  130. * - PWC_PVM_OUTPUT_FLAG
  131. * @retval state of select flag(SET or RESET).
  132. */
  133. flag_status pwc_flag_get(uint32_t pwc_flag)
  134. {
  135. flag_status status = RESET;
  136. if ((PWC->ctrlsts & pwc_flag) == RESET)
  137. {
  138. status = RESET;
  139. }
  140. else
  141. {
  142. status = SET;
  143. }
  144. return status;
  145. }
  146. /**
  147. * @brief enter pwc sleep mode
  148. * @param sleep_mode_enter: choose the instruction to enter sleep mode.
  149. * this parameter can be one of the following values:
  150. * - PWC_SLEEP_ENTER_WFI
  151. * - PWC_SLEEP_ENTER_WFE
  152. * @retval none
  153. */
  154. void pwc_sleep_mode_enter(pwc_sleep_enter_type pwc_sleep_enter)
  155. {
  156. SCB->SCR &= (uint32_t)~0x4;
  157. if(pwc_sleep_enter == PWC_SLEEP_ENTER_WFE)
  158. {
  159. __SEV();
  160. __WFE();
  161. __WFE();
  162. }
  163. else if(pwc_sleep_enter == PWC_SLEEP_ENTER_WFI)
  164. {
  165. __WFI();
  166. }
  167. }
  168. /**
  169. * @brief enter pwc deep-sleep mode
  170. * @param pwc_deep_sleep_enter: choose the instruction to enter deep sleep mode.
  171. * this parameter can be one of the following values:
  172. * - PWC_DEEP_SLEEP_ENTER_WFI
  173. * - PWC_DEEP_SLEEP_ENTER_WFE
  174. * @retval none
  175. */
  176. void pwc_deep_sleep_mode_enter(pwc_deep_sleep_enter_type pwc_deep_sleep_enter)
  177. {
  178. SCB->SCR |= 0x04;
  179. if(pwc_deep_sleep_enter == PWC_DEEP_SLEEP_ENTER_WFE)
  180. {
  181. __SEV();
  182. __WFE();
  183. __WFE();
  184. }
  185. else if(pwc_deep_sleep_enter == PWC_DEEP_SLEEP_ENTER_WFI)
  186. {
  187. __WFI();
  188. }
  189. SCB->SCR &= (uint32_t)~0x4;
  190. }
  191. /**
  192. * @brief regulate low power consumption in the deep sleep mode
  193. * @param pwc_regulator: set the regulator state.
  194. * this parameter can be one of the following values:
  195. * - PWC_REGULATOR_ON
  196. * - PWC_REGULATOR_LOW_POWER
  197. * - PWC_REGULATOR_EXTRA_LOW_POWER
  198. * @retval none
  199. */
  200. void pwc_voltage_regulate_set(pwc_regulator_type pwc_regulator)
  201. {
  202. switch(pwc_regulator)
  203. {
  204. case PWC_REGULATOR_ON:
  205. PWC->ldoov_bit.vrexlpen = FALSE;
  206. PWC->ctrl_bit.vrsel = FALSE;
  207. break;
  208. case PWC_REGULATOR_LOW_POWER:
  209. PWC->ldoov_bit.vrexlpen = FALSE;
  210. PWC->ctrl_bit.vrsel = TRUE;
  211. break;
  212. case PWC_REGULATOR_EXTRA_LOW_POWER:
  213. PWC->ldoov_bit.vrexlpen = TRUE;
  214. PWC->ctrl_bit.vrsel = TRUE;
  215. break;
  216. default:
  217. break;
  218. }
  219. }
  220. /**
  221. * @brief enter pwc standby mode
  222. * @param none
  223. * @retval none
  224. */
  225. void pwc_standby_mode_enter(void)
  226. {
  227. PWC->ctrl_bit.clswef = TRUE;
  228. PWC->ctrl_bit.lpsel = TRUE;
  229. SCB->SCR |= 0x04;
  230. #if defined (__CC_ARM)
  231. __force_stores();
  232. #endif
  233. while(1)
  234. {
  235. __WFI();
  236. }
  237. }
  238. /**
  239. * @}
  240. */
  241. #endif
  242. /**
  243. * @}
  244. */
  245. /**
  246. * @}
  247. */