A re-written SDFileSystem library with improved compatibility, CRC support, and card removal/replacement support.

Dependencies:   FATFileSystem

Dependents:   xadow_m0_SD_Hello roam_v1 roam_v2 Polytech_tours ... more

Committer:
neilt6
Date:
Mon Aug 18 15:09:52 2014 +0000
Revision:
13:635147efa748
Parent:
11:67ddc53e3983
Significant performance improvements with better busy wait logic

Who changed what in which revision?

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