forked from micropython/micropython
-
Notifications
You must be signed in to change notification settings - Fork 171
/
powerctrl.c
304 lines (264 loc) · 10.7 KB
/
powerctrl.c
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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
/*
* This file is part of the MicroPython project, http://micropython.org/
*
* The MIT License (MIT)
*
* Copyright (c) 2013-2018 Damien P. George
* Copyright (c) 2022 Renesas Electronics Corporation
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include "py/runtime.h"
#include "py/mperrno.h"
#include "py/mphal.h"
#include "hal_data.h"
#include "rtc.h"
#include "powerctrl.h"
#if 0
// Sleep Mode
// At power on, by default sleep is set as the low-power mode. Sleep
// mode is the most convenient low-power mode available, as it does
// not require any special configuration (other than configuring and
// enabling a suitable interrupt or event to wake the MCU from sleep)
// to return to normal program-execution mode.
// The states of the SRAM, the processor registers, and the hardware
// peripherals are all maintained in sleep mode, and the time needed
// to enter and wake from sleep is minimal. Any interrupt causes the
// MCU device to wake from sleep mode, including the Systick interrupt
// used by the RTOS scheduler.
// Software Standby Mode
// In software-standby mode, the CPU, as well as most of the on-chip
// peripheral functions and all of the internal oscillators, are
// stopped.
// The contents of the CPU internal registers and SRAM data, the states
// of on-chip peripheral functions, and I/O Ports are all retained.
// Software-standby mode allows significant reduction in power
// consumption, because most of the oscillators are stopped in this mode.
// Like sleep mode, standby mode requires an interrupt or event be
// configured and enabled to wake up.
// Snooze Mode
// Snooze mode can be used with some MCU peripherals to execute basic
// tasks while keeping the MCU in a low-power state.
// Many core peripherals and all clocks can be selected to run during
// Snooze, allowing for more flexible low-power configuration than
// Software Standby mode. To enable Snooze, select "Software Standby
// mode with Snooze mode enabled" for the "Low Power Mode" configuration
// option.
// Snooze mode settings (including entry/exit sources) are available
// under "Standby Options".
// Deep Software Standby Mode
// Deep Software Standby Mode is only available on some MCU devices.
// The MCU always wakes from Deep Software Standby Mode by going
// through reset, either by the negation of the reset pin or by one of
// the wakeup sources configurable in the "Deep Standby Options"
// configuration group.
#endif
#if defined(USE_FSP_LPM)
// LPM_MODE_SLEEP: Sleep mode
// LPM_MODE_STANDBY: Software Standby mode
// LPM_MODE_STANDBY_SNOOZE: Software Standby mode with Snooze mode enabled
// LPM_MODE_DEEP: Deep Software Standby mode
lpm_instance_ctrl_t g_lpm_sleep_ctrl;
const lpm_cfg_t g_lpm_sleep_cfg = {
.low_power_mode = LPM_MODE_SLEEP,
.snooze_cancel_sources = LPM_SNOOZE_CANCEL_SOURCE_NONE,
.standby_wake_sources = (lpm_standby_wake_source_t)0,
.snooze_request_source = LPM_SNOOZE_REQUEST_RTC_PERIOD,
.snooze_end_sources = (lpm_snooze_end_t)0,
.dtc_state_in_snooze = LPM_SNOOZE_DTC_DISABLE,
#if BSP_FEATURE_LPM_HAS_SBYCR_OPE
.output_port_enable = 0,
#endif
#if BSP_FEATURE_LPM_HAS_DEEP_STANDBY
.io_port_state = 0,
.power_supply_state = 0,
.deep_standby_cancel_source = (lpm_deep_standby_cancel_source_t)0,
.deep_standby_cancel_edge = (lpm_deep_standby_cancel_edge_t)0,
#endif
.p_extend = NULL,
};
const lpm_instance_t g_lpm_sleep = {
.p_api = &g_lpm_on_lpm,
.p_ctrl = &g_lpm_sleep_ctrl,
.p_cfg = &g_lpm_sleep_cfg
};
lpm_instance_ctrl_t g_lpm_deep_ctrl;
const lpm_cfg_t g_lpm_deep_cfg = {
.low_power_mode = LPM_MODE_DEEP,
.snooze_cancel_sources =
LPM_SNOOZE_CANCEL_SOURCE_NONE,
.standby_wake_sources =
(lpm_standby_wake_source_t)0,
.snooze_request_source =
LPM_SNOOZE_REQUEST_RXD0_FALLING,
.snooze_end_sources =
(lpm_snooze_end_t)0,
.dtc_state_in_snooze =
LPM_SNOOZE_DTC_DISABLE,
#if BSP_FEATURE_LPM_HAS_SBYCR_OPE
.output_port_enable = LPM_OUTPUT_PORT_ENABLE_RETAIN,
#endif
#if BSP_FEATURE_LPM_HAS_DEEP_STANDBY
.io_port_state = LPM_IO_PORT_NO_CHANGE,
.power_supply_state = LPM_POWER_SUPPLY_DEEPCUT0,
.deep_standby_cancel_source = LPM_DEEP_STANDBY_CANCEL_SOURCE_RTC_INTERVAL | LPM_DEEP_STANDBY_CANCEL_SOURCE_RTC_ALARM | (lpm_deep_standby_cancel_source_t)0,
.deep_standby_cancel_edge = (lpm_deep_standby_cancel_edge_t)0,
#endif
.p_extend = NULL,
};
const lpm_instance_t g_lpm_deep = {
.p_api = &g_lpm_on_lpm,
.p_ctrl = &g_lpm_deep_ctrl,
.p_cfg = &g_lpm_deep_cfg
};
lpm_instance_ctrl_t g_lpm_standby_ctrl;
const lpm_cfg_t g_lpm_standby_cfg = {
.low_power_mode = LPM_MODE_STANDBY,
.snooze_cancel_sources =
LPM_SNOOZE_CANCEL_SOURCE_NONE,
.standby_wake_sources =
LPM_STANDBY_WAKE_SOURCE_RTCALM | LPM_STANDBY_WAKE_SOURCE_RTCPRD | (lpm_standby_wake_source_t)0,
.snooze_request_source = LPM_SNOOZE_REQUEST_RXD0_FALLING,
.snooze_end_sources = (lpm_snooze_end_t)0,
.dtc_state_in_snooze = LPM_SNOOZE_DTC_DISABLE,
#if BSP_FEATURE_LPM_HAS_SBYCR_OPE
.output_port_enable = LPM_OUTPUT_PORT_ENABLE_RETAIN,
#endif
#if BSP_FEATURE_LPM_HAS_DEEP_STANDBY
.io_port_state = LPM_IO_PORT_NO_CHANGE,
.power_supply_state = LPM_POWER_SUPPLY_DEEPCUT0,
.deep_standby_cancel_source = LPM_DEEP_STANDBY_CANCEL_SOURCE_RTC_INTERVAL | (lpm_deep_standby_cancel_source_t)0,
.deep_standby_cancel_edge = (lpm_deep_standby_cancel_edge_t)0,
#endif
.p_extend = NULL,
};
const lpm_instance_t g_lpm_standby = {
.p_api = &g_lpm_on_lpm,
.p_ctrl = &g_lpm_standby_ctrl,
.p_cfg = &g_lpm_standby_cfg
};
#endif
NORETURN void powerctrl_mcu_reset(void) {
#if BSP_FEATURE_TZ_HAS_TRUSTZONE
R_BSP_NonSecureEnter();
#else
NVIC_SystemReset();
#endif
while (1) {
;
}
}
NORETURN void powerctrl_enter_bootloader(uint32_t r0, uint32_t bl_addr) {
while (1) {
;
}
}
// static __attribute__((naked)) void branch_to_bootloader(uint32_t r0, uint32_t
// bl_addr) {
// }
void powerctrl_check_enter_bootloader(void) {
}
void powerctrl_enter_sleep_mode(void) {
// start trandition to RA MCU sleep mode
#if defined(USE_FSP_LPM)
fsp_err_t err;
err = R_LPM_Open(&g_lpm_sleep_ctrl, &g_lpm_sleep_cfg);
if (err != FSP_SUCCESS) {
mp_raise_msg_varg(&mp_type_ValueError, MP_ERROR_TEXT("Can't enter sleep mode"));
}
err = R_LPM_LowPowerModeEnter(&g_lpm_sleep_ctrl);
if (err != FSP_SUCCESS) {
mp_raise_msg_varg(&mp_type_ValueError, MP_ERROR_TEXT("Can't enter sleep mode"));
}
#endif
}
void powerctrl_enter_stop_mode(void) {
// Disable IRQs so that the IRQ that wakes the device from stop mode is not
// executed until after the clocks are reconfigured
uint32_t irq_state = disable_irq();
#if defined(MICROPY_BOARD_ENTER_STOP)
MICROPY_BOARD_ENTER_STOP
#endif
// start trandition to RA MCU sleep mode
#if defined(USE_FSP_LPM)
fsp_err_t err;
err = R_LPM_Open(&g_lpm_standby_ctrl, &g_lpm_standby_cfg);
if (err != FSP_SUCCESS) {
mp_raise_msg_varg(&mp_type_ValueError, MP_ERROR_TEXT("Can't enter stop mode"));
}
err = R_LPM_LowPowerModeEnter(&g_lpm_standby_ctrl);
if (err != FSP_SUCCESS) {
mp_raise_msg_varg(&mp_type_ValueError, MP_ERROR_TEXT("Can't enter stop mode"));
}
#endif
// reconfigure the system clock after waking up
#if defined(MICROPY_BOARD_LEAVE_STOP)
MICROPY_BOARD_LEAVE_STOP
#endif
// Enable IRQs now that all clocks are reconfigured
enable_irq(irq_state);
}
void powerctrl_enter_standby_mode(void) {
rtc_init_finalise();
#if defined(MICROPY_BOARD_ENTER_STANDBY)
MICROPY_BOARD_ENTER_STANDBY
#endif
// start trandition to RA MCU deep software standby mode via software standby mode
#if defined(USE_FSP_LPM)
fsp_err_t err;
err = R_LPM_Open(&g_lpm_deep_ctrl, &g_lpm_deep_cfg);
/* Handle any errors. This function should be defined by the user. */
if (err != FSP_SUCCESS) {
mp_raise_msg_varg(&mp_type_ValueError, MP_ERROR_TEXT("Can't enter stop mode"));
}
/* Check the Deep Software Standby Reset Flag. */
if (1U == R_SYSTEM->RSTSR0_b.DPSRSTF) {
/* Clear the IOKEEP bit to allow I/O Port use. */
err = R_LPM_IoKeepClear(&g_lpm_deep_ctrl);
if (err != FSP_SUCCESS) {
mp_raise_msg_varg(&mp_type_ValueError, MP_ERROR_TEXT("Can't enter stop mode"));
}
}
/* Add user code here. */
/* Reconfigure the module to set the IOKEEP bit before entering deep software standby. */
err = R_LPM_LowPowerReconfigure(&g_lpm_deep_ctrl, &g_lpm_deep_cfg);
if (err != FSP_SUCCESS) {
mp_raise_msg_varg(&mp_type_ValueError, MP_ERROR_TEXT("Can't enter stop mode"));
}
err = R_LPM_LowPowerModeEnter(&g_lpm_deep_ctrl);
/* Code after R_LPM_LowPowerModeEnter when using Deep Software Standby never be executed.
* Deep software standby exits by resetting the MCU. */
if (err != FSP_SUCCESS) {
mp_raise_msg_varg(&mp_type_ValueError, MP_ERROR_TEXT("Can't enter stop mode"));
}
#endif
// We need to clear the PWR wake-up-flag before entering standby, since
// the flag may have been set by a previous wake-up event. Furthermore,
// we need to disable the wake-up sources while clearing this flag, so
// that if a source is active it does actually wake the device.
// See section 5.3.7 of RM0090.
// save RTC interrupts
// disable register write protection
// disable RTC interrupts
// clear RTC wake-up flags
// enable previously-enabled RTC interrupts
// enable register write protection
// enter standby mode
// we never return; MCU is reset on exit from standby
}