Example SDFileSystem in which FATDirHandle exposes the file info struct of the current file/directory.

Committer:
uci1
Date:
Fri Nov 28 05:12:38 2014 +0000
Revision:
1:a7d8fc28a863
Parent:
0:687056ba3278
change SD init so it won't stall on failed initialization

Who changed what in which revision?

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