This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

FATfs basics

Other Parts Discussed in Thread: SYSBIOS

i understand that FATFs is a 3rd party, open source resource but i also know that TI will likely handle it a little differently

i have a project that i am running on the LCDK, and i have it based on the MMCSD FATfs project. i am afraid the documentation doesn't fill me in like i would prefer.

my first question is: how does PSP_blkmediaFATfsRegister(MMC_INST_ID, PSP_MMCSD_SAMPLE_DRIVE); register the FATFS system with the SD card? in the 6748 is the SD card ALWAYS driver ID 1? is the "drive" number always 1 for the CD card or can that be anything? 

i am trying to figure out how to create a directory, and more importantly, how the directory structure works on this.

my first question has to do with directory structure.  when i add in the string to access a file how does it look? i understand it has to start with "fat:"drive number": but after that do i use slashes? i saw that you should use "\\" to prevent special characters from making it in. if i want to create a folder "folder" and i made a file called "file.txt" would it look like "fat:1:\\folder\\file.txt"? 

 if i stick the SD card in a windows 7 computer would it recognize the file system?

so after the format i am trying to make a directory: f_mkdir("fat:1:\\folder") i also tried f_mkdir("folder") and this always results in a return value of 12, which translates to FR_NOT_ENABLED which means "Work area for the logical drive has not been registered by f_mount function" (this was quite the pain to find by the way).  so i seem to need to do an f_mount on my SD card, right?  i guess i would have imagined that registering the CD card with the FATFS file system would have done this for me (since in the help for FATFS it says it "registers" the drive to a mount # which i thought it already did). so this leads to the question of what is a "FATFS *" type?  i am guessing it is a pointer to a FATFS file system root.  do i have to create one? is that a file global variable i have to keep track of?  once i create one do i have to create one every time i boot my project? is this a value that i keep in nonvolatile memory?  can i "find" a FATFS once i make one or does registering a drive find one that was already there once before? if i don't need to make one and PSP_blkmediaFATfsRegister does the register for me, what am i doing wrong to make a directory?

i am sure i will have more questions once i get further into it.

  • new questions to add:

    looking at the API i see there is a way to get the remaining free space from the FATfs system in terms of clusters.  that API requires a FATFS * parameter.  i have no idea how i am supposed to get that variable out of the system. it seems the routine PSP_blkmediaFATfsRegister would have to use such a variable but i have no idea how to get at it. 

    next question: can i create multiple partitions on my SD card or am i limited to one partition per device?

  • Hi cobsonchael,

    cobsonchael said:
    in the 6748 is the SD card ALWAYS driver ID 1? is the "drive" number always 1 for the CD card or can that be anything? 

    Not necessary. It can be anything between 0 and 3. i.e, Drive id - 0,1,2 or 3.

    cobsonchael said:
    when i add in the string to access a file how does it look? i understand it has to start with "fat:"drive number": but after that do i use slashes? i saw that you should use "\\" to prevent special characters from making it in. if i want to create a folder "folder" and i made a file called "file.txt" would it look like "fat:1:\\folder\\file.txt"? 

    If you are using drive id: 1, and wish to create a file inside a directory/folder, it'll look like - 1:\\folder\\file.txt 

    cobsonchael said:
    so after the format i am trying to make a directory: f_mkdir("fat:1:\\folder") i also tried f_mkdir("folder") and this always results in a return value of 12

    For Ex: To create a file(file.txt) in directory(folder) -

    /***************************************************************************/

    NOTE: This is just a sample code snippet for basic understanding purpose

    FIL   stFilePtr;

    retVal = f_mkdir("1:\\folder"); and then,

    retVal = f_open(&stFilePtr, "1:\\folder\\file.txt", (FA_WRITE | FA_READ | FA_CREATE_ALWAYS));

    /*  To Close the file after creation */

    retVal = f_close(&stFilePtr);

    /**************************************************************************/

    If you are using drive id - '0' then you can simply do a f_mkdir("folder").

    cobsonchael said:
     if i stick the SD card in a windows 7 computer would it recognize the file system?

    Yes, it should be able to recognize. Infact, the files created will be visible. 

    Hope this helps..

    Best Regards,

    Raghavendra

  • it helps some. i still have the problem where i cannot create a directory and i cannot make any special partitions because  i do not have the FATFS * variable available to me.  i cannot make a directory because the drive was not registered, so it appears i have to do an f_mount, but i cannot do an f_mount without the FATFS pointer, but i cannot get to the FATFS pointer because it is not provided to me by the driver (i would imagine the driver would have to have this variable because how else would you register it if you do not have a FATFS pointer?). it is quite the chain of events.

    so how do i get access to the FATFS pointer? if one does not exist how do i make this work using the drivers if the drivers have to register the SD card with FATFS but then i have to register it again with f_mount in order to create a directory?

  • cobonchael,

    If you use the BIOS PSP mmcsd_fatfs application, and follow the steps mentioned in my previous post you should be able to create the directory and also create files in it.

    If you refer the "C6748_BIOSPSP_Userguide" placed in the top level docs folder of the BIOSPSP package, section 10.6 explains the use of a Block media driver for File system interface. "Block media driver is an interface layer between FATFS and low level device driver for storage" and "The registration of a storage driver to the file system is take care by the Block media driver". Use the API - PSP_blkmediaFATfsRegister(..) to register the storage driver with the FAT File system which mounts the requested logical drive. The Block media driver, maintains the file system object(FATFS) for every drive that is mounted. Check for the "PSP_BlkDiskIOReg_t" structure in blkmedia.

    Best Regards,

    Raghavendra

  • yes i see that now. i was still adding the "fat:" to my make directory. it does work now.

    so does that mean that i use PSP_blkmediaFATfsRegister to make multiple partitions somehow if i wish to make multiple partitions? it would seem that in the f_fdisk code example it requires an f_mount to occur, which requires a FATFS pointer. or do i use the f_fdisk and then PSP_blkmediaFATfsRegister each partition instead of f_mount?

    also f_getfree  requires a pointer to a pointer of a FATFS variable.  how do i figure out how much space is left in the file system?

  • Hi Cobsonchael,

    cobsonchael said:
    so does that mean that i use PSP_blkmediaFATfsRegister to make multiple partitions somehow if i wish to make multiple partitions?

    To have FatFs support multiple partitions you need modify SYS/BIOS' FatFs module and then rebuild SYS/BIOS.

    Inside the SYS/BIOS product/packages/ directory you'd be looking for ti/sysbios/fatfs/ffconf.h. There is a #define statement for _MULTI_PARTITION which needs to be updated with the number of logical partitions you want. We leave this as 0 by default. If you choose to enable it, you will also have to also supply a VolToPart[] array of type PARTITION. This is defined in ff.h. Each element of this array will associate a drive number with a partition number.

    At that point, when you use a drive number with your f_open (FatFs) API, this gets indexed into the array of structures and will allow FatFs to identify the physical drive and partition number. pd is used to chose which FatFs driver is used, whereas pt is used by the FatFs to identify the logical partition.

    #if _MULTI_PARTITION            /* Multiple partition configuration */
    #define LD2PD(vol) (VolToPart[vol].pd)  /* Get physical drive# */
    #define LD2PT(vol) (VolToPart[vol].pt)  /* Get partition# */
    typedef struct {
            BYTE pd;        /* Physical drive# */
            BYTE pt;        /* Partition # (0-3) */
    } PARTITION;
    extern const PARTITION VolToPart[];     /* Volume - Physical location resolution table */

    #else                           /* Single partition configuration */
    #define LD2PD(vol) (vol)        /* Logical drive# is bound to the same physical drive# */
    #define LD2PT(vol) 0            /* Always mounts the 1st partition */

    #endif

    As sample table is shown in the FatFs API docs: here.

    cobsonchael said:
    it would seem that in the f_fdisk code example it requires an f_mount to occur, which requires a FATFS pointer. or do i use the f_fdisk and then PSP_blkmediaFATfsRegister each partition instead of f_mount?

    At the moment, SYS/BIOS uses FatFs R0.08a and doesn't support f_fdisk. f_fdisk has been added as of FatFs R0.09.

    cobsonchael said:
    _getfree  requires a pointer to a pointer of a FATFS variable.  how do i figure out how much space is left in the file system?

    Unfortunately, since the FATFS variable is static you can't access it. Perhaps a future API could be added in upcoming releases to calculate the free space or just an API that could provide the address to the FATFS pointer. For now you could modify the source code to provide a function that returns the value of the FATFS pointer. However, from that point on you would have your custom build of the blkmedia driver.

  • ok, i guess i could forgive not allowing multiple partitions right out of the box....but you guys developed drivers for a file system and you did not include a way to check the free space?  

    i know the FATfs support is new to your tools but come on!

    I hope i am wrong and that you did include in your tools some way to calculate free space.  please tell me how i can check my free space using the PSP drivers as they exist today?

  • Hi cobsonchael,

    Sorry for the delayed response.

    As of today, there is no facility to calculate free space using the PSP drivers. As a workaround(for time being) we have implemented an API to calculate free space using the f_getfree(...).

    If you refer to the FATfS documentation, there is an API “f_getfree(…)” which can be used in calculating the free space available on a drive. Now since, the FATFS pointer is not directly accessible from the application, we can call this API from the blkmedia layer.

    Attached is a code snippet - 6735.codeSnippet.txt

    You should place it(code snippet) in the blkmedia driver(blkmedia.c file), and can call this API(PSP_blkmediaFATfsFreeSpace(…)) from the application.

    The PSP_blkmediaFATfsFreeSpace(…) API returns the free drive space available in KB.

     

    NOTE: The Blkmedia driver has to be rebuilt.

     

    Best Regards,

    Raghavendra

  • I found that the code snippet is way more work than necessary. if you simply do the FATFS command "f_readdir" on any directory in the file system you get a DIR structure filled. that DIR structure includes the FATFS * that i need and i can simply use that as input to the "f_getfree" command or i can read it directly from the FATFS structure under the "free_clust" field and the "csize" field to calculate the free space.