First working version of a FATFileSystem compatible Chan FAT v0.8 implementation. This is intended to use with long file names and RTOS support. For now long file names work but RTOS support is still untested.

Dependents:   USB_MSC USB_CDC_MSD_Hello TFTPServerTest DMXStation ... more

Committer:
NeoBelerophon
Date:
Fri Feb 04 21:14:33 2011 +0000
Revision:
2:629e4be333f3
Parent:
0:8ea634413549
getdir() did not work -> remove

Who changed what in which revision?

UserRevisionLine numberNew contents of line
NeoBelerophon 0:8ea634413549 1 /* mbed Microcontroller Library - FATFileSystem
NeoBelerophon 0:8ea634413549 2 * Copyright (c) 2008, sford
NeoBelerophon 0:8ea634413549 3 */
NeoBelerophon 0:8ea634413549 4
NeoBelerophon 0:8ea634413549 5 #include "FATFileSystem.h"
NeoBelerophon 0:8ea634413549 6
NeoBelerophon 0:8ea634413549 7 #include "mbed.h"
NeoBelerophon 0:8ea634413549 8
NeoBelerophon 0:8ea634413549 9 #include "FileSystemLike.h"
NeoBelerophon 0:8ea634413549 10 #include "FATFileHandle.h"
NeoBelerophon 0:8ea634413549 11 #include "FATDirHandle.h"
NeoBelerophon 0:8ea634413549 12 #include "ff.h"
NeoBelerophon 0:8ea634413549 13 //#include "Debug.h"
NeoBelerophon 0:8ea634413549 14 #include <stdio.h>
NeoBelerophon 0:8ea634413549 15 #include <stdlib.h>
NeoBelerophon 0:8ea634413549 16
NeoBelerophon 0:8ea634413549 17 DWORD get_fattime (void) {
NeoBelerophon 0:8ea634413549 18 return 999;
NeoBelerophon 0:8ea634413549 19 }
NeoBelerophon 0:8ea634413549 20
NeoBelerophon 0:8ea634413549 21 namespace mbed {
NeoBelerophon 0:8ea634413549 22
NeoBelerophon 0:8ea634413549 23 #if FFSDEBUG_ENABLED
NeoBelerophon 0:8ea634413549 24 static const char *FR_ERRORS[] = {
NeoBelerophon 0:8ea634413549 25 "FR_OK = 0",
NeoBelerophon 0:8ea634413549 26 "FR_NOT_READY",
NeoBelerophon 0:8ea634413549 27 "FR_NO_FILE",
NeoBelerophon 0:8ea634413549 28 "FR_NO_PATH",
NeoBelerophon 0:8ea634413549 29 "FR_INVALID_NAME",
NeoBelerophon 0:8ea634413549 30 "FR_INVALID_DRIVE",
NeoBelerophon 0:8ea634413549 31 "FR_DENIED",
NeoBelerophon 0:8ea634413549 32 "FR_EXIST",
NeoBelerophon 0:8ea634413549 33 "FR_RW_ERROR",
NeoBelerophon 0:8ea634413549 34 "FR_WRITE_PROTECTED",
NeoBelerophon 0:8ea634413549 35 "FR_NOT_ENABLED",
NeoBelerophon 0:8ea634413549 36 "FR_NO_FILESYSTEM",
NeoBelerophon 0:8ea634413549 37 "FR_INVALID_OBJECT",
NeoBelerophon 0:8ea634413549 38 "FR_MKFS_ABORTED"
NeoBelerophon 0:8ea634413549 39 };
NeoBelerophon 0:8ea634413549 40 #endif
NeoBelerophon 0:8ea634413549 41
NeoBelerophon 0:8ea634413549 42 FATFileSystem *FATFileSystem::_ffs[_DRIVES] = {0};
NeoBelerophon 0:8ea634413549 43
NeoBelerophon 0:8ea634413549 44 FATFileSystem::FATFileSystem(const char* n) : FileSystemLike(n) {
NeoBelerophon 0:8ea634413549 45 FFSDEBUG("FATFileSystem(%s)\n", n);
NeoBelerophon 0:8ea634413549 46 for(int i=0; i<_DRIVES; i++) {
NeoBelerophon 0:8ea634413549 47 if(_ffs[i] == 0) {
NeoBelerophon 0:8ea634413549 48 _ffs[i] = this;
NeoBelerophon 0:8ea634413549 49 _fsid = i;
NeoBelerophon 0:8ea634413549 50 FFSDEBUG("Mounting [%s] on ffs drive [%d]\n", _name, _fsid);
NeoBelerophon 0:8ea634413549 51 f_mount(i, &_fs);
NeoBelerophon 0:8ea634413549 52 return;
NeoBelerophon 0:8ea634413549 53 }
NeoBelerophon 0:8ea634413549 54 }
NeoBelerophon 0:8ea634413549 55 error("Couldn't create %s in FATFileSystem::FATFileSystem\n",n);
NeoBelerophon 0:8ea634413549 56 }
NeoBelerophon 0:8ea634413549 57
NeoBelerophon 0:8ea634413549 58 FATFileSystem::~FATFileSystem() {
NeoBelerophon 0:8ea634413549 59 for(int i=0; i<_DRIVES; i++) {
NeoBelerophon 0:8ea634413549 60 if(_ffs[i] == this) {
NeoBelerophon 0:8ea634413549 61 _ffs[i] = 0;
NeoBelerophon 0:8ea634413549 62 f_mount(i, NULL);
NeoBelerophon 0:8ea634413549 63 }
NeoBelerophon 0:8ea634413549 64 }
NeoBelerophon 0:8ea634413549 65 }
NeoBelerophon 0:8ea634413549 66
NeoBelerophon 0:8ea634413549 67 FileHandle *FATFileSystem::open(const char* name, int flags) {
NeoBelerophon 0:8ea634413549 68 FFSDEBUG("open(%s) on filesystem [%s], drv [%d]\n", name, _name, _fsid);
NeoBelerophon 0:8ea634413549 69 char n[64];
NeoBelerophon 0:8ea634413549 70 sprintf(n, "%d:/%s", _fsid, name);
NeoBelerophon 0:8ea634413549 71
NeoBelerophon 0:8ea634413549 72 /* POSIX flags -> FatFS open mode */
NeoBelerophon 0:8ea634413549 73 BYTE openmode;
NeoBelerophon 0:8ea634413549 74 if(flags & O_RDWR) {
NeoBelerophon 0:8ea634413549 75 openmode = FA_READ|FA_WRITE;
NeoBelerophon 0:8ea634413549 76 } else if(flags & O_WRONLY) {
NeoBelerophon 0:8ea634413549 77 openmode = FA_WRITE;
NeoBelerophon 0:8ea634413549 78 } else {
NeoBelerophon 0:8ea634413549 79 openmode = FA_READ;
NeoBelerophon 0:8ea634413549 80 }
NeoBelerophon 0:8ea634413549 81 if(flags & O_CREAT) {
NeoBelerophon 0:8ea634413549 82 if(flags & O_TRUNC) {
NeoBelerophon 0:8ea634413549 83 openmode |= FA_CREATE_ALWAYS;
NeoBelerophon 0:8ea634413549 84 } else {
NeoBelerophon 0:8ea634413549 85 openmode |= FA_OPEN_ALWAYS;
NeoBelerophon 0:8ea634413549 86 }
NeoBelerophon 0:8ea634413549 87 }
NeoBelerophon 0:8ea634413549 88
NeoBelerophon 0:8ea634413549 89 FIL_t fh;
NeoBelerophon 0:8ea634413549 90 FRESULT res = f_open(&fh, n, openmode);
NeoBelerophon 0:8ea634413549 91 if(res) {
NeoBelerophon 0:8ea634413549 92 FFSDEBUG("f_open('w') failed (%d, %s)\n", res, FR_ERRORS[res]);
NeoBelerophon 0:8ea634413549 93 return NULL;
NeoBelerophon 0:8ea634413549 94 }
NeoBelerophon 0:8ea634413549 95 if(flags & O_APPEND) {
NeoBelerophon 0:8ea634413549 96 f_lseek(&fh, fh.fsize);
NeoBelerophon 0:8ea634413549 97 }
NeoBelerophon 0:8ea634413549 98 return new FATFileHandle(fh);
NeoBelerophon 0:8ea634413549 99 }
NeoBelerophon 0:8ea634413549 100
NeoBelerophon 0:8ea634413549 101 int FATFileSystem::remove(const char *filename) {
NeoBelerophon 0:8ea634413549 102 FRESULT res = f_unlink(filename);
NeoBelerophon 0:8ea634413549 103 if(res) {
NeoBelerophon 0:8ea634413549 104 FFSDEBUG("f_unlink() failed (%d, %s)\n", res, FR_ERRORS[res]);
NeoBelerophon 0:8ea634413549 105 return -1;
NeoBelerophon 0:8ea634413549 106 }
NeoBelerophon 0:8ea634413549 107 return 0;
NeoBelerophon 0:8ea634413549 108 }
NeoBelerophon 0:8ea634413549 109
NeoBelerophon 0:8ea634413549 110 int FATFileSystem::format() {
NeoBelerophon 0:8ea634413549 111 FFSDEBUG("format()\n");
NeoBelerophon 0:8ea634413549 112 FRESULT res = f_mkfs(_fsid, 0, 512); // Logical drive number, Partitioning rule, Allocation unit size (bytes per cluster)
NeoBelerophon 0:8ea634413549 113 if(res) {
NeoBelerophon 0:8ea634413549 114 FFSDEBUG("f_mkfs() failed (%d, %s)\n", res, FR_ERRORS[res]);
NeoBelerophon 0:8ea634413549 115 return -1;
NeoBelerophon 0:8ea634413549 116 }
NeoBelerophon 0:8ea634413549 117 return 0;
NeoBelerophon 0:8ea634413549 118 }
NeoBelerophon 0:8ea634413549 119
NeoBelerophon 0:8ea634413549 120 DirHandle *FATFileSystem::opendir(const char *name) {
NeoBelerophon 0:8ea634413549 121 DIR_t dir;
NeoBelerophon 0:8ea634413549 122 FRESULT res = f_opendir(&dir, name);
NeoBelerophon 0:8ea634413549 123 if(res != 0) {
NeoBelerophon 0:8ea634413549 124 return NULL;
NeoBelerophon 0:8ea634413549 125 }
NeoBelerophon 0:8ea634413549 126 return new FATDirHandle(dir);
NeoBelerophon 0:8ea634413549 127 }
NeoBelerophon 0:8ea634413549 128
NeoBelerophon 0:8ea634413549 129 int FATFileSystem::mkdir(const char *name, mode_t mode) {
NeoBelerophon 0:8ea634413549 130 FRESULT res = f_mkdir(name);
NeoBelerophon 0:8ea634413549 131 return res == 0 ? 0 : -1;
NeoBelerophon 0:8ea634413549 132 }
NeoBelerophon 0:8ea634413549 133
NeoBelerophon 0:8ea634413549 134 } // namespace mbed