STM32F031K6 Fork of mbed library sources. PLL are disabled for lower clock.

Fork of mbed-dev by mbed official

Committer:
Dot
Date:
Tue May 31 22:46:42 2016 +0000
Revision:
133:6b577b9a0a7d
Parent:
85:4b017b08d507
Forked for F031K6 for UART & I2C

Who changed what in which revision?

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