dd

Dependencies:   C12832 LM75B mbed

Committer:
pfe
Date:
Tue Apr 21 10:16:20 2015 +0000
Revision:
0:05a20e3e3179
dd

Who changed what in which revision?

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