bugfixing in time handling
This commit is contained in:
		
							parent
							
								
									a2b0cc7315
								
							
						
					
					
						commit
						ab5cd0b0a2
					
				| @ -12,8 +12,8 @@ | ||||
| extern RtcDS3231<TwoWire> Rtc; // make RTC instance globally available
 | ||||
| 
 | ||||
| int rtc_init(void); | ||||
| int set_rtctime(RtcDateTime t); | ||||
| int set_rtctime(uint32_t t); | ||||
| int set_rtctime(time_t t); | ||||
| void sync_rtctime(void); | ||||
| time_t get_rtctime(void); | ||||
| float get_rtctemp(void); | ||||
|  | ||||
| @ -38,10 +38,10 @@ void doHousekeeping() { | ||||
| // do cyclic write back system time to RTC if we have an external time source
 | ||||
| #if (defined TIME_SYNC_INTERVAL_LORA || defined TIME_SYNC_INTERVAL_GPS) &&     \ | ||||
|     defined HAS_RTC | ||||
|   if ((millis() >= nextRTCTimeSync) && timeSet) { | ||||
|   if ((millis() >= nextRTCTimeSync) && (timeStatus() == timeSet)) { | ||||
|     nextRTCTimeSync = millis() + TIME_WRITE_INTERVAL_RTC * | ||||
|                                      60000; // set up next time sync period
 | ||||
|     if (!set_rtctime(now())) | ||||
|     if (!set_rtctime(now())) // epoch time
 | ||||
|       ESP_LOGE(TAG, "RTC set time failure"); | ||||
|     else | ||||
|       ESP_LOGI(TAG, "RTC time updated"); | ||||
|  | ||||
| @ -70,9 +70,12 @@ time_t tmConvert_t(uint16_t YYYY, uint8_t MM, uint8_t DD, uint8_t hh, | ||||
| time_t get_gpstime(void) { | ||||
|   // never call now() in this function, this would cause a recursion!
 | ||||
|   time_t t = 0; | ||||
|   if (gps.time.age() < 1500) { | ||||
|   if ((gps.time.age() < 1500) && (gps.time.isValid())) { | ||||
|     t = tmConvert_t(gps.date.year(), gps.date.month(), gps.date.day(), | ||||
|                     gps.time.hour(), gps.time.minute(), gps.time.second()); | ||||
|     ESP_LOGD(TAG, "GPS time: %d/%d/%d %d:%d:%d", gps.date.year(), | ||||
|              gps.date.month(), gps.date.day(), gps.time.hour(), | ||||
|              gps.time.minute(), gps.time.second()); | ||||
|   } else { | ||||
|     ESP_LOGW(TAG, "GPS has no confident time"); | ||||
|   } | ||||
|  | ||||
| @ -105,7 +105,9 @@ int if482_init(void) { | ||||
| 
 | ||||
| } // if482_init
 | ||||
| 
 | ||||
| String if482Telegram(time_t t) { | ||||
| String if482Telegram(time_t tt) { | ||||
| 
 | ||||
|   time_t t = myTZ.toLocal(tt); | ||||
| 
 | ||||
|   char mon; | ||||
|   char buf[14] = "000000F000000"; | ||||
| @ -123,7 +125,8 @@ String if482Telegram(time_t t) { | ||||
|     break; | ||||
|   } // switch
 | ||||
| 
 | ||||
|   if (!timeNotSet) // do we have valid time?
 | ||||
|   if ((timeStatus() == timeSet) || | ||||
|       (timeStatus() == timeNeedsSync)) // do we have valid time?
 | ||||
|     snprintf(buf, sizeof buf, "%02u%02u%02u%1u%02u%02u%02u", year(t) - 2000, | ||||
|              month(t), day(t), weekday(t), hour(t), minute(t), second(t)); | ||||
| 
 | ||||
| @ -137,15 +140,18 @@ void if482_loop(void *pvParameters) { | ||||
| 
 | ||||
|   TickType_t wakeTime; | ||||
|   time_t t, tt; | ||||
|   const TickType_t shotTime = pdMS_TO_TICKS(IF482_OFFSET); | ||||
|   const TickType_t timeOffset = | ||||
|       pdMS_TO_TICKS(IF482_OFFSET); // duration of telegram transmit
 | ||||
|   const TickType_t startTime = xTaskGetTickCount(); // now
 | ||||
| 
 | ||||
|   // wait until begin of a new second to sync clock signal and absolute time
 | ||||
|   // wait until begin of a new second
 | ||||
|   t = tt = now(); | ||||
|   do { | ||||
|     tt = now(); | ||||
|   } while (t == tt); | ||||
| 
 | ||||
|   const TickType_t startOffset = xTaskGetTickCount(); | ||||
|   // take timestamp at moment of start of new second
 | ||||
|   const TickType_t shotTime = xTaskGetTickCount() - startTime - timeOffset; | ||||
| 
 | ||||
|   // task remains in blocked state until it is notified by isr
 | ||||
|   for (;;) { | ||||
| @ -155,13 +161,10 @@ void if482_loop(void *pvParameters) { | ||||
|         &wakeTime,      // receives moment of call from isr
 | ||||
|         portMAX_DELAY); // wait forever (missing error handling here...)
 | ||||
| 
 | ||||
|     t = myTZ.toLocal(now()); | ||||
|     wakeTime -= startOffset; | ||||
| 
 | ||||
|     // now we're synced to start of second t and wait
 | ||||
|     // until it's time to start transmit telegram for t+1
 | ||||
|     vTaskDelayUntil(&wakeTime, shotTime); | ||||
|     IF482.print(if482Telegram(t + 1)); | ||||
|     // now we're synced to start of second tt and wait
 | ||||
|     // until it's time to start transmit telegram for tt+1
 | ||||
|     vTaskDelayUntil(&wakeTime, shotTime); // sets waketime to moment of shot
 | ||||
|     IF482.print(if482Telegram(now() + 1)); | ||||
|   } | ||||
|   vTaskDelete(IF482Task); // shoud never be reached
 | ||||
| } // if482_loop()
 | ||||
|  | ||||
| @ -457,7 +457,7 @@ void user_request_network_time_callback(void *pVoidUserUTCTime, | ||||
|   setTime(*pUserUTCTime); | ||||
|   ESP_LOGI(TAG, "LoRaWAN network has set the system time"); | ||||
| #ifdef HAS_RTC | ||||
|   if (!set_rtctime(*pUserUTCTime)) | ||||
|   if (!set_rtctime(*pUserUTCTime)) // epoch time
 | ||||
|     ESP_LOGE(TAG, "RTC set time failure"); | ||||
| #endif | ||||
| } | ||||
| @ -429,7 +429,7 @@ void setup() { | ||||
|   else { | ||||
|     ESP_LOGI(TAG, "GPS has set the system time"); | ||||
| #ifdef HAS_RTC | ||||
|     if (!set_rtctime(now())) | ||||
|     if (!set_rtctime(now())) // epoch time
 | ||||
|       ESP_LOGE(TAG, "RTC set time failure"); | ||||
| #endif | ||||
|   } | ||||
|  | ||||
| @ -57,16 +57,19 @@ error: | ||||
| 
 | ||||
| } // rtc_init()
 | ||||
| 
 | ||||
| int set_rtctime(RtcDateTime t) { | ||||
| int set_rtctime(time_t t) { // t is epoch time starting 1.1.1970
 | ||||
|   if (I2C_MUTEX_LOCK()) { | ||||
|     Rtc.SetDateTime(t); | ||||
|     Rtc.SetDateTime(RtcDateTime(t)); | ||||
|     I2C_MUTEX_UNLOCK(); // release i2c bus access
 | ||||
|     return 1;           // success
 | ||||
|   } | ||||
|   return 0; // failure
 | ||||
| } // set_rtctime()
 | ||||
| 
 | ||||
| int set_rtctime(uint32_t t) { return set_rtctime(RtcDateTime(t)); }; | ||||
| int set_rtctime(uint32_t t) { // t is epoch seconds starting 1.1.1970
 | ||||
|   return set_rtctime(static_cast<time_t>(t)); | ||||
|   // set_rtctime()
 | ||||
| } | ||||
| 
 | ||||
| time_t get_rtctime(void) { | ||||
|   // never call now() in this function, this would cause a recursion!
 | ||||
|  | ||||
		Loading…
	
		Reference in New Issue
	
	Block a user