2015-06-26 08:36:39 +02:00
|
|
|
// Module for RTC time keeping
|
2015-06-25 07:32:21 +02:00
|
|
|
|
2015-12-16 06:04:58 +01:00
|
|
|
#include "module.h"
|
2015-06-25 07:32:21 +02:00
|
|
|
#include "lauxlib.h"
|
2015-07-06 05:45:35 +02:00
|
|
|
|
|
|
|
#include "rtc/rtctime_internal.h"
|
2015-06-25 07:32:21 +02:00
|
|
|
#include "rtc/rtctime.h"
|
|
|
|
|
2015-07-06 05:45:35 +02:00
|
|
|
|
2016-07-19 22:01:26 +02:00
|
|
|
/* seconds per day */
|
|
|
|
#define SPD 24*60*60
|
|
|
|
|
|
|
|
/* days per month -- nonleap! */
|
|
|
|
static const short __spm[13] =
|
|
|
|
{ 0,
|
|
|
|
(31),
|
|
|
|
(31+28),
|
|
|
|
(31+28+31),
|
|
|
|
(31+28+31+30),
|
|
|
|
(31+28+31+30+31),
|
|
|
|
(31+28+31+30+31+30),
|
|
|
|
(31+28+31+30+31+30+31),
|
|
|
|
(31+28+31+30+31+30+31+31),
|
|
|
|
(31+28+31+30+31+30+31+31+30),
|
|
|
|
(31+28+31+30+31+30+31+31+30+31),
|
|
|
|
(31+28+31+30+31+30+31+31+30+31+30),
|
|
|
|
(31+28+31+30+31+30+31+31+30+31+30+31),
|
|
|
|
};
|
|
|
|
|
|
|
|
static int __isleap (int year) {
|
|
|
|
/* every fourth year is a leap year except for century years that are
|
|
|
|
* not divisible by 400. */
|
|
|
|
/* return (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0)); */
|
|
|
|
return (!(year % 4) && ((year % 100) || !(year % 400)));
|
|
|
|
}
|
|
|
|
|
2015-07-06 05:45:35 +02:00
|
|
|
// ******* C API functions *************
|
2016-12-14 12:39:31 +01:00
|
|
|
void __attribute__((noreturn)) TEXT_SECTION_ATTR rtc_time_enter_deep_sleep_final (void)
|
|
|
|
{
|
|
|
|
ets_intr_lock();
|
|
|
|
Cache_Read_Disable();
|
|
|
|
rtc_reg_write(0x18,8);
|
|
|
|
rtc_reg_write_and_loop(0x08,0x00100000); // go to sleep
|
|
|
|
__builtin_unreachable();
|
|
|
|
}
|
2015-07-06 05:45:35 +02:00
|
|
|
|
|
|
|
void rtctime_early_startup (void)
|
|
|
|
{
|
2019-04-05 17:01:45 +02:00
|
|
|
// Cache_Read_Enable (0, 0, 1);
|
2015-07-06 05:45:35 +02:00
|
|
|
rtc_time_register_bootup ();
|
|
|
|
rtc_time_switch_clocks ();
|
2019-04-05 17:01:45 +02:00
|
|
|
// Cache_Read_Disable ();
|
2015-07-06 05:45:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void rtctime_late_startup (void)
|
|
|
|
{
|
|
|
|
rtc_time_switch_system ();
|
|
|
|
}
|
|
|
|
|
2017-07-18 22:51:20 +02:00
|
|
|
void rtctime_adjust_rate (int rate)
|
|
|
|
{
|
|
|
|
rtc_time_set_rate (rate);
|
|
|
|
}
|
|
|
|
|
2015-07-06 05:45:35 +02:00
|
|
|
void rtctime_gettimeofday (struct rtc_timeval *tv)
|
|
|
|
{
|
|
|
|
rtc_time_gettimeofday (tv);
|
|
|
|
}
|
|
|
|
|
|
|
|
void rtctime_settimeofday (const struct rtc_timeval *tv)
|
|
|
|
{
|
|
|
|
if (!rtc_time_check_magic ())
|
|
|
|
rtc_time_prepare ();
|
2017-07-18 22:51:20 +02:00
|
|
|
int32_t rate = rtc_time_get_rate();
|
2015-07-06 05:45:35 +02:00
|
|
|
rtc_time_settimeofday (tv);
|
2017-07-18 22:51:20 +02:00
|
|
|
rtc_time_set_rate(rate);
|
2015-07-06 05:45:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool rtctime_have_time (void)
|
|
|
|
{
|
|
|
|
return rtc_time_have_time ();
|
|
|
|
}
|
|
|
|
|
|
|
|
void rtctime_deep_sleep_us (uint32_t us)
|
|
|
|
{
|
|
|
|
rtc_time_deep_sleep_us (us);
|
|
|
|
}
|
|
|
|
|
|
|
|
void rtctime_deep_sleep_until_aligned_us (uint32_t align_us, uint32_t min_us)
|
|
|
|
{
|
|
|
|
rtc_time_deep_sleep_until_aligned (align_us, min_us);
|
|
|
|
}
|
|
|
|
|
2016-07-19 22:01:26 +02:00
|
|
|
void rtctime_gmtime (const int32 stamp, struct rtc_tm *r)
|
|
|
|
{
|
|
|
|
int32_t i;
|
|
|
|
int32_t work = stamp % (SPD);
|
|
|
|
r->tm_sec = work % 60; work /= 60;
|
|
|
|
r->tm_min = work % 60; r->tm_hour = work / 60;
|
|
|
|
work = stamp / (SPD);
|
|
|
|
r->tm_wday = (4 + work) % 7;
|
|
|
|
for (i = 1970; ; ++i) {
|
|
|
|
int32_t k = __isleap (i) ? 366 : 365;
|
|
|
|
if (work >= k) {
|
|
|
|
work -= k;
|
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
r->tm_year = i - 1900;
|
|
|
|
r->tm_yday = work;
|
|
|
|
|
|
|
|
r->tm_mday = 1;
|
|
|
|
if (__isleap (i) && (work > 58)) {
|
|
|
|
if (work == 59) r->tm_mday = 2; /* 29.2. */
|
|
|
|
work -= 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 11; i && (__spm[i] > work); --i) ;
|
|
|
|
r->tm_mon = i;
|
|
|
|
r->tm_mday += work - __spm[i];
|
|
|
|
}
|
|
|
|
|
2015-07-06 05:45:35 +02:00
|
|
|
|
|
|
|
|
|
|
|
// ******* Lua API functions *************
|
|
|
|
|
|
|
|
// rtctime.set (sec, usec)
|
|
|
|
static int rtctime_set (lua_State *L)
|
2015-06-25 07:32:21 +02:00
|
|
|
{
|
|
|
|
if (!rtc_time_check_magic ())
|
|
|
|
rtc_time_prepare ();
|
|
|
|
|
2020-06-20 22:06:32 +02:00
|
|
|
uint32_t sec = luaL_checkinteger (L, 1);
|
2015-06-25 07:32:21 +02:00
|
|
|
uint32_t usec = 0;
|
|
|
|
if (lua_isnumber (L, 2))
|
2020-06-20 22:06:32 +02:00
|
|
|
usec = lua_tointeger (L, 2);
|
2015-06-25 07:32:21 +02:00
|
|
|
|
|
|
|
struct rtc_timeval tv = { sec, usec };
|
2015-07-06 05:45:35 +02:00
|
|
|
rtctime_settimeofday (&tv);
|
2017-07-18 22:51:20 +02:00
|
|
|
|
|
|
|
if (lua_isnumber(L, 3))
|
2020-06-20 22:06:32 +02:00
|
|
|
rtc_time_set_rate(lua_tointeger(L, 3));
|
2015-06-25 07:32:21 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-07-06 05:45:35 +02:00
|
|
|
// sec, usec = rtctime.get ()
|
|
|
|
static int rtctime_get (lua_State *L)
|
2015-06-25 07:32:21 +02:00
|
|
|
{
|
|
|
|
struct rtc_timeval tv;
|
2015-07-06 05:45:35 +02:00
|
|
|
rtctime_gettimeofday (&tv);
|
2020-06-20 22:06:32 +02:00
|
|
|
lua_pushinteger (L, tv.tv_sec);
|
|
|
|
lua_pushinteger (L, tv.tv_usec);
|
|
|
|
lua_pushinteger (L, rtc_time_get_rate());
|
2017-07-18 22:51:20 +02:00
|
|
|
return 3;
|
2015-06-25 07:32:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void do_sleep_opt (lua_State *L, int idx)
|
|
|
|
{
|
|
|
|
if (lua_isnumber (L, idx))
|
|
|
|
{
|
2020-06-20 22:06:32 +02:00
|
|
|
uint32_t opt = lua_tointeger (L, idx);
|
2015-06-25 07:32:21 +02:00
|
|
|
if (opt < 0 || opt > 4)
|
|
|
|
luaL_error (L, "unknown sleep option");
|
2016-01-20 09:37:03 +01:00
|
|
|
system_deep_sleep_set_option (opt);
|
2015-06-25 07:32:21 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-23 23:12:23 +01:00
|
|
|
// rtctime.adjust_delta (usec)
|
|
|
|
static int rtctime_adjust_delta (lua_State *L)
|
|
|
|
{
|
2020-06-20 22:06:32 +02:00
|
|
|
uint32_t us = luaL_checkinteger (L, 1);
|
|
|
|
lua_pushinteger(L, rtc_time_adjust_delta_by_rate(us));
|
2018-02-23 23:12:23 +01:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-06-25 07:32:21 +02:00
|
|
|
// rtctime.dsleep (usec, option)
|
|
|
|
static int rtctime_dsleep (lua_State *L)
|
|
|
|
{
|
2020-06-20 22:06:32 +02:00
|
|
|
uint32_t us = luaL_checkinteger (L, 1);
|
2015-06-25 07:32:21 +02:00
|
|
|
do_sleep_opt (L, 2);
|
2015-07-06 05:45:35 +02:00
|
|
|
rtctime_deep_sleep_us (us); // does not return
|
2015-06-25 07:32:21 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// rtctime.dsleep_aligned (aligned_usec, min_usec, option)
|
|
|
|
static int rtctime_dsleep_aligned (lua_State *L)
|
|
|
|
{
|
2015-07-06 05:45:35 +02:00
|
|
|
if (!rtctime_have_time ())
|
2015-06-25 07:32:21 +02:00
|
|
|
return luaL_error (L, "time not available, unable to align");
|
|
|
|
|
2020-06-20 22:06:32 +02:00
|
|
|
uint32_t align_us = luaL_checkinteger (L, 1);
|
|
|
|
uint32_t min_us = luaL_checkinteger (L, 2);
|
2015-06-25 07:32:21 +02:00
|
|
|
do_sleep_opt (L, 3);
|
2015-07-06 05:45:35 +02:00
|
|
|
rtctime_deep_sleep_until_aligned_us (align_us, min_us); // does not return
|
2015-06-25 07:32:21 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-01-22 20:17:46 +01:00
|
|
|
#define ADD_TABLE_ITEM(L, key, val) \
|
|
|
|
lua_pushinteger (L, val); \
|
|
|
|
lua_setfield (L, -2, key);
|
2016-07-19 22:01:26 +02:00
|
|
|
|
|
|
|
// rtctime.epoch2cal (stamp)
|
|
|
|
static int rtctime_epoch2cal (lua_State *L)
|
|
|
|
{
|
|
|
|
struct rtc_tm date;
|
|
|
|
int32_t stamp = luaL_checkint (L, 1);
|
|
|
|
luaL_argcheck (L, stamp >= 0, 1, "wrong arg range");
|
|
|
|
|
|
|
|
rtctime_gmtime (stamp, &date);
|
|
|
|
|
|
|
|
/* construct Lua table */
|
|
|
|
lua_createtable (L, 0, 8);
|
2017-01-22 20:17:46 +01:00
|
|
|
ADD_TABLE_ITEM (L, "yday", date.tm_yday + 1);
|
|
|
|
ADD_TABLE_ITEM (L, "wday", date.tm_wday + 1);
|
|
|
|
ADD_TABLE_ITEM (L, "year", date.tm_year + 1900);
|
|
|
|
ADD_TABLE_ITEM (L, "mon", date.tm_mon + 1);
|
|
|
|
ADD_TABLE_ITEM (L, "day", date.tm_mday);
|
|
|
|
ADD_TABLE_ITEM (L, "hour", date.tm_hour);
|
|
|
|
ADD_TABLE_ITEM (L, "min", date.tm_min);
|
|
|
|
ADD_TABLE_ITEM (L, "sec", date.tm_sec);
|
2016-07-19 22:01:26 +02:00
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2015-06-25 07:32:21 +02:00
|
|
|
// Module function map
|
2020-04-27 02:13:38 +02:00
|
|
|
LROT_BEGIN(rtctime, NULL, 0)
|
2019-05-08 13:08:20 +02:00
|
|
|
LROT_FUNCENTRY( set, rtctime_set )
|
|
|
|
LROT_FUNCENTRY( get, rtctime_get )
|
|
|
|
LROT_FUNCENTRY( adjust_delta, rtctime_adjust_delta )
|
|
|
|
LROT_FUNCENTRY( dsleep, rtctime_dsleep )
|
|
|
|
LROT_FUNCENTRY( dsleep_aligned, rtctime_dsleep_aligned )
|
|
|
|
LROT_FUNCENTRY( epoch2cal, rtctime_epoch2cal )
|
2020-04-27 02:13:38 +02:00
|
|
|
LROT_END(rtctime, NULL, 0)
|
2019-05-08 13:08:20 +02:00
|
|
|
|
|
|
|
|
|
|
|
NODEMCU_MODULE(RTCTIME, "rtctime", rtctime, NULL);
|