SD FAT & GPS / Checksum Errors

Status
Not open for further replies.

Paraglider

Well-known member
When logging data to an SD card using SD FAT and reading/parsing GPS NMEA strings using TinyGPSPlus in the same loop then the there there are checksum errors. On an average 1 of 30 or 40 GPS readings is corrupt when logging lots of data from an AHRS, baro and GPS with 50Hz. When using dummy data, so no reading of IMU and pressure sensor then the error is the same, there are roughly the same amount of checksum errors over time. But when logging data only at 5Hz for a KML file then the checksum errors rarely appear.

I already increased the RX buffer for the GPS to 250, but there was no difference to see. The GPS outputs NMEA strings with 115200 baud at 5Hz, so the buffer should be big enough. It looks like it has something to do with the writing of the data to the SD card, like the RX buffer of Serial1 would be blocked when data is written to the SD card. Data is added to a string, and whenever the string is longer than 512 then the 512 first bytes are written to the SD card.

The relevant code snippets are as follows:

Code:
#define GPSSerial Serial1
static const uint32_t GPSBaud = 115200;

Read data from GPS using serialEvent1:

Code:
void serialEvent1() {
  while (GPSSerial.available()) {
    char c = GPSSerial.read();
    gps.encode(c);
  }
}

Data is written to the SD card in chuncks of 512 bytes:

Code:
    file.print(SDbufStr);
    file.sync();

How can I solve this problem? Is this issue related to SPI transaction?
 
The complete code is here: View attachment GPS_SD_FAT_NMEA_CHECKSUM_ERROR.zip The schematic is very simple. The GPS is connected to RX1 / TX1, and the SD card is connected to pins 10,11,12 & 13. Data get's written to the SD card, that's not the problem. And the GPS NMEA strings will get parsed with no checksum errors when not writing to the SD card. But when writing only few data like position and alt with 5Hz there are a few checksum errors, and a lot of checksum errors when writing also IMU data at 50Hz.
 
Guess: SD card I/O is causing lost bytes from GPS via UART. But GPS NMEA defaults to 4800 baud - pretty slow character rate. The UART drivers (should) implement the UART FIFO so even in interrupts are blocked in the SD card for an absurdly long time, there still shouldn't be lost bytes. FIFO is an option in the driver.

You can check the NMEA sentence length to see if bytes are lost.
 
Thanks for the reply. I don't know if there are bytes lost, since the checksum errors appear after parsing the NMEA strings. But I guess that it's probably the case. How can I enable the UART FIFO? Could you give me advice how to do that?
 
The UART FIFO should be used by default in the serial library, I assume.
But truly, the SD card usage, causing NMEA serial checkum errors, indicates some more serious problem in blocking interrupts way too long, as a guess.

If bytes are dropped, you can see that by printing out the NMEA sentence that has a bad checksum.
 
Problem Solved

Increasing the RX buffer of Serial1 did the trick. Now data logging works for hours without any checksum error.
 
Status
Not open for further replies.
Back
Top