Versión de Firmware con funciones de RAM incorporadas.

Dependencies:   mbed

Fork of VmRecorderV1dot1 by virtualmech

Committer:
JuanManuelAmador
Date:
Mon Jun 15 15:34:27 2015 +0000
Revision:
0:3d456b8ce449
prueba

Who changed what in which revision?

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