Merge pull request #745 from notro/time_rtc

RFC: Add rtc module
This commit is contained in:
Scott Shawcroft 2018-04-16 15:26:54 -07:00 committed by GitHub
commit 81d395d825
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
20 changed files with 625 additions and 25 deletions

View File

@ -39,6 +39,7 @@ INC += -I. \
-Iasf4/$(CHIP_FAMILY)/hpl/gclk \
-Iasf4/$(CHIP_FAMILY)/hpl/pm \
-Iasf4/$(CHIP_FAMILY)/hpl/port \
-Iasf4/$(CHIP_FAMILY)/hpl/rtc \
-Iasf4/$(CHIP_FAMILY)/hpl/tc \
-Iasf4/$(CHIP_FAMILY)/include \
-Iasf4/$(CHIP_FAMILY)/CMSIS/Include \
@ -173,6 +174,7 @@ SRC_ASF := \
gcc/system_$(CHIP_FAMILY).c \
hal/src/hal_adc_sync.c \
hal/src/hal_atomic.c \
hal/src/hal_calendar.c \
hal/src/hal_dac_sync.c \
hal/src/hal_delay.c \
hal/src/hal_flash.c \
@ -278,6 +280,8 @@ SRC_COMMON_HAL = \
microcontroller/Processor.c \
neopixel_write/__init__.c \
os/__init__.c \
rtc/__init__.c \
rtc/RTC.c \
storage/__init__.c \
supervisor/__init__.c \
supervisor/Runtime.c \

@ -1 +1 @@
Subproject commit aaa0f428111fbea7d56ab548053b11c9f12068f1
Subproject commit 02e264253c76b85c567ecdfce69ad0de0970da63

View File

@ -190,7 +190,7 @@
// <i> Indicates whether generic clock 2 configuration is enabled or not
// <id> enable_gclk_gen_2
#ifndef CONF_GCLK_GENERATOR_2_CONFIG
#define CONF_GCLK_GENERATOR_2_CONFIG 0
#define CONF_GCLK_GENERATOR_2_CONFIG 1
#endif
// <h> Generic Clock Generator Control
@ -205,7 +205,7 @@
// <i> Indicates whether Divide Selection is enabled or not
// <id> gclk_gen_2_div_sel
#ifndef CONF_GCLK_GEN_2_DIVSEL
#define CONF_GCLK_GEN_2_DIVSEL 0
#define CONF_GCLK_GEN_2_DIVSEL 1
#endif
// <q> Output Enable
@ -233,7 +233,7 @@
// <i> Indicates whether Generic Clock Generator Enable is enabled or not
// <id> gclk_arch_gen_2_enable
#ifndef CONF_GCLK_GEN_2_GENEN
#define CONF_GCLK_GEN_2_GENEN 0
#define CONF_GCLK_GEN_2_GENEN 1
#endif
// <y> Generic clock generator 2 source
@ -259,7 +259,7 @@
// <i> This defines the clock source for generic clock generator 2
// <id> gclk_gen_2_oscillator
#ifndef CONF_GCLK_GEN_2_SRC
#define CONF_GCLK_GEN_2_SRC GCLK_GENCTRL_SRC_OSC32K
#define CONF_GCLK_GEN_2_SRC GCLK_GENCTRL_SRC_XOSC32K
#endif
// </h>
@ -268,7 +268,7 @@
// <i>
// <id> gclk_gen_2_div
#ifndef CONF_GCLK_GEN_2_DIV
#define CONF_GCLK_GEN_2_DIV 1
#define CONF_GCLK_GEN_2_DIV 4
#endif
// </h>

View File

@ -10,6 +10,14 @@
#define CONF_RTC_ENABLE 1
#endif
// <q> Force reset RTC on initialization
// <i> Force RTC to reset on initialization, else init is not going on if RTC is already enabled.
// <i> Note that the previous power down data in RTC is lost if it's enabled.
// <id> rtc_arch_init_reset
#ifndef CONF_RTC_INIT_RESET
#define CONF_RTC_INIT_RESET 0
#endif
// <o> Prescaler configuration
// <0x0=>Peripheral clock divided by 1
// <0x1=>Peripheral clock divided by 2
@ -25,7 +33,7 @@
// <i> These bits define the RTC clock relative to the peripheral clock
// <id> rtc_arch_prescaler
#ifndef CONF_RTC_PRESCALER
#define CONF_RTC_PRESCALER 0x0
#define CONF_RTC_PRESCALER 0xa
#endif

View File

@ -197,7 +197,7 @@
// <i> Indicates whether configuration for External 32K Osc is enabled or not
// <id> enable_xosc32k
#ifndef CONF_XOSC32K_CONFIG
#define CONF_XOSC32K_CONFIG 0
#define CONF_XOSC32K_CONFIG 1
#endif
// <h> 32kHz External Crystal Oscillator (XOSC32K) Control
@ -205,7 +205,7 @@
// <i> Indicates whether External 32K Oscillator is enabled or not
// <id> xosc32k_arch_enable
#ifndef CONF_XOSC32K_ENABLE
#define CONF_XOSC32K_ENABLE 0
#define CONF_XOSC32K_ENABLE 1
#endif
// <q> On Demand
@ -223,7 +223,7 @@
// <i> If this bit is 1: The oscillator is not stopped in standby sleep mode.
// <id> xosc32k_arch_runstdby
#ifndef CONF_XOSC32K_RUNSTDBY
#define CONF_XOSC32K_RUNSTDBY 0
#define CONF_XOSC32K_RUNSTDBY 1
#endif
// <q> Enable 1K
@ -237,14 +237,14 @@
// <i> Enable 32 Khz Output
// <id> xosc32k_arch_en32k
#ifndef CONF_XOSC32K_EN32K
#define CONF_XOSC32K_EN32K 0
#define CONF_XOSC32K_EN32K 1
#endif
// <q> Enable XTAL
// <i> Enable XTAL
// <id> xosc32k_arch_xtalen
#ifndef CONF_XOSC32K_XTALEN
#define CONF_XOSC32K_XTALEN 0
#define CONF_XOSC32K_XTALEN 1
#endif
// <q> Write Lock
@ -274,7 +274,7 @@
// <i> Default: 122 us
// <id> xosc32k_arch_startup
#ifndef CONF_XOSC32K_STARTUP
#define CONF_XOSC32K_STARTUP CONF_XOSC32K_STARTUP_TIME_122MCS
#define CONF_XOSC32K_STARTUP CONF_XOSC32K_STARTUP_TIME_2000092MCS
#endif
// </h>

View File

@ -321,7 +321,7 @@
// <i> Select the clock source for RTC.
#ifndef CONF_GCLK_RTC_SRC
#define CONF_GCLK_RTC_SRC GCLK_CLKCTRL_GEN_GCLK0_Val
#define CONF_GCLK_RTC_SRC GCLK_CLKCTRL_GEN_GCLK2_Val
#endif
/**

View File

@ -172,7 +172,7 @@
// <i> Indicates whether generic clock 2 configuration is enabled or not
// <id> enable_gclk_gen_2
#ifndef CONF_GCLK_GENERATOR_2_CONFIG
#define CONF_GCLK_GENERATOR_2_CONFIG 0
#define CONF_GCLK_GENERATOR_2_CONFIG 1
#endif
// <h> Generic Clock Generator Control
@ -188,7 +188,7 @@
// <i> This defines the clock source for generic clock generator 2
// <id> gclk_gen_2_oscillator
#ifndef CONF_GCLK_GEN_2_SOURCE
#define CONF_GCLK_GEN_2_SOURCE GCLK_GENCTRL_SRC_XOSC1
#define CONF_GCLK_GEN_2_SOURCE GCLK_GENCTRL_SRC_XOSC32K
#endif
// <q> Run in Standby
@ -230,7 +230,7 @@
// <i> Indicates whether Generic Clock Generator Enable is enabled or not
// <id> gclk_arch_gen_2_enable
#ifndef CONF_GCLK_GEN_2_GENEN
#define CONF_GCLK_GEN_2_GENEN 0
#define CONF_GCLK_GEN_2_GENEN 1
#endif
// </h>
@ -238,7 +238,7 @@
//<o> Generic clock generator 2 division <0x0000-0xFFFF>
// <id> gclk_gen_2_div
#ifndef CONF_GCLK_GEN_2_DIV
#define CONF_GCLK_GEN_2_DIV 1
#define CONF_GCLK_GEN_2_DIV 4
#endif
// </h>
// </e>

View File

@ -23,7 +23,7 @@
// <q> Use 1 kHz output
// <id> rtc_1khz_selection
#ifndef CONF_RTCCTRL_1KHZ
#define CONF_RTCCTRL_1KHZ 1
#define CONF_RTCCTRL_1KHZ 0
#endif
#if CONF_RTCCTRL_SRC == GCLK_GENCTRL_SRC_OSCULP32K
@ -62,7 +62,7 @@
// <0x6=>8000092us
// <id> xosc32k_arch_startup
#ifndef CONF_XOSC32K_STARTUP
#define CONF_XOSC32K_STARTUP 0x0
#define CONF_XOSC32K_STARTUP 0x4
#endif
// <q> On Demand Control
@ -76,7 +76,7 @@
// <i> Indicates whether Run in Standby is enabled or not
// <id> xosc32k_arch_runstdby
#ifndef CONF_XOSC32K_RUNSTDBY
#define CONF_XOSC32K_RUNSTDBY 0
#define CONF_XOSC32K_RUNSTDBY 1
#endif
// <q> 1kHz Output Enable
@ -90,7 +90,7 @@
// <i> Indicates whether 32kHz Output is enabled or not
// <id> xosc32k_arch_en32k
#ifndef CONF_XOSC32K_EN32K
#define CONF_XOSC32K_EN32K 0
#define CONF_XOSC32K_EN32K 1
#endif
// <q> Clock Switch Back
@ -118,7 +118,7 @@
// <i> Indicates whether the connections between the I/O pads and the external clock or crystal oscillator is enabled or not
// <id> xosc32k_arch_xtalen
#ifndef CONF_XOSC32K_XTALEN
#define CONF_XOSC32K_XTALEN 0
#define CONF_XOSC32K_XTALEN 1
#endif
// <o> Control Gain Mode
@ -137,7 +137,7 @@
// <i> Indicates whether configuration for OSCULP32K is enabled or not
// <id> enable_osculp32k
#ifndef CONF_OSCULP32K_CONFIG
#define CONF_OSCULP32K_CONFIG 1
#define CONF_OSCULP32K_CONFIG 0
#endif
// <h> 32kHz Ultra Low Power Internal Oscillator Control

View File

@ -10,6 +10,14 @@
#define CONF_RTC_ENABLE 1
#endif
// <q> Force reset RTC on initialization
// <i> Force RTC to reset on initialization.
// <i> Note that the previous power down data in RTC is lost if it's enabled.
// <id> rtc_arch_init_reset
#ifndef CONF_RTC_INIT_RESET
#define CONF_RTC_INIT_RESET 0
#endif
// <o> Prescaler configuration
// <0x0=>OFF(Peripheral clock divided by 1)
// <0x1=>Peripheral clock divided by 1
@ -26,7 +34,7 @@
// <i> These bits define the RTC clock relative to the peripheral clock
// <id> rtc_arch_prescaler
#ifndef CONF_RTC_PRESCALER
#define CONF_RTC_PRESCALER 0x0
#define CONF_RTC_PRESCALER 0xb
#endif

View File

@ -0,0 +1,98 @@
/*
* 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.
*/
#include <stdlib.h>
#include <peripheral_clk_config.h>
#include <hal_init.h>
#include <hpl_gclk_base.h>
#include <hpl_pm_base.h>
#include <hal_calendar.h>
#include "py/obj.h"
#include "py/runtime.h"
#include "lib/timeutils/timeutils.h"
#include "shared-bindings/rtc/__init__.h"
static struct calendar_descriptor calendar;
void rtc_init(void) {
#ifdef SAMD21
_gclk_enable_channel(RTC_GCLK_ID, CONF_GCLK_RTC_SRC);
#endif
#ifdef SAMD51
hri_mclk_set_APBAMASK_RTC_bit(MCLK);
#endif
calendar_init(&calendar, RTC);
calendar_set_baseyear(&calendar, 2000);
calendar_enable(&calendar);
}
void common_hal_rtc_get_time(timeutils_struct_time_t *tm) {
struct calendar_date_time datetime;
calendar_get_date_time(&calendar, &datetime);
tm->tm_year = datetime.date.year;
tm->tm_mon = datetime.date.month;
tm->tm_mday = datetime.date.day;
tm->tm_hour = datetime.time.hour;
tm->tm_min = datetime.time.min;
tm->tm_sec = datetime.time.sec;
}
void common_hal_rtc_set_time(timeutils_struct_time_t *tm) {
struct calendar_date date = {
.year = tm->tm_year,
.month = tm->tm_mon,
.day = tm->tm_mday,
};
calendar_set_date(&calendar, &date);
struct calendar_time time = {
.hour = tm->tm_hour,
.min = tm->tm_min,
.sec = tm->tm_sec,
};
calendar_set_time(&calendar, &time);
}
// A positive value speeds up the clock by removing clock cycles.
int common_hal_rtc_get_calibration(void) {
int calibration = hri_rtcmode0_read_FREQCORR_VALUE_bf(calendar.device.hw);
if (!hri_rtcmode0_get_FREQCORR_SIGN_bit(calendar.device.hw))
calibration = -calibration;
return calibration;
}
void common_hal_rtc_set_calibration(int calibration) {
if (calibration > 127 || calibration < -127)
mp_raise_ValueError("calibration value out of range +/-127");
hri_rtcmode0_write_FREQCORR_SIGN_bit(calendar.device.hw, calibration < 0 ? 0 : 1);
hri_rtcmode0_write_FREQCORR_VALUE_bf(calendar.device.hw, abs(calibration));
}

View File

@ -0,0 +1,32 @@
/*
* 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_ATMEL_SAMD_COMMON_HAL_RTC_RTC_H
#define MICROPY_INCLUDED_ATMEL_SAMD_COMMON_HAL_RTC_RTC_H
extern void rtc_init(void);
#endif // MICROPY_INCLUDED_ATMEL_SAMD_COMMON_HAL_RTC_RTC_H

View File

@ -166,6 +166,7 @@ extern const struct _mp_obj_module_t board_module;
extern const struct _mp_obj_module_t math_module;
extern const struct _mp_obj_module_t os_module;
extern const struct _mp_obj_module_t random_module;
extern const struct _mp_obj_module_t rtc_module;
extern const struct _mp_obj_module_t storage_module;
extern const struct _mp_obj_module_t struct_module;
extern const struct _mp_obj_module_t time_module;
@ -234,6 +235,7 @@ extern const struct _mp_obj_module_t usb_hid_module;
{ MP_OBJ_NEW_QSTR(MP_QSTR_os), (mp_obj_t)&os_module }, \
{ MP_OBJ_NEW_QSTR(MP_QSTR_pulseio), (mp_obj_t)&pulseio_module }, \
{ MP_OBJ_NEW_QSTR(MP_QSTR_random), (mp_obj_t)&random_module }, \
{ MP_OBJ_NEW_QSTR(MP_QSTR_rtc), (mp_obj_t)&rtc_module }, \
{ MP_OBJ_NEW_QSTR(MP_QSTR_storage), (mp_obj_t)&storage_module }, \
{ MP_OBJ_NEW_QSTR(MP_QSTR_struct), (mp_obj_t)&struct_module }, \
{ MP_OBJ_NEW_QSTR(MP_QSTR_supervisor), (mp_obj_t)&supervisor_module }, \
@ -266,6 +268,7 @@ extern const struct _mp_obj_module_t usb_hid_module;
const char *readline_hist[8]; \
vstr_t *repl_line; \
mp_obj_t playing_audio[AUDIO_DMA_CHANNEL_COUNT]; \
mp_obj_t rtc_time_source; \
FLASH_ROOT_POINTERS \
void run_background_tasks(void);

View File

@ -52,7 +52,9 @@
#include "common-hal/pulseio/PulseIn.h"
#include "common-hal/pulseio/PulseOut.h"
#include "common-hal/pulseio/PWMOut.h"
#include "common-hal/rtc/RTC.h"
#include "common-hal/usb_hid/Device.h"
#include "shared-bindings/rtc/__init__.h"
#include "clocks.h"
#include "events.h"
#include "shared_dma.h"
@ -124,6 +126,7 @@ safe_mode_t port_init(void) {
// Configure millisecond timer initialization.
tick_init();
rtc_init();
// Init the nvm controller.
// struct nvm_config config_nvm;
@ -180,6 +183,7 @@ void reset_port(void) {
pulsein_reset();
pulseout_reset();
pwmout_reset();
rtc_reset();
reset_gclks();

135
shared-bindings/rtc/RTC.c Normal file
View File

@ -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
//|
//| 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,
};

49
shared-bindings/rtc/RTC.h Normal file
View File

@ -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

View File

@ -0,0 +1,100 @@
/*
* 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.
//|
//| Libraries
//|
//| .. toctree::
//| :maxdepth: 3
//|
//| RTC
//|
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,
};

View File

@ -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

View File

@ -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);

View File

@ -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);