PokittoLib is the library needed for programming the Pokitto DIY game console (www.pokitto.com)

Dependents:   Sensitive

Fork of PokittoLib by Jonne Valola

Committer:
spinal
Date:
Wed Oct 18 14:47:54 2017 +0000
Revision:
15:0bbe8f6fae32
Parent:
6:ea7377f3d1af
direct lcd stuff used by sensitive

Who changed what in which revision?

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