Output the audio signal with filtering by graphic equalizer in the *.wav file on the SD card using onboard CODEC. SD カードの *.wav ファイルのオーディオ信号をグラフィック・イコライザを通して,ボードに搭載されているCODEC で出力する.

Dependencies:   F746_GUI F746_SAI_IO SD_PlayerSkeleton FrequencyResponseDrawer

Committer:
MikamiUitOpen
Date:
Sun Oct 02 06:33:59 2016 +0000
Revision:
16:cbb726ac20d8
17

Who changed what in which revision?

UserRevisionLine numberNew contents of line
MikamiUitOpen 16:cbb726ac20d8 1 /* mbed Microcontroller Library
MikamiUitOpen 16:cbb726ac20d8 2 * Copyright (c) 2006-2015 ARM Limited
MikamiUitOpen 16:cbb726ac20d8 3 *
MikamiUitOpen 16:cbb726ac20d8 4 * Licensed under the Apache License, Version 2.0 (the "License");
MikamiUitOpen 16:cbb726ac20d8 5 * you may not use this file except in compliance with the License.
MikamiUitOpen 16:cbb726ac20d8 6 * You may obtain a copy of the License at
MikamiUitOpen 16:cbb726ac20d8 7 *
MikamiUitOpen 16:cbb726ac20d8 8 * http://www.apache.org/licenses/LICENSE-2.0
MikamiUitOpen 16:cbb726ac20d8 9 *
MikamiUitOpen 16:cbb726ac20d8 10 * Unless required by applicable law or agreed to in writing, software
MikamiUitOpen 16:cbb726ac20d8 11 * distributed under the License is distributed on an "AS IS" BASIS,
MikamiUitOpen 16:cbb726ac20d8 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
MikamiUitOpen 16:cbb726ac20d8 13 * See the License for the specific language governing permissions and
MikamiUitOpen 16:cbb726ac20d8 14 * limitations under the License.
MikamiUitOpen 16:cbb726ac20d8 15 */
MikamiUitOpen 16:cbb726ac20d8 16 #include "platform.h"
MikamiUitOpen 16:cbb726ac20d8 17 #include "FileHandle.h"
MikamiUitOpen 16:cbb726ac20d8 18 #include "FileSystemLike.h"
MikamiUitOpen 16:cbb726ac20d8 19 #include "FilePath.h"
MikamiUitOpen 16:cbb726ac20d8 20 #include "serial_api.h"
MikamiUitOpen 16:cbb726ac20d8 21 #include "toolchain.h"
MikamiUitOpen 16:cbb726ac20d8 22 #include "semihost_api.h"
MikamiUitOpen 16:cbb726ac20d8 23 #include "mbed_interface.h"
MikamiUitOpen 16:cbb726ac20d8 24 #if DEVICE_STDIO_MESSAGES
MikamiUitOpen 16:cbb726ac20d8 25 #include <stdio.h>
MikamiUitOpen 16:cbb726ac20d8 26 #endif
MikamiUitOpen 16:cbb726ac20d8 27 #include <errno.h>
MikamiUitOpen 16:cbb726ac20d8 28
MikamiUitOpen 16:cbb726ac20d8 29 #if defined(__ARMCC_VERSION)
MikamiUitOpen 16:cbb726ac20d8 30 # include <rt_sys.h>
MikamiUitOpen 16:cbb726ac20d8 31 # define PREFIX(x) _sys##x
MikamiUitOpen 16:cbb726ac20d8 32 # define OPEN_MAX _SYS_OPEN
MikamiUitOpen 16:cbb726ac20d8 33 # ifdef __MICROLIB
MikamiUitOpen 16:cbb726ac20d8 34 # pragma import(__use_full_stdio)
MikamiUitOpen 16:cbb726ac20d8 35 # endif
MikamiUitOpen 16:cbb726ac20d8 36
MikamiUitOpen 16:cbb726ac20d8 37 #elif defined(__ICCARM__)
MikamiUitOpen 16:cbb726ac20d8 38 # include <yfuns.h>
MikamiUitOpen 16:cbb726ac20d8 39 # define PREFIX(x) _##x
MikamiUitOpen 16:cbb726ac20d8 40 # define OPEN_MAX 16
MikamiUitOpen 16:cbb726ac20d8 41
MikamiUitOpen 16:cbb726ac20d8 42 # define STDIN_FILENO 0
MikamiUitOpen 16:cbb726ac20d8 43 # define STDOUT_FILENO 1
MikamiUitOpen 16:cbb726ac20d8 44 # define STDERR_FILENO 2
MikamiUitOpen 16:cbb726ac20d8 45
MikamiUitOpen 16:cbb726ac20d8 46 #else
MikamiUitOpen 16:cbb726ac20d8 47 # include <sys/stat.h>
MikamiUitOpen 16:cbb726ac20d8 48 # include <sys/unistd.h>
MikamiUitOpen 16:cbb726ac20d8 49 # include <sys/syslimits.h>
MikamiUitOpen 16:cbb726ac20d8 50 # define PREFIX(x) x
MikamiUitOpen 16:cbb726ac20d8 51 #endif
MikamiUitOpen 16:cbb726ac20d8 52
MikamiUitOpen 16:cbb726ac20d8 53 using namespace mbed;
MikamiUitOpen 16:cbb726ac20d8 54
MikamiUitOpen 16:cbb726ac20d8 55 #if defined(__MICROLIB) && (__ARMCC_VERSION>5030000)
MikamiUitOpen 16:cbb726ac20d8 56 // Before version 5.03, we were using a patched version of microlib with proper names
MikamiUitOpen 16:cbb726ac20d8 57 extern const char __stdin_name[] = ":tt";
MikamiUitOpen 16:cbb726ac20d8 58 extern const char __stdout_name[] = ":tt";
MikamiUitOpen 16:cbb726ac20d8 59 extern const char __stderr_name[] = ":tt";
MikamiUitOpen 16:cbb726ac20d8 60
MikamiUitOpen 16:cbb726ac20d8 61 #else
MikamiUitOpen 16:cbb726ac20d8 62 extern const char __stdin_name[] = "/stdin";
MikamiUitOpen 16:cbb726ac20d8 63 extern const char __stdout_name[] = "/stdout";
MikamiUitOpen 16:cbb726ac20d8 64 extern const char __stderr_name[] = "/stderr";
MikamiUitOpen 16:cbb726ac20d8 65 #endif
MikamiUitOpen 16:cbb726ac20d8 66
MikamiUitOpen 16:cbb726ac20d8 67 /* newlib has the filehandle field in the FILE struct as a short, so
MikamiUitOpen 16:cbb726ac20d8 68 * we can't just return a Filehandle* from _open and instead have to
MikamiUitOpen 16:cbb726ac20d8 69 * put it in a filehandles array and return the index into that array
MikamiUitOpen 16:cbb726ac20d8 70 * (or rather index+3, as filehandles 0-2 are stdin/out/err).
MikamiUitOpen 16:cbb726ac20d8 71 */
MikamiUitOpen 16:cbb726ac20d8 72 static FileHandle *filehandles[OPEN_MAX];
MikamiUitOpen 16:cbb726ac20d8 73
MikamiUitOpen 16:cbb726ac20d8 74 FileHandle::~FileHandle() {
MikamiUitOpen 16:cbb726ac20d8 75 /* Remove all open filehandles for this */
MikamiUitOpen 16:cbb726ac20d8 76 for (unsigned int fh_i = 0; fh_i < sizeof(filehandles)/sizeof(*filehandles); fh_i++) {
MikamiUitOpen 16:cbb726ac20d8 77 if (filehandles[fh_i] == this) {
MikamiUitOpen 16:cbb726ac20d8 78 filehandles[fh_i] = NULL;
MikamiUitOpen 16:cbb726ac20d8 79 }
MikamiUitOpen 16:cbb726ac20d8 80 }
MikamiUitOpen 16:cbb726ac20d8 81 }
MikamiUitOpen 16:cbb726ac20d8 82
MikamiUitOpen 16:cbb726ac20d8 83 #if DEVICE_SERIAL
MikamiUitOpen 16:cbb726ac20d8 84 extern int stdio_uart_inited;
MikamiUitOpen 16:cbb726ac20d8 85 extern serial_t stdio_uart;
MikamiUitOpen 16:cbb726ac20d8 86 #endif
MikamiUitOpen 16:cbb726ac20d8 87
MikamiUitOpen 16:cbb726ac20d8 88 static void init_serial() {
MikamiUitOpen 16:cbb726ac20d8 89 #if DEVICE_SERIAL
MikamiUitOpen 16:cbb726ac20d8 90 if (stdio_uart_inited) return;
MikamiUitOpen 16:cbb726ac20d8 91 serial_init(&stdio_uart, STDIO_UART_TX, STDIO_UART_RX);
MikamiUitOpen 16:cbb726ac20d8 92 #endif
MikamiUitOpen 16:cbb726ac20d8 93 }
MikamiUitOpen 16:cbb726ac20d8 94
MikamiUitOpen 16:cbb726ac20d8 95 static inline int openmode_to_posix(int openmode) {
MikamiUitOpen 16:cbb726ac20d8 96 int posix = openmode;
MikamiUitOpen 16:cbb726ac20d8 97 #ifdef __ARMCC_VERSION
MikamiUitOpen 16:cbb726ac20d8 98 if (openmode & OPEN_PLUS) {
MikamiUitOpen 16:cbb726ac20d8 99 posix = O_RDWR;
MikamiUitOpen 16:cbb726ac20d8 100 } else if(openmode & OPEN_W) {
MikamiUitOpen 16:cbb726ac20d8 101 posix = O_WRONLY;
MikamiUitOpen 16:cbb726ac20d8 102 } else if(openmode & OPEN_A) {
MikamiUitOpen 16:cbb726ac20d8 103 posix = O_WRONLY|O_APPEND;
MikamiUitOpen 16:cbb726ac20d8 104 } else {
MikamiUitOpen 16:cbb726ac20d8 105 posix = O_RDONLY;
MikamiUitOpen 16:cbb726ac20d8 106 }
MikamiUitOpen 16:cbb726ac20d8 107 /* a, w, a+, w+ all create if file does not already exist */
MikamiUitOpen 16:cbb726ac20d8 108 if (openmode & (OPEN_A|OPEN_W)) {
MikamiUitOpen 16:cbb726ac20d8 109 posix |= O_CREAT;
MikamiUitOpen 16:cbb726ac20d8 110 }
MikamiUitOpen 16:cbb726ac20d8 111 /* w and w+ truncate */
MikamiUitOpen 16:cbb726ac20d8 112 if (openmode & OPEN_W) {
MikamiUitOpen 16:cbb726ac20d8 113 posix |= O_TRUNC;
MikamiUitOpen 16:cbb726ac20d8 114 }
MikamiUitOpen 16:cbb726ac20d8 115 #elif defined(__ICCARM__)
MikamiUitOpen 16:cbb726ac20d8 116 switch (openmode & _LLIO_RDWRMASK) {
MikamiUitOpen 16:cbb726ac20d8 117 case _LLIO_RDONLY: posix = O_RDONLY; break;
MikamiUitOpen 16:cbb726ac20d8 118 case _LLIO_WRONLY: posix = O_WRONLY; break;
MikamiUitOpen 16:cbb726ac20d8 119 case _LLIO_RDWR : posix = O_RDWR ; break;
MikamiUitOpen 16:cbb726ac20d8 120 }
MikamiUitOpen 16:cbb726ac20d8 121 if (openmode & _LLIO_CREAT ) posix |= O_CREAT;
MikamiUitOpen 16:cbb726ac20d8 122 if (openmode & _LLIO_APPEND) posix |= O_APPEND;
MikamiUitOpen 16:cbb726ac20d8 123 if (openmode & _LLIO_TRUNC ) posix |= O_TRUNC;
MikamiUitOpen 16:cbb726ac20d8 124 #elif defined(TOOLCHAIN_GCC)
MikamiUitOpen 16:cbb726ac20d8 125 posix &= ~O_BINARY;
MikamiUitOpen 16:cbb726ac20d8 126 #endif
MikamiUitOpen 16:cbb726ac20d8 127 return posix;
MikamiUitOpen 16:cbb726ac20d8 128 }
MikamiUitOpen 16:cbb726ac20d8 129
MikamiUitOpen 16:cbb726ac20d8 130 extern "C" FILEHANDLE PREFIX(_open)(const char* name, int openmode) {
MikamiUitOpen 16:cbb726ac20d8 131 #if defined(__MICROLIB) && (__ARMCC_VERSION>5030000)
MikamiUitOpen 16:cbb726ac20d8 132 // Before version 5.03, we were using a patched version of microlib with proper names
MikamiUitOpen 16:cbb726ac20d8 133 // This is the workaround that the microlib author suggested us
MikamiUitOpen 16:cbb726ac20d8 134 static int n = 0;
MikamiUitOpen 16:cbb726ac20d8 135 if (!std::strcmp(name, ":tt")) return n++;
MikamiUitOpen 16:cbb726ac20d8 136
MikamiUitOpen 16:cbb726ac20d8 137 #else
MikamiUitOpen 16:cbb726ac20d8 138 /* Use the posix convention that stdin,out,err are filehandles 0,1,2.
MikamiUitOpen 16:cbb726ac20d8 139 */
MikamiUitOpen 16:cbb726ac20d8 140 if (std::strcmp(name, __stdin_name) == 0) {
MikamiUitOpen 16:cbb726ac20d8 141 init_serial();
MikamiUitOpen 16:cbb726ac20d8 142 return 0;
MikamiUitOpen 16:cbb726ac20d8 143 } else if (std::strcmp(name, __stdout_name) == 0) {
MikamiUitOpen 16:cbb726ac20d8 144 init_serial();
MikamiUitOpen 16:cbb726ac20d8 145 return 1;
MikamiUitOpen 16:cbb726ac20d8 146 } else if (std::strcmp(name, __stderr_name) == 0) {
MikamiUitOpen 16:cbb726ac20d8 147 init_serial();
MikamiUitOpen 16:cbb726ac20d8 148 return 2;
MikamiUitOpen 16:cbb726ac20d8 149 }
MikamiUitOpen 16:cbb726ac20d8 150 #endif
MikamiUitOpen 16:cbb726ac20d8 151
MikamiUitOpen 16:cbb726ac20d8 152 // find the first empty slot in filehandles
MikamiUitOpen 16:cbb726ac20d8 153 unsigned int fh_i;
MikamiUitOpen 16:cbb726ac20d8 154 for (fh_i = 0; fh_i < sizeof(filehandles)/sizeof(*filehandles); fh_i++) {
MikamiUitOpen 16:cbb726ac20d8 155 if (filehandles[fh_i] == NULL) break;
MikamiUitOpen 16:cbb726ac20d8 156 }
MikamiUitOpen 16:cbb726ac20d8 157 if (fh_i >= sizeof(filehandles)/sizeof(*filehandles)) {
MikamiUitOpen 16:cbb726ac20d8 158 return -1;
MikamiUitOpen 16:cbb726ac20d8 159 }
MikamiUitOpen 16:cbb726ac20d8 160
MikamiUitOpen 16:cbb726ac20d8 161 FileHandle *res;
MikamiUitOpen 16:cbb726ac20d8 162
MikamiUitOpen 16:cbb726ac20d8 163 /* FILENAME: ":0x12345678" describes a FileLike* */
MikamiUitOpen 16:cbb726ac20d8 164 if (name[0] == ':') {
MikamiUitOpen 16:cbb726ac20d8 165 void *p;
MikamiUitOpen 16:cbb726ac20d8 166 sscanf(name, ":%p", &p);
MikamiUitOpen 16:cbb726ac20d8 167 res = (FileHandle*)p;
MikamiUitOpen 16:cbb726ac20d8 168
MikamiUitOpen 16:cbb726ac20d8 169 /* FILENAME: "/file_system/file_name" */
MikamiUitOpen 16:cbb726ac20d8 170 } else {
MikamiUitOpen 16:cbb726ac20d8 171 FilePath path(name);
MikamiUitOpen 16:cbb726ac20d8 172
MikamiUitOpen 16:cbb726ac20d8 173 if (!path.exists())
MikamiUitOpen 16:cbb726ac20d8 174 return -1;
MikamiUitOpen 16:cbb726ac20d8 175 else if (path.isFile()) {
MikamiUitOpen 16:cbb726ac20d8 176 res = path.file();
MikamiUitOpen 16:cbb726ac20d8 177 } else {
MikamiUitOpen 16:cbb726ac20d8 178 FileSystemLike *fs = path.fileSystem();
MikamiUitOpen 16:cbb726ac20d8 179 if (fs == NULL) return -1;
MikamiUitOpen 16:cbb726ac20d8 180 int posix_mode = openmode_to_posix(openmode);
MikamiUitOpen 16:cbb726ac20d8 181 res = fs->open(path.fileName(), posix_mode); /* NULL if fails */
MikamiUitOpen 16:cbb726ac20d8 182 }
MikamiUitOpen 16:cbb726ac20d8 183 }
MikamiUitOpen 16:cbb726ac20d8 184
MikamiUitOpen 16:cbb726ac20d8 185 if (res == NULL) return -1;
MikamiUitOpen 16:cbb726ac20d8 186 filehandles[fh_i] = res;
MikamiUitOpen 16:cbb726ac20d8 187
MikamiUitOpen 16:cbb726ac20d8 188 return fh_i + 3; // +3 as filehandles 0-2 are stdin/out/err
MikamiUitOpen 16:cbb726ac20d8 189 }
MikamiUitOpen 16:cbb726ac20d8 190
MikamiUitOpen 16:cbb726ac20d8 191 extern "C" int PREFIX(_close)(FILEHANDLE fh) {
MikamiUitOpen 16:cbb726ac20d8 192 if (fh < 3) return 0;
MikamiUitOpen 16:cbb726ac20d8 193
MikamiUitOpen 16:cbb726ac20d8 194 FileHandle* fhc = filehandles[fh-3];
MikamiUitOpen 16:cbb726ac20d8 195 filehandles[fh-3] = NULL;
MikamiUitOpen 16:cbb726ac20d8 196 if (fhc == NULL) return -1;
MikamiUitOpen 16:cbb726ac20d8 197
MikamiUitOpen 16:cbb726ac20d8 198 return fhc->close();
MikamiUitOpen 16:cbb726ac20d8 199 }
MikamiUitOpen 16:cbb726ac20d8 200
MikamiUitOpen 16:cbb726ac20d8 201 #if defined(__ICCARM__)
MikamiUitOpen 16:cbb726ac20d8 202 extern "C" size_t __write (int fh, const unsigned char *buffer, size_t length) {
MikamiUitOpen 16:cbb726ac20d8 203 #else
MikamiUitOpen 16:cbb726ac20d8 204 extern "C" int PREFIX(_write)(FILEHANDLE fh, const unsigned char *buffer, unsigned int length, int mode) {
MikamiUitOpen 16:cbb726ac20d8 205 #endif
MikamiUitOpen 16:cbb726ac20d8 206 int n; // n is the number of bytes written
MikamiUitOpen 16:cbb726ac20d8 207 if (fh < 3) {
MikamiUitOpen 16:cbb726ac20d8 208 #if DEVICE_SERIAL
MikamiUitOpen 16:cbb726ac20d8 209 if (!stdio_uart_inited) init_serial();
MikamiUitOpen 16:cbb726ac20d8 210 for (unsigned int i = 0; i < length; i++) {
MikamiUitOpen 16:cbb726ac20d8 211 serial_putc(&stdio_uart, buffer[i]);
MikamiUitOpen 16:cbb726ac20d8 212 }
MikamiUitOpen 16:cbb726ac20d8 213 #endif
MikamiUitOpen 16:cbb726ac20d8 214 n = length;
MikamiUitOpen 16:cbb726ac20d8 215 } else {
MikamiUitOpen 16:cbb726ac20d8 216 FileHandle* fhc = filehandles[fh-3];
MikamiUitOpen 16:cbb726ac20d8 217 if (fhc == NULL) return -1;
MikamiUitOpen 16:cbb726ac20d8 218
MikamiUitOpen 16:cbb726ac20d8 219 n = fhc->write(buffer, length);
MikamiUitOpen 16:cbb726ac20d8 220 }
MikamiUitOpen 16:cbb726ac20d8 221 #ifdef __ARMCC_VERSION
MikamiUitOpen 16:cbb726ac20d8 222 return length-n;
MikamiUitOpen 16:cbb726ac20d8 223 #else
MikamiUitOpen 16:cbb726ac20d8 224 return n;
MikamiUitOpen 16:cbb726ac20d8 225 #endif
MikamiUitOpen 16:cbb726ac20d8 226 }
MikamiUitOpen 16:cbb726ac20d8 227
MikamiUitOpen 16:cbb726ac20d8 228 #if defined(__ICCARM__)
MikamiUitOpen 16:cbb726ac20d8 229 extern "C" size_t __read (int fh, unsigned char *buffer, size_t length) {
MikamiUitOpen 16:cbb726ac20d8 230 #else
MikamiUitOpen 16:cbb726ac20d8 231 extern "C" int PREFIX(_read)(FILEHANDLE fh, unsigned char *buffer, unsigned int length, int mode) {
MikamiUitOpen 16:cbb726ac20d8 232 #endif
MikamiUitOpen 16:cbb726ac20d8 233 int n; // n is the number of bytes read
MikamiUitOpen 16:cbb726ac20d8 234 if (fh < 3) {
MikamiUitOpen 16:cbb726ac20d8 235 // only read a character at a time from stdin
MikamiUitOpen 16:cbb726ac20d8 236 #if DEVICE_SERIAL
MikamiUitOpen 16:cbb726ac20d8 237 if (!stdio_uart_inited) init_serial();
MikamiUitOpen 16:cbb726ac20d8 238 *buffer = serial_getc(&stdio_uart);
MikamiUitOpen 16:cbb726ac20d8 239 #endif
MikamiUitOpen 16:cbb726ac20d8 240 n = 1;
MikamiUitOpen 16:cbb726ac20d8 241 } else {
MikamiUitOpen 16:cbb726ac20d8 242 FileHandle* fhc = filehandles[fh-3];
MikamiUitOpen 16:cbb726ac20d8 243 if (fhc == NULL) return -1;
MikamiUitOpen 16:cbb726ac20d8 244
MikamiUitOpen 16:cbb726ac20d8 245 n = fhc->read(buffer, length);
MikamiUitOpen 16:cbb726ac20d8 246 }
MikamiUitOpen 16:cbb726ac20d8 247 #ifdef __ARMCC_VERSION
MikamiUitOpen 16:cbb726ac20d8 248 return length-n;
MikamiUitOpen 16:cbb726ac20d8 249 #else
MikamiUitOpen 16:cbb726ac20d8 250 return n;
MikamiUitOpen 16:cbb726ac20d8 251 #endif
MikamiUitOpen 16:cbb726ac20d8 252 }
MikamiUitOpen 16:cbb726ac20d8 253
MikamiUitOpen 16:cbb726ac20d8 254 #ifdef __ARMCC_VERSION
MikamiUitOpen 16:cbb726ac20d8 255 extern "C" int PREFIX(_istty)(FILEHANDLE fh)
MikamiUitOpen 16:cbb726ac20d8 256 #else
MikamiUitOpen 16:cbb726ac20d8 257 extern "C" int _isatty(FILEHANDLE fh)
MikamiUitOpen 16:cbb726ac20d8 258 #endif
MikamiUitOpen 16:cbb726ac20d8 259 {
MikamiUitOpen 16:cbb726ac20d8 260 /* stdin, stdout and stderr should be tty */
MikamiUitOpen 16:cbb726ac20d8 261 if (fh < 3) return 1;
MikamiUitOpen 16:cbb726ac20d8 262
MikamiUitOpen 16:cbb726ac20d8 263 FileHandle* fhc = filehandles[fh-3];
MikamiUitOpen 16:cbb726ac20d8 264 if (fhc == NULL) return -1;
MikamiUitOpen 16:cbb726ac20d8 265
MikamiUitOpen 16:cbb726ac20d8 266 return fhc->isatty();
MikamiUitOpen 16:cbb726ac20d8 267 }
MikamiUitOpen 16:cbb726ac20d8 268
MikamiUitOpen 16:cbb726ac20d8 269 extern "C"
MikamiUitOpen 16:cbb726ac20d8 270 #if defined(__ARMCC_VERSION)
MikamiUitOpen 16:cbb726ac20d8 271 int _sys_seek(FILEHANDLE fh, long position)
MikamiUitOpen 16:cbb726ac20d8 272 #elif defined(__ICCARM__)
MikamiUitOpen 16:cbb726ac20d8 273 long __lseek(int fh, long offset, int whence)
MikamiUitOpen 16:cbb726ac20d8 274 #else
MikamiUitOpen 16:cbb726ac20d8 275 int _lseek(FILEHANDLE fh, int offset, int whence)
MikamiUitOpen 16:cbb726ac20d8 276 #endif
MikamiUitOpen 16:cbb726ac20d8 277 {
MikamiUitOpen 16:cbb726ac20d8 278 if (fh < 3) return 0;
MikamiUitOpen 16:cbb726ac20d8 279
MikamiUitOpen 16:cbb726ac20d8 280 FileHandle* fhc = filehandles[fh-3];
MikamiUitOpen 16:cbb726ac20d8 281 if (fhc == NULL) return -1;
MikamiUitOpen 16:cbb726ac20d8 282
MikamiUitOpen 16:cbb726ac20d8 283 #if defined(__ARMCC_VERSION)
MikamiUitOpen 16:cbb726ac20d8 284 return fhc->lseek(position, SEEK_SET);
MikamiUitOpen 16:cbb726ac20d8 285 #else
MikamiUitOpen 16:cbb726ac20d8 286 return fhc->lseek(offset, whence);
MikamiUitOpen 16:cbb726ac20d8 287 #endif
MikamiUitOpen 16:cbb726ac20d8 288 }
MikamiUitOpen 16:cbb726ac20d8 289
MikamiUitOpen 16:cbb726ac20d8 290 #ifdef __ARMCC_VERSION
MikamiUitOpen 16:cbb726ac20d8 291 extern "C" int PREFIX(_ensure)(FILEHANDLE fh) {
MikamiUitOpen 16:cbb726ac20d8 292 if (fh < 3) return 0;
MikamiUitOpen 16:cbb726ac20d8 293
MikamiUitOpen 16:cbb726ac20d8 294 FileHandle* fhc = filehandles[fh-3];
MikamiUitOpen 16:cbb726ac20d8 295 if (fhc == NULL) return -1;
MikamiUitOpen 16:cbb726ac20d8 296
MikamiUitOpen 16:cbb726ac20d8 297 return fhc->fsync();
MikamiUitOpen 16:cbb726ac20d8 298 }
MikamiUitOpen 16:cbb726ac20d8 299
MikamiUitOpen 16:cbb726ac20d8 300 extern "C" long PREFIX(_flen)(FILEHANDLE fh) {
MikamiUitOpen 16:cbb726ac20d8 301 if (fh < 3) return 0;
MikamiUitOpen 16:cbb726ac20d8 302
MikamiUitOpen 16:cbb726ac20d8 303 FileHandle* fhc = filehandles[fh-3];
MikamiUitOpen 16:cbb726ac20d8 304 if (fhc == NULL) return -1;
MikamiUitOpen 16:cbb726ac20d8 305
MikamiUitOpen 16:cbb726ac20d8 306 return fhc->flen();
MikamiUitOpen 16:cbb726ac20d8 307 }
MikamiUitOpen 16:cbb726ac20d8 308 #endif
MikamiUitOpen 16:cbb726ac20d8 309
MikamiUitOpen 16:cbb726ac20d8 310
MikamiUitOpen 16:cbb726ac20d8 311 #if !defined(__ARMCC_VERSION) && !defined(__ICCARM__)
MikamiUitOpen 16:cbb726ac20d8 312 extern "C" int _fstat(int fd, struct stat *st) {
MikamiUitOpen 16:cbb726ac20d8 313 if ((STDOUT_FILENO == fd) || (STDERR_FILENO == fd) || (STDIN_FILENO == fd)) {
MikamiUitOpen 16:cbb726ac20d8 314 st->st_mode = S_IFCHR;
MikamiUitOpen 16:cbb726ac20d8 315 return 0;
MikamiUitOpen 16:cbb726ac20d8 316 }
MikamiUitOpen 16:cbb726ac20d8 317
MikamiUitOpen 16:cbb726ac20d8 318 errno = EBADF;
MikamiUitOpen 16:cbb726ac20d8 319 return -1;
MikamiUitOpen 16:cbb726ac20d8 320 }
MikamiUitOpen 16:cbb726ac20d8 321 #endif
MikamiUitOpen 16:cbb726ac20d8 322
MikamiUitOpen 16:cbb726ac20d8 323 namespace std {
MikamiUitOpen 16:cbb726ac20d8 324 extern "C" int remove(const char *path) {
MikamiUitOpen 16:cbb726ac20d8 325 FilePath fp(path);
MikamiUitOpen 16:cbb726ac20d8 326 FileSystemLike *fs = fp.fileSystem();
MikamiUitOpen 16:cbb726ac20d8 327 if (fs == NULL) return -1;
MikamiUitOpen 16:cbb726ac20d8 328
MikamiUitOpen 16:cbb726ac20d8 329 return fs->remove(fp.fileName());
MikamiUitOpen 16:cbb726ac20d8 330 }
MikamiUitOpen 16:cbb726ac20d8 331
MikamiUitOpen 16:cbb726ac20d8 332 extern "C" int rename(const char *oldname, const char *newname) {
MikamiUitOpen 16:cbb726ac20d8 333 FilePath fpOld(oldname);
MikamiUitOpen 16:cbb726ac20d8 334 FilePath fpNew(newname);
MikamiUitOpen 16:cbb726ac20d8 335 FileSystemLike *fsOld = fpOld.fileSystem();
MikamiUitOpen 16:cbb726ac20d8 336 FileSystemLike *fsNew = fpNew.fileSystem();
MikamiUitOpen 16:cbb726ac20d8 337
MikamiUitOpen 16:cbb726ac20d8 338 /* rename only if both files are on the same FS */
MikamiUitOpen 16:cbb726ac20d8 339 if (fsOld != fsNew || fsOld == NULL) return -1;
MikamiUitOpen 16:cbb726ac20d8 340
MikamiUitOpen 16:cbb726ac20d8 341 return fsOld->rename(fpOld.fileName(), fpNew.fileName());
MikamiUitOpen 16:cbb726ac20d8 342 }
MikamiUitOpen 16:cbb726ac20d8 343
MikamiUitOpen 16:cbb726ac20d8 344 extern "C" char *tmpnam(char *s) {
MikamiUitOpen 16:cbb726ac20d8 345 return NULL;
MikamiUitOpen 16:cbb726ac20d8 346 }
MikamiUitOpen 16:cbb726ac20d8 347
MikamiUitOpen 16:cbb726ac20d8 348 extern "C" FILE *tmpfile() {
MikamiUitOpen 16:cbb726ac20d8 349 return NULL;
MikamiUitOpen 16:cbb726ac20d8 350 }
MikamiUitOpen 16:cbb726ac20d8 351 } // namespace std
MikamiUitOpen 16:cbb726ac20d8 352
MikamiUitOpen 16:cbb726ac20d8 353 #ifdef __ARMCC_VERSION
MikamiUitOpen 16:cbb726ac20d8 354 extern "C" char *_sys_command_string(char *cmd, int len) {
MikamiUitOpen 16:cbb726ac20d8 355 return NULL;
MikamiUitOpen 16:cbb726ac20d8 356 }
MikamiUitOpen 16:cbb726ac20d8 357 #endif
MikamiUitOpen 16:cbb726ac20d8 358
MikamiUitOpen 16:cbb726ac20d8 359 extern "C" DIR *opendir(const char *path) {
MikamiUitOpen 16:cbb726ac20d8 360 /* root dir is FileSystemLike */
MikamiUitOpen 16:cbb726ac20d8 361 if (path[0] == '/' && path[1] == 0) {
MikamiUitOpen 16:cbb726ac20d8 362 return FileSystemLike::opendir();
MikamiUitOpen 16:cbb726ac20d8 363 }
MikamiUitOpen 16:cbb726ac20d8 364
MikamiUitOpen 16:cbb726ac20d8 365 FilePath fp(path);
MikamiUitOpen 16:cbb726ac20d8 366 FileSystemLike* fs = fp.fileSystem();
MikamiUitOpen 16:cbb726ac20d8 367 if (fs == NULL) return NULL;
MikamiUitOpen 16:cbb726ac20d8 368
MikamiUitOpen 16:cbb726ac20d8 369 return fs->opendir(fp.fileName());
MikamiUitOpen 16:cbb726ac20d8 370 }
MikamiUitOpen 16:cbb726ac20d8 371
MikamiUitOpen 16:cbb726ac20d8 372 extern "C" struct dirent *readdir(DIR *dir) {
MikamiUitOpen 16:cbb726ac20d8 373 return dir->readdir();
MikamiUitOpen 16:cbb726ac20d8 374 }
MikamiUitOpen 16:cbb726ac20d8 375
MikamiUitOpen 16:cbb726ac20d8 376 extern "C" int closedir(DIR *dir) {
MikamiUitOpen 16:cbb726ac20d8 377 return dir->closedir();
MikamiUitOpen 16:cbb726ac20d8 378 }
MikamiUitOpen 16:cbb726ac20d8 379
MikamiUitOpen 16:cbb726ac20d8 380 extern "C" void rewinddir(DIR *dir) {
MikamiUitOpen 16:cbb726ac20d8 381 dir->rewinddir();
MikamiUitOpen 16:cbb726ac20d8 382 }
MikamiUitOpen 16:cbb726ac20d8 383
MikamiUitOpen 16:cbb726ac20d8 384 extern "C" off_t telldir(DIR *dir) {
MikamiUitOpen 16:cbb726ac20d8 385 return dir->telldir();
MikamiUitOpen 16:cbb726ac20d8 386 }
MikamiUitOpen 16:cbb726ac20d8 387
MikamiUitOpen 16:cbb726ac20d8 388 extern "C" void seekdir(DIR *dir, off_t off) {
MikamiUitOpen 16:cbb726ac20d8 389 dir->seekdir(off);
MikamiUitOpen 16:cbb726ac20d8 390 }
MikamiUitOpen 16:cbb726ac20d8 391
MikamiUitOpen 16:cbb726ac20d8 392 extern "C" int mkdir(const char *path, mode_t mode) {
MikamiUitOpen 16:cbb726ac20d8 393 FilePath fp(path);
MikamiUitOpen 16:cbb726ac20d8 394 FileSystemLike *fs = fp.fileSystem();
MikamiUitOpen 16:cbb726ac20d8 395 if (fs == NULL) return -1;
MikamiUitOpen 16:cbb726ac20d8 396
MikamiUitOpen 16:cbb726ac20d8 397 return fs->mkdir(fp.fileName(), mode);
MikamiUitOpen 16:cbb726ac20d8 398 }
MikamiUitOpen 16:cbb726ac20d8 399
MikamiUitOpen 16:cbb726ac20d8 400 #if defined(TOOLCHAIN_GCC)
MikamiUitOpen 16:cbb726ac20d8 401 /* prevents the exception handling name demangling code getting pulled in */
MikamiUitOpen 16:cbb726ac20d8 402 #include "mbed_error.h"
MikamiUitOpen 16:cbb726ac20d8 403 namespace __gnu_cxx {
MikamiUitOpen 16:cbb726ac20d8 404 void __verbose_terminate_handler() {
MikamiUitOpen 16:cbb726ac20d8 405 error("Exception");
MikamiUitOpen 16:cbb726ac20d8 406 }
MikamiUitOpen 16:cbb726ac20d8 407 }
MikamiUitOpen 16:cbb726ac20d8 408 extern "C" WEAK void __cxa_pure_virtual(void);
MikamiUitOpen 16:cbb726ac20d8 409 extern "C" WEAK void __cxa_pure_virtual(void) {
MikamiUitOpen 16:cbb726ac20d8 410 exit(1);
MikamiUitOpen 16:cbb726ac20d8 411 }
MikamiUitOpen 16:cbb726ac20d8 412
MikamiUitOpen 16:cbb726ac20d8 413 #endif
MikamiUitOpen 16:cbb726ac20d8 414
MikamiUitOpen 16:cbb726ac20d8 415 // ****************************************************************************
MikamiUitOpen 16:cbb726ac20d8 416 // mbed_main is a function that is called before main()
MikamiUitOpen 16:cbb726ac20d8 417 // mbed_sdk_init() is also a function that is called before main(), but unlike
MikamiUitOpen 16:cbb726ac20d8 418 // mbed_main(), it is not meant for user code, but for the SDK itself to perform
MikamiUitOpen 16:cbb726ac20d8 419 // initializations before main() is called.
MikamiUitOpen 16:cbb726ac20d8 420
MikamiUitOpen 16:cbb726ac20d8 421 extern "C" WEAK void mbed_main(void);
MikamiUitOpen 16:cbb726ac20d8 422 extern "C" WEAK void mbed_main(void) {
MikamiUitOpen 16:cbb726ac20d8 423 }
MikamiUitOpen 16:cbb726ac20d8 424
MikamiUitOpen 16:cbb726ac20d8 425 extern "C" WEAK void mbed_sdk_init(void);
MikamiUitOpen 16:cbb726ac20d8 426 extern "C" WEAK void mbed_sdk_init(void) {
MikamiUitOpen 16:cbb726ac20d8 427 }
MikamiUitOpen 16:cbb726ac20d8 428
MikamiUitOpen 16:cbb726ac20d8 429 #if defined(TOOLCHAIN_ARM)
MikamiUitOpen 16:cbb726ac20d8 430 extern "C" int $Super$$main(void);
MikamiUitOpen 16:cbb726ac20d8 431
MikamiUitOpen 16:cbb726ac20d8 432 extern "C" int $Sub$$main(void) {
MikamiUitOpen 16:cbb726ac20d8 433 mbed_sdk_init();
MikamiUitOpen 16:cbb726ac20d8 434 mbed_main();
MikamiUitOpen 16:cbb726ac20d8 435 return $Super$$main();
MikamiUitOpen 16:cbb726ac20d8 436 }
MikamiUitOpen 16:cbb726ac20d8 437 #elif defined(TOOLCHAIN_GCC)
MikamiUitOpen 16:cbb726ac20d8 438 extern "C" int __real_main(void);
MikamiUitOpen 16:cbb726ac20d8 439
MikamiUitOpen 16:cbb726ac20d8 440 extern "C" int __wrap_main(void) {
MikamiUitOpen 16:cbb726ac20d8 441 mbed_sdk_init();
MikamiUitOpen 16:cbb726ac20d8 442 mbed_main();
MikamiUitOpen 16:cbb726ac20d8 443 return __real_main();
MikamiUitOpen 16:cbb726ac20d8 444 }
MikamiUitOpen 16:cbb726ac20d8 445 #elif defined(TOOLCHAIN_IAR)
MikamiUitOpen 16:cbb726ac20d8 446 // IAR doesn't have the $Super/$Sub mechanism of armcc, nor something equivalent
MikamiUitOpen 16:cbb726ac20d8 447 // to ld's --wrap. It does have a --redirect, but that doesn't help, since redirecting
MikamiUitOpen 16:cbb726ac20d8 448 // 'main' to another symbol looses the original 'main' symbol. However, its startup
MikamiUitOpen 16:cbb726ac20d8 449 // code will call a function to setup argc and argv (__iar_argc_argv) if it is defined.
MikamiUitOpen 16:cbb726ac20d8 450 // Since mbed doesn't use argc/argv, we use this function to call our mbed_main.
MikamiUitOpen 16:cbb726ac20d8 451 extern "C" void __iar_argc_argv() {
MikamiUitOpen 16:cbb726ac20d8 452 mbed_sdk_init();
MikamiUitOpen 16:cbb726ac20d8 453 mbed_main();
MikamiUitOpen 16:cbb726ac20d8 454 }
MikamiUitOpen 16:cbb726ac20d8 455 #endif
MikamiUitOpen 16:cbb726ac20d8 456
MikamiUitOpen 16:cbb726ac20d8 457 // Provide implementation of _sbrk (low-level dynamic memory allocation
MikamiUitOpen 16:cbb726ac20d8 458 // routine) for GCC_ARM which compares new heap pointer with MSP instead of
MikamiUitOpen 16:cbb726ac20d8 459 // SP. This make it compatible with RTX RTOS thread stacks.
MikamiUitOpen 16:cbb726ac20d8 460 #if defined(TOOLCHAIN_GCC_ARM)
MikamiUitOpen 16:cbb726ac20d8 461 // Linker defined symbol used by _sbrk to indicate where heap should start.
MikamiUitOpen 16:cbb726ac20d8 462 extern "C" int __end__;
MikamiUitOpen 16:cbb726ac20d8 463
MikamiUitOpen 16:cbb726ac20d8 464 #if defined(TARGET_CORTEX_A)
MikamiUitOpen 16:cbb726ac20d8 465 extern "C" uint32_t __HeapLimit;
MikamiUitOpen 16:cbb726ac20d8 466 #endif
MikamiUitOpen 16:cbb726ac20d8 467
MikamiUitOpen 16:cbb726ac20d8 468 // Turn off the errno macro and use actual global variable instead.
MikamiUitOpen 16:cbb726ac20d8 469 #undef errno
MikamiUitOpen 16:cbb726ac20d8 470 extern "C" int errno;
MikamiUitOpen 16:cbb726ac20d8 471
MikamiUitOpen 16:cbb726ac20d8 472 // For ARM7 only
MikamiUitOpen 16:cbb726ac20d8 473 register unsigned char * stack_ptr __asm ("sp");
MikamiUitOpen 16:cbb726ac20d8 474
MikamiUitOpen 16:cbb726ac20d8 475 // Dynamic memory allocation related syscall.
MikamiUitOpen 16:cbb726ac20d8 476 extern "C" caddr_t _sbrk(int incr) {
MikamiUitOpen 16:cbb726ac20d8 477 static unsigned char* heap = (unsigned char*)&__end__;
MikamiUitOpen 16:cbb726ac20d8 478 unsigned char* prev_heap = heap;
MikamiUitOpen 16:cbb726ac20d8 479 unsigned char* new_heap = heap + incr;
MikamiUitOpen 16:cbb726ac20d8 480
MikamiUitOpen 16:cbb726ac20d8 481 #if defined(TARGET_ARM7)
MikamiUitOpen 16:cbb726ac20d8 482 if (new_heap >= stack_ptr) {
MikamiUitOpen 16:cbb726ac20d8 483 #elif defined(TARGET_CORTEX_A)
MikamiUitOpen 16:cbb726ac20d8 484 if (new_heap >= (unsigned char*)&__HeapLimit) { /* __HeapLimit is end of heap section */
MikamiUitOpen 16:cbb726ac20d8 485 #else
MikamiUitOpen 16:cbb726ac20d8 486 if (new_heap >= (unsigned char*)__get_MSP()) {
MikamiUitOpen 16:cbb726ac20d8 487 #endif
MikamiUitOpen 16:cbb726ac20d8 488 errno = ENOMEM;
MikamiUitOpen 16:cbb726ac20d8 489 return (caddr_t)-1;
MikamiUitOpen 16:cbb726ac20d8 490 }
MikamiUitOpen 16:cbb726ac20d8 491
MikamiUitOpen 16:cbb726ac20d8 492 heap = new_heap;
MikamiUitOpen 16:cbb726ac20d8 493 return (caddr_t) prev_heap;
MikamiUitOpen 16:cbb726ac20d8 494 }
MikamiUitOpen 16:cbb726ac20d8 495 #endif
MikamiUitOpen 16:cbb726ac20d8 496
MikamiUitOpen 16:cbb726ac20d8 497
MikamiUitOpen 16:cbb726ac20d8 498 #if defined TOOLCHAIN_GCC_ARM
MikamiUitOpen 16:cbb726ac20d8 499 extern "C" void _exit(int return_code) {
MikamiUitOpen 16:cbb726ac20d8 500 #else
MikamiUitOpen 16:cbb726ac20d8 501 namespace std {
MikamiUitOpen 16:cbb726ac20d8 502 extern "C" void exit(int return_code) {
MikamiUitOpen 16:cbb726ac20d8 503 #endif
MikamiUitOpen 16:cbb726ac20d8 504
MikamiUitOpen 16:cbb726ac20d8 505 #if DEVICE_STDIO_MESSAGES
MikamiUitOpen 16:cbb726ac20d8 506 fflush(stdout);
MikamiUitOpen 16:cbb726ac20d8 507 fflush(stderr);
MikamiUitOpen 16:cbb726ac20d8 508 #endif
MikamiUitOpen 16:cbb726ac20d8 509
MikamiUitOpen 16:cbb726ac20d8 510 #if DEVICE_SEMIHOST
MikamiUitOpen 16:cbb726ac20d8 511 if (mbed_interface_connected()) {
MikamiUitOpen 16:cbb726ac20d8 512 semihost_exit();
MikamiUitOpen 16:cbb726ac20d8 513 }
MikamiUitOpen 16:cbb726ac20d8 514 #endif
MikamiUitOpen 16:cbb726ac20d8 515 if (return_code) {
MikamiUitOpen 16:cbb726ac20d8 516 mbed_die();
MikamiUitOpen 16:cbb726ac20d8 517 }
MikamiUitOpen 16:cbb726ac20d8 518
MikamiUitOpen 16:cbb726ac20d8 519 while (1);
MikamiUitOpen 16:cbb726ac20d8 520 }
MikamiUitOpen 16:cbb726ac20d8 521
MikamiUitOpen 16:cbb726ac20d8 522 #if !defined(TOOLCHAIN_GCC_ARM)
MikamiUitOpen 16:cbb726ac20d8 523 } //namespace std
MikamiUitOpen 16:cbb726ac20d8 524 #endif
MikamiUitOpen 16:cbb726ac20d8 525
MikamiUitOpen 16:cbb726ac20d8 526
MikamiUitOpen 16:cbb726ac20d8 527 namespace mbed {
MikamiUitOpen 16:cbb726ac20d8 528
MikamiUitOpen 16:cbb726ac20d8 529 void mbed_set_unbuffered_stream(FILE *_file) {
MikamiUitOpen 16:cbb726ac20d8 530 #if defined (__ICCARM__)
MikamiUitOpen 16:cbb726ac20d8 531 char buf[2];
MikamiUitOpen 16:cbb726ac20d8 532 std::setvbuf(_file,buf,_IONBF,NULL);
MikamiUitOpen 16:cbb726ac20d8 533 #else
MikamiUitOpen 16:cbb726ac20d8 534 setbuf(_file, NULL);
MikamiUitOpen 16:cbb726ac20d8 535 #endif
MikamiUitOpen 16:cbb726ac20d8 536 }
MikamiUitOpen 16:cbb726ac20d8 537
MikamiUitOpen 16:cbb726ac20d8 538 int mbed_getc(FILE *_file){
MikamiUitOpen 16:cbb726ac20d8 539 #if defined (__ICCARM__)
MikamiUitOpen 16:cbb726ac20d8 540 /*This is only valid for unbuffered streams*/
MikamiUitOpen 16:cbb726ac20d8 541 int res = std::fgetc(_file);
MikamiUitOpen 16:cbb726ac20d8 542 if (res>=0){
MikamiUitOpen 16:cbb726ac20d8 543 _file->_Mode = (unsigned short)(_file->_Mode & ~ 0x1000);/* Unset read mode */
MikamiUitOpen 16:cbb726ac20d8 544 _file->_Rend = _file->_Wend;
MikamiUitOpen 16:cbb726ac20d8 545 _file->_Next = _file->_Wend;
MikamiUitOpen 16:cbb726ac20d8 546 }
MikamiUitOpen 16:cbb726ac20d8 547 return res;
MikamiUitOpen 16:cbb726ac20d8 548 #else
MikamiUitOpen 16:cbb726ac20d8 549 return std::fgetc(_file);
MikamiUitOpen 16:cbb726ac20d8 550 #endif
MikamiUitOpen 16:cbb726ac20d8 551 }
MikamiUitOpen 16:cbb726ac20d8 552
MikamiUitOpen 16:cbb726ac20d8 553 char* mbed_gets(char*s, int size, FILE *_file){
MikamiUitOpen 16:cbb726ac20d8 554 #if defined (__ICCARM__)
MikamiUitOpen 16:cbb726ac20d8 555 /*This is only valid for unbuffered streams*/
MikamiUitOpen 16:cbb726ac20d8 556 char *str = fgets(s,size,_file);
MikamiUitOpen 16:cbb726ac20d8 557 if (str!=NULL){
MikamiUitOpen 16:cbb726ac20d8 558 _file->_Mode = (unsigned short)(_file->_Mode & ~ 0x1000);/* Unset read mode */
MikamiUitOpen 16:cbb726ac20d8 559 _file->_Rend = _file->_Wend;
MikamiUitOpen 16:cbb726ac20d8 560 _file->_Next = _file->_Wend;
MikamiUitOpen 16:cbb726ac20d8 561 }
MikamiUitOpen 16:cbb726ac20d8 562 return str;
MikamiUitOpen 16:cbb726ac20d8 563 #else
MikamiUitOpen 16:cbb726ac20d8 564 return std::fgets(s,size,_file);
MikamiUitOpen 16:cbb726ac20d8 565 #endif
MikamiUitOpen 16:cbb726ac20d8 566 }
MikamiUitOpen 16:cbb726ac20d8 567
MikamiUitOpen 16:cbb726ac20d8 568 } // namespace mbed