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