SDFileSystem-Smartes

Dependents:   Demo_NucleoF4xx_SD_Card soundthing

Committer:
FelipeVR
Date:
Tue Jun 12 18:15:57 2018 +0000
Revision:
0:70ff1cadb69a
SD Smartest;

Who changed what in which revision?

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