SD card - f_open always works with spi.

Tip / Sign in to post questions, reply, level up, and achieve exciting badges. Know more

cross mob
User20173
Level 2
Level 2
First solution authored
Hello ))

What I do:
f_mount(&fs, "0", 1); returns always "OK", and f_open/f_write work fine, addressing floders and files present on SPI.

f_mount(&fs, "1", 1); returns "OK" only when SD-card is inside the slot, so I am sure, that "1" is correct address of SD Card.
But the following call of f_open/f_write does the same as with SPI: it address file structures and creates files present on SPI.
If I take SD card after and connect to PC with cardreader, I see there is no change.

How to explicitly tell f_open() function to address SD, and not SPI??

Is there any input parameter, or a filed inside FIL structure?

Update:
I try to call it:
res = f_open(&fil, "1:xmc4800/hello.txt", FA_CREATE_ALWAYS | FA_WRITE | FA_READ);

but the resultis Invalid-Drive!

so, f_mount(&fs, "1", 1); returns OK,
but f_open with a path startin with "1:.. return invalid drive... hm...
0 Likes
1 Solution
User20173
Level 2
Level 2
First solution authored
This code is working fine, question closed.

void testSD()
{
FATFS fs; /* File system object (volume work area) */
FIL fil; /* File object */
FRESULT res; /* API result code */
UINT bw,br; /* Bytes written */
char buff[20];

res = FR_OK;

DSTATUS status = disk_initialize(0);
if (status!=0)
{
printf(status);
//return;
}

/* Register work area */
res = f_mount(&fs, "0", 1);

if (res == FR_OK)
{
/* Create a new directory */
//res = f_mkdir("XMC4800");
if ((res == FR_OK) || (res == FR_EXIST))
{

// test read-dir
DIR dp;
char* path="0:xmc4800";
res=f_opendir(&dp, path);
if (res == FR_OK)
{
FILINFO fno;
res=f_readdir(&dp, &fno);
if (res != FR_OK)
{
return;
}
res=f_closedir(&dp);
}


/* Create a file as new */
const char* ptr ="0:/hello.txt";
res = f_open(&fil, ptr, FA_CREATE_ALWAYS | FA_WRITE | FA_READ);
if ((res == FR_OK) || (res == FR_EXIST))
{
/* Write a message */
res = f_write(&fil, "Hello, World!\r\n", 15, &bw);
if (res == FR_OK )
{
res = f_lseek(&fil, 0);
/* Read the buffer from file */
res = f_read(&fil, &buff[0], 15, &br);
if(res == FR_OK)
{
/* Go to location 15 */
res = f_lseek(&fil, 15);
if(res == FR_OK)
{
/* Add some more content to the file */
res = f_write(&fil, "\nWelcome to Infineon", 20, &bw);
if(res == FR_OK)
{
f_close(&fil);
}
}
}
}
}
}
}
res = f_unmount("0");
}

View solution in original post

0 Likes
2 Replies
User20173
Level 2
Level 2
First solution authored
This code is working fine, question closed.

void testSD()
{
FATFS fs; /* File system object (volume work area) */
FIL fil; /* File object */
FRESULT res; /* API result code */
UINT bw,br; /* Bytes written */
char buff[20];

res = FR_OK;

DSTATUS status = disk_initialize(0);
if (status!=0)
{
printf(status);
//return;
}

/* Register work area */
res = f_mount(&fs, "0", 1);

if (res == FR_OK)
{
/* Create a new directory */
//res = f_mkdir("XMC4800");
if ((res == FR_OK) || (res == FR_EXIST))
{

// test read-dir
DIR dp;
char* path="0:xmc4800";
res=f_opendir(&dp, path);
if (res == FR_OK)
{
FILINFO fno;
res=f_readdir(&dp, &fno);
if (res != FR_OK)
{
return;
}
res=f_closedir(&dp);
}


/* Create a file as new */
const char* ptr ="0:/hello.txt";
res = f_open(&fil, ptr, FA_CREATE_ALWAYS | FA_WRITE | FA_READ);
if ((res == FR_OK) || (res == FR_EXIST))
{
/* Write a message */
res = f_write(&fil, "Hello, World!\r\n", 15, &bw);
if (res == FR_OK )
{
res = f_lseek(&fil, 0);
/* Read the buffer from file */
res = f_read(&fil, &buff[0], 15, &br);
if(res == FR_OK)
{
/* Go to location 15 */
res = f_lseek(&fil, 15);
if(res == FR_OK)
{
/* Add some more content to the file */
res = f_write(&fil, "\nWelcome to Infineon", 20, &bw);
if(res == FR_OK)
{
f_close(&fil);
}
}
}
}
}
}
}
res = f_unmount("0");
}
0 Likes
Vasanth
Moderator
Moderator
Moderator
250 sign-ins 500 solutions authored First question asked
Hi,

Thanks for sharing the solution to the community.

Best Regards,
Vasanth
0 Likes