SPKT

Dependencies:   F746_GUI SD_PlayerSkeleton F746_SAI_IO

Committer:
phungductung
Date:
Tue Jun 04 21:37:21 2019 +0000
Revision:
0:8ede47d38d10
SPKT

Who changed what in which revision?

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