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