diff options
| author | Noralf Trønnes <noralf@tronnes.org> | 2018-04-07 15:00:13 +0200 |
|---|---|---|
| committer | Noralf Trønnes <noralf@tronnes.org> | 2018-04-16 12:49:10 +0200 |
| commit | 8d1719f190f249ebb0e53c4f42a81d7fc16f4e00 (patch) | |
| tree | 311a43ac6f98940c5a15aa45b160112d5135cbc0 | |
| parent | 10eabf6bc22391292c7bec42f611a876698d587f (diff) | |
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.
| -rw-r--r-- | shared-bindings/rtc/RTC.c | 135 | ||||
| -rw-r--r-- | shared-bindings/rtc/RTC.h | 49 | ||||
| -rw-r--r-- | shared-bindings/rtc/__init__.c | 93 | ||||
| -rw-r--r-- | shared-bindings/rtc/__init__.h | 33 | ||||
| -rw-r--r-- | shared-bindings/time/__init__.c | 121 | ||||
| -rw-r--r-- | shared-bindings/time/__init__.h | 5 |
6 files changed, 436 insertions, 0 deletions
diff --git a/shared-bindings/rtc/RTC.c b/shared-bindings/rtc/RTC.c new file mode 100644 index 000000000..30df20145 --- /dev/null +++ b/shared-bindings/rtc/RTC.c @@ -0,0 +1,135 @@ +/* + * This file is part of the Micro Python project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2018 Noralf Trønnes + * Copyright (c) 2013, 2014 Damien P. George + * + * 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 <string.h> + +#include "py/obj.h" +#include "py/objproperty.h" +#include "py/runtime.h" +#include "lib/timeutils/timeutils.h" +#include "shared-bindings/rtc/__init__.h" +#include "shared-bindings/rtc/RTC.h" +#include "shared-bindings/time/__init__.h" + +void MP_WEAK common_hal_rtc_get_time(timeutils_struct_time_t *tm) { + mp_raise_NotImplementedError("RTC is not supported on this board"); +} + +void MP_WEAK common_hal_rtc_set_time(timeutils_struct_time_t *tm) { + mp_raise_NotImplementedError("RTC is not supported on this board"); +} + +int MP_WEAK common_hal_rtc_get_calibration(void) { + return 0; +} + +void MP_WEAK common_hal_rtc_set_calibration(int calibration) { + mp_raise_NotImplementedError("RTC calibration is not supported on this board"); +} + +const rtc_rtc_obj_t rtc_rtc_obj = {{&rtc_rtc_type}}; + +//| .. currentmodule:: rtc +//| +//| :class:`RTC` --- Real Time Clock +//| -------------------------------- +//| +//| .. class:: RTC() +//| +//| This class represents the onboard Real Time Clock. It is a singleton and will always return the same instance. +//| +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) { + // No arguments + mp_arg_check_num(n_args, n_kw, 0, 0, false); + + // return constant object + return (mp_obj_t)&rtc_rtc_obj; +} + +//| .. attribute:: datetime +//| +//| Get or set the date and time of the RTC. +//| +STATIC mp_obj_t rtc_rtc_obj_get_datetime(mp_obj_t self_in) { + timeutils_struct_time_t tm; + common_hal_rtc_get_time(&tm); + return struct_time_from_tm(&tm); +} +MP_DEFINE_CONST_FUN_OBJ_1(rtc_rtc_get_datetime_obj, rtc_rtc_obj_get_datetime); + +STATIC mp_obj_t rtc_rtc_obj_set_datetime(mp_obj_t self_in, mp_obj_t datetime) { + timeutils_struct_time_t tm; + struct_time_to_tm(datetime, &tm); + common_hal_rtc_set_time(&tm); + return mp_const_none; +} +MP_DEFINE_CONST_FUN_OBJ_2(rtc_rtc_set_datetime_obj, rtc_rtc_obj_set_datetime); + +const mp_obj_property_t rtc_rtc_datetime_obj = { + .base.type = &mp_type_property, + .proxy = {(mp_obj_t)&rtc_rtc_get_datetime_obj, + (mp_obj_t)&rtc_rtc_set_datetime_obj, + (mp_obj_t)&mp_const_none_obj}, +}; + +//| .. attribute:: calibration +//| +//| The RTC calibration value. +//| A positive value speeds up the clock and a negative value slows it down. +//| Range and value is hardware specific, but one step is often approx. 1 ppm. +//| +STATIC mp_obj_t rtc_rtc_obj_get_calibration(mp_obj_t self_in) { + int calibration = common_hal_rtc_get_calibration(); + return mp_obj_new_int(calibration); +} +MP_DEFINE_CONST_FUN_OBJ_1(rtc_rtc_get_calibration_obj, rtc_rtc_obj_get_calibration); + +STATIC mp_obj_t rtc_rtc_obj_set_calibration(mp_obj_t self_in, mp_obj_t calibration) { + common_hal_rtc_set_calibration(mp_obj_get_int(calibration)); + return mp_const_none; +} +MP_DEFINE_CONST_FUN_OBJ_2(rtc_rtc_set_calibration_obj, rtc_rtc_obj_set_calibration); + +const mp_obj_property_t rtc_rtc_calibration_obj = { + .base.type = &mp_type_property, + .proxy = {(mp_obj_t)&rtc_rtc_get_calibration_obj, + (mp_obj_t)&rtc_rtc_set_calibration_obj, + (mp_obj_t)&mp_const_none_obj}, +}; + +STATIC const mp_rom_map_elem_t rtc_rtc_locals_dict_table[] = { + { MP_ROM_QSTR(MP_QSTR_datetime), MP_ROM_PTR(&rtc_rtc_datetime_obj) }, + { MP_ROM_QSTR(MP_QSTR_calibration), MP_ROM_PTR(&rtc_rtc_calibration_obj) }, +}; +STATIC MP_DEFINE_CONST_DICT(rtc_rtc_locals_dict, rtc_rtc_locals_dict_table); + +const mp_obj_type_t rtc_rtc_type = { + { &mp_type_type }, + .name = MP_QSTR_RTC, + .make_new = rtc_rtc_make_new, + .locals_dict = (mp_obj_dict_t*)&rtc_rtc_locals_dict, +}; diff --git a/shared-bindings/rtc/RTC.h b/shared-bindings/rtc/RTC.h new file mode 100644 index 000000000..76510bd72 --- /dev/null +++ b/shared-bindings/rtc/RTC.h @@ -0,0 +1,49 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2018 Noralf Trønnes + * + * 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. + */ + +#ifndef MICROPY_INCLUDED_SHARED_BINDINGS_RTC_RTC_H +#define MICROPY_INCLUDED_SHARED_BINDINGS_RTC_RTC_H + +#include <stdint.h> +#include <stdbool.h> + +#include "lib/timeutils/timeutils.h" + +extern void common_hal_rtc_get_time(timeutils_struct_time_t *tm); +extern void common_hal_rtc_set_time(timeutils_struct_time_t *tm); + +extern int common_hal_rtc_get_calibration(void); +extern void common_hal_rtc_set_calibration(int calibration); + +extern const mp_obj_type_t rtc_rtc_type; + +typedef struct _rtc_rtc_obj_t { + mp_obj_base_t base; +} rtc_rtc_obj_t; + +extern const rtc_rtc_obj_t rtc_rtc_obj; + +#endif // MICROPY_INCLUDED_SHARED_BINDINGS_RTC_RTC_H diff --git a/shared-bindings/rtc/__init__.c b/shared-bindings/rtc/__init__.c new file mode 100644 index 000000000..89e3a24dd --- /dev/null +++ b/shared-bindings/rtc/__init__.c @@ -0,0 +1,93 @@ +/* + * This file is part of the Micro Python project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2018 Noralf Trønnes + * Copyright (c) 2013, 2014 Damien P. George + * + * 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 "py/obj.h" +#include "py/runtime.h" +#include "shared-bindings/rtc/__init__.h" +#include "shared-bindings/rtc/RTC.h" +#include "shared-bindings/time/__init__.h" + +//| :mod:`rtc` --- Real Time Clock +//| ======================================================== +//| +//| .. module:: rtc +//| :synopsis: Real Time Clock +//| :platform: SAMD21 +//| +//| The `rtc` module provides support for a Real Time Clock. +//| It also backs the `time.time()` and `time.localtime()` functions using the onboard RTC if present. +//| + +void rtc_reset(void) { + MP_STATE_VM(rtc_time_source) = (mp_obj_t)&rtc_rtc_obj; +} + +mp_obj_t rtc_get_time_source_time(void) { + mp_obj_t datetime = mp_load_attr(MP_STATE_VM(rtc_time_source), MP_QSTR_datetime); + timeutils_struct_time_t tm; + struct_time_to_tm(datetime, &tm); + // This sets tm_wday and tm_yday + return struct_time_from_tm(&tm); +} + +//| .. function:: set_time_source(rtc) +//| +//| Sets the rtc time source used by time.localtime(). +//| The default is `rtc.RTC()`. +//| +//| Example usage:: +//| +//| import rtc +//| import time +//| +//| class RTC(object): +//| @property +//| def datetime(self): +//| return time.struct_time((2018, 3, 17, 21, 1, 47, 0, 0, 0)) +//| +//| r = RTC() +//| rtc.set_time_source(r) +//| +STATIC mp_obj_t rtc_set_time_source(mp_obj_t time_source) { + MP_STATE_VM(rtc_time_source) = time_source; + + return mp_const_none; +} +MP_DEFINE_CONST_FUN_OBJ_1(rtc_set_time_source_obj, rtc_set_time_source); + +STATIC const mp_rom_map_elem_t rtc_module_globals_table[] = { + { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_rtc) }, + { MP_ROM_QSTR(MP_QSTR_set_time_source), MP_ROM_PTR(&rtc_set_time_source_obj) }, + { MP_ROM_QSTR(MP_QSTR_RTC), MP_ROM_PTR(&rtc_rtc_type) }, +}; + +STATIC MP_DEFINE_CONST_DICT(rtc_module_globals, rtc_module_globals_table); + +const mp_obj_module_t rtc_module = { + .base = { &mp_type_module }, + .globals = (mp_obj_dict_t*)&rtc_module_globals, +}; diff --git a/shared-bindings/rtc/__init__.h b/shared-bindings/rtc/__init__.h new file mode 100644 index 000000000..0ee51fba5 --- /dev/null +++ b/shared-bindings/rtc/__init__.h @@ -0,0 +1,33 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2018 Noralf Trønnes + * + * 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. + */ + +#ifndef MICROPY_INCLUDED_SHARED_BINDINGS_RTC___INIT___H +#define MICROPY_INCLUDED_SHARED_BINDINGS_RTC___INIT___H + +extern void rtc_reset(void); +extern mp_obj_t rtc_get_time_source_time(void); + +#endif // MICROPY_INCLUDED_SHARED_BINDINGS_RTC___INIT___H diff --git a/shared-bindings/time/__init__.c b/shared-bindings/time/__init__.c index 6956cc4e6..b4137706e 100644 --- a/shared-bindings/time/__init__.c +++ b/shared-bindings/time/__init__.c @@ -30,6 +30,8 @@ #include "py/obj.h" #include "py/objnamedtuple.h" +#include "lib/timeutils/timeutils.h" +#include "shared-bindings/rtc/__init__.h" #include "shared-bindings/time/__init__.h" //| :mod:`time` --- time and timing related functions @@ -133,6 +135,122 @@ const mp_obj_namedtuple_type_t struct_time_type_obj = { MP_QSTR_tm_isdst }, }; + +mp_obj_t struct_time_from_tm(timeutils_struct_time_t *tm) { + timeutils_struct_time_t tmp; + mp_uint_t secs = timeutils_seconds_since_2000(tm->tm_year, tm->tm_mon, tm->tm_mday, + tm->tm_hour, tm->tm_min, tm->tm_sec); + timeutils_seconds_since_2000_to_struct_time(secs, &tmp); + tm->tm_wday = tmp.tm_wday; + tm->tm_yday = tmp.tm_yday; + + mp_obj_t elems[9] = { + mp_obj_new_int(tm->tm_year), + mp_obj_new_int(tm->tm_mon), + mp_obj_new_int(tm->tm_mday), + mp_obj_new_int(tm->tm_hour), + mp_obj_new_int(tm->tm_min), + mp_obj_new_int(tm->tm_sec), + mp_obj_new_int(tm->tm_wday), + mp_obj_new_int(tm->tm_yday), + mp_obj_new_int(-1), // tm_isdst is not supported + }; + + return namedtuple_make_new((const mp_obj_type_t*)&struct_time_type_obj, 9, 0, elems); +}; + +void struct_time_to_tm(mp_obj_t t, timeutils_struct_time_t *tm) { + mp_obj_t *elems; + size_t len; + + if (!MP_OBJ_IS_TYPE(t, &mp_type_tuple) && !MP_OBJ_IS_TYPE(t, MP_OBJ_FROM_PTR(&struct_time_type_obj))) { + mp_raise_TypeError("Tuple or struct_time argument required"); + } + + mp_obj_tuple_get(t, &len, &elems); + if (len != 9) { + mp_raise_TypeError("function takes exactly 9 arguments"); + } + + tm->tm_year = mp_obj_get_int(elems[0]); + tm->tm_mon = mp_obj_get_int(elems[1]); + tm->tm_mday = mp_obj_get_int(elems[2]); + tm->tm_hour = mp_obj_get_int(elems[3]); + tm->tm_min = mp_obj_get_int(elems[4]); + tm->tm_sec = mp_obj_get_int(elems[5]); + tm->tm_wday = mp_obj_get_int(elems[6]); + tm->tm_yday = mp_obj_get_int(elems[7]); + // elems[8] tm_isdst is not supported +} + +mp_obj_t MP_WEAK rtc_get_time_source_time(void) { + mp_raise_RuntimeError("RTC is not supported on this board"); +} + +//| .. method:: time() +//| +//| Return the current time in seconds since since Jan 1, 2000. +//| +//| :return: the current time +//| :rtype: int +//| +STATIC mp_obj_t time_time(void) { + timeutils_struct_time_t tm; + struct_time_to_tm(rtc_get_time_source_time(), &tm); + mp_uint_t secs = timeutils_seconds_since_2000(tm.tm_year, tm.tm_mon, tm.tm_mday, // mp_uint_t date + tm.tm_hour, tm.tm_min, tm.tm_sec); + return mp_obj_new_int_from_uint(secs); +} +MP_DEFINE_CONST_FUN_OBJ_0(time_time_obj, time_time); + +//| .. method:: localtime([secs]) +//| +//| Convert a time expressed in seconds since Jan 1, 2000 to a struct_time in +//| local time. If secs is not provided or None, the current time as returned +//| by time() is used. +//| +//| :return: the current time +//| :rtype: time.struct_time +//| +STATIC mp_obj_t time_localtime(size_t n_args, const mp_obj_t *args) { + if (n_args == 0 || args[0] == mp_const_none) { + return rtc_get_time_source_time(); + } + + timeutils_struct_time_t tm; + timeutils_seconds_since_2000_to_struct_time(mp_obj_get_int(args[0]), &tm); + + return struct_time_from_tm(&tm); +} +MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(time_localtime_obj, 0, 1, time_localtime); + +//| .. method:: mktime(t) +//| +//| This is the inverse function of localtime(). Its argument is the +//| struct_time or full 9-tuple (since the dst flag is needed; use -1 as the +//| dst flag if it is unknown) which expresses the time in local time, not UTC. +//| The earliest date for which it can generate a time is Jan 1, 2000. +//| +//| :return: seconds +//| :rtype: int +//| +STATIC mp_obj_t time_mktime(mp_obj_t t) { + mp_obj_t *elem; + size_t len; + + if (!MP_OBJ_IS_TYPE(t, &mp_type_tuple) && !MP_OBJ_IS_TYPE(t, MP_OBJ_FROM_PTR(&struct_time_type_obj))) { + mp_raise_TypeError("Tuple or struct_time argument required"); + } + + mp_obj_tuple_get(t, &len, &elem); + if (len != 9) { + mp_raise_TypeError("function takes exactly 9 arguments"); + } + + return mp_obj_new_int_from_uint(timeutils_mktime(mp_obj_get_int(elem[0]), mp_obj_get_int(elem[1]), mp_obj_get_int(elem[2]), + mp_obj_get_int(elem[3]), mp_obj_get_int(elem[4]), mp_obj_get_int(elem[5]))); +} +MP_DEFINE_CONST_FUN_OBJ_1(time_mktime_obj, time_mktime); #endif STATIC const mp_rom_map_elem_t time_module_globals_table[] = { @@ -142,7 +260,10 @@ STATIC const mp_rom_map_elem_t time_module_globals_table[] = { { MP_ROM_QSTR(MP_QSTR_sleep), MP_ROM_PTR(&time_sleep_obj) }, #if MICROPY_PY_COLLECTIONS { MP_ROM_QSTR(MP_QSTR_struct_time), MP_ROM_PTR(&struct_time_type_obj) }, + { MP_ROM_QSTR(MP_QSTR_localtime), MP_ROM_PTR(&time_localtime_obj) }, + { MP_ROM_QSTR(MP_QSTR_mktime), MP_ROM_PTR(&time_mktime_obj) }, #endif + { MP_ROM_QSTR(MP_QSTR_time), MP_ROM_PTR(&time_time_obj) }, }; STATIC MP_DEFINE_CONST_DICT(time_module_globals, time_module_globals_table); diff --git a/shared-bindings/time/__init__.h b/shared-bindings/time/__init__.h index f6ecb5989..c5a0b47fc 100644 --- a/shared-bindings/time/__init__.h +++ b/shared-bindings/time/__init__.h @@ -30,6 +30,11 @@ #include <stdint.h> #include <stdbool.h> +#include "lib/timeutils/timeutils.h" + +extern mp_obj_t struct_time_from_tm(timeutils_struct_time_t *tm); +extern void struct_time_to_tm(mp_obj_t t, timeutils_struct_time_t *tm); + extern uint64_t common_hal_time_monotonic(void); extern void common_hal_time_delay_ms(uint32_t); |
