Norimasa Okamoto / F32RFileSystem

Fork of F12RFileSystem by Norimasa Okamoto

Committer:
va009039
Date:
Sat Oct 31 00:30:21 2015 +0000
Revision:
6:3c5b3606e019
Parent:
FATFileSystem.cpp@5:b3b3370574cf
Child:
7:f9f52d9c0c57
fork from FATFileSystem

Who changed what in which revision?

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