mbed library for NZ32-SC151

Committer:
modtronix-com
Date:
Fri Aug 19 15:46:42 2016 +1000
Revision:
17:639ed60ce759
Parent:
1:71204b8406f2
Added tag v1.1 for changeset 076cbe3e55be

Who changed what in which revision?

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