Includes library modifications to allow access to AIN_4 (AIN_0 / 5)

Committer:
bryantaylor
Date:
Tue Sep 20 21:26:12 2016 +0000
Revision:
0:eafc3fd41f75
hackathon

Who changed what in which revision?

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