Xbee CountUp

Dependencies:   mbed

Fork of HeptaXbee_CountUp by 智也 大野

Committer:
tomoya123
Date:
Tue Dec 13 07:55:03 2016 +0000
Revision:
1:715b80d2a02b
Parent:
0:0a7fa0911e6c
Xbee CountUp

Who changed what in which revision?

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