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