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