Hi to everybody! I've been following this forum for a while. As my first post, i wanted to show you an issue i've been dealing lately.
My final goal is to realize a fast data logger , collecting data from an MPU 6050 and a GPS, and store them into an SD.
It's obvious that the SD writing time is going to set a lower bound on how fast i could go with the sampling time of the sensors data.
The main issue with it is that, once in a while, writing and synching time of the SD randomly goes as up as ~100-200ms.
I'm attaching here the routine i've been using for debugging this issue:
NB queue is a buffer long 64bytes, and BUFFER_SIZE is defined as 64, while FILE_SIZE is the number of writes i allow, for debugging purposes.
FILENAME_TEST is defined as "TEST.BIN"
void log_data()
{
// Open File
data_row.open(FILENAME_TEST, O_CREAT | O_WRITE);// Logging loop has begun!
delay(1000);
Serial.println(F("Starting logging routine"));
Serial.flush();
delay(10);
uint32_t bn = 0;
uint32_t dt_write;
uint32_t dt_sync;
uint32_t max_dt_write = 0;
uint32_t max_dt_sync = 0;
struct timer {
uint16_t st;
uint16_t old_st;
uint16_t max_st;
} sampling;
uint16_t bytes_written;
uint32_t max_bytes;
bool sync_flag = false;
// Fill buffer
for(uint8_t i = 0; i<BUFFER_SIZE; i++)
{
queue = i;
}*
// Free cache*
uint8_t cache = (uint8_t)SD.vol()->cacheClear();delay(1000);*
if(cache == 0)*
{*
Serial.println(F("Cache clear error"));*
}*
while(sd_ready)*
{*
sampling.st = millis() - sampling.old_st;*
queue[0] = bn;*
sampling.old_st = millis();*
if(sampling.st > sampling.max_st)*
{*
sampling.max_st = sampling.st;*
}*
digitalWrite(WRITE_PIN, HIGH);*
dt_write = millis();*
// Writing full array*
bytes_written = data_row.write((uint8_t*) queue, BUFFER_SIZE);digitalWrite(WRITE_PIN, LOW);*
dt_write = millis() - dt_write;*
dt_sync = millis();*
bn++; // Update written data block*
// FILE SIZE ROUTINE*
if(bn == FILE_SIZE)*
{*
break;*
}*
if(sync_flag)*
{*
digitalWrite(SYNC_LED, HIGH);*
bn = 0;*
data_row.sync();*
digitalWrite(SYNC_LED, LOW);*
}*
dt_sync = millis() - dt_sync;*
//Update current max*
if(dt_write > max_dt_write)*
{*
max_dt_write = dt_write;*
max_bytes = bn * BUFFER_SIZE;}*
if(dt_sync > max_dt_sync)*
{*
max_dt_sync = dt_sync;*
}*
// When user inputs to the serial, then stop logging*
if(Serial.available())*
{*
Serial.println(F("Stop LOG"));*
break;*
}*
}*
// Close file*
uint32_t dt_close;*
Serial.println(F("Closing file..."));*
dt_close = millis();*
data_row.close();*
dt_close = millis() - dt_close;*
Serial.print(F("Closing file took -> ")); Serial.println(dt_close);*
Serial.print(F("Max writing time -> ")); Serial.println(max_dt_write);*
Serial.print(F("Max sync time -> ")); Serial.println(max_dt_sync);*
Serial.print(F("Sampling Time -> ")); Serial.println(sampling.max_st);*
Serial.print(F("Written bytes -> ")); Serial.println(bytes_written);*
Serial.print(F("Bytes written when MAX -> ")); Serial.println(max_bytes);*
}[/quote]
Through an oscilloscope i've found that, after i've wrote 512bytes worth of data, sync method is automatically called. I think the problem is related to the actual cluster size of my sd card, which is 32kb (formatted using SDFormatter, as the top topic on this forum suggested).
Any suggestion about this is appreciated.