EDIT: See my comment below for simpler test
I’ve narrowed down on why certain files are failing to write to SD card. This is my test:
const char* testFileName = "test.txt";
char testBuffer[1024]; // Buffer for testing
memset(testBuffer, 'A', sizeof(testBuffer)); // Fill buffer with dummy data
// Test with different sizes
size_t testSizes[] = {256, 400, 500, 511, 512, 1024}; // Sizes to test
for (size_t i = 0; i < sizeof(testSizes) / sizeof(testSizes[0]); i++) {
size_t testSize = testSizes[i];
mLog(SD_CARD, "test: %d bytes", testSize);
// Open file for writing
FRESULT result = f_open(&jsonFile_, testFileName, FA_CREATE_ALWAYS | FA_WRITE);
if (result != FR_OK) {
mLog(SD_CARD, "Open Err: %d", result);
continue;
}
// Write data to file
size_t bytesWritten = 0;
result = f_write(&jsonFile_, testBuffer, testSize, &bytesWritten);
if (result == FR_OK && bytesWritten == testSize) {
mLog(SD_CARD, "Write 1: %d bytes written", bytesWritten);
} else {
mLog(SD_CARD, "Write 0: %d, Bytes Written: %d", result, bytesWritten);
}
// Close the file
f_close(&jsonFile_);
and my results are:
test: 256 bytes
Write 1: 256 bytes written
test: 400 bytes
Write 1: 400 bytes written
test: 500 bytes
Write 1: 500 bytes written
test: 511 bytes
Write 1: 511 bytes written
test: 512 bytes
Write 0: 1, Bytes Written: 0
test: 1024 bytes
Write 0: 1, Bytes Written: 0
This says to me that writing fails when the file is larger than 511 bytes which is just under the size of the usual sector size for an SD card.
Perhaps someone more knowledgeable than me can tell me what I might not be doing correctly for multi-sector writes? Or if there is some limit built into the daisy libraries?
Thanks