dhgdh

Dependencies:   MAX44000 PWM_Tone_Library nexpaq_mdk

Fork of LED_Demo by joey shelton

Committer:
cyberjoey
Date:
Sat Oct 22 01:31:58 2016 +0000
Revision:
9:6bb35cef007d
Parent:
1:55a6170b404f
WORKING

Who changed what in which revision?

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