Skip to content

Commit 4d8ffdc

Browse files
microdev1tannewt
authored andcommitted
restructure alarm modules
1 parent e5ff55b commit 4d8ffdc

File tree

13 files changed

+191
-109
lines changed

13 files changed

+191
-109
lines changed
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
/*
2+
* This file is part of the MicroPython project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2016 Scott Shawcroft for Adafruit Industries
7+
* Copyright (c) 2019 Lucian Copeland for Adafruit Industries
8+
*
9+
* Permission is hereby granted, free of charge, to any person obtaining a copy
10+
* of this software and associated documentation files (the "Software"), to deal
11+
* in the Software without restriction, including without limitation the rights
12+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13+
* copies of the Software, and to permit persons to whom the Software is
14+
* furnished to do so, subject to the following conditions:
15+
*
16+
* The above copyright notice and this permission notice shall be included in
17+
* all copies or substantial portions of the Software.
18+
*
19+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25+
* THE SOFTWARE.
26+
*/
27+
28+
#include <sys/time.h>
29+
30+
#include "common-hal/alarm/__init__.h"
31+
#include "shared-bindings/alarm/__init__.h"
32+
33+
#include "esp_sleep.h"
34+
#include "soc/rtc_periph.h"
35+
#include "driver/rtc_io.h"
36+
37+
static RTC_DATA_ATTR struct timeval sleep_enter_time;
38+
static RTC_DATA_ATTR struct timeval sleep_exit_time;
39+
static RTC_DATA_ATTR uint8_t wake_io;
40+
41+
int common_hal_alarm_get_sleep_time(void) {
42+
return (sleep_exit_time.tv_sec - sleep_enter_time.tv_sec) * 1000 + (sleep_exit_time.tv_usec - sleep_enter_time.tv_usec) / 1000;
43+
}
44+
45+
void RTC_IRAM_ATTR esp_wake_deep_sleep(void) {
46+
esp_default_wake_deep_sleep();
47+
wake_io = rtc_gpio_get_level(6);
48+
gettimeofday(&sleep_exit_time, NULL);
49+
}
50+
51+
mp_obj_t common_hal_alarm_get_wake_alarm(void) {
52+
switch (esp_sleep_get_wakeup_cause()) {
53+
case ESP_SLEEP_WAKEUP_TIMER: ;
54+
//Wake up from timer.
55+
alarm_time_obj_t *timer = m_new_obj(alarm_time_obj_t);
56+
timer->base.type = &alarm_time_type;
57+
return timer;
58+
case ESP_SLEEP_WAKEUP_EXT0: ;
59+
//Wake up from GPIO
60+
/*alarm_io_obj_t *ext0 = m_new_obj(alarm_io_obj_t);
61+
ext0->base.type = &alarm_io_type;
62+
return ext0;*/
63+
return mp_obj_new_int(wake_io);
64+
case ESP_SLEEP_WAKEUP_EXT1:
65+
//Wake up from GPIO, returns -> esp_sleep_get_ext1_wakeup_status()
66+
/*uint64_t wakeup_pin_mask = esp_sleep_get_ext1_wakeup_status();
67+
if (wakeup_pin_mask != 0) {
68+
int pin = __builtin_ffsll(wakeup_pin_mask) - 1;
69+
printf("Wake up from GPIO %d\n", pin);
70+
} else {
71+
printf("Wake up from GPIO\n");
72+
}*/
73+
break;
74+
case ESP_SLEEP_WAKEUP_TOUCHPAD:
75+
//TODO: implement TouchIO
76+
//Wake up from touch on pad, returns -> esp_sleep_get_touchpad_wakeup_status()
77+
break;
78+
case ESP_SLEEP_WAKEUP_UNDEFINED:
79+
default:
80+
//Not a deep sleep reset
81+
break;
82+
}
83+
return mp_const_none;
84+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#ifndef MICROPY_INCLUDED_ESP32S2_COMMON_HAL_MICROCONTROLLER_ALARM_H
2+
#define MICROPY_INCLUDED_ESP32S2_COMMON_HAL_MICROCONTROLLER_ALARM_H
3+
4+
extern void esp_wake_deep_sleep(void);
5+
6+
#endif // MICROPY_INCLUDED_ESP32S2_COMMON_HAL_MICROCONTROLLER_ALARM_H

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

Lines changed: 1 addition & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,8 @@
2525
* THE SOFTWARE.
2626
*/
2727

28-
#include <sys/time.h>
29-
30-
#include "py/mphal.h"
3128
#include "py/obj.h"
29+
#include "py/mphal.h"
3230
#include "py/runtime.h"
3331

3432
#include "common-hal/microcontroller/Pin.h"
@@ -44,9 +42,6 @@
4442
#include "freertos/FreeRTOS.h"
4543

4644
#include "esp_sleep.h"
47-
#include "soc/rtc_periph.h"
48-
49-
static RTC_DATA_ATTR struct timeval sleep_enter_time;
5045

5146
void common_hal_mcu_delay_us(uint32_t delay) {
5247

@@ -85,50 +80,9 @@ void common_hal_mcu_reset(void) {
8580
}
8681

8782
void common_hal_mcu_sleep(void) {
88-
gettimeofday(&sleep_enter_time, NULL);
8983
esp_deep_sleep_start();
9084
}
9185

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-
alarm_time_obj_t *timer = m_new_obj(alarm_time_obj_t);
103-
timer->base.type = &alarm_time_type;
104-
return timer;
105-
case ESP_SLEEP_WAKEUP_EXT0: ;
106-
//Wake up from GPIO
107-
alarm_io_obj_t *ext0 = m_new_obj(alarm_io_obj_t);
108-
ext0->base.type = &alarm_io_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-
13286
// The singleton microcontroller.Processor object, bound to microcontroller.cpu
13387
// It currently only has properties, and no state.
13488
const mcu_processor_obj_t common_hal_mcu_processor_obj = {

ports/esp32s2/mpconfigport.mk

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ LONGINT_IMPL = MPZ
1414

1515
# These modules are implemented in ports/<port>/common-hal:
1616
CIRCUITPY_FULL_BUILD = 1
17+
CIRCUITPY_ALARM = 1
18+
CIRCUITPY_ALARM_IO = 1
19+
CIRCUITPY_ALARM_TIME = 1
1720
CIRCUITPY_AUDIOBUSIO = 0
1821
CIRCUITPY_AUDIOIO = 0
1922
CIRCUITPY_CANIO = 1
@@ -22,8 +25,7 @@ CIRCUITPY_FREQUENCYIO = 0
2225
CIRCUITPY_I2CPERIPHERAL = 0
2326
CIRCUITPY_ROTARYIO = 0
2427
CIRCUITPY_NVM = 0
25-
CIRCUITPY_ALARM_TIME = 1
26-
CIRCUITPY_ALARM_IO = 1
28+
2729
# We don't have enough endpoints to include MIDI.
2830
CIRCUITPY_USB_MIDI = 0
2931
CIRCUITPY_WIFI = 1

ports/esp32s2/supervisor/port.c

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,14 @@
3434
#include "freertos/FreeRTOS.h"
3535
#include "freertos/task.h"
3636

37-
#include "common-hal/microcontroller/Pin.h"
37+
#include "common-hal/alarm/__init__.h"
3838
#include "common-hal/analogio/AnalogOut.h"
3939
#include "common-hal/busio/I2C.h"
4040
#include "common-hal/busio/SPI.h"
4141
#include "common-hal/busio/UART.h"
42-
#include "common-hal/pulseio/PulseIn.h"
4342
#include "common-hal/pwmio/PWMOut.h"
43+
#include "common-hal/pulseio/PulseIn.h"
44+
#include "common-hal/microcontroller/Pin.h"
4445
#include "common-hal/wifi/__init__.h"
4546
#include "supervisor/memory.h"
4647
#include "supervisor/shared/tick.h"
@@ -87,6 +88,8 @@ safe_mode_t port_init(void) {
8788
return NO_HEAP;
8889
}
8990

91+
esp_wake_deep_sleep();
92+
9093
return NO_SAFE_MODE;
9194
}
9295

py/circuitpy_defns.mk

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,15 @@ endif
9999
ifeq ($(CIRCUITPY_AESIO),1)
100100
SRC_PATTERNS += aesio/%
101101
endif
102+
ifeq ($(CIRCUITPY_ALARM),1)
103+
SRC_PATTERNS += alarm/%
104+
endif
105+
ifeq ($(CIRCUITPY_ALARM_IO),1)
106+
SRC_PATTERNS += alarm_io/%
107+
endif
108+
ifeq ($(CIRCUITPY_ALARM_TIME),1)
109+
SRC_PATTERNS += alarm_time/%
110+
endif
102111
ifeq ($(CIRCUITPY_ANALOGIO),1)
103112
SRC_PATTERNS += analogio/%
104113
endif
@@ -259,12 +268,6 @@ endif
259268
ifeq ($(CIRCUITPY_TIME),1)
260269
SRC_PATTERNS += time/%
261270
endif
262-
ifeq ($(CIRCUITPY_ALARM_TIME),1)
263-
SRC_PATTERNS += alarm_time/%
264-
endif
265-
ifeq ($(CIRCUITPY_ALARM_IO),1)
266-
SRC_PATTERNS += alarm_io/%
267-
endif
268271
ifeq ($(CIRCUITPY_TOUCHIO),1)
269272
SRC_PATTERNS += touchio/%
270273
endif
@@ -304,6 +307,9 @@ SRC_COMMON_HAL_ALL = \
304307
_bleio/__init__.c \
305308
_pew/PewPew.c \
306309
_pew/__init__.c \
310+
alarm/__init__.c \
311+
alarm_io/__init__.c \
312+
alarm_time/__init__.c \
307313
analogio/AnalogIn.c \
308314
analogio/AnalogOut.c \
309315
analogio/__init__.c \
@@ -366,8 +372,6 @@ SRC_COMMON_HAL_ALL = \
366372
ssl/SSLContext.c \
367373
supervisor/Runtime.c \
368374
supervisor/__init__.c \
369-
alarm_time/__init__.c \
370-
alarm_io/__init__.c \
371375
watchdog/WatchDogMode.c \
372376
watchdog/WatchDogTimer.c \
373377
watchdog/__init__.c \

py/circuitpy_mpconfig.h

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,27 @@ extern const struct _mp_obj_module_t aesio_module;
240240
#define AESIO_MODULE
241241
#endif
242242

243+
#if CIRCUITPY_ALARM
244+
extern const struct _mp_obj_module_t alarm_module;
245+
#define ALARM_MODULE { MP_OBJ_NEW_QSTR(MP_QSTR_alarm), (mp_obj_t)&alarm_module },
246+
#else
247+
#define ALARM_MODULE
248+
#endif
249+
250+
#if CIRCUITPY_ALARM_IO
251+
extern const struct _mp_obj_module_t alarm_io_module;
252+
#define ALARM_IO_MODULE { MP_OBJ_NEW_QSTR(MP_QSTR_alarm_io), (mp_obj_t)&alarm_io_module },
253+
#else
254+
#define ALARM_IO_MODULE
255+
#endif
256+
257+
#if CIRCUITPY_ALARM_TIME
258+
extern const struct _mp_obj_module_t alarm_time_module;
259+
#define ALARM_TIME_MODULE { MP_OBJ_NEW_QSTR(MP_QSTR_alarm_time), (mp_obj_t)&alarm_time_module },
260+
#else
261+
#define ALARM_TIME_MODULE
262+
#endif
263+
243264
#if CIRCUITPY_ANALOGIO
244265
#define ANALOGIO_MODULE { MP_OBJ_NEW_QSTR(MP_QSTR_analogio), (mp_obj_t)&analogio_module },
245266
extern const struct _mp_obj_module_t analogio_module;
@@ -663,20 +684,6 @@ extern const struct _mp_obj_module_t time_module;
663684
#define TIME_MODULE_ALT_NAME
664685
#endif
665686

666-
#if CIRCUITPY_ALARM_TIME
667-
extern const struct _mp_obj_module_t alarm_time_module;
668-
#define ALARM_TIME_MODULE { MP_OBJ_NEW_QSTR(MP_QSTR_alarm_time), (mp_obj_t)&alarm_time_module },
669-
#else
670-
#define ALARM_TIME_MODULE
671-
#endif
672-
673-
#if CIRCUITPY_ALARM_IO
674-
extern const struct _mp_obj_module_t alarm_io_module;
675-
#define ALARM_IO_MODULE { MP_OBJ_NEW_QSTR(MP_QSTR_alarm_io), (mp_obj_t)&alarm_io_module },
676-
#else
677-
#define ALARM_IO_MODULE
678-
#endif
679-
680687
#if CIRCUITPY_TOUCHIO
681688
extern const struct _mp_obj_module_t touchio_module;
682689
#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;
777784
// Some are omitted because they're in MICROPY_PORT_BUILTIN_MODULE_WEAK_LINKS above.
778785
#define MICROPY_PORT_BUILTIN_MODULES_STRONG_LINKS \
779786
AESIO_MODULE \
787+
ALARM_MODULE \
788+
ALARM_IO_MODULE \
789+
ALARM_TIME_MODULE \
780790
ANALOGIO_MODULE \
781791
AUDIOBUSIO_MODULE \
782792
AUDIOCORE_MODULE \
@@ -835,8 +845,6 @@ extern const struct _mp_obj_module_t wifi_module;
835845
STRUCT_MODULE \
836846
SUPERVISOR_MODULE \
837847
TOUCHIO_MODULE \
838-
ALARM_TIME_MODULE \
839-
ALARM_IO_MODULE \
840848
UHEAP_MODULE \
841849
USB_HID_MODULE \
842850
USB_MIDI_MODULE \

py/circuitpy_mpconfig.mk

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,15 @@ CFLAGS += -DMICROPY_PY_ASYNC_AWAIT=$(MICROPY_PY_ASYNC_AWAIT)
3939
CIRCUITPY_AESIO ?= 0
4040
CFLAGS += -DCIRCUITPY_AESIO=$(CIRCUITPY_AESIO)
4141

42+
CIRCUITPY_ALARM ?= 0
43+
CFLAGS += -DCIRCUITPY_ALARM=$(CIRCUITPY_ALARM)
44+
45+
CIRCUITPY_ALARM_IO ?= 0
46+
CFLAGS += -DCIRCUITPY_ALARM_IO=$(CIRCUITPY_ALARM_IO)
47+
48+
CIRCUITPY_ALARM_TIME ?= 0
49+
CFLAGS += -DCIRCUITPY_ALARM_TIME=$(CIRCUITPY_ALARM_TIME)
50+
4251
CIRCUITPY_ANALOGIO ?= 1
4352
CFLAGS += -DCIRCUITPY_ANALOGIO=$(CIRCUITPY_ANALOGIO)
4453

@@ -234,12 +243,6 @@ CFLAGS += -DCIRCUITPY_TERMINALIO=$(CIRCUITPY_TERMINALIO)
234243
CIRCUITPY_TIME ?= 1
235244
CFLAGS += -DCIRCUITPY_TIME=$(CIRCUITPY_TIME)
236245

237-
CIRCUITPY_ALARM_TIME ?= 0
238-
CFLAGS += -DCIRCUITPY_ALARM_TIME=$(CIRCUITPY_ALARM_TIME)
239-
240-
CIRCUITPY_ALARM_IO ?= 0
241-
CFLAGS += -DCIRCUITPY_ALARM_IO=$(CIRCUITPY_ALARM_IO)
242-
243246
# touchio might be native or generic. See circuitpy_defns.mk.
244247
CIRCUITPY_TOUCHIO_USE_NATIVE ?= 0
245248
CFLAGS += -DCIRCUITPY_TOUCHIO_USE_NATIVE=$(CIRCUITPY_TOUCHIO_USE_NATIVE)

shared-bindings/alarm/__init__.c

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#include "shared-bindings/alarm/__init__.h"
2+
3+
//| def getWakeAlarm() -> None:
4+
//| """This returns the alarm that triggered wakeup,
5+
//| also returns alarm specific parameters`.
6+
//|
7+
STATIC mp_obj_t alarm_get_wake_alarm(void) {
8+
return common_hal_alarm_get_wake_alarm();
9+
}
10+
STATIC MP_DEFINE_CONST_FUN_OBJ_0(alarm_get_wake_alarm_obj, alarm_get_wake_alarm);
11+
12+
//| def getSleepTime() -> None:
13+
//| """This returns the period of time in ms,
14+
//| in which the board was in deep sleep`.
15+
//|
16+
STATIC mp_obj_t alarm_get_sleep_time(void) {
17+
return mp_obj_new_int(common_hal_alarm_get_sleep_time());
18+
}
19+
STATIC MP_DEFINE_CONST_FUN_OBJ_0(alarm_get_sleep_time_obj, alarm_get_sleep_time);
20+
21+
STATIC const mp_rom_map_elem_t alarm_module_globals_table[] = {
22+
{ MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_alarm) },
23+
{ MP_ROM_QSTR(MP_QSTR_getSleepTime), MP_ROM_PTR(&alarm_get_sleep_time_obj) },
24+
{ MP_ROM_QSTR(MP_QSTR_getWakeAlarm), MP_ROM_PTR(&alarm_get_wake_alarm_obj) },
25+
};
26+
27+
STATIC MP_DEFINE_CONST_DICT(alarm_module_globals, alarm_module_globals_table);
28+
29+
const mp_obj_module_t alarm_module = {
30+
.base = { &mp_type_module },
31+
.globals = (mp_obj_dict_t*)&alarm_module_globals,
32+
};

0 commit comments

Comments
 (0)