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
|
|
|
|
|
|
|
// ******* C API functions *************
|
|
|
|
|
|
|
|
void rtctime_early_startup (void)
|
|
|
|
{
|
|
|
|
Cache_Read_Enable (0, 0, 1);
|
|
|
|
rtc_time_register_bootup ();
|
|
|
|
rtc_time_switch_clocks ();
|
|
|
|
Cache_Read_Disable ();
|
|
|
|
}
|
|
|
|
|
|
|
|
void rtctime_late_startup (void)
|
|
|
|
{
|
|
|
|
rtc_time_switch_system ();
|
|
|
|
}
|
|
|
|
|
|
|
|
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 ();
|
|
|
|
rtc_time_settimeofday (tv);
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// ******* 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 ();
|
|
|
|
|
|
|
|
uint32_t sec = luaL_checknumber (L, 1);
|
|
|
|
uint32_t usec = 0;
|
|
|
|
if (lua_isnumber (L, 2))
|
|
|
|
usec = lua_tonumber (L, 2);
|
|
|
|
|
|
|
|
struct rtc_timeval tv = { sec, usec };
|
2015-07-06 05:45:35 +02:00
|
|
|
rtctime_settimeofday (&tv);
|
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);
|
2015-06-25 07:32:21 +02:00
|
|
|
lua_pushnumber (L, tv.tv_sec);
|
|
|
|
lua_pushnumber (L, tv.tv_usec);
|
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void do_sleep_opt (lua_State *L, int idx)
|
|
|
|
{
|
|
|
|
if (lua_isnumber (L, idx))
|
|
|
|
{
|
|
|
|
uint32_t opt = lua_tonumber (L, idx);
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// rtctime.dsleep (usec, option)
|
|
|
|
static int rtctime_dsleep (lua_State *L)
|
|
|
|
{
|
|
|
|
uint32_t us = luaL_checknumber (L, 1);
|
|
|
|
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");
|
|
|
|
|
|
|
|
uint32_t align_us = luaL_checknumber (L, 1);
|
|
|
|
uint32_t min_us = luaL_checknumber (L, 2);
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Module function map
|
2015-12-16 06:04:58 +01:00
|
|
|
static const LUA_REG_TYPE rtctime_map[] = {
|
2015-12-13 03:29:37 +01:00
|
|
|
{ LSTRKEY("set"), LFUNCVAL(rtctime_set) },
|
|
|
|
{ LSTRKEY("get"), LFUNCVAL(rtctime_get) },
|
|
|
|
{ LSTRKEY("dsleep"), LFUNCVAL(rtctime_dsleep) },
|
2015-06-25 07:32:21 +02:00
|
|
|
{ LSTRKEY("dsleep_aligned"), LFUNCVAL(rtctime_dsleep_aligned) },
|
|
|
|
{ LNILKEY, LNILVAL }
|
|
|
|
};
|
|
|
|
|
2015-12-16 06:04:58 +01:00
|
|
|
NODEMCU_MODULE(RTCTIME, "rtctime", rtctime_map, NULL);
|