temp

Dependencies:   mbed SDFileSystem MS5607 ADXL345_I2C FATFileSystem

Committer:
IKobayashi
Date:
Mon Mar 16 23:37:42 2020 +0900
Revision:
0:c88c3b616c00
copy

Who changed what in which revision?

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