Add function to get time elapsed during sleep
This commit is contained in:
parent
e310b871c8
commit
05a3f203db
@ -25,6 +25,8 @@
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
|
||||
#include <sys/time.h>
|
||||
|
||||
#include "py/mphal.h"
|
||||
#include "py/obj.h"
|
||||
#include "py/runtime.h"
|
||||
@ -42,6 +44,9 @@
|
||||
#include "freertos/FreeRTOS.h"
|
||||
|
||||
#include "esp_sleep.h"
|
||||
#include "soc/rtc_periph.h"
|
||||
|
||||
static RTC_DATA_ATTR struct timeval sleep_enter_time;
|
||||
|
||||
void common_hal_mcu_delay_us(uint32_t delay) {
|
||||
|
||||
@ -80,9 +85,50 @@ void common_hal_mcu_reset(void) {
|
||||
}
|
||||
|
||||
void common_hal_mcu_sleep(void) {
|
||||
gettimeofday(&sleep_enter_time, NULL);
|
||||
esp_deep_sleep_start();
|
||||
}
|
||||
|
||||
int common_hal_mcu_get_sleep_time(void) {
|
||||
struct timeval now;
|
||||
gettimeofday(&now, NULL);
|
||||
return (now.tv_sec - sleep_enter_time.tv_sec) * 1000 + (now.tv_usec - sleep_enter_time.tv_usec) / 1000;
|
||||
}
|
||||
|
||||
mp_obj_t common_hal_mcu_get_wake_alarm(void) {
|
||||
switch (esp_sleep_get_wakeup_cause()) {
|
||||
case ESP_SLEEP_WAKEUP_TIMER: ;
|
||||
//Wake up from timer.
|
||||
time_alarm_obj_t *timer = m_new_obj(time_alarm_obj_t);
|
||||
timer->base.type = &time_alarm_type;
|
||||
return timer;
|
||||
case ESP_SLEEP_WAKEUP_EXT0: ;
|
||||
//Wake up from GPIO
|
||||
io_alarm_obj_t *ext0 = m_new_obj(io_alarm_obj_t);
|
||||
ext0->base.type = &io_alarm_type;
|
||||
return ext0;
|
||||
case ESP_SLEEP_WAKEUP_EXT1:
|
||||
//Wake up from GPIO, returns -> esp_sleep_get_ext1_wakeup_status()
|
||||
/*uint64_t wakeup_pin_mask = esp_sleep_get_ext1_wakeup_status();
|
||||
if (wakeup_pin_mask != 0) {
|
||||
int pin = __builtin_ffsll(wakeup_pin_mask) - 1;
|
||||
printf("Wake up from GPIO %d\n", pin);
|
||||
} else {
|
||||
printf("Wake up from GPIO\n");
|
||||
}*/
|
||||
break;
|
||||
case ESP_SLEEP_WAKEUP_TOUCHPAD:
|
||||
//TODO: implement TouchIO
|
||||
//Wake up from touch on pad, returns -> esp_sleep_get_touchpad_wakeup_status()
|
||||
break;
|
||||
case ESP_SLEEP_WAKEUP_UNDEFINED:
|
||||
default:
|
||||
//Not a deep sleep reset
|
||||
break;
|
||||
}
|
||||
return mp_const_none;
|
||||
}
|
||||
|
||||
// The singleton microcontroller.Processor object, bound to microcontroller.cpu
|
||||
// It currently only has properties, and no state.
|
||||
const mcu_processor_obj_t common_hal_mcu_processor_obj = {
|
||||
|
@ -152,6 +152,24 @@ STATIC mp_obj_t mcu_sleep(void) {
|
||||
}
|
||||
STATIC MP_DEFINE_CONST_FUN_OBJ_0(mcu_sleep_obj, mcu_sleep);
|
||||
|
||||
//| def getWakeAlarm() -> None:
|
||||
//| """This returns the alarm that triggered wakeup,
|
||||
//| also returns alarm specific parameters`.
|
||||
//|
|
||||
STATIC mp_obj_t mcu_get_wake_alarm(void) {
|
||||
return common_hal_mcu_get_wake_alarm();
|
||||
}
|
||||
STATIC MP_DEFINE_CONST_FUN_OBJ_0(mcu_get_wake_alarm_obj, mcu_get_wake_alarm);
|
||||
|
||||
//| def getSleepTime() -> None:
|
||||
//| """This returns the period of time in ms,
|
||||
//| in which the board was in deep sleep`.
|
||||
//|
|
||||
STATIC mp_obj_t mcu_get_sleep_time(void) {
|
||||
return mp_obj_new_int(common_hal_mcu_get_sleep_time());
|
||||
}
|
||||
STATIC MP_DEFINE_CONST_FUN_OBJ_0(mcu_get_sleep_time_obj, mcu_get_sleep_time);
|
||||
|
||||
//| nvm: Optional[ByteArray]
|
||||
//| """Available non-volatile memory.
|
||||
//| 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[] = {
|
||||
{ MP_ROM_QSTR(MP_QSTR_on_next_reset), MP_ROM_PTR(&mcu_on_next_reset_obj) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_reset), MP_ROM_PTR(&mcu_reset_obj) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_sleep), MP_ROM_PTR(&mcu_sleep_obj) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_getSleepTime), MP_ROM_PTR(&mcu_get_sleep_time_obj) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_getWakeAlarm), MP_ROM_PTR(&mcu_get_wake_alarm_obj) },
|
||||
#if CIRCUITPY_INTERNAL_NVM_SIZE > 0
|
||||
{ MP_ROM_QSTR(MP_QSTR_nvm), MP_ROM_PTR(&common_hal_mcu_nvm_obj) },
|
||||
#else
|
||||
|
@ -35,6 +35,9 @@
|
||||
|
||||
#include "shared-bindings/microcontroller/RunMode.h"
|
||||
|
||||
#include "shared-bindings/io_alarm/__init__.h"
|
||||
#include "shared-bindings/time_alarm/__init__.h"
|
||||
|
||||
extern void common_hal_mcu_delay_us(uint32_t);
|
||||
|
||||
extern void common_hal_mcu_disable_interrupts(void);
|
||||
@ -44,6 +47,8 @@ extern void common_hal_mcu_on_next_reset(mcu_runmode_t runmode);
|
||||
extern void common_hal_mcu_reset(void);
|
||||
|
||||
extern void common_hal_mcu_sleep(void);
|
||||
extern int common_hal_mcu_get_sleep_time(void);
|
||||
extern mp_obj_t common_hal_mcu_get_wake_alarm(void);
|
||||
|
||||
extern const mp_obj_dict_t mcu_pin_globals;
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user