Skip to content

Commit 44033a1

Browse files
MrSurlydpgeorge
authored andcommitted
esp32/machine_rtc: Add RTC class to machine module with sleep impl.
The machine.RTC class is added and the machine module is updated with the implementation of sleep, deepsleep, reset_cause and wake_reason.
1 parent 73d1d20 commit 44033a1

6 files changed

Lines changed: 349 additions & 1 deletion

File tree

ports/esp32/Makefile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,7 @@ SRC_C = \
152152
machine_hw_spi.c \
153153
machine_wdt.c \
154154
mpthreadport.c \
155+
machine_rtc.c \
155156
$(SRC_MOD)
156157

157158
EXTMOD_SRC_C = $(addprefix extmod/,\
@@ -251,6 +252,7 @@ ESPIDF_ESP32_O = $(addprefix $(ESPCOMP)/esp32/,\
251252
dport_access.o \
252253
wifi_init.o \
253254
wifi_internal.o \
255+
sleep_modes.o \
254256
)
255257

256258
ESPIDF_HEAP_O = $(addprefix $(ESPCOMP)/heap/,\

ports/esp32/machine_rtc.c

Lines changed: 162 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,162 @@
1+
/*
2+
* This file is part of the MicroPython project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2017 "Eric Poulsen" <eric@zyxod.com>
7+
* Copyright (c) 2017 "Tom Manning" <tom@manningetal.com>
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 <stdio.h>
29+
#include <string.h>
30+
31+
#include <time.h>
32+
#include <sys/time.h>
33+
#include "driver/gpio.h"
34+
35+
#include "py/nlr.h"
36+
#include "py/obj.h"
37+
#include "py/runtime.h"
38+
#include "py/mphal.h"
39+
#include "timeutils.h"
40+
#include "modmachine.h"
41+
#include "machine_rtc.h"
42+
43+
typedef struct _machine_rtc_obj_t {
44+
mp_obj_base_t base;
45+
} machine_rtc_obj_t;
46+
47+
#define MEM_MAGIC 0x75507921
48+
/* There is 8K of rtc_slow_memory, but some is used by the system software
49+
If the USER_MAXLEN is set to high, the following compile error will happen:
50+
region `rtc_slow_seg' overflowed by N bytes
51+
The current system software allows almost 4096 to be used.
52+
To avoid running into issues if the system software uses more, 2048 was picked as a max length
53+
*/
54+
#define MEM_USER_MAXLEN 2048
55+
RTC_DATA_ATTR uint32_t rtc_user_mem_magic;
56+
RTC_DATA_ATTR uint32_t rtc_user_mem_len;
57+
RTC_DATA_ATTR uint8_t rtc_user_mem_data[MEM_USER_MAXLEN];
58+
59+
// singleton RTC object
60+
STATIC const machine_rtc_obj_t machine_rtc_obj = {{&machine_rtc_type}};
61+
62+
machine_rtc_config_t machine_rtc_config = {
63+
.ext1_pins = 0,
64+
.ext0_pin = -1
65+
};
66+
67+
STATIC mp_obj_t machine_rtc_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args) {
68+
// check arguments
69+
mp_arg_check_num(n_args, n_kw, 0, 0, false);
70+
71+
// return constant object
72+
return (mp_obj_t)&machine_rtc_obj;
73+
}
74+
75+
STATIC mp_obj_t machine_rtc_datetime_helper(mp_uint_t n_args, const mp_obj_t *args) {
76+
if (n_args == 1) {
77+
// Get time
78+
79+
struct timeval tv;
80+
81+
gettimeofday(&tv, NULL);
82+
timeutils_struct_time_t tm;
83+
84+
timeutils_seconds_since_2000_to_struct_time(tv.tv_sec, &tm);
85+
86+
mp_obj_t tuple[8] = {
87+
mp_obj_new_int(tm.tm_year),
88+
mp_obj_new_int(tm.tm_mon),
89+
mp_obj_new_int(tm.tm_mday),
90+
mp_obj_new_int(tm.tm_wday),
91+
mp_obj_new_int(tm.tm_hour),
92+
mp_obj_new_int(tm.tm_min),
93+
mp_obj_new_int(tm.tm_sec),
94+
mp_obj_new_int(tv.tv_usec)
95+
};
96+
97+
return mp_obj_new_tuple(8, tuple);
98+
} else {
99+
// Set time
100+
101+
mp_obj_t *items;
102+
mp_obj_get_array_fixed_n(args[1], 8, &items);
103+
104+
struct timeval tv = {0};
105+
tv.tv_sec = timeutils_seconds_since_2000(mp_obj_get_int(items[0]), mp_obj_get_int(items[1]), mp_obj_get_int(items[2]), mp_obj_get_int(items[4]), mp_obj_get_int(items[5]), mp_obj_get_int(items[6]));
106+
settimeofday(&tv, NULL);
107+
108+
return mp_const_none;
109+
}
110+
}
111+
STATIC mp_obj_t machine_rtc_datetime(mp_uint_t n_args, const mp_obj_t *args) {
112+
return machine_rtc_datetime_helper(n_args, args);
113+
}
114+
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(machine_rtc_datetime_obj, 1, 2, machine_rtc_datetime);
115+
116+
STATIC mp_obj_t machine_rtc_init(mp_obj_t self_in, mp_obj_t date) {
117+
mp_obj_t args[2] = {self_in, date};
118+
machine_rtc_datetime_helper(2, args);
119+
120+
if (rtc_user_mem_magic != MEM_MAGIC) {
121+
rtc_user_mem_magic = MEM_MAGIC;
122+
rtc_user_mem_len = 0;
123+
}
124+
return mp_const_none;
125+
}
126+
STATIC MP_DEFINE_CONST_FUN_OBJ_2(machine_rtc_init_obj, machine_rtc_init);
127+
128+
STATIC mp_obj_t machine_rtc_memory(mp_uint_t n_args, const mp_obj_t *args) {
129+
if (n_args == 1) {
130+
// read RTC memory
131+
uint32_t len = rtc_user_mem_len;
132+
uint8_t rtcram[MEM_USER_MAXLEN];
133+
memcpy( (char *) rtcram, (char *) rtc_user_mem_data, len);
134+
return mp_obj_new_bytes(rtcram, len);
135+
} else {
136+
// write RTC memory
137+
mp_buffer_info_t bufinfo;
138+
mp_get_buffer_raise(args[1], &bufinfo, MP_BUFFER_READ);
139+
140+
if (bufinfo.len > MEM_USER_MAXLEN) {
141+
mp_raise_ValueError("buffer too long");
142+
}
143+
memcpy( (char *) rtc_user_mem_data, (char *) bufinfo.buf, bufinfo.len);
144+
rtc_user_mem_len = bufinfo.len;
145+
return mp_const_none;
146+
}
147+
}
148+
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(machine_rtc_memory_obj, 1, 2, machine_rtc_memory);
149+
150+
STATIC const mp_map_elem_t machine_rtc_locals_dict_table[] = {
151+
{ MP_OBJ_NEW_QSTR(MP_QSTR_init), (mp_obj_t)&machine_rtc_datetime_obj },
152+
{ MP_OBJ_NEW_QSTR(MP_QSTR_datetime), (mp_obj_t)&machine_rtc_datetime_obj },
153+
{ MP_OBJ_NEW_QSTR(MP_QSTR_memory), (mp_obj_t)&machine_rtc_memory_obj },
154+
};
155+
STATIC MP_DEFINE_CONST_DICT(machine_rtc_locals_dict, machine_rtc_locals_dict_table);
156+
157+
const mp_obj_type_t machine_rtc_type = {
158+
{ &mp_type_type },
159+
.name = MP_QSTR_RTC,
160+
.make_new = machine_rtc_make_new,
161+
.locals_dict = (mp_obj_t)&machine_rtc_locals_dict,
162+
};

ports/esp32/machine_rtc.h

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
* This file is part of the MicroPython project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2017 "Eric Poulsen" <eric@zyxod.com>
7+
* Copyright (c) 2017 "Tom Manning" <tom@manningetal.com>
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+
#ifndef MICROPY_INCLUDED_ESP32_MACHINE_RTC_H
29+
#define MICROPY_INCLUDED_ESP32_MACHINE_RTC_H
30+
31+
#include "modmachine.h"
32+
33+
typedef struct {
34+
uint64_t ext1_pins; // set bit == pin#
35+
int8_t ext0_pin; // just the pin#, -1 == None
36+
bool wake_on_touch : 1;
37+
bool ext0_level : 1;
38+
wake_type_t ext0_wake_types;
39+
bool ext1_level : 1;
40+
} machine_rtc_config_t;
41+
42+
#endif

0 commit comments

Comments
 (0)