Only Yesterday 制御プログラム

Dependencies:   mbed FATFileSystem

Fork of OnlyYestaerday by Junichi Katsu

Committer:
jksoft
Date:
Wed Apr 02 01:43:55 2014 +0000
Revision:
0:5975af170e43
1st Prototype

Who changed what in which revision?

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