Stand-alone HMD demo system built on mbed FRDM-K64F.

Dependencies:   SDFileSystem mbed FATFileSystem

Committer:
maruishi
Date:
Tue Jul 29 11:41:59 2014 +0000
Revision:
0:9bbe50de69d8
FRDM-K64F based 3D Head-mount-display for Interface(CQ Pub)

Who changed what in which revision?

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