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