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