alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 1 | /* |
Kshitij Sisodia | d5679cc | 2022-03-03 16:30:07 +0000 | [diff] [blame^] | 2 | * Copyright (c) 2022 Arm Limited. All rights reserved. |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 3 | * SPDX-License-Identifier: Apache-2.0 |
| 4 | * |
| 5 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | * you may not use this file except in compliance with the License. |
| 7 | * You may obtain a copy of the License at |
| 8 | * |
| 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | * |
| 11 | * Unless required by applicable law or agreed to in writing, software |
| 12 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | * See the License for the specific language governing permissions and |
| 15 | * limitations under the License. |
| 16 | */ |
Kshitij Sisodia | acc6b85 | 2022-03-01 10:23:11 +0000 | [diff] [blame] | 17 | #if !defined(USE_SEMIHOSTING) |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 18 | |
| 19 | #include "uart_stdout.h" |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 20 | |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 21 | #include <stdio.h> |
| 22 | #include <string.h> |
| 23 | #include <time.h> |
Kshitij Sisodia | f9c19ea | 2021-05-07 16:08:14 +0100 | [diff] [blame] | 24 | |
| 25 | #if defined(__ARMCC_VERSION) && (__ARMCC_VERSION >= 6100100) |
| 26 | /* Arm compiler re-targeting */ |
| 27 | |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 28 | #include <rt_misc.h> |
| 29 | #include <rt_sys.h> |
| 30 | |
| 31 | |
| 32 | /* Standard IO device handles. */ |
Kshitij Sisodia | f9c19ea | 2021-05-07 16:08:14 +0100 | [diff] [blame] | 33 | #define STDIN 0x8001 |
| 34 | #define STDOUT 0x8002 |
| 35 | #define STDERR 0x8003 |
| 36 | |
| 37 | #define RETARGET(fun) _sys##fun |
| 38 | |
| 39 | #else |
| 40 | /* GNU compiler re-targeting */ |
| 41 | |
| 42 | /* |
| 43 | * This type is used by the _ I/O functions to denote an open |
| 44 | * file. |
| 45 | */ |
| 46 | typedef int FILEHANDLE; |
| 47 | |
| 48 | /* |
| 49 | * Open a file. May return -1 if the file failed to open. |
| 50 | */ |
| 51 | extern FILEHANDLE _open(const char * /*name*/, int /*openmode*/); |
| 52 | |
| 53 | /* Standard IO device handles. */ |
| 54 | #define STDIN 0x00 |
| 55 | #define STDOUT 0x01 |
| 56 | #define STDERR 0x02 |
| 57 | |
| 58 | #define RETARGET(fun) fun |
| 59 | |
| 60 | #endif |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 61 | |
| 62 | /* Standard IO device name defines. */ |
Kshitij Sisodia | f9c19ea | 2021-05-07 16:08:14 +0100 | [diff] [blame] | 63 | const char __stdin_name[] __attribute__((aligned(4))) = "STDIN"; |
| 64 | const char __stdout_name[] __attribute__((aligned(4))) = "STDOUT"; |
| 65 | const char __stderr_name[] __attribute__((aligned(4))) = "STDERR"; |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 66 | |
Kshitij Sisodia | d5679cc | 2022-03-03 16:30:07 +0000 | [diff] [blame^] | 67 | __attribute__((noreturn)) static void UartEndSimulation(int code) |
| 68 | { |
| 69 | UartPutc((char) 0x4); // End of simulation |
| 70 | UartPutc((char) code); // Exit code |
| 71 | while(1); |
| 72 | } |
| 73 | |
Kshitij Sisodia | f9c19ea | 2021-05-07 16:08:14 +0100 | [diff] [blame] | 74 | void _ttywrch(int ch) { |
| 75 | (void)fputc(ch, stdout); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 76 | } |
| 77 | |
Kshitij Sisodia | f9c19ea | 2021-05-07 16:08:14 +0100 | [diff] [blame] | 78 | FILEHANDLE RETARGET(_open)(const char *name, int openmode) |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 79 | { |
alexander | 31ae9f0 | 2022-02-10 16:15:54 +0000 | [diff] [blame] | 80 | (void)(openmode); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 81 | |
Kshitij Sisodia | f9c19ea | 2021-05-07 16:08:14 +0100 | [diff] [blame] | 82 | if (strcmp(name, __stdin_name) == 0) { |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 83 | return (STDIN); |
| 84 | } |
Kshitij Sisodia | f9c19ea | 2021-05-07 16:08:14 +0100 | [diff] [blame] | 85 | |
| 86 | if (strcmp(name, __stdout_name) == 0) { |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 87 | return (STDOUT); |
| 88 | } |
Kshitij Sisodia | f9c19ea | 2021-05-07 16:08:14 +0100 | [diff] [blame] | 89 | |
| 90 | if (strcmp(name, __stderr_name) == 0) { |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 91 | return (STDERR); |
| 92 | } |
Kshitij Sisodia | f9c19ea | 2021-05-07 16:08:14 +0100 | [diff] [blame] | 93 | |
| 94 | return -1; |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 95 | } |
| 96 | |
Kshitij Sisodia | f9c19ea | 2021-05-07 16:08:14 +0100 | [diff] [blame] | 97 | int RETARGET(_write)(FILEHANDLE fh, const unsigned char *buf, unsigned int len, int mode) |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 98 | { |
alexander | 31ae9f0 | 2022-02-10 16:15:54 +0000 | [diff] [blame] | 99 | (void)(mode); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 100 | |
Kshitij Sisodia | f9c19ea | 2021-05-07 16:08:14 +0100 | [diff] [blame] | 101 | switch (fh) { |
| 102 | case STDOUT: |
| 103 | case STDERR: { |
| 104 | int c; |
| 105 | |
| 106 | while (len-- > 0) { |
| 107 | c = fputc(*buf++, stdout); |
| 108 | if (c == EOF) { |
| 109 | return EOF; |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | return 0; |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 114 | } |
Kshitij Sisodia | f9c19ea | 2021-05-07 16:08:14 +0100 | [diff] [blame] | 115 | default: |
| 116 | return EOF; |
| 117 | } |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 118 | } |
| 119 | |
Kshitij Sisodia | f9c19ea | 2021-05-07 16:08:14 +0100 | [diff] [blame] | 120 | int RETARGET(_read)(FILEHANDLE fh, unsigned char *buf, unsigned int len, int mode) |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 121 | { |
alexander | 31ae9f0 | 2022-02-10 16:15:54 +0000 | [diff] [blame] | 122 | (void)(mode); |
Kshitij Sisodia | f9c19ea | 2021-05-07 16:08:14 +0100 | [diff] [blame] | 123 | |
| 124 | switch (fh) { |
| 125 | case STDIN: { |
| 126 | int c; |
| 127 | |
| 128 | while (len-- > 0) { |
| 129 | c = fgetc(stdin); |
| 130 | if (c == EOF) { |
| 131 | return EOF; |
| 132 | } |
| 133 | |
| 134 | *buf++ = (unsigned char)c; |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 135 | } |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 136 | |
Kshitij Sisodia | f9c19ea | 2021-05-07 16:08:14 +0100 | [diff] [blame] | 137 | return 0; |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 138 | } |
Kshitij Sisodia | f9c19ea | 2021-05-07 16:08:14 +0100 | [diff] [blame] | 139 | default: |
| 140 | return EOF; |
| 141 | } |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 142 | } |
| 143 | |
Kshitij Sisodia | f9c19ea | 2021-05-07 16:08:14 +0100 | [diff] [blame] | 144 | int RETARGET(_istty)(FILEHANDLE fh) |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 145 | { |
Kshitij Sisodia | f9c19ea | 2021-05-07 16:08:14 +0100 | [diff] [blame] | 146 | switch (fh) { |
| 147 | case STDIN: |
| 148 | case STDOUT: |
| 149 | case STDERR: |
| 150 | return 1; |
| 151 | default: |
| 152 | return 0; |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 153 | } |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 154 | } |
| 155 | |
Kshitij Sisodia | f9c19ea | 2021-05-07 16:08:14 +0100 | [diff] [blame] | 156 | int RETARGET(_close)(FILEHANDLE fh) |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 157 | { |
Kshitij Sisodia | f9c19ea | 2021-05-07 16:08:14 +0100 | [diff] [blame] | 158 | if (RETARGET(_istty(fh))) { |
| 159 | return 0; |
| 160 | } |
| 161 | |
| 162 | return -1; |
| 163 | } |
| 164 | |
| 165 | int RETARGET(_seek)(FILEHANDLE fh, long pos) |
| 166 | { |
alexander | 31ae9f0 | 2022-02-10 16:15:54 +0000 | [diff] [blame] | 167 | (void)(fh); |
| 168 | (void)(pos); |
Kshitij Sisodia | f9c19ea | 2021-05-07 16:08:14 +0100 | [diff] [blame] | 169 | |
| 170 | return -1; |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 171 | } |
| 172 | |
Kshitij Sisodia | f9c19ea | 2021-05-07 16:08:14 +0100 | [diff] [blame] | 173 | int RETARGET(_ensure)(FILEHANDLE fh) |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 174 | { |
alexander | 31ae9f0 | 2022-02-10 16:15:54 +0000 | [diff] [blame] | 175 | (void)(fh); |
Kshitij Sisodia | f9c19ea | 2021-05-07 16:08:14 +0100 | [diff] [blame] | 176 | |
| 177 | return -1; |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 178 | } |
| 179 | |
Kshitij Sisodia | f9c19ea | 2021-05-07 16:08:14 +0100 | [diff] [blame] | 180 | long RETARGET(_flen)(FILEHANDLE fh) |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 181 | { |
Kshitij Sisodia | f9c19ea | 2021-05-07 16:08:14 +0100 | [diff] [blame] | 182 | if (RETARGET(_istty)(fh)) { |
| 183 | return 0; |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 184 | } |
Kshitij Sisodia | f9c19ea | 2021-05-07 16:08:14 +0100 | [diff] [blame] | 185 | |
| 186 | return -1; |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 187 | } |
| 188 | |
Kshitij Sisodia | f9c19ea | 2021-05-07 16:08:14 +0100 | [diff] [blame] | 189 | int RETARGET(_tmpnam)(char *name, int sig, unsigned int maxlen) |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 190 | { |
alexander | 31ae9f0 | 2022-02-10 16:15:54 +0000 | [diff] [blame] | 191 | (void)(name); |
| 192 | (void)(sig); |
| 193 | (void)(maxlen); |
Kshitij Sisodia | f9c19ea | 2021-05-07 16:08:14 +0100 | [diff] [blame] | 194 | |
| 195 | return 1; |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 196 | } |
| 197 | |
Kshitij Sisodia | f9c19ea | 2021-05-07 16:08:14 +0100 | [diff] [blame] | 198 | char *RETARGET(_command_string)(char *cmd, int len) |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 199 | { |
alexander | 31ae9f0 | 2022-02-10 16:15:54 +0000 | [diff] [blame] | 200 | (void)(len); |
Kshitij Sisodia | f9c19ea | 2021-05-07 16:08:14 +0100 | [diff] [blame] | 201 | |
| 202 | return cmd; |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 203 | } |
| 204 | |
Kshitij Sisodia | f9c19ea | 2021-05-07 16:08:14 +0100 | [diff] [blame] | 205 | void RETARGET(_exit)(int return_code) |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 206 | { |
| 207 | UartEndSimulation(return_code); |
alexander | 31ae9f0 | 2022-02-10 16:15:54 +0000 | [diff] [blame] | 208 | while(1); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 209 | } |
| 210 | |
| 211 | int system(const char *cmd) |
| 212 | { |
alexander | 31ae9f0 | 2022-02-10 16:15:54 +0000 | [diff] [blame] | 213 | (void)(cmd); |
Kshitij Sisodia | f9c19ea | 2021-05-07 16:08:14 +0100 | [diff] [blame] | 214 | |
| 215 | return 0; |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 216 | } |
| 217 | |
| 218 | time_t time(time_t *timer) |
| 219 | { |
| 220 | time_t current; |
| 221 | |
Kshitij Sisodia | f9c19ea | 2021-05-07 16:08:14 +0100 | [diff] [blame] | 222 | current = 0; // To Do !! No RTC implemented |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 223 | |
| 224 | if (timer != NULL) { |
| 225 | *timer = current; |
| 226 | } |
| 227 | |
Kshitij Sisodia | f9c19ea | 2021-05-07 16:08:14 +0100 | [diff] [blame] | 228 | return current; |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 229 | } |
| 230 | |
Kshitij Sisodia | f9c19ea | 2021-05-07 16:08:14 +0100 | [diff] [blame] | 231 | void _clock_init(void) {} |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 232 | |
Kshitij Sisodia | f9c19ea | 2021-05-07 16:08:14 +0100 | [diff] [blame] | 233 | clock_t clock(void) |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 234 | { |
Kshitij Sisodia | f9c19ea | 2021-05-07 16:08:14 +0100 | [diff] [blame] | 235 | return (clock_t)-1; |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 236 | } |
| 237 | |
Kshitij Sisodia | f9c19ea | 2021-05-07 16:08:14 +0100 | [diff] [blame] | 238 | int remove(const char *arg) { |
alexander | 31ae9f0 | 2022-02-10 16:15:54 +0000 | [diff] [blame] | 239 | (void)(arg); |
Kshitij Sisodia | f9c19ea | 2021-05-07 16:08:14 +0100 | [diff] [blame] | 240 | |
| 241 | return 0; |
| 242 | } |
| 243 | |
| 244 | int rename(const char *oldn, const char *newn) |
| 245 | { |
alexander | 31ae9f0 | 2022-02-10 16:15:54 +0000 | [diff] [blame] | 246 | (void)(oldn); |
| 247 | (void)(newn); |
Kshitij Sisodia | f9c19ea | 2021-05-07 16:08:14 +0100 | [diff] [blame] | 248 | |
| 249 | return 0; |
| 250 | } |
| 251 | |
| 252 | int fputc(int ch, FILE *f) |
| 253 | { |
alexander | 31ae9f0 | 2022-02-10 16:15:54 +0000 | [diff] [blame] | 254 | (void)(f); |
Kshitij Sisodia | f9c19ea | 2021-05-07 16:08:14 +0100 | [diff] [blame] | 255 | |
| 256 | return UartPutc(ch); |
| 257 | } |
| 258 | |
| 259 | int fgetc(FILE *f) |
| 260 | { |
alexander | 31ae9f0 | 2022-02-10 16:15:54 +0000 | [diff] [blame] | 261 | (void)(f); |
Kshitij Sisodia | f9c19ea | 2021-05-07 16:08:14 +0100 | [diff] [blame] | 262 | |
| 263 | return UartPutc(UartGetc()); |
| 264 | } |
| 265 | |
| 266 | #ifndef ferror |
| 267 | |
| 268 | /* arm-none-eabi-gcc with newlib uses a define for ferror */ |
| 269 | int ferror(FILE *f) |
| 270 | { |
alexander | 31ae9f0 | 2022-02-10 16:15:54 +0000 | [diff] [blame] | 271 | (void)(f); |
Kshitij Sisodia | f9c19ea | 2021-05-07 16:08:14 +0100 | [diff] [blame] | 272 | |
| 273 | return EOF; |
| 274 | } |
| 275 | |
| 276 | #endif /* #ifndef ferror */ |
Kshitij Sisodia | acc6b85 | 2022-03-01 10:23:11 +0000 | [diff] [blame] | 277 | |
| 278 | #endif /* !defined(USE_SEMIHOSTING) */ |