Multi-sector(?) SD write fails

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

Just realized I could try a more stable test with the SDMMC.cpp example. All I did was adjust variable len to set the size of the file. Same thing: successful up till 511 bytes (len = 511). Fails at 512. Even if I set all the buffers to be 1024.

Ok learning more about fatfs. I’m not sure if this is the “correct” way or if there is some function in fatfs I should be using instead but this will write a file of any len if you replace line 67 in SDMMC.cpp (starting with f_write) with:

size_t chunk_size = 511;
size_t remaining = len;
size_t offset = 0;
while (remaining > 0) {
    size_t to_write = (remaining > chunk_size) ? chunk_size : remaining;
    f_write(&SDFile, outbuff + offset, to_write, &byteswritten);
    offset += to_write;
    remaining -= to_write;
}

That will write it in chunks not bigger than 511 bytes.