Skip to content

Commit c176fa9

Browse files
committed
Fixes and improvements from 50Hz testing
1 parent 4e51051 commit c176fa9

File tree

2 files changed

+122
-70
lines changed

2 files changed

+122
-70
lines changed

command_interface.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ extern volatile bool update_rtc;
2929
extern volatile bool log_file_active;
3030

3131
static lfs_file_t file_command_interface;
32-
static uint8_t lfs_file_buf[256]; // Must be cache size
32+
static uint8_t lfs_file_buf[4096]; // Must be cache size
3333
static struct lfs_file_config lfs_file_config;
3434
static lfs_dir_t directory;
3535
static int32_t bytes_sent = -1; //file.ctz.size;

main.c

Lines changed: 121 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -123,19 +123,23 @@ void i2c_oled_comm_handle(uint8_t hdl_address, uint8_t *hdl_buffer, size_t hdl_b
123123
#endif
124124

125125
////////////////////////////////////////
126-
// Time tracking
126+
// Time/Event tracking
127127
////////////////////////////////////////
128128
#include "rtc.h"
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;
132132
static time_t currentTime;
133+
static time_t lastSyncTime;
133134
static char datetimestring[ 64 ] = { 0 };
134135
volatile bool log_file_active = false;
135136
static volatile bool write_logdata_now = false;
136137
static volatile bool gps_signal_locked = false;
137138
static time_t time_esc_last_responded; // For triggering TX and RX pin swapping
138139
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;
139143

140144
void update_time(int syear, int smonth, int sday, int shour, int sminute, int ssecond)
141145
{
@@ -412,10 +416,10 @@ int qspi_sync(const struct lfs_config *c)
412416
uint16_t lfs_file_count = 0;
413417
static lfs_t lfs;
414418
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
419423
static struct lfs_file_config lfs_file_config;
420424

421425
// configuration of the filesystem is provided by this struct
@@ -431,8 +435,8 @@ const struct lfs_config cfg = {
431435
.prog_size = 4,
432436
.block_size = 4096,
433437
.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,
436440
.block_cycles = 500,
437441

438442
.read_buffer = lfs_read_buf,
@@ -807,9 +811,11 @@ static void pm_evt_handler(pm_evt_t const * p_evt)
807811
p_evt->params.conn_sec_succeeded.procedure);
808812
is_connection_secure = true;
809813
melody_play(MELODY_BLE_SUCCESS, true); // Play BLE Success sound
814+
#if HAS_DISPLAY
810815
// Notify user connection successful
811816
Adafruit_GFX_print("BLE OK", 64, 0);
812817
update_display = true;
818+
#endif
813819
}
814820
else
815821
{
@@ -1071,6 +1077,11 @@ static void ble_evt_handler(ble_evt_t const * p_ble_evt, void * p_context) {
10711077
case BLE_GAP_EVT_DISCONNECTED:
10721078
nrf_gpio_pin_clear(LED_PIN);
10731079
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
10741085
NRF_LOG_INFO("Disconnected");
10751086
m_conn_handle = BLE_CONN_HANDLE_INVALID;
10761087
// Check if the last connected peer had not used MITM, if so, delete its bond information.
@@ -1238,6 +1249,18 @@ enum {
12381249
#define PIN_STATES 3
12391250
static void uart_swap_pins(void) {
12401251
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+
12411264
app_uart_close();
12421265

12431266
if (++pin_swap_state == PIN_STATES) {
@@ -1347,9 +1370,9 @@ static void process_packet_ble(unsigned char *data, unsigned int len) {
13471370
return;
13481371
}
13491372

1350-
CRITICAL_REGION_ENTER();
1373+
//TODO: This is old code. Also not SD aware. Remove? CRITICAL_REGION_ENTER();
13511374
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();
13531376
}
13541377

13551378
void update_status_packet(char * buffer);
@@ -1597,6 +1620,35 @@ void process_packet_vesc(unsigned char *data, unsigned int len) {
15971620
packet_send_packet(data, len, PACKET_BLE);
15981621
}
15991622
}
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+
}
16001652
}
16011653

16021654
void ble_printf(const char* format, ...) {
@@ -1637,11 +1689,11 @@ static void packet_timer_handler(void *p_context) {
16371689
(void)p_context;
16381690
packet_timerfunc();
16391691

1640-
CRITICAL_REGION_ENTER();
1692+
//TODO: This is old code. Also not SD aware. Remove? CRITICAL_REGION_ENTER();
16411693
if (m_other_comm_disable_time > 0) {
16421694
m_other_comm_disable_time--;
16431695
}
1644-
CRITICAL_REGION_EXIT();
1696+
//TODO: This is old code. Also not SD aware. Remove? CRITICAL_REGION_EXIT();
16451697
}
16461698

16471699
static void logging_timer_handler(void *p_context) {
@@ -1690,15 +1742,7 @@ static void logging_timer_handler(void *p_context) {
16901742
log_message_gps.latitude = hgps.latitude * 100000;
16911743
log_message_gps.longitude = hgps.longitude * 100000;
16921744

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;
17021746
}
17031747
// We can write a GPS Delta message!
17041748
else
@@ -1720,15 +1764,7 @@ static void logging_timer_handler(void *p_context) {
17201764
log_message_gps.latitude = hgps.latitude * 100000;
17211765
log_message_gps.longitude = hgps.longitude * 100000;
17221766

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;
17321768
}
17331769
}
17341770

@@ -1744,29 +1780,7 @@ static void logging_timer_handler(void *p_context) {
17441780
melody_play(MELODY_GPS_LOCK, false); // Play GPS locked melody, do not interrupt
17451781
}
17461782

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
17701784
if (!rtc_time_has_sync && gps_signal_locked && hgps.seconds != 247 && hgps.seconds != 0)
17711785
{
17721786
//strftime(datetimestring, 64, "%Y-%m-%dT%H:%M:%S", tmTime);
@@ -1795,15 +1809,7 @@ static void logging_timer_handler(void *p_context) {
17951809
log_message_freesk8.event_type = TIME_SYNC;
17961810
log_message_freesk8.event_data = newTimeSeconds - currentTime;
17971811

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;
18071813
}
18081814

18091815
// Update time in memory
@@ -1979,7 +1985,7 @@ static ret_code_t twi_master_init(void)
19791985
#define QSPI_STD_CMD_WRSR 0x01
19801986
#define QSPI_STD_CMD_RSTEN 0x66
19811987
#define QSPI_STD_CMD_RST 0x99
1982-
1988+
#define QSPI_CMD_EN4B 0xB7 // Enable 32-bit addressing
19831989
static void configure_memory()
19841990
{
19851991
uint8_t temporary = 0x40;
@@ -2008,6 +2014,12 @@ static void configure_memory()
20082014
err_code = nrf_drv_qspi_cinstr_xfer(&cinstr_cfg, &temporary, NULL);
20092015
APP_ERROR_CHECK(err_code);
20102016

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+
20112023
NRF_LOG_INFO("QSPI Memory Configured");
20122024
NRF_LOG_FLUSH();
20132025
}
@@ -2187,12 +2199,17 @@ void littlefs_init()
21872199
NRF_LOG_INFO("LittleFS initializing");
21882200
NRF_LOG_FLUSH();
21892201

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+
21902207
// mount the filesystem
21912208
int err = lfs_mount(&lfs, &cfg);
21922209

21932210
// reformat if we can't mount the filesystem
21942211
// this should only happen on the first boot
2195-
if (err) {
2212+
if (err < 0) {
21962213
NRF_LOG_WARNING("LittleFS needs to format the storage");
21972214
NRF_LOG_FLUSH();
21982215
int lfs_format_response = lfs_format(&lfs, &cfg);
@@ -2205,14 +2222,9 @@ void littlefs_init()
22052222
#endif
22062223
melody_play(MELODY_GOTCHI_FAULT, true); // Play robogotchi fault, interrupt
22072224
}
2208-
NRF_LOG_INFO("LittleFS initialized");
2225+
NRF_LOG_INFO("LittleFS initialized. Result: (%d)", err);
22092226
NRF_LOG_FLUSH();
22102227

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-
22162228
// read current count
22172229
lfs_file_opencfg(&lfs, &file, "boot_count", LFS_O_RDWR | LFS_O_CREAT, &lfs_file_config);
22182230
lfs_file_read(&lfs, &file, &boot_count, sizeof(boot_count));
@@ -2544,6 +2556,46 @@ int main(void) {
25442556
packet_process_byte(byte, PACKET_VESC);
25452557
}
25462558

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+
25472599
while (gps_uart_get(&byte) == NRF_SUCCESS) {
25482600
time_gps_last_responded = currentTime;
25492601
lwgps_process(&hgps, &byte, sizeof(byte));

0 commit comments

Comments
 (0)