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