Skip to content

Commit 8b7c23c

Browse files
committed
address review comments
1 parent 28d9e91 commit 8b7c23c

File tree

20 files changed

+160
-103
lines changed

20 files changed

+160
-103
lines changed

lib/utils/pyexec.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ STATIC int parse_compile_execute(const void *source, mp_parse_input_kind_t input
101101
#endif
102102
}
103103

104-
// If the code was loaded from a file its likely to be running for a while so we'll long
104+
// If the code was loaded from a file it's likely to be running for a while so we'll long
105105
// live it and collect any garbage before running.
106106
if (input_kind == MP_PARSE_FILE_INPUT) {
107107
module_fun = make_obj_long_lived(module_fun, 6);
@@ -132,6 +132,10 @@ STATIC int parse_compile_execute(const void *source, mp_parse_input_kind_t input
132132
if (mp_obj_is_subclass_fast(mp_obj_get_type((mp_obj_t)nlr.ret_val), &mp_type_SystemExit)) {
133133
// at the moment, the value of SystemExit is unused
134134
ret = pyexec_system_exit;
135+
#if CIRCUITPY_ALARM
136+
} else if (mp_obj_is_subclass_fast(mp_obj_get_type((mp_obj_t)nlr.ret_val), &mp_type_DeepSleepRequest)) {
137+
ret = PYEXEC_DEEP_SLEEP;
138+
#endif
135139
} else {
136140
if ((mp_obj_t) nlr.ret_val != MP_OBJ_FROM_PTR(&MP_STATE_VM(mp_reload_exception))) {
137141
mp_obj_print_exception(&mp_plat_print, (mp_obj_t)nlr.ret_val);

lib/utils/pyexec.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ extern int pyexec_system_exit;
4949
#define PYEXEC_FORCED_EXIT (0x100)
5050
#define PYEXEC_SWITCH_MODE (0x200)
5151
#define PYEXEC_EXCEPTION (0x400)
52+
#define PYEXEC_DEEP_SLEEP (0x800)
5253

5354
int pyexec_raw_repl(void);
5455
int pyexec_friendly_repl(void);

main.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,11 +291,21 @@ STATIC bool run_code_py(safe_mode_t safe_mode) {
291291
}
292292
}
293293
#endif
294+
295+
// TODO: on deep sleep, make sure display is refreshed before sleeping (for e-ink).
296+
294297
cleanup_after_vm(heap);
295298

296299
if (result.return_code & PYEXEC_FORCED_EXIT) {
297300
return reload_requested;
298301
}
302+
303+
#if CIRCUITPY_ALARM
304+
if (result.return_code & PYEXEC_DEEP_SLEEP) {
305+
common_hal_alarm_enter_deep_sleep();
306+
// Does not return.
307+
}
308+
#endif
299309
}
300310

301311
// Program has finished running.

ports/atmel-samd/common-hal/microcontroller/Processor.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -352,5 +352,5 @@ void common_hal_mcu_processor_get_uid(uint8_t raw_id[]) {
352352
}
353353

354354
mcu_reset_reason_t common_hal_mcu_processor_get_reset_reason(void) {
355-
return RESET_REASON_POWER_ON;
355+
return RESET_REASON_UNKNOWN;
356356
}

ports/cxd56/common-hal/microcontroller/Processor.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,5 +50,5 @@ void common_hal_mcu_processor_get_uid(uint8_t raw_id[]) {
5050
}
5151

5252
mcu_reset_reason_t common_hal_mcu_processor_get_reset_reason(void) {
53-
return RESET_REASON_POWER_ON;
53+
return RESET_REASON_UNKNOWN;
5454
}

ports/esp32s2/common-hal/alarm/__init__.c

Lines changed: 30 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -29,15 +29,16 @@
2929
#include "py/objtuple.h"
3030
#include "py/runtime.h"
3131

32-
#include "shared-bindings/alarm/__init__.h"
3332
#include "shared-bindings/alarm/pin/PinAlarm.h"
3433
#include "shared-bindings/alarm/time/TimeAlarm.h"
3534
#include "shared-bindings/microcontroller/__init__.h"
3635
#include "shared-bindings/time/__init__.h"
36+
#include "shared-bindings/wifi/__init__.h"
37+
38+
#include "common-hal/alarm/__init__.h"
3739

3840
#include "esp_log.h"
3941
#include "esp_sleep.h"
40-
#include "esp_wifi.h"
4142

4243
STATIC mp_obj_tuple_t *_deep_sleep_alarms;
4344

@@ -101,7 +102,7 @@ STATIC void setup_sleep_alarms(size_t n_alarms, const mp_obj_t *alarms) {
101102
mp_float_t now_secs = uint64_to_float(common_hal_time_monotonic_ms()) / 1000.0f;
102103
mp_float_t wakeup_in_secs = MAX(0.0f, time_alarm->monotonic_time - now_secs);
103104
const uint64_t sleep_for_us = (uint64_t) (wakeup_in_secs * 1000000);
104-
ESP_LOGI("ALARM", "Sleep for us: %lld", sleep_for_us);
105+
ESP_LOGI("ALARM", "will sleep for us: %lld", sleep_for_us);
105106
esp_sleep_enable_timer_wakeup(sleep_for_us);
106107
}
107108
}
@@ -157,25 +158,41 @@ mp_obj_t common_hal_alarm_wait_until_alarms(size_t n_alarms, const mp_obj_t *ala
157158
return mp_const_none;
158159
}
159160

160-
mp_obj_t common_hal_alarm_sleep_until_alarms(size_t n_alarms, const mp_obj_t *alarms) {
161+
// Is it safe to do a light sleep? Check whether WiFi is on or there are
162+
// other ongoing tasks that should not be shut down.
163+
static bool light_sleep_ok(void) {
164+
return !common_hal_wifi_radio_get_enabled(&common_hal_wifi_radio_obj);
165+
}
166+
167+
mp_obj_t common_hal_alarm_light_sleep_until_alarms(size_t n_alarms, const mp_obj_t *alarms) {
161168
if (n_alarms == 0) {
162169
return mp_const_none;
163170
}
164171

165-
setup_sleep_alarms(n_alarms, alarms);
166-
167-
// Shut down wifi cleanly.
168-
esp_wifi_stop();
169-
ESP_LOGI("ALARM", "start light sleep");
170-
esp_light_sleep_start();
171-
return common_hal_alarm_get_wake_alarm();
172+
if (light_sleep_ok()) {
173+
ESP_LOGI("ALARM", "start light sleep");
174+
setup_sleep_alarms(n_alarms, alarms);
175+
esp_light_sleep_start();
176+
return common_hal_alarm_get_wake_alarm();
177+
} else {
178+
// Don't do an ESP32 light sleep.
179+
return common_hal_alarm_wait_until_alarms(n_alarms, alarms);
180+
}
172181
}
173182

174183
void common_hal_alarm_exit_and_deep_sleep_until_alarms(size_t n_alarms, const mp_obj_t *alarms) {
175184
setup_sleep_alarms(n_alarms, alarms);
176185

177-
// Shut down wifi cleanly.
178-
esp_wifi_stop();
186+
// Raise an exception, which will be processed in main.c.
187+
mp_raise_arg1(&mp_type_DeepSleepRequest, NULL);
188+
}
189+
190+
void common_hal_alarm_prepare_for_deep_sleep(void) {
191+
// Turn off WiFi and anything else that should be shut down cleanly.
192+
common_hal_wifi_radio_set_enabled(&common_hal_wifi_radio_obj, false);
193+
}
194+
195+
void NORETURN common_hal_alarm_enter_deep_sleep(void) {
179196
ESP_LOGI("ALARM", "start deep sleep");
180197
esp_deep_sleep_start();
181198
}

ports/esp32s2/common-hal/alarm/pin/PinAlarm.c

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,26 +29,21 @@
2929
#include "shared-bindings/alarm/pin/PinAlarm.h"
3030
#include "shared-bindings/microcontroller/Pin.h"
3131

32-
void common_hal_alarm_pin_pin_alarm_construct(alarm_pin_pin_alarm_obj_t *self, const mp_obj_t pins[], size_t num_pins, bool value, bool all_same_value, bool edge, bool pull) {
33-
self->pins = mp_obj_new_tuple(num_pins, pins);
32+
void common_hal_alarm_pin_pin_alarm_construct(alarm_pin_pin_alarm_obj_t *self, mcu_pin_obj_t *pin, bool value, bool edge, bool pull) {
33+
self->pin = pin;
3434
self->value = value;
35-
self->all_same_value = all_same_value;
3635
self->edge = edge;
3736
self->pull = pull;
3837
}
3938

40-
mp_obj_tuple_t *common_hal_alarm_pin_pin_alarm_get_pins(alarm_pin_pin_alarm_obj_t *self) {
41-
return self->pins;
39+
mcu_pin_obj_t *common_hal_alarm_pin_pin_alarm_get_pin(alarm_pin_pin_alarm_obj_t *self) {
40+
return self->pin;
4241
}
4342

4443
bool common_hal_alarm_pin_pin_alarm_get_value(alarm_pin_pin_alarm_obj_t *self) {
4544
return self->value;
4645
}
4746

48-
bool common_hal_alarm_pin_pin_alarm_get_all_same_value(alarm_pin_pin_alarm_obj_t *self) {
49-
return self->all_same_value;
50-
}
51-
5247
bool common_hal_alarm_pin_pin_alarm_get_edge(alarm_pin_pin_alarm_obj_t *self) {
5348
return self->edge;
5449
}

ports/esp32s2/common-hal/alarm/pin/PinAlarm.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929

3030
typedef struct {
3131
mp_obj_base_t base;
32-
mp_obj_tuple_t *pins;
32+
mcu_pin_obj_t *pin;
3333
bool value;
3434
bool all_same_value;
3535
bool edge;

ports/litex/common-hal/microcontroller/Processor.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,5 +66,5 @@ void common_hal_mcu_processor_get_uid(uint8_t raw_id[]) {
6666
}
6767

6868
mcu_reset_reason_t common_hal_mcu_processor_get_reset_reason(void) {
69-
return RESET_REASON_POWER_ON;
69+
return RESET_REASON_UNKNOWN;
7070
}

ports/mimxrt10xx/common-hal/microcontroller/Processor.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,5 +73,5 @@ void common_hal_mcu_processor_get_uid(uint8_t raw_id[]) {
7373
}
7474

7575
mcu_reset_reason_t common_hal_mcu_processor_get_reset_reason(void) {
76-
return RESET_REASON_POWER_ON;
76+
return RESET_REASON_UNKNOWN;
7777
}

ports/nrf/common-hal/microcontroller/Processor.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,5 +123,5 @@ void common_hal_mcu_processor_get_uid(uint8_t raw_id[]) {
123123
}
124124

125125
mcu_reset_reason_t common_hal_mcu_processor_get_reset_reason(void) {
126-
return RESET_REASON_POWER_ON;
126+
return RESET_REASON_UNKNOWN;
127127
}

ports/stm/common-hal/microcontroller/Processor.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,5 +143,5 @@ void common_hal_mcu_processor_get_uid(uint8_t raw_id[]) {
143143
}
144144

145145
mcu_reset_reason_t common_hal_mcu_processor_get_reset_reason(void) {
146-
return RESET_REASON_POWER_ON;
146+
return RESET_REASON_UNKNOWN;
147147
}

py/obj.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -640,6 +640,10 @@ extern const mp_obj_type_t mp_type_UnicodeError;
640640
extern const mp_obj_type_t mp_type_ValueError;
641641
extern const mp_obj_type_t mp_type_ViperTypeError;
642642
extern const mp_obj_type_t mp_type_ZeroDivisionError;
643+
#if CIRCUITPY_ALARM
644+
extern const mp_obj_type_t mp_type_DeepSleepRequest;
645+
#endif
646+
643647

644648
// Constant objects, globally accessible
645649
// The macros are for convenience only

py/objexcept.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,9 @@ MP_DEFINE_EXCEPTION(Exception, BaseException)
318318
#if MICROPY_PY_BUILTINS_STR_UNICODE
319319
MP_DEFINE_EXCEPTION(UnicodeError, ValueError)
320320
//TODO: Implement more UnicodeError subclasses which take arguments
321+
#endif
322+
#if CIRCUITPY_ALARM
323+
MP_DEFINE_EXCEPTION(DeepSleepRequest, BaseException)
321324
#endif
322325
MP_DEFINE_EXCEPTION(MpyError, ValueError)
323326
/*

py/reload.c

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,22 @@
1-
//
2-
// Created by Roy Hooper on 2018-05-14.
3-
//
1+
/*
2+
* This file is part of the MicroPython project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2018 by Roy Hooper
7+
* furnished to do so, subject to the following conditions:
8+
*
9+
* The above copyright notice and this permission notice shall be included in
10+
* all copies or substantial portions of the Software.
11+
*
12+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
13+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
14+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
15+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
16+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
17+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
18+
* THE SOFTWARE.
19+
*/
420

521
#include "reload.h"
622
#include "py/mpstate.h"

py/reload.h

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,22 @@
1-
//
2-
// Created by Roy Hooper on 2018-05-14.
3-
//
1+
/*
2+
* This file is part of the MicroPython project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2018 by Roy Hooper
7+
* furnished to do so, subject to the following conditions:
8+
*
9+
* The above copyright notice and this permission notice shall be included in
10+
* all copies or substantial portions of the Software.
11+
*
12+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
13+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
14+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
15+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
16+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
17+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
18+
* THE SOFTWARE.
19+
*/
420

521
#ifndef CIRCUITPYTHON_RELOAD_H
622
#define CIRCUITPYTHON_RELOAD_H

0 commit comments

Comments
 (0)