Skip to main content
jean_prieur
Associate III
April 16, 2014
Question

Problem with file creating on SD card with FatFs lib

  • April 16, 2014
  • 20 replies
  • 4247 views
Posted on April 16, 2014 at 12:36

Hello,

I use a STM32F427 running with FreeRTOS. I try to create a file on my SD card, through SDIO, thestm32f4_discovery_sdio_sd.c (DMA mode). I also use the librairyFatFs R0.09, ported by Clive I think :)

This is my code to write a file:

FRESULT res;
FILINFO fno;
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;
#endif
void
testsdcard(
void
)
{
// 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);
f_close(&fil);

}

Of course before I configured the NVIC:

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

}

The behavior seems random. - sometimes it works, all of the ''res'' returns are OK and the file is created on the SD card - sometimesf_open returns ''FR_NOT_READY'' andf_write returns ''FR_INVALID_OBJECT'' Do you have any idea ? It's hard to debug because of the random behavior... Thanks !!
    This topic has been closed for replies.

    20 replies

    chen
    Associate II
    April 16, 2014
    Posted on April 16, 2014 at 13:40

    Hi

    '' I also use the librairy FatFs R0.09, ported by Clive I think''

    This makes life difficult (unless you have the reference manual/documentation for that version).

    According to the WEB site :

    http://elm-chan.org/fsw/ff/00index_e.html

    Both the mount and the f_open command need the drive as par of the path.

    (but that is for the current version of fatfs)

    frankmeyer9
    Associate III
    April 16, 2014
    Posted on April 16, 2014 at 14:38

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

     

    Instrumentation.

    Add debug output to the code path's involved in fails, and perhaps add test code (open/close file in a loop to provoke the problem).

    If necessary, drill down to the lower-level functions, until the fail-generating code is found.

    Tesla DeLorean
    Guru
    April 16, 2014
    Posted on April 16, 2014 at 17:12

    Make sure you start with the most current port, writing tends to be more problematic and involves the SDIO FIFO. Probably want both the SDIO and DMA interrupts.

    This is the STM32F4-DISCO + STM32F4-DIS-BB implementation using the then current F4 SDIO code.

    https://drive.google.com/file/d/0B7OY5pub_GfIY01DaHY4OVp4NUk/edit?usp=sharing

    Tips, Buy me a coffee, or three.. PayPal Venmo (See Profile) Up vote any posts that you find helpful, it shows what's working..
    jean_prieur
    Associate III
    May 6, 2014
    Posted on May 06, 2014 at 11:41

    Thanks everybody ! I find a new STM32F4 SDIO driver for my project, it's very easy to implement : the stm32_ub_fatfs

    http://www.coocox.org/driver_comp/fatfs_sd-for-stf4-c1670.html?mc=3&sc=13

    jean_prieur
    Associate III
    May 6, 2014
    Posted on May 06, 2014 at 16:02

    I have two other questions:

    - Do you know how I can create files in lowercase ? 

    - Why the maximum file or directory name length is 8 characters ?

    Thanks !
    chen
    Associate II
    May 6, 2014
    Posted on May 06, 2014 at 16:30

    Hi

    ''- Do you know how I can create files in lowercase ? 

    - Why the maximum file or directory name length is 8 characters ?''

    Sound like FatFS is accessing the device in Fat16 mode.

    These are limitations of old Fat16 (related to pre-Windows days of filing system).

    jean_prieur
    Associate III
    May 6, 2014
    Posted on May 06, 2014 at 18:31

    Yes but this is strange, my SD card is FAT32 formatted.

    Moreover, FatFs see my card as FAT32 (I can see it in the ff.c lib, the code passes through the ''case FS_FAT32'' and not through the ''case FS_FAT16''
    chen
    Associate II
    May 6, 2014
    Posted on May 06, 2014 at 19:09

    Hi

    There may be other compiler directive in FatFS

    It is all to do with 'Long Filenames' :

    http://stackoverflow.com/questions/14123302/fat32-set-long-filename-and-8-3-filename-separately

    Check if FatFS has separate functions (create and write) for long file name.

    Tesla DeLorean
    Guru
    May 6, 2014
    Posted on May 06, 2014 at 19:19

    Got nothing to do with FAT16/FAT32

    Tips, Buy me a coffee, or three.. PayPal Venmo (See Profile) Up vote any posts that you find helpful, it shows what's working..
    francescatodiego
    Associate III
    May 7, 2014
    Posted on May 07, 2014 at 09:01

    My fatfs version R0.10a work with long file names and upper/lower case

    tIn the ff.c haeder comment :

    / Nov 03,'09 R0.07e Separated out configuration options from ff.h to ffconf.h.

    / Fixed f_unlink() fails to remove a sub-directory on _FS_RPATH.

    / Fixed name matching error on the 13 character boundary.

    / Added a configuration option, _LFN_UNICODE.

    / Changed f_readdir() to return the SFN with always upper case on non-LFN cfg.

    Configure the _USE_LFN define in ffconf.h file