Skip to main content
Associate III
June 24, 2026
Solved

FATFS writing issues

  • June 24, 2026
  • 5 replies
  • 94 views

Hi, 

I’m working with FATFS + SDIO + USB mass storage class.

But I can’t seem to get writing and as a consequence reading working.

in my main.c I do:

	//fatfs test
makeFileData("test12.txt", "TEST123");

char* data = readFileData("test12.txt");

printf("read data = %s\r\n", data);

 

And in my console I get as a response:


D-SH 0 /System Volume Information
---- 209 /x1_DEFAULT.JSON
---- 211 /x2_DEFAULT.JSON
---- 211 /x3_DEFAULT.JSON
---- 211 /Ik_heb_iets_verzonne.JSON
Total blocks: 1922016 (961 Mb)
Free blocks : 1921888 (960 Mb)
SD Card Block size = 512
SD Card Block number = 1953792
SD Card size = 1000341
SD Card Version = 1
Init WVR complete!

f_write failed: 1
read data =

So It can open the card and perform a ls command, but for some reason I can’t write. I spotted that some people look at DMA but I’m not using DMA.

My entire SD card Handler:

/*
* SD_Card_Handler.c
*
* Created on: June 24, 2026
* Author: william.vanraemdonck
*/

#include "WVR/SD/SD_Card_Handler.h"
#include "fatfs.h"
#include <stdio.h>
#include <string.h>

const char tmpFileName[] = "temp.json";
uint32_t rbytes, wbytes;

char readData[256];


//init
void initSdCard(void){
//mount the card
if(f_mount(&SDFatFS, (TCHAR const*)SDPath, 0) != FR_OK){
printf("Unable to mount disk\r\n");
Error_Handler();
}

//list the files
ls();
}

char* readFileData(char* filename){

if(f_open(&SDFile, filename, FA_OPEN_EXISTING | FA_READ) == FR_OK){
if(f_read(&SDFile, readData, sizeof(readData), (void*)&rbytes) == FR_OK){
printf("read data = %s\r\n", readData);
f_close(&SDFile);
}
else{
printf("NFC Unable to read data from file\r\n");
return "ERROR";
}
}
return readData;
}

uint8_t makeFileData(char* filename, char* data){
FRESULT res;

res = f_open(&SDFile, filename, FA_CREATE_ALWAYS | FA_WRITE);
if(res == FR_OK)
{
res = f_write(&SDFile, data, strlen(data), &wbytes);
if(res == FR_OK)
{
printf("Written %lu bytes\r\n", wbytes);
}
else
{
printf("f_write failed: %d\r\n", res);
}
f_close(&SDFile);
return 2;
}
else{
printf("f_open failed: %d\r\n", res);
return 3;
}
return 0;
}


//helpers
uint8_t BSP_SD_IsDetected(void){
__IO uint8_t status = SD_PRESENT;
return status;
}


void ls(void) {

FRESULT res;
DIR dir;
char *path;

path = ""; // where you want to list

res = f_opendir(&dir, path);

#ifdef DEBUG
if (res != FR_OK)
printf("res = %d f_opendir\n\r", res);
#endif

if (res == FR_OK) {
while (1) {

FILINFO fno;

res = f_readdir(&dir, &fno);

#ifdef DEBUG
if (res != FR_OK)
printf("res = %d f_readdir\n\r", res);
#endif

if ((res != FR_OK) || (fno.fname[0] == 0))
break;

printf("%c%c%c%c %10d %s/%s\n\r",
((fno.fattrib & AM_DIR) ? 'D' : '-'),
((fno.fattrib & AM_RDO) ? 'R' : '-'),
((fno.fattrib & AM_SYS) ? 'S' : '-'),
((fno.fattrib & AM_HID) ? 'H' : '-'),
(int) fno.fsize, path, fno.fname);
}

}

uint32_t free_clusters;
FATFS *fs_ptr;

res = f_getfree("", &free_clusters, &fs_ptr);
if (res == FR_OK) {
uint32_t totalBlocks = (fs_ptr->n_fatent - 2) * fs_ptr->csize;
uint32_t freeBlocks = free_clusters * fs_ptr->csize;

printf("Total blocks: %lu (%lu Mb)\n\r", totalBlocks, totalBlocks / 2000);
printf("Free blocks : %lu (%lu Mb)\n\r", freeBlocks, freeBlocks / 2000);
} else {
printf("Unable to get free space\n\r");
}

}

Based on this video:

 

Does anyone spot the mistake, I’m guessing I forgot something stupid.

Kind regards, 

William

 

 

Best answer by William_RnD

Hi,

I looked at at with a fresh head.

I lowered the clock frequency to 4MHz and that worked!

Thank you for the support!

William

5 replies

mƎALLEm
ST Technical Moderator
June 24, 2026

Hello ​@William_RnD ,

Such kind of questions need to be asked in “STM32 MCUs Embedded software” forum board.

I’ve already moved the post.

Thank you for your understanding & Good luck.

To give better visibility on the answered topics, please click "Best answer" on the reply which solved your issue or answered your question.
Andrew Neil
Super User
June 24, 2026

 I can’t seem to get writing and as a consequence reading working.

That seems to be a non-sequitur ?

Why do you think that not reading is a result of not writing ?

 

f_write failed: 1

So have you looked-up what that error code means?

Have you stepped into f_write to see where it fails ?

A complex system that works is invariably found to have evolved from a simple system that worked.A complex system designed from scratch never works and cannot be patched up to make it work.
Associate III
June 24, 2026

Hi,

My wording was a bit wrong there, the read I was trying to do after trying to write a file also failed. Because the file wasn’t written.

in ff.c line 3657

If (clst == 0xFFFFFFFF) ABORT(fs, FR_DISK_ERR); this return that 1

 

But I didn’t get much wiser from that;

When using the USB interface and the mass storage class I do see the files on the SD card.
(Those files I put on before plugging it into my board).

Kind regards, 

William

 

 

 

Andrew Neil
Super User
June 24, 2026

the read I was trying to do after trying to write a file also failed. Because the file wasn’t written.

So can you read a file which does exist ?

 

If (clst == 0xFFFFFFFF) ABORT(fs, FR_DISK_ERR); this return that 1

Presumably, 0xFFFFFFFF is some sort of default and clst should have been set to something else if things were working.

So look back to see what would have set it - and shy that didn’t happen ...

A complex system that works is invariably found to have evolved from a simple system that worked.A complex system designed from scratch never works and cannot be patched up to make it work.
William_RnDAuthorBest answer
Associate III
June 25, 2026

Hi,

I looked at at with a fresh head.

I lowered the clock frequency to 4MHz and that worked!

Thank you for the support!

William