Mark Schwarzer / SDFileSystem

Dependents:   Schwarzer_A7_1

Committer:
markschwarzer
Date:
Mon Nov 09 14:25:13 2020 +0000
Revision:
0:964d386ab059
logged data in SD card

Who changed what in which revision?

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