Adapting FatFs Generic File System Module for CodeVision

ChaN has published a pretty amazing generic FAT file system - as he says FatFs is a generic FAT file system module for small embedded systems. The FatFs is written in compliance with ANSI C and completely separated from the disk I/O layer. Therefore it is independent of hardware architecture. It can be incorporated into low cost microcontrollers, such as AVR, 8051, PIC, ARM, Z80, 68k and etc..., without any change.

Here are a few (unessential) tweaks that I use with the CodeVisionAVR V2 compiler.

Tweaking Notes

These notes apply to the current (6-79-2011) version of FsFAT (R0.09) and CodeVisionAVR (V2.05.3a). They only cover changes to the ff.h and ff.c files as the other files are highly dependant on the hardware you use.

Changes to ff.h:

These changes and additions look line this:

… line 25ish #include <integer.h> /* Basic integer types */ #include "ffconf.h" /* FatFs configuration options */ #include <ctype.h> #include <string.h> … line 340ish #pragma library ff.lib #ifdef __cplusplus } #endif #endif /* _FATFS */

Changes to ff.c:

These changes and additions look line this:

… line 90ish #include <ff.h> /* FatFs configurations and declarations */ #include <diskio.h> /* Declarations of low level disk I/O functions */ #ifdef _PROMOTE_CHAR_TO_INT_OFF_ #define PROMOTE_OFF #endif #pragma promotechar+ // added to ensure correct arithmetic in places … line 350ish #define IsUpper isupper #define IsLower islower #define IsDigit isdigit //#define IsUpper(c) (((c)>='A')&&((c)<='Z')) //#define IsLower(c) (((c)>='a')&&((c)<='z')) //#define IsDigit(c) (((c)>='0')&&((c)<='9')) … line 520ish #define mem_cpy memcpy #define mem_set memset #define mem_cmp memcmp #define chk_chr strchr #if 0 /* Copy memory to memory */ static void mem_cpy (void* dst, const void* src, UINT cnt) { … line 375ish return *str; } #endif … line 4000ish (actually end of file) #ifdef PROMOTE_OFF #pragma promotechar- #endif

Obviously these are just tweaks and most of them are not absolutely necessary:

Going slightly further…

I did a little more to suit myself that you might consider:

… line 1400ish #else /* Non LFN configuration */ if (/*!(dir[DIR_Attr] & AM_VOL) &&*/ !mem_cmp(dir, dj->fn, 11)) /* Is it a valid entry? */ break; #endif res = dir_next(dj, 0); /* Next entry */ … line 1450ish #else /* Non LFN configuration */ if (c != 0xE5 && (_FS_RPATH || c != '.') /* && !(dir[DIR_Attr] & AM_VOL)*/) /* Is it a valid entry? */ break; #endif res = dir_next(dj, 0); /* Next entry */ if (res != FR_OK) break; }

Afterword

I have only tested these changes in a limited fashion - for example I have not used long filenames. However, I have used the module with CodeVisionAVR with some pretty large projects (by my standards) with complete success.

The FatFs module is not mine - all credit and honour goes to ChaN.