AnsweredAssumed Answered

Problem with file creating on SD card with FatFs lib

Question asked by prieur.jean on Apr 16, 2014
Latest reply on May 8, 2014 by prieur.jean

I use a STM32F427 running with FreeRTOS.

I try to create a file on my SD card, through SDIO, the stm32f4_discovery_sdio_sd.c (DMA mode). I also use the librairy FatFs R0.09, ported by Clive I think :)

This is my code to write a file:

FIL fil;
DIR dir;
FATFS fs32;
UINT bw;             /* File write count */
char* path;
char *fn;   /* This function is assuming non-Unicode cfg. */

#if _USE_LFN
static char lfn[_MAX_LFN + 1];
fno.lfname = lfn;
fno.lfsize = sizeof lfn;

    // SDCard mount
    memset(&fs32, 0, sizeof(FATFS));
    res = f_mount(0, &fs32);
    memset(&fil, 0, sizeof(FIL));
    res = f_open(&fil, "NEW.TXT", FA_CREATE_ALWAYS | FA_WRITE);
    res = f_write(&fil, "hello YOU", sizeof("hello YOU") , &bw);

Of course before I configured the NVIC:

int init_NVIC_sdio(void)
    NVIC_InitTypeDef NVIC_InitStructure;
    // SDIO Interrupt ENABLE
    NVIC_InitStructure.NVIC_IRQChannel = SDIO_IRQn;
    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 6;
    NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
    NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;

The behavior seems random.
- sometimes it works, all of the "res" returns are OK and the file is created on the SD card
- sometimes f_open returns "FR_NOT_READY" and f_write returns "FR_INVALID_OBJECT"

Do you have any idea ? It's hard to debug because of the random behavior...

Thanks !!