Maxim mbed development library

Dependents:   sensomed

Committer:
switches
Date:
Tue Nov 08 18:27:11 2016 +0000
Revision:
0:0e018d759a2a
Initial commit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
switches 0:0e018d759a2a 1 /* mbed Microcontroller Library
switches 0:0e018d759a2a 2 * Copyright (c) 2006-2015 ARM Limited
switches 0:0e018d759a2a 3 *
switches 0:0e018d759a2a 4 * Licensed under the Apache License, Version 2.0 (the "License");
switches 0:0e018d759a2a 5 * you may not use this file except in compliance with the License.
switches 0:0e018d759a2a 6 * You may obtain a copy of the License at
switches 0:0e018d759a2a 7 *
switches 0:0e018d759a2a 8 * http://www.apache.org/licenses/LICENSE-2.0
switches 0:0e018d759a2a 9 *
switches 0:0e018d759a2a 10 * Unless required by applicable law or agreed to in writing, software
switches 0:0e018d759a2a 11 * distributed under the License is distributed on an "AS IS" BASIS,
switches 0:0e018d759a2a 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
switches 0:0e018d759a2a 13 * See the License for the specific language governing permissions and
switches 0:0e018d759a2a 14 * limitations under the License.
switches 0:0e018d759a2a 15 */
switches 0:0e018d759a2a 16 #include "platform/platform.h"
switches 0:0e018d759a2a 17 #include "drivers/FileHandle.h"
switches 0:0e018d759a2a 18 #include "drivers/FileSystemLike.h"
switches 0:0e018d759a2a 19 #include "drivers/FilePath.h"
switches 0:0e018d759a2a 20 #include "hal/serial_api.h"
switches 0:0e018d759a2a 21 #include "platform/toolchain.h"
switches 0:0e018d759a2a 22 #include "platform/semihost_api.h"
switches 0:0e018d759a2a 23 #include "platform/mbed_interface.h"
switches 0:0e018d759a2a 24 #include "platform/SingletonPtr.h"
switches 0:0e018d759a2a 25 #include "platform/PlatformMutex.h"
switches 0:0e018d759a2a 26 #include "platform/mbed_error.h"
switches 0:0e018d759a2a 27 #include "platform/mbed_stats.h"
switches 0:0e018d759a2a 28 #include <stdlib.h>
switches 0:0e018d759a2a 29 #include <string.h>
switches 0:0e018d759a2a 30 #if DEVICE_STDIO_MESSAGES
switches 0:0e018d759a2a 31 #include <stdio.h>
switches 0:0e018d759a2a 32 #endif
switches 0:0e018d759a2a 33 #include <errno.h>
switches 0:0e018d759a2a 34
switches 0:0e018d759a2a 35 #if defined(__ARMCC_VERSION)
switches 0:0e018d759a2a 36 # include <rt_sys.h>
switches 0:0e018d759a2a 37 # define PREFIX(x) _sys##x
switches 0:0e018d759a2a 38 # define OPEN_MAX _SYS_OPEN
switches 0:0e018d759a2a 39 # ifdef __MICROLIB
switches 0:0e018d759a2a 40 # pragma import(__use_full_stdio)
switches 0:0e018d759a2a 41 # endif
switches 0:0e018d759a2a 42
switches 0:0e018d759a2a 43 #elif defined(__ICCARM__)
switches 0:0e018d759a2a 44 # include <yfuns.h>
switches 0:0e018d759a2a 45 # define PREFIX(x) _##x
switches 0:0e018d759a2a 46 # define OPEN_MAX 16
switches 0:0e018d759a2a 47
switches 0:0e018d759a2a 48 # define STDIN_FILENO 0
switches 0:0e018d759a2a 49 # define STDOUT_FILENO 1
switches 0:0e018d759a2a 50 # define STDERR_FILENO 2
switches 0:0e018d759a2a 51
switches 0:0e018d759a2a 52 #else
switches 0:0e018d759a2a 53 # include <sys/stat.h>
switches 0:0e018d759a2a 54 # include <sys/unistd.h>
switches 0:0e018d759a2a 55 # include <sys/syslimits.h>
switches 0:0e018d759a2a 56 # define PREFIX(x) x
switches 0:0e018d759a2a 57 #endif
switches 0:0e018d759a2a 58
switches 0:0e018d759a2a 59 #define FILE_HANDLE_RESERVED 0xFFFFFFFF
switches 0:0e018d759a2a 60
switches 0:0e018d759a2a 61 using namespace mbed;
switches 0:0e018d759a2a 62
switches 0:0e018d759a2a 63 #if defined(__MICROLIB) && (__ARMCC_VERSION>5030000)
switches 0:0e018d759a2a 64 // Before version 5.03, we were using a patched version of microlib with proper names
switches 0:0e018d759a2a 65 extern const char __stdin_name[] = ":tt";
switches 0:0e018d759a2a 66 extern const char __stdout_name[] = ":tt";
switches 0:0e018d759a2a 67 extern const char __stderr_name[] = ":tt";
switches 0:0e018d759a2a 68
switches 0:0e018d759a2a 69 #else
switches 0:0e018d759a2a 70 extern const char __stdin_name[] = "/stdin";
switches 0:0e018d759a2a 71 extern const char __stdout_name[] = "/stdout";
switches 0:0e018d759a2a 72 extern const char __stderr_name[] = "/stderr";
switches 0:0e018d759a2a 73 #endif
switches 0:0e018d759a2a 74
switches 0:0e018d759a2a 75 // Heap limits - only used if set
switches 0:0e018d759a2a 76 unsigned char *mbed_heap_start = 0;
switches 0:0e018d759a2a 77 uint32_t mbed_heap_size = 0;
switches 0:0e018d759a2a 78
switches 0:0e018d759a2a 79 /* newlib has the filehandle field in the FILE struct as a short, so
switches 0:0e018d759a2a 80 * we can't just return a Filehandle* from _open and instead have to
switches 0:0e018d759a2a 81 * put it in a filehandles array and return the index into that array
switches 0:0e018d759a2a 82 * (or rather index+3, as filehandles 0-2 are stdin/out/err).
switches 0:0e018d759a2a 83 */
switches 0:0e018d759a2a 84 static FileHandle *filehandles[OPEN_MAX];
switches 0:0e018d759a2a 85 static SingletonPtr<PlatformMutex> filehandle_mutex;
switches 0:0e018d759a2a 86
switches 0:0e018d759a2a 87 FileHandle::~FileHandle() {
switches 0:0e018d759a2a 88 filehandle_mutex->lock();
switches 0:0e018d759a2a 89 /* Remove all open filehandles for this */
switches 0:0e018d759a2a 90 for (unsigned int fh_i = 0; fh_i < sizeof(filehandles)/sizeof(*filehandles); fh_i++) {
switches 0:0e018d759a2a 91 if (filehandles[fh_i] == this) {
switches 0:0e018d759a2a 92 filehandles[fh_i] = NULL;
switches 0:0e018d759a2a 93 }
switches 0:0e018d759a2a 94 }
switches 0:0e018d759a2a 95 filehandle_mutex->unlock();
switches 0:0e018d759a2a 96 }
switches 0:0e018d759a2a 97
switches 0:0e018d759a2a 98 #if DEVICE_SERIAL
switches 0:0e018d759a2a 99 extern int stdio_uart_inited;
switches 0:0e018d759a2a 100 extern serial_t stdio_uart;
switches 0:0e018d759a2a 101 #if MBED_CONF_PLATFORM_STDIO_CONVERT_NEWLINES
switches 0:0e018d759a2a 102 static char stdio_in_prev;
switches 0:0e018d759a2a 103 static char stdio_out_prev;
switches 0:0e018d759a2a 104 #endif
switches 0:0e018d759a2a 105 #endif
switches 0:0e018d759a2a 106
switches 0:0e018d759a2a 107 static void init_serial() {
switches 0:0e018d759a2a 108 #if DEVICE_SERIAL
switches 0:0e018d759a2a 109 if (stdio_uart_inited) return;
switches 0:0e018d759a2a 110 serial_init(&stdio_uart, STDIO_UART_TX, STDIO_UART_RX);
switches 0:0e018d759a2a 111 #if MBED_CONF_PLATFORM_STDIO_BAUD_RATE
switches 0:0e018d759a2a 112 serial_baud(&stdio_uart, MBED_CONF_PLATFORM_STDIO_BAUD_RATE);
switches 0:0e018d759a2a 113 #endif
switches 0:0e018d759a2a 114 #endif
switches 0:0e018d759a2a 115 }
switches 0:0e018d759a2a 116
switches 0:0e018d759a2a 117 static inline int openmode_to_posix(int openmode) {
switches 0:0e018d759a2a 118 int posix = openmode;
switches 0:0e018d759a2a 119 #ifdef __ARMCC_VERSION
switches 0:0e018d759a2a 120 if (openmode & OPEN_PLUS) {
switches 0:0e018d759a2a 121 posix = O_RDWR;
switches 0:0e018d759a2a 122 } else if(openmode & OPEN_W) {
switches 0:0e018d759a2a 123 posix = O_WRONLY;
switches 0:0e018d759a2a 124 } else if(openmode & OPEN_A) {
switches 0:0e018d759a2a 125 posix = O_WRONLY|O_APPEND;
switches 0:0e018d759a2a 126 } else {
switches 0:0e018d759a2a 127 posix = O_RDONLY;
switches 0:0e018d759a2a 128 }
switches 0:0e018d759a2a 129 /* a, w, a+, w+ all create if file does not already exist */
switches 0:0e018d759a2a 130 if (openmode & (OPEN_A|OPEN_W)) {
switches 0:0e018d759a2a 131 posix |= O_CREAT;
switches 0:0e018d759a2a 132 }
switches 0:0e018d759a2a 133 /* w and w+ truncate */
switches 0:0e018d759a2a 134 if (openmode & OPEN_W) {
switches 0:0e018d759a2a 135 posix |= O_TRUNC;
switches 0:0e018d759a2a 136 }
switches 0:0e018d759a2a 137 #elif defined(__ICCARM__)
switches 0:0e018d759a2a 138 switch (openmode & _LLIO_RDWRMASK) {
switches 0:0e018d759a2a 139 case _LLIO_RDONLY: posix = O_RDONLY; break;
switches 0:0e018d759a2a 140 case _LLIO_WRONLY: posix = O_WRONLY; break;
switches 0:0e018d759a2a 141 case _LLIO_RDWR : posix = O_RDWR ; break;
switches 0:0e018d759a2a 142 }
switches 0:0e018d759a2a 143 if (openmode & _LLIO_CREAT ) posix |= O_CREAT;
switches 0:0e018d759a2a 144 if (openmode & _LLIO_APPEND) posix |= O_APPEND;
switches 0:0e018d759a2a 145 if (openmode & _LLIO_TRUNC ) posix |= O_TRUNC;
switches 0:0e018d759a2a 146 #elif defined(TOOLCHAIN_GCC)
switches 0:0e018d759a2a 147 posix &= ~O_BINARY;
switches 0:0e018d759a2a 148 #endif
switches 0:0e018d759a2a 149 return posix;
switches 0:0e018d759a2a 150 }
switches 0:0e018d759a2a 151
switches 0:0e018d759a2a 152 extern "C" WEAK void mbed_sdk_init(void);
switches 0:0e018d759a2a 153 extern "C" WEAK void mbed_sdk_init(void) {
switches 0:0e018d759a2a 154 }
switches 0:0e018d759a2a 155
switches 0:0e018d759a2a 156 extern "C" FILEHANDLE PREFIX(_open)(const char* name, int openmode) {
switches 0:0e018d759a2a 157 #if defined(__MICROLIB) && (__ARMCC_VERSION>5030000)
switches 0:0e018d759a2a 158 // Before version 5.03, we were using a patched version of microlib with proper names
switches 0:0e018d759a2a 159 // This is the workaround that the microlib author suggested us
switches 0:0e018d759a2a 160 static int n = 0;
switches 0:0e018d759a2a 161 static int mbed_sdk_inited = 0;
switches 0:0e018d759a2a 162 if (!mbed_sdk_inited) {
switches 0:0e018d759a2a 163 mbed_sdk_inited = 1;
switches 0:0e018d759a2a 164 mbed_sdk_init();
switches 0:0e018d759a2a 165 }
switches 0:0e018d759a2a 166 if (!std::strcmp(name, ":tt")) return n++;
switches 0:0e018d759a2a 167 #else
switches 0:0e018d759a2a 168 /* Use the posix convention that stdin,out,err are filehandles 0,1,2.
switches 0:0e018d759a2a 169 */
switches 0:0e018d759a2a 170 if (std::strcmp(name, __stdin_name) == 0) {
switches 0:0e018d759a2a 171 init_serial();
switches 0:0e018d759a2a 172 return 0;
switches 0:0e018d759a2a 173 } else if (std::strcmp(name, __stdout_name) == 0) {
switches 0:0e018d759a2a 174 init_serial();
switches 0:0e018d759a2a 175 return 1;
switches 0:0e018d759a2a 176 } else if (std::strcmp(name, __stderr_name) == 0) {
switches 0:0e018d759a2a 177 init_serial();
switches 0:0e018d759a2a 178 return 2;
switches 0:0e018d759a2a 179 }
switches 0:0e018d759a2a 180 #endif
switches 0:0e018d759a2a 181
switches 0:0e018d759a2a 182 // find the first empty slot in filehandles
switches 0:0e018d759a2a 183 filehandle_mutex->lock();
switches 0:0e018d759a2a 184 unsigned int fh_i;
switches 0:0e018d759a2a 185 for (fh_i = 0; fh_i < sizeof(filehandles)/sizeof(*filehandles); fh_i++) {
switches 0:0e018d759a2a 186 if (filehandles[fh_i] == NULL) break;
switches 0:0e018d759a2a 187 }
switches 0:0e018d759a2a 188 if (fh_i >= sizeof(filehandles)/sizeof(*filehandles)) {
switches 0:0e018d759a2a 189 filehandle_mutex->unlock();
switches 0:0e018d759a2a 190 return -1;
switches 0:0e018d759a2a 191 }
switches 0:0e018d759a2a 192 filehandles[fh_i] = (FileHandle*)FILE_HANDLE_RESERVED;
switches 0:0e018d759a2a 193 filehandle_mutex->unlock();
switches 0:0e018d759a2a 194
switches 0:0e018d759a2a 195 FileHandle *res;
switches 0:0e018d759a2a 196
switches 0:0e018d759a2a 197 /* FILENAME: ":0x12345678" describes a FileLike* */
switches 0:0e018d759a2a 198 if (name[0] == ':') {
switches 0:0e018d759a2a 199 void *p;
switches 0:0e018d759a2a 200 sscanf(name, ":%p", &p);
switches 0:0e018d759a2a 201 res = (FileHandle*)p;
switches 0:0e018d759a2a 202
switches 0:0e018d759a2a 203 /* FILENAME: "/file_system/file_name" */
switches 0:0e018d759a2a 204 } else {
switches 0:0e018d759a2a 205 FilePath path(name);
switches 0:0e018d759a2a 206
switches 0:0e018d759a2a 207 if (!path.exists()) {
switches 0:0e018d759a2a 208 // Free file handle
switches 0:0e018d759a2a 209 filehandles[fh_i] = NULL;
switches 0:0e018d759a2a 210 return -1;
switches 0:0e018d759a2a 211 } else if (path.isFile()) {
switches 0:0e018d759a2a 212 res = path.file();
switches 0:0e018d759a2a 213 } else {
switches 0:0e018d759a2a 214 FileSystemLike *fs = path.fileSystem();
switches 0:0e018d759a2a 215 if (fs == NULL) {
switches 0:0e018d759a2a 216 // Free file handle
switches 0:0e018d759a2a 217 filehandles[fh_i] = NULL;
switches 0:0e018d759a2a 218 return -1;
switches 0:0e018d759a2a 219 }
switches 0:0e018d759a2a 220 int posix_mode = openmode_to_posix(openmode);
switches 0:0e018d759a2a 221 res = fs->open(path.fileName(), posix_mode); /* NULL if fails */
switches 0:0e018d759a2a 222 }
switches 0:0e018d759a2a 223 }
switches 0:0e018d759a2a 224
switches 0:0e018d759a2a 225 if (res == NULL) {
switches 0:0e018d759a2a 226 // Free file handle
switches 0:0e018d759a2a 227 filehandles[fh_i] = NULL;
switches 0:0e018d759a2a 228 return -1;
switches 0:0e018d759a2a 229 }
switches 0:0e018d759a2a 230 filehandles[fh_i] = res;
switches 0:0e018d759a2a 231
switches 0:0e018d759a2a 232 return fh_i + 3; // +3 as filehandles 0-2 are stdin/out/err
switches 0:0e018d759a2a 233 }
switches 0:0e018d759a2a 234
switches 0:0e018d759a2a 235 extern "C" int PREFIX(_close)(FILEHANDLE fh) {
switches 0:0e018d759a2a 236 if (fh < 3) return 0;
switches 0:0e018d759a2a 237
switches 0:0e018d759a2a 238 FileHandle* fhc = filehandles[fh-3];
switches 0:0e018d759a2a 239 filehandles[fh-3] = NULL;
switches 0:0e018d759a2a 240 if (fhc == NULL) return -1;
switches 0:0e018d759a2a 241
switches 0:0e018d759a2a 242 return fhc->close();
switches 0:0e018d759a2a 243 }
switches 0:0e018d759a2a 244
switches 0:0e018d759a2a 245 #if defined(__ICCARM__)
switches 0:0e018d759a2a 246 extern "C" size_t __write (int fh, const unsigned char *buffer, size_t length) {
switches 0:0e018d759a2a 247 #else
switches 0:0e018d759a2a 248 extern "C" int PREFIX(_write)(FILEHANDLE fh, const unsigned char *buffer, unsigned int length, int mode) {
switches 0:0e018d759a2a 249 #endif
switches 0:0e018d759a2a 250 int n; // n is the number of bytes written
switches 0:0e018d759a2a 251 if (fh < 3) {
switches 0:0e018d759a2a 252 #if DEVICE_SERIAL
switches 0:0e018d759a2a 253 if (!stdio_uart_inited) init_serial();
switches 0:0e018d759a2a 254 #if MBED_CONF_PLATFORM_STDIO_CONVERT_NEWLINES
switches 0:0e018d759a2a 255 for (unsigned int i = 0; i < length; i++) {
switches 0:0e018d759a2a 256 if (buffer[i] == '\n' && stdio_out_prev != '\r') {
switches 0:0e018d759a2a 257 serial_putc(&stdio_uart, '\r');
switches 0:0e018d759a2a 258 }
switches 0:0e018d759a2a 259 serial_putc(&stdio_uart, buffer[i]);
switches 0:0e018d759a2a 260 stdio_out_prev = buffer[i];
switches 0:0e018d759a2a 261 }
switches 0:0e018d759a2a 262 #else
switches 0:0e018d759a2a 263 for (unsigned int i = 0; i < length; i++) {
switches 0:0e018d759a2a 264 serial_putc(&stdio_uart, buffer[i]);
switches 0:0e018d759a2a 265 }
switches 0:0e018d759a2a 266 #endif
switches 0:0e018d759a2a 267 #endif
switches 0:0e018d759a2a 268 n = length;
switches 0:0e018d759a2a 269 } else {
switches 0:0e018d759a2a 270 FileHandle* fhc = filehandles[fh-3];
switches 0:0e018d759a2a 271 if (fhc == NULL) return -1;
switches 0:0e018d759a2a 272
switches 0:0e018d759a2a 273 n = fhc->write(buffer, length);
switches 0:0e018d759a2a 274 }
switches 0:0e018d759a2a 275 #ifdef __ARMCC_VERSION
switches 0:0e018d759a2a 276 return length-n;
switches 0:0e018d759a2a 277 #else
switches 0:0e018d759a2a 278 return n;
switches 0:0e018d759a2a 279 #endif
switches 0:0e018d759a2a 280 }
switches 0:0e018d759a2a 281
switches 0:0e018d759a2a 282 #if defined(__ICCARM__)
switches 0:0e018d759a2a 283 extern "C" size_t __read (int fh, unsigned char *buffer, size_t length) {
switches 0:0e018d759a2a 284 #else
switches 0:0e018d759a2a 285 extern "C" int PREFIX(_read)(FILEHANDLE fh, unsigned char *buffer, unsigned int length, int mode) {
switches 0:0e018d759a2a 286 #endif
switches 0:0e018d759a2a 287 int n; // n is the number of bytes read
switches 0:0e018d759a2a 288 if (fh < 3) {
switches 0:0e018d759a2a 289 // only read a character at a time from stdin
switches 0:0e018d759a2a 290 #if DEVICE_SERIAL
switches 0:0e018d759a2a 291 if (!stdio_uart_inited) init_serial();
switches 0:0e018d759a2a 292 #if MBED_CONF_PLATFORM_STDIO_CONVERT_NEWLINES
switches 0:0e018d759a2a 293 while (true) {
switches 0:0e018d759a2a 294 char c = serial_getc(&stdio_uart);
switches 0:0e018d759a2a 295 if ((c == '\r' && stdio_in_prev != '\n') ||
switches 0:0e018d759a2a 296 (c == '\n' && stdio_in_prev != '\r')) {
switches 0:0e018d759a2a 297 stdio_in_prev = c;
switches 0:0e018d759a2a 298 *buffer = '\n';
switches 0:0e018d759a2a 299 break;
switches 0:0e018d759a2a 300 } else if ((c == '\r' && stdio_in_prev == '\n') ||
switches 0:0e018d759a2a 301 (c == '\n' && stdio_in_prev == '\r')) {
switches 0:0e018d759a2a 302 stdio_in_prev = c;
switches 0:0e018d759a2a 303 // onto next character
switches 0:0e018d759a2a 304 continue;
switches 0:0e018d759a2a 305 } else {
switches 0:0e018d759a2a 306 stdio_in_prev = c;
switches 0:0e018d759a2a 307 *buffer = c;
switches 0:0e018d759a2a 308 break;
switches 0:0e018d759a2a 309 }
switches 0:0e018d759a2a 310 }
switches 0:0e018d759a2a 311 #else
switches 0:0e018d759a2a 312 *buffer = serial_getc(&stdio_uart);
switches 0:0e018d759a2a 313 #endif
switches 0:0e018d759a2a 314 #endif
switches 0:0e018d759a2a 315 n = 1;
switches 0:0e018d759a2a 316 } else {
switches 0:0e018d759a2a 317 FileHandle* fhc = filehandles[fh-3];
switches 0:0e018d759a2a 318 if (fhc == NULL) return -1;
switches 0:0e018d759a2a 319
switches 0:0e018d759a2a 320 n = fhc->read(buffer, length);
switches 0:0e018d759a2a 321 }
switches 0:0e018d759a2a 322 #ifdef __ARMCC_VERSION
switches 0:0e018d759a2a 323 return length-n;
switches 0:0e018d759a2a 324 #else
switches 0:0e018d759a2a 325 return n;
switches 0:0e018d759a2a 326 #endif
switches 0:0e018d759a2a 327 }
switches 0:0e018d759a2a 328
switches 0:0e018d759a2a 329 #ifdef __ARMCC_VERSION
switches 0:0e018d759a2a 330 extern "C" int PREFIX(_istty)(FILEHANDLE fh)
switches 0:0e018d759a2a 331 #else
switches 0:0e018d759a2a 332 extern "C" int _isatty(FILEHANDLE fh)
switches 0:0e018d759a2a 333 #endif
switches 0:0e018d759a2a 334 {
switches 0:0e018d759a2a 335 /* stdin, stdout and stderr should be tty */
switches 0:0e018d759a2a 336 if (fh < 3) return 1;
switches 0:0e018d759a2a 337
switches 0:0e018d759a2a 338 FileHandle* fhc = filehandles[fh-3];
switches 0:0e018d759a2a 339 if (fhc == NULL) return -1;
switches 0:0e018d759a2a 340
switches 0:0e018d759a2a 341 return fhc->isatty();
switches 0:0e018d759a2a 342 }
switches 0:0e018d759a2a 343
switches 0:0e018d759a2a 344 extern "C"
switches 0:0e018d759a2a 345 #if defined(__ARMCC_VERSION)
switches 0:0e018d759a2a 346 int _sys_seek(FILEHANDLE fh, long position)
switches 0:0e018d759a2a 347 #elif defined(__ICCARM__)
switches 0:0e018d759a2a 348 long __lseek(int fh, long offset, int whence)
switches 0:0e018d759a2a 349 #else
switches 0:0e018d759a2a 350 int _lseek(FILEHANDLE fh, int offset, int whence)
switches 0:0e018d759a2a 351 #endif
switches 0:0e018d759a2a 352 {
switches 0:0e018d759a2a 353 if (fh < 3) return 0;
switches 0:0e018d759a2a 354
switches 0:0e018d759a2a 355 FileHandle* fhc = filehandles[fh-3];
switches 0:0e018d759a2a 356 if (fhc == NULL) return -1;
switches 0:0e018d759a2a 357
switches 0:0e018d759a2a 358 #if defined(__ARMCC_VERSION)
switches 0:0e018d759a2a 359 return fhc->lseek(position, SEEK_SET);
switches 0:0e018d759a2a 360 #else
switches 0:0e018d759a2a 361 return fhc->lseek(offset, whence);
switches 0:0e018d759a2a 362 #endif
switches 0:0e018d759a2a 363 }
switches 0:0e018d759a2a 364
switches 0:0e018d759a2a 365 #ifdef __ARMCC_VERSION
switches 0:0e018d759a2a 366 extern "C" int PREFIX(_ensure)(FILEHANDLE fh) {
switches 0:0e018d759a2a 367 if (fh < 3) return 0;
switches 0:0e018d759a2a 368
switches 0:0e018d759a2a 369 FileHandle* fhc = filehandles[fh-3];
switches 0:0e018d759a2a 370 if (fhc == NULL) return -1;
switches 0:0e018d759a2a 371
switches 0:0e018d759a2a 372 return fhc->fsync();
switches 0:0e018d759a2a 373 }
switches 0:0e018d759a2a 374
switches 0:0e018d759a2a 375 extern "C" long PREFIX(_flen)(FILEHANDLE fh) {
switches 0:0e018d759a2a 376 if (fh < 3) return 0;
switches 0:0e018d759a2a 377
switches 0:0e018d759a2a 378 FileHandle* fhc = filehandles[fh-3];
switches 0:0e018d759a2a 379 if (fhc == NULL) return -1;
switches 0:0e018d759a2a 380
switches 0:0e018d759a2a 381 return fhc->flen();
switches 0:0e018d759a2a 382 }
switches 0:0e018d759a2a 383 #endif
switches 0:0e018d759a2a 384
switches 0:0e018d759a2a 385
switches 0:0e018d759a2a 386 #if !defined(__ARMCC_VERSION) && !defined(__ICCARM__)
switches 0:0e018d759a2a 387 extern "C" int _fstat(int fd, struct stat *st) {
switches 0:0e018d759a2a 388 if ((STDOUT_FILENO == fd) || (STDERR_FILENO == fd) || (STDIN_FILENO == fd)) {
switches 0:0e018d759a2a 389 st->st_mode = S_IFCHR;
switches 0:0e018d759a2a 390 return 0;
switches 0:0e018d759a2a 391 }
switches 0:0e018d759a2a 392
switches 0:0e018d759a2a 393 errno = EBADF;
switches 0:0e018d759a2a 394 return -1;
switches 0:0e018d759a2a 395 }
switches 0:0e018d759a2a 396 #endif
switches 0:0e018d759a2a 397
switches 0:0e018d759a2a 398 namespace std {
switches 0:0e018d759a2a 399 extern "C" int remove(const char *path) {
switches 0:0e018d759a2a 400 FilePath fp(path);
switches 0:0e018d759a2a 401 FileSystemLike *fs = fp.fileSystem();
switches 0:0e018d759a2a 402 if (fs == NULL) return -1;
switches 0:0e018d759a2a 403
switches 0:0e018d759a2a 404 return fs->remove(fp.fileName());
switches 0:0e018d759a2a 405 }
switches 0:0e018d759a2a 406
switches 0:0e018d759a2a 407 extern "C" int rename(const char *oldname, const char *newname) {
switches 0:0e018d759a2a 408 FilePath fpOld(oldname);
switches 0:0e018d759a2a 409 FilePath fpNew(newname);
switches 0:0e018d759a2a 410 FileSystemLike *fsOld = fpOld.fileSystem();
switches 0:0e018d759a2a 411 FileSystemLike *fsNew = fpNew.fileSystem();
switches 0:0e018d759a2a 412
switches 0:0e018d759a2a 413 /* rename only if both files are on the same FS */
switches 0:0e018d759a2a 414 if (fsOld != fsNew || fsOld == NULL) return -1;
switches 0:0e018d759a2a 415
switches 0:0e018d759a2a 416 return fsOld->rename(fpOld.fileName(), fpNew.fileName());
switches 0:0e018d759a2a 417 }
switches 0:0e018d759a2a 418
switches 0:0e018d759a2a 419 extern "C" char *tmpnam(char *s) {
switches 0:0e018d759a2a 420 return NULL;
switches 0:0e018d759a2a 421 }
switches 0:0e018d759a2a 422
switches 0:0e018d759a2a 423 extern "C" FILE *tmpfile() {
switches 0:0e018d759a2a 424 return NULL;
switches 0:0e018d759a2a 425 }
switches 0:0e018d759a2a 426 } // namespace std
switches 0:0e018d759a2a 427
switches 0:0e018d759a2a 428 #ifdef __ARMCC_VERSION
switches 0:0e018d759a2a 429 extern "C" char *_sys_command_string(char *cmd, int len) {
switches 0:0e018d759a2a 430 return NULL;
switches 0:0e018d759a2a 431 }
switches 0:0e018d759a2a 432 #endif
switches 0:0e018d759a2a 433
switches 0:0e018d759a2a 434 extern "C" DIR *opendir(const char *path) {
switches 0:0e018d759a2a 435 /* root dir is FileSystemLike */
switches 0:0e018d759a2a 436 if (path[0] == '/' && path[1] == 0) {
switches 0:0e018d759a2a 437 return FileSystemLike::opendir();
switches 0:0e018d759a2a 438 }
switches 0:0e018d759a2a 439
switches 0:0e018d759a2a 440 FilePath fp(path);
switches 0:0e018d759a2a 441 FileSystemLike* fs = fp.fileSystem();
switches 0:0e018d759a2a 442 if (fs == NULL) return NULL;
switches 0:0e018d759a2a 443
switches 0:0e018d759a2a 444 return fs->opendir(fp.fileName());
switches 0:0e018d759a2a 445 }
switches 0:0e018d759a2a 446
switches 0:0e018d759a2a 447 extern "C" struct dirent *readdir(DIR *dir) {
switches 0:0e018d759a2a 448 return dir->readdir();
switches 0:0e018d759a2a 449 }
switches 0:0e018d759a2a 450
switches 0:0e018d759a2a 451 extern "C" int closedir(DIR *dir) {
switches 0:0e018d759a2a 452 return dir->closedir();
switches 0:0e018d759a2a 453 }
switches 0:0e018d759a2a 454
switches 0:0e018d759a2a 455 extern "C" void rewinddir(DIR *dir) {
switches 0:0e018d759a2a 456 dir->rewinddir();
switches 0:0e018d759a2a 457 }
switches 0:0e018d759a2a 458
switches 0:0e018d759a2a 459 extern "C" off_t telldir(DIR *dir) {
switches 0:0e018d759a2a 460 return dir->telldir();
switches 0:0e018d759a2a 461 }
switches 0:0e018d759a2a 462
switches 0:0e018d759a2a 463 extern "C" void seekdir(DIR *dir, off_t off) {
switches 0:0e018d759a2a 464 dir->seekdir(off);
switches 0:0e018d759a2a 465 }
switches 0:0e018d759a2a 466
switches 0:0e018d759a2a 467 extern "C" int mkdir(const char *path, mode_t mode) {
switches 0:0e018d759a2a 468 FilePath fp(path);
switches 0:0e018d759a2a 469 FileSystemLike *fs = fp.fileSystem();
switches 0:0e018d759a2a 470 if (fs == NULL) return -1;
switches 0:0e018d759a2a 471
switches 0:0e018d759a2a 472 return fs->mkdir(fp.fileName(), mode);
switches 0:0e018d759a2a 473 }
switches 0:0e018d759a2a 474
switches 0:0e018d759a2a 475 #if defined(TOOLCHAIN_GCC)
switches 0:0e018d759a2a 476 /* prevents the exception handling name demangling code getting pulled in */
switches 0:0e018d759a2a 477 #include "mbed_error.h"
switches 0:0e018d759a2a 478 namespace __gnu_cxx {
switches 0:0e018d759a2a 479 void __verbose_terminate_handler() {
switches 0:0e018d759a2a 480 error("Exception");
switches 0:0e018d759a2a 481 }
switches 0:0e018d759a2a 482 }
switches 0:0e018d759a2a 483 extern "C" WEAK void __cxa_pure_virtual(void);
switches 0:0e018d759a2a 484 extern "C" WEAK void __cxa_pure_virtual(void) {
switches 0:0e018d759a2a 485 exit(1);
switches 0:0e018d759a2a 486 }
switches 0:0e018d759a2a 487
switches 0:0e018d759a2a 488 #endif
switches 0:0e018d759a2a 489
switches 0:0e018d759a2a 490 #if defined(TOOLCHAIN_GCC)
switches 0:0e018d759a2a 491
switches 0:0e018d759a2a 492 #ifdef FEATURE_UVISOR
switches 0:0e018d759a2a 493 #include "uvisor-lib/uvisor-lib.h"
switches 0:0e018d759a2a 494 #endif/* FEATURE_UVISOR */
switches 0:0e018d759a2a 495
switches 0:0e018d759a2a 496
switches 0:0e018d759a2a 497 extern "C" WEAK void software_init_hook_rtos(void)
switches 0:0e018d759a2a 498 {
switches 0:0e018d759a2a 499 // Do nothing by default.
switches 0:0e018d759a2a 500 }
switches 0:0e018d759a2a 501
switches 0:0e018d759a2a 502 extern "C" void software_init_hook(void)
switches 0:0e018d759a2a 503 {
switches 0:0e018d759a2a 504 #ifdef FEATURE_UVISOR
switches 0:0e018d759a2a 505 int return_code;
switches 0:0e018d759a2a 506
switches 0:0e018d759a2a 507 return_code = uvisor_lib_init();
switches 0:0e018d759a2a 508 if (return_code) {
switches 0:0e018d759a2a 509 mbed_die();
switches 0:0e018d759a2a 510 }
switches 0:0e018d759a2a 511 #endif/* FEATURE_UVISOR */
switches 0:0e018d759a2a 512 mbed_sdk_init();
switches 0:0e018d759a2a 513 software_init_hook_rtos();
switches 0:0e018d759a2a 514 }
switches 0:0e018d759a2a 515 #endif
switches 0:0e018d759a2a 516
switches 0:0e018d759a2a 517 // ****************************************************************************
switches 0:0e018d759a2a 518 // mbed_main is a function that is called before main()
switches 0:0e018d759a2a 519 // mbed_sdk_init() is also a function that is called before main(), but unlike
switches 0:0e018d759a2a 520 // mbed_main(), it is not meant for user code, but for the SDK itself to perform
switches 0:0e018d759a2a 521 // initializations before main() is called.
switches 0:0e018d759a2a 522
switches 0:0e018d759a2a 523 extern "C" WEAK void mbed_main(void);
switches 0:0e018d759a2a 524 extern "C" WEAK void mbed_main(void) {
switches 0:0e018d759a2a 525 }
switches 0:0e018d759a2a 526
switches 0:0e018d759a2a 527 #if defined(TOOLCHAIN_ARM)
switches 0:0e018d759a2a 528 extern "C" int $Super$$main(void);
switches 0:0e018d759a2a 529
switches 0:0e018d759a2a 530 extern "C" int $Sub$$main(void) {
switches 0:0e018d759a2a 531 mbed_main();
switches 0:0e018d759a2a 532 return $Super$$main();
switches 0:0e018d759a2a 533 }
switches 0:0e018d759a2a 534
switches 0:0e018d759a2a 535 extern "C" void _platform_post_stackheap_init (void) {
switches 0:0e018d759a2a 536 mbed_sdk_init();
switches 0:0e018d759a2a 537 }
switches 0:0e018d759a2a 538
switches 0:0e018d759a2a 539 #elif defined(TOOLCHAIN_GCC)
switches 0:0e018d759a2a 540 extern "C" int __real_main(void);
switches 0:0e018d759a2a 541
switches 0:0e018d759a2a 542 extern "C" int __wrap_main(void) {
switches 0:0e018d759a2a 543 mbed_main();
switches 0:0e018d759a2a 544 return __real_main();
switches 0:0e018d759a2a 545 }
switches 0:0e018d759a2a 546 #elif defined(TOOLCHAIN_IAR)
switches 0:0e018d759a2a 547 // IAR doesn't have the $Super/$Sub mechanism of armcc, nor something equivalent
switches 0:0e018d759a2a 548 // to ld's --wrap. It does have a --redirect, but that doesn't help, since redirecting
switches 0:0e018d759a2a 549 // 'main' to another symbol looses the original 'main' symbol. However, its startup
switches 0:0e018d759a2a 550 // code will call a function to setup argc and argv (__iar_argc_argv) if it is defined.
switches 0:0e018d759a2a 551 // Since mbed doesn't use argc/argv, we use this function to call our mbed_main.
switches 0:0e018d759a2a 552 extern "C" void __iar_argc_argv() {
switches 0:0e018d759a2a 553 mbed_main();
switches 0:0e018d759a2a 554 }
switches 0:0e018d759a2a 555 #endif
switches 0:0e018d759a2a 556
switches 0:0e018d759a2a 557 // Provide implementation of _sbrk (low-level dynamic memory allocation
switches 0:0e018d759a2a 558 // routine) for GCC_ARM which compares new heap pointer with MSP instead of
switches 0:0e018d759a2a 559 // SP. This make it compatible with RTX RTOS thread stacks.
switches 0:0e018d759a2a 560 #if defined(TOOLCHAIN_GCC_ARM) || defined(TOOLCHAIN_GCC_CR)
switches 0:0e018d759a2a 561 // Linker defined symbol used by _sbrk to indicate where heap should start.
switches 0:0e018d759a2a 562 extern "C" int __end__;
switches 0:0e018d759a2a 563
switches 0:0e018d759a2a 564 #if defined(TARGET_CORTEX_A)
switches 0:0e018d759a2a 565 extern "C" uint32_t __HeapLimit;
switches 0:0e018d759a2a 566 #endif
switches 0:0e018d759a2a 567
switches 0:0e018d759a2a 568 // Turn off the errno macro and use actual global variable instead.
switches 0:0e018d759a2a 569 #undef errno
switches 0:0e018d759a2a 570 extern "C" int errno;
switches 0:0e018d759a2a 571
switches 0:0e018d759a2a 572 // For ARM7 only
switches 0:0e018d759a2a 573 register unsigned char * stack_ptr __asm ("sp");
switches 0:0e018d759a2a 574
switches 0:0e018d759a2a 575 // Dynamic memory allocation related syscall.
switches 0:0e018d759a2a 576 #if defined(TARGET_NUMAKER_PFM_NUC472) || defined(TARGET_NUMAKER_PFM_M453)
switches 0:0e018d759a2a 577 // Overwrite _sbrk() to support two region model (heap and stack are two distinct regions).
switches 0:0e018d759a2a 578 // __wrap__sbrk() is implemented in:
switches 0:0e018d759a2a 579 // TARGET_NUMAKER_PFM_NUC472 hal/targets/cmsis/TARGET_NUVOTON/TARGET_NUC472/TARGET_NUMAKER_PFM_NUC472/TOOLCHAIN_GCC_ARM/retarget.c
switches 0:0e018d759a2a 580 // TARGET_NUMAKER_PFM_M453 hal/targets/cmsis/TARGET_NUVOTON/TARGET_M451/TARGET_NUMAKER_PFM_M453/TOOLCHAIN_GCC_ARM/retarget.c
switches 0:0e018d759a2a 581 extern "C" void *__wrap__sbrk(int incr);
switches 0:0e018d759a2a 582 extern "C" caddr_t _sbrk(int incr) {
switches 0:0e018d759a2a 583 return (caddr_t) __wrap__sbrk(incr);
switches 0:0e018d759a2a 584 }
switches 0:0e018d759a2a 585 #else
switches 0:0e018d759a2a 586 extern "C" caddr_t _sbrk(int incr) {
switches 0:0e018d759a2a 587 static unsigned char* heap = (unsigned char*)&__end__;
switches 0:0e018d759a2a 588 unsigned char* prev_heap = heap;
switches 0:0e018d759a2a 589 unsigned char* new_heap = heap + incr;
switches 0:0e018d759a2a 590
switches 0:0e018d759a2a 591 #if defined(TARGET_ARM7)
switches 0:0e018d759a2a 592 if (new_heap >= stack_ptr) {
switches 0:0e018d759a2a 593 #elif defined(TARGET_CORTEX_A)
switches 0:0e018d759a2a 594 if (new_heap >= (unsigned char*)&__HeapLimit) { /* __HeapLimit is end of heap section */
switches 0:0e018d759a2a 595 #else
switches 0:0e018d759a2a 596 if (new_heap >= (unsigned char*)__get_MSP()) {
switches 0:0e018d759a2a 597 #endif
switches 0:0e018d759a2a 598 errno = ENOMEM;
switches 0:0e018d759a2a 599 return (caddr_t)-1;
switches 0:0e018d759a2a 600 }
switches 0:0e018d759a2a 601
switches 0:0e018d759a2a 602 // Additional heap checking if set
switches 0:0e018d759a2a 603 if (mbed_heap_size && (new_heap >= mbed_heap_start + mbed_heap_size)) {
switches 0:0e018d759a2a 604 errno = ENOMEM;
switches 0:0e018d759a2a 605 return (caddr_t)-1;
switches 0:0e018d759a2a 606 }
switches 0:0e018d759a2a 607
switches 0:0e018d759a2a 608 heap = new_heap;
switches 0:0e018d759a2a 609 return (caddr_t) prev_heap;
switches 0:0e018d759a2a 610 }
switches 0:0e018d759a2a 611 #endif
switches 0:0e018d759a2a 612 #endif
switches 0:0e018d759a2a 613
switches 0:0e018d759a2a 614 #if defined(TOOLCHAIN_GCC_ARM) || defined(TOOLCHAIN_GCC_CR)
switches 0:0e018d759a2a 615 extern "C" void _exit(int return_code) {
switches 0:0e018d759a2a 616 #else
switches 0:0e018d759a2a 617 namespace std {
switches 0:0e018d759a2a 618 extern "C" void exit(int return_code) {
switches 0:0e018d759a2a 619 #endif
switches 0:0e018d759a2a 620
switches 0:0e018d759a2a 621 #if DEVICE_STDIO_MESSAGES
switches 0:0e018d759a2a 622 #if MBED_CONF_PLATFORM_STDIO_FLUSH_AT_EXIT
switches 0:0e018d759a2a 623 fflush(stdout);
switches 0:0e018d759a2a 624 fflush(stderr);
switches 0:0e018d759a2a 625 #endif
switches 0:0e018d759a2a 626 #endif
switches 0:0e018d759a2a 627
switches 0:0e018d759a2a 628 #if DEVICE_SEMIHOST
switches 0:0e018d759a2a 629 if (mbed_interface_connected()) {
switches 0:0e018d759a2a 630 semihost_exit();
switches 0:0e018d759a2a 631 }
switches 0:0e018d759a2a 632 #endif
switches 0:0e018d759a2a 633 if (return_code) {
switches 0:0e018d759a2a 634 mbed_die();
switches 0:0e018d759a2a 635 }
switches 0:0e018d759a2a 636
switches 0:0e018d759a2a 637 while (1);
switches 0:0e018d759a2a 638 }
switches 0:0e018d759a2a 639
switches 0:0e018d759a2a 640 #if !defined(TOOLCHAIN_GCC_ARM) && !defined(TOOLCHAIN_GCC_CR)
switches 0:0e018d759a2a 641 } //namespace std
switches 0:0e018d759a2a 642 #endif
switches 0:0e018d759a2a 643
switches 0:0e018d759a2a 644 #if defined(TOOLCHAIN_ARM) || defined(TOOLCHAIN_GCC)
switches 0:0e018d759a2a 645
switches 0:0e018d759a2a 646 // This series of function disable the registration of global destructors
switches 0:0e018d759a2a 647 // in a dynamic table which will be called when the application exit.
switches 0:0e018d759a2a 648 // In mbed, program never exit properly, it dies.
switches 0:0e018d759a2a 649 // More informations about this topic for ARMCC here:
switches 0:0e018d759a2a 650 // http://infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.faqs/6449.html
switches 0:0e018d759a2a 651 extern "C" {
switches 0:0e018d759a2a 652 int __aeabi_atexit(void *object, void (*dtor)(void* /*this*/), void *handle) {
switches 0:0e018d759a2a 653 return 1;
switches 0:0e018d759a2a 654 }
switches 0:0e018d759a2a 655
switches 0:0e018d759a2a 656 int __cxa_atexit(void (*dtor)(void* /*this*/), void *object, void *handle) {
switches 0:0e018d759a2a 657 return 1;
switches 0:0e018d759a2a 658 }
switches 0:0e018d759a2a 659
switches 0:0e018d759a2a 660 void __cxa_finalize(void *handle) {
switches 0:0e018d759a2a 661 }
switches 0:0e018d759a2a 662
switches 0:0e018d759a2a 663 } // end of extern "C"
switches 0:0e018d759a2a 664
switches 0:0e018d759a2a 665 #endif
switches 0:0e018d759a2a 666
switches 0:0e018d759a2a 667
switches 0:0e018d759a2a 668 #if defined(TOOLCHAIN_GCC)
switches 0:0e018d759a2a 669
switches 0:0e018d759a2a 670 /*
switches 0:0e018d759a2a 671 * Depending on how newlib is configured, it is often not enough to define
switches 0:0e018d759a2a 672 * __aeabi_atexit, __cxa_atexit and __cxa_finalize in order to override the
switches 0:0e018d759a2a 673 * behavior regarding the registration of handlers with atexit.
switches 0:0e018d759a2a 674 *
switches 0:0e018d759a2a 675 * To overcome this limitation, exit and atexit are overriden here.
switches 0:0e018d759a2a 676 */
switches 0:0e018d759a2a 677 extern "C"{
switches 0:0e018d759a2a 678
switches 0:0e018d759a2a 679 /**
switches 0:0e018d759a2a 680 * @brief Retarget of exit for GCC.
switches 0:0e018d759a2a 681 * @details Unlike the standard version, this function doesn't call any function
switches 0:0e018d759a2a 682 * registered with atexit before calling _exit.
switches 0:0e018d759a2a 683 */
switches 0:0e018d759a2a 684 void __wrap_exit(int return_code) {
switches 0:0e018d759a2a 685 _exit(return_code);
switches 0:0e018d759a2a 686 }
switches 0:0e018d759a2a 687
switches 0:0e018d759a2a 688 /**
switches 0:0e018d759a2a 689 * @brief Retarget atexit from GCC.
switches 0:0e018d759a2a 690 * @details This function will always fail and never register any handler to be
switches 0:0e018d759a2a 691 * called at exit.
switches 0:0e018d759a2a 692 */
switches 0:0e018d759a2a 693 int __wrap_atexit(void (*func)()) {
switches 0:0e018d759a2a 694 return 1;
switches 0:0e018d759a2a 695 }
switches 0:0e018d759a2a 696
switches 0:0e018d759a2a 697 }
switches 0:0e018d759a2a 698
switches 0:0e018d759a2a 699 #endif
switches 0:0e018d759a2a 700
switches 0:0e018d759a2a 701
switches 0:0e018d759a2a 702
switches 0:0e018d759a2a 703 namespace mbed {
switches 0:0e018d759a2a 704
switches 0:0e018d759a2a 705 void mbed_set_unbuffered_stream(FILE *_file) {
switches 0:0e018d759a2a 706 #if defined (__ICCARM__)
switches 0:0e018d759a2a 707 char buf[2];
switches 0:0e018d759a2a 708 std::setvbuf(_file,buf,_IONBF,NULL);
switches 0:0e018d759a2a 709 #else
switches 0:0e018d759a2a 710 setbuf(_file, NULL);
switches 0:0e018d759a2a 711 #endif
switches 0:0e018d759a2a 712 }
switches 0:0e018d759a2a 713
switches 0:0e018d759a2a 714 int mbed_getc(FILE *_file){
switches 0:0e018d759a2a 715 #if defined (__ICCARM__)
switches 0:0e018d759a2a 716 /*This is only valid for unbuffered streams*/
switches 0:0e018d759a2a 717 int res = std::fgetc(_file);
switches 0:0e018d759a2a 718 if (res>=0){
switches 0:0e018d759a2a 719 _file->_Mode = (unsigned short)(_file->_Mode & ~ 0x1000);/* Unset read mode */
switches 0:0e018d759a2a 720 _file->_Rend = _file->_Wend;
switches 0:0e018d759a2a 721 _file->_Next = _file->_Wend;
switches 0:0e018d759a2a 722 }
switches 0:0e018d759a2a 723 return res;
switches 0:0e018d759a2a 724 #else
switches 0:0e018d759a2a 725 return std::fgetc(_file);
switches 0:0e018d759a2a 726 #endif
switches 0:0e018d759a2a 727 }
switches 0:0e018d759a2a 728
switches 0:0e018d759a2a 729 char* mbed_gets(char*s, int size, FILE *_file){
switches 0:0e018d759a2a 730 #if defined (__ICCARM__)
switches 0:0e018d759a2a 731 /*This is only valid for unbuffered streams*/
switches 0:0e018d759a2a 732 char *str = fgets(s,size,_file);
switches 0:0e018d759a2a 733 if (str!=NULL){
switches 0:0e018d759a2a 734 _file->_Mode = (unsigned short)(_file->_Mode & ~ 0x1000);/* Unset read mode */
switches 0:0e018d759a2a 735 _file->_Rend = _file->_Wend;
switches 0:0e018d759a2a 736 _file->_Next = _file->_Wend;
switches 0:0e018d759a2a 737 }
switches 0:0e018d759a2a 738 return str;
switches 0:0e018d759a2a 739 #else
switches 0:0e018d759a2a 740 return std::fgets(s,size,_file);
switches 0:0e018d759a2a 741 #endif
switches 0:0e018d759a2a 742 }
switches 0:0e018d759a2a 743
switches 0:0e018d759a2a 744 } // namespace mbed
switches 0:0e018d759a2a 745
switches 0:0e018d759a2a 746 #if defined (__ICCARM__)
switches 0:0e018d759a2a 747 // Stub out locks when an rtos is not present
switches 0:0e018d759a2a 748 extern "C" WEAK void __iar_system_Mtxinit(__iar_Rmtx *mutex) {}
switches 0:0e018d759a2a 749 extern "C" WEAK void __iar_system_Mtxdst(__iar_Rmtx *mutex) {}
switches 0:0e018d759a2a 750 extern "C" WEAK void __iar_system_Mtxlock(__iar_Rmtx *mutex) {}
switches 0:0e018d759a2a 751 extern "C" WEAK void __iar_system_Mtxunlock(__iar_Rmtx *mutex) {}
switches 0:0e018d759a2a 752 extern "C" WEAK void __iar_file_Mtxinit(__iar_Rmtx *mutex) {}
switches 0:0e018d759a2a 753 extern "C" WEAK void __iar_file_Mtxdst(__iar_Rmtx *mutex) {}
switches 0:0e018d759a2a 754 extern "C" WEAK void __iar_file_Mtxlock(__iar_Rmtx *mutex) {}
switches 0:0e018d759a2a 755 extern "C" WEAK void __iar_file_Mtxunlock(__iar_Rmtx *mutex) {}
switches 0:0e018d759a2a 756 #elif defined(__CC_ARM)
switches 0:0e018d759a2a 757 // Do nothing
switches 0:0e018d759a2a 758 #elif defined (__GNUC__)
switches 0:0e018d759a2a 759 struct _reent;
switches 0:0e018d759a2a 760 // Stub out locks when an rtos is not present
switches 0:0e018d759a2a 761 extern "C" WEAK void __rtos_malloc_lock( struct _reent *_r ) {}
switches 0:0e018d759a2a 762 extern "C" WEAK void __rtos_malloc_unlock( struct _reent *_r ) {}
switches 0:0e018d759a2a 763 extern "C" WEAK void __rtos_env_lock( struct _reent *_r ) {}
switches 0:0e018d759a2a 764 extern "C" WEAK void __rtos_env_unlock( struct _reent *_r ) {}
switches 0:0e018d759a2a 765
switches 0:0e018d759a2a 766 extern "C" void __malloc_lock( struct _reent *_r )
switches 0:0e018d759a2a 767 {
switches 0:0e018d759a2a 768 __rtos_malloc_lock(_r);
switches 0:0e018d759a2a 769 }
switches 0:0e018d759a2a 770
switches 0:0e018d759a2a 771 extern "C" void __malloc_unlock( struct _reent *_r )
switches 0:0e018d759a2a 772 {
switches 0:0e018d759a2a 773 __rtos_malloc_unlock(_r);
switches 0:0e018d759a2a 774 }
switches 0:0e018d759a2a 775
switches 0:0e018d759a2a 776 extern "C" void __env_lock( struct _reent *_r )
switches 0:0e018d759a2a 777 {
switches 0:0e018d759a2a 778 __rtos_env_lock(_r);
switches 0:0e018d759a2a 779 }
switches 0:0e018d759a2a 780
switches 0:0e018d759a2a 781 extern "C" void __env_unlock( struct _reent *_r )
switches 0:0e018d759a2a 782 {
switches 0:0e018d759a2a 783 __rtos_env_unlock(_r);
switches 0:0e018d759a2a 784 }
switches 0:0e018d759a2a 785
switches 0:0e018d759a2a 786 #define CXA_GUARD_INIT_DONE (1 << 0)
switches 0:0e018d759a2a 787 #define CXA_GUARD_INIT_IN_PROGRESS (1 << 1)
switches 0:0e018d759a2a 788 #define CXA_GUARD_MASK (CXA_GUARD_INIT_DONE | CXA_GUARD_INIT_IN_PROGRESS)
switches 0:0e018d759a2a 789
switches 0:0e018d759a2a 790 extern "C" int __cxa_guard_acquire(int *guard_object_p)
switches 0:0e018d759a2a 791 {
switches 0:0e018d759a2a 792 uint8_t *guard_object = (uint8_t *)guard_object_p;
switches 0:0e018d759a2a 793 if (CXA_GUARD_INIT_DONE == (*guard_object & CXA_GUARD_MASK)) {
switches 0:0e018d759a2a 794 return 0;
switches 0:0e018d759a2a 795 }
switches 0:0e018d759a2a 796 singleton_lock();
switches 0:0e018d759a2a 797 if (CXA_GUARD_INIT_DONE == (*guard_object & CXA_GUARD_MASK)) {
switches 0:0e018d759a2a 798 singleton_unlock();
switches 0:0e018d759a2a 799 return 0;
switches 0:0e018d759a2a 800 }
switches 0:0e018d759a2a 801 MBED_ASSERT(0 == (*guard_object & CXA_GUARD_MASK));
switches 0:0e018d759a2a 802 *guard_object = *guard_object | CXA_GUARD_INIT_IN_PROGRESS;
switches 0:0e018d759a2a 803 return 1;
switches 0:0e018d759a2a 804 }
switches 0:0e018d759a2a 805
switches 0:0e018d759a2a 806 extern "C" void __cxa_guard_release(int *guard_object_p)
switches 0:0e018d759a2a 807 {
switches 0:0e018d759a2a 808 uint8_t *guard_object = (uint8_t *)guard_object_p;
switches 0:0e018d759a2a 809 MBED_ASSERT(CXA_GUARD_INIT_IN_PROGRESS == (*guard_object & CXA_GUARD_MASK));
switches 0:0e018d759a2a 810 *guard_object = (*guard_object & ~CXA_GUARD_MASK) | CXA_GUARD_INIT_DONE;
switches 0:0e018d759a2a 811 singleton_unlock();
switches 0:0e018d759a2a 812 }
switches 0:0e018d759a2a 813
switches 0:0e018d759a2a 814 extern "C" void __cxa_guard_abort(int *guard_object_p)
switches 0:0e018d759a2a 815 {
switches 0:0e018d759a2a 816 uint8_t *guard_object = (uint8_t *)guard_object_p;
switches 0:0e018d759a2a 817 MBED_ASSERT(CXA_GUARD_INIT_IN_PROGRESS == (*guard_object & CXA_GUARD_MASK));
switches 0:0e018d759a2a 818 *guard_object = *guard_object & ~CXA_GUARD_INIT_IN_PROGRESS;
switches 0:0e018d759a2a 819 singleton_unlock();
switches 0:0e018d759a2a 820 }
switches 0:0e018d759a2a 821
switches 0:0e018d759a2a 822 #endif
switches 0:0e018d759a2a 823
switches 0:0e018d759a2a 824 void *operator new(std::size_t count)
switches 0:0e018d759a2a 825 {
switches 0:0e018d759a2a 826 void *buffer = malloc(count);
switches 0:0e018d759a2a 827 if (NULL == buffer) {
switches 0:0e018d759a2a 828 error("Operator new out of memory\r\n");
switches 0:0e018d759a2a 829 }
switches 0:0e018d759a2a 830 return buffer;
switches 0:0e018d759a2a 831 }
switches 0:0e018d759a2a 832
switches 0:0e018d759a2a 833 void *operator new[](std::size_t count)
switches 0:0e018d759a2a 834 {
switches 0:0e018d759a2a 835 void *buffer = malloc(count);
switches 0:0e018d759a2a 836 if (NULL == buffer) {
switches 0:0e018d759a2a 837 error("Operator new[] out of memory\r\n");
switches 0:0e018d759a2a 838 }
switches 0:0e018d759a2a 839 return buffer;
switches 0:0e018d759a2a 840 }
switches 0:0e018d759a2a 841
switches 0:0e018d759a2a 842 void operator delete(void *ptr)
switches 0:0e018d759a2a 843 {
switches 0:0e018d759a2a 844 if (ptr != NULL) {
switches 0:0e018d759a2a 845 free(ptr);
switches 0:0e018d759a2a 846 }
switches 0:0e018d759a2a 847 }
switches 0:0e018d759a2a 848 void operator delete[](void *ptr)
switches 0:0e018d759a2a 849 {
switches 0:0e018d759a2a 850 if (ptr != NULL) {
switches 0:0e018d759a2a 851 free(ptr);
switches 0:0e018d759a2a 852 }
switches 0:0e018d759a2a 853 }