fft

Dependencies:   BSP_DISCO_F746NG SDFileSystem_Warning_Fixed

Committer:
aria19970520
Date:
Fri Mar 06 06:52:14 2020 +0000
Revision:
3:6f7e05dd8d15
Parent:
0:3e46577dc273
fft

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