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" |
| 19 | #include "bsp_core_log.h" |
| 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 | f9c19ea | 2021-05-07 16:08:14 +0100 | [diff] [blame] | 67 | void _ttywrch(int ch) { |
| 68 | (void)fputc(ch, stdout); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 69 | } |
| 70 | |
Kshitij Sisodia | f9c19ea | 2021-05-07 16:08:14 +0100 | [diff] [blame] | 71 | FILEHANDLE RETARGET(_open)(const char *name, int openmode) |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 72 | { |
| 73 | UNUSED(openmode); |
| 74 | |
Kshitij Sisodia | f9c19ea | 2021-05-07 16:08:14 +0100 | [diff] [blame] | 75 | if (strcmp(name, __stdin_name) == 0) { |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 76 | return (STDIN); |
| 77 | } |
Kshitij Sisodia | f9c19ea | 2021-05-07 16:08:14 +0100 | [diff] [blame] | 78 | |
| 79 | if (strcmp(name, __stdout_name) == 0) { |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 80 | return (STDOUT); |
| 81 | } |
Kshitij Sisodia | f9c19ea | 2021-05-07 16:08:14 +0100 | [diff] [blame] | 82 | |
| 83 | if (strcmp(name, __stderr_name) == 0) { |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 84 | return (STDERR); |
| 85 | } |
Kshitij Sisodia | f9c19ea | 2021-05-07 16:08:14 +0100 | [diff] [blame] | 86 | |
| 87 | return -1; |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 88 | } |
| 89 | |
Kshitij Sisodia | f9c19ea | 2021-05-07 16:08:14 +0100 | [diff] [blame] | 90 | 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] | 91 | { |
| 92 | UNUSED(mode); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 93 | |
Kshitij Sisodia | f9c19ea | 2021-05-07 16:08:14 +0100 | [diff] [blame] | 94 | switch (fh) { |
| 95 | case STDOUT: |
| 96 | case STDERR: { |
| 97 | int c; |
| 98 | |
| 99 | while (len-- > 0) { |
| 100 | c = fputc(*buf++, stdout); |
| 101 | if (c == EOF) { |
| 102 | return EOF; |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | return 0; |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 107 | } |
Kshitij Sisodia | f9c19ea | 2021-05-07 16:08:14 +0100 | [diff] [blame] | 108 | default: |
| 109 | return EOF; |
| 110 | } |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 111 | } |
| 112 | |
Kshitij Sisodia | f9c19ea | 2021-05-07 16:08:14 +0100 | [diff] [blame] | 113 | int RETARGET(_read)(FILEHANDLE fh, unsigned char *buf, unsigned int len, int mode) |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 114 | { |
| 115 | UNUSED(mode); |
Kshitij Sisodia | f9c19ea | 2021-05-07 16:08:14 +0100 | [diff] [blame] | 116 | |
| 117 | switch (fh) { |
| 118 | case STDIN: { |
| 119 | int c; |
| 120 | |
| 121 | while (len-- > 0) { |
| 122 | c = fgetc(stdin); |
| 123 | if (c == EOF) { |
| 124 | return EOF; |
| 125 | } |
| 126 | |
| 127 | *buf++ = (unsigned char)c; |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 128 | } |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 129 | |
Kshitij Sisodia | f9c19ea | 2021-05-07 16:08:14 +0100 | [diff] [blame] | 130 | return 0; |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 131 | } |
Kshitij Sisodia | f9c19ea | 2021-05-07 16:08:14 +0100 | [diff] [blame] | 132 | default: |
| 133 | return EOF; |
| 134 | } |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 135 | } |
| 136 | |
Kshitij Sisodia | f9c19ea | 2021-05-07 16:08:14 +0100 | [diff] [blame] | 137 | int RETARGET(_istty)(FILEHANDLE fh) |
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 | switch (fh) { |
| 140 | case STDIN: |
| 141 | case STDOUT: |
| 142 | case STDERR: |
| 143 | return 1; |
| 144 | default: |
| 145 | return 0; |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 146 | } |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 147 | } |
| 148 | |
Kshitij Sisodia | f9c19ea | 2021-05-07 16:08:14 +0100 | [diff] [blame] | 149 | int RETARGET(_close)(FILEHANDLE fh) |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 150 | { |
Kshitij Sisodia | f9c19ea | 2021-05-07 16:08:14 +0100 | [diff] [blame] | 151 | if (RETARGET(_istty(fh))) { |
| 152 | return 0; |
| 153 | } |
| 154 | |
| 155 | return -1; |
| 156 | } |
| 157 | |
| 158 | int RETARGET(_seek)(FILEHANDLE fh, long pos) |
| 159 | { |
| 160 | UNUSED(fh); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 161 | UNUSED(pos); |
Kshitij Sisodia | f9c19ea | 2021-05-07 16:08:14 +0100 | [diff] [blame] | 162 | |
| 163 | return -1; |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 164 | } |
| 165 | |
Kshitij Sisodia | f9c19ea | 2021-05-07 16:08:14 +0100 | [diff] [blame] | 166 | int RETARGET(_ensure)(FILEHANDLE fh) |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 167 | { |
Kshitij Sisodia | f9c19ea | 2021-05-07 16:08:14 +0100 | [diff] [blame] | 168 | UNUSED(fh); |
| 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 | long RETARGET(_flen)(FILEHANDLE fh) |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 174 | { |
Kshitij Sisodia | f9c19ea | 2021-05-07 16:08:14 +0100 | [diff] [blame] | 175 | if (RETARGET(_istty)(fh)) { |
| 176 | return 0; |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 177 | } |
Kshitij Sisodia | f9c19ea | 2021-05-07 16:08:14 +0100 | [diff] [blame] | 178 | |
| 179 | return -1; |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 180 | } |
| 181 | |
Kshitij Sisodia | f9c19ea | 2021-05-07 16:08:14 +0100 | [diff] [blame] | 182 | int RETARGET(_tmpnam)(char *name, int sig, unsigned int maxlen) |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 183 | { |
| 184 | UNUSED(name); |
| 185 | UNUSED(sig); |
| 186 | UNUSED(maxlen); |
Kshitij Sisodia | f9c19ea | 2021-05-07 16:08:14 +0100 | [diff] [blame] | 187 | |
| 188 | return 1; |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 189 | } |
| 190 | |
Kshitij Sisodia | f9c19ea | 2021-05-07 16:08:14 +0100 | [diff] [blame] | 191 | char *RETARGET(_command_string)(char *cmd, int len) |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 192 | { |
| 193 | UNUSED(len); |
Kshitij Sisodia | f9c19ea | 2021-05-07 16:08:14 +0100 | [diff] [blame] | 194 | |
| 195 | return cmd; |
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 | void RETARGET(_exit)(int return_code) |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 199 | { |
| 200 | UartEndSimulation(return_code); |
| 201 | } |
| 202 | |
| 203 | int system(const char *cmd) |
| 204 | { |
| 205 | UNUSED(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) { |
| 231 | UNUSED(arg); |
| 232 | |
| 233 | return 0; |
| 234 | } |
| 235 | |
| 236 | int rename(const char *oldn, const char *newn) |
| 237 | { |
| 238 | UNUSED(oldn); |
| 239 | UNUSED(newn); |
| 240 | |
| 241 | return 0; |
| 242 | } |
| 243 | |
| 244 | int fputc(int ch, FILE *f) |
| 245 | { |
| 246 | UNUSED(f); |
| 247 | |
| 248 | return UartPutc(ch); |
| 249 | } |
| 250 | |
| 251 | int fgetc(FILE *f) |
| 252 | { |
| 253 | UNUSED(f); |
| 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 | { |
| 263 | UNUSED(f); |
| 264 | |
| 265 | return EOF; |
| 266 | } |
| 267 | |
| 268 | #endif /* #ifndef ferror */ |