Skip to main content
jean_prieur
Associate III
Apr 16, 2014 10:36 AM UTC
Question

Problem with file creating on SD card with FatFs lib

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

    jean_prieur
    Associate III
    May 7, 2014 1:46 PM UTC
    Posted on May 07, 2014 at 15:46

    Thanks. I'm not sure to understand this _LFN_UNICODE and _USE_LFN.

    In my code:

    _LFN_UNICODE = 0 (

    0

    =

    ANSI/OEM

     and 

    1

    =

    Unicode

    )

    _USE_LFN = 0 (

    0

    =

    Disable LFN feature. _MAX_LFN and _LFN_UNICODE have no effect.

     1= Enable LFN)

    Is it the same for you ?

    Thanks a lot !

    jean_prieur
    Associate III
    May 7, 2014 1:53 PM UTC
    Posted on May 07, 2014 at 15:53

    If I configure_USE_LFN to =1 I need ff_convert() and ff_wtoupper() functions:

    // The LFN working buffer occupies (_MAX_LFN + 1) * 2 bytes. To enable LFN,
    // Unicode handling functions ff_convert() and ff_wtoupper() must be added
    // to the project. When enable to use heap, memory control functions
    // ff_memalloc() and ff_memfree() must be added to the project. */

    But I'm unable to find these functions in my project. What I have to do ?
    francescatodiego
    Associate III
    May 7, 2014 2:15 PM UTC
    Posted on May 07, 2014 at 16:15

    I'm sorry jean but

    I have

    alzheimer

    ....

    I totally forgot

    You must include

    the file

    in the directory

    option

    according to the

    chosen

    code page

    (Which

    is always located

    in the file

    ffconf.h

    ) My project work with

    _CODE_PAGE 1252

    and include ccsbcs.c from option directory

    Sorry again

    I hope

    I've given you

    all the details

    I have added my ffconf.h see attachment

    ________________

    Attachments :

    ffconf.h : https://st--c.eu10.content.force.com/sfc/dist/version/download/?oid=00Db0000000YtG6&ids=0680X000006I14B&d=%2Fa%2F0X0000000bhh%2F7EY_H7YLydopDRRIZamrBZP0iLJBg0WY8pC0RTkP9_o&asPdf=false
    chen
    Associate II
    May 7, 2014 2:20 PM UTC
    Posted on May 07, 2014 at 16:20

    Hi

    LFN = Long File Names.

    ''If I configure _USE_LFN to =1 I need  ff_convert() and ff_wtoupper() functions:''

    See :

    http://elm-chan.org/fsw/ff/en/appnote.html

    So the convert function does some of the convertion functions from wchar.h

    http://www.cplusplus.com/reference/cwchar/

    jean_prieur
    Associate III
    May 7, 2014 3:04 PM UTC
    Posted on May 07, 2014 at 17:04

    Hi Francescato,

    Thanks for this comment. I try to put the ccsbcs.c file in my project, but it seems i also need to import 

    pgmspace.h and maybe others...

    This is a lot of functions and new libs just to write in lowercase and >8 characters isn't it ?

    Do you think there is a proper way ? thanks !

    francescatodiego
    Associate III
    May 8, 2014 5:56 AM UTC
    Posted on May 08, 2014 at 07:56

    Jean

    I'm not sure

    that

    is the right way

    I

    just

    described

    what

    I did to

    use long names

    I'm not

    a

    FATFS

    expert

    but I'm sure

     , as the

    comment

    in

    ffconf.h

    ,

    _USE_LFN

    must be

    !

    =

    0 to enable

    long file name

    I do not know

    the file

    pgmspace.h

    I tried

    google

    and

    I have found only

    references to

    Arduino/Atmel AVR

    .

    What

    compiler

    do you use?

    I work with

    IAR

    and the file

    is not part

    of my project

    .

    jean_prieur
    Associate III
    May 8, 2014 10:12 AM UTC
    Posted on May 08, 2014 at 12:12

    I use Coocox, 

    pgmspace.h is also not a part of my project.

    I understand that I need 

    _USE_LFN

     > 0 to enable long file name and lowercase, and thus I need ff_convert() and ff_wtoupper() functions which are not included in my project.

    Do you use these two functions ?

    Thanks a lot !

    chen
    Associate II
    May 8, 2014 10:28 AM UTC
    Posted on May 08, 2014 at 12:28

    Chan's app note :

    http://elm-chan.org/fsw/ff/en/appnote.html

    ''To enable LFN feature, set _USE_LFN to 1, 2 or 3, and add a Unicode code conversion function ff_convert() and ff_wtoupper() to the project.''

    I doubt any environment provides ff_convert() and ff_wtoupper() - these are specific to FatFS BUT they can be just wrappers around other functions that may already exist!

    francescatodiego
    Associate III
    May 8, 2014 11:31 AM UTC
    Posted on May 08, 2014 at 13:31

    In my project

    I've also included

    the

    ccsbcs.c

    file

    (attached)

    It contains some

    code tables

    and

    the definition of

    WCHAR

    ff_convert

    (/ *

    Convert

    character

    , it returns

    zero

    in case of error

    *

    /

    WCHAR

    chr

    , / *

    Character code

    to be converted

    *

    /

    UINT

    dir /

    *

    0:

    Unicode

    OEMCP

    , 1:

    OEMCP

    to Unicode

    *

    /

    )

    WCHAR

    ff_wtoupper

    (/ *

    High

    converting the

    character

    *

    /

    WCHAR

    chr

    / *

    character

    input

    *

    /

    )

    ccsbcs.c

    is part of

    FATFS

    .

    zip

    (directory

    src.option

    ) downloaded from FATFS site

    The directory src.option

    contains several files

    where

    the functions

    ff_convert

    and

    ff_wtoupper

    are defined.

    I think

    the file you

    choose

    must be consistent

    with the selection of

    _CODE_PAGE

    in

    file

    ffconf.h

    ________________

    Attachments :

    ccsbcs.c : https://st--c.eu10.content.force.com/sfc/dist/version/download/?oid=00Db0000000YtG6&ids=0680X000006I146&d=%2Fa%2F0X0000000bhg%2FDAgD8FtdQt9M7u9Y4lnaOShhzBC0qIsbAKLnfat6Jd0&asPdf=false
    jean_prieur
    Associate III
    May 8, 2014 1:36 PM UTC
    Posted on May 08, 2014 at 15:36

    Thanks everybody, my problem is solved !

    In fact, I didn't use the right ccsbcs.c file

    Now I use the one from FatFs site and everything seems working, I can write in lowercase !