diff options
-rw-r--r-- | src/common/includes/Core/common.h | 6 | ||||
-rw-r--r-- | src/common/src/Core/common.c | 39 | ||||
-rw-r--r-- | src/common/src/Core/main.c | 56 | ||||
-rw-r--r-- | src/common/src/GPS/gps.c | 2 |
4 files changed, 91 insertions, 12 deletions
diff --git a/src/common/includes/Core/common.h b/src/common/includes/Core/common.h index 7e06975..5703cbd 100644 --- a/src/common/includes/Core/common.h +++ b/src/common/includes/Core/common.h @@ -48,6 +48,10 @@ uint64_t timestamp_now(void); // regardless of return value. int local_time(struct tm *time); +// Try to calculate local time, based on a past valid local time and the current timestamp +// Return 1 on success, 0 on failure +int local_derived_time(struct tm *time); + // Return either 0 or 1, somewhat randomly int random_bool(void); @@ -69,3 +73,5 @@ void hard_fault_handler_c(uint32_t *); // Round a value to the nearest 0.5 float round_float_to_half_steps(float value); + +#define GPS_MS_TIMEOUT 2000ul diff --git a/src/common/src/Core/common.c b/src/common/src/Core/common.c index 54067c1..8b0dddb 100644 --- a/src/common/src/Core/common.c +++ b/src/common/src/Core/common.c @@ -39,6 +39,11 @@ static uint16_t lfsr; static void common_increase_timestamp(TimerHandle_t t); +struct tm last_derived_time; +static uint64_t last_derived_time_timestamp = 0; +int last_derived_time_valid = 0; +int last_derived_time_delta_applied = 0; + #ifdef SIMULATOR long timestamp_delta = 0; @@ -141,11 +146,45 @@ int local_time(struct tm *time) { valid = 0; } + if (valid) { + last_derived_time = *time; + last_derived_time_timestamp = timestamp_now(); + last_derived_time_valid = 1; + last_derived_time_delta_applied = 0; + } + } return valid; } +int local_derived_time(struct tm *time) { + + if (last_derived_time_valid == 0) { + return 0; + } + + // As there is a GPS timeout, local_time will think he has valid GPS data for GPS_MS_TIMEOUT. We need to remove it for better calculations. + if (last_derived_time_delta_applied == 0) { + last_derived_time_delta_applied = 1; + last_derived_time_timestamp -= GPS_MS_TIMEOUT; + } + + uint64_t new_timestamp = timestamp_now(); + + while (new_timestamp - last_derived_time_timestamp > 1000) { + last_derived_time.tm_sec += 1; + last_derived_time_timestamp += 1000; + } + + mktime(&last_derived_time); + + *time = last_derived_time; + + return 1; + +} + void common_init(void) { diff --git a/src/common/src/Core/main.c b/src/common/src/Core/main.c index 9c27b2a..f80003c 100644 --- a/src/common/src/Core/main.c +++ b/src/common/src/Core/main.c @@ -298,6 +298,7 @@ static void gps_monit_task(void __attribute__ ((unused))*pvParameters) { while (1) { struct tm time; int time_valid = local_time(&time); + int derived_mode = 0; if (time_valid) { if (time.tm_sec % 4 >= 2) { @@ -309,37 +310,70 @@ static void gps_monit_task(void __attribute__ ((unused))*pvParameters) { // Even hours: tm_trigger=1, odd hours: tm_trigger=0 tm_trigger = (time.tm_hour + 1) % 2; + + derived_mode = 0; + + } else { + + time_valid = local_derived_time(&time); + + if (time_valid) { + if (time.tm_sec % 2) { + leds_turn_on(LED_BLUE); + } + else { + leds_turn_off(LED_BLUE); + } + + derived_mode = 1; + } + + } + + if (time_valid) { + // Even hours: tm_trigger=1, odd hours: tm_trigger=0 + tm_trigger = (time.tm_hour + 1) % 2; } gps_utctime(&gps_time); - if (gps_time.tm_sec % 30 == 0 && t_gps_print_latch == 0) { + if (time.tm_sec % 30 == 0 && t_gps_print_latch == 0) { usart_debug("T_GPS %04d-%02d-%02d %02d:%02d:%02d\r\n", - gps_time.tm_year, gps_time.tm_mon, gps_time.tm_mday, - gps_time.tm_hour, gps_time.tm_min, gps_time.tm_sec); + gps_time.tm_year, gps_time.tm_mon, gps_time.tm_mday, + gps_time.tm_hour, gps_time.tm_min, gps_time.tm_sec); + + char * mode; - usart_debug("TIME %04d-%02d-%02d %02d:%02d:%02d\r\n", + if (derived_mode) { + mode = "Derived"; + } else { + mode = "GPS"; + } + + usart_debug("TIME %04d-%02d-%02d %02d:%02d:%02d [%s]\r\n", #ifdef SIMULATOR - time.tm_year + 1900, + time.tm_year + 1900, #else - time.tm_year, + time.tm_year, #endif - time.tm_mon, time.tm_mday, - time.tm_hour, time.tm_min, time.tm_sec); + time.tm_mon, time.tm_mday, + time.tm_hour, time.tm_min, time.tm_sec, + mode); t_gps_print_latch = 1; } - if (gps_time.tm_sec % 30 > 0) { + + if (time.tm_sec % 30 > 0) { t_gps_print_latch = 0; } - if (time_valid && gps_time.tm_sec == 0 && gps_time.tm_min == 0 && t_gps_hours_handeled == 0) { + if (time_valid && derived_mode == 0 && gps_time.tm_sec == 0 && gps_time.tm_min == 0 && t_gps_hours_handeled == 0) { uint64_t current_timestamp = timestamp_now(); if (last_hour_timestamp == 0) { - usart_debug("DERIV INIT TS=%i\r\n", current_timestamp); + usart_debug("DERIV INIT TS=%lld\r\n", current_timestamp); } else { usart_debug("DERIV TS=%lld Excepted=%lld Delta=%lld\r\n", diff --git a/src/common/src/GPS/gps.c b/src/common/src/GPS/gps.c index a1fb043..3ac2c52 100644 --- a/src/common/src/GPS/gps.c +++ b/src/common/src/GPS/gps.c @@ -36,7 +36,7 @@ TickType_t gps_timeutc_last_updated = 0; static struct tm gps_timeutc; static int gps_timeutc_valid; -const TickType_t gps_data_validity_timeout = 10000ul / portTICK_PERIOD_MS; +const TickType_t gps_data_validity_timeout = GPS_MS_TIMEOUT / portTICK_PERIOD_MS; static void gps_task(void *pvParameters); |