Integrated program of 4 different kinds of application programs for processing sound signal. 4種類のサウンド信号処理を統合したプログラム.

Dependencies:   F746_GUI F746_SAI_IO FrequencyResponseDrawer SD_PlayerSkeleton UIT_FFT_Real

Committer:
MikamiUitOpen
Date:
Mon Sep 26 07:07:05 2016 +0000
Revision:
2:fcba17e3d573
Parent:
mbed_src_STM/common/LocalFileSystem.cpp@0:224dccbc4edd
3

Who changed what in which revision?

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