@@ -129,8 +129,9 @@ void i2c_oled_comm_handle(uint8_t hdl_address, uint8_t *hdl_buffer, size_t hdl_b
129
129
volatile bool update_rtc = false; // Set to true to trigger I2C communication with RTC module
130
130
volatile bool rtc_time_has_sync = false; // Set to true when the RTC has been set by GPS or Mobile app
131
131
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
134
135
static char datetimestring [ 64 ] = { 0 };
135
136
volatile bool log_file_active = false;
136
137
static volatile bool write_logdata_now = false;
@@ -1622,9 +1623,9 @@ void process_packet_vesc(unsigned char *data, unsigned int len) {
1622
1623
}
1623
1624
1624
1625
// 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 )
1626
1627
{
1627
- lastSyncTime = currentTime ;
1628
+ fileSystemSyncTime = currentTime ;
1628
1629
int sync_result = lfs_file_sync (& lfs , & file );
1629
1630
NRF_LOG_INFO ("Sync result: %d" , sync_result );
1630
1631
NRF_LOG_FLUSH ();
@@ -1696,6 +1697,27 @@ static void packet_timer_handler(void *p_context) {
1696
1697
//TODO: This is old code. Also not SD aware. Remove? CRITICAL_REGION_EXIT();
1697
1698
}
1698
1699
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
+
1699
1721
static void logging_timer_handler (void * p_context ) {
1700
1722
(void )p_context ;
1701
1723
static char gps_status [11 ] = {0 };
@@ -1788,7 +1810,6 @@ static void logging_timer_handler(void *p_context) {
1788
1810
//NRF_LOG_FLUSH();
1789
1811
1790
1812
// Convert the time from the GPS
1791
- time_t newTimeSeconds ;
1792
1813
struct tm gpsTime ;
1793
1814
gpsTime .tm_year = 2000 + hgps .year - 1900 ;
1794
1815
gpsTime .tm_mon = hgps .month - 1 ;
@@ -1797,38 +1818,24 @@ static void logging_timer_handler(void *p_context) {
1797
1818
gpsTime .tm_min = hgps .minutes ;
1798
1819
gpsTime .tm_sec = hgps .seconds ;
1799
1820
// Give it to me in time_t
1800
- newTimeSeconds = mktime (& gpsTime );
1821
+ timeSyncTime = mktime (& gpsTime );
1801
1822
1802
1823
//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 );
1804
1825
//NRF_LOG_FLUSH();
1805
1826
1806
1827
// Store time sync difference in seconds if we are currently logging
1807
1828
if (log_file_active )
1808
1829
{
1809
1830
log_message_freesk8 .event_type = TIME_SYNC ;
1810
- log_message_freesk8 .event_data = newTimeSeconds - currentTime ;
1831
+ log_message_freesk8 .event_data = timeSyncTime - currentTime ;
1811
1832
1812
1833
write_time_sync_now = true;
1813
1834
}
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
+ }
1832
1839
}
1833
1840
1834
1841
// Check if the ESC has not responded in 1 second and try swapping the UART pin configuration
@@ -2555,19 +2562,23 @@ int main(void) {
2555
2562
// Monitor button press; Do not block for more than 25ms
2556
2563
process_user_input ();
2557
2564
2565
+ // Reset UART and packet on error
2566
+ //TODO: m_uart_error is never set to true
2558
2567
if (m_uart_error ) {
2559
2568
app_uart_close ();
2560
2569
uart_init ();
2561
2570
packet_reset (PACKET_VESC );
2562
2571
m_uart_error = false;
2563
2572
}
2564
2573
2574
+ // Read ESC data; Potentially writing log data
2565
2575
uint8_t byte ;
2566
2576
while (app_uart_get (& byte ) == NRF_SUCCESS ) {
2567
2577
time_esc_last_responded = currentTime ;
2568
2578
packet_process_byte (byte , PACKET_VESC );
2569
2579
}
2570
2580
2581
+ // Check log file writing flags from 1Hz timer
2571
2582
if (write_gps_now )
2572
2583
{
2573
2584
write_gps_now = false;
@@ -2606,13 +2617,17 @@ int main(void) {
2606
2617
bytes_written += lfs_file_write (& lfs , & file , & end , sizeof (end ));
2607
2618
NRF_LOG_INFO ("TIME_SYNC Bytes Written: %ld" , bytes_written );
2608
2619
NRF_LOG_FLUSH ();
2620
+
2621
+ finalize_time_sync ();
2609
2622
}
2610
2623
2624
+ // Update GPS data
2611
2625
while (gps_uart_get (& byte ) == NRF_SUCCESS ) {
2612
2626
time_gps_last_responded = currentTime ;
2613
2627
lwgps_process (& hgps , & byte , sizeof (byte ));
2614
2628
}
2615
2629
2630
+ // Update RTC
2616
2631
if (update_rtc ) {
2617
2632
update_rtc = false;
2618
2633
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