FAT

Dependents:   SDFileSystem_Axelta

Committer:
gauthibit
Date:
Fri Nov 11 06:39:00 2016 +0000
Revision:
0:6547fbc0f7c0
FAT

Who changed what in which revision?

UserRevisionLine numberNew contents of line
gauthibit 0:6547fbc0f7c0 1 /* mbed Microcontroller Library
gauthibit 0:6547fbc0f7c0 2 * Copyright (c) 2006-2012 ARM Limited
gauthibit 0:6547fbc0f7c0 3 *
gauthibit 0:6547fbc0f7c0 4 * Permission is hereby granted, free of charge, to any person obtaining a copy
gauthibit 0:6547fbc0f7c0 5 * of this software and associated documentation files (the "Software"), to deal
gauthibit 0:6547fbc0f7c0 6 * in the Software without restriction, including without limitation the rights
gauthibit 0:6547fbc0f7c0 7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
gauthibit 0:6547fbc0f7c0 8 * copies of the Software, and to permit persons to whom the Software is
gauthibit 0:6547fbc0f7c0 9 * furnished to do so, subject to the following conditions:
gauthibit 0:6547fbc0f7c0 10 *
gauthibit 0:6547fbc0f7c0 11 * The above copyright notice and this permission notice shall be included in
gauthibit 0:6547fbc0f7c0 12 * all copies or substantial portions of the Software.
gauthibit 0:6547fbc0f7c0 13 *
gauthibit 0:6547fbc0f7c0 14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
gauthibit 0:6547fbc0f7c0 15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
gauthibit 0:6547fbc0f7c0 16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
gauthibit 0:6547fbc0f7c0 17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
gauthibit 0:6547fbc0f7c0 18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
gauthibit 0:6547fbc0f7c0 19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
gauthibit 0:6547fbc0f7c0 20 * SOFTWARE.
gauthibit 0:6547fbc0f7c0 21 */
gauthibit 0:6547fbc0f7c0 22 #include "mbed.h"
gauthibit 0:6547fbc0f7c0 23
gauthibit 0:6547fbc0f7c0 24 #include "ffconf.h"
gauthibit 0:6547fbc0f7c0 25 #include "mbed_debug.h"
gauthibit 0:6547fbc0f7c0 26
gauthibit 0:6547fbc0f7c0 27 #include "FATFileSystem.h"
gauthibit 0:6547fbc0f7c0 28 #include "FATFileHandle.h"
gauthibit 0:6547fbc0f7c0 29 #include "FATDirHandle.h"
gauthibit 0:6547fbc0f7c0 30
gauthibit 0:6547fbc0f7c0 31 DWORD get_fattime(void) {
gauthibit 0:6547fbc0f7c0 32 time_t rawtime;
gauthibit 0:6547fbc0f7c0 33 time(&rawtime);
gauthibit 0:6547fbc0f7c0 34 struct tm *ptm = localtime(&rawtime);
gauthibit 0:6547fbc0f7c0 35 return (DWORD)(ptm->tm_year - 80) << 25
gauthibit 0:6547fbc0f7c0 36 | (DWORD)(ptm->tm_mon + 1 ) << 21
gauthibit 0:6547fbc0f7c0 37 | (DWORD)(ptm->tm_mday ) << 16
gauthibit 0:6547fbc0f7c0 38 | (DWORD)(ptm->tm_hour ) << 11
gauthibit 0:6547fbc0f7c0 39 | (DWORD)(ptm->tm_min ) << 5
gauthibit 0:6547fbc0f7c0 40 | (DWORD)(ptm->tm_sec/2 );
gauthibit 0:6547fbc0f7c0 41 }
gauthibit 0:6547fbc0f7c0 42
gauthibit 0:6547fbc0f7c0 43 FATFileSystem *FATFileSystem::_ffs[_VOLUMES] = {0};
gauthibit 0:6547fbc0f7c0 44
gauthibit 0:6547fbc0f7c0 45 FATFileSystem::FATFileSystem(const char* n) : FileSystemLike(n) {
gauthibit 0:6547fbc0f7c0 46 debug_if(FFS_DBG, "FATFileSystem(%s)\n", n);
gauthibit 0:6547fbc0f7c0 47 for(int i=0; i<_VOLUMES; i++) {
gauthibit 0:6547fbc0f7c0 48 if(_ffs[i] == 0) {
gauthibit 0:6547fbc0f7c0 49 _ffs[i] = this;
gauthibit 0:6547fbc0f7c0 50 _fsid = i;
gauthibit 0:6547fbc0f7c0 51 // debug_if(FFS_DBG, "Mounting [%s] on ffs drive [%d]\n", _name, _fsid);
gauthibit 0:6547fbc0f7c0 52 f_mount(i, &_fs);
gauthibit 0:6547fbc0f7c0 53 return;
gauthibit 0:6547fbc0f7c0 54 }
gauthibit 0:6547fbc0f7c0 55 }
gauthibit 0:6547fbc0f7c0 56 error("Couldn't create %s in FATFileSystem::FATFileSystem\n", n);
gauthibit 0:6547fbc0f7c0 57 }
gauthibit 0:6547fbc0f7c0 58
gauthibit 0:6547fbc0f7c0 59 FATFileSystem::~FATFileSystem() {
gauthibit 0:6547fbc0f7c0 60 for (int i=0; i<_VOLUMES; i++) {
gauthibit 0:6547fbc0f7c0 61 if (_ffs[i] == this) {
gauthibit 0:6547fbc0f7c0 62 _ffs[i] = 0;
gauthibit 0:6547fbc0f7c0 63 f_mount(i, NULL);
gauthibit 0:6547fbc0f7c0 64 }
gauthibit 0:6547fbc0f7c0 65 }
gauthibit 0:6547fbc0f7c0 66 }
gauthibit 0:6547fbc0f7c0 67
gauthibit 0:6547fbc0f7c0 68 FileHandle *FATFileSystem::open(const char* name, int flags) {
gauthibit 0:6547fbc0f7c0 69 // debug_if(FFS_DBG, "open(%s) on filesystem [%s], drv [%d]\n", name, _name, _fsid);
gauthibit 0:6547fbc0f7c0 70 char n[64];
gauthibit 0:6547fbc0f7c0 71 sprintf(n, "%d:/%s", _fsid, name);
gauthibit 0:6547fbc0f7c0 72
gauthibit 0:6547fbc0f7c0 73 /* POSIX flags -> FatFS open mode */
gauthibit 0:6547fbc0f7c0 74 BYTE openmode;
gauthibit 0:6547fbc0f7c0 75 if (flags & O_RDWR) {
gauthibit 0:6547fbc0f7c0 76 openmode = FA_READ|FA_WRITE;
gauthibit 0:6547fbc0f7c0 77 } else if(flags & O_WRONLY) {
gauthibit 0:6547fbc0f7c0 78 openmode = FA_WRITE;
gauthibit 0:6547fbc0f7c0 79 } else {
gauthibit 0:6547fbc0f7c0 80 openmode = FA_READ;
gauthibit 0:6547fbc0f7c0 81 }
gauthibit 0:6547fbc0f7c0 82 if(flags & O_CREAT) {
gauthibit 0:6547fbc0f7c0 83 if(flags & O_TRUNC) {
gauthibit 0:6547fbc0f7c0 84 openmode |= FA_CREATE_ALWAYS;
gauthibit 0:6547fbc0f7c0 85 } else {
gauthibit 0:6547fbc0f7c0 86 openmode |= FA_OPEN_ALWAYS;
gauthibit 0:6547fbc0f7c0 87 }
gauthibit 0:6547fbc0f7c0 88 }
gauthibit 0:6547fbc0f7c0 89
gauthibit 0:6547fbc0f7c0 90 FIL fh;
gauthibit 0:6547fbc0f7c0 91 FRESULT res = f_open(&fh, n, openmode);
gauthibit 0:6547fbc0f7c0 92 if (res) {
gauthibit 0:6547fbc0f7c0 93 debug_if(FFS_DBG, "f_open('w') failed: %d\n", res);
gauthibit 0:6547fbc0f7c0 94 return NULL;
gauthibit 0:6547fbc0f7c0 95 }
gauthibit 0:6547fbc0f7c0 96 if (flags & O_APPEND) {
gauthibit 0:6547fbc0f7c0 97 f_lseek(&fh, fh.fsize);
gauthibit 0:6547fbc0f7c0 98 }
gauthibit 0:6547fbc0f7c0 99 return new FATFileHandle(fh);
gauthibit 0:6547fbc0f7c0 100 }
gauthibit 0:6547fbc0f7c0 101
gauthibit 0:6547fbc0f7c0 102 int FATFileSystem::remove(const char *filename) {
gauthibit 0:6547fbc0f7c0 103 FRESULT res = f_unlink(filename);
gauthibit 0:6547fbc0f7c0 104 if (res) {
gauthibit 0:6547fbc0f7c0 105 debug_if(FFS_DBG, "f_unlink() failed: %d\n", res);
gauthibit 0:6547fbc0f7c0 106 return -1;
gauthibit 0:6547fbc0f7c0 107 }
gauthibit 0:6547fbc0f7c0 108 return 0;
gauthibit 0:6547fbc0f7c0 109 }
gauthibit 0:6547fbc0f7c0 110
gauthibit 0:6547fbc0f7c0 111 int FATFileSystem::format() {
gauthibit 0:6547fbc0f7c0 112 FRESULT res = f_mkfs(_fsid, 0, 512); // Logical drive number, Partitioning rule, Allocation unit size (bytes per cluster)
gauthibit 0:6547fbc0f7c0 113 if (res) {
gauthibit 0:6547fbc0f7c0 114 debug_if(FFS_DBG, "f_mkfs() failed: %d\n", res);
gauthibit 0:6547fbc0f7c0 115 return -1;
gauthibit 0:6547fbc0f7c0 116 }
gauthibit 0:6547fbc0f7c0 117 return 0;
gauthibit 0:6547fbc0f7c0 118 }
gauthibit 0:6547fbc0f7c0 119
gauthibit 0:6547fbc0f7c0 120 DirHandle *FATFileSystem::opendir(const char *name) {
gauthibit 0:6547fbc0f7c0 121 FATFS_DIR dir;
gauthibit 0:6547fbc0f7c0 122 FRESULT res = f_opendir(&dir, name);
gauthibit 0:6547fbc0f7c0 123 if (res != 0) {
gauthibit 0:6547fbc0f7c0 124 return NULL;
gauthibit 0:6547fbc0f7c0 125 }
gauthibit 0:6547fbc0f7c0 126 return new FATDirHandle(dir);
gauthibit 0:6547fbc0f7c0 127 }
gauthibit 0:6547fbc0f7c0 128
gauthibit 0:6547fbc0f7c0 129 int FATFileSystem::mkdir(const char *name, mode_t mode) {
gauthibit 0:6547fbc0f7c0 130 FRESULT res = f_mkdir(name);
gauthibit 0:6547fbc0f7c0 131 return res == 0 ? 0 : -1;
gauthibit 0:6547fbc0f7c0 132 }