ロケット用プログラム

Dependencies:   mbed

Committer:
ishiyamayuto
Date:
Wed Sep 11 06:37:20 2019 +0000
Revision:
0:d58274531d38
BMP180,MPU6050

Who changed what in which revision?

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