AnsweredAssumed Answered

How to enable "Long File Name" support in FAT file system included with LPCOpen?

Question asked by LPCware Support on Mar 30, 2016

Summary

LPCOpen uses ChanFatFS to support FAT File system in examples like SDMMC and other examples that needs file system support. The package is supplied with the default configuration (without any modifications to the configuration files). If users want to modify any configuration parameters it is recommended to consult the documentation pages at this link to understand how a configuration change might affect the behavior of the filesystem. Moreover LFN is a patent owned by Microsoft and may be that is the reason elm-chan.org decided not to enable it in the default configuration. If users want to enable unicode support they can read the documents here to know information on how to do that.

The documentation provided here provides information about enabling the LFN support. According to this document to enable LFN the user must implement the functions ff_convertand ff_wtoupper these functions could be found inside (software\filesystems\fatfs\src\ccsbcs.c) of the LPCOpen package. Adding it to the projects that uses FATFS or implementing these functions inside the application/example area can get the first step of LFN support done.

Enabling Long File Name support

The following steps will provide details on how to enable the long file name support in LPCOpen projects that uses FAT Filesystem

Implement a simple conversion function [sample code given below could be used as is] (This will be helpful for users that does not use UNICODE, and want to reduce the size of the generated binary)

 

WCHAR ff_convert (WCHAR wch, UINT dir) 
{
          if (wch < 0x80) {
                    /* ASCII Char */
                    return wch;
          } 

          /* I don't support unicode it is too big! */
          return 0;


WCHAR ff_wtoupper (WCHAR wch)
{
          if (wch < 0x80) {     
                    /* ASCII Char */
                    if (wch >= 'a' && wch <= 'z') {
                              wch &= ~0x20;
                     }
                      return wch;
          } 

          /* I don't support unicode it is too big! */
          return 0;
}

Enable the LFN support by (defining _USE_LFN as 1 in software\filesystems\fatfs\src\ffconf.h, It is recommended to leave _LFN_UNICODE as the default 0) [Unless you want unicode filename support]

#define     _USE_LFN     1 

That is it, now the following should work fine

f_open(&Fil, "New_Text_Document.html", FA_READ); 

 

Listing files that does not follow 8.3 file name format

 

The following code segment will show how to list files with long file names

static TCHAR lfname[_MAX_LFN]; 
 result = f_opendir(&dir, ""); 
/* Check result for errors */

printf("Directory listing...\r\n");
for (;; ) {
        fno.lfname = lfname;
        fno.lfsize = _MAX_LFN - 1;
        /* Read a directory item */
        rc = f_readdir(&dir, &fno);
        if (rc || !fno.fname[0]) {
               break;                         /* Error or end of dir */
        } if (fno.fattrib & AM_DIR) {
                  sprintf(debugBuf, "   [dir]  %s\r\n", fno.lfname[0] ? fno.lfname : fno.fname);
        }
        else {
                  sprintf(debugBuf, "   %8lu  %s\r\n", fno.fsize, fno.lfname[0] ? fno.lfname : fno.fname);
        }
        printf(debugBuf);
}

 

 

Outcomes