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