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:
Thu Apr 27 23:55:53 2017 +0000
Revision:
16:d60e5187fd31
Parent:
2:fcba17e3d573
17

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