SD Card Write Reliability Problems - STM32F405
Posted: Wed Mar 04, 2020 3:32 pm
Hi guys,
I'm currently using a Adafruit STM32F405 Feather Express to log sensor data to SD and am having problems continuously writing large blocks of data (eg 2048 bytes) to the card in a loop. The problem is that it works for a varied number of seconds and ends with an error which is either:
1. Unable to write the number of bytes requested
2. Error opening file after last close
It seems to be possible to make it slightly better by implementing delays, but the results are unreliable.
I have attached a sample code, which demonstrates the problem below. If delay1 is removed, error results eventually. If delay1 and delay2 are removed it fails immediately. Also if the data size, ASIZE is increased it also results in an error sooner.
I am using the following:
Board: Adafruit STM32F405 Feather Express
microSD: Sandisk Extreme Pro and Ultra 32GB
Board manager -> STM32 Cores version 1.8.0
Libraries -> STM32duino STM32SD version 1.2.2
Test program:
Please let me know if you have any advice to improve the reliability of SD writing.
Many thanks,
Jim
I'm currently using a Adafruit STM32F405 Feather Express to log sensor data to SD and am having problems continuously writing large blocks of data (eg 2048 bytes) to the card in a loop. The problem is that it works for a varied number of seconds and ends with an error which is either:
1. Unable to write the number of bytes requested
2. Error opening file after last close
It seems to be possible to make it slightly better by implementing delays, but the results are unreliable.
I have attached a sample code, which demonstrates the problem below. If delay1 is removed, error results eventually. If delay1 and delay2 are removed it fails immediately. Also if the data size, ASIZE is increased it also results in an error sooner.
I am using the following:
Board: Adafruit STM32F405 Feather Express
microSD: Sandisk Extreme Pro and Ultra 32GB
Board manager -> STM32 Cores version 1.8.0
Libraries -> STM32duino STM32SD version 1.2.2
Test program:
Code: Select all
#include <STM32SD.h>
// Test filename
#define FILEGPS "gps.txt"
File dataFile ;
#ifndef SD_DETECT_PIN
#define SD_DETECT_PIN SD_DETECT_NONE
#endif
#define ASIZE 2048
char a[ASIZE];
void setup()
{
Serial.begin(115200);
while (!Serial); //Wait for user to open terminal
// ********** setup SD Card
Serial.println("SD Card setup start");
while (!SD.begin(SD_DETECT_PIN))
{
delay(10);
}
delay(100);
SD.remove(FILEGPS);
Serial.println("SD Card setup completed successfully");
// Setting up some data
for (int i = 0; i < ASIZE; i++)
a[i] = '0';
a[ASIZE-3] ='\0';
strcat(a,"\r\n");
}
void loop()
{
int i,len, offset;
dataFile = SD.open(FILEGPS, FILE_WRITE);
if (!dataFile)
{
Serial.println("error opening gps.txt");
while(1);
}
for (i=0;i<10;i++)
{
offset = dataFile.size();
dataFile.seek(offset);
len = dataFile.write(a,strlen(a));
if (len != strlen(a))
{
Serial.println("Error");
while(1);
}
else
Serial.println("OK");
delay(2); // delay 1
}
delay(3); // delay 2
dataFile.close();
}
Please let me know if you have any advice to improve the reliability of SD writing.
Many thanks,
Jim