Preliminary main mbed library for nexpaq development

Committer:
nexpaq
Date:
Fri Nov 04 20:27:58 2016 +0000
Revision:
0:6c56fb4bc5f0
Moving to library for sharing updates

Who changed what in which revision?

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