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