Skip to content

Commit 700f8fb

Browse files
committed
Finalize time sync when event is written
1 parent 5dcc6f5 commit 700f8fb

File tree

1 file changed

+41
-26
lines changed

1 file changed

+41
-26
lines changed

main.c

Lines changed: 41 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -129,8 +129,9 @@ void i2c_oled_comm_handle(uint8_t hdl_address, uint8_t *hdl_buffer, size_t hdl_b
129129
volatile bool update_rtc = false; // Set to true to trigger I2C communication with RTC module
130130
volatile bool rtc_time_has_sync = false; // Set to true when the RTC has been set by GPS or Mobile app
131131
static struct tm * tmTime;
132-
static time_t currentTime;
133-
static time_t lastSyncTime;
132+
static time_t currentTime; // Current time of the Robogotchi
133+
static time_t timeSyncTime; // New time from GPS to be set during timeSync event
134+
static time_t fileSystemSyncTime; // The last time the filesystem performed a sync to QSPI
134135
static char datetimestring[ 64 ] = { 0 };
135136
volatile bool log_file_active = false;
136137
static volatile bool write_logdata_now = false;
@@ -1622,9 +1623,9 @@ void process_packet_vesc(unsigned char *data, unsigned int len) {
16221623
}
16231624

16241625
// Sync filesystem contents every 60 seconds
1625-
if (log_file_active && currentTime % 60 == 0 && currentTime != lastSyncTime)
1626+
if (log_file_active && currentTime % 60 == 0 && currentTime != fileSystemSyncTime)
16261627
{
1627-
lastSyncTime = currentTime;
1628+
fileSystemSyncTime = currentTime;
16281629
int sync_result = lfs_file_sync(&lfs, &file);
16291630
NRF_LOG_INFO("Sync result: %d", sync_result);
16301631
NRF_LOG_FLUSH();
@@ -1696,6 +1697,27 @@ static void packet_timer_handler(void *p_context) {
16961697
//TODO: This is old code. Also not SD aware. Remove? CRITICAL_REGION_EXIT();
16971698
}
16981699

1700+
static void finalize_time_sync()
1701+
{
1702+
// Update time in memory
1703+
memcpy(tmTime, &gpsTime, sizeof(struct tm));
1704+
currentTime = timeSyncTime;
1705+
1706+
// Update time on RTC
1707+
update_rtc = true;
1708+
1709+
// Update internal state
1710+
log_message_gps.dt = 0; // Ensure non-delta is written next
1711+
log_message_esc.dt = 0; // Ensure non-delta is written next
1712+
time_esc_last_responded = currentTime;
1713+
time_gps_last_responded = currentTime;
1714+
lastTimeBoardMoved = currentTime;
1715+
strftime(datetimestring, 64, "%Y-%m-%dT%H:%M:%S", tmTime);
1716+
1717+
NRF_LOG_INFO("Time set from GPS %ld", currentTime);
1718+
NRF_LOG_FLUSH();
1719+
}
1720+
16991721
static void logging_timer_handler(void *p_context) {
17001722
(void)p_context;
17011723
static char gps_status[11] = {0};
@@ -1788,7 +1810,6 @@ static void logging_timer_handler(void *p_context) {
17881810
//NRF_LOG_FLUSH();
17891811

17901812
// Convert the time from the GPS
1791-
time_t newTimeSeconds;
17921813
struct tm gpsTime;
17931814
gpsTime.tm_year = 2000 + hgps.year - 1900;
17941815
gpsTime.tm_mon = hgps.month - 1;
@@ -1797,38 +1818,24 @@ static void logging_timer_handler(void *p_context) {
17971818
gpsTime.tm_min = hgps.minutes;
17981819
gpsTime.tm_sec = hgps.seconds;
17991820
// Give it to me in time_t
1800-
newTimeSeconds = mktime(&gpsTime);
1821+
timeSyncTime = mktime(&gpsTime);
18011822

18021823
//strftime(datetimestring, 64, "%Y-%m-%dT%H:%M:%S", tmTime);
1803-
//NRF_LOG_INFO("Setting time from GPS; time now %s or %ld", datetimestring, newTimeSeconds);
1824+
//NRF_LOG_INFO("Setting time from GPS; time now %s or %ld", datetimestring, timeSyncTime);
18041825
//NRF_LOG_FLUSH();
18051826

18061827
// Store time sync difference in seconds if we are currently logging
18071828
if (log_file_active)
18081829
{
18091830
log_message_freesk8.event_type = TIME_SYNC;
1810-
log_message_freesk8.event_data = newTimeSeconds - currentTime;
1831+
log_message_freesk8.event_data = timeSyncTime - currentTime;
18111832

18121833
write_time_sync_now = true;
18131834
}
1814-
1815-
// Update time in memory
1816-
memcpy(tmTime, &gpsTime, sizeof(struct tm));
1817-
currentTime = newTimeSeconds;
1818-
1819-
// Update time on RTC
1820-
update_rtc = true;
1821-
1822-
// Update internal state
1823-
log_message_gps.dt = 0; // Ensure non-delta is written next
1824-
log_message_esc.dt = 0; // Ensure non-delta is written next
1825-
time_esc_last_responded = currentTime;
1826-
time_gps_last_responded = currentTime;
1827-
lastTimeBoardMoved = currentTime;
1828-
strftime(datetimestring, 64, "%Y-%m-%dT%H:%M:%S", tmTime);
1829-
1830-
NRF_LOG_INFO("Time set from GPS %ld", currentTime);
1831-
NRF_LOG_FLUSH();
1835+
else
1836+
{
1837+
finalize_time_sync();
1838+
}
18321839
}
18331840

18341841
// Check if the ESC has not responded in 1 second and try swapping the UART pin configuration
@@ -2555,19 +2562,23 @@ int main(void) {
25552562
// Monitor button press; Do not block for more than 25ms
25562563
process_user_input();
25572564

2565+
// Reset UART and packet on error
2566+
//TODO: m_uart_error is never set to true
25582567
if (m_uart_error) {
25592568
app_uart_close();
25602569
uart_init();
25612570
packet_reset(PACKET_VESC);
25622571
m_uart_error = false;
25632572
}
25642573

2574+
// Read ESC data; Potentially writing log data
25652575
uint8_t byte;
25662576
while (app_uart_get(&byte) == NRF_SUCCESS) {
25672577
time_esc_last_responded = currentTime;
25682578
packet_process_byte(byte, PACKET_VESC);
25692579
}
25702580

2581+
// Check log file writing flags from 1Hz timer
25712582
if (write_gps_now)
25722583
{
25732584
write_gps_now = false;
@@ -2606,13 +2617,17 @@ int main(void) {
26062617
bytes_written += lfs_file_write(&lfs, &file, &end, sizeof(end));
26072618
NRF_LOG_INFO("TIME_SYNC Bytes Written: %ld", bytes_written);
26082619
NRF_LOG_FLUSH();
2620+
2621+
finalize_time_sync();
26092622
}
26102623

2624+
// Update GPS data
26112625
while (gps_uart_get(&byte) == NRF_SUCCESS) {
26122626
time_gps_last_responded = currentTime;
26132627
lwgps_process(&hgps, &byte, sizeof(byte));
26142628
}
26152629

2630+
// Update RTC
26162631
if (update_rtc) {
26172632
update_rtc = false;
26182633
rtc_set_time( tmTime->tm_year + 1900, tmTime->tm_mon + 1, tmTime->tm_mday, tmTime->tm_hour, tmTime->tm_min, tmTime->tm_sec );

0 commit comments

Comments
 (0)