Skip to content

Commit 103d12a

Browse files
atxdpgeorge
authored andcommitted
esp8266: Add utime and pyb.RTC
1 parent b479319 commit 103d12a

8 files changed

Lines changed: 348 additions & 0 deletions

File tree

esp8266/Makefile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ INC += -I..
1818
INC += -I../stmhal
1919
INC += -I../lib/mp-readline
2020
INC += -I../lib/netutils
21+
INC += -I../lib/timeutils
2122
INC += -I$(BUILD)
2223
INC += -I$(ESP_SDK)/include
2324

@@ -52,7 +53,9 @@ SRC_C = \
5253
uart.c \
5354
modpyb.c \
5455
modpybpin.c \
56+
modpybrtc.c \
5557
modesp.c \
58+
modutime.c \
5659
utils.c \
5760
$(BUILD)/frozen.c \
5861

@@ -66,6 +69,7 @@ LIB_SRC_C = $(addprefix lib/,\
6669
libc/string0.c \
6770
mp-readline/readline.c \
6871
netutils/netutils.c \
72+
timeutils/timeutils.c \
6973
)
7074

7175
SRC_S = \

esp8266/modpyb.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,7 @@ STATIC const mp_map_elem_t pyb_module_globals_table[] = {
159159
{ MP_OBJ_NEW_QSTR(MP_QSTR_hard_reset), (mp_obj_t)&pyb_hard_reset_obj },
160160

161161
{ MP_OBJ_NEW_QSTR(MP_QSTR_Pin), (mp_obj_t)&pyb_pin_type },
162+
{ MP_OBJ_NEW_QSTR(MP_QSTR_RTC), (mp_obj_t)&pyb_rtc_obj },
162163
};
163164

164165
STATIC MP_DEFINE_CONST_DICT(pyb_module_globals, pyb_module_globals_table);

esp8266/modpyb.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
extern const mp_obj_type_t pyb_pin_type;
2+
extern const mp_obj_base_t pyb_rtc_obj;

esp8266/modpybrtc.c

Lines changed: 167 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,167 @@
1+
/*
2+
* This file is part of the Micro Python project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2015 Josef Gajdusek
7+
*
8+
* Permission is hereby granted, free of charge, to any person obtaining a copy
9+
* of this software and associated documentation files (the "Software"), to deal
10+
* in the Software without restriction, including without limitation the rights
11+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12+
* copies of the Software, and to permit persons to whom the Software is
13+
* furnished to do so, subject to the following conditions:
14+
*
15+
* The above copyright notice and this permission notice shall be included in
16+
* all copies or substantial portions of the Software.
17+
*
18+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24+
* THE SOFTWARE.
25+
*/
26+
27+
#include <stdio.h>
28+
#include <string.h>
29+
30+
#include "py/nlr.h"
31+
#include "py/obj.h"
32+
#include "py/runtime.h"
33+
#include MICROPY_HAL_H
34+
#include "timeutils.h"
35+
#include "user_interface.h"
36+
37+
typedef struct _pyb_rtc_obj_t {
38+
mp_obj_base_t base;
39+
} pyb_rtc_obj_t;
40+
41+
#define MEM_MAGIC 0x75507921
42+
#define MEM_DELTA_ADDR 64
43+
#define MEM_CAL_ADDR (MEM_DELTA_ADDR + 2)
44+
#define MEM_USER_MAGIC_ADDR (MEM_CAL_ADDR + 1)
45+
#define MEM_USER_LEN_ADDR (MEM_USER_MAGIC_ADDR + 1)
46+
#define MEM_USER_DATA_ADDR (MEM_USER_LEN_ADDR + 1)
47+
#define MEM_USER_MAXLEN (512 - (MEM_USER_DATA_ADDR - MEM_DELTA_ADDR) * 4)
48+
49+
STATIC uint64_t pyb_rtc_raw_us(uint64_t cal) {
50+
return system_get_rtc_time() * ((cal >> 12) * 1000 + (cal & 0x0) / 4) / 1000;
51+
};
52+
53+
void pyb_rtc_set_us_since_2000(uint64_t nowus) {
54+
uint32_t cal = system_rtc_clock_cali_proc();
55+
int64_t delta = nowus - pyb_rtc_raw_us(cal);
56+
57+
// As the calibration value jitters quite a bit, to make the
58+
// clock at least somewhat practially usable, we need to store it
59+
system_rtc_mem_write(MEM_CAL_ADDR, &cal, sizeof(cal));
60+
system_rtc_mem_write(MEM_DELTA_ADDR, &delta, sizeof(delta));
61+
};
62+
63+
uint64_t pyb_rtc_get_us_since_2000() {
64+
uint32_t cal;
65+
int64_t delta;
66+
67+
system_rtc_mem_read(MEM_CAL_ADDR, &cal, sizeof(cal));
68+
system_rtc_mem_read(MEM_DELTA_ADDR, &delta, sizeof(delta));
69+
70+
return pyb_rtc_raw_us(cal) + delta;
71+
};
72+
73+
STATIC mp_obj_t pyb_rtc_datetime(mp_uint_t n_args, const mp_obj_t *args) {
74+
if (n_args == 1) {
75+
// Get time
76+
uint64_t msecs = pyb_rtc_get_us_since_2000() / 1000;
77+
78+
timeutils_struct_time_t tm;
79+
timeutils_seconds_since_2000_to_struct_time(msecs / 1000, &tm);
80+
81+
mp_obj_t tuple[8] = {
82+
mp_obj_new_int(tm.tm_year),
83+
mp_obj_new_int(tm.tm_mon),
84+
mp_obj_new_int(tm.tm_mday),
85+
mp_obj_new_int(tm.tm_wday),
86+
mp_obj_new_int(tm.tm_hour),
87+
mp_obj_new_int(tm.tm_min),
88+
mp_obj_new_int(tm.tm_sec),
89+
mp_obj_new_int(msecs % 1000)
90+
};
91+
92+
return mp_obj_new_tuple(8, tuple);
93+
} else {
94+
// Set time
95+
mp_obj_t *items;
96+
mp_obj_get_array_fixed_n(args[1], 8, &items);
97+
98+
pyb_rtc_set_us_since_2000(
99+
((uint64_t)timeutils_seconds_since_2000(
100+
mp_obj_get_int(items[0]),
101+
mp_obj_get_int(items[1]),
102+
mp_obj_get_int(items[2]),
103+
mp_obj_get_int(items[4]),
104+
mp_obj_get_int(items[5]),
105+
mp_obj_get_int(items[6])) * 1000 + mp_obj_get_int(items[7])) * 1000);
106+
107+
return mp_const_none;
108+
}
109+
}
110+
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(pyb_rtc_datetime_obj, 1, 2, pyb_rtc_datetime);
111+
112+
STATIC mp_obj_t pyb_rtc_memory(mp_uint_t n_args, const mp_obj_t *args) {
113+
uint8_t rtcram[MEM_USER_MAXLEN];
114+
uint32_t len;
115+
uint32_t magic;
116+
117+
if (n_args == 1) {
118+
119+
system_rtc_mem_read(MEM_USER_MAGIC_ADDR, &magic, sizeof(magic));
120+
if (magic != MEM_MAGIC) {
121+
return mp_const_none;
122+
}
123+
124+
system_rtc_mem_read(MEM_USER_LEN_ADDR, &len, sizeof(len));
125+
system_rtc_mem_read(MEM_USER_DATA_ADDR, rtcram, len + (4 - len % 4));
126+
127+
return mp_obj_new_bytes(rtcram, len);
128+
} else {
129+
mp_buffer_info_t bufinfo;
130+
mp_get_buffer_raise(args[1], &bufinfo, MP_BUFFER_READ);
131+
132+
if (bufinfo.len > MEM_USER_MAXLEN) {
133+
nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError,
134+
"buffer too long"));
135+
}
136+
137+
magic = MEM_MAGIC;
138+
system_rtc_mem_write(MEM_USER_MAGIC_ADDR, &magic, sizeof(len));
139+
len = bufinfo.len;
140+
system_rtc_mem_write(MEM_USER_LEN_ADDR, &len, sizeof(len));
141+
142+
int i = 0;
143+
for (; i < bufinfo.len; i++) {
144+
rtcram[i] = ((uint8_t *)bufinfo.buf)[i];
145+
}
146+
147+
system_rtc_mem_write(MEM_USER_DATA_ADDR, rtcram, len + (4 - len % 4));
148+
149+
return mp_const_none;
150+
}
151+
152+
}
153+
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(pyb_rtc_memory_obj, 1, 2, pyb_rtc_memory);
154+
155+
STATIC const mp_map_elem_t pyb_rtc_locals_dict_table[] = {
156+
{ MP_OBJ_NEW_QSTR(MP_QSTR_datetime), (mp_obj_t)&pyb_rtc_datetime_obj },
157+
{ MP_OBJ_NEW_QSTR(MP_QSTR_memory), (mp_obj_t)&pyb_rtc_memory_obj },
158+
};
159+
STATIC MP_DEFINE_CONST_DICT(pyb_rtc_locals_dict, pyb_rtc_locals_dict_table);
160+
161+
STATIC const mp_obj_type_t pyb_rtc_type = {
162+
{ &mp_type_type },
163+
.name = MP_QSTR_RTC,
164+
.locals_dict = (mp_obj_t)&pyb_rtc_locals_dict,
165+
};
166+
167+
const mp_obj_base_t pyb_rtc_obj = {&pyb_rtc_type};

esp8266/modpybrtc.h

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*
2+
* This file is part of the Micro Python project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2015 Josef Gajdusek
7+
*
8+
* Permission is hereby granted, free of charge, to any person obtaining a copy
9+
* of this software and associated documentation files (the "Software"), to deal
10+
* in the Software without restriction, including without limitation the rights
11+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12+
* copies of the Software, and to permit persons to whom the Software is
13+
* furnished to do so, subject to the following conditions:
14+
*
15+
* The above copyright notice and this permission notice shall be included in
16+
* all copies or substantial portions of the Software.
17+
*
18+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24+
* THE SOFTWARE.
25+
*/
26+
27+
void pyb_rtc_set_us_since_2000(uint64_t nowus);
28+
29+
uint64_t pyb_rtc_get_us_since_2000();

esp8266/modutime.c

Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
/*
2+
* This file is part of the Micro Python project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2013, 2014 Damien P. George
7+
* Copyright (c) 2015 Josef Gajdusek
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 "py/nlr.h"
32+
#include "py/obj.h"
33+
#include "py/gc.h"
34+
#include "py/runtime.h"
35+
#include MICROPY_HAL_H
36+
#include "modpyb.h"
37+
#include "modpybrtc.h"
38+
#include "timeutils.h"
39+
#include "user_interface.h"
40+
41+
/// \module time - time related functions
42+
///
43+
/// The `time` module provides functions for getting the current time and date,
44+
/// and for sleeping.
45+
46+
/// \function localtime([secs])
47+
/// Convert a time expressed in seconds since Jan 1, 2000 into an 8-tuple which
48+
/// contains: (year, month, mday, hour, minute, second, weekday, yearday)
49+
/// If secs is not provided or None, then the current time from the RTC is used.
50+
/// year includes the century (for example 2014)
51+
/// month is 1-12
52+
/// mday is 1-31
53+
/// hour is 0-23
54+
/// minute is 0-59
55+
/// second is 0-59
56+
/// weekday is 0-6 for Mon-Sun.
57+
/// yearday is 1-366
58+
STATIC mp_obj_t time_localtime(mp_uint_t n_args, const mp_obj_t *args) {
59+
timeutils_struct_time_t tm;
60+
mp_int_t seconds;
61+
if (n_args == 0 || args[0] == mp_const_none) {
62+
seconds = pyb_rtc_get_us_since_2000() / 1000 / 1000;
63+
} else {
64+
seconds = mp_obj_get_int(args[0]);
65+
}
66+
timeutils_seconds_since_2000_to_struct_time(seconds, &tm);
67+
mp_obj_t tuple[8] = {
68+
tuple[0] = mp_obj_new_int(tm.tm_year),
69+
tuple[1] = mp_obj_new_int(tm.tm_mon),
70+
tuple[2] = mp_obj_new_int(tm.tm_mday),
71+
tuple[3] = mp_obj_new_int(tm.tm_hour),
72+
tuple[4] = mp_obj_new_int(tm.tm_min),
73+
tuple[5] = mp_obj_new_int(tm.tm_sec),
74+
tuple[6] = mp_obj_new_int(tm.tm_wday),
75+
tuple[7] = mp_obj_new_int(tm.tm_yday),
76+
};
77+
return mp_obj_new_tuple(8, tuple);
78+
}
79+
MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(time_localtime_obj, 0, 1, time_localtime);
80+
81+
/// \function mktime()
82+
/// This is inverse function of localtime. It's argument is a full 8-tuple
83+
/// which expresses a time as per localtime. It returns an integer which is
84+
/// the number of seconds since Jan 1, 2000.
85+
STATIC mp_obj_t time_mktime(mp_obj_t tuple) {
86+
mp_uint_t len;
87+
mp_obj_t *elem;
88+
mp_obj_get_array(tuple, &len, &elem);
89+
90+
// localtime generates a tuple of len 8. CPython uses 9, so we accept both.
91+
if (len < 8 || len > 9) {
92+
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError, "mktime needs a tuple of length 8 or 9 (%d given)", len));
93+
}
94+
95+
return mp_obj_new_int_from_uint(timeutils_mktime(mp_obj_get_int(elem[0]),
96+
mp_obj_get_int(elem[1]), mp_obj_get_int(elem[2]), mp_obj_get_int(elem[3]),
97+
mp_obj_get_int(elem[4]), mp_obj_get_int(elem[5])));
98+
}
99+
MP_DEFINE_CONST_FUN_OBJ_1(time_mktime_obj, time_mktime);
100+
101+
/// \function sleep(seconds)
102+
/// Sleep for the given number of seconds.
103+
STATIC mp_obj_t time_sleep(mp_obj_t seconds_o) {
104+
HAL_Delay(1000 * mp_obj_get_int(seconds_o));
105+
return mp_const_none;
106+
}
107+
MP_DEFINE_CONST_FUN_OBJ_1(time_sleep_obj, time_sleep);
108+
109+
/// \function time()
110+
/// Returns the number of seconds, as an integer, since 1/1/2000.
111+
STATIC mp_obj_t time_time(void) {
112+
// get date and time
113+
return mp_obj_new_int(pyb_rtc_get_us_since_2000() / 1000 / 1000);
114+
}
115+
MP_DEFINE_CONST_FUN_OBJ_0(time_time_obj, time_time);
116+
117+
STATIC const mp_map_elem_t time_module_globals_table[] = {
118+
{ MP_OBJ_NEW_QSTR(MP_QSTR___name__), MP_OBJ_NEW_QSTR(MP_QSTR_utime) },
119+
120+
{ MP_OBJ_NEW_QSTR(MP_QSTR_localtime), (mp_obj_t)&time_localtime_obj },
121+
{ MP_OBJ_NEW_QSTR(MP_QSTR_mktime), (mp_obj_t)&time_mktime_obj },
122+
{ MP_OBJ_NEW_QSTR(MP_QSTR_sleep), (mp_obj_t)&time_sleep_obj },
123+
{ MP_OBJ_NEW_QSTR(MP_QSTR_time), (mp_obj_t)&time_time_obj },
124+
};
125+
126+
STATIC MP_DEFINE_CONST_DICT(time_module_globals, time_module_globals_table);
127+
128+
const mp_obj_module_t utime_module = {
129+
.base = { &mp_type_module },
130+
.name = MP_QSTR_utime,
131+
.globals = (mp_obj_dict_t*)&time_module_globals,
132+
};

esp8266/mpconfigport.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,10 +64,12 @@ extern const struct _mp_obj_fun_builtin_t mp_builtin_open_obj;
6464
// extra built in modules to add to the list of known ones
6565
extern const struct _mp_obj_module_t pyb_module;
6666
extern const struct _mp_obj_module_t esp_module;
67+
extern const struct _mp_obj_module_t utime_module;
6768

6869
#define MICROPY_PORT_BUILTIN_MODULES \
6970
{ MP_OBJ_NEW_QSTR(MP_QSTR_pyb), (mp_obj_t)&pyb_module }, \
7071
{ MP_OBJ_NEW_QSTR(MP_QSTR_esp), (mp_obj_t)&esp_module }, \
72+
{ MP_OBJ_NEW_QSTR(MP_QSTR_utime), (mp_obj_t)&utime_module }, \
7173

7274
#define MP_STATE_PORT MP_STATE_VM
7375

esp8266/qstrdefsport.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,3 +85,15 @@ Q(OUT_OD)
8585
Q(PULL_NONE)
8686
Q(PULL_UP)
8787
Q(PULL_DOWN)
88+
89+
// RTC
90+
Q(RTC)
91+
Q(datetime)
92+
Q(memory)
93+
94+
// utime
95+
Q(utime)
96+
Q(localtime)
97+
Q(mktime)
98+
Q(sleep)
99+
Q(time)

0 commit comments

Comments
 (0)