cancel
Showing results for 
Search instead for 
Did you mean: 

STM32F10x + FatFs issue

saudkhanis
Associate II
Posted on April 09, 2014 at 01:52

Hi All,

I am new to this community so if I have posted this in the wrong place then please let me know. I have a STM32 Discovery board which I am trying to log data onto a SD card through SPI. I am using the FatFs library for this which seems to work fine with my driver code. The issue ive got is explained bellow: If I have my SD card connected to the adapter and then switch my discovery board on, I am able to mount the drive and read and write data to the SD card and then unmount it (this is the place i think the issue is). Now if I disconnect the SD card while the discovery board is running and reconnect the SD card, my discovery board cannot mount the drive again. Even if I press the reset button on board it still gets stuck on trying to mount the drive and then fails (returning error 13 -There is no valid FAT volume. I can be wrong about this). I should mention here that the code I written literally prints a line in the text file and then closes it and umounts the drive. Further to this, if I switch the board off completely and switch it back on, the SD card mounts fine and then i can write to it too. I have looked around on google for hours now and I cant figure out why I am getting this issue. I have a feeling that this has got to do something with the way im unmounting and removing the SD card. Anyone got any idea what can be going wrong? The following is my main.c code.

int
main(
void
) {
/*FatFs definitions--------------------------------------------------------*/
FATFS FatFs;
FIL fil;
FRESULT res;
UINT
br, bw; 
/* File read/write count */
unsigned 
char
buffer[300];
Serial_PutString(
''Mounting volume...''
);
res = f_mount(&FatFs, 
''''
, 1); 
// mount the drive
sprintf
(voltage_string, 
''%d''
, res);
// send the value of res over usart
Serial_PutString(
''res=: ''
);
Serial_PutString(voltage_string);
Serial_PutString(
''\n''
);
if
(res) {
Serial_PutString(
''error occured!\n''
);
while
(1)
;
}
Serial_PutString(
''success!\n''
);
Serial_PutString(
''Opening file: \''log.csv\''...''
); 
//creating/opening file
res = f_open(&fil, 
''log.csv''
, FA_OPEN_ALWAYS | FA_READ | FA_WRITE); 
// open existing file in read and write mode
if
(res) {
Serial_PutString(
''error occured!\n''
);
while
(1)
;
}
Serial_PutString(
''success!\n''
);
Serial_PutString(
''Writing to file.\n''
); 
//now writing to file
f_lseek(&fil, f_size(&fil)); 
//move the pointer to EOF
res = f_write(&fil, 
log
, 
sizeof
(
log
), &bw); 
//write to file
if
(res == FR_OK) {
f_write(&fil, 
''\n\r''
, 2, &bw);
Serial_PutString(
''File Written!''
);
sprintf
(voltage_string, 
''%d''
, f_size(&fil));
Serial_PutString(
''Size of file: ''
);
Serial_PutString(voltage_string);
} 
else
{
Serial_PutString(
''Not Written!''
);
}
f_gets(buffer, 0, &fil); 
//ignore this for now
f_lseek(&fil, 0); 
//ignore this for now
Serial_PutString(
''I read: \''''
);
Serial_PutString(buffer);
Serial_PutString(
''\''\n\r''
);
Serial_PutString(
''Printing to file what i read\n''
);
f_puts(buffer, &fil);
Serial_PutString(
''closing file\n''
);
f_close(&fil); 
// close the file
res = f_mount(0, 
''''
, 1); 
// unmount the drive.
sprintf
(voltage_string, 
''%d''
, res);
Serial_PutString(
''res=: ''
);
Serial_PutString(voltage_string);
Serial_PutString(
''\n''
);
Serial_PutString(
''SD Card Unmounted''
);
}

#fatfs-issue
7 REPLIES 7
Posted on April 09, 2014 at 02:20

The problem is occurring at a lower level. You need to analyze what the SD/SPI driver software is doing, how it initializes, and what errors you are getting from it. The mount/unmount may not trigger anything at the SD card level, it is designed to flush file system data, writing any pending buffers. The driver code may not understand that it is being disconnected, and needs to reinitialize. You should look at what state information the driver is maintaining, and if you need to reset/clear that.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
saudkhanis
Associate II
Posted on April 09, 2014 at 02:44

The original post was too long to process during our migration. Please click on the provided URL to read the original post. https://st--c.eu10.content.force.com/sfc/dist/version/download/?oid=00Db0000000YtG6&ids=0680X000006I6eW&d=%2Fa%2F0X0000000bsU%2Fqj5wE2Gj3oWIe5tAvfAONWLv5vjf1B9ia1fvxj6_hFs&asPdf=false
Posted on April 09, 2014 at 03:00

You'd want to instrument the disk_initialize() and disk_read(), and understand what is happening with them in your failing condition.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
saudkhanis
Associate II
Posted on April 11, 2014 at 00:51

Thank you clive1 for helping me! I have managed to fix the issue. My issue was with using the wrong GPIO mode. I had initially set it to GPIO_MODE_IN_FLOATING whereas it needed to be set to GPIO_MODE_IPD.  To be honest I dont know the big difference between these two modes. I might have to read a bit more about this. or maybe someone can explain it to me?

once again.. thanks a lot!

saudkhanis
Associate II
Posted on April 11, 2014 at 00:52

sorry I forgot to mention. Its the GPIO pin for SD card detect pin that needed to be set to GPIO_MODE_IPD.

saudkhanis
Associate II
Posted on April 11, 2014 at 01:35

oo boy.. ignore all i said earlier. The bug wasnt because of the fact i had the wrong GPIO mode selected. it was because if you look at my ''sdc_isConn()'' function, im checking pin 12 whereas I should be checking pin 10, as pin 10 is the sd card select pin. I really should go to bed now -.-

antonius
Senior
Posted on April 11, 2014 at 11:24

Try changing the speed of GPIO to 10Mhz

GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz;