PokittoLib with changes to lcd refresh etc.

Dependents:   Pokittris

Fork of Pokitto by Pokitto Community Team

This is a fork by user @Spinal, and is used in Pokittris for testing. Do not import this to your own program.

Committer:
Pokitto
Date:
Sat Oct 07 21:31:12 2017 +0000
Revision:
5:7e5c566b1760
mbed-pokitto integrated

Who changed what in which revision?

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