Skip to content

Commit 90b9ec6

Browse files
microdev1tannewt
authored andcommitted
Initial Sleep Support
1 parent 557a58b commit 90b9ec6

10 files changed

Lines changed: 90 additions & 0 deletions

File tree

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@
4141

4242
#include "freertos/FreeRTOS.h"
4343

44+
#include "esp_sleep.h"
45+
4446
void common_hal_mcu_delay_us(uint32_t delay) {
4547

4648
}
@@ -77,6 +79,10 @@ void common_hal_mcu_reset(void) {
7779
while(1);
7880
}
7981

82+
void common_hal_mcu_sleep(void) {
83+
esp_deep_sleep_start();
84+
}
85+
8086
// The singleton microcontroller.Processor object, bound to microcontroller.cpu
8187
// It currently only has properties, and no state.
8288
const mcu_processor_obj_t common_hal_mcu_processor_obj = {
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#include "esp_sleep.h"
2+
3+
#include "shared-bindings/timealarm/__init__.h"
4+
5+
void common_hal_timealarm_duration (uint32_t ms) {
6+
if (esp_sleep_enable_timer_wakeup((ms) * 1000) == ESP_ERR_INVALID_ARG) {
7+
mp_raise_ValueError(translate("time out of range"));
8+
}
9+
}
10+

ports/esp32s2/mpconfigport.mk

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ CIRCUITPY_FREQUENCYIO = 0
2222
CIRCUITPY_I2CPERIPHERAL = 0
2323
CIRCUITPY_ROTARYIO = 0
2424
CIRCUITPY_NVM = 0
25+
CIRCUITPY_TIMEALARM = 1
2526
# We don't have enough endpoints to include MIDI.
2627
CIRCUITPY_USB_MIDI = 0
2728
CIRCUITPY_WIFI = 1

py/circuitpy_defns.mk

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,9 @@ endif
259259
ifeq ($(CIRCUITPY_TIME),1)
260260
SRC_PATTERNS += time/%
261261
endif
262+
ifeq ($(CIRCUITPY_TIMEALARM),1)
263+
SRC_PATTERNS += timealarm/%
264+
endif
262265
ifeq ($(CIRCUITPY_TOUCHIO),1)
263266
SRC_PATTERNS += touchio/%
264267
endif
@@ -360,6 +363,7 @@ SRC_COMMON_HAL_ALL = \
360363
ssl/SSLContext.c \
361364
supervisor/Runtime.c \
362365
supervisor/__init__.c \
366+
timealarm/__init__.c \
363367
watchdog/WatchDogMode.c \
364368
watchdog/WatchDogTimer.c \
365369
watchdog/__init__.c \

py/circuitpy_mpconfig.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -663,6 +663,13 @@ extern const struct _mp_obj_module_t time_module;
663663
#define TIME_MODULE_ALT_NAME
664664
#endif
665665

666+
#if CIRCUITPY_TIMEALARM
667+
extern const struct _mp_obj_module_t timealarm_module;
668+
#define TIMEALARM_MODULE { MP_OBJ_NEW_QSTR(MP_QSTR_timealarm), (mp_obj_t)&timealarm_module },
669+
#else
670+
#define TIMEALARM_MODULE
671+
#endif
672+
666673
#if CIRCUITPY_TOUCHIO
667674
extern const struct _mp_obj_module_t touchio_module;
668675
#define TOUCHIO_MODULE { MP_OBJ_NEW_QSTR(MP_QSTR_touchio), (mp_obj_t)&touchio_module },
@@ -821,6 +828,7 @@ extern const struct _mp_obj_module_t wifi_module;
821828
STRUCT_MODULE \
822829
SUPERVISOR_MODULE \
823830
TOUCHIO_MODULE \
831+
TIMEALARM_MODULE \
824832
UHEAP_MODULE \
825833
USB_HID_MODULE \
826834
USB_MIDI_MODULE \

py/circuitpy_mpconfig.mk

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,9 @@ CFLAGS += -DCIRCUITPY_TERMINALIO=$(CIRCUITPY_TERMINALIO)
234234
CIRCUITPY_TIME ?= 1
235235
CFLAGS += -DCIRCUITPY_TIME=$(CIRCUITPY_TIME)
236236

237+
CIRCUITPY_TIMEALARM ?= 0
238+
CFLAGS += -DCIRCUITPY_TIMEALARM=$(CIRCUITPY_TIMEALARM)
239+
237240
# touchio might be native or generic. See circuitpy_defns.mk.
238241
CIRCUITPY_TOUCHIO_USE_NATIVE ?= 0
239242
CFLAGS += -DCIRCUITPY_TOUCHIO_USE_NATIVE=$(CIRCUITPY_TOUCHIO_USE_NATIVE)

shared-bindings/microcontroller/__init__.c

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,22 @@ STATIC mp_obj_t mcu_reset(void) {
136136
}
137137
STATIC MP_DEFINE_CONST_FUN_OBJ_0(mcu_reset_obj, mcu_reset);
138138

139+
//| def sleep() -> None:
140+
//| """Microcontroller will go into deep sleep.
141+
//| cpy will restart when wakeup func. is triggered`.
142+
//|
143+
//| .. warning:: This may result in file system corruption when connected to a
144+
//| host computer. Be very careful when calling this! Make sure the device
145+
//| "Safely removed" on Windows or "ejected" on Mac OSX and Linux."""
146+
//| ...
147+
//|
148+
STATIC mp_obj_t mcu_sleep(void) {
149+
common_hal_mcu_sleep();
150+
// We won't actually get here because mcu is going into sleep.
151+
return mp_const_none;
152+
}
153+
STATIC MP_DEFINE_CONST_FUN_OBJ_0(mcu_sleep_obj, mcu_sleep);
154+
139155
//| nvm: Optional[ByteArray]
140156
//| """Available non-volatile memory.
141157
//| This object is the sole instance of `nvm.ByteArray` when available or ``None`` otherwise.
@@ -171,6 +187,7 @@ STATIC const mp_rom_map_elem_t mcu_module_globals_table[] = {
171187
{ MP_ROM_QSTR(MP_QSTR_enable_interrupts), MP_ROM_PTR(&mcu_enable_interrupts_obj) },
172188
{ MP_ROM_QSTR(MP_QSTR_on_next_reset), MP_ROM_PTR(&mcu_on_next_reset_obj) },
173189
{ MP_ROM_QSTR(MP_QSTR_reset), MP_ROM_PTR(&mcu_reset_obj) },
190+
{ MP_ROM_QSTR(MP_QSTR_sleep), MP_ROM_PTR(&mcu_sleep_obj) },
174191
#if CIRCUITPY_INTERNAL_NVM_SIZE > 0
175192
{ MP_ROM_QSTR(MP_QSTR_nvm), MP_ROM_PTR(&common_hal_mcu_nvm_obj) },
176193
#else

shared-bindings/microcontroller/__init__.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ extern void common_hal_mcu_enable_interrupts(void);
4343
extern void common_hal_mcu_on_next_reset(mcu_runmode_t runmode);
4444
extern void common_hal_mcu_reset(void);
4545

46+
extern void common_hal_mcu_sleep(void);
47+
4648
extern const mp_obj_dict_t mcu_pin_globals;
4749

4850
extern const mcu_processor_obj_t common_hal_mcu_processor_obj;
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#include "py/obj.h"
2+
#include "shared-bindings/timealarm/__init__.h"
3+
4+
//| Set Timer Wakeup
5+
//|
6+
STATIC mp_obj_t timealarm_duration(mp_obj_t seconds_o) {
7+
#if MICROPY_PY_BUILTINS_FLOAT
8+
mp_float_t seconds = mp_obj_get_float(seconds_o);
9+
mp_float_t msecs = 1000.0f * seconds + 0.5f;
10+
#else
11+
mp_int_t seconds = mp_obj_get_int(seconds_o);
12+
mp_int_t msecs = 1000 * seconds;
13+
#endif
14+
if (seconds < 0) {
15+
mp_raise_ValueError(translate("sleep length must be non-negative"));
16+
}
17+
common_hal_timealarm_duration(msecs);
18+
return mp_const_none;
19+
}
20+
STATIC MP_DEFINE_CONST_FUN_OBJ_1(timealarm_duration_obj, timealarm_duration);
21+
22+
STATIC const mp_rom_map_elem_t timealarm_module_globals_table[] = {
23+
{ MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_timealarm) },
24+
{ MP_OBJ_NEW_QSTR(MP_QSTR_duration), MP_ROM_PTR(&timealarm_duration_obj) },
25+
};
26+
STATIC MP_DEFINE_CONST_DICT(timealarm_module_globals, timealarm_module_globals_table);
27+
28+
const mp_obj_module_t timealarm_module = {
29+
.base = { &mp_type_module },
30+
.globals = (mp_obj_dict_t*)&timealarm_module_globals,
31+
};
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#ifndef MICROPY_INCLUDED_SHARED_BINDINGS_ulp___INIT___H
2+
#define MICROPY_INCLUDED_SHARED_BINDINGS_ulp___INIT___H
3+
4+
#include "py/runtime.h"
5+
6+
extern void common_hal_timealarm_duration(uint32_t);
7+
8+
#endif

0 commit comments

Comments
 (0)