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
