fix nrf51822 i2c & spi conflict

Dependencies:   BLE_API eMPL_MPU6050 nRF51822

Fork of Seeed_Tiny_BLE_Flash by Darren Huang

Committer:
yihui
Date:
Tue Nov 17 07:48:56 2015 +0000
Revision:
5:b8c02645e6af
fix i2c & spi conflict

Who changed what in which revision?

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