uSD random slow writes

Hello everyone.

I am running atmega328p with uSD card on custom PCB. Using write() method I'm trying to write array that contains 192 byte elements. According to millis() its usually takes around 2ms, sometimes up to 7ms and occasionally like 18ms or even 200ms. Why is it so irregular and so unreliable? uSD card was formatted with SDFormatter v4.

Here is endless loop that execute mentioned operation:

while(1) {

PCICR &= ~(0b00000110); //blocking PC interrupts that i use

time=millis(); myFile.write(a,192); //a is declared like this: byte a[192]={'/1'}; if(millis()-time>6) Serial.println(millis()-time); //if time extended 6ms its being printed to serial myFile.flush(); PCICR |= 0b00000110; //enabling interrupts }

Thanks in advance.

SD cards are optimised for writing large blocks of data, not a high transaction rate with small blocks of data. A Google search for "sd card write latency" shows that you have the same problem as everyone else.

Try packaging your data into larger blocks of data that are a multiple of 512 bytes. Do you see any change in the transaction rate?

You could fit 5 x 192 into 1024 bytes with some padding bytes at the end.

There's also FRAM memory https://www.adafruit.com/products/1895