mbed library sources. Supersedes mbed-src.

Fork of mbed-dev by mbed official

Committer:
AnnaBridge
Date:
Mon Oct 02 15:33:19 2017 +0100
Revision:
174:b96e65c34a4d
Parent:
172:7d866c31b3c5
Child:
175:af195413fb11
This updates the lib to the mbed lib v 152

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