Library for SD card

Dependents:   SDFileSystem_HelloWorld Sharp_ce140f_emul

Committer:
AlexVC97
Date:
Wed Mar 29 07:00:22 2017 +0000
Revision:
0:3bdfc1556537
SDFileSystem Library

Who changed what in which revision?

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