Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 995114b

Browse files
authoredAug 8, 2020
Add support to set the clock rate and have sntp use it. (#3236)
1 parent 28c6bda commit 995114b

File tree

4 files changed

+28
-13
lines changed

4 files changed

+28
-13
lines changed
 

‎app/include/rtc/rtctime.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ struct rtc_tm{
6363
void TEXT_SECTION_ATTR rtctime_early_startup (void);
6464
void rtctime_late_startup (void);
6565
void rtctime_adjust_rate (int rate);
66+
int rtctime_get_rate (void);
6667
void rtctime_gettimeofday (struct rtc_timeval *tv);
6768
void rtctime_settimeofday (const struct rtc_timeval *tv);
6869
bool rtctime_have_time (void);

‎app/modules/rtctime.c

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,11 @@ void rtctime_late_startup (void)
5757
rtc_time_switch_system ();
5858
}
5959

60+
int rtctime_get_rate (void)
61+
{
62+
return rtc_time_get_rate();
63+
}
64+
6065
void rtctime_adjust_rate (int rate)
6166
{
6267
rtc_time_set_rate (rate);
@@ -131,13 +136,15 @@ static int rtctime_set (lua_State *L)
131136
if (!rtc_time_check_magic ())
132137
rtc_time_prepare ();
133138

134-
uint32_t sec = luaL_checkinteger (L, 1);
135-
uint32_t usec = 0;
136-
if (lua_isnumber (L, 2))
137-
usec = lua_tointeger (L, 2);
139+
if (lua_isnumber(L, 1)) {
140+
uint32_t sec = luaL_checkinteger (L, 1);
141+
uint32_t usec = 0;
142+
if (lua_isnumber (L, 2))
143+
usec = lua_tointeger (L, 2);
138144

139-
struct rtc_timeval tv = { sec, usec };
140-
rtctime_settimeofday (&tv);
145+
struct rtc_timeval tv = { sec, usec };
146+
rtctime_settimeofday (&tv);
147+
}
141148

142149
if (lua_isnumber(L, 3))
143150
rtc_time_set_rate(lua_tointeger(L, 3));

‎app/modules/sntp.c

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ static uint8_t using_offset;
148148
static uint8_t the_offset;
149149
static uint8_t pending_LI;
150150
static int32_t next_midnight;
151-
static uint64_t pll_increment;
151+
static int32_t pll_increment;
152152

153153
#define PLL_A (1 << (32 - 11))
154154
#define PLL_B (1 << (32 - 11 - 2))
@@ -257,12 +257,12 @@ static void sntp_handle_result(lua_State *L) {
257257
}
258258
if (state->is_on_timeout && state->best.delta > SUS_TO_FRAC(-200000) && state->best.delta < SUS_TO_FRAC(200000)) {
259259
// Adjust rate
260-
// f is frequency -- f should be 1 << 32 for nominal
261-
sntp_dbg("delta=%d, increment=%d, ", (int32_t) state->best.delta, (int32_t) pll_increment);
262-
int64_t f = ((state->best.delta * PLL_A) >> 32) + pll_increment;
260+
// f is frequency -- f should be 1 << 32 for nominal -- but we store it as an offset
261+
sntp_dbg("delta=%d, increment=%d, ", (int32_t) state->best.delta, pll_increment);
262+
int f = ((state->best.delta * PLL_A) >> 32) + pll_increment;
263263
pll_increment += (state->best.delta * PLL_B) >> 32;
264-
sntp_dbg("f=%d, increment=%d\n", (int32_t) f, (int32_t) pll_increment);
265-
rtctime_adjust_rate((int32_t) f);
264+
sntp_dbg("f=%d, increment=%d\n", f, pll_increment);
265+
rtctime_adjust_rate(f);
266266
} else {
267267
rtctime_settimeofday (&tv);
268268
}
@@ -824,6 +824,10 @@ static int sntp_sync (lua_State *L)
824824
}
825825
}
826826

827+
#ifdef LUA_USE_MODULES_RTCTIME
828+
pll_increment = rtctime_get_rate();
829+
#endif
830+
827831
luaL_unref (L, LUA_REGISTRYINDEX, state->list_ref);
828832
state->list_ref = luaL_ref(L, LUA_REGISTRYINDEX);
829833
sntp_dolookups(L);

‎docs/modules/rtctime.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,8 +139,11 @@ It is highly recommended that the timestamp is obtained via NTP (see [SNTP modul
139139

140140
Values very close to the epoch are not supported. This is a side effect of keeping the memory requirements as low as possible. Considering that it's no longer 1970, this is not considered a problem.
141141

142+
Times are specified across two arguments, seconds and microseconds. If microseconds are not specified, the default is 0 (i.e., time is exactly at the boundary between two seconds).
143+
In addition to the time, a third argument specifies the local clock's drift rate estimate, as documented in `rtctime.get()`. If this argument is not given or is `nil`, the current rate estimate will not be altered, and the rate estimate may be set without changing the time by leaving the seconds and microseconds arguments `nil`. The rate is not (unfortunately) a fixed parameter for a particular nodemcu board. It varies with (at least) the temperature and the age of the board.
144+
142145
#### Syntax
143-
`rtctime.set(seconds, microseconds, [rate])`
146+
`rtctime.set([seconds], [microseconds], [rate])`
144147

145148
#### Parameters
146149
- `seconds` the seconds part, counted from the Unix epoch

0 commit comments

Comments
 (0)
Please sign in to comment.