SD File System

Committer:
lucydamon
Date:
Thu Dec 01 23:57:53 2022 +0000
Revision:
0:4c17779d2cc6
1st commit- code but not working needs some debugs;

Who changed what in which revision?

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