summaryrefslogtreecommitdiff
path: root/bsp/at32/libraries/AT32F413_Firmware_Library/drivers/src/at32f413_pwc.c
blob: d0c93983aa9c71e3c507a9f0ff65ea8c0a6d4ca5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
/**
  **************************************************************************
  * @file     at32f413_pwc.c
  * @version  v2.0.5
  * @date     2022-05-20
  * @brief    contains all the functions for the pwc firmware library
  **************************************************************************
  *                       Copyright notice & Disclaimer
  *
  * The software Board Support Package (BSP) that is made available to
  * download from Artery official website is the copyrighted work of Artery.
  * Artery authorizes customers to use, copy, and distribute the BSP
  * software and its related documentation for the purpose of design and
  * development in conjunction with Artery microcontrollers. Use of the
  * software is governed by this copyright notice and the following disclaimer.
  *
  * THIS SOFTWARE IS PROVIDED ON "AS IS" BASIS WITHOUT WARRANTIES,
  * GUARANTEES OR REPRESENTATIONS OF ANY KIND. ARTERY EXPRESSLY DISCLAIMS,
  * TO THE FULLEST EXTENT PERMITTED BY LAW, ALL EXPRESS, IMPLIED OR
  * STATUTORY OR OTHER WARRANTIES, GUARANTEES OR REPRESENTATIONS,
  * INCLUDING BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY,
  * FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT.
  *
  **************************************************************************
  */

#include "at32f413_conf.h"

/** @addtogroup AT32F413_periph_driver
  * @{
  */

/** @defgroup PWC
  * @brief PWC driver modules
  * @{
  */

#ifdef PWC_MODULE_ENABLED

/** @defgroup PWC_private_functions
  * @{
  */

/**
  * @brief  deinitialize the pwc peripheral registers to their default reset values.
  * @param  none
  * @retval none
  */
void pwc_reset(void)
{
  crm_periph_reset(CRM_PWC_PERIPH_RESET, TRUE);
  crm_periph_reset(CRM_PWC_PERIPH_RESET, FALSE);
}

/**
  * @brief  enable or disable access to the battery powered domain.
  * @param  new_state: new state of battery powered domain access.
  *         this parameter can be: TRUE or FALSE.
  * @retval none
  */
void pwc_battery_powered_domain_access(confirm_state new_state)
{
  PWC->ctrl_bit.bpwen = new_state;
}

/**
  * @brief  select the voltage threshold detected by the power voltage detector.
  * @param  pvm_voltage: select pwc pvm voltage
  *         this parameter can be one of the following values:
  *         - PWC_PVM_VOLTAGE_2V3
  *         - PWC_PVM_VOLTAGE_2V4
  *         - PWC_PVM_VOLTAGE_2V5
  *         - PWC_PVM_VOLTAGE_2V6
  *         - PWC_PVM_VOLTAGE_2V7
  *         - PWC_PVM_VOLTAGE_2V8
  *         - PWC_PVM_VOLTAGE_2V9
  * @retval none
  */
void pwc_pvm_level_select(pwc_pvm_voltage_type pvm_voltage)
{
  PWC->ctrl_bit.pvmsel = pvm_voltage;
}

/**
  * @brief  enable or disable pwc power voltage monitor (pvm)
  * @param  new_state: new state of pvm.
  *         this parameter can be: TRUE or FALSE.
  * @retval none
  */
void pwc_power_voltage_monitor_enable(confirm_state new_state)
{
  PWC->ctrl_bit.pvmen = new_state;
}

/**
  * @brief  enable or disable pwc standby wakeup pin
  * @param  pin_num: choose the wakeup pin.
  *         this parameter can be be any combination of the following values:
  *         - PWC_WAKEUP_PIN_1
  * @param  new_state: new state of the standby wakeup pin.
  *         this parameter can be one of the following values:
  *         - TRUE <wakeup pin is used for wake up cpu from standby mode>
  *         - FALSE <wakeup pin is used for general purpose I/O>
  * @retval none
  */
void pwc_wakeup_pin_enable(uint32_t pin_num, confirm_state new_state)
{
  if(new_state == TRUE)
  {
    PWC->ctrlsts |= pin_num;
  }
  else
  {
    PWC->ctrlsts &= ~pin_num;
  }
}

/**
  * @brief  clear flag of pwc
  * @param  pwc_flag: select the pwc flag.
  *         this parameter can be any combination of the following values:
  *         - PWC_WAKEUP_FLAG
  *         - PWC_STANDBY_FLAG
  *         - note:"PWC_PVM_OUTPUT_FLAG" cannot be choose!this bit is readonly bit,it means the voltage monitoring output state
  * @retval none
  */
void pwc_flag_clear(uint32_t pwc_flag)
{
  if(pwc_flag & PWC_STANDBY_FLAG)
    PWC->ctrl_bit.clsef = TRUE;
  if(pwc_flag & PWC_WAKEUP_FLAG)
    PWC->ctrl_bit.clswef = TRUE;
}

/**
  * @brief  get flag of pwc
  * @param  pwc_flag: select the pwc flag.
  *         this parameter can be one of the following values:
  *         - PWC_WAKEUP_FLAG
  *         - PWC_STANDBY_FLAG
  *         - PWC_PVM_OUTPUT_FLAG
  * @retval state of select flag(SET or RESET).
  */
flag_status pwc_flag_get(uint32_t pwc_flag)
{
  flag_status status = RESET;
  if ((PWC->ctrlsts & pwc_flag) == RESET)
  {
    status = RESET;
  }
  else
  {
    status = SET;
  }
  return status;
}

/**
  * @brief  enter pwc sleep mode
  * @param  sleep_mode_enter: choose the instruction to enter sleep mode.
  *         this parameter can be one of the following values:
  *         - PWC_SLEEP_ENTER_WFI
  *         - PWC_SLEEP_ENTER_WFE
  * @retval none
  */
void pwc_sleep_mode_enter(pwc_sleep_enter_type pwc_sleep_enter)
{
  SCB->SCR &= (uint32_t)~0x4;
  if(pwc_sleep_enter == PWC_SLEEP_ENTER_WFE)
  {
    __SEV();
    __WFE();
    __WFE();
  }
  else if(pwc_sleep_enter == PWC_SLEEP_ENTER_WFI)
  {
    __WFI();
  }
}

/**
  * @brief  enter pwc deep-sleep mode
  * @param  pwc_deep_sleep_enter: choose the instruction to enter deep sleep mode.
  *         this parameter can be one of the following values:
  *         - PWC_DEEP_SLEEP_ENTER_WFI
  *         - PWC_DEEP_SLEEP_ENTER_WFE
  * @retval none
  */
void pwc_deep_sleep_mode_enter(pwc_deep_sleep_enter_type pwc_deep_sleep_enter)
{
  SCB->SCR |= 0x04;
  if(pwc_deep_sleep_enter == PWC_DEEP_SLEEP_ENTER_WFE)
  {
    __SEV();
    __WFE();
    __WFE();
  }
  else if(pwc_deep_sleep_enter == PWC_DEEP_SLEEP_ENTER_WFI)
  {
    __WFI();
  }
  SCB->SCR &= (uint32_t)~0x4;
}

/**
  * @brief  regulate low power consumption in the deep sleep mode
  * @param  pwc_regulator: set the regulator state.
  *         this parameter can be one of the following values:
  *         - PWC_REGULATOR_ON
  *         - PWC_REGULATOR_LOW_POWER
  * @retval none
  */
void pwc_voltage_regulate_set(pwc_regulator_type pwc_regulator)
{
  PWC->ctrl_bit.vrsel = pwc_regulator;
}

/**
  * @brief  enter pwc standby mode
  * @param  none
  * @retval none
  */
void pwc_standby_mode_enter(void)
{
  PWC->ctrl_bit.clswef = TRUE;
  PWC->ctrl_bit.lpsel = TRUE;
  SCB->SCR |= 0x04;
#if defined (__CC_ARM)
  __force_stores();
#endif
  __WFI();
}

/**
  * @}
  */

#endif

/**
  * @}
  */

/**
  * @}
  */