mbed-src updated for BMD-200 evaluation board. Just pin numbers are updated.

Dependents:   mbed_blinky-bmd-200 bmd-200_accel_demo firstRig

Fork of mbed-src by mbed official

Replacement for the "mbed" or "mbed-src" library when using the BMD-200 Evaluation kit. This library only remaps the pin names (i.e. LED1 points to p0.01 instead of p0.18, etc) as used by the BMD-200 Evaluation board (select the nRF51822_mkit platform). All other code is untouched.

Committer:
mbed_official
Date:
Fri Feb 27 10:00:08 2015 +0000
Revision:
485:d9a48e768ce0
Parent:
479:4dac56e9ae44
Synchronized with git revision 43d7f387ec8e6fef8c03cb5e3a74f7b1596c8f8c

Full URL: https://github.com/mbedmicro/mbed/commit/43d7f387ec8e6fef8c03cb5e3a74f7b1596c8f8c/

RZ/A1H - Modify to support GCC and Fix some bugs of driver.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
bogdanm 13:0645d8841f51 1 /* mbed Microcontroller Library
mbed_official 476:44b5a0bc6da6 2 * Copyright (c) 2006-2015 ARM Limited
bogdanm 13:0645d8841f51 3 *
bogdanm 13:0645d8841f51 4 * Licensed under the Apache License, Version 2.0 (the "License");
bogdanm 13:0645d8841f51 5 * you may not use this file except in compliance with the License.
bogdanm 13:0645d8841f51 6 * You may obtain a copy of the License at
bogdanm 13:0645d8841f51 7 *
bogdanm 13:0645d8841f51 8 * http://www.apache.org/licenses/LICENSE-2.0
bogdanm 13:0645d8841f51 9 *
bogdanm 13:0645d8841f51 10 * Unless required by applicable law or agreed to in writing, software
bogdanm 13:0645d8841f51 11 * distributed under the License is distributed on an "AS IS" BASIS,
bogdanm 13:0645d8841f51 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
bogdanm 13:0645d8841f51 13 * See the License for the specific language governing permissions and
bogdanm 13:0645d8841f51 14 * limitations under the License.
bogdanm 13:0645d8841f51 15 */
bogdanm 13:0645d8841f51 16 #include "platform.h"
bogdanm 13:0645d8841f51 17 #include "FileHandle.h"
bogdanm 13:0645d8841f51 18 #include "FileSystemLike.h"
bogdanm 13:0645d8841f51 19 #include "FilePath.h"
bogdanm 13:0645d8841f51 20 #include "serial_api.h"
bogdanm 13:0645d8841f51 21 #include "toolchain.h"
mbed_official 476:44b5a0bc6da6 22 #include "semihost_api.h"
mbed_official 476:44b5a0bc6da6 23 #include "mbed_interface.h"
mbed_official 476:44b5a0bc6da6 24 #if DEVICE_STDIO_MESSAGES
mbed_official 476:44b5a0bc6da6 25 #include <stdio.h>
mbed_official 476:44b5a0bc6da6 26 #endif
bogdanm 13:0645d8841f51 27 #include <errno.h>
bogdanm 13:0645d8841f51 28
bogdanm 13:0645d8841f51 29 #if defined(__ARMCC_VERSION)
bogdanm 13:0645d8841f51 30 # include <rt_sys.h>
bogdanm 13:0645d8841f51 31 # define PREFIX(x) _sys##x
bogdanm 13:0645d8841f51 32 # define OPEN_MAX _SYS_OPEN
bogdanm 13:0645d8841f51 33 # ifdef __MICROLIB
bogdanm 13:0645d8841f51 34 # pragma import(__use_full_stdio)
bogdanm 13:0645d8841f51 35 # endif
bogdanm 13:0645d8841f51 36
bogdanm 13:0645d8841f51 37 #elif defined(__ICCARM__)
bogdanm 13:0645d8841f51 38 # include <yfuns.h>
bogdanm 13:0645d8841f51 39 # define PREFIX(x) _##x
bogdanm 13:0645d8841f51 40 # define OPEN_MAX 16
bogdanm 13:0645d8841f51 41
bogdanm 13:0645d8841f51 42 # define STDIN_FILENO 0
bogdanm 13:0645d8841f51 43 # define STDOUT_FILENO 1
bogdanm 13:0645d8841f51 44 # define STDERR_FILENO 2
bogdanm 13:0645d8841f51 45
bogdanm 13:0645d8841f51 46 #else
bogdanm 13:0645d8841f51 47 # include <sys/stat.h>
bogdanm 13:0645d8841f51 48 # include <sys/unistd.h>
bogdanm 13:0645d8841f51 49 # include <sys/syslimits.h>
bogdanm 13:0645d8841f51 50 # define PREFIX(x) x
bogdanm 13:0645d8841f51 51 #endif
bogdanm 13:0645d8841f51 52
bogdanm 13:0645d8841f51 53 using namespace mbed;
bogdanm 13:0645d8841f51 54
bogdanm 13:0645d8841f51 55 #if defined(__MICROLIB) && (__ARMCC_VERSION>5030000)
bogdanm 13:0645d8841f51 56 // Before version 5.03, we were using a patched version of microlib with proper names
bogdanm 13:0645d8841f51 57 extern const char __stdin_name[] = ":tt";
bogdanm 13:0645d8841f51 58 extern const char __stdout_name[] = ":tt";
bogdanm 13:0645d8841f51 59 extern const char __stderr_name[] = ":tt";
bogdanm 13:0645d8841f51 60
bogdanm 13:0645d8841f51 61 #else
bogdanm 13:0645d8841f51 62 extern const char __stdin_name[] = "/stdin";
bogdanm 13:0645d8841f51 63 extern const char __stdout_name[] = "/stdout";
bogdanm 13:0645d8841f51 64 extern const char __stderr_name[] = "/stderr";
bogdanm 13:0645d8841f51 65 #endif
bogdanm 13:0645d8841f51 66
bogdanm 13:0645d8841f51 67 /* newlib has the filehandle field in the FILE struct as a short, so
bogdanm 13:0645d8841f51 68 * we can't just return a Filehandle* from _open and instead have to
bogdanm 13:0645d8841f51 69 * put it in a filehandles array and return the index into that array
bogdanm 13:0645d8841f51 70 * (or rather index+3, as filehandles 0-2 are stdin/out/err).
bogdanm 13:0645d8841f51 71 */
bogdanm 13:0645d8841f51 72 static FileHandle *filehandles[OPEN_MAX];
bogdanm 13:0645d8841f51 73
bogdanm 13:0645d8841f51 74 FileHandle::~FileHandle() {
bogdanm 13:0645d8841f51 75 /* Remove all open filehandles for this */
bogdanm 13:0645d8841f51 76 for (unsigned int fh_i = 0; fh_i < sizeof(filehandles)/sizeof(*filehandles); fh_i++) {
bogdanm 13:0645d8841f51 77 if (filehandles[fh_i] == this) {
bogdanm 13:0645d8841f51 78 filehandles[fh_i] = NULL;
bogdanm 13:0645d8841f51 79 }
bogdanm 13:0645d8841f51 80 }
bogdanm 13:0645d8841f51 81 }
bogdanm 13:0645d8841f51 82
bogdanm 13:0645d8841f51 83 #if DEVICE_SERIAL
bogdanm 13:0645d8841f51 84 extern int stdio_uart_inited;
bogdanm 13:0645d8841f51 85 extern serial_t stdio_uart;
bogdanm 13:0645d8841f51 86 #endif
bogdanm 13:0645d8841f51 87
bogdanm 13:0645d8841f51 88 static void init_serial() {
bogdanm 13:0645d8841f51 89 #if DEVICE_SERIAL
bogdanm 13:0645d8841f51 90 if (stdio_uart_inited) return;
bogdanm 13:0645d8841f51 91 serial_init(&stdio_uart, STDIO_UART_TX, STDIO_UART_RX);
bogdanm 13:0645d8841f51 92 #endif
bogdanm 13:0645d8841f51 93 }
bogdanm 13:0645d8841f51 94
bogdanm 13:0645d8841f51 95 static inline int openmode_to_posix(int openmode) {
bogdanm 13:0645d8841f51 96 int posix = openmode;
bogdanm 13:0645d8841f51 97 #ifdef __ARMCC_VERSION
bogdanm 13:0645d8841f51 98 if (openmode & OPEN_PLUS) {
bogdanm 13:0645d8841f51 99 posix = O_RDWR;
bogdanm 13:0645d8841f51 100 } else if(openmode & OPEN_W) {
bogdanm 13:0645d8841f51 101 posix = O_WRONLY;
bogdanm 13:0645d8841f51 102 } else if(openmode & OPEN_A) {
bogdanm 13:0645d8841f51 103 posix = O_WRONLY|O_APPEND;
bogdanm 13:0645d8841f51 104 } else {
bogdanm 13:0645d8841f51 105 posix = O_RDONLY;
bogdanm 13:0645d8841f51 106 }
bogdanm 13:0645d8841f51 107 /* a, w, a+, w+ all create if file does not already exist */
bogdanm 13:0645d8841f51 108 if (openmode & (OPEN_A|OPEN_W)) {
bogdanm 13:0645d8841f51 109 posix |= O_CREAT;
bogdanm 13:0645d8841f51 110 }
bogdanm 13:0645d8841f51 111 /* w and w+ truncate */
bogdanm 13:0645d8841f51 112 if (openmode & OPEN_W) {
bogdanm 13:0645d8841f51 113 posix |= O_TRUNC;
bogdanm 13:0645d8841f51 114 }
bogdanm 13:0645d8841f51 115 #elif defined(__ICCARM__)
bogdanm 13:0645d8841f51 116 switch (openmode & _LLIO_RDWRMASK) {
bogdanm 13:0645d8841f51 117 case _LLIO_RDONLY: posix = O_RDONLY; break;
bogdanm 13:0645d8841f51 118 case _LLIO_WRONLY: posix = O_WRONLY; break;
bogdanm 13:0645d8841f51 119 case _LLIO_RDWR : posix = O_RDWR ; break;
bogdanm 13:0645d8841f51 120 }
bogdanm 13:0645d8841f51 121 if (openmode & _LLIO_CREAT ) posix |= O_CREAT;
bogdanm 13:0645d8841f51 122 if (openmode & _LLIO_APPEND) posix |= O_APPEND;
bogdanm 13:0645d8841f51 123 if (openmode & _LLIO_TRUNC ) posix |= O_TRUNC;
bogdanm 13:0645d8841f51 124 #endif
bogdanm 13:0645d8841f51 125 return posix;
bogdanm 13:0645d8841f51 126 }
bogdanm 13:0645d8841f51 127
bogdanm 13:0645d8841f51 128 extern "C" FILEHANDLE PREFIX(_open)(const char* name, int openmode) {
bogdanm 13:0645d8841f51 129 #if defined(__MICROLIB) && (__ARMCC_VERSION>5030000)
bogdanm 13:0645d8841f51 130 // Before version 5.03, we were using a patched version of microlib with proper names
bogdanm 13:0645d8841f51 131 // This is the workaround that the microlib author suggested us
bogdanm 13:0645d8841f51 132 static int n = 0;
bogdanm 13:0645d8841f51 133 if (!std::strcmp(name, ":tt")) return n++;
mbed_official 221:8276e3a4886f 134
bogdanm 13:0645d8841f51 135 #else
bogdanm 13:0645d8841f51 136 /* Use the posix convention that stdin,out,err are filehandles 0,1,2.
bogdanm 13:0645d8841f51 137 */
bogdanm 13:0645d8841f51 138 if (std::strcmp(name, __stdin_name) == 0) {
bogdanm 13:0645d8841f51 139 init_serial();
bogdanm 13:0645d8841f51 140 return 0;
bogdanm 13:0645d8841f51 141 } else if (std::strcmp(name, __stdout_name) == 0) {
bogdanm 13:0645d8841f51 142 init_serial();
bogdanm 13:0645d8841f51 143 return 1;
bogdanm 13:0645d8841f51 144 } else if (std::strcmp(name, __stderr_name) == 0) {
bogdanm 13:0645d8841f51 145 init_serial();
bogdanm 13:0645d8841f51 146 return 2;
bogdanm 13:0645d8841f51 147 }
bogdanm 13:0645d8841f51 148 #endif
mbed_official 221:8276e3a4886f 149
bogdanm 13:0645d8841f51 150 // find the first empty slot in filehandles
bogdanm 13:0645d8841f51 151 unsigned int fh_i;
bogdanm 13:0645d8841f51 152 for (fh_i = 0; fh_i < sizeof(filehandles)/sizeof(*filehandles); fh_i++) {
bogdanm 13:0645d8841f51 153 if (filehandles[fh_i] == NULL) break;
bogdanm 13:0645d8841f51 154 }
bogdanm 13:0645d8841f51 155 if (fh_i >= sizeof(filehandles)/sizeof(*filehandles)) {
bogdanm 13:0645d8841f51 156 return -1;
bogdanm 13:0645d8841f51 157 }
bogdanm 13:0645d8841f51 158
bogdanm 13:0645d8841f51 159 FileHandle *res;
bogdanm 13:0645d8841f51 160
bogdanm 13:0645d8841f51 161 /* FILENAME: ":0x12345678" describes a FileLike* */
bogdanm 13:0645d8841f51 162 if (name[0] == ':') {
bogdanm 13:0645d8841f51 163 void *p;
bogdanm 13:0645d8841f51 164 sscanf(name, ":%p", &p);
bogdanm 13:0645d8841f51 165 res = (FileHandle*)p;
bogdanm 13:0645d8841f51 166
bogdanm 13:0645d8841f51 167 /* FILENAME: "/file_system/file_name" */
bogdanm 13:0645d8841f51 168 } else {
bogdanm 13:0645d8841f51 169 FilePath path(name);
bogdanm 13:0645d8841f51 170
bogdanm 20:4263a77256ae 171 if (!path.exists())
bogdanm 20:4263a77256ae 172 return -1;
bogdanm 20:4263a77256ae 173 else if (path.isFile()) {
bogdanm 13:0645d8841f51 174 res = path.file();
bogdanm 13:0645d8841f51 175 } else {
bogdanm 13:0645d8841f51 176 FileSystemLike *fs = path.fileSystem();
bogdanm 13:0645d8841f51 177 if (fs == NULL) return -1;
bogdanm 13:0645d8841f51 178 int posix_mode = openmode_to_posix(openmode);
bogdanm 13:0645d8841f51 179 res = fs->open(path.fileName(), posix_mode); /* NULL if fails */
bogdanm 13:0645d8841f51 180 }
bogdanm 13:0645d8841f51 181 }
bogdanm 13:0645d8841f51 182
bogdanm 13:0645d8841f51 183 if (res == NULL) return -1;
bogdanm 13:0645d8841f51 184 filehandles[fh_i] = res;
bogdanm 13:0645d8841f51 185
bogdanm 13:0645d8841f51 186 return fh_i + 3; // +3 as filehandles 0-2 are stdin/out/err
bogdanm 13:0645d8841f51 187 }
bogdanm 13:0645d8841f51 188
bogdanm 13:0645d8841f51 189 extern "C" int PREFIX(_close)(FILEHANDLE fh) {
bogdanm 13:0645d8841f51 190 if (fh < 3) return 0;
bogdanm 13:0645d8841f51 191
bogdanm 13:0645d8841f51 192 FileHandle* fhc = filehandles[fh-3];
bogdanm 13:0645d8841f51 193 filehandles[fh-3] = NULL;
bogdanm 13:0645d8841f51 194 if (fhc == NULL) return -1;
bogdanm 13:0645d8841f51 195
bogdanm 13:0645d8841f51 196 return fhc->close();
bogdanm 13:0645d8841f51 197 }
bogdanm 13:0645d8841f51 198
bogdanm 13:0645d8841f51 199 #if defined(__ICCARM__)
bogdanm 13:0645d8841f51 200 extern "C" size_t __write (int fh, const unsigned char *buffer, size_t length) {
bogdanm 13:0645d8841f51 201 #else
bogdanm 13:0645d8841f51 202 extern "C" int PREFIX(_write)(FILEHANDLE fh, const unsigned char *buffer, unsigned int length, int mode) {
bogdanm 13:0645d8841f51 203 #endif
bogdanm 13:0645d8841f51 204 int n; // n is the number of bytes written
bogdanm 13:0645d8841f51 205 if (fh < 3) {
bogdanm 13:0645d8841f51 206 #if DEVICE_SERIAL
bogdanm 13:0645d8841f51 207 if (!stdio_uart_inited) init_serial();
bogdanm 13:0645d8841f51 208 for (unsigned int i = 0; i < length; i++) {
bogdanm 13:0645d8841f51 209 serial_putc(&stdio_uart, buffer[i]);
bogdanm 13:0645d8841f51 210 }
bogdanm 13:0645d8841f51 211 #endif
bogdanm 13:0645d8841f51 212 n = length;
bogdanm 13:0645d8841f51 213 } else {
bogdanm 13:0645d8841f51 214 FileHandle* fhc = filehandles[fh-3];
bogdanm 13:0645d8841f51 215 if (fhc == NULL) return -1;
bogdanm 13:0645d8841f51 216
bogdanm 13:0645d8841f51 217 n = fhc->write(buffer, length);
bogdanm 13:0645d8841f51 218 }
bogdanm 13:0645d8841f51 219 #ifdef __ARMCC_VERSION
bogdanm 13:0645d8841f51 220 return length-n;
bogdanm 13:0645d8841f51 221 #else
bogdanm 13:0645d8841f51 222 return n;
bogdanm 13:0645d8841f51 223 #endif
bogdanm 13:0645d8841f51 224 }
bogdanm 13:0645d8841f51 225
bogdanm 13:0645d8841f51 226 #if defined(__ICCARM__)
bogdanm 13:0645d8841f51 227 extern "C" size_t __read (int fh, unsigned char *buffer, size_t length) {
bogdanm 13:0645d8841f51 228 #else
bogdanm 13:0645d8841f51 229 extern "C" int PREFIX(_read)(FILEHANDLE fh, unsigned char *buffer, unsigned int length, int mode) {
bogdanm 13:0645d8841f51 230 #endif
bogdanm 13:0645d8841f51 231 int n; // n is the number of bytes read
bogdanm 13:0645d8841f51 232 if (fh < 3) {
bogdanm 13:0645d8841f51 233 // only read a character at a time from stdin
bogdanm 13:0645d8841f51 234 #if DEVICE_SERIAL
mbed_official 269:0e58554f11d5 235 if (!stdio_uart_inited) init_serial();
bogdanm 13:0645d8841f51 236 *buffer = serial_getc(&stdio_uart);
bogdanm 13:0645d8841f51 237 #endif
bogdanm 13:0645d8841f51 238 n = 1;
bogdanm 13:0645d8841f51 239 } else {
bogdanm 13:0645d8841f51 240 FileHandle* fhc = filehandles[fh-3];
bogdanm 13:0645d8841f51 241 if (fhc == NULL) return -1;
bogdanm 13:0645d8841f51 242
bogdanm 13:0645d8841f51 243 n = fhc->read(buffer, length);
bogdanm 13:0645d8841f51 244 }
bogdanm 13:0645d8841f51 245 #ifdef __ARMCC_VERSION
bogdanm 13:0645d8841f51 246 return length-n;
bogdanm 13:0645d8841f51 247 #else
bogdanm 13:0645d8841f51 248 return n;
bogdanm 13:0645d8841f51 249 #endif
bogdanm 13:0645d8841f51 250 }
bogdanm 13:0645d8841f51 251
bogdanm 13:0645d8841f51 252 #ifdef __ARMCC_VERSION
bogdanm 13:0645d8841f51 253 extern "C" int PREFIX(_istty)(FILEHANDLE fh)
bogdanm 13:0645d8841f51 254 #else
bogdanm 13:0645d8841f51 255 extern "C" int _isatty(FILEHANDLE fh)
bogdanm 13:0645d8841f51 256 #endif
bogdanm 13:0645d8841f51 257 {
bogdanm 13:0645d8841f51 258 /* stdin, stdout and stderr should be tty */
bogdanm 13:0645d8841f51 259 if (fh < 3) return 1;
bogdanm 13:0645d8841f51 260
bogdanm 13:0645d8841f51 261 FileHandle* fhc = filehandles[fh-3];
bogdanm 13:0645d8841f51 262 if (fhc == NULL) return -1;
bogdanm 13:0645d8841f51 263
bogdanm 13:0645d8841f51 264 return fhc->isatty();
bogdanm 13:0645d8841f51 265 }
bogdanm 13:0645d8841f51 266
bogdanm 13:0645d8841f51 267 extern "C"
bogdanm 13:0645d8841f51 268 #if defined(__ARMCC_VERSION)
bogdanm 13:0645d8841f51 269 int _sys_seek(FILEHANDLE fh, long position)
bogdanm 13:0645d8841f51 270 #elif defined(__ICCARM__)
bogdanm 13:0645d8841f51 271 long __lseek(int fh, long offset, int whence)
bogdanm 13:0645d8841f51 272 #else
bogdanm 13:0645d8841f51 273 int _lseek(FILEHANDLE fh, int offset, int whence)
bogdanm 13:0645d8841f51 274 #endif
bogdanm 13:0645d8841f51 275 {
bogdanm 13:0645d8841f51 276 if (fh < 3) return 0;
bogdanm 13:0645d8841f51 277
bogdanm 13:0645d8841f51 278 FileHandle* fhc = filehandles[fh-3];
bogdanm 13:0645d8841f51 279 if (fhc == NULL) return -1;
bogdanm 13:0645d8841f51 280
bogdanm 13:0645d8841f51 281 #if defined(__ARMCC_VERSION)
bogdanm 13:0645d8841f51 282 return fhc->lseek(position, SEEK_SET);
bogdanm 13:0645d8841f51 283 #else
bogdanm 13:0645d8841f51 284 return fhc->lseek(offset, whence);
bogdanm 13:0645d8841f51 285 #endif
bogdanm 13:0645d8841f51 286 }
bogdanm 13:0645d8841f51 287
bogdanm 13:0645d8841f51 288 #ifdef __ARMCC_VERSION
bogdanm 13:0645d8841f51 289 extern "C" int PREFIX(_ensure)(FILEHANDLE fh) {
bogdanm 13:0645d8841f51 290 if (fh < 3) return 0;
bogdanm 13:0645d8841f51 291
bogdanm 13:0645d8841f51 292 FileHandle* fhc = filehandles[fh-3];
bogdanm 13:0645d8841f51 293 if (fhc == NULL) return -1;
bogdanm 13:0645d8841f51 294
bogdanm 13:0645d8841f51 295 return fhc->fsync();
bogdanm 13:0645d8841f51 296 }
bogdanm 13:0645d8841f51 297
bogdanm 13:0645d8841f51 298 extern "C" long PREFIX(_flen)(FILEHANDLE fh) {
bogdanm 13:0645d8841f51 299 if (fh < 3) return 0;
bogdanm 13:0645d8841f51 300
bogdanm 13:0645d8841f51 301 FileHandle* fhc = filehandles[fh-3];
bogdanm 13:0645d8841f51 302 if (fhc == NULL) return -1;
bogdanm 13:0645d8841f51 303
bogdanm 13:0645d8841f51 304 return fhc->flen();
bogdanm 13:0645d8841f51 305 }
bogdanm 13:0645d8841f51 306 #endif
bogdanm 13:0645d8841f51 307
bogdanm 13:0645d8841f51 308
bogdanm 13:0645d8841f51 309 #if !defined(__ARMCC_VERSION) && !defined(__ICCARM__)
bogdanm 13:0645d8841f51 310 extern "C" int _fstat(int fd, struct stat *st) {
bogdanm 13:0645d8841f51 311 if ((STDOUT_FILENO == fd) || (STDERR_FILENO == fd) || (STDIN_FILENO == fd)) {
bogdanm 13:0645d8841f51 312 st->st_mode = S_IFCHR;
bogdanm 13:0645d8841f51 313 return 0;
bogdanm 13:0645d8841f51 314 }
bogdanm 13:0645d8841f51 315
bogdanm 13:0645d8841f51 316 errno = EBADF;
bogdanm 13:0645d8841f51 317 return -1;
bogdanm 13:0645d8841f51 318 }
bogdanm 13:0645d8841f51 319 #endif
bogdanm 13:0645d8841f51 320
bogdanm 13:0645d8841f51 321 namespace std {
bogdanm 13:0645d8841f51 322 extern "C" int remove(const char *path) {
bogdanm 13:0645d8841f51 323 FilePath fp(path);
bogdanm 13:0645d8841f51 324 FileSystemLike *fs = fp.fileSystem();
bogdanm 13:0645d8841f51 325 if (fs == NULL) return -1;
bogdanm 13:0645d8841f51 326
bogdanm 13:0645d8841f51 327 return fs->remove(fp.fileName());
bogdanm 13:0645d8841f51 328 }
bogdanm 13:0645d8841f51 329
bogdanm 13:0645d8841f51 330 extern "C" int rename(const char *oldname, const char *newname) {
mbed_official 284:859ffaa7c331 331 FilePath fpOld(oldname);
mbed_official 284:859ffaa7c331 332 FilePath fpNew(newname);
mbed_official 284:859ffaa7c331 333 FileSystemLike *fsOld = fpOld.fileSystem();
mbed_official 284:859ffaa7c331 334 FileSystemLike *fsNew = fpNew.fileSystem();
mbed_official 284:859ffaa7c331 335
mbed_official 284:859ffaa7c331 336 /* rename only if both files are on the same FS */
mbed_official 284:859ffaa7c331 337 if (fsOld != fsNew || fsOld == NULL) return -1;
mbed_official 284:859ffaa7c331 338
mbed_official 284:859ffaa7c331 339 return fsOld->rename(fpOld.fileName(), fpNew.fileName());
bogdanm 13:0645d8841f51 340 }
bogdanm 13:0645d8841f51 341
bogdanm 13:0645d8841f51 342 extern "C" char *tmpnam(char *s) {
bogdanm 13:0645d8841f51 343 return NULL;
bogdanm 13:0645d8841f51 344 }
bogdanm 13:0645d8841f51 345
bogdanm 13:0645d8841f51 346 extern "C" FILE *tmpfile() {
bogdanm 13:0645d8841f51 347 return NULL;
bogdanm 13:0645d8841f51 348 }
bogdanm 13:0645d8841f51 349 } // namespace std
bogdanm 13:0645d8841f51 350
bogdanm 13:0645d8841f51 351 #ifdef __ARMCC_VERSION
bogdanm 13:0645d8841f51 352 extern "C" char *_sys_command_string(char *cmd, int len) {
bogdanm 13:0645d8841f51 353 return NULL;
bogdanm 13:0645d8841f51 354 }
bogdanm 13:0645d8841f51 355 #endif
bogdanm 13:0645d8841f51 356
bogdanm 13:0645d8841f51 357 extern "C" DIR *opendir(const char *path) {
bogdanm 13:0645d8841f51 358 /* root dir is FileSystemLike */
bogdanm 13:0645d8841f51 359 if (path[0] == '/' && path[1] == 0) {
bogdanm 13:0645d8841f51 360 return FileSystemLike::opendir();
bogdanm 13:0645d8841f51 361 }
bogdanm 13:0645d8841f51 362
bogdanm 13:0645d8841f51 363 FilePath fp(path);
bogdanm 13:0645d8841f51 364 FileSystemLike* fs = fp.fileSystem();
bogdanm 13:0645d8841f51 365 if (fs == NULL) return NULL;
bogdanm 13:0645d8841f51 366
bogdanm 13:0645d8841f51 367 return fs->opendir(fp.fileName());
bogdanm 13:0645d8841f51 368 }
bogdanm 13:0645d8841f51 369
bogdanm 13:0645d8841f51 370 extern "C" struct dirent *readdir(DIR *dir) {
bogdanm 13:0645d8841f51 371 return dir->readdir();
bogdanm 13:0645d8841f51 372 }
bogdanm 13:0645d8841f51 373
bogdanm 13:0645d8841f51 374 extern "C" int closedir(DIR *dir) {
bogdanm 13:0645d8841f51 375 return dir->closedir();
bogdanm 13:0645d8841f51 376 }
bogdanm 13:0645d8841f51 377
bogdanm 13:0645d8841f51 378 extern "C" void rewinddir(DIR *dir) {
bogdanm 13:0645d8841f51 379 dir->rewinddir();
bogdanm 13:0645d8841f51 380 }
bogdanm 13:0645d8841f51 381
bogdanm 13:0645d8841f51 382 extern "C" off_t telldir(DIR *dir) {
bogdanm 13:0645d8841f51 383 return dir->telldir();
bogdanm 13:0645d8841f51 384 }
bogdanm 13:0645d8841f51 385
bogdanm 13:0645d8841f51 386 extern "C" void seekdir(DIR *dir, off_t off) {
bogdanm 13:0645d8841f51 387 dir->seekdir(off);
bogdanm 13:0645d8841f51 388 }
bogdanm 13:0645d8841f51 389
bogdanm 13:0645d8841f51 390 extern "C" int mkdir(const char *path, mode_t mode) {
bogdanm 13:0645d8841f51 391 FilePath fp(path);
bogdanm 13:0645d8841f51 392 FileSystemLike *fs = fp.fileSystem();
bogdanm 13:0645d8841f51 393 if (fs == NULL) return -1;
bogdanm 13:0645d8841f51 394
bogdanm 13:0645d8841f51 395 return fs->mkdir(fp.fileName(), mode);
bogdanm 13:0645d8841f51 396 }
bogdanm 13:0645d8841f51 397
bogdanm 13:0645d8841f51 398 #if defined(TOOLCHAIN_GCC)
bogdanm 13:0645d8841f51 399 /* prevents the exception handling name demangling code getting pulled in */
mbed_official 285:31249416b6f9 400 #include "mbed_error.h"
bogdanm 13:0645d8841f51 401 namespace __gnu_cxx {
bogdanm 13:0645d8841f51 402 void __verbose_terminate_handler() {
bogdanm 13:0645d8841f51 403 error("Exception");
bogdanm 13:0645d8841f51 404 }
bogdanm 13:0645d8841f51 405 }
bogdanm 13:0645d8841f51 406 extern "C" WEAK void __cxa_pure_virtual(void);
bogdanm 13:0645d8841f51 407 extern "C" WEAK void __cxa_pure_virtual(void) {
bogdanm 13:0645d8841f51 408 exit(1);
bogdanm 13:0645d8841f51 409 }
bogdanm 13:0645d8841f51 410
bogdanm 13:0645d8841f51 411 #endif
bogdanm 13:0645d8841f51 412
bogdanm 13:0645d8841f51 413 // ****************************************************************************
bogdanm 13:0645d8841f51 414 // mbed_main is a function that is called before main()
mbed_official 83:5a6f638110fe 415 // mbed_sdk_init() is also a function that is called before main(), but unlike
mbed_official 83:5a6f638110fe 416 // mbed_main(), it is not meant for user code, but for the SDK itself to perform
mbed_official 83:5a6f638110fe 417 // initializations before main() is called.
bogdanm 13:0645d8841f51 418
bogdanm 13:0645d8841f51 419 extern "C" WEAK void mbed_main(void);
bogdanm 13:0645d8841f51 420 extern "C" WEAK void mbed_main(void) {
bogdanm 13:0645d8841f51 421 }
bogdanm 13:0645d8841f51 422
mbed_official 83:5a6f638110fe 423 extern "C" WEAK void mbed_sdk_init(void);
mbed_official 83:5a6f638110fe 424 extern "C" WEAK void mbed_sdk_init(void) {
mbed_official 83:5a6f638110fe 425 }
mbed_official 83:5a6f638110fe 426
bogdanm 13:0645d8841f51 427 #if defined(TOOLCHAIN_ARM)
bogdanm 13:0645d8841f51 428 extern "C" int $Super$$main(void);
bogdanm 13:0645d8841f51 429
bogdanm 13:0645d8841f51 430 extern "C" int $Sub$$main(void) {
mbed_official 83:5a6f638110fe 431 mbed_sdk_init();
bogdanm 13:0645d8841f51 432 mbed_main();
bogdanm 13:0645d8841f51 433 return $Super$$main();
bogdanm 13:0645d8841f51 434 }
bogdanm 13:0645d8841f51 435 #elif defined(TOOLCHAIN_GCC)
bogdanm 13:0645d8841f51 436 extern "C" int __real_main(void);
bogdanm 13:0645d8841f51 437
bogdanm 13:0645d8841f51 438 extern "C" int __wrap_main(void) {
mbed_official 83:5a6f638110fe 439 mbed_sdk_init();
bogdanm 13:0645d8841f51 440 mbed_main();
bogdanm 13:0645d8841f51 441 return __real_main();
bogdanm 13:0645d8841f51 442 }
bogdanm 13:0645d8841f51 443 #elif defined(TOOLCHAIN_IAR)
bogdanm 13:0645d8841f51 444 // IAR doesn't have the $Super/$Sub mechanism of armcc, nor something equivalent
bogdanm 13:0645d8841f51 445 // to ld's --wrap. It does have a --redirect, but that doesn't help, since redirecting
bogdanm 13:0645d8841f51 446 // 'main' to another symbol looses the original 'main' symbol. However, its startup
bogdanm 13:0645d8841f51 447 // code will call a function to setup argc and argv (__iar_argc_argv) if it is defined.
bogdanm 13:0645d8841f51 448 // Since mbed doesn't use argc/argv, we use this function to call our mbed_main.
bogdanm 13:0645d8841f51 449 extern "C" void __iar_argc_argv() {
mbed_official 83:5a6f638110fe 450 mbed_sdk_init();
bogdanm 13:0645d8841f51 451 mbed_main();
bogdanm 13:0645d8841f51 452 }
bogdanm 13:0645d8841f51 453 #endif
bogdanm 20:4263a77256ae 454
bogdanm 20:4263a77256ae 455 // Provide implementation of _sbrk (low-level dynamic memory allocation
bogdanm 20:4263a77256ae 456 // routine) for GCC_ARM which compares new heap pointer with MSP instead of
bogdanm 20:4263a77256ae 457 // SP. This make it compatible with RTX RTOS thread stacks.
bogdanm 20:4263a77256ae 458 #if defined(TOOLCHAIN_GCC_ARM)
bogdanm 20:4263a77256ae 459 // Linker defined symbol used by _sbrk to indicate where heap should start.
bogdanm 20:4263a77256ae 460 extern "C" int __end__;
bogdanm 20:4263a77256ae 461
mbed_official 485:d9a48e768ce0 462 #if defined(TARGET_CORTEX_A)
mbed_official 485:d9a48e768ce0 463 extern "C" uint32_t __HeapLimit;
mbed_official 485:d9a48e768ce0 464 #endif
mbed_official 485:d9a48e768ce0 465
bogdanm 20:4263a77256ae 466 // Turn off the errno macro and use actual global variable instead.
bogdanm 20:4263a77256ae 467 #undef errno
bogdanm 20:4263a77256ae 468 extern "C" int errno;
bogdanm 20:4263a77256ae 469
mbed_official 23:8d50de55f208 470 // For ARM7 only
mbed_official 23:8d50de55f208 471 register unsigned char * stack_ptr __asm ("sp");
mbed_official 23:8d50de55f208 472
bogdanm 20:4263a77256ae 473 // Dynamic memory allocation related syscall.
bogdanm 20:4263a77256ae 474 extern "C" caddr_t _sbrk(int incr) {
bogdanm 20:4263a77256ae 475 static unsigned char* heap = (unsigned char*)&__end__;
bogdanm 20:4263a77256ae 476 unsigned char* prev_heap = heap;
bogdanm 20:4263a77256ae 477 unsigned char* new_heap = heap + incr;
bogdanm 20:4263a77256ae 478
mbed_official 24:75304dd5f5fb 479 #if defined(TARGET_ARM7)
mbed_official 24:75304dd5f5fb 480 if (new_heap >= stack_ptr) {
mbed_official 485:d9a48e768ce0 481 #elif defined(TARGET_CORTEX_A)
mbed_official 485:d9a48e768ce0 482 if (new_heap >= (unsigned char*)&__HeapLimit) { /* __HeapLimit is end of heap section */
mbed_official 24:75304dd5f5fb 483 #else
bogdanm 20:4263a77256ae 484 if (new_heap >= (unsigned char*)__get_MSP()) {
mbed_official 23:8d50de55f208 485 #endif
bogdanm 20:4263a77256ae 486 errno = ENOMEM;
bogdanm 20:4263a77256ae 487 return (caddr_t)-1;
bogdanm 20:4263a77256ae 488 }
mbed_official 221:8276e3a4886f 489
bogdanm 20:4263a77256ae 490 heap = new_heap;
bogdanm 20:4263a77256ae 491 return (caddr_t) prev_heap;
bogdanm 20:4263a77256ae 492 }
bogdanm 20:4263a77256ae 493 #endif
mbed_official 432:255afbe6270c 494
mbed_official 432:255afbe6270c 495
mbed_official 476:44b5a0bc6da6 496 #ifdef TOOLCHAIN_GCC_CW
mbed_official 476:44b5a0bc6da6 497 // TODO: Ideally, we would like to define directly "_ExitProcess"
mbed_official 476:44b5a0bc6da6 498 extern "C" void mbed_exit(int return_code) {
mbed_official 476:44b5a0bc6da6 499 #elif defined TOOLCHAIN_GCC_ARM
mbed_official 476:44b5a0bc6da6 500 extern "C" void _exit(int return_code) {
mbed_official 476:44b5a0bc6da6 501 #else
mbed_official 479:4dac56e9ae44 502 namespace std {
mbed_official 476:44b5a0bc6da6 503 extern "C" void exit(int return_code) {
mbed_official 476:44b5a0bc6da6 504 #endif
mbed_official 476:44b5a0bc6da6 505
mbed_official 476:44b5a0bc6da6 506 #if DEVICE_STDIO_MESSAGES
mbed_official 476:44b5a0bc6da6 507 fflush(stdout);
mbed_official 476:44b5a0bc6da6 508 fflush(stderr);
mbed_official 476:44b5a0bc6da6 509 #endif
mbed_official 476:44b5a0bc6da6 510
mbed_official 476:44b5a0bc6da6 511 #if DEVICE_SEMIHOST
mbed_official 476:44b5a0bc6da6 512 if (mbed_interface_connected()) {
mbed_official 476:44b5a0bc6da6 513 semihost_exit();
mbed_official 476:44b5a0bc6da6 514 }
mbed_official 476:44b5a0bc6da6 515 #endif
mbed_official 476:44b5a0bc6da6 516 if (return_code) {
mbed_official 476:44b5a0bc6da6 517 mbed_die();
mbed_official 476:44b5a0bc6da6 518 }
mbed_official 476:44b5a0bc6da6 519
mbed_official 476:44b5a0bc6da6 520 while (1);
mbed_official 476:44b5a0bc6da6 521 }
mbed_official 476:44b5a0bc6da6 522
mbed_official 479:4dac56e9ae44 523 #if !defined(TOOLCHAIN_GCC_ARM) && !defined(TOOLCHAIN_GCC_CW)
mbed_official 479:4dac56e9ae44 524 } //namespace std
mbed_official 479:4dac56e9ae44 525 #endif
mbed_official 479:4dac56e9ae44 526
mbed_official 476:44b5a0bc6da6 527
mbed_official 432:255afbe6270c 528 namespace mbed {
mbed_official 432:255afbe6270c 529
mbed_official 432:255afbe6270c 530 void mbed_set_unbuffered_stream(FILE *_file) {
mbed_official 432:255afbe6270c 531 #if defined (__ICCARM__)
mbed_official 432:255afbe6270c 532 char buf[2];
mbed_official 432:255afbe6270c 533 std::setvbuf(_file,buf,_IONBF,NULL);
mbed_official 432:255afbe6270c 534 #else
mbed_official 432:255afbe6270c 535 setbuf(_file, NULL);
mbed_official 432:255afbe6270c 536 #endif
mbed_official 432:255afbe6270c 537 }
mbed_official 432:255afbe6270c 538
mbed_official 432:255afbe6270c 539 int mbed_getc(FILE *_file){
mbed_official 432:255afbe6270c 540 #if defined (__ICCARM__)
mbed_official 432:255afbe6270c 541 /*This is only valid for unbuffered streams*/
mbed_official 432:255afbe6270c 542 int res = std::fgetc(_file);
mbed_official 432:255afbe6270c 543 if (res>=0){
mbed_official 432:255afbe6270c 544 _file->_Mode = (unsigned short)(_file->_Mode & ~ 0x1000);/* Unset read mode */
mbed_official 432:255afbe6270c 545 _file->_Rend = _file->_Wend;
mbed_official 432:255afbe6270c 546 _file->_Next = _file->_Wend;
mbed_official 432:255afbe6270c 547 }
mbed_official 432:255afbe6270c 548 return res;
mbed_official 432:255afbe6270c 549 #else
mbed_official 432:255afbe6270c 550 return std::fgetc(_file);
mbed_official 432:255afbe6270c 551 #endif
mbed_official 432:255afbe6270c 552 }
mbed_official 432:255afbe6270c 553
mbed_official 432:255afbe6270c 554 char* mbed_gets(char*s, int size, FILE *_file){
mbed_official 432:255afbe6270c 555 #if defined (__ICCARM__)
mbed_official 432:255afbe6270c 556 /*This is only valid for unbuffered streams*/
mbed_official 432:255afbe6270c 557 char *str = fgets(s,size,_file);
mbed_official 432:255afbe6270c 558 if (str!=NULL){
mbed_official 432:255afbe6270c 559 _file->_Mode = (unsigned short)(_file->_Mode & ~ 0x1000);/* Unset read mode */
mbed_official 432:255afbe6270c 560 _file->_Rend = _file->_Wend;
mbed_official 432:255afbe6270c 561 _file->_Next = _file->_Wend;
mbed_official 432:255afbe6270c 562 }
mbed_official 432:255afbe6270c 563 return str;
mbed_official 432:255afbe6270c 564 #else
mbed_official 432:255afbe6270c 565 return std::fgets(s,size,_file);
mbed_official 432:255afbe6270c 566 #endif
mbed_official 432:255afbe6270c 567 }
mbed_official 432:255afbe6270c 568
mbed_official 432:255afbe6270c 569 } // namespace mbed