summaryrefslogtreecommitdiffstats
path: root/libopencm3/lib/stm32/common/pwr_common_all.c
blob: 53e6b42a68c0a99c78ace7ed5805aa151139b1b9 (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
/** @addtogroup pwr_file PWR

@author @htmlonly © @endhtmlonly 2012
Ken Sarkies <ksarkies@internode.on.net>

*/
/*
 * This file is part of the libopencm3 project.
 *
 * Copyright (C) 2012 Ken Sarkies <ksarkies@internode.on.net>
 *
 * This library is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this library.  If not, see <http://www.gnu.org/licenses/>.
 */

/**@{*/

#include <libopencm3/stm32/pwr.h>

/*---------------------------------------------------------------------------*/
/** @brief Disable Backup Domain Write Protection.

This allows backup domain registers to be changed. These registers are write
protected after a reset.
*/

void pwr_disable_backup_domain_write_protect(void)
{
	PWR_CR |= PWR_CR_DBP;
}

/*---------------------------------------------------------------------------*/
/** @brief Re-enable Backup Domain Write Protection.

This protects backup domain registers from inadvertent change.
*/

void pwr_enable_backup_domain_write_protect(void)
{
	PWR_CR &= ~PWR_CR_DBP;
}

/*---------------------------------------------------------------------------*/
/** @brief Enable Power Voltage Detector.

This provides voltage level threshold detection. The result of detection is
provided in the power voltage detector output flag (see @ref pwr_voltage_high)
or by setting the EXTI16 interrupt (see datasheet for configuration details).

@param[in] pvd_level uint32_t. Taken from @ref pwr_pls.
*/

void pwr_enable_power_voltage_detect(uint32_t pvd_level)
{
	PWR_CR &= ~PWR_CR_PLS_MASK;
	PWR_CR |= (PWR_CR_PVDE | pvd_level);
}

/*---------------------------------------------------------------------------*/
/** @brief Disable Power Voltage Detector.

*/

void pwr_disable_power_voltage_detect(void)
{
	PWR_CR &= ~PWR_CR_PVDE;
}

/*---------------------------------------------------------------------------*/
/** @brief Clear the Standby Flag.

This is set when the processor returns from a standby mode.
*/

void pwr_clear_standby_flag(void)
{
	PWR_CR |= PWR_CR_CSBF;
}

/*---------------------------------------------------------------------------*/
/** @brief Clear the Wakeup Flag.

This is set when the processor receives a wakeup signal.
*/

void pwr_clear_wakeup_flag(void)
{
	PWR_CR |= PWR_CR_CWUF;
}

/*---------------------------------------------------------------------------*/
/** @brief Set Standby Mode in Deep Sleep.

*/

void pwr_set_standby_mode(void)
{
	PWR_CR |= PWR_CR_PDDS;
}

/*---------------------------------------------------------------------------*/
/** @brief Set Stop Mode in Deep Sleep.

*/

void pwr_set_stop_mode(void)
{
	PWR_CR &= ~PWR_CR_PDDS;
}

/*---------------------------------------------------------------------------*/
/** @brief Voltage Regulator On in Stop Mode.

*/

void pwr_voltage_regulator_on_in_stop(void)
{
	PWR_CR &= ~PWR_CR_LPDS;
}

/*---------------------------------------------------------------------------*/
/** @brief Voltage Regulator Low Power in Stop Mode.

*/

void pwr_voltage_regulator_low_power_in_stop(void)
{
	PWR_CR |= PWR_CR_LPDS;
}

/*---------------------------------------------------------------------------*/
/** @brief Enable Wakeup Pin.

The wakeup pin is used for waking the processor from standby mode.
*/

void pwr_enable_wakeup_pin(void)
{
	PWR_CSR |= PWR_CSR_EWUP;
}

/*---------------------------------------------------------------------------*/
/** @brief Release Wakeup Pin.

The wakeup pin is used for general purpose I/O.
*/

void pwr_disable_wakeup_pin(void)
{
	PWR_CSR &= ~PWR_CSR_EWUP;
}

/*---------------------------------------------------------------------------*/
/** @brief Get Voltage Detector Output.

The voltage detector threshold must be set when the power voltage detector is
enabled, see @ref pwr_enable_power_voltage_detect.

@returns boolean: TRUE if the power voltage is above the preset voltage
threshold.
*/

bool pwr_voltage_high(void)
{
	return PWR_CSR & PWR_CSR_PVDO;
}

/*---------------------------------------------------------------------------*/
/** @brief Get Standby Flag.

The standby flag is set when the processor returns from a standby state. It is
cleared by software (see @ref pwr_clear_standby_flag).

@returns boolean: TRUE if the processor was in standby state.
*/

bool pwr_get_standby_flag(void)
{
	return PWR_CSR & PWR_CSR_SBF;
}

/*---------------------------------------------------------------------------*/
/** @brief Get Wakeup Flag.

The wakeup flag is set when a wakeup event has been received. It is
cleared by software (see @ref pwr_clear_wakeup_flag).

@returns boolean: TRUE if a wakeup event was received.
*/

bool pwr_get_wakeup_flag(void)
{
	return PWR_CSR & PWR_CSR_WUF;
}
/**@}*/