Lcd companion boards support (VKLCD50RTA & VKLCD70RT)
What is this ?
This is a demo program using Renesas RGA library & USB Camera to demonstrate VK-RZ/A1H's companion boards workability.
Supported companion Boards:
VKLCD50RTA
VKLCD70RT
How to Configure ?
You can choose which display is installed by altering the lcd_panel.h file
Leave the active one & comment out the others:
#define LCD_VDC5_CH0_PANEL LCD_CH0_PANEL_VKLCD50RTA //#define LCD_VDC5_CH0_PANEL LCD_CH0_PANEL_VKLCD70RT
You can alter the whole demo with your pictures if you like:
How to compile ?
- The Demo can be compiled in 3 modes:
- I. Execution from the internal 10-MB on-chip SRAM.
- II. Execution from the on-board serial FALSH in dual (32-MB) mode.
- After import in the online compiler just leave only the VKRZA1H_DOUBLE.sct & delete all others linker files in the TOOLCHAIN_ARM_STD folder.
- Drag & drop the result binary in MBED disk, (previously inited in double flash mode)
- III. Execution from the on-board serial FALSH in single (16-MB) mode.
- After import in the online compiler just leave only the VKRZA1H_SINGLE.sct & delete all others linker files in the TOOLCHAIN_ARM_STD folder.
- Drag & drop the result binary in MBED disk, (previously inited in single flash mode )
Quick presentation:
Other demos ?
More demos you can find on our FTP
hal/common/retarget.cpp@0:6435b67ad23c, 2017-02-16 (annotated)
- Committer:
- tvendov
- Date:
- Thu Feb 16 10:23:48 2017 +0000
- Revision:
- 0:6435b67ad23c
Initial lcd support (VKLCD50RTA & VKLCD70RT companion boards)
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
tvendov | 0:6435b67ad23c | 1 | /* mbed Microcontroller Library |
tvendov | 0:6435b67ad23c | 2 | * Copyright (c) 2006-2015 ARM Limited |
tvendov | 0:6435b67ad23c | 3 | * |
tvendov | 0:6435b67ad23c | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
tvendov | 0:6435b67ad23c | 5 | * you may not use this file except in compliance with the License. |
tvendov | 0:6435b67ad23c | 6 | * You may obtain a copy of the License at |
tvendov | 0:6435b67ad23c | 7 | * |
tvendov | 0:6435b67ad23c | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
tvendov | 0:6435b67ad23c | 9 | * |
tvendov | 0:6435b67ad23c | 10 | * Unless required by applicable law or agreed to in writing, software |
tvendov | 0:6435b67ad23c | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
tvendov | 0:6435b67ad23c | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
tvendov | 0:6435b67ad23c | 13 | * See the License for the specific language governing permissions and |
tvendov | 0:6435b67ad23c | 14 | * limitations under the License. |
tvendov | 0:6435b67ad23c | 15 | */ |
tvendov | 0:6435b67ad23c | 16 | #include "platform.h" |
tvendov | 0:6435b67ad23c | 17 | #include "FileHandle.h" |
tvendov | 0:6435b67ad23c | 18 | #include "FileSystemLike.h" |
tvendov | 0:6435b67ad23c | 19 | #include "FilePath.h" |
tvendov | 0:6435b67ad23c | 20 | #include "serial_api.h" |
tvendov | 0:6435b67ad23c | 21 | #include "toolchain.h" |
tvendov | 0:6435b67ad23c | 22 | #include "semihost_api.h" |
tvendov | 0:6435b67ad23c | 23 | #include "mbed_interface.h" |
tvendov | 0:6435b67ad23c | 24 | #include "SingletonPtr.h" |
tvendov | 0:6435b67ad23c | 25 | #include "PlatformMutex.h" |
tvendov | 0:6435b67ad23c | 26 | #include "mbed_error.h" |
tvendov | 0:6435b67ad23c | 27 | #include "mbed_stats.h" |
tvendov | 0:6435b67ad23c | 28 | #include <stdlib.h> |
tvendov | 0:6435b67ad23c | 29 | #include <string.h> |
tvendov | 0:6435b67ad23c | 30 | #if DEVICE_STDIO_MESSAGES |
tvendov | 0:6435b67ad23c | 31 | #include <stdio.h> |
tvendov | 0:6435b67ad23c | 32 | #endif |
tvendov | 0:6435b67ad23c | 33 | #include <errno.h> |
tvendov | 0:6435b67ad23c | 34 | |
tvendov | 0:6435b67ad23c | 35 | #if defined(__ARMCC_VERSION) |
tvendov | 0:6435b67ad23c | 36 | # include <rt_sys.h> |
tvendov | 0:6435b67ad23c | 37 | # define PREFIX(x) _sys##x |
tvendov | 0:6435b67ad23c | 38 | # define OPEN_MAX _SYS_OPEN |
tvendov | 0:6435b67ad23c | 39 | # ifdef __MICROLIB |
tvendov | 0:6435b67ad23c | 40 | # pragma import(__use_full_stdio) |
tvendov | 0:6435b67ad23c | 41 | # endif |
tvendov | 0:6435b67ad23c | 42 | |
tvendov | 0:6435b67ad23c | 43 | #elif defined(__ICCARM__) |
tvendov | 0:6435b67ad23c | 44 | # include <yfuns.h> |
tvendov | 0:6435b67ad23c | 45 | # define PREFIX(x) _##x |
tvendov | 0:6435b67ad23c | 46 | # define OPEN_MAX 16 |
tvendov | 0:6435b67ad23c | 47 | |
tvendov | 0:6435b67ad23c | 48 | # define STDIN_FILENO 0 |
tvendov | 0:6435b67ad23c | 49 | # define STDOUT_FILENO 1 |
tvendov | 0:6435b67ad23c | 50 | # define STDERR_FILENO 2 |
tvendov | 0:6435b67ad23c | 51 | |
tvendov | 0:6435b67ad23c | 52 | #else |
tvendov | 0:6435b67ad23c | 53 | # include <sys/stat.h> |
tvendov | 0:6435b67ad23c | 54 | # include <sys/unistd.h> |
tvendov | 0:6435b67ad23c | 55 | # include <sys/syslimits.h> |
tvendov | 0:6435b67ad23c | 56 | # define PREFIX(x) x |
tvendov | 0:6435b67ad23c | 57 | #endif |
tvendov | 0:6435b67ad23c | 58 | |
tvendov | 0:6435b67ad23c | 59 | #define FILE_HANDLE_RESERVED 0xFFFFFFFF |
tvendov | 0:6435b67ad23c | 60 | |
tvendov | 0:6435b67ad23c | 61 | using namespace mbed; |
tvendov | 0:6435b67ad23c | 62 | |
tvendov | 0:6435b67ad23c | 63 | #if defined(__MICROLIB) && (__ARMCC_VERSION>5030000) |
tvendov | 0:6435b67ad23c | 64 | // Before version 5.03, we were using a patched version of microlib with proper names |
tvendov | 0:6435b67ad23c | 65 | extern const char __stdin_name[] = ":tt"; |
tvendov | 0:6435b67ad23c | 66 | extern const char __stdout_name[] = ":tt"; |
tvendov | 0:6435b67ad23c | 67 | extern const char __stderr_name[] = ":tt"; |
tvendov | 0:6435b67ad23c | 68 | |
tvendov | 0:6435b67ad23c | 69 | #else |
tvendov | 0:6435b67ad23c | 70 | extern const char __stdin_name[] = "/stdin"; |
tvendov | 0:6435b67ad23c | 71 | extern const char __stdout_name[] = "/stdout"; |
tvendov | 0:6435b67ad23c | 72 | extern const char __stderr_name[] = "/stderr"; |
tvendov | 0:6435b67ad23c | 73 | #endif |
tvendov | 0:6435b67ad23c | 74 | |
tvendov | 0:6435b67ad23c | 75 | // Heap limits - only used if set |
tvendov | 0:6435b67ad23c | 76 | unsigned char *mbed_heap_start = 0; |
tvendov | 0:6435b67ad23c | 77 | uint32_t mbed_heap_size = 0; |
tvendov | 0:6435b67ad23c | 78 | |
tvendov | 0:6435b67ad23c | 79 | /* newlib has the filehandle field in the FILE struct as a short, so |
tvendov | 0:6435b67ad23c | 80 | * we can't just return a Filehandle* from _open and instead have to |
tvendov | 0:6435b67ad23c | 81 | * put it in a filehandles array and return the index into that array |
tvendov | 0:6435b67ad23c | 82 | * (or rather index+3, as filehandles 0-2 are stdin/out/err). |
tvendov | 0:6435b67ad23c | 83 | */ |
tvendov | 0:6435b67ad23c | 84 | static FileHandle *filehandles[OPEN_MAX]; |
tvendov | 0:6435b67ad23c | 85 | static SingletonPtr<PlatformMutex> filehandle_mutex; |
tvendov | 0:6435b67ad23c | 86 | |
tvendov | 0:6435b67ad23c | 87 | FileHandle::~FileHandle() { |
tvendov | 0:6435b67ad23c | 88 | filehandle_mutex->lock(); |
tvendov | 0:6435b67ad23c | 89 | /* Remove all open filehandles for this */ |
tvendov | 0:6435b67ad23c | 90 | for (unsigned int fh_i = 0; fh_i < sizeof(filehandles)/sizeof(*filehandles); fh_i++) { |
tvendov | 0:6435b67ad23c | 91 | if (filehandles[fh_i] == this) { |
tvendov | 0:6435b67ad23c | 92 | filehandles[fh_i] = NULL; |
tvendov | 0:6435b67ad23c | 93 | } |
tvendov | 0:6435b67ad23c | 94 | } |
tvendov | 0:6435b67ad23c | 95 | filehandle_mutex->unlock(); |
tvendov | 0:6435b67ad23c | 96 | } |
tvendov | 0:6435b67ad23c | 97 | |
tvendov | 0:6435b67ad23c | 98 | #if DEVICE_SERIAL |
tvendov | 0:6435b67ad23c | 99 | extern int stdio_uart_inited; |
tvendov | 0:6435b67ad23c | 100 | extern serial_t stdio_uart; |
tvendov | 0:6435b67ad23c | 101 | #if MBED_CONF_CORE_STDIO_CONVERT_NEWLINES |
tvendov | 0:6435b67ad23c | 102 | static char stdio_in_prev; |
tvendov | 0:6435b67ad23c | 103 | static char stdio_out_prev; |
tvendov | 0:6435b67ad23c | 104 | #endif |
tvendov | 0:6435b67ad23c | 105 | #endif |
tvendov | 0:6435b67ad23c | 106 | |
tvendov | 0:6435b67ad23c | 107 | static void init_serial() { |
tvendov | 0:6435b67ad23c | 108 | #if DEVICE_SERIAL |
tvendov | 0:6435b67ad23c | 109 | if (stdio_uart_inited) return; |
tvendov | 0:6435b67ad23c | 110 | serial_init(&stdio_uart, STDIO_UART_TX, STDIO_UART_RX); |
tvendov | 0:6435b67ad23c | 111 | #if MBED_CONF_CORE_STDIO_BAUD_RATE |
tvendov | 0:6435b67ad23c | 112 | serial_baud(&stdio_uart, MBED_CONF_CORE_STDIO_BAUD_RATE); |
tvendov | 0:6435b67ad23c | 113 | #endif |
tvendov | 0:6435b67ad23c | 114 | #endif |
tvendov | 0:6435b67ad23c | 115 | } |
tvendov | 0:6435b67ad23c | 116 | |
tvendov | 0:6435b67ad23c | 117 | static inline int openmode_to_posix(int openmode) { |
tvendov | 0:6435b67ad23c | 118 | int posix = openmode; |
tvendov | 0:6435b67ad23c | 119 | #ifdef __ARMCC_VERSION |
tvendov | 0:6435b67ad23c | 120 | if (openmode & OPEN_PLUS) { |
tvendov | 0:6435b67ad23c | 121 | posix = O_RDWR; |
tvendov | 0:6435b67ad23c | 122 | } else if(openmode & OPEN_W) { |
tvendov | 0:6435b67ad23c | 123 | posix = O_WRONLY; |
tvendov | 0:6435b67ad23c | 124 | } else if(openmode & OPEN_A) { |
tvendov | 0:6435b67ad23c | 125 | posix = O_WRONLY|O_APPEND; |
tvendov | 0:6435b67ad23c | 126 | } else { |
tvendov | 0:6435b67ad23c | 127 | posix = O_RDONLY; |
tvendov | 0:6435b67ad23c | 128 | } |
tvendov | 0:6435b67ad23c | 129 | /* a, w, a+, w+ all create if file does not already exist */ |
tvendov | 0:6435b67ad23c | 130 | if (openmode & (OPEN_A|OPEN_W)) { |
tvendov | 0:6435b67ad23c | 131 | posix |= O_CREAT; |
tvendov | 0:6435b67ad23c | 132 | } |
tvendov | 0:6435b67ad23c | 133 | /* w and w+ truncate */ |
tvendov | 0:6435b67ad23c | 134 | if (openmode & OPEN_W) { |
tvendov | 0:6435b67ad23c | 135 | posix |= O_TRUNC; |
tvendov | 0:6435b67ad23c | 136 | } |
tvendov | 0:6435b67ad23c | 137 | #elif defined(__ICCARM__) |
tvendov | 0:6435b67ad23c | 138 | switch (openmode & _LLIO_RDWRMASK) { |
tvendov | 0:6435b67ad23c | 139 | case _LLIO_RDONLY: posix = O_RDONLY; break; |
tvendov | 0:6435b67ad23c | 140 | case _LLIO_WRONLY: posix = O_WRONLY; break; |
tvendov | 0:6435b67ad23c | 141 | case _LLIO_RDWR : posix = O_RDWR ; break; |
tvendov | 0:6435b67ad23c | 142 | } |
tvendov | 0:6435b67ad23c | 143 | if (openmode & _LLIO_CREAT ) posix |= O_CREAT; |
tvendov | 0:6435b67ad23c | 144 | if (openmode & _LLIO_APPEND) posix |= O_APPEND; |
tvendov | 0:6435b67ad23c | 145 | if (openmode & _LLIO_TRUNC ) posix |= O_TRUNC; |
tvendov | 0:6435b67ad23c | 146 | #elif defined(TOOLCHAIN_GCC) |
tvendov | 0:6435b67ad23c | 147 | posix &= ~O_BINARY; |
tvendov | 0:6435b67ad23c | 148 | #endif |
tvendov | 0:6435b67ad23c | 149 | return posix; |
tvendov | 0:6435b67ad23c | 150 | } |
tvendov | 0:6435b67ad23c | 151 | |
tvendov | 0:6435b67ad23c | 152 | extern "C" FILEHANDLE PREFIX(_open)(const char* name, int openmode) { |
tvendov | 0:6435b67ad23c | 153 | #if defined(__MICROLIB) && (__ARMCC_VERSION>5030000) |
tvendov | 0:6435b67ad23c | 154 | // Before version 5.03, we were using a patched version of microlib with proper names |
tvendov | 0:6435b67ad23c | 155 | // This is the workaround that the microlib author suggested us |
tvendov | 0:6435b67ad23c | 156 | static int n = 0; |
tvendov | 0:6435b67ad23c | 157 | if (!std::strcmp(name, ":tt")) return n++; |
tvendov | 0:6435b67ad23c | 158 | |
tvendov | 0:6435b67ad23c | 159 | #else |
tvendov | 0:6435b67ad23c | 160 | /* Use the posix convention that stdin,out,err are filehandles 0,1,2. |
tvendov | 0:6435b67ad23c | 161 | */ |
tvendov | 0:6435b67ad23c | 162 | if (std::strcmp(name, __stdin_name) == 0) { |
tvendov | 0:6435b67ad23c | 163 | init_serial(); |
tvendov | 0:6435b67ad23c | 164 | return 0; |
tvendov | 0:6435b67ad23c | 165 | } else if (std::strcmp(name, __stdout_name) == 0) { |
tvendov | 0:6435b67ad23c | 166 | init_serial(); |
tvendov | 0:6435b67ad23c | 167 | return 1; |
tvendov | 0:6435b67ad23c | 168 | } else if (std::strcmp(name, __stderr_name) == 0) { |
tvendov | 0:6435b67ad23c | 169 | init_serial(); |
tvendov | 0:6435b67ad23c | 170 | return 2; |
tvendov | 0:6435b67ad23c | 171 | } |
tvendov | 0:6435b67ad23c | 172 | #endif |
tvendov | 0:6435b67ad23c | 173 | |
tvendov | 0:6435b67ad23c | 174 | // find the first empty slot in filehandles |
tvendov | 0:6435b67ad23c | 175 | filehandle_mutex->lock(); |
tvendov | 0:6435b67ad23c | 176 | unsigned int fh_i; |
tvendov | 0:6435b67ad23c | 177 | for (fh_i = 0; fh_i < sizeof(filehandles)/sizeof(*filehandles); fh_i++) { |
tvendov | 0:6435b67ad23c | 178 | if (filehandles[fh_i] == NULL) break; |
tvendov | 0:6435b67ad23c | 179 | } |
tvendov | 0:6435b67ad23c | 180 | if (fh_i >= sizeof(filehandles)/sizeof(*filehandles)) { |
tvendov | 0:6435b67ad23c | 181 | filehandle_mutex->unlock(); |
tvendov | 0:6435b67ad23c | 182 | return -1; |
tvendov | 0:6435b67ad23c | 183 | } |
tvendov | 0:6435b67ad23c | 184 | filehandles[fh_i] = (FileHandle*)FILE_HANDLE_RESERVED; |
tvendov | 0:6435b67ad23c | 185 | filehandle_mutex->unlock(); |
tvendov | 0:6435b67ad23c | 186 | |
tvendov | 0:6435b67ad23c | 187 | FileHandle *res; |
tvendov | 0:6435b67ad23c | 188 | |
tvendov | 0:6435b67ad23c | 189 | /* FILENAME: ":0x12345678" describes a FileLike* */ |
tvendov | 0:6435b67ad23c | 190 | if (name[0] == ':') { |
tvendov | 0:6435b67ad23c | 191 | void *p; |
tvendov | 0:6435b67ad23c | 192 | sscanf(name, ":%p", &p); |
tvendov | 0:6435b67ad23c | 193 | res = (FileHandle*)p; |
tvendov | 0:6435b67ad23c | 194 | |
tvendov | 0:6435b67ad23c | 195 | /* FILENAME: "/file_system/file_name" */ |
tvendov | 0:6435b67ad23c | 196 | } else { |
tvendov | 0:6435b67ad23c | 197 | FilePath path(name); |
tvendov | 0:6435b67ad23c | 198 | |
tvendov | 0:6435b67ad23c | 199 | if (!path.exists()) { |
tvendov | 0:6435b67ad23c | 200 | // Free file handle |
tvendov | 0:6435b67ad23c | 201 | filehandles[fh_i] = NULL; |
tvendov | 0:6435b67ad23c | 202 | return -1; |
tvendov | 0:6435b67ad23c | 203 | } else if (path.isFile()) { |
tvendov | 0:6435b67ad23c | 204 | res = path.file(); |
tvendov | 0:6435b67ad23c | 205 | } else { |
tvendov | 0:6435b67ad23c | 206 | FileSystemLike *fs = path.fileSystem(); |
tvendov | 0:6435b67ad23c | 207 | if (fs == NULL) { |
tvendov | 0:6435b67ad23c | 208 | // Free file handle |
tvendov | 0:6435b67ad23c | 209 | filehandles[fh_i] = NULL; |
tvendov | 0:6435b67ad23c | 210 | return -1; |
tvendov | 0:6435b67ad23c | 211 | } |
tvendov | 0:6435b67ad23c | 212 | int posix_mode = openmode_to_posix(openmode); |
tvendov | 0:6435b67ad23c | 213 | res = fs->open(path.fileName(), posix_mode); /* NULL if fails */ |
tvendov | 0:6435b67ad23c | 214 | } |
tvendov | 0:6435b67ad23c | 215 | } |
tvendov | 0:6435b67ad23c | 216 | |
tvendov | 0:6435b67ad23c | 217 | if (res == NULL) { |
tvendov | 0:6435b67ad23c | 218 | // Free file handle |
tvendov | 0:6435b67ad23c | 219 | filehandles[fh_i] = NULL; |
tvendov | 0:6435b67ad23c | 220 | return -1; |
tvendov | 0:6435b67ad23c | 221 | } |
tvendov | 0:6435b67ad23c | 222 | filehandles[fh_i] = res; |
tvendov | 0:6435b67ad23c | 223 | |
tvendov | 0:6435b67ad23c | 224 | return fh_i + 3; // +3 as filehandles 0-2 are stdin/out/err |
tvendov | 0:6435b67ad23c | 225 | } |
tvendov | 0:6435b67ad23c | 226 | |
tvendov | 0:6435b67ad23c | 227 | extern "C" int PREFIX(_close)(FILEHANDLE fh) { |
tvendov | 0:6435b67ad23c | 228 | if (fh < 3) return 0; |
tvendov | 0:6435b67ad23c | 229 | |
tvendov | 0:6435b67ad23c | 230 | FileHandle* fhc = filehandles[fh-3]; |
tvendov | 0:6435b67ad23c | 231 | filehandles[fh-3] = NULL; |
tvendov | 0:6435b67ad23c | 232 | if (fhc == NULL) return -1; |
tvendov | 0:6435b67ad23c | 233 | |
tvendov | 0:6435b67ad23c | 234 | return fhc->close(); |
tvendov | 0:6435b67ad23c | 235 | } |
tvendov | 0:6435b67ad23c | 236 | |
tvendov | 0:6435b67ad23c | 237 | #if defined(__ICCARM__) |
tvendov | 0:6435b67ad23c | 238 | extern "C" size_t __write (int fh, const unsigned char *buffer, size_t length) { |
tvendov | 0:6435b67ad23c | 239 | #else |
tvendov | 0:6435b67ad23c | 240 | extern "C" int PREFIX(_write)(FILEHANDLE fh, const unsigned char *buffer, unsigned int length, int mode) { |
tvendov | 0:6435b67ad23c | 241 | #endif |
tvendov | 0:6435b67ad23c | 242 | int n; // n is the number of bytes written |
tvendov | 0:6435b67ad23c | 243 | if (fh < 3) { |
tvendov | 0:6435b67ad23c | 244 | #if DEVICE_SERIAL |
tvendov | 0:6435b67ad23c | 245 | if (!stdio_uart_inited) init_serial(); |
tvendov | 0:6435b67ad23c | 246 | #if MBED_CONF_CORE_STDIO_CONVERT_NEWLINES |
tvendov | 0:6435b67ad23c | 247 | for (unsigned int i = 0; i < length; i++) { |
tvendov | 0:6435b67ad23c | 248 | if (buffer[i] == '\n' && stdio_out_prev != '\r') { |
tvendov | 0:6435b67ad23c | 249 | serial_putc(&stdio_uart, '\r'); |
tvendov | 0:6435b67ad23c | 250 | } |
tvendov | 0:6435b67ad23c | 251 | serial_putc(&stdio_uart, buffer[i]); |
tvendov | 0:6435b67ad23c | 252 | stdio_out_prev = buffer[i]; |
tvendov | 0:6435b67ad23c | 253 | } |
tvendov | 0:6435b67ad23c | 254 | #else |
tvendov | 0:6435b67ad23c | 255 | for (unsigned int i = 0; i < length; i++) { |
tvendov | 0:6435b67ad23c | 256 | serial_putc(&stdio_uart, buffer[i]); |
tvendov | 0:6435b67ad23c | 257 | } |
tvendov | 0:6435b67ad23c | 258 | #endif |
tvendov | 0:6435b67ad23c | 259 | #endif |
tvendov | 0:6435b67ad23c | 260 | n = length; |
tvendov | 0:6435b67ad23c | 261 | } else { |
tvendov | 0:6435b67ad23c | 262 | FileHandle* fhc = filehandles[fh-3]; |
tvendov | 0:6435b67ad23c | 263 | if (fhc == NULL) return -1; |
tvendov | 0:6435b67ad23c | 264 | |
tvendov | 0:6435b67ad23c | 265 | n = fhc->write(buffer, length); |
tvendov | 0:6435b67ad23c | 266 | } |
tvendov | 0:6435b67ad23c | 267 | #ifdef __ARMCC_VERSION |
tvendov | 0:6435b67ad23c | 268 | return length-n; |
tvendov | 0:6435b67ad23c | 269 | #else |
tvendov | 0:6435b67ad23c | 270 | return n; |
tvendov | 0:6435b67ad23c | 271 | #endif |
tvendov | 0:6435b67ad23c | 272 | } |
tvendov | 0:6435b67ad23c | 273 | |
tvendov | 0:6435b67ad23c | 274 | #if defined(__ICCARM__) |
tvendov | 0:6435b67ad23c | 275 | extern "C" size_t __read (int fh, unsigned char *buffer, size_t length) { |
tvendov | 0:6435b67ad23c | 276 | #else |
tvendov | 0:6435b67ad23c | 277 | extern "C" int PREFIX(_read)(FILEHANDLE fh, unsigned char *buffer, unsigned int length, int mode) { |
tvendov | 0:6435b67ad23c | 278 | #endif |
tvendov | 0:6435b67ad23c | 279 | int n; // n is the number of bytes read |
tvendov | 0:6435b67ad23c | 280 | if (fh < 3) { |
tvendov | 0:6435b67ad23c | 281 | // only read a character at a time from stdin |
tvendov | 0:6435b67ad23c | 282 | #if DEVICE_SERIAL |
tvendov | 0:6435b67ad23c | 283 | if (!stdio_uart_inited) init_serial(); |
tvendov | 0:6435b67ad23c | 284 | #if MBED_CONF_CORE_STDIO_CONVERT_NEWLINES |
tvendov | 0:6435b67ad23c | 285 | while (true) { |
tvendov | 0:6435b67ad23c | 286 | char c = serial_getc(&stdio_uart); |
tvendov | 0:6435b67ad23c | 287 | if ((c == '\r' && stdio_in_prev != '\n') || |
tvendov | 0:6435b67ad23c | 288 | (c == '\n' && stdio_in_prev != '\r')) { |
tvendov | 0:6435b67ad23c | 289 | stdio_in_prev = c; |
tvendov | 0:6435b67ad23c | 290 | *buffer = '\n'; |
tvendov | 0:6435b67ad23c | 291 | break; |
tvendov | 0:6435b67ad23c | 292 | } else if ((c == '\r' && stdio_in_prev == '\n') || |
tvendov | 0:6435b67ad23c | 293 | (c == '\n' && stdio_in_prev == '\r')) { |
tvendov | 0:6435b67ad23c | 294 | stdio_in_prev = c; |
tvendov | 0:6435b67ad23c | 295 | // onto next character |
tvendov | 0:6435b67ad23c | 296 | continue; |
tvendov | 0:6435b67ad23c | 297 | } else { |
tvendov | 0:6435b67ad23c | 298 | stdio_in_prev = c; |
tvendov | 0:6435b67ad23c | 299 | *buffer = c; |
tvendov | 0:6435b67ad23c | 300 | break; |
tvendov | 0:6435b67ad23c | 301 | } |
tvendov | 0:6435b67ad23c | 302 | } |
tvendov | 0:6435b67ad23c | 303 | #else |
tvendov | 0:6435b67ad23c | 304 | *buffer = serial_getc(&stdio_uart); |
tvendov | 0:6435b67ad23c | 305 | #endif |
tvendov | 0:6435b67ad23c | 306 | #endif |
tvendov | 0:6435b67ad23c | 307 | n = 1; |
tvendov | 0:6435b67ad23c | 308 | } else { |
tvendov | 0:6435b67ad23c | 309 | FileHandle* fhc = filehandles[fh-3]; |
tvendov | 0:6435b67ad23c | 310 | if (fhc == NULL) return -1; |
tvendov | 0:6435b67ad23c | 311 | |
tvendov | 0:6435b67ad23c | 312 | n = fhc->read(buffer, length); |
tvendov | 0:6435b67ad23c | 313 | } |
tvendov | 0:6435b67ad23c | 314 | #ifdef __ARMCC_VERSION |
tvendov | 0:6435b67ad23c | 315 | return length-n; |
tvendov | 0:6435b67ad23c | 316 | #else |
tvendov | 0:6435b67ad23c | 317 | return n; |
tvendov | 0:6435b67ad23c | 318 | #endif |
tvendov | 0:6435b67ad23c | 319 | } |
tvendov | 0:6435b67ad23c | 320 | |
tvendov | 0:6435b67ad23c | 321 | #ifdef __ARMCC_VERSION |
tvendov | 0:6435b67ad23c | 322 | extern "C" int PREFIX(_istty)(FILEHANDLE fh) |
tvendov | 0:6435b67ad23c | 323 | #else |
tvendov | 0:6435b67ad23c | 324 | extern "C" int _isatty(FILEHANDLE fh) |
tvendov | 0:6435b67ad23c | 325 | #endif |
tvendov | 0:6435b67ad23c | 326 | { |
tvendov | 0:6435b67ad23c | 327 | /* stdin, stdout and stderr should be tty */ |
tvendov | 0:6435b67ad23c | 328 | if (fh < 3) return 1; |
tvendov | 0:6435b67ad23c | 329 | |
tvendov | 0:6435b67ad23c | 330 | FileHandle* fhc = filehandles[fh-3]; |
tvendov | 0:6435b67ad23c | 331 | if (fhc == NULL) return -1; |
tvendov | 0:6435b67ad23c | 332 | |
tvendov | 0:6435b67ad23c | 333 | return fhc->isatty(); |
tvendov | 0:6435b67ad23c | 334 | } |
tvendov | 0:6435b67ad23c | 335 | |
tvendov | 0:6435b67ad23c | 336 | extern "C" |
tvendov | 0:6435b67ad23c | 337 | #if defined(__ARMCC_VERSION) |
tvendov | 0:6435b67ad23c | 338 | int _sys_seek(FILEHANDLE fh, long position) |
tvendov | 0:6435b67ad23c | 339 | #elif defined(__ICCARM__) |
tvendov | 0:6435b67ad23c | 340 | long __lseek(int fh, long offset, int whence) |
tvendov | 0:6435b67ad23c | 341 | #else |
tvendov | 0:6435b67ad23c | 342 | int _lseek(FILEHANDLE fh, int offset, int whence) |
tvendov | 0:6435b67ad23c | 343 | #endif |
tvendov | 0:6435b67ad23c | 344 | { |
tvendov | 0:6435b67ad23c | 345 | if (fh < 3) return 0; |
tvendov | 0:6435b67ad23c | 346 | |
tvendov | 0:6435b67ad23c | 347 | FileHandle* fhc = filehandles[fh-3]; |
tvendov | 0:6435b67ad23c | 348 | if (fhc == NULL) return -1; |
tvendov | 0:6435b67ad23c | 349 | |
tvendov | 0:6435b67ad23c | 350 | #if defined(__ARMCC_VERSION) |
tvendov | 0:6435b67ad23c | 351 | return fhc->lseek(position, SEEK_SET); |
tvendov | 0:6435b67ad23c | 352 | #else |
tvendov | 0:6435b67ad23c | 353 | return fhc->lseek(offset, whence); |
tvendov | 0:6435b67ad23c | 354 | #endif |
tvendov | 0:6435b67ad23c | 355 | } |
tvendov | 0:6435b67ad23c | 356 | |
tvendov | 0:6435b67ad23c | 357 | #ifdef __ARMCC_VERSION |
tvendov | 0:6435b67ad23c | 358 | extern "C" int PREFIX(_ensure)(FILEHANDLE fh) { |
tvendov | 0:6435b67ad23c | 359 | if (fh < 3) return 0; |
tvendov | 0:6435b67ad23c | 360 | |
tvendov | 0:6435b67ad23c | 361 | FileHandle* fhc = filehandles[fh-3]; |
tvendov | 0:6435b67ad23c | 362 | if (fhc == NULL) return -1; |
tvendov | 0:6435b67ad23c | 363 | |
tvendov | 0:6435b67ad23c | 364 | return fhc->fsync(); |
tvendov | 0:6435b67ad23c | 365 | } |
tvendov | 0:6435b67ad23c | 366 | |
tvendov | 0:6435b67ad23c | 367 | extern "C" long PREFIX(_flen)(FILEHANDLE fh) { |
tvendov | 0:6435b67ad23c | 368 | if (fh < 3) return 0; |
tvendov | 0:6435b67ad23c | 369 | |
tvendov | 0:6435b67ad23c | 370 | FileHandle* fhc = filehandles[fh-3]; |
tvendov | 0:6435b67ad23c | 371 | if (fhc == NULL) return -1; |
tvendov | 0:6435b67ad23c | 372 | |
tvendov | 0:6435b67ad23c | 373 | return fhc->flen(); |
tvendov | 0:6435b67ad23c | 374 | } |
tvendov | 0:6435b67ad23c | 375 | #endif |
tvendov | 0:6435b67ad23c | 376 | |
tvendov | 0:6435b67ad23c | 377 | |
tvendov | 0:6435b67ad23c | 378 | #if !defined(__ARMCC_VERSION) && !defined(__ICCARM__) |
tvendov | 0:6435b67ad23c | 379 | extern "C" int _fstat(int fd, struct stat *st) { |
tvendov | 0:6435b67ad23c | 380 | if ((STDOUT_FILENO == fd) || (STDERR_FILENO == fd) || (STDIN_FILENO == fd)) { |
tvendov | 0:6435b67ad23c | 381 | st->st_mode = S_IFCHR; |
tvendov | 0:6435b67ad23c | 382 | return 0; |
tvendov | 0:6435b67ad23c | 383 | } |
tvendov | 0:6435b67ad23c | 384 | |
tvendov | 0:6435b67ad23c | 385 | errno = EBADF; |
tvendov | 0:6435b67ad23c | 386 | return -1; |
tvendov | 0:6435b67ad23c | 387 | } |
tvendov | 0:6435b67ad23c | 388 | #endif |
tvendov | 0:6435b67ad23c | 389 | |
tvendov | 0:6435b67ad23c | 390 | namespace std { |
tvendov | 0:6435b67ad23c | 391 | extern "C" int remove(const char *path) { |
tvendov | 0:6435b67ad23c | 392 | FilePath fp(path); |
tvendov | 0:6435b67ad23c | 393 | FileSystemLike *fs = fp.fileSystem(); |
tvendov | 0:6435b67ad23c | 394 | if (fs == NULL) return -1; |
tvendov | 0:6435b67ad23c | 395 | |
tvendov | 0:6435b67ad23c | 396 | return fs->remove(fp.fileName()); |
tvendov | 0:6435b67ad23c | 397 | } |
tvendov | 0:6435b67ad23c | 398 | |
tvendov | 0:6435b67ad23c | 399 | extern "C" int rename(const char *oldname, const char *newname) { |
tvendov | 0:6435b67ad23c | 400 | FilePath fpOld(oldname); |
tvendov | 0:6435b67ad23c | 401 | FilePath fpNew(newname); |
tvendov | 0:6435b67ad23c | 402 | FileSystemLike *fsOld = fpOld.fileSystem(); |
tvendov | 0:6435b67ad23c | 403 | FileSystemLike *fsNew = fpNew.fileSystem(); |
tvendov | 0:6435b67ad23c | 404 | |
tvendov | 0:6435b67ad23c | 405 | /* rename only if both files are on the same FS */ |
tvendov | 0:6435b67ad23c | 406 | if (fsOld != fsNew || fsOld == NULL) return -1; |
tvendov | 0:6435b67ad23c | 407 | |
tvendov | 0:6435b67ad23c | 408 | return fsOld->rename(fpOld.fileName(), fpNew.fileName()); |
tvendov | 0:6435b67ad23c | 409 | } |
tvendov | 0:6435b67ad23c | 410 | |
tvendov | 0:6435b67ad23c | 411 | extern "C" char *tmpnam(char *s) { |
tvendov | 0:6435b67ad23c | 412 | return NULL; |
tvendov | 0:6435b67ad23c | 413 | } |
tvendov | 0:6435b67ad23c | 414 | |
tvendov | 0:6435b67ad23c | 415 | extern "C" FILE *tmpfile() { |
tvendov | 0:6435b67ad23c | 416 | return NULL; |
tvendov | 0:6435b67ad23c | 417 | } |
tvendov | 0:6435b67ad23c | 418 | } // namespace std |
tvendov | 0:6435b67ad23c | 419 | |
tvendov | 0:6435b67ad23c | 420 | #ifdef __ARMCC_VERSION |
tvendov | 0:6435b67ad23c | 421 | extern "C" char *_sys_command_string(char *cmd, int len) { |
tvendov | 0:6435b67ad23c | 422 | return NULL; |
tvendov | 0:6435b67ad23c | 423 | } |
tvendov | 0:6435b67ad23c | 424 | #endif |
tvendov | 0:6435b67ad23c | 425 | |
tvendov | 0:6435b67ad23c | 426 | extern "C" DIR *opendir(const char *path) { |
tvendov | 0:6435b67ad23c | 427 | /* root dir is FileSystemLike */ |
tvendov | 0:6435b67ad23c | 428 | if (path[0] == '/' && path[1] == 0) { |
tvendov | 0:6435b67ad23c | 429 | return FileSystemLike::opendir(); |
tvendov | 0:6435b67ad23c | 430 | } |
tvendov | 0:6435b67ad23c | 431 | |
tvendov | 0:6435b67ad23c | 432 | FilePath fp(path); |
tvendov | 0:6435b67ad23c | 433 | FileSystemLike* fs = fp.fileSystem(); |
tvendov | 0:6435b67ad23c | 434 | if (fs == NULL) return NULL; |
tvendov | 0:6435b67ad23c | 435 | |
tvendov | 0:6435b67ad23c | 436 | return fs->opendir(fp.fileName()); |
tvendov | 0:6435b67ad23c | 437 | } |
tvendov | 0:6435b67ad23c | 438 | |
tvendov | 0:6435b67ad23c | 439 | extern "C" struct dirent *readdir(DIR *dir) { |
tvendov | 0:6435b67ad23c | 440 | return dir->readdir(); |
tvendov | 0:6435b67ad23c | 441 | } |
tvendov | 0:6435b67ad23c | 442 | |
tvendov | 0:6435b67ad23c | 443 | extern "C" int closedir(DIR *dir) { |
tvendov | 0:6435b67ad23c | 444 | return dir->closedir(); |
tvendov | 0:6435b67ad23c | 445 | } |
tvendov | 0:6435b67ad23c | 446 | |
tvendov | 0:6435b67ad23c | 447 | extern "C" void rewinddir(DIR *dir) { |
tvendov | 0:6435b67ad23c | 448 | dir->rewinddir(); |
tvendov | 0:6435b67ad23c | 449 | } |
tvendov | 0:6435b67ad23c | 450 | |
tvendov | 0:6435b67ad23c | 451 | extern "C" off_t telldir(DIR *dir) { |
tvendov | 0:6435b67ad23c | 452 | return dir->telldir(); |
tvendov | 0:6435b67ad23c | 453 | } |
tvendov | 0:6435b67ad23c | 454 | |
tvendov | 0:6435b67ad23c | 455 | extern "C" void seekdir(DIR *dir, off_t off) { |
tvendov | 0:6435b67ad23c | 456 | dir->seekdir(off); |
tvendov | 0:6435b67ad23c | 457 | } |
tvendov | 0:6435b67ad23c | 458 | |
tvendov | 0:6435b67ad23c | 459 | extern "C" int mkdir(const char *path, mode_t mode) { |
tvendov | 0:6435b67ad23c | 460 | FilePath fp(path); |
tvendov | 0:6435b67ad23c | 461 | FileSystemLike *fs = fp.fileSystem(); |
tvendov | 0:6435b67ad23c | 462 | if (fs == NULL) return -1; |
tvendov | 0:6435b67ad23c | 463 | |
tvendov | 0:6435b67ad23c | 464 | return fs->mkdir(fp.fileName(), mode); |
tvendov | 0:6435b67ad23c | 465 | } |
tvendov | 0:6435b67ad23c | 466 | |
tvendov | 0:6435b67ad23c | 467 | #if defined(TOOLCHAIN_GCC) |
tvendov | 0:6435b67ad23c | 468 | /* prevents the exception handling name demangling code getting pulled in */ |
tvendov | 0:6435b67ad23c | 469 | #include "mbed_error.h" |
tvendov | 0:6435b67ad23c | 470 | namespace __gnu_cxx { |
tvendov | 0:6435b67ad23c | 471 | void __verbose_terminate_handler() { |
tvendov | 0:6435b67ad23c | 472 | error("Exception"); |
tvendov | 0:6435b67ad23c | 473 | } |
tvendov | 0:6435b67ad23c | 474 | } |
tvendov | 0:6435b67ad23c | 475 | extern "C" WEAK void __cxa_pure_virtual(void); |
tvendov | 0:6435b67ad23c | 476 | extern "C" WEAK void __cxa_pure_virtual(void) { |
tvendov | 0:6435b67ad23c | 477 | exit(1); |
tvendov | 0:6435b67ad23c | 478 | } |
tvendov | 0:6435b67ad23c | 479 | |
tvendov | 0:6435b67ad23c | 480 | #endif |
tvendov | 0:6435b67ad23c | 481 | |
tvendov | 0:6435b67ad23c | 482 | #if defined(TOOLCHAIN_GCC) |
tvendov | 0:6435b67ad23c | 483 | /* uVisor wraps malloc_r, realloc_r and free_r, but not calloc_r! */ |
tvendov | 0:6435b67ad23c | 484 | #ifndef FEATURE_UVISOR |
tvendov | 0:6435b67ad23c | 485 | |
tvendov | 0:6435b67ad23c | 486 | |
tvendov | 0:6435b67ad23c | 487 | #endif/* FEATURE_UVISOR */ |
tvendov | 0:6435b67ad23c | 488 | |
tvendov | 0:6435b67ad23c | 489 | |
tvendov | 0:6435b67ad23c | 490 | extern "C" WEAK void software_init_hook_rtos(void) |
tvendov | 0:6435b67ad23c | 491 | { |
tvendov | 0:6435b67ad23c | 492 | // Do nothing by default. |
tvendov | 0:6435b67ad23c | 493 | } |
tvendov | 0:6435b67ad23c | 494 | |
tvendov | 0:6435b67ad23c | 495 | extern "C" void software_init_hook(void) |
tvendov | 0:6435b67ad23c | 496 | { |
tvendov | 0:6435b67ad23c | 497 | #ifdef FEATURE_UVISOR |
tvendov | 0:6435b67ad23c | 498 | int return_code; |
tvendov | 0:6435b67ad23c | 499 | |
tvendov | 0:6435b67ad23c | 500 | return_code = uvisor_lib_init(); |
tvendov | 0:6435b67ad23c | 501 | if (return_code) { |
tvendov | 0:6435b67ad23c | 502 | mbed_die(); |
tvendov | 0:6435b67ad23c | 503 | } |
tvendov | 0:6435b67ad23c | 504 | #endif/* FEATURE_UVISOR */ |
tvendov | 0:6435b67ad23c | 505 | |
tvendov | 0:6435b67ad23c | 506 | software_init_hook_rtos(); |
tvendov | 0:6435b67ad23c | 507 | } |
tvendov | 0:6435b67ad23c | 508 | #endif |
tvendov | 0:6435b67ad23c | 509 | |
tvendov | 0:6435b67ad23c | 510 | // **************************************************************************** |
tvendov | 0:6435b67ad23c | 511 | // mbed_main is a function that is called before main() |
tvendov | 0:6435b67ad23c | 512 | // mbed_sdk_init() is also a function that is called before main(), but unlike |
tvendov | 0:6435b67ad23c | 513 | // mbed_main(), it is not meant for user code, but for the SDK itself to perform |
tvendov | 0:6435b67ad23c | 514 | // initializations before main() is called. |
tvendov | 0:6435b67ad23c | 515 | |
tvendov | 0:6435b67ad23c | 516 | extern "C" WEAK void mbed_main(void); |
tvendov | 0:6435b67ad23c | 517 | extern "C" WEAK void mbed_main(void) { |
tvendov | 0:6435b67ad23c | 518 | } |
tvendov | 0:6435b67ad23c | 519 | |
tvendov | 0:6435b67ad23c | 520 | extern "C" WEAK void mbed_sdk_init(void); |
tvendov | 0:6435b67ad23c | 521 | extern "C" WEAK void mbed_sdk_init(void) { |
tvendov | 0:6435b67ad23c | 522 | } |
tvendov | 0:6435b67ad23c | 523 | |
tvendov | 0:6435b67ad23c | 524 | #if defined(TOOLCHAIN_ARM) |
tvendov | 0:6435b67ad23c | 525 | extern "C" int $Super$$main(void); |
tvendov | 0:6435b67ad23c | 526 | |
tvendov | 0:6435b67ad23c | 527 | extern "C" int $Sub$$main(void) { |
tvendov | 0:6435b67ad23c | 528 | mbed_sdk_init(); |
tvendov | 0:6435b67ad23c | 529 | mbed_main(); |
tvendov | 0:6435b67ad23c | 530 | return $Super$$main(); |
tvendov | 0:6435b67ad23c | 531 | } |
tvendov | 0:6435b67ad23c | 532 | #elif defined(TOOLCHAIN_GCC) |
tvendov | 0:6435b67ad23c | 533 | extern "C" int __real_main(void); |
tvendov | 0:6435b67ad23c | 534 | |
tvendov | 0:6435b67ad23c | 535 | extern "C" int __wrap_main(void) { |
tvendov | 0:6435b67ad23c | 536 | mbed_sdk_init(); |
tvendov | 0:6435b67ad23c | 537 | mbed_main(); |
tvendov | 0:6435b67ad23c | 538 | return __real_main(); |
tvendov | 0:6435b67ad23c | 539 | } |
tvendov | 0:6435b67ad23c | 540 | #elif defined(TOOLCHAIN_IAR) |
tvendov | 0:6435b67ad23c | 541 | // IAR doesn't have the $Super/$Sub mechanism of armcc, nor something equivalent |
tvendov | 0:6435b67ad23c | 542 | // to ld's --wrap. It does have a --redirect, but that doesn't help, since redirecting |
tvendov | 0:6435b67ad23c | 543 | // 'main' to another symbol looses the original 'main' symbol. However, its startup |
tvendov | 0:6435b67ad23c | 544 | // code will call a function to setup argc and argv (__iar_argc_argv) if it is defined. |
tvendov | 0:6435b67ad23c | 545 | // Since mbed doesn't use argc/argv, we use this function to call our mbed_main. |
tvendov | 0:6435b67ad23c | 546 | extern "C" void __iar_argc_argv() { |
tvendov | 0:6435b67ad23c | 547 | mbed_main(); |
tvendov | 0:6435b67ad23c | 548 | } |
tvendov | 0:6435b67ad23c | 549 | #endif |
tvendov | 0:6435b67ad23c | 550 | |
tvendov | 0:6435b67ad23c | 551 | // Provide implementation of _sbrk (low-level dynamic memory allocation |
tvendov | 0:6435b67ad23c | 552 | // routine) for GCC_ARM which compares new heap pointer with MSP instead of |
tvendov | 0:6435b67ad23c | 553 | // SP. This make it compatible with RTX RTOS thread stacks. |
tvendov | 0:6435b67ad23c | 554 | #if defined(TOOLCHAIN_GCC_ARM) || defined(TOOLCHAIN_GCC_CR) |
tvendov | 0:6435b67ad23c | 555 | // Linker defined symbol used by _sbrk to indicate where heap should start. |
tvendov | 0:6435b67ad23c | 556 | extern "C" int __end__; |
tvendov | 0:6435b67ad23c | 557 | |
tvendov | 0:6435b67ad23c | 558 | #if defined(TARGET_CORTEX_A) |
tvendov | 0:6435b67ad23c | 559 | extern "C" uint32_t __HeapLimit; |
tvendov | 0:6435b67ad23c | 560 | #endif |
tvendov | 0:6435b67ad23c | 561 | |
tvendov | 0:6435b67ad23c | 562 | // Turn off the errno macro and use actual global variable instead. |
tvendov | 0:6435b67ad23c | 563 | #undef errno |
tvendov | 0:6435b67ad23c | 564 | extern "C" int errno; |
tvendov | 0:6435b67ad23c | 565 | |
tvendov | 0:6435b67ad23c | 566 | // For ARM7 only |
tvendov | 0:6435b67ad23c | 567 | register unsigned char * stack_ptr __asm ("sp"); |
tvendov | 0:6435b67ad23c | 568 | |
tvendov | 0:6435b67ad23c | 569 | // Dynamic memory allocation related syscall. |
tvendov | 0:6435b67ad23c | 570 | #if defined(TARGET_NUMAKER_PFM_NUC472) |
tvendov | 0:6435b67ad23c | 571 | // Overwrite _sbrk() to support two region model. |
tvendov | 0:6435b67ad23c | 572 | extern "C" void *__wrap__sbrk(int incr); |
tvendov | 0:6435b67ad23c | 573 | extern "C" caddr_t _sbrk(int incr) { |
tvendov | 0:6435b67ad23c | 574 | return (caddr_t) __wrap__sbrk(incr); |
tvendov | 0:6435b67ad23c | 575 | } |
tvendov | 0:6435b67ad23c | 576 | #else |
tvendov | 0:6435b67ad23c | 577 | extern "C" caddr_t _sbrk(int incr) { |
tvendov | 0:6435b67ad23c | 578 | static unsigned char* heap = (unsigned char*)&__end__; |
tvendov | 0:6435b67ad23c | 579 | unsigned char* prev_heap = heap; |
tvendov | 0:6435b67ad23c | 580 | unsigned char* new_heap = heap + incr; |
tvendov | 0:6435b67ad23c | 581 | |
tvendov | 0:6435b67ad23c | 582 | #if defined(TARGET_ARM7) |
tvendov | 0:6435b67ad23c | 583 | if (new_heap >= stack_ptr) { |
tvendov | 0:6435b67ad23c | 584 | #elif defined(TARGET_CORTEX_A) |
tvendov | 0:6435b67ad23c | 585 | if (new_heap >= (unsigned char*)&__HeapLimit) { /* __HeapLimit is end of heap section */ |
tvendov | 0:6435b67ad23c | 586 | #else |
tvendov | 0:6435b67ad23c | 587 | if (new_heap >= (unsigned char*)__get_MSP()) { |
tvendov | 0:6435b67ad23c | 588 | #endif |
tvendov | 0:6435b67ad23c | 589 | errno = ENOMEM; |
tvendov | 0:6435b67ad23c | 590 | return (caddr_t)-1; |
tvendov | 0:6435b67ad23c | 591 | } |
tvendov | 0:6435b67ad23c | 592 | |
tvendov | 0:6435b67ad23c | 593 | // Additional heap checking if set |
tvendov | 0:6435b67ad23c | 594 | if (mbed_heap_size && (new_heap >= mbed_heap_start + mbed_heap_size)) { |
tvendov | 0:6435b67ad23c | 595 | errno = ENOMEM; |
tvendov | 0:6435b67ad23c | 596 | return (caddr_t)-1; |
tvendov | 0:6435b67ad23c | 597 | } |
tvendov | 0:6435b67ad23c | 598 | |
tvendov | 0:6435b67ad23c | 599 | heap = new_heap; |
tvendov | 0:6435b67ad23c | 600 | return (caddr_t) prev_heap; |
tvendov | 0:6435b67ad23c | 601 | } |
tvendov | 0:6435b67ad23c | 602 | #endif |
tvendov | 0:6435b67ad23c | 603 | #endif |
tvendov | 0:6435b67ad23c | 604 | |
tvendov | 0:6435b67ad23c | 605 | #if defined(TOOLCHAIN_GCC_ARM) || defined(TOOLCHAIN_GCC_CR) |
tvendov | 0:6435b67ad23c | 606 | extern "C" void _exit(int return_code) { |
tvendov | 0:6435b67ad23c | 607 | #else |
tvendov | 0:6435b67ad23c | 608 | namespace std { |
tvendov | 0:6435b67ad23c | 609 | extern "C" void exit(int return_code) { |
tvendov | 0:6435b67ad23c | 610 | #endif |
tvendov | 0:6435b67ad23c | 611 | |
tvendov | 0:6435b67ad23c | 612 | #if DEVICE_STDIO_MESSAGES |
tvendov | 0:6435b67ad23c | 613 | fflush(stdout); |
tvendov | 0:6435b67ad23c | 614 | fflush(stderr); |
tvendov | 0:6435b67ad23c | 615 | #endif |
tvendov | 0:6435b67ad23c | 616 | |
tvendov | 0:6435b67ad23c | 617 | #if DEVICE_SEMIHOST |
tvendov | 0:6435b67ad23c | 618 | if (mbed_interface_connected()) { |
tvendov | 0:6435b67ad23c | 619 | semihost_exit(); |
tvendov | 0:6435b67ad23c | 620 | } |
tvendov | 0:6435b67ad23c | 621 | #endif |
tvendov | 0:6435b67ad23c | 622 | if (return_code) { |
tvendov | 0:6435b67ad23c | 623 | mbed_die(); |
tvendov | 0:6435b67ad23c | 624 | } |
tvendov | 0:6435b67ad23c | 625 | |
tvendov | 0:6435b67ad23c | 626 | while (1); |
tvendov | 0:6435b67ad23c | 627 | } |
tvendov | 0:6435b67ad23c | 628 | |
tvendov | 0:6435b67ad23c | 629 | #if !defined(TOOLCHAIN_GCC_ARM) && !defined(TOOLCHAIN_GCC_CR) |
tvendov | 0:6435b67ad23c | 630 | } //namespace std |
tvendov | 0:6435b67ad23c | 631 | #endif |
tvendov | 0:6435b67ad23c | 632 | |
tvendov | 0:6435b67ad23c | 633 | |
tvendov | 0:6435b67ad23c | 634 | namespace mbed { |
tvendov | 0:6435b67ad23c | 635 | |
tvendov | 0:6435b67ad23c | 636 | void mbed_set_unbuffered_stream(FILE *_file) { |
tvendov | 0:6435b67ad23c | 637 | #if defined (__ICCARM__) |
tvendov | 0:6435b67ad23c | 638 | char buf[2]; |
tvendov | 0:6435b67ad23c | 639 | std::setvbuf(_file,buf,_IONBF,NULL); |
tvendov | 0:6435b67ad23c | 640 | #else |
tvendov | 0:6435b67ad23c | 641 | setbuf(_file, NULL); |
tvendov | 0:6435b67ad23c | 642 | #endif |
tvendov | 0:6435b67ad23c | 643 | } |
tvendov | 0:6435b67ad23c | 644 | |
tvendov | 0:6435b67ad23c | 645 | int mbed_getc(FILE *_file){ |
tvendov | 0:6435b67ad23c | 646 | #if defined (__ICCARM__) |
tvendov | 0:6435b67ad23c | 647 | /*This is only valid for unbuffered streams*/ |
tvendov | 0:6435b67ad23c | 648 | int res = std::fgetc(_file); |
tvendov | 0:6435b67ad23c | 649 | if (res>=0){ |
tvendov | 0:6435b67ad23c | 650 | _file->_Mode = (unsigned short)(_file->_Mode & ~ 0x1000);/* Unset read mode */ |
tvendov | 0:6435b67ad23c | 651 | _file->_Rend = _file->_Wend; |
tvendov | 0:6435b67ad23c | 652 | _file->_Next = _file->_Wend; |
tvendov | 0:6435b67ad23c | 653 | } |
tvendov | 0:6435b67ad23c | 654 | return res; |
tvendov | 0:6435b67ad23c | 655 | #else |
tvendov | 0:6435b67ad23c | 656 | return std::fgetc(_file); |
tvendov | 0:6435b67ad23c | 657 | #endif |
tvendov | 0:6435b67ad23c | 658 | } |
tvendov | 0:6435b67ad23c | 659 | |
tvendov | 0:6435b67ad23c | 660 | char* mbed_gets(char*s, int size, FILE *_file){ |
tvendov | 0:6435b67ad23c | 661 | #if defined (__ICCARM__) |
tvendov | 0:6435b67ad23c | 662 | /*This is only valid for unbuffered streams*/ |
tvendov | 0:6435b67ad23c | 663 | char *str = fgets(s,size,_file); |
tvendov | 0:6435b67ad23c | 664 | if (str!=NULL){ |
tvendov | 0:6435b67ad23c | 665 | _file->_Mode = (unsigned short)(_file->_Mode & ~ 0x1000);/* Unset read mode */ |
tvendov | 0:6435b67ad23c | 666 | _file->_Rend = _file->_Wend; |
tvendov | 0:6435b67ad23c | 667 | _file->_Next = _file->_Wend; |
tvendov | 0:6435b67ad23c | 668 | } |
tvendov | 0:6435b67ad23c | 669 | return str; |
tvendov | 0:6435b67ad23c | 670 | #else |
tvendov | 0:6435b67ad23c | 671 | return std::fgets(s,size,_file); |
tvendov | 0:6435b67ad23c | 672 | #endif |
tvendov | 0:6435b67ad23c | 673 | } |
tvendov | 0:6435b67ad23c | 674 | |
tvendov | 0:6435b67ad23c | 675 | } // namespace mbed |
tvendov | 0:6435b67ad23c | 676 | |
tvendov | 0:6435b67ad23c | 677 | #if defined (__ICCARM__) |
tvendov | 0:6435b67ad23c | 678 | // Stub out locks when an rtos is not present |
tvendov | 0:6435b67ad23c | 679 | extern "C" WEAK void __iar_system_Mtxinit(__iar_Rmtx *mutex) {} |
tvendov | 0:6435b67ad23c | 680 | extern "C" WEAK void __iar_system_Mtxdst(__iar_Rmtx *mutex) {} |
tvendov | 0:6435b67ad23c | 681 | extern "C" WEAK void __iar_system_Mtxlock(__iar_Rmtx *mutex) {} |
tvendov | 0:6435b67ad23c | 682 | extern "C" WEAK void __iar_system_Mtxunlock(__iar_Rmtx *mutex) {} |
tvendov | 0:6435b67ad23c | 683 | extern "C" WEAK void __iar_file_Mtxinit(__iar_Rmtx *mutex) {} |
tvendov | 0:6435b67ad23c | 684 | extern "C" WEAK void __iar_file_Mtxdst(__iar_Rmtx *mutex) {} |
tvendov | 0:6435b67ad23c | 685 | extern "C" WEAK void __iar_file_Mtxlock(__iar_Rmtx *mutex) {} |
tvendov | 0:6435b67ad23c | 686 | extern "C" WEAK void __iar_file_Mtxunlock(__iar_Rmtx *mutex) {} |
tvendov | 0:6435b67ad23c | 687 | #elif defined(__CC_ARM) |
tvendov | 0:6435b67ad23c | 688 | // Do nothing |
tvendov | 0:6435b67ad23c | 689 | #elif defined (__GNUC__) |
tvendov | 0:6435b67ad23c | 690 | struct _reent; |
tvendov | 0:6435b67ad23c | 691 | // Stub out locks when an rtos is not present |
tvendov | 0:6435b67ad23c | 692 | extern "C" WEAK void __rtos_malloc_lock( struct _reent *_r ) {} |
tvendov | 0:6435b67ad23c | 693 | extern "C" WEAK void __rtos_malloc_unlock( struct _reent *_r ) {} |
tvendov | 0:6435b67ad23c | 694 | extern "C" WEAK void __rtos_env_lock( struct _reent *_r ) {} |
tvendov | 0:6435b67ad23c | 695 | extern "C" WEAK void __rtos_env_unlock( struct _reent *_r ) {} |
tvendov | 0:6435b67ad23c | 696 | |
tvendov | 0:6435b67ad23c | 697 | extern "C" void __malloc_lock( struct _reent *_r ) |
tvendov | 0:6435b67ad23c | 698 | { |
tvendov | 0:6435b67ad23c | 699 | __rtos_malloc_lock(_r); |
tvendov | 0:6435b67ad23c | 700 | } |
tvendov | 0:6435b67ad23c | 701 | |
tvendov | 0:6435b67ad23c | 702 | extern "C" void __malloc_unlock( struct _reent *_r ) |
tvendov | 0:6435b67ad23c | 703 | { |
tvendov | 0:6435b67ad23c | 704 | __rtos_malloc_unlock(_r); |
tvendov | 0:6435b67ad23c | 705 | } |
tvendov | 0:6435b67ad23c | 706 | |
tvendov | 0:6435b67ad23c | 707 | extern "C" void __env_lock( struct _reent *_r ) |
tvendov | 0:6435b67ad23c | 708 | { |
tvendov | 0:6435b67ad23c | 709 | __rtos_env_lock(_r); |
tvendov | 0:6435b67ad23c | 710 | } |
tvendov | 0:6435b67ad23c | 711 | |
tvendov | 0:6435b67ad23c | 712 | extern "C" void __env_unlock( struct _reent *_r ) |
tvendov | 0:6435b67ad23c | 713 | { |
tvendov | 0:6435b67ad23c | 714 | __rtos_env_unlock(_r); |
tvendov | 0:6435b67ad23c | 715 | } |
tvendov | 0:6435b67ad23c | 716 | #endif |
tvendov | 0:6435b67ad23c | 717 | |
tvendov | 0:6435b67ad23c | 718 | void *operator new(std::size_t count) |
tvendov | 0:6435b67ad23c | 719 | { |
tvendov | 0:6435b67ad23c | 720 | void *buffer = malloc(count); |
tvendov | 0:6435b67ad23c | 721 | if (NULL == buffer) { |
tvendov | 0:6435b67ad23c | 722 | error("Operator new out of memory\r\n"); |
tvendov | 0:6435b67ad23c | 723 | } |
tvendov | 0:6435b67ad23c | 724 | return buffer; |
tvendov | 0:6435b67ad23c | 725 | } |
tvendov | 0:6435b67ad23c | 726 | |
tvendov | 0:6435b67ad23c | 727 | void *operator new[](std::size_t count) |
tvendov | 0:6435b67ad23c | 728 | { |
tvendov | 0:6435b67ad23c | 729 | void *buffer = malloc(count); |
tvendov | 0:6435b67ad23c | 730 | if (NULL == buffer) { |
tvendov | 0:6435b67ad23c | 731 | error("Operator new[] out of memory\r\n"); |
tvendov | 0:6435b67ad23c | 732 | } |
tvendov | 0:6435b67ad23c | 733 | return buffer; |
tvendov | 0:6435b67ad23c | 734 | } |
tvendov | 0:6435b67ad23c | 735 | |
tvendov | 0:6435b67ad23c | 736 | void operator delete(void *ptr) |
tvendov | 0:6435b67ad23c | 737 | { |
tvendov | 0:6435b67ad23c | 738 | if (ptr != NULL) { |
tvendov | 0:6435b67ad23c | 739 | free(ptr); |
tvendov | 0:6435b67ad23c | 740 | } |
tvendov | 0:6435b67ad23c | 741 | } |
tvendov | 0:6435b67ad23c | 742 | void operator delete[](void *ptr) |
tvendov | 0:6435b67ad23c | 743 | { |
tvendov | 0:6435b67ad23c | 744 | if (ptr != NULL) { |
tvendov | 0:6435b67ad23c | 745 | free(ptr); |
tvendov | 0:6435b67ad23c | 746 | } |
tvendov | 0:6435b67ad23c | 747 | } |
tvendov | 0:6435b67ad23c | 748 |