fft

Dependencies:   BSP_DISCO_F746NG SDFileSystem_Warning_Fixed

Committer:
MikamiUitOpen
Date:
Sun Oct 09 10:11:14 2016 +0000
Revision:
0:3e46577dc273
1

Who changed what in which revision?

UserRevisionLine numberNew contents of line
MikamiUitOpen 0:3e46577dc273 1 /* mbed Microcontroller Library
MikamiUitOpen 0:3e46577dc273 2 * Copyright (c) 2006-2013 ARM Limited
MikamiUitOpen 0:3e46577dc273 3 *
MikamiUitOpen 0:3e46577dc273 4 * Licensed under the Apache License, Version 2.0 (the "License");
MikamiUitOpen 0:3e46577dc273 5 * you may not use this file except in compliance with the License.
MikamiUitOpen 0:3e46577dc273 6 * You may obtain a copy of the License at
MikamiUitOpen 0:3e46577dc273 7 *
MikamiUitOpen 0:3e46577dc273 8 * http://www.apache.org/licenses/LICENSE-2.0
MikamiUitOpen 0:3e46577dc273 9 *
MikamiUitOpen 0:3e46577dc273 10 * Unless required by applicable law or agreed to in writing, software
MikamiUitOpen 0:3e46577dc273 11 * distributed under the License is distributed on an "AS IS" BASIS,
MikamiUitOpen 0:3e46577dc273 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
MikamiUitOpen 0:3e46577dc273 13 * See the License for the specific language governing permissions and
MikamiUitOpen 0:3e46577dc273 14 * limitations under the License.
MikamiUitOpen 0:3e46577dc273 15 */
MikamiUitOpen 0:3e46577dc273 16 #include "LocalFileSystem.h"
MikamiUitOpen 0:3e46577dc273 17
MikamiUitOpen 0:3e46577dc273 18 #if DEVICE_LOCALFILESYSTEM
MikamiUitOpen 0:3e46577dc273 19
MikamiUitOpen 0:3e46577dc273 20 #include "semihost_api.h"
MikamiUitOpen 0:3e46577dc273 21 #include <string.h>
MikamiUitOpen 0:3e46577dc273 22 #include <stdio.h>
MikamiUitOpen 0:3e46577dc273 23
MikamiUitOpen 0:3e46577dc273 24 namespace mbed {
MikamiUitOpen 0:3e46577dc273 25
MikamiUitOpen 0:3e46577dc273 26 /* Extension to FINFO type defined in RTL.h (in Keil RL) - adds 'create time'. */
MikamiUitOpen 0:3e46577dc273 27 typedef struct {
MikamiUitOpen 0:3e46577dc273 28 unsigned char hr; /* Hours [0..23] */
MikamiUitOpen 0:3e46577dc273 29 unsigned char min; /* Minutes [0..59] */
MikamiUitOpen 0:3e46577dc273 30 unsigned char sec; /* Seconds [0..59] */
MikamiUitOpen 0:3e46577dc273 31 unsigned char day; /* Day [1..31] */
MikamiUitOpen 0:3e46577dc273 32 unsigned char mon; /* Month [1..12] */
MikamiUitOpen 0:3e46577dc273 33 unsigned short year; /* Year [1980..2107] */
MikamiUitOpen 0:3e46577dc273 34 } FTIME;
MikamiUitOpen 0:3e46577dc273 35
MikamiUitOpen 0:3e46577dc273 36 typedef struct { /* File Search info record */
MikamiUitOpen 0:3e46577dc273 37 char name[32]; /* File name */
MikamiUitOpen 0:3e46577dc273 38 long size; /* File size in bytes */
MikamiUitOpen 0:3e46577dc273 39 int fileID; /* System File Identification */
MikamiUitOpen 0:3e46577dc273 40 FTIME create_time; /* Date & time file was created */
MikamiUitOpen 0:3e46577dc273 41 FTIME write_time; /* Date & time of last write */
MikamiUitOpen 0:3e46577dc273 42 } XFINFO;
MikamiUitOpen 0:3e46577dc273 43
MikamiUitOpen 0:3e46577dc273 44 #define RESERVED_FOR_USER_APPLICATIONS (0x100) /* 0x100 - 0x1ff */
MikamiUitOpen 0:3e46577dc273 45 #define USR_XFFIND (RESERVED_FOR_USER_APPLICATIONS + 0)
MikamiUitOpen 0:3e46577dc273 46
MikamiUitOpen 0:3e46577dc273 47 static int xffind (const char *pattern, XFINFO *info) {
MikamiUitOpen 0:3e46577dc273 48 unsigned param[4];
MikamiUitOpen 0:3e46577dc273 49
MikamiUitOpen 0:3e46577dc273 50 param[0] = (unsigned long)pattern;
MikamiUitOpen 0:3e46577dc273 51 param[1] = (unsigned long)strlen(pattern);
MikamiUitOpen 0:3e46577dc273 52 param[2] = (unsigned long)info;
MikamiUitOpen 0:3e46577dc273 53 param[3] = (unsigned long)sizeof(XFINFO);
MikamiUitOpen 0:3e46577dc273 54
MikamiUitOpen 0:3e46577dc273 55 return __semihost(USR_XFFIND, param);
MikamiUitOpen 0:3e46577dc273 56 }
MikamiUitOpen 0:3e46577dc273 57
MikamiUitOpen 0:3e46577dc273 58 #define OPEN_R 0
MikamiUitOpen 0:3e46577dc273 59 #define OPEN_B 1
MikamiUitOpen 0:3e46577dc273 60 #define OPEN_PLUS 2
MikamiUitOpen 0:3e46577dc273 61 #define OPEN_W 4
MikamiUitOpen 0:3e46577dc273 62 #define OPEN_A 8
MikamiUitOpen 0:3e46577dc273 63 #define OPEN_INVALID -1
MikamiUitOpen 0:3e46577dc273 64
MikamiUitOpen 0:3e46577dc273 65 int posix_to_semihost_open_flags(int flags) {
MikamiUitOpen 0:3e46577dc273 66 /* POSIX flags -> semihosting open mode */
MikamiUitOpen 0:3e46577dc273 67 int openmode;
MikamiUitOpen 0:3e46577dc273 68 if (flags & O_RDWR) {
MikamiUitOpen 0:3e46577dc273 69 /* a plus mode */
MikamiUitOpen 0:3e46577dc273 70 openmode = OPEN_PLUS;
MikamiUitOpen 0:3e46577dc273 71 if (flags & O_APPEND) {
MikamiUitOpen 0:3e46577dc273 72 openmode |= OPEN_A;
MikamiUitOpen 0:3e46577dc273 73 } else if (flags & O_TRUNC) {
MikamiUitOpen 0:3e46577dc273 74 openmode |= OPEN_W;
MikamiUitOpen 0:3e46577dc273 75 } else {
MikamiUitOpen 0:3e46577dc273 76 openmode |= OPEN_R;
MikamiUitOpen 0:3e46577dc273 77 }
MikamiUitOpen 0:3e46577dc273 78 } else if (flags & O_WRONLY) {
MikamiUitOpen 0:3e46577dc273 79 /* write or append */
MikamiUitOpen 0:3e46577dc273 80 if (flags & O_APPEND) {
MikamiUitOpen 0:3e46577dc273 81 openmode = OPEN_A;
MikamiUitOpen 0:3e46577dc273 82 } else {
MikamiUitOpen 0:3e46577dc273 83 openmode = OPEN_W;
MikamiUitOpen 0:3e46577dc273 84 }
MikamiUitOpen 0:3e46577dc273 85 } else if (flags == O_RDONLY) {
MikamiUitOpen 0:3e46577dc273 86 /* read mode */
MikamiUitOpen 0:3e46577dc273 87 openmode = OPEN_R;
MikamiUitOpen 0:3e46577dc273 88 } else {
MikamiUitOpen 0:3e46577dc273 89 /* invalid flags */
MikamiUitOpen 0:3e46577dc273 90 openmode = OPEN_INVALID;
MikamiUitOpen 0:3e46577dc273 91 }
MikamiUitOpen 0:3e46577dc273 92
MikamiUitOpen 0:3e46577dc273 93 return openmode;
MikamiUitOpen 0:3e46577dc273 94 }
MikamiUitOpen 0:3e46577dc273 95
MikamiUitOpen 0:3e46577dc273 96 FILEHANDLE local_file_open(const char* name, int flags) {
MikamiUitOpen 0:3e46577dc273 97 int openmode = posix_to_semihost_open_flags(flags);
MikamiUitOpen 0:3e46577dc273 98 if (openmode == OPEN_INVALID) {
MikamiUitOpen 0:3e46577dc273 99 return (FILEHANDLE)NULL;
MikamiUitOpen 0:3e46577dc273 100 }
MikamiUitOpen 0:3e46577dc273 101
MikamiUitOpen 0:3e46577dc273 102 FILEHANDLE fh = semihost_open(name, openmode);
MikamiUitOpen 0:3e46577dc273 103 if (fh == -1) {
MikamiUitOpen 0:3e46577dc273 104 return (FILEHANDLE)NULL;
MikamiUitOpen 0:3e46577dc273 105 }
MikamiUitOpen 0:3e46577dc273 106
MikamiUitOpen 0:3e46577dc273 107 return fh;
MikamiUitOpen 0:3e46577dc273 108 }
MikamiUitOpen 0:3e46577dc273 109
MikamiUitOpen 0:3e46577dc273 110 LocalFileHandle::LocalFileHandle(FILEHANDLE fh) : _fh(fh), pos(0) {
MikamiUitOpen 0:3e46577dc273 111 }
MikamiUitOpen 0:3e46577dc273 112
MikamiUitOpen 0:3e46577dc273 113 int LocalFileHandle::close() {
MikamiUitOpen 0:3e46577dc273 114 int retval = semihost_close(_fh);
MikamiUitOpen 0:3e46577dc273 115 delete this;
MikamiUitOpen 0:3e46577dc273 116 return retval;
MikamiUitOpen 0:3e46577dc273 117 }
MikamiUitOpen 0:3e46577dc273 118
MikamiUitOpen 0:3e46577dc273 119 ssize_t LocalFileHandle::write(const void *buffer, size_t length) {
MikamiUitOpen 0:3e46577dc273 120 ssize_t n = semihost_write(_fh, (const unsigned char*)buffer, length, 0); // number of characters not written
MikamiUitOpen 0:3e46577dc273 121 n = length - n; // number of characters written
MikamiUitOpen 0:3e46577dc273 122 pos += n;
MikamiUitOpen 0:3e46577dc273 123 return n;
MikamiUitOpen 0:3e46577dc273 124 }
MikamiUitOpen 0:3e46577dc273 125
MikamiUitOpen 0:3e46577dc273 126 ssize_t LocalFileHandle::read(void *buffer, size_t length) {
MikamiUitOpen 0:3e46577dc273 127 ssize_t n = semihost_read(_fh, (unsigned char*)buffer, length, 0); // number of characters not read
MikamiUitOpen 0:3e46577dc273 128 n = length - n; // number of characters read
MikamiUitOpen 0:3e46577dc273 129 pos += n;
MikamiUitOpen 0:3e46577dc273 130 return n;
MikamiUitOpen 0:3e46577dc273 131 }
MikamiUitOpen 0:3e46577dc273 132
MikamiUitOpen 0:3e46577dc273 133 int LocalFileHandle::isatty() {
MikamiUitOpen 0:3e46577dc273 134 return semihost_istty(_fh);
MikamiUitOpen 0:3e46577dc273 135 }
MikamiUitOpen 0:3e46577dc273 136
MikamiUitOpen 0:3e46577dc273 137 off_t LocalFileHandle::lseek(off_t position, int whence) {
MikamiUitOpen 0:3e46577dc273 138 if (whence == SEEK_CUR) {
MikamiUitOpen 0:3e46577dc273 139 position += pos;
MikamiUitOpen 0:3e46577dc273 140 } else if (whence == SEEK_END) {
MikamiUitOpen 0:3e46577dc273 141 position += semihost_flen(_fh);
MikamiUitOpen 0:3e46577dc273 142 } /* otherwise SEEK_SET, so position is fine */
MikamiUitOpen 0:3e46577dc273 143
MikamiUitOpen 0:3e46577dc273 144 /* Always seems to return -1, so just ignore for now. */
MikamiUitOpen 0:3e46577dc273 145 semihost_seek(_fh, position);
MikamiUitOpen 0:3e46577dc273 146 pos = position;
MikamiUitOpen 0:3e46577dc273 147 return position;
MikamiUitOpen 0:3e46577dc273 148 }
MikamiUitOpen 0:3e46577dc273 149
MikamiUitOpen 0:3e46577dc273 150 int LocalFileHandle::fsync() {
MikamiUitOpen 0:3e46577dc273 151 return semihost_ensure(_fh);
MikamiUitOpen 0:3e46577dc273 152 }
MikamiUitOpen 0:3e46577dc273 153
MikamiUitOpen 0:3e46577dc273 154 off_t LocalFileHandle::flen() {
MikamiUitOpen 0:3e46577dc273 155 return semihost_flen(_fh);
MikamiUitOpen 0:3e46577dc273 156 }
MikamiUitOpen 0:3e46577dc273 157
MikamiUitOpen 0:3e46577dc273 158 class LocalDirHandle : public DirHandle {
MikamiUitOpen 0:3e46577dc273 159
MikamiUitOpen 0:3e46577dc273 160 public:
MikamiUitOpen 0:3e46577dc273 161 struct dirent cur_entry;
MikamiUitOpen 0:3e46577dc273 162 XFINFO info;
MikamiUitOpen 0:3e46577dc273 163
MikamiUitOpen 0:3e46577dc273 164 LocalDirHandle() : cur_entry(), info() {
MikamiUitOpen 0:3e46577dc273 165 }
MikamiUitOpen 0:3e46577dc273 166
MikamiUitOpen 0:3e46577dc273 167 virtual int closedir() {
MikamiUitOpen 0:3e46577dc273 168 delete this;
MikamiUitOpen 0:3e46577dc273 169 return 0;
MikamiUitOpen 0:3e46577dc273 170 }
MikamiUitOpen 0:3e46577dc273 171
MikamiUitOpen 0:3e46577dc273 172 virtual struct dirent *readdir() {
MikamiUitOpen 0:3e46577dc273 173 if (xffind("*", &info)!=0) {
MikamiUitOpen 0:3e46577dc273 174 return NULL;
MikamiUitOpen 0:3e46577dc273 175 }
MikamiUitOpen 0:3e46577dc273 176 memcpy(cur_entry.d_name, info.name, sizeof(info.name));
MikamiUitOpen 0:3e46577dc273 177 return &cur_entry;
MikamiUitOpen 0:3e46577dc273 178 }
MikamiUitOpen 0:3e46577dc273 179
MikamiUitOpen 0:3e46577dc273 180 virtual void rewinddir() {
MikamiUitOpen 0:3e46577dc273 181 info.fileID = 0;
MikamiUitOpen 0:3e46577dc273 182 }
MikamiUitOpen 0:3e46577dc273 183
MikamiUitOpen 0:3e46577dc273 184 virtual off_t telldir() {
MikamiUitOpen 0:3e46577dc273 185 return info.fileID;
MikamiUitOpen 0:3e46577dc273 186 }
MikamiUitOpen 0:3e46577dc273 187
MikamiUitOpen 0:3e46577dc273 188 virtual void seekdir(off_t offset) {
MikamiUitOpen 0:3e46577dc273 189 info.fileID = offset;
MikamiUitOpen 0:3e46577dc273 190 }
MikamiUitOpen 0:3e46577dc273 191 };
MikamiUitOpen 0:3e46577dc273 192
MikamiUitOpen 0:3e46577dc273 193 FileHandle *LocalFileSystem::open(const char* name, int flags) {
MikamiUitOpen 0:3e46577dc273 194 /* reject filenames with / in them */
MikamiUitOpen 0:3e46577dc273 195 for (const char *tmp = name; *tmp; tmp++) {
MikamiUitOpen 0:3e46577dc273 196 if (*tmp == '/') {
MikamiUitOpen 0:3e46577dc273 197 return NULL;
MikamiUitOpen 0:3e46577dc273 198 }
MikamiUitOpen 0:3e46577dc273 199 }
MikamiUitOpen 0:3e46577dc273 200
MikamiUitOpen 0:3e46577dc273 201 int openmode = posix_to_semihost_open_flags(flags);
MikamiUitOpen 0:3e46577dc273 202 if (openmode == OPEN_INVALID) {
MikamiUitOpen 0:3e46577dc273 203 return NULL;
MikamiUitOpen 0:3e46577dc273 204 }
MikamiUitOpen 0:3e46577dc273 205
MikamiUitOpen 0:3e46577dc273 206 FILEHANDLE fh = semihost_open(name, openmode);
MikamiUitOpen 0:3e46577dc273 207 if (fh == -1) {
MikamiUitOpen 0:3e46577dc273 208 return NULL;
MikamiUitOpen 0:3e46577dc273 209 }
MikamiUitOpen 0:3e46577dc273 210 return new LocalFileHandle(fh);
MikamiUitOpen 0:3e46577dc273 211 }
MikamiUitOpen 0:3e46577dc273 212
MikamiUitOpen 0:3e46577dc273 213 int LocalFileSystem::remove(const char *filename) {
MikamiUitOpen 0:3e46577dc273 214 return semihost_remove(filename);
MikamiUitOpen 0:3e46577dc273 215 }
MikamiUitOpen 0:3e46577dc273 216
MikamiUitOpen 0:3e46577dc273 217 DirHandle *LocalFileSystem::opendir(const char *name) {
MikamiUitOpen 0:3e46577dc273 218 return new LocalDirHandle();
MikamiUitOpen 0:3e46577dc273 219 }
MikamiUitOpen 0:3e46577dc273 220
MikamiUitOpen 0:3e46577dc273 221 } // namespace mbed
MikamiUitOpen 0:3e46577dc273 222
MikamiUitOpen 0:3e46577dc273 223 #endif