2015-02-06 09:35:48 -05:00
|
|
|
/*
|
|
|
|
* This file is part of the Micro Python project, http://micropython.org/
|
|
|
|
*
|
|
|
|
* The MIT License (MIT)
|
|
|
|
*
|
|
|
|
* Copyright (c) 2013, 2014 Damien P. George
|
|
|
|
* Copyright (c) 2015 Daniel Campora
|
|
|
|
*
|
|
|
|
* 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 <stdio.h>
|
|
|
|
|
2015-02-21 13:58:43 -05:00
|
|
|
#include "py/mpconfig.h"
|
2015-02-06 09:35:48 -05:00
|
|
|
#include MICROPY_HAL_H
|
2015-02-21 13:58:43 -05:00
|
|
|
#include "py/obj.h"
|
|
|
|
#include "py/runtime.h"
|
2015-02-06 09:35:48 -05:00
|
|
|
#include "inc/hw_types.h"
|
|
|
|
#include "inc/hw_ints.h"
|
|
|
|
#include "inc/hw_memmap.h"
|
|
|
|
#include "rom_map.h"
|
|
|
|
#include "prcm.h"
|
|
|
|
#include "pybrtc.h"
|
2015-03-14 04:59:47 -04:00
|
|
|
#include "pybsleep.h"
|
|
|
|
#include "mpcallback.h"
|
2015-05-10 20:00:19 -04:00
|
|
|
#include "timeutils.h"
|
2015-02-06 09:35:48 -05:00
|
|
|
|
|
|
|
/// \moduleref pyb
|
|
|
|
/// \class RTC - real time clock
|
|
|
|
///
|
|
|
|
/// The RTC is and independent clock that keeps track of the date
|
|
|
|
/// and time.
|
|
|
|
///
|
|
|
|
/// Example usage:
|
|
|
|
///
|
|
|
|
/// rtc = pyb.RTC()
|
|
|
|
/// rtc.datetime((2014, 5, 1, 4, 13, 0, 0, 0))
|
|
|
|
/// print(rtc.datetime())
|
|
|
|
|
2015-03-14 04:59:47 -04:00
|
|
|
/******************************************************************************
|
2015-03-17 08:20:15 -04:00
|
|
|
DECLARE CONSTANTS
|
|
|
|
******************************************************************************/
|
|
|
|
#define PYBRTC_CLOCK_FREQUENCY_HZ 32768
|
|
|
|
#define PYBRTC_MIN_INTERVAL_VALUE 25
|
|
|
|
|
|
|
|
/******************************************************************************
|
|
|
|
DEFINE TYPES
|
2015-03-14 04:59:47 -04:00
|
|
|
******************************************************************************/
|
|
|
|
typedef struct {
|
2015-03-17 08:20:15 -04:00
|
|
|
byte prwmode;
|
2015-03-14 04:59:47 -04:00
|
|
|
} pybrtc_data_t;
|
|
|
|
|
|
|
|
/******************************************************************************
|
|
|
|
DECLARE PRIVATE DATA
|
|
|
|
******************************************************************************/
|
2015-03-15 14:53:12 -04:00
|
|
|
STATIC pybrtc_data_t pybrtc_data;
|
2015-03-14 04:59:47 -04:00
|
|
|
STATIC const mp_cb_methods_t pybrtc_cb_methods;
|
|
|
|
|
|
|
|
/******************************************************************************
|
|
|
|
DECLARE PUBLIC FUNCTIONS
|
|
|
|
******************************************************************************/
|
2015-03-02 08:11:02 -05:00
|
|
|
__attribute__ ((section (".boot")))
|
2015-02-06 09:35:48 -05:00
|
|
|
void pybrtc_init(void) {
|
2015-03-04 07:52:39 -05:00
|
|
|
// if the RTC was previously set, leave it alone
|
2015-02-06 09:35:48 -05:00
|
|
|
if (MAP_PRCMSysResetCauseGet() == PRCM_POWER_ON) {
|
2015-03-04 07:52:39 -05:00
|
|
|
// fresh reset; configure the RTC Calendar
|
2015-02-06 09:35:48 -05:00
|
|
|
// set the date to 1st Jan 2015
|
|
|
|
// set the time to 00:00:00
|
2015-05-10 20:00:19 -04:00
|
|
|
uint32_t seconds = timeutils_seconds_since_2000(2015, 1, 1, 0, 0, 0);
|
2015-02-06 09:35:48 -05:00
|
|
|
|
2015-03-14 04:59:47 -04:00
|
|
|
// Mark the RTC in use first
|
2015-02-06 09:35:48 -05:00
|
|
|
MAP_PRCMRTCInUseSet();
|
2015-03-14 04:59:47 -04:00
|
|
|
|
|
|
|
// Now set the RTC calendar seconds
|
|
|
|
MAP_PRCMRTCSet(seconds, 0);
|
2015-02-06 09:35:48 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-09 11:46:16 -04:00
|
|
|
void pyb_rtc_callback_disable (mp_obj_t self_in) {
|
|
|
|
// check the wake from param
|
|
|
|
if (pybrtc_data.prwmode & PYB_PWR_MODE_ACTIVE) {
|
|
|
|
// disable the slow clock interrupt
|
|
|
|
MAP_PRCMIntDisable(PRCM_INT_SLOW_CLK_CTR);
|
|
|
|
}
|
|
|
|
// disable wake from ldps and hibernate
|
|
|
|
pybsleep_configure_timer_wakeup (PYB_PWR_MODE_ACTIVE);
|
|
|
|
// read the interrupt status to clear any pending interrupt
|
|
|
|
(void)MAP_PRCMIntStatus();
|
|
|
|
}
|
|
|
|
|
2015-03-14 04:59:47 -04:00
|
|
|
/******************************************************************************
|
|
|
|
DECLARE PRIVATE FUNCTIONS
|
|
|
|
******************************************************************************/
|
|
|
|
STATIC void pyb_rtc_callback_enable (mp_obj_t self_in) {
|
2015-03-17 08:20:15 -04:00
|
|
|
// check the wake from param
|
|
|
|
if (pybrtc_data.prwmode & PYB_PWR_MODE_ACTIVE) {
|
|
|
|
// enable the slow clock interrupt
|
|
|
|
MAP_PRCMIntEnable(PRCM_INT_SLOW_CLK_CTR);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
// just in case it was already enabled before
|
|
|
|
MAP_PRCMIntDisable(PRCM_INT_SLOW_CLK_CTR);
|
|
|
|
}
|
|
|
|
pybsleep_configure_timer_wakeup (pybrtc_data.prwmode);
|
2015-03-14 04:59:47 -04:00
|
|
|
}
|
2015-02-06 09:35:48 -05:00
|
|
|
|
2015-03-14 04:59:47 -04:00
|
|
|
/******************************************************************************/
|
|
|
|
// Micro Python bindings
|
|
|
|
|
2015-02-06 09:35:48 -05:00
|
|
|
/// \method datetime([datetimetuple])
|
|
|
|
/// Get or set the date and time of the RTC.
|
|
|
|
///
|
|
|
|
/// With no arguments, this method returns an 8-tuple with the current
|
2015-03-04 07:52:39 -05:00
|
|
|
/// date and time. With 1 argument (being an 8-tuple) it sets the date
|
2015-02-06 09:35:48 -05:00
|
|
|
/// and time.
|
|
|
|
///
|
|
|
|
/// The 8-tuple has the following format:
|
|
|
|
///
|
|
|
|
/// (year, month, day, weekday, hours, minutes, seconds, milliseconds)
|
|
|
|
///
|
2015-03-15 19:40:02 -04:00
|
|
|
/// `weekday` is 0-6 for Monday through Sunday.
|
2015-02-06 09:35:48 -05:00
|
|
|
///
|
|
|
|
mp_obj_t pyb_rtc_datetime(mp_uint_t n_args, const mp_obj_t *args) {
|
2015-05-10 20:00:19 -04:00
|
|
|
timeutils_struct_time_t tm;
|
2015-02-06 09:35:48 -05:00
|
|
|
uint32_t seconds;
|
|
|
|
uint16_t mseconds;
|
|
|
|
|
|
|
|
if (n_args == 1) {
|
|
|
|
// get the seconds and the milliseconds from the RTC
|
|
|
|
MAP_PRCMRTCGet(&seconds, &mseconds);
|
|
|
|
mseconds = RTC_CYCLES_U16MS(mseconds);
|
2015-05-10 20:00:19 -04:00
|
|
|
timeutils_seconds_since_2000_to_struct_time(seconds, &tm);
|
2015-02-06 09:35:48 -05:00
|
|
|
|
|
|
|
mp_obj_t tuple[8] = {
|
|
|
|
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_wday),
|
|
|
|
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(mseconds)
|
|
|
|
};
|
|
|
|
return mp_obj_new_tuple(8, tuple);
|
|
|
|
} else {
|
|
|
|
// set date and time
|
|
|
|
mp_obj_t *items;
|
|
|
|
mp_obj_get_array_fixed_n(args[1], 8, &items);
|
|
|
|
|
|
|
|
tm.tm_year = mp_obj_get_int(items[0]);
|
|
|
|
tm.tm_mon = mp_obj_get_int(items[1]);
|
|
|
|
tm.tm_mday = mp_obj_get_int(items[2]);
|
|
|
|
// Skip the weekday
|
|
|
|
tm.tm_hour = mp_obj_get_int(items[4]);
|
|
|
|
tm.tm_min = mp_obj_get_int(items[5]);
|
|
|
|
tm.tm_sec = mp_obj_get_int(items[6]);
|
|
|
|
mseconds = mp_obj_get_int(items[7]);
|
|
|
|
|
2015-05-10 20:00:19 -04:00
|
|
|
seconds = timeutils_seconds_since_2000(tm.tm_year, tm.tm_mon, tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec);
|
2015-02-06 09:35:48 -05:00
|
|
|
mseconds = RTC_U16MS_CYCLES(mseconds);
|
|
|
|
MAP_PRCMRTCSet(seconds, mseconds);
|
|
|
|
|
|
|
|
return mp_const_none;
|
|
|
|
}
|
|
|
|
}
|
2015-03-15 14:53:12 -04:00
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(pyb_rtc_datetime_obj, 1, 2, pyb_rtc_datetime);
|
2015-02-06 09:35:48 -05:00
|
|
|
|
2015-03-26 05:25:28 -04:00
|
|
|
/// \method callback(handler, value, pwrmode)
|
2015-03-14 04:59:47 -04:00
|
|
|
/// Creates a callback object associated with the real time clock
|
|
|
|
/// min num of arguments is 1 (value). The value is the alarm time
|
|
|
|
/// in the future, in msec
|
|
|
|
STATIC mp_obj_t pyb_rtc_callback (mp_uint_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
|
|
|
|
mp_arg_val_t args[mpcallback_INIT_NUM_ARGS];
|
|
|
|
mp_arg_parse_all(n_args - 1, pos_args + 1, kw_args, mpcallback_INIT_NUM_ARGS, mpcallback_init_args, args);
|
|
|
|
|
|
|
|
// check if any parameters were passed
|
2015-03-15 14:53:12 -04:00
|
|
|
mp_obj_t _callback = mpcallback_find((mp_obj_t)&pyb_rtc_obj);
|
2015-03-26 05:25:28 -04:00
|
|
|
if (kw_args->used > 0 || !_callback) {
|
2015-05-17 10:09:27 -04:00
|
|
|
uint32_t f_mseconds = MAX(1, args[3].u_int);
|
2015-03-14 04:59:47 -04:00
|
|
|
uint32_t seconds;
|
|
|
|
uint16_t mseconds;
|
|
|
|
// get the seconds and the milliseconds from the RTC
|
|
|
|
MAP_PRCMRTCGet(&seconds, &mseconds);
|
|
|
|
mseconds = RTC_CYCLES_U16MS(mseconds);
|
|
|
|
|
|
|
|
// configure the rtc alarm accordingly
|
2015-03-17 08:20:15 -04:00
|
|
|
seconds += f_mseconds / 1000;
|
|
|
|
mseconds += f_mseconds - ((f_mseconds / 1000) * 1000);
|
|
|
|
|
2015-03-26 05:25:28 -04:00
|
|
|
// disable the interrupt before updating anything
|
|
|
|
// (the object is not relevant here, the function already knows it)
|
|
|
|
pyb_rtc_callback_disable(NULL);
|
|
|
|
|
2015-03-17 08:20:15 -04:00
|
|
|
// set the match value
|
|
|
|
MAP_PRCMRTCMatchSet(seconds, mseconds);
|
|
|
|
|
2015-05-11 13:56:55 -04:00
|
|
|
// save the power mode data for later
|
2015-03-17 08:20:15 -04:00
|
|
|
pybrtc_data.prwmode = args[4].u_int;
|
|
|
|
|
|
|
|
// create the callback
|
2015-03-15 14:53:12 -04:00
|
|
|
_callback = mpcallback_new ((mp_obj_t)&pyb_rtc_obj, args[1].u_obj, &pybrtc_cb_methods);
|
2015-03-17 08:20:15 -04:00
|
|
|
|
|
|
|
// set the lpds callback
|
|
|
|
pybsleep_set_timer_lpds_callback(_callback);
|
|
|
|
|
2015-05-01 16:04:26 -04:00
|
|
|
// the interrupt priority is ignored since it's already set to to highest level by the sleep module
|
2015-03-17 08:20:15 -04:00
|
|
|
// to make sure that the wakeup callbacks are always called first when resuming from sleep
|
|
|
|
|
|
|
|
// enable the interrupt (the object is not relevant here, the function already knows it)
|
|
|
|
pyb_rtc_callback_enable(NULL);
|
2015-03-14 04:59:47 -04:00
|
|
|
}
|
2015-03-15 14:53:12 -04:00
|
|
|
return _callback;
|
2015-03-14 04:59:47 -04:00
|
|
|
}
|
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_KW(pyb_rtc_callback_obj, 1, pyb_rtc_callback);
|
|
|
|
|
2015-02-06 09:35:48 -05:00
|
|
|
STATIC const mp_map_elem_t pyb_rtc_locals_dict_table[] = {
|
|
|
|
{ MP_OBJ_NEW_QSTR(MP_QSTR_datetime), (mp_obj_t)&pyb_rtc_datetime_obj },
|
2015-03-14 04:59:47 -04:00
|
|
|
{ MP_OBJ_NEW_QSTR(MP_QSTR_callback), (mp_obj_t)&pyb_rtc_callback_obj },
|
2015-02-06 09:35:48 -05:00
|
|
|
};
|
|
|
|
STATIC MP_DEFINE_CONST_DICT(pyb_rtc_locals_dict, pyb_rtc_locals_dict_table);
|
|
|
|
|
2015-03-14 04:59:47 -04:00
|
|
|
STATIC const mp_obj_type_t pyb_rtc_type = {
|
2015-02-06 09:35:48 -05:00
|
|
|
{ &mp_type_type },
|
|
|
|
.name = MP_QSTR_RTC,
|
|
|
|
.locals_dict = (mp_obj_t)&pyb_rtc_locals_dict,
|
|
|
|
};
|
2015-03-14 04:59:47 -04:00
|
|
|
|
|
|
|
STATIC const mp_cb_methods_t pybrtc_cb_methods = {
|
|
|
|
.init = pyb_rtc_callback,
|
|
|
|
.enable = pyb_rtc_callback_enable,
|
|
|
|
.disable = pyb_rtc_callback_disable,
|
|
|
|
};
|
|
|
|
|
|
|
|
const mp_obj_base_t pyb_rtc_obj = {&pyb_rtc_type};
|