time accuracy improvement

This commit is contained in:
Verkehrsrot 2019-03-19 01:46:20 +01:00
parent 0f311b5ea9
commit ffaa13283b
6 changed files with 32 additions and 26 deletions

View File

@ -112,7 +112,7 @@ extern uint16_t volatile macs_total, macs_wifi, macs_ble,
extern bool volatile TimePulseTick; // 1sec pps flag set by GPS or RTC
extern timesource_t timeSource;
extern hw_timer_t *displayIRQ, *ppsIRQ;
extern SemaphoreHandle_t I2Caccess, TimePulse;
extern SemaphoreHandle_t I2Caccess;
extern TaskHandle_t irqHandlerTask, ClockTask;
extern TimerHandle_t WifiChanTimer;
extern Timezone myTZ;

View File

@ -84,7 +84,7 @@ uint16_t volatile macs_total = 0, macs_wifi = 0, macs_ble = 0,
hw_timer_t *ppsIRQ = NULL, *displayIRQ = NULL;
TaskHandle_t irqHandlerTask, ClockTask;
SemaphoreHandle_t I2Caccess, TimePulse;
SemaphoreHandle_t I2Caccess;
bool volatile TimePulseTick = false;
time_t userUTCTime = 0;
timesource_t timeSource = _unsynced;
@ -113,8 +113,6 @@ void setup() {
if (I2Caccess)
xSemaphoreGive(I2Caccess); // Flag the i2c bus available for use
TimePulse = xSemaphoreCreateBinary(); // as signal that shows time pulse flip
// disable brownout detection
#ifdef DISABLE_BROWNOUT
// register with brownout is at address DR_REG_RTCCNTL_BASE + 0xd4

View File

@ -49,6 +49,10 @@ uint8_t rtc_init(void) {
uint8_t set_rtctime(time_t t) { // t is UTC in seconds epoch time
if (I2C_MUTEX_LOCK()) {
Rtc.SetDateTime(RtcDateTime(t - SECS_YR_2000)); // epoch -> sec2000
#ifdef RTC_INT // sync rtc 1Hz pulse on top of second
Rtc.SetSquareWavePin(DS3231SquareWavePin_ModeNone); // off
Rtc.SetSquareWavePin(DS3231SquareWavePin_ModeClock); // start
#endif
I2C_MUTEX_UNLOCK();
ESP_LOGI(TAG, "RTC time synced");
return 1; // success

View File

@ -124,7 +124,6 @@ void IRAM_ATTR CLOCKIRQ(void) {
&xHigherPriorityTaskWoken);
#if defined GPS_INT || defined RTC_INT
xSemaphoreGiveFromISR(TimePulse, &xHigherPriorityTaskWoken);
TimePulseTick = !TimePulseTick; // flip ticker
#endif

View File

@ -109,10 +109,14 @@ void process_timesync_req(void *taskparameter) {
// wait until next cycle
vTaskDelay(pdMS_TO_TICKS(TIME_SYNC_CYCLE * 1000));
} else {
// send flush to open a receive window for last time_sync_ans
payload.reset();
payload.addByte(0x99);
SendPayload(RCMDPORT, prio_high);
// send flush to open a receive window for last time_sync_answer
// payload.reset();
// payload.addByte(0x99);
// SendPayload(RCMDPORT, prio_high);
// Send a payload-less message to open a receive window for last
// time_sync_answer
void LMIC_sendAlive();
}
}
} // for
@ -145,13 +149,15 @@ void process_timesync_req(void *taskparameter) {
ESP_LOGD(TAG, "[%0.3f] waiting %d ms", millis() / 1000.0, wait_ms);
vTaskDelay(pdMS_TO_TICKS(wait_ms));
#if !defined(GPS_INT) && !defined(RTC_INT)
// sync timer pps to top of second
if (ppsIRQ) {
timerRestart(ppsIRQ); // reset pps timer
CLOCKIRQ(); // fire clock pps interrupt
}
time_to_set++; // advance time 1 second
#endif
setTime(time_to_set); // set the time on top of second
setTime(++time_to_set); // +1 sec after waiting for top of seceond
#ifdef HAS_RTC
set_rtctime(time_to_set); // calibrate RTC if we have one
#endif
@ -162,8 +168,7 @@ void process_timesync_req(void *taskparameter) {
ESP_LOGI(TAG, "[%0.3f] Timesync finished, time adjusted by %.3f sec",
millis() / 1000.0, myClock_secTick(time_offset).count());
} else
ESP_LOGI(TAG,
"[%0.3f] Timesync finished, time is up to date",
ESP_LOGI(TAG, "[%0.3f] Timesync finished, time is up to date",
millis() / 1000.0);
} else
ESP_LOGW(TAG, "[%0.3f] Timesync failed, outdated time calculated",
@ -212,9 +217,9 @@ int recv_timesync_ans(uint8_t buf[], uint8_t buf_len) {
uint16_t timestamp_msec; // convert 1/250th sec fractions to ms
uint32_t timestamp_sec;
// fetch timeserver time from 4 bytes containing the UTC seconds since unix
// epoch. Octet order is big endian. Casts are necessary, because buf is an
// array of single byte values, and they might overflow when shifted
// fetch timeserver time from 4 bytes containing the UTC seconds since
// unix epoch. Octet order is big endian. Casts are necessary, because buf
// is an array of single byte values, and they might overflow when shifted
timestamp_sec = ((uint32_t)buf[4]) | (((uint32_t)buf[3]) << 8) |
(((uint32_t)buf[2]) << 16) | (((uint32_t)buf[1]) << 24);