8000 Add rtc module · rbarraud/circuitpython@8d1719f · GitHub
[go: up one dir, main page]

Skip to content

Commit 8d1719f

Browse files
committed
Add rtc module
Add an rtc module that provides a singleton RTC class with - a datetime property to set and get time if the board supports it. - a calbration property to adjust the clock. There's also an rtc.set_time_source() method to override this RTC object using pure python. The time module gets 3 methods: - time.time() - time.localtime() - time.mktime() The rtc timesource is used to provide time to the time module. lib/timeutils is used for time conversions and thus only supports dates after 2000.
1 parent 10eabf6 commit 8d1719f

File tree

6 files changed

+436
-0
lines changed

6 files changed

+436
-0
lines changed

shared-bindings/rtc/RTC.c

Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
/*
2+
* This file is part of the Micro Python project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2018 Noralf Trønnes
7+
* Copyright (c) 2013, 2014 Damien P. George
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 <string.h>
29+
30+
#include "py/obj.h"
31+
#include "py/objproperty.h"
32+
#include "py/runtime.h"
33+
#include "lib/timeutils/timeutils.h"
34+
#include "shared-bindings/rtc/__init__.h"
35+
#include "shared-bindings/rtc/RTC.h"
36+
#include "shared-bindings/time/__init__.h"
37+
38+
void MP_WEAK common_hal_rtc_get_time(timeutils_struct_time_t *tm) {
39+
mp_raise_NotImplementedError("RTC is not supported on this board");
40+
}
41+
42+
void MP_WEAK common_hal_rtc_set_time(timeutils_struct_time_t *tm) {
43+
mp_raise_NotImplementedError("RTC is not supported on this board");
44+
}
45+
46+
int MP_WEAK common_hal_rtc_get_calibration(void) {
47+
return 0;
48+
}
49+
50+
void MP_WEAK common_hal_rtc_set_calibration(int calibration) {
51+
mp_raise_NotImplementedError("RTC calibration is not supported on this board");
52+
}
53+
54+
const rtc_rtc_obj_t rtc_rtc_obj = {{&rtc_rtc_type}};
55+
56+
//| .. currentmodule:: rtc
57+
//|
58+
//| :class:`RTC` --- Real Time Clock
59+
//| --------------------------------
60+
//|
61+
//| .. class:: RTC()
62+
//|
63+
//| This class represents the onboard Real Time Clock. It is a singleton and will always return the same instance.
64+
//|
65+
STATIC mp_obj_t rtc_rtc_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args) {
66+
// No arguments
67+
mp_arg_check_num(n_args, n_kw, 0, 0, false);
68+
69+
// return constant object
70+
return (mp_obj_t)&rtc_rtc_obj;
71+
}
72+
73+
//| .. attribute:: datetime
74+
//|
75+
//| Get or set the date and time of the RTC.
76+
//|
77+
STATIC mp_obj_t rtc_rtc_obj_get_datetime(mp_obj_t self_in) {
78+
timeutils_struct_time_t tm;
79+
common_hal_rtc_get_time(&tm);
80+
return struct_time_from_tm(&tm);
81+
}
82+
MP_DEFINE_CONST_FUN_OBJ_1(rtc_rtc_get_datetime_obj, rtc_rtc_obj_get_datetime);
83+
84+
STATIC mp_obj_t rtc_rtc_obj_set_datetime(mp_obj_t self_in, mp_obj_t datetime) {
85+
timeutils_struct_time_t tm;
86+
struct_time_to_tm(datetime, &tm);
87+
common_hal_rtc_set_time(&tm);
88+
return mp_const_none;
89+
}
90+
MP_DEFINE_CONST_FUN_OBJ_2(rtc_rtc_set_datetime_obj, rtc_rtc_obj_set_datetime);
91+
92+
const mp_obj_property_t rtc_rtc_datetime_obj = {
93+
.base.type = &mp_type_property,
94+
.proxy = {(mp_obj_t)&rtc_rtc_get_datetime_obj,
95+
(mp_obj_t)&rtc_rtc_set_datetime_obj,
96+
(mp_obj_t)&mp_const_none_obj},
97+
};
98+
99+
//| .. attribute:: calibration
100+
//|
101+
//| The RTC calibration value.
102+
//| A positive value speeds up the clock and a negative value slows it down.
103+
//| Range and value is hardware specific, but one step is often approx. 1 ppm.
104+
//|
105+
STATIC mp_obj_t rtc_rtc_obj_get_calibration(mp_obj_t self_in) {
106+
int calibration = common_hal_rtc_get_calibration();
107+
return mp_obj_new_int(calibration);
108+
}
109+
MP_DEFINE_CONST_FUN_OBJ_1(rtc_rtc_get_calibration_obj, rtc_rtc_obj_get_calibration);
110+
111+
STATIC mp_obj_t rtc_rtc_obj_set_calibration(mp_obj_t self_in, mp_obj_t calibration) {
112+
common_hal_rtc_set_calibration(mp_obj_get_int(calibration));
113+
return mp_const_none;
114+
}
115+
MP_DEFINE_CONST_FUN_OBJ_2(rtc_rtc_set_calibration_obj, rtc_rtc_obj_set_calibration);
116+
117+
const mp_obj_property_t rtc_rtc_calibration_obj = {
118+
.base.type = &mp_type_property,
119+
.proxy = {(mp_obj_t)&rtc_rtc_get_calibration_obj,
120+
(mp_obj_t)&rtc_rtc_set_calibration_obj,
121+
(mp_obj_t)&mp_const_none_obj},
122+
};
123+
124+
STATIC const mp_rom_map_elem_t rtc_rtc_locals_dict_table[] = {
125+
{ MP_ROM_QSTR(MP_QSTR_datetime), MP_ROM_PTR(&rtc_rtc_datetime_obj) },
126+
{ MP_ROM_QSTR(MP_QSTR_calibration), MP_ROM_PTR(&rtc_rtc_calibration_obj) },
127+
};
128+
STATIC MP_DEFINE_CONST_DICT(rtc_rtc_locals_dict, rtc_rtc_locals_dict_table);
129+
130+
const mp_obj_type_t rtc_rtc_type = {
131+
{ &mp_type_type },
132+
.name = MP_QSTR_RTC,
133+
.make_new = rtc_rtc_make_new,
134+
.locals_dict = (mp_obj_dict_t*)&rtc_rtc_locals_dict,
135+
};

shared-bindings/rtc/RTC.h

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
* This file is part of the MicroPython project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2018 Noralf Trønnes
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+
#ifndef MICROPY_INCLUDED_SHARED_BINDINGS_RTC_RTC_H
28+
#define MICROPY_INCLUDED_SHARED_BINDINGS_RTC_RTC_H
29+
30+
#include <stdint.h>
31+
#include <stdbool.h>
32+
33+
#include "lib/timeutils/timeutils.h"
34+
35+
extern void common_hal_rtc_get_time(timeutils_struct_time_t *tm);
36+
extern void common_hal_rtc_set_time(timeutils_struct_time_t *tm);
37+
38+
extern int common_hal_rtc_get_calibration(void);
39+
extern void common_hal_rtc_set_calibration(int calibration);
40+
41+
extern const mp_obj_type_t rtc_rtc_type;
42+
43+
typedef struct _rtc_rtc_obj_t {
44+
mp_obj_base_t base;
45+
} rtc_rtc_obj_t;
46+
47+
extern const rtc_rtc_obj_t rtc_rtc_obj;
48+
49+
#endif // MICROPY_INCLUDED_SHARED_BINDINGS_RTC_RTC_H

shared-bindings/rtc/__init__.c

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
/*
2+
* This file is part of the Micro Python project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2018 Noralf Trønnes
7+
* Copyright (c) 2013, 2014 Damien P. George
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 "py/obj.h"
29+
#include "py/runtime.h"
30+
#include "shared-bindings/rtc/__init__.h"
31+
#include "shared-bindings/rtc/RTC.h"
32+
#include "shared-bindings/time/__init__.h"
33+
34+
//| :mod:`rtc` --- Real Time Clock
35+
//| ========================================================
36+
//|
37+
//| .. module:: rtc
38+
//| :synopsis: Real Time Clock
39+
//| :platform: SAMD21
40+
//|
41+
//| The `rtc` module provides support for a Real Time Clock.
42+
//| It also backs the `time.time()` and `time.localtime()` functions using the onboard RTC if present.
43+
//|
44+
45+
void rtc_reset(void) {
46+
MP_STATE_VM(rtc_time_source) = (mp_obj_t)&rtc_rtc_obj;
47+
}
48+
49+
mp_obj_t rtc_get_time_source_time(void) {
50+
mp_obj_t datetime = mp_load_attr(MP_STATE_VM(rtc_time_source), MP_QSTR_datetime);
51+
timeutils_struct_time_t tm;
52+
struct_time_to_tm(datetime, &tm);
53+
// This sets tm_wday and tm_yday
54+
return struct_time_from_tm(&tm);
55+
}
56+
57+
//| .. function:: set_time_source(rtc)
58+
//|
59+
//| Sets the rtc time source used by time.localtime().
60+
//| The default is `rtc.RTC()`.
61+
//|
62+
//| Example usage::
63+
//|
64+
//| import rtc
65+
//| import time
66+
//|
67+
//| class RTC(object):
68+
//| @property
69+
//| def datetime(self):
70+
//| return time.struct_time((2018, 3, 17, 21, 1, 47, 0, 0, 0))
71+
//|
72+
//| r = RTC()
73+
//| rtc.set_time_source(r)
74+
//|
75+
STATIC mp_obj_t rtc_set_time_source(mp_obj_t time_source) {
76+
MP_STATE_VM(rtc_time_source) = time_source;
77+
78+
return mp_const_none;
79+
}
80+
MP_DEFINE_CONST_FUN_OBJ_1(rtc_set_time_source_obj, rtc_set_time_source);
81+
82+
STATIC const mp_rom_map_elem_t rtc_module_globals_table[] = {
83+
{ MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_rtc) },
84+
{ MP_ROM_QSTR(MP_QSTR_set_time_source), MP_ROM_PTR(&rtc_set_time_source_obj) },
85+
{ MP_ROM_QSTR(MP_QSTR_RTC), MP_ROM_PTR(&rtc_rtc_type) },
86+
};
87+
88+
STATIC MP_DEFINE_CONST_DICT(rtc_module_globals, rtc_module_globals_table);
89+
90+
const mp_obj_module_t rtc_module = {
91+
.base = { &mp_type_module },
92+
.globals = (mp_obj_dict_t*)&rtc_module_globals,
93+
};

shared-bindings/rtc/__init__.h

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
* This file is part of the MicroPython project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2018 Noralf Trønnes
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+
#ifndef MICROPY_INCLUDED_SHARED_BINDINGS_RTC___INIT___H
28+
#define MICROPY_INCLUDED_SHARED_BINDINGS_RTC___INIT___H
29+
30+
extern void rtc_reset(void);
31+
extern mp_obj_t rtc_get_time_source_time(void);
32+
33+
#endif // MICROPY_INCLUDED_SHARED_BINDINGS_RTC___INIT___H

0 commit comments

Comments
 (0)
0