AnsweredAssumed Answered

STM32F10x + FatFs issue

Question asked by khan.saud on Apr 9, 2014
Latest reply on Apr 11, 2014 by H.Rick
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");
}

Outcomes