restructure alarm modules

This commit is contained in:
microDev 2020-09-24 10:59:04 +05:30 committed by Scott Shawcroft
parent e5ff55b15c
commit 4d8ffdca8d
No known key found for this signature in database
GPG Key ID: 0DFD512649C052DA
13 changed files with 191 additions and 109 deletions

View File

@ -0,0 +1,84 @@
/*
* This file is part of the MicroPython project, http://micropython.org/
*
* The MIT License (MIT)
*
* Copyright (c) 2016 Scott Shawcroft for Adafruit Industries
* Copyright (c) 2019 Lucian Copeland for Adafruit Industries
*
* 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 <sys/time.h>
#include "common-hal/alarm/__init__.h"
#include "shared-bindings/alarm/__init__.h"
#include "esp_sleep.h"
#include "soc/rtc_periph.h"
#include "driver/rtc_io.h"
static RTC_DATA_ATTR struct timeval sleep_enter_time;
static RTC_DATA_ATTR struct timeval sleep_exit_time;
static RTC_DATA_ATTR uint8_t wake_io;
int common_hal_alarm_get_sleep_time(void) {
return (sleep_exit_time.tv_sec - sleep_enter_time.tv_sec) * 1000 + (sleep_exit_time.tv_usec - sleep_enter_time.tv_usec) / 1000;
}
void RTC_IRAM_ATTR esp_wake_deep_sleep(void) {
esp_default_wake_deep_sleep();
wake_io = rtc_gpio_get_level(6);
gettimeofday(&sleep_exit_time, NULL);
}
mp_obj_t common_hal_alarm_get_wake_alarm(void) {
switch (esp_sleep_get_wakeup_cause()) {
case ESP_SLEEP_WAKEUP_TIMER: ;
//Wake up from timer.
alarm_time_obj_t *timer = m_new_obj(alarm_time_obj_t);
timer->base.type = &alarm_time_type;
return timer;
case ESP_SLEEP_WAKEUP_EXT0: ;
//Wake up from GPIO
/*alarm_io_obj_t *ext0 = m_new_obj(alarm_io_obj_t);
ext0->base.type = &alarm_io_type;
return ext0;*/
return mp_obj_new_int(wake_io);
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;
}

View File

@ -0,0 +1,6 @@
#ifndef MICROPY_INCLUDED_ESP32S2_COMMON_HAL_MICROCONTROLLER_ALARM_H
#define MICROPY_INCLUDED_ESP32S2_COMMON_HAL_MICROCONTROLLER_ALARM_H
extern void esp_wake_deep_sleep(void);
#endif // MICROPY_INCLUDED_ESP32S2_COMMON_HAL_MICROCONTROLLER_ALARM_H

View File

@ -25,10 +25,8 @@
* THE SOFTWARE.
*/
#include <sys/time.h>
#include "py/mphal.h"
#include "py/obj.h"
#include "py/mphal.h"
#include "py/runtime.h"
#include "common-hal/microcontroller/Pin.h"
@ -44,9 +42,6 @@
#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) {
@ -85,50 +80,9 @@ 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.
alarm_time_obj_t *timer = m_new_obj(alarm_time_obj_t);
timer->base.type = &alarm_time_type;
return timer;
case ESP_SLEEP_WAKEUP_EXT0: ;
//Wake up from GPIO
alarm_io_obj_t *ext0 = m_new_obj(alarm_io_obj_t);
ext0->base.type = &alarm_io_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 = {

View File

@ -14,6 +14,9 @@ LONGINT_IMPL = MPZ
# These modules are implemented in ports/<port>/common-hal:
CIRCUITPY_FULL_BUILD = 1
CIRCUITPY_ALARM = 1
CIRCUITPY_ALARM_IO = 1
CIRCUITPY_ALARM_TIME = 1
CIRCUITPY_AUDIOBUSIO = 0
CIRCUITPY_AUDIOIO = 0
CIRCUITPY_CANIO = 1
@ -22,8 +25,7 @@ CIRCUITPY_FREQUENCYIO = 0
CIRCUITPY_I2CPERIPHERAL = 0
CIRCUITPY_ROTARYIO = 0
CIRCUITPY_NVM = 0
CIRCUITPY_ALARM_TIME = 1
CIRCUITPY_ALARM_IO = 1
# We don't have enough endpoints to include MIDI.
CIRCUITPY_USB_MIDI = 0
CIRCUITPY_WIFI = 1

View File

@ -34,13 +34,14 @@
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "common-hal/microcontroller/Pin.h"
#include "common-hal/alarm/__init__.h"
#include "common-hal/analogio/AnalogOut.h"
#include "common-hal/busio/I2C.h"
#include "common-hal/busio/SPI.h"
#include "common-hal/busio/UART.h"
#include "common-hal/pulseio/PulseIn.h"
#include "common-hal/pwmio/PWMOut.h"
#include "common-hal/pulseio/PulseIn.h"
#include "common-hal/microcontroller/Pin.h"
#include "common-hal/wifi/__init__.h"
#include "supervisor/memory.h"
#include "supervisor/shared/tick.h"
@ -87,6 +88,8 @@ safe_mode_t port_init(void) {
return NO_HEAP;
}
esp_wake_deep_sleep();
return NO_SAFE_MODE;
}

View File

@ -99,6 +99,15 @@ endif
ifeq ($(CIRCUITPY_AESIO),1)
SRC_PATTERNS += aesio/%
endif
ifeq ($(CIRCUITPY_ALARM),1)
SRC_PATTERNS += alarm/%
endif
ifeq ($(CIRCUITPY_ALARM_IO),1)
SRC_PATTERNS += alarm_io/%
endif
ifeq ($(CIRCUITPY_ALARM_TIME),1)
SRC_PATTERNS += alarm_time/%
endif
ifeq ($(CIRCUITPY_ANALOGIO),1)
SRC_PATTERNS += analogio/%
endif
@ -259,12 +268,6 @@ endif
ifeq ($(CIRCUITPY_TIME),1)
SRC_PATTERNS += time/%
endif
ifeq ($(CIRCUITPY_ALARM_TIME),1)
SRC_PATTERNS += alarm_time/%
endif
ifeq ($(CIRCUITPY_ALARM_IO),1)
SRC_PATTERNS += alarm_io/%
endif
ifeq ($(CIRCUITPY_TOUCHIO),1)
SRC_PATTERNS += touchio/%
endif
@ -304,6 +307,9 @@ SRC_COMMON_HAL_ALL = \
_bleio/__init__.c \
_pew/PewPew.c \
_pew/__init__.c \
alarm/__init__.c \
alarm_io/__init__.c \
alarm_time/__init__.c \
analogio/AnalogIn.c \
analogio/AnalogOut.c \
analogio/__init__.c \
@ -366,8 +372,6 @@ SRC_COMMON_HAL_ALL = \
ssl/SSLContext.c \
supervisor/Runtime.c \
supervisor/__init__.c \
alarm_time/__init__.c \
alarm_io/__init__.c \
watchdog/WatchDogMode.c \
watchdog/WatchDogTimer.c \
watchdog/__init__.c \

View File

@ -240,6 +240,27 @@ extern const struct _mp_obj_module_t aesio_module;
#define AESIO_MODULE
#endif
#if CIRCUITPY_ALARM
extern const struct _mp_obj_module_t alarm_module;
#define ALARM_MODULE { MP_OBJ_NEW_QSTR(MP_QSTR_alarm), (mp_obj_t)&alarm_module },
#else
#define ALARM_MODULE
#endif
#if CIRCUITPY_ALARM_IO
extern const struct _mp_obj_module_t alarm_io_module;
#define ALARM_IO_MODULE { MP_OBJ_NEW_QSTR(MP_QSTR_alarm_io), (mp_obj_t)&alarm_io_module },
#else
#define ALARM_IO_MODULE
#endif
#if CIRCUITPY_ALARM_TIME
extern const struct _mp_obj_module_t alarm_time_module;
#define ALARM_TIME_MODULE { MP_OBJ_NEW_QSTR(MP_QSTR_alarm_time), (mp_obj_t)&alarm_time_module },
#else
#define ALARM_TIME_MODULE
#endif
#if CIRCUITPY_ANALOGIO
#define ANALOGIO_MODULE { MP_OBJ_NEW_QSTR(MP_QSTR_analogio), (mp_obj_t)&analogio_module },
extern const struct _mp_obj_module_t analogio_module;
@ -663,20 +684,6 @@ extern const struct _mp_obj_module_t time_module;
#define TIME_MODULE_ALT_NAME
#endif
#if CIRCUITPY_ALARM_TIME
extern const struct _mp_obj_module_t alarm_time_module;
#define ALARM_TIME_MODULE { MP_OBJ_NEW_QSTR(MP_QSTR_alarm_time), (mp_obj_t)&alarm_time_module },
#else
#define ALARM_TIME_MODULE
#endif
#if CIRCUITPY_ALARM_IO
extern const struct _mp_obj_module_t alarm_io_module;
#define ALARM_IO_MODULE { MP_OBJ_NEW_QSTR(MP_QSTR_alarm_io), (mp_obj_t)&alarm_io_module },
#else
#define ALARM_IO_MODULE
#endif
#if CIRCUITPY_TOUCHIO
extern const struct _mp_obj_module_t touchio_module;
#define TOUCHIO_MODULE { MP_OBJ_NEW_QSTR(MP_QSTR_touchio), (mp_obj_t)&touchio_module },
@ -777,6 +784,9 @@ extern const struct _mp_obj_module_t wifi_module;
// Some are omitted because they're in MICROPY_PORT_BUILTIN_MODULE_WEAK_LINKS above.
#define MICROPY_PORT_BUILTIN_MODULES_STRONG_LINKS \
AESIO_MODULE \
ALARM_MODULE \
ALARM_IO_MODULE \
ALARM_TIME_MODULE \
ANALOGIO_MODULE \
AUDIOBUSIO_MODULE \
AUDIOCORE_MODULE \
@ -835,8 +845,6 @@ extern const struct _mp_obj_module_t wifi_module;
STRUCT_MODULE \
SUPERVISOR_MODULE \
TOUCHIO_MODULE \
ALARM_TIME_MODULE \
ALARM_IO_MODULE \
UHEAP_MODULE \
USB_HID_MODULE \
USB_MIDI_MODULE \

View File

@ -39,6 +39,15 @@ CFLAGS += -DMICROPY_PY_ASYNC_AWAIT=$(MICROPY_PY_ASYNC_AWAIT)
CIRCUITPY_AESIO ?= 0
CFLAGS += -DCIRCUITPY_AESIO=$(CIRCUITPY_AESIO)
CIRCUITPY_ALARM ?= 0
CFLAGS += -DCIRCUITPY_ALARM=$(CIRCUITPY_ALARM)
CIRCUITPY_ALARM_IO ?= 0
CFLAGS += -DCIRCUITPY_ALARM_IO=$(CIRCUITPY_ALARM_IO)
CIRCUITPY_ALARM_TIME ?= 0
CFLAGS += -DCIRCUITPY_ALARM_TIME=$(CIRCUITPY_ALARM_TIME)
CIRCUITPY_ANALOGIO ?= 1
CFLAGS += -DCIRCUITPY_ANALOGIO=$(CIRCUITPY_ANALOGIO)
@ -234,12 +243,6 @@ CFLAGS += -DCIRCUITPY_TERMINALIO=$(CIRCUITPY_TERMINALIO)
CIRCUITPY_TIME ?= 1
CFLAGS += -DCIRCUITPY_TIME=$(CIRCUITPY_TIME)
CIRCUITPY_ALARM_TIME ?= 0
CFLAGS += -DCIRCUITPY_ALARM_TIME=$(CIRCUITPY_ALARM_TIME)
CIRCUITPY_ALARM_IO ?= 0
CFLAGS += -DCIRCUITPY_ALARM_IO=$(CIRCUITPY_ALARM_IO)
# touchio might be native or generic. See circuitpy_defns.mk.
CIRCUITPY_TOUCHIO_USE_NATIVE ?= 0
CFLAGS += -DCIRCUITPY_TOUCHIO_USE_NATIVE=$(CIRCUITPY_TOUCHIO_USE_NATIVE)

View File

@ -0,0 +1,32 @@
#include "shared-bindings/alarm/__init__.h"
//| def getWakeAlarm() -> None:
//| """This returns the alarm that triggered wakeup,
//| also returns alarm specific parameters`.
//|
STATIC mp_obj_t alarm_get_wake_alarm(void) {
return common_hal_alarm_get_wake_alarm();
}
STATIC MP_DEFINE_CONST_FUN_OBJ_0(alarm_get_wake_alarm_obj, alarm_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 alarm_get_sleep_time(void) {
return mp_obj_new_int(common_hal_alarm_get_sleep_time());
}
STATIC MP_DEFINE_CONST_FUN_OBJ_0(alarm_get_sleep_time_obj, alarm_get_sleep_time);
STATIC const mp_rom_map_elem_t alarm_module_globals_table[] = {
{ MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_alarm) },
{ MP_ROM_QSTR(MP_QSTR_getSleepTime), MP_ROM_PTR(&alarm_get_sleep_time_obj) },
{ MP_ROM_QSTR(MP_QSTR_getWakeAlarm), MP_ROM_PTR(&alarm_get_wake_alarm_obj) },
};
STATIC MP_DEFINE_CONST_DICT(alarm_module_globals, alarm_module_globals_table);
const mp_obj_module_t alarm_module = {
.base = { &mp_type_module },
.globals = (mp_obj_dict_t*)&alarm_module_globals,
};

View File

@ -0,0 +1,12 @@
#ifndef MICROPY_INCLUDED_SHARED_BINDINGS_ALARM___INIT___H
#define MICROPY_INCLUDED_SHARED_BINDINGS_ALARM___INIT___H
#include "py/obj.h"
#include "shared-bindings/alarm_io/__init__.h"
#include "shared-bindings/alarm_time/__init__.h"
extern int common_hal_alarm_get_sleep_time(void);
extern mp_obj_t common_hal_alarm_get_wake_alarm(void);
#endif // MICROPY_INCLUDED_SHARED_BINDINGS_ALARM___INIT___H

View File

@ -9,7 +9,7 @@ typedef struct {
extern const mp_obj_type_t alarm_time_type;
extern void common_hal_alarm_time_duration(uint32_t);
extern void common_hal_alarm_time_duration (uint32_t);
extern void common_hal_alarm_time_disable (void);
#endif //MICROPY_INCLUDED_SHARED_BINDINGS_ALARM_TIME___INIT___H

View File

@ -39,7 +39,6 @@
#include "shared-bindings/microcontroller/Pin.h"
#include "shared-bindings/microcontroller/Processor.h"
#include "py/runtime.h"
#include "supervisor/shared/translate.h"
//| """Pin references and cpu functionality
@ -152,24 +151,6 @@ 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.
@ -206,8 +187,6 @@ 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

View File

@ -28,16 +28,13 @@
#ifndef MICROPY_INCLUDED_SHARED_BINDINGS_MICROCONTROLLER___INIT___H
#define MICROPY_INCLUDED_SHARED_BINDINGS_MICROCONTROLLER___INIT___H
#include "py/mpconfig.h"
#include "py/obj.h"
#include "py/mpconfig.h"
#include "common-hal/microcontroller/Processor.h"
#include "shared-bindings/microcontroller/RunMode.h"
#include "shared-bindings/alarm_io/__init__.h"
#include "shared-bindings/alarm_time/__init__.h"
extern void common_hal_mcu_delay_us(uint32_t);
extern void common_hal_mcu_disable_interrupts(void);
@ -47,8 +44,6 @@ 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;