@@ -123,19 +123,23 @@ void i2c_oled_comm_handle(uint8_t hdl_address, uint8_t *hdl_buffer, size_t hdl_b
123
123
#endif
124
124
125
125
////////////////////////////////////////
126
- // Time tracking
126
+ // Time/Event tracking
127
127
////////////////////////////////////////
128
128
#include "rtc.h"
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
132
static time_t currentTime ;
133
+ static time_t lastSyncTime ;
133
134
static char datetimestring [ 64 ] = { 0 };
134
135
volatile bool log_file_active = false;
135
136
static volatile bool write_logdata_now = false;
136
137
static volatile bool gps_signal_locked = false;
137
138
static time_t time_esc_last_responded ; // For triggering TX and RX pin swapping
138
139
static time_t time_gps_last_responded ; // For detecting stale data
140
+ volatile bool write_gps_now = false;
141
+ volatile bool write_gps_delta_now = false;
142
+ volatile bool write_time_sync_now = false;
139
143
140
144
void update_time (int syear , int smonth , int sday , int shour , int sminute , int ssecond )
141
145
{
@@ -412,10 +416,10 @@ int qspi_sync(const struct lfs_config *c)
412
416
uint16_t lfs_file_count = 0 ;
413
417
static lfs_t lfs ;
414
418
static lfs_file_t file ;
415
- static uint8_t lfs_read_buf [256 ]; // Must be cache_size
416
- static uint8_t lfs_prog_buf [256 ]; // Must be cache_size
417
- static uint8_t lfs_lookahead_buf [16 ]; // 128/8=16
418
- static uint8_t lfs_file_buf [256 ]; // Must be cache size
419
+ static uint8_t lfs_read_buf [4096 ]; // Must be cache_size
420
+ static uint8_t lfs_prog_buf [4096 ]; // Must be cache_size
421
+ static uint8_t lfs_lookahead_buf [4096 ]; // 128/8=16
422
+ static uint8_t lfs_file_buf [4096 ]; // Must be cache size
419
423
static struct lfs_file_config lfs_file_config ;
420
424
421
425
// configuration of the filesystem is provided by this struct
@@ -431,8 +435,8 @@ const struct lfs_config cfg = {
431
435
.prog_size = 4 ,
432
436
.block_size = 4096 ,
433
437
.block_count = 8192 , //4096 bytes/block @ 256Mbit (33554432 bytes) = 8192 blocks
434
- .cache_size = 256 ,
435
- .lookahead_size = 16 ,
438
+ .cache_size = 4096 ,
439
+ .lookahead_size = 4096 ,
436
440
.block_cycles = 500 ,
437
441
438
442
.read_buffer = lfs_read_buf ,
@@ -807,9 +811,11 @@ static void pm_evt_handler(pm_evt_t const * p_evt)
807
811
p_evt -> params .conn_sec_succeeded .procedure );
808
812
is_connection_secure = true;
809
813
melody_play (MELODY_BLE_SUCCESS , true); // Play BLE Success sound
814
+ #if HAS_DISPLAY
810
815
// Notify user connection successful
811
816
Adafruit_GFX_print ("BLE OK" , 64 , 0 );
812
817
update_display = true;
818
+ #endif
813
819
}
814
820
else
815
821
{
@@ -1071,6 +1077,11 @@ static void ble_evt_handler(ble_evt_t const * p_ble_evt, void * p_context) {
1071
1077
case BLE_GAP_EVT_DISCONNECTED :
1072
1078
nrf_gpio_pin_clear (LED_PIN );
1073
1079
is_connection_secure = false;
1080
+ #if HAS_DISPLAY
1081
+ // Notify user connection successful
1082
+ Adafruit_GFX_print (" " , 64 , 0 );
1083
+ update_display = true;
1084
+ #endif
1074
1085
NRF_LOG_INFO ("Disconnected" );
1075
1086
m_conn_handle = BLE_CONN_HANDLE_INVALID ;
1076
1087
// Check if the last connected peer had not used MITM, if so, delete its bond information.
@@ -1238,6 +1249,18 @@ enum {
1238
1249
#define PIN_STATES 3
1239
1250
static void uart_swap_pins (void ) {
1240
1251
static int pin_swap_state = PIN_STATE_DEFAULT ;
1252
+
1253
+ app_uart_flush ();
1254
+ packet_reset (PACKET_VESC );
1255
+
1256
+ m_uart_comm_params .rx_pin_no = UART_RX ;
1257
+ m_uart_comm_params .tx_pin_no = UART_TX ;
1258
+ m_uart_comm_params .rts_pin_no = 0 ;
1259
+ m_uart_comm_params .cts_pin_no = 0 ;
1260
+ m_uart_comm_params .flow_control = APP_UART_FLOW_CONTROL_DISABLED ;
1261
+ m_uart_comm_params .use_parity = false;
1262
+ m_uart_comm_params .baud_rate = NRF_UARTE_BAUDRATE_115200 ;
1263
+
1241
1264
app_uart_close ();
1242
1265
1243
1266
if (++ pin_swap_state == PIN_STATES ) {
@@ -1347,9 +1370,9 @@ static void process_packet_ble(unsigned char *data, unsigned int len) {
1347
1370
return ;
1348
1371
}
1349
1372
1350
- CRITICAL_REGION_ENTER ();
1373
+ //TODO: This is old code. Also not SD aware. Remove? CRITICAL_REGION_ENTER();
1351
1374
packet_send_packet (data , len , PACKET_VESC );
1352
- CRITICAL_REGION_EXIT ();
1375
+ //TODO: This is old code. Also not SD aware. Remove? CRITICAL_REGION_EXIT();
1353
1376
}
1354
1377
1355
1378
void update_status_packet (char * buffer );
@@ -1597,6 +1620,35 @@ void process_packet_vesc(unsigned char *data, unsigned int len) {
1597
1620
packet_send_packet (data , len , PACKET_BLE );
1598
1621
}
1599
1622
}
1623
+
1624
+ // Sync filesystem contents every 60 seconds
1625
+ if (log_file_active && currentTime % 60 == 0 && currentTime != lastSyncTime )
1626
+ {
1627
+ lastSyncTime = currentTime ;
1628
+ int sync_result = lfs_file_sync (& lfs , & file );
1629
+ NRF_LOG_INFO ("Sync result: %d" , sync_result );
1630
+ NRF_LOG_FLUSH ();
1631
+ if (sync_result < 0 )
1632
+ {
1633
+ APP_ERROR_CHECK (sync_result );
1634
+ }
1635
+
1636
+ // Check for free space after committing data to storage
1637
+ if (lfs_free_space_check () < 10 ) //TODO: define percentage free limit
1638
+ {
1639
+ // Check if user wants to erase old files automatically
1640
+ if (gotchi_cfg_user .log_auto_erase_when_full == 1 )
1641
+ {
1642
+ //TODO: call method to erase oldest file until free space is happy
1643
+ log_file_stop ();
1644
+ }
1645
+ else
1646
+ {
1647
+ // Stop logging to prevent corruption
1648
+ log_file_stop ();
1649
+ }
1650
+ }
1651
+ }
1600
1652
}
1601
1653
1602
1654
void ble_printf (const char * format , ...) {
@@ -1637,11 +1689,11 @@ static void packet_timer_handler(void *p_context) {
1637
1689
(void )p_context ;
1638
1690
packet_timerfunc ();
1639
1691
1640
- CRITICAL_REGION_ENTER ();
1692
+ //TODO: This is old code. Also not SD aware. Remove? CRITICAL_REGION_ENTER();
1641
1693
if (m_other_comm_disable_time > 0 ) {
1642
1694
m_other_comm_disable_time -- ;
1643
1695
}
1644
- CRITICAL_REGION_EXIT ();
1696
+ //TODO: This is old code. Also not SD aware. Remove? CRITICAL_REGION_EXIT();
1645
1697
}
1646
1698
1647
1699
static void logging_timer_handler (void * p_context ) {
@@ -1690,15 +1742,7 @@ static void logging_timer_handler(void *p_context) {
1690
1742
log_message_gps .latitude = hgps .latitude * 100000 ;
1691
1743
log_message_gps .longitude = hgps .longitude * 100000 ;
1692
1744
1693
- // Write out full GPS message
1694
- size_t bytes_written = 0 ;
1695
- char start [3 ] = {PACKET_START , GPS , sizeof (log_message_gps )};
1696
- char end [1 ] = {PACKET_END };
1697
- bytes_written += lfs_file_write (& lfs , & file , & start , sizeof (start ));
1698
- bytes_written += lfs_file_write (& lfs , & file , & log_message_gps , sizeof (log_message_gps ));
1699
- bytes_written += lfs_file_write (& lfs , & file , & end , sizeof (end ));
1700
- //NRF_LOG_INFO("GPS Bytes Written: %ld", bytes_written);
1701
- //NRF_LOG_FLUSH();
1745
+ write_gps_now = true;
1702
1746
}
1703
1747
// We can write a GPS Delta message!
1704
1748
else
@@ -1720,15 +1764,7 @@ static void logging_timer_handler(void *p_context) {
1720
1764
log_message_gps .latitude = hgps .latitude * 100000 ;
1721
1765
log_message_gps .longitude = hgps .longitude * 100000 ;
1722
1766
1723
- // Write out GPS DELTA message
1724
- size_t bytes_written = 0 ;
1725
- char start [3 ] = {PACKET_START , GPS_DELTA , sizeof (log_message_gps_delta )};
1726
- char end [1 ] = {PACKET_END };
1727
- bytes_written += lfs_file_write (& lfs , & file , & start , sizeof (start ));
1728
- bytes_written += lfs_file_write (& lfs , & file , & log_message_gps_delta , sizeof (log_message_gps_delta ));
1729
- bytes_written += lfs_file_write (& lfs , & file , & end , sizeof (end ));
1730
- //NRF_LOG_INFO("GPS DELTA Bytes Written: %ld", bytes_written);
1731
- //NRF_LOG_FLUSH();
1767
+ write_gps_delta_now = true;
1732
1768
}
1733
1769
}
1734
1770
@@ -1744,29 +1780,7 @@ static void logging_timer_handler(void *p_context) {
1744
1780
melody_play (MELODY_GPS_LOCK , false); // Play GPS locked melody, do not interrupt
1745
1781
}
1746
1782
1747
- // Sync filesystem contents every 60 seconds
1748
- if (log_file_active && currentTime % 60 == 0 )
1749
- {
1750
- lfs_file_sync (& lfs , & file );
1751
-
1752
- // Check for free space after committing data to storage
1753
- if (lfs_free_space_check () < 10 ) //TODO: define percentage free limit
1754
- {
1755
- // Check if user wants to erase old files automatically
1756
- if (gotchi_cfg_user .log_auto_erase_when_full == 1 )
1757
- {
1758
- //TODO: call method to erase oldest file until free space is happy
1759
- log_file_stop ();
1760
- }
1761
- else
1762
- {
1763
- // Stop logging to prevent corruption
1764
- log_file_stop ();
1765
- }
1766
- }
1767
- }
1768
-
1769
- //TODO: Sync GPS time - this works but is UTC and we've made no commitment to timezones
1783
+ //Sync GPS time - Robogotchi <3 UTC time
1770
1784
if (!rtc_time_has_sync && gps_signal_locked && hgps .seconds != 247 && hgps .seconds != 0 )
1771
1785
{
1772
1786
//strftime(datetimestring, 64, "%Y-%m-%dT%H:%M:%S", tmTime);
@@ -1795,15 +1809,7 @@ static void logging_timer_handler(void *p_context) {
1795
1809
log_message_freesk8 .event_type = TIME_SYNC ;
1796
1810
log_message_freesk8 .event_data = newTimeSeconds - currentTime ;
1797
1811
1798
- // Write out FREESK8 TIME_SYNC event
1799
- size_t bytes_written = 0 ;
1800
- char start [3 ] = {PACKET_START , FREESK8 , sizeof (log_message_freesk8 )};
1801
- char end [1 ] = {PACKET_END };
1802
- bytes_written += lfs_file_write (& lfs , & file , & start , sizeof (start ));
1803
- bytes_written += lfs_file_write (& lfs , & file , & log_message_freesk8 , sizeof (log_message_freesk8 ));
1804
- bytes_written += lfs_file_write (& lfs , & file , & end , sizeof (end ));
1805
- NRF_LOG_INFO ("TIME_SYNC Bytes Written: %ld" , bytes_written );
1806
- NRF_LOG_FLUSH ();
1812
+ write_time_sync_now = true;
1807
1813
}
1808
1814
1809
1815
// Update time in memory
@@ -1979,7 +1985,7 @@ static ret_code_t twi_master_init(void)
1979
1985
#define QSPI_STD_CMD_WRSR 0x01
1980
1986
#define QSPI_STD_CMD_RSTEN 0x66
1981
1987
#define QSPI_STD_CMD_RST 0x99
1982
-
1988
+ #define QSPI_CMD_EN4B 0xB7 // Enable 32-bit addressing
1983
1989
static void configure_memory ()
1984
1990
{
1985
1991
uint8_t temporary = 0x40 ;
@@ -2008,6 +2014,12 @@ static void configure_memory()
2008
2014
err_code = nrf_drv_qspi_cinstr_xfer (& cinstr_cfg , & temporary , NULL );
2009
2015
APP_ERROR_CHECK (err_code );
2010
2016
2017
+ // Enter 4-Byte mode (32-bit)
2018
+ cinstr_cfg .opcode = QSPI_CMD_EN4B ;
2019
+ cinstr_cfg .length = NRF_QSPI_CINSTR_LEN_1B ;
2020
+ err_code = nrf_drv_qspi_cinstr_xfer (& cinstr_cfg , NULL , NULL );
2021
+ APP_ERROR_CHECK (err_code );
2022
+
2011
2023
NRF_LOG_INFO ("QSPI Memory Configured" );
2012
2024
NRF_LOG_FLUSH ();
2013
2025
}
@@ -2187,12 +2199,17 @@ void littlefs_init()
2187
2199
NRF_LOG_INFO ("LittleFS initializing" );
2188
2200
NRF_LOG_FLUSH ();
2189
2201
2202
+ // Prepare the static file buffer
2203
+ memset (& lfs_file_config , 0 , sizeof (struct lfs_file_config ));
2204
+ lfs_file_config .buffer = lfs_file_buf ;
2205
+ lfs_file_config .attr_count = 0 ;
2206
+
2190
2207
// mount the filesystem
2191
2208
int err = lfs_mount (& lfs , & cfg );
2192
2209
2193
2210
// reformat if we can't mount the filesystem
2194
2211
// this should only happen on the first boot
2195
- if (err ) {
2212
+ if (err < 0 ) {
2196
2213
NRF_LOG_WARNING ("LittleFS needs to format the storage" );
2197
2214
NRF_LOG_FLUSH ();
2198
2215
int lfs_format_response = lfs_format (& lfs , & cfg );
@@ -2205,14 +2222,9 @@ void littlefs_init()
2205
2222
#endif
2206
2223
melody_play (MELODY_GOTCHI_FAULT , true); // Play robogotchi fault, interrupt
2207
2224
}
2208
- NRF_LOG_INFO ("LittleFS initialized" );
2225
+ NRF_LOG_INFO ("LittleFS initialized. Result: (%d)" , err );
2209
2226
NRF_LOG_FLUSH ();
2210
2227
2211
- // Prepare the static file buffer
2212
- memset (& lfs_file_config , 0 , sizeof (struct lfs_file_config ));
2213
- lfs_file_config .buffer = lfs_file_buf ;
2214
- lfs_file_config .attr_count = 0 ;
2215
-
2216
2228
// read current count
2217
2229
lfs_file_opencfg (& lfs , & file , "boot_count" , LFS_O_RDWR | LFS_O_CREAT , & lfs_file_config );
2218
2230
lfs_file_read (& lfs , & file , & boot_count , sizeof (boot_count ));
@@ -2544,6 +2556,46 @@ int main(void) {
2544
2556
packet_process_byte (byte , PACKET_VESC );
2545
2557
}
2546
2558
2559
+ if (write_gps_now )
2560
+ {
2561
+ write_gps_now = false;
2562
+ // Write out full GPS message
2563
+ size_t bytes_written = 0 ;
2564
+ char start [3 ] = {PACKET_START , GPS , sizeof (log_message_gps )};
2565
+ char end [1 ] = {PACKET_END };
2566
+ bytes_written += lfs_file_write (& lfs , & file , & start , sizeof (start ));
2567
+ bytes_written += lfs_file_write (& lfs , & file , & log_message_gps , sizeof (log_message_gps ));
2568
+ bytes_written += lfs_file_write (& lfs , & file , & end , sizeof (end ));
2569
+ NRF_LOG_INFO ("GPS Bytes Written: %ld" , bytes_written );
2570
+ NRF_LOG_FLUSH ();
2571
+ }
2572
+ if (write_gps_delta_now )
2573
+ {
2574
+ write_gps_delta_now = false;
2575
+ // Write out GPS DELTA message
2576
+ size_t bytes_written = 0 ;
2577
+ char start [3 ] = {PACKET_START , GPS_DELTA , sizeof (log_message_gps_delta )};
2578
+ char end [1 ] = {PACKET_END };
2579
+ bytes_written += lfs_file_write (& lfs , & file , & start , sizeof (start ));
2580
+ bytes_written += lfs_file_write (& lfs , & file , & log_message_gps_delta , sizeof (log_message_gps_delta ));
2581
+ bytes_written += lfs_file_write (& lfs , & file , & end , sizeof (end ));
2582
+ NRF_LOG_INFO ("GPS DELTA Bytes Written: %ld" , bytes_written );
2583
+ NRF_LOG_FLUSH ();
2584
+ }
2585
+ if (write_time_sync_now )
2586
+ {
2587
+ write_time_sync_now = false;
2588
+ // Write out FREESK8 TIME_SYNC event
2589
+ size_t bytes_written = 0 ;
2590
+ char start [3 ] = {PACKET_START , FREESK8 , sizeof (log_message_freesk8 )};
2591
+ char end [1 ] = {PACKET_END };
2592
+ bytes_written += lfs_file_write (& lfs , & file , & start , sizeof (start ));
2593
+ bytes_written += lfs_file_write (& lfs , & file , & log_message_freesk8 , sizeof (log_message_freesk8 ));
2594
+ bytes_written += lfs_file_write (& lfs , & file , & end , sizeof (end ));
2595
+ NRF_LOG_INFO ("TIME_SYNC Bytes Written: %ld" , bytes_written );
2596
+ NRF_LOG_FLUSH ();
2597
+ }
2598
+
2547
2599
while (gps_uart_get (& byte ) == NRF_SUCCESS ) {
2548
2600
time_gps_last_responded = currentTime ;
2549
2601
lwgps_process (& hgps , & byte , sizeof (byte ));
0 commit comments