publication inutile mais obligée

Committer:
adrevong
Date:
Tue Jan 28 11:14:21 2020 +0000
Revision:
0:8b2a15992487

        

Who changed what in which revision?

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