FAT File Server - fatfs
#include <device.h>
#include
<fsysinit.h>
uint_16 fsi_msgsize; uint_16 fsi_nclones; /* Number of servers to create, not used for FATFS */ uint_32 fsi_datalog; /* 1 - enable xprintf output for debug purpose, 0 - disable */ uint_16 fsi_nbuffers; /* Number of buffers in buffer cache, minimum 2, not used for FATFS*/ uint_16 fsi_blocksize; /* Memory block size: 256 for RAM-disc, 512 for sd/mmc-card, not used for FATFS */ uint_16 fsi_maxopen; /* Max number of open files */ uint_16 fsi_ndrives; /* Maximum number of allocated disk, valid for FATFS only*/ };
The fat file server, fatfs provides fast fat based, windows fat compatible file system. It is fully compatible with the multimedia file system and can be used to replace the multimedia file system with a FAT compatible file system for use with SD cards, uSD cards, MMC cards, and USB data sticks. It supports the following operations:
Typical operation for the fat file server involves several steps common in various operating system environments including Linux.
Create a fat file server first so that commands can be carried out for the selected media. Use pthread_create to do this.
Register the fat file server so that it can be found as a server in the system.
Make a file system using mkfs. This creates a root directory structure on the media (doing device formatting, all information on media will be erased). It uses the optimal FAT 12, 16 or 32 format depending on the size of the media. This step is optional, and used only if need to create file system at new media. If media already has FAT, then this step could be avoided.
Mount the file system. Note that an extra directory or a root directory is specified in addition to the registered file server name.
The file system is now ready to use with the remaining file system commands.
Compile Options
The following compile time options may be used. Additional file compile time options do exist but should not be used without consulting the factory.
Run Time Options
Initialization Options
Open Options
IOCTL Options
For more details, see functions description in I/O Library.
In case of successful start up pthread_create will return 0 value.
If any of the following conditions occur, the function call shall return -1 and set errno to the corresponding POSIX compatible error code.
struct fsysinit {
uint_16 fsi_msgsize;
uint_16 fsi_nclones; /* Number of servers to create. */
uint_32 fsi_datalog; /* 1 - enable xprintf output for debug purpose, 0 - disable */
uint_16 fsi_nbuffers; /* Number of buffers in buffer cache, minimum 2*/
uint_16 fsi_blocksize; /* Memory block size: 256 for RAM-disc, 512 for sd/mmc-card */
uint_16 fsi_maxopen; /* Max number of open files */
uint_16 fsi_ndrives; /* Maximum number of allocated disk, valid for FATFS only*/
};
#define FATFS_DIRECTORY "/dev/sd"
#define FATFS_MOUNT FATFS_DIRECTORY "/d1"
...
/*
*
* FAT FS Initialization
*
*
*/
/* create the File server and mount disk */
fsysinit.fsi_nclones = 1; //should be at least 1
fsysinit.fsi_datalog = 1; //1 - enable xprintf output for debug purpose, 0 - disable
fsysinit.fsi_ndrives = 1; //maximum allowed disks, MAX = 9
// set up stack size and priority for main fsys threads and all clones
pthread_attr_init(&attr);
pthread_attr_setstacksize(&attr, FATFSSTACKSIZE);
myNewFsysPriority.sched_priority = FATFSPRIO;
pthread_attr_setschedparam(&attr, &myNewFsysPriority);
if(!pthread_create(&tid, &attr, (void*(*)(void*))_fatfs_server, (char*)&fsysinit))
{
if(!dir_register(FATFS_DIRECTORY, tid, TYPE_SERVER))
{
xprintf("..Unable to register name of File Server..\n");
xprintf("..Required resource not present - aborting..\n");
#if RTOS_DEBUG
dir_deregister("Main thread");
#endif
pthread_exit((void*)1);
}
}
/*preparing adding driver for SD/MMC/CF card disk*/
hw_fat_drv.disk_initialize = hw_disk_initialize;
hw_fat_drv.disk_ioctl = hw_disk_ioctl;
hw_fat_drv.disk_read = hw_disk_read;
hw_fat_drv.disk_write = hw_disk_write;
hw_fat_drv.disk_status = hw_disk_status;
hw_fat_drv.disk_timerproc = hw_disk_timerproc;
res = fatfs_add_drv(FATFS_DIRECTORY, &hw_fat_drv, &HW_DEVICE_NO); //add new disk driver to driver list structure //return HW_DEVICE_NO - local disk driver num
#ifdef MAKE_DISK_FORMATTING
//mkfs will create FAT system at specified HW_DEVICE_NO.
//FAT type will be automatically chooses, depending of flash drive capacity
mkfs_status = mkfs(FATFS_DIRECTORY, HW_DEVICE_NO, 0, 0);
#endif
mount(HW_DEVICE_NO, FATFS_MOUNT, 0);
//Now FATFS ready for usage.
...
****** Hardware dependent low level driver functions ******
//Initialize Disk Drive
DSTATUS hw_disk_initialize (void);
//Get Disk Status
DSTATUS hw_disk_status ();
//Read Sector(s)
DRESULT hw_disk_read (BYTE *,DWORD , BYTE );
//Write Sector(s)
DRESULT hw_disk_write (const BYTE *, DWORD, BYTE );
//Functions to obtain disc parameters -
//GET_SECTOR_COUNT - Get number of sectors on the disk
//GET_SECTOR_SIZE - Get sector size (WORD)
//From these values FATFS know capacity of drive.
DRESULT hw_disk_ioctl (BYTE , void *);
//typically used for SD cards to automatically detect card insert/eject and write protect.
void hw_disk_timerproc (void);
See demo examples for more details
chmod(), close(), lseek(),mkdir(), mkfs(), mount(), open(), read(), stat(), write(), fsys,