SD card: https://www.amazon.co.uk/Kingston-SDCS- ... B079GTYCW4
MCU: STM32 L452RE
This code collects and writes samples to the SD card for 1 second, i expect 16,000 samples to be wrote, but after 1 second, only 4608 samples are writte. This means the file.write() call is blocked for a large amount of time, does anyone know how to solve this?

Code: Select all
#include <SPI.h>
#include <SdFat.h>
#include <HardwareSerial.h>
#include <HardwareTimer.h>
HardwareTimer TIMER(TIM2);
HardwareSerial SerialDebug((uint32_t)PA3, (uint32_t)PA2);
SdFat sd;
SdBaseFile file;
uint32_t TIME_STARTED = 0U;
#define SD1 PD2
#define CS2 PC8
#define MOSI1 PA7
#define MISO1 PA6
#define SCK1 PA5
uint32_t SAMPLES_WROTE = 0U;
uint8_t buff[512];
uint16_t buffIndex = 0U;
void samplerTimer_Callback(HardwareTimer * pInTimer)
{
uint8_t sample = random(0, 255);
buff[buffIndex] = sample;
SAMPLES_WROTE++;
buffIndex++;
if (buffIndex >= 512)
{
file.write(buff, 512);
buffIndex = 0;
}
if ((millis() - TIME_STARTED) > 1000U)
{
SerialDebug.println(SAMPLES_WROTE);
TIMER.pause();
file.close();
}
}
void setup()
{
SerialDebug.begin(9600);
SerialDebug.println("Starting...");
pinMode(SD1, OUTPUT);
digitalWrite(SD1, HIGH);
pinMode(CS2, OUTPUT);
SPI.setSCLK(SCK1); // CS2
SPI.setMISO(MISO1); // MISO1
SPI.setMOSI(MOSI1); // MOSI1
bool sdOK = sd.begin(CS2, SD_SCK_MHZ(40));
if (!sdOK)
{
SerialDebug.println("SD CARD FAIL");
while(true) { }
}
bool fileOK = file.open("T.BIN", O_CREAT | O_WRITE);
if (!fileOK)
{
SerialDebug.println("FILE FAIL");
while(true) { }
}
TIMER.setMode(1, TIMER_OUTPUT_COMPARE, NC); // remove this for new version of STM32Dino, required version 1.8.0.
TIMER.setOverflow(16000, HERTZ_FORMAT);
TIMER.attachInterrupt(samplerTimer_Callback);
TIMER.resume();
TIME_STARTED = millis();
}
void loop()
{
}
Running this program multiple times with 1 SD card results in different writing speeds, see the following:
I alternates between 13282 samples and 4722 samples over restarts? Weirdly enough, 4096 is the default allocation size.11:44:04.720 -> Starting...
11:44:05.823 -> 4096 samples
11:47:45.430 -> Starting...
11:47:46.524 -> 13249 samples
11:47:58.151 -> Starting...
11:47:59.243 -> 4608 samples
11:48:17.136 -> Starting...
11:48:18.230 -> 13282 samples
11:48:27.568 -> Starting...
11:48:28.668 -> 4722 samples