mbed library sources. Supersedes mbed-src.

Dependents:   BREAK_SENSOR_LED

Fork of mbed-dev by mbed official

Committer:
<>
Date:
Tue Mar 14 16:40:56 2017 +0000
Revision:
160:d5399cc887bb
Child:
163:74e0ce7f98e8
This updates the lib to the mbed lib v138

Who changed what in which revision?

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