Skip to content

Commit 05a3f20

Browse files
microdev1tannewt
authored andcommitted
Add function to get time elapsed during sleep
1 parent e310b87 commit 05a3f20

File tree

3 files changed

+71
-0
lines changed

3 files changed

+71
-0
lines changed

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

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525
* THE SOFTWARE.
2626
*/
2727

28+
#include <sys/time.h>
29+
2830
#include "py/mphal.h"
2931
#include "py/obj.h"
3032
#include "py/runtime.h"
@@ -42,6 +44,9 @@
4244
#include "freertos/FreeRTOS.h"
4345

4446
#include "esp_sleep.h"
47+
#include "soc/rtc_periph.h"
48+
49+
static RTC_DATA_ATTR struct timeval sleep_enter_time;
4550

4651
void common_hal_mcu_delay_us(uint32_t delay) {
4752

@@ -80,9 +85,50 @@ void common_hal_mcu_reset(void) {
8085
}
8186

8287
void common_hal_mcu_sleep(void) {
88+
gettimeofday(&sleep_enter_time, NULL);
8389
esp_deep_sleep_start();
8490
}
8591

92+
int common_hal_mcu_get_sleep_time(void) {
93+
struct timeval now;
94+
gettimeofday(&now, NULL);
95+
return (now.tv_sec - sleep_enter_time.tv_sec) * 1000 + (now.tv_usec - sleep_enter_time.tv_usec) / 1000;
96+
}
97+
98+
mp_obj_t common_hal_mcu_get_wake_alarm(void) {
99+
switch (esp_sleep_get_wakeup_cause()) {
100+
case ESP_SLEEP_WAKEUP_TIMER: ;
101+
//Wake up from timer.
102+
time_alarm_obj_t *timer = m_new_obj(time_alarm_obj_t);
103+
timer->base.type = &time_alarm_type;
104+
return timer;
105+
case ESP_SLEEP_WAKEUP_EXT0: ;
106+
//Wake up from GPIO
107+
io_alarm_obj_t *ext0 = m_new_obj(io_alarm_obj_t);
108+
ext0->base.type = &io_alarm_type;
109+
return ext0;
110+
case ESP_SLEEP_WAKEUP_EXT1:
111+
//Wake up from GPIO, returns -> esp_sleep_get_ext1_wakeup_status()
112+
/*uint64_t wakeup_pin_mask = esp_sleep_get_ext1_wakeup_status();
113+
if (wakeup_pin_mask != 0) {
114+
int pin = __builtin_ffsll(wakeup_pin_mask) - 1;
115+
printf("Wake up from GPIO %d\n", pin);
116+
} else {
117+
printf("Wake up from GPIO\n");
118+
}*/
119+
break;
120+
case ESP_SLEEP_WAKEUP_TOUCHPAD:
121+
//TODO: implement TouchIO
122+
//Wake up from touch on pad, returns -> esp_sleep_get_touchpad_wakeup_status()
123+
break;
124+
case ESP_SLEEP_WAKEUP_UNDEFINED:
125+
default:
126+
//Not a deep sleep reset
127+
break;
128+
}
129+
return mp_const_none;
130+
}
131+
86132
// The singleton microcontroller.Processor object, bound to microcontroller.cpu
87133
// It currently only has properties, and no state.
88134
const mcu_processor_obj_t common_hal_mcu_processor_obj = {

shared-bindings/microcontroller/__init__.c

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,24 @@ STATIC mp_obj_t mcu_sleep(void) {
152152
}
153153
STATIC MP_DEFINE_CONST_FUN_OBJ_0(mcu_sleep_obj, mcu_sleep);
154154

155+
//| def getWakeAlarm() -> None:
156+
//| """This returns the alarm that triggered wakeup,
157+
//| also returns alarm specific parameters`.
158+
//|
159+
STATIC mp_obj_t mcu_get_wake_alarm(void) {
160+
return common_hal_mcu_get_wake_alarm();
161+
}
162+
STATIC MP_DEFINE_CONST_FUN_OBJ_0(mcu_get_wake_alarm_obj, mcu_get_wake_alarm);
163+
164+
//| def getSleepTime() -> None:
165+
//| """This returns the period of time in ms,
166+
//| in which the board was in deep sleep`.
167+
//|
168+
STATIC mp_obj_t mcu_get_sleep_time(void) {
169+
return mp_obj_new_int(common_hal_mcu_get_sleep_time());
170+
}
171+
STATIC MP_DEFINE_CONST_FUN_OBJ_0(mcu_get_sleep_time_obj, mcu_get_sleep_time);
172+
155173
//| nvm: Optional[ByteArray]
156174
//| """Available non-volatile memory.
157175
//| This object is the sole instance of `nvm.ByteArray` when available or ``None`` otherwise.
@@ -188,6 +206,8 @@ STATIC const mp_rom_map_elem_t mcu_module_globals_table[] = {
188206
{ MP_ROM_QSTR(MP_QSTR_on_next_reset), MP_ROM_PTR(&mcu_on_next_reset_obj) },
189207
{ MP_ROM_QSTR(MP_QSTR_reset), MP_ROM_PTR(&mcu_reset_obj) },
190208
{ MP_ROM_QSTR(MP_QSTR_sleep), MP_ROM_PTR(&mcu_sleep_obj) },
209+
{ MP_ROM_QSTR(MP_QSTR_getSleepTime), MP_ROM_PTR(&mcu_get_sleep_time_obj) },
210+
{ MP_ROM_QSTR(MP_QSTR_getWakeAlarm), MP_ROM_PTR(&mcu_get_wake_alarm_obj) },
191211
#if CIRCUITPY_INTERNAL_NVM_SIZE > 0
192212
{ MP_ROM_QSTR(MP_QSTR_nvm), MP_ROM_PTR(&common_hal_mcu_nvm_obj) },
193213
#else

shared-bindings/microcontroller/__init__.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@
3535

3636
#include "shared-bindings/microcontroller/RunMode.h"
3737

38+
#include "shared-bindings/io_alarm/__init__.h"
39+
#include "shared-bindings/time_alarm/__init__.h"
40+
3841
extern void common_hal_mcu_delay_us(uint32_t);
3942

4043
extern void common_hal_mcu_disable_interrupts(void);
@@ -44,6 +47,8 @@ extern void common_hal_mcu_on_next_reset(mcu_runmode_t runmode);
4447
extern void common_hal_mcu_reset(void);
4548

4649
extern void common_hal_mcu_sleep(void);
50+
extern int common_hal_mcu_get_sleep_time(void);
51+
extern mp_obj_t common_hal_mcu_get_wake_alarm(void);
4752

4853
extern const mp_obj_dict_t mcu_pin_globals;
4954

0 commit comments

Comments
 (0)