Removed non working clock_* calls from example. Using vars for tz and dst.

This commit is contained in:
doragasu 2016-03-13 17:04:03 +01:00
parent 0482aebf7d
commit f14025b1c7
3 changed files with 62 additions and 37 deletions

View file

@ -51,12 +51,12 @@ void SntpTsk(void *pvParameters)
sntp_set_servers(servers, sizeof(servers) / sizeof(char*)); sntp_set_servers(servers, sizeof(servers) / sizeof(char*));
printf("DONE!\n"); printf("DONE!\n");
struct timespec ts; // struct timespec ts;
clock_getres(CLOCK_REALTIME, &ts); // clock_getres(CLOCK_REALTIME, &ts);
printf("Time resolution: %d secs, %d nanosecs\n", t.tv_sec, t.tv_nsec); // printf("Time resolution: %d secs, %d nanosecs\n", t.tv_sec, t.tv_nsec);
//
clock_gettime(CLOCK_REALTIME, &t); // clock_gettime(CLOCK_REALTIME, &t);
printf("Time: %d secs, %d nanosecs\n", t.tv_sec, t.tv_nsec); // printf("Time: %d secs, %d nanosecs\n", t.tv_sec, t.tv_nsec);
while(1) { while(1) {
vTaskDelayMs(5000); vTaskDelayMs(5000);

View file

@ -39,6 +39,10 @@ void sntp_set_daylight(int day_light);
/// us is not null, it will be filled with the microseconds. /// us is not null, it will be filled with the microseconds.
time_t sntp_get_rtc_time(int32_t *us); time_t sntp_get_rtc_time(int32_t *us);
/// Returns the time in seconds since Epoch. If tloc is not NULL, return
/// value is also stored in the memory pointed by tloc.
time_t time(time_t *tloc);
/// Set SNTP servers. Up to SNTP_NUM_SERVERS_SUPPORTED can be set. /// Set SNTP servers. Up to SNTP_NUM_SERVERS_SUPPORTED can be set.
/// Returns 0 if OK, less than 0 if error. /// Returns 0 if OK, less than 0 if error.
/// NOTE: This function must NOT be called before sntp_initialize(). /// NOTE: This function must NOT be called before sntp_initialize().

View file

@ -1,5 +1,5 @@
/* /*
* Auxiliar functions to handle date/time along with lwIP sntp implementation. * Auxiliary functions to handle date/time along with lwIP sntp implementation.
* *
* Jesus Alonso (doragasu) * Jesus Alonso (doragasu)
*/ */
@ -15,46 +15,56 @@
#include "sntp.h" #include "sntp.h"
#define TIMER_COUNT RTC.COUNTER #define TIMER_COUNT RTC.COUNTER
#define __UNUSED(var) (void)var
// daylight settings // daylight settings
// Base calculated with value obtained from NTP server (64 bits) // Base calculated with value obtained from NTP server (64 bits)
#define sntp_base (*((uint64_t*)RTC.SCRATCH)) #define sntp_base (*((uint64_t*)RTC.SCRATCH))
// Timer value when base was obtained // Timer value when base was obtained
#define tim_ref (RTC.SCRATCH[2]) #define tim_ref (RTC.SCRATCH[2])
// Calibration value
#define cal (RTC.SCRATCH[3])
// Timezone (-11 to +13)
static int8_t tz;
// Daylight savings
static bool dst;
// Setters and getters for CAL, TZ and DST. //// Setters and getters for CAL, TZ and DST.
#define RTC_CAL_SET(val) (RTC.SCRATCH[3] |= (val) & 0x0000FFFF) //#define RTC_CAL_SET(val) (RTC.SCRATCH[3] |= (val) & 0x0000FFFF)
#define RTC_DST_SET(val) (RTC.SCRATCH[3] |= ((val)<<16) & 0x00010000) //#define RTC_DST_SET(val) (RTC.SCRATCH[3] |= ((val)<<16) & 0x00010000)
#define RTC_TZ_SET(val) (RTC.SCRATCH[3] |= ((val)<<24) & 0xFF000000) //#define RTC_TZ_SET(val) (RTC.SCRATCH[3] |= ((val)<<24) & 0xFF000000)
//
#define RTC_CAL_GET() (RTC.SCRATCH[3] & 0x0000FFFF) //#define RTC_CAL_GET() (RTC.SCRATCH[3] & 0x0000FFFF)
#define RTC_DST_GET() ((RTC.SCRATCH[3] & 0x00010000)>>16) //#define RTC_DST_GET() ((RTC.SCRATCH[3] & 0x00010000)>>16)
#define RTC_TZ_GET() ((((int)RTC.SCRATCH[3]) & ((int)0xFF000000))>>24) //#define RTC_TZ_GET() ((((int)RTC.SCRATCH[3]) & ((int)0xFF000000))>>24)
// Implemented in sntp.c // Implemented in sntp.c
void sntp_init(void); void sntp_init(void);
// Sets time zone. Allowed values are in the range [-11, 13]. // Sets time zone. Allowed values are in the range [-11, 13].
// NOTE: Settings do not take effect until SNTP time is updated. It is // NOTE: Settings do not take effect until SNTP time is updated.
void sntp_set_timezone(int time_zone) { void sntp_set_timezone(int time_zone) {
//tz = time_zone; tz = time_zone;
RTC_TZ_SET(time_zone); //RTC_TZ_SET(time_zone);
} }
// Sets daylight. // Sets daylight.
// NOTE: Settings do not take effect until SNTP time is updated. // NOTE: Settings do not take effect until SNTP time is updated.
void sntp_set_daylight(int day_light) { void sntp_set_daylight(int day_light) {
//dst = day_light; dst = day_light;
RTC_DST_SET(day_light); //RTC_DST_SET(day_light);
} }
void sntp_initialize(int time_zone, int day_light) { void sntp_initialize(int time_zone, int day_light) {
sntp_base = 0; sntp_base = 0;
RTC_TZ_SET(time_zone); tz = time_zone;
RTC_DST_SET(day_light); //RTC_TZ_SET(time_zone);
dst = day_light;
//RTC_DST_SET(day_light);
// To avoid div by 0 exceptions if requesting time before SNTP config // To avoid div by 0 exceptions if requesting time before SNTP config
RTC_CAL_SET(1); cal = 1;
//RTC_CAL_SET(1);
tim_ref = TIMER_COUNT; tim_ref = TIMER_COUNT;
sntp_init(); sntp_init();
} }
@ -72,7 +82,7 @@ static inline void sntp_check_timer_wrap(uint32_t current_value) {
} }
// Return secs. If us is not a null pointer, fill it with usecs // Return secs. If us is not a null pointer, fill it with usecs
time_t sntp_get_rtc_time(int32_t *us) { inline time_t sntp_get_rtc_time(int32_t *us) {
time_t secs; time_t secs;
uint32_t tim; uint32_t tim;
uint64_t base; uint64_t base;
@ -81,39 +91,50 @@ time_t sntp_get_rtc_time(int32_t *us) {
// Check for timer wrap // Check for timer wrap
sntp_check_timer_wrap(tim); sntp_check_timer_wrap(tim);
base = sntp_base + tim - tim_ref; base = sntp_base + tim - tim_ref;
secs = base * RTC_CAL_GET() / (1000000U<<12); // secs = base * RTC_CAL_GET() / (1000000U<<12);
secs = base * cal / (1000000U<<12);
if (us) { if (us) {
*us = base * RTC_CAL_GET() % (1000000U<<12); // *us = base * RTC_CAL_GET() % (1000000U<<12);
*us = base * cal % (1000000U<<12);
} }
return secs; return secs;
} }
// Syscall implementation
int _gettimeofday_r(struct _reent *r, struct timeval *tp, void *tzp) { int _gettimeofday_r(struct _reent *r, struct timeval *tp, void *tzp) {
(void)r; __UNUSED(r);
(void)tzp; __UNUSED(tzp);
printf("DEB; gettimeofday called"); printf("DEB; gettimeofday called");
tp->tv_sec = sntp_get_rtc_time((int32_t*)&tp->tv_usec); tp->tv_sec = sntp_get_rtc_time((int32_t*)&tp->tv_usec);
return 0; return 0;
} }
time_t time(time_t *tloc) {
time_t datetime;
datetime = sntp_get_rtc_time(NULL);
if (tloc) *tloc = datetime;
return datetime;
}
/// Update RTC timer. Called by SNTP module each time it receives an update. /// Update RTC timer. Called by SNTP module each time it receives an update.
void sntp_update_rtc(time_t t, uint32_t us) { void sntp_update_rtc(time_t t, uint32_t us) {
// Apply daylight and timezone correction // Apply daylight and timezone correction
t += (RTC_TZ_GET() + RTC_DST_GET()) * 3600; // t += (RTC_TZ_GET() + RTC_DST_GET()) * 3600;
t += (tz + dst) * 3600;
// DEBUG: Compute and print drift // DEBUG: Compute and print drift
int64_t sntp_current = sntp_base + TIMER_COUNT - tim_ref; int64_t sntp_current = sntp_base + TIMER_COUNT - tim_ref;
int64_t sntp_correct = (((uint64_t)us + (uint64_t)t * 1000000U)<<12) / RTC_CAL_GET(); // int64_t sntp_correct = (((uint64_t)us + (uint64_t)t * 1000000U)<<12) / RTC_CAL_GET();
printf("\nRTC Adjust: drift = %ld ticks, cal = %d\n", (time_t)(sntp_correct - sntp_current), RTC_CAL_GET()); int64_t sntp_correct = (((uint64_t)us + (uint64_t)t * 1000000U)<<12) / cal;
// printf("\nRTC Adjust: drift = %ld ticks, cal = %d\n", (time_t)(sntp_correct - sntp_current), RTC_CAL_GET());
printf("\nRTC Adjust: drift = %ld ticks, cal = %d\n", (time_t)(sntp_correct - sntp_current), cal);
tim_ref = TIMER_COUNT; tim_ref = TIMER_COUNT;
RTC_CAL_SET(sdk_system_rtc_clock_cali_proc()); cal = sdk_system_rtc_clock_cali_proc();
// RTC_CAL_SET(sdk_system_rtc_clock_cali_proc());
sntp_base = (((uint64_t)us + (uint64_t)t * 1000000U)<<12) / RTC_CAL_GET(); // sntp_base = (((uint64_t)us + (uint64_t)t * 1000000U)<<12) / RTC_CAL_GET();
sntp_base = (((uint64_t)us + (uint64_t)t * 1000000U)<<12) / cal;
// DEBUG: Print obtained secs and check calculated secs are the same
time_t deb = sntp_base * RTC_CAL_GET() / (1000000U<<12);
printf("\nT: %lu, %lu, %s\n", t, deb, ctime(&deb));
} }