Development mbed library for MAX32630FTHR

Dependents:   blinky_max32630fthr

Committer:
switches
Date:
Fri Dec 16 16:27:57 2016 +0000
Revision:
3:1198227e6421
Parent:
0:5c4d7b2438d3
Changed ADC scale for MAX32625 platforms to 1.2V full scale to match MAX32630 platforms

Who changed what in which revision?

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