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 | */ |
| 17 | #if defined(__ARMCC_VERSION) && (__ARMCC_VERSION >= 6010050) |
| 18 | |
| 19 | #include "uart_stdout.h" |
| 20 | #include "bsp_core_log.h" |
| 21 | |
| 22 | #if defined (MPS3_PLATFORM) |
| 23 | #include "smm_mps3.h" |
| 24 | #endif /* MPS3_PLATFORM */ |
| 25 | |
| 26 | #include <stdio.h> |
| 27 | #include <string.h> |
| 28 | #include <time.h> |
| 29 | #include <rt_misc.h> |
| 30 | #include <rt_sys.h> |
| 31 | |
| 32 | |
| 33 | /* Standard IO device handles. */ |
| 34 | #define STDIN 0x8001 |
| 35 | #define STDOUT 0x8002 |
| 36 | #define STDERR 0x8003 |
| 37 | |
| 38 | /* Standard IO device name defines. */ |
| 39 | const char __stdin_name[] = "STDIN"; |
| 40 | const char __stdout_name[] = "STDOUT"; |
| 41 | const char __stderr_name[] = "STDERR"; |
| 42 | |
| 43 | int fputc(int ch, FILE *f) |
| 44 | { |
| 45 | UNUSED(f); |
| 46 | return (UartPutc(ch)); |
| 47 | } |
| 48 | |
| 49 | int fgetc(FILE *f) |
| 50 | { |
| 51 | UNUSED(f); |
| 52 | return (UartPutc(UartGetc())); |
| 53 | } |
| 54 | |
| 55 | int ferror(FILE *f) |
| 56 | { |
| 57 | UNUSED(f); |
| 58 | /* Your implementation of ferror */ |
| 59 | return EOF; |
| 60 | } |
| 61 | |
| 62 | void _ttywrch(int ch) |
| 63 | { |
| 64 | UartPutc(ch); |
| 65 | } |
| 66 | |
| 67 | FILEHANDLE _sys_open(const char *name, int openmode) |
| 68 | { |
| 69 | UNUSED(openmode); |
| 70 | |
| 71 | /* Register standard Input Output devices. */ |
| 72 | if (strcmp(name, "STDIN") == 0) |
| 73 | { |
| 74 | return (STDIN); |
| 75 | } |
| 76 | if (strcmp(name, "STDOUT") == 0) |
| 77 | { |
| 78 | return (STDOUT); |
| 79 | } |
| 80 | if (strcmp(name, "STDERR") == 0) |
| 81 | { |
| 82 | return (STDERR); |
| 83 | } |
| 84 | return (-1); |
| 85 | } |
| 86 | |
| 87 | int _sys_close(FILEHANDLE fh) |
| 88 | { |
| 89 | if (fh > 0x8000) |
| 90 | { |
| 91 | return (0); |
| 92 | } |
| 93 | return (-1); |
| 94 | } |
| 95 | |
| 96 | int _sys_write(FILEHANDLE fh, const unsigned char *buf, unsigned int len, int mode) |
| 97 | { |
| 98 | UNUSED(mode); |
| 99 | if (fh == STDOUT || fh == STDERR ) |
| 100 | { |
| 101 | /* Standard Output device. */ |
| 102 | for (; len; len--) |
| 103 | { |
| 104 | UartPutc(*buf++); |
| 105 | } |
| 106 | return (0); |
| 107 | } |
| 108 | |
| 109 | if (fh > 0x8000) |
| 110 | { |
| 111 | return (-1); |
| 112 | } |
| 113 | return (-1); |
| 114 | } |
| 115 | |
| 116 | int _sys_read(FILEHANDLE fh, unsigned char *buf, unsigned int len, int mode) |
| 117 | { |
| 118 | UNUSED(mode); |
| 119 | if (fh == STDIN) |
| 120 | { |
| 121 | /* Standard Input device. */ |
| 122 | for (; len; len--) |
| 123 | { |
| 124 | *buf++ = UartGetc(); |
| 125 | } |
| 126 | return (0); |
| 127 | } |
| 128 | |
| 129 | if (fh > 0x8000) |
| 130 | { |
| 131 | return (-1); |
| 132 | } |
| 133 | return (-1); |
| 134 | } |
| 135 | |
| 136 | int _sys_istty(FILEHANDLE fh) |
| 137 | { |
| 138 | if (fh > 0x8000) |
| 139 | { |
| 140 | return (1); |
| 141 | } |
| 142 | return (0); |
| 143 | } |
| 144 | |
| 145 | int _sys_seek(FILEHANDLE fh, long pos) |
| 146 | { |
| 147 | UNUSED(pos); |
| 148 | if (fh > 0x8000) |
| 149 | { |
| 150 | return (-1); |
| 151 | } |
| 152 | return (-1); |
| 153 | } |
| 154 | |
| 155 | int _sys_ensure(FILEHANDLE fh) |
| 156 | { |
| 157 | if (fh > 0x8000) |
| 158 | { |
| 159 | return (-1); |
| 160 | } |
| 161 | return (-1); |
| 162 | } |
| 163 | |
| 164 | long _sys_flen(FILEHANDLE fh) |
| 165 | { |
| 166 | if (fh > 0x8000) |
| 167 | { |
| 168 | return (0); |
| 169 | } |
| 170 | return (-1); |
| 171 | } |
| 172 | |
| 173 | int _sys_tmpnam(char *name, int sig, unsigned maxlen) |
| 174 | { |
| 175 | UNUSED(name); |
| 176 | UNUSED(sig); |
| 177 | UNUSED(maxlen); |
| 178 | return (1); |
| 179 | } |
| 180 | |
| 181 | char *_sys_command_string(char *cmd, int len) |
| 182 | { |
| 183 | UNUSED(len); |
| 184 | return (cmd); |
| 185 | } |
| 186 | |
| 187 | void _sys_exit(int return_code) |
| 188 | { |
| 189 | UartEndSimulation(return_code); |
| 190 | } |
| 191 | |
| 192 | int system(const char *cmd) |
| 193 | { |
| 194 | UNUSED(cmd); |
| 195 | return (0); |
| 196 | } |
| 197 | |
| 198 | time_t time(time_t *timer) |
| 199 | { |
| 200 | time_t current; |
| 201 | |
| 202 | #if defined (MPS3_PLATFORM) |
| 203 | current = MPS3_FPGAIO->COUNTER; |
| 204 | #else /* MPS3_PLATFORM */ |
| 205 | current = 0; /* No RTC implementation available. */ |
| 206 | #endif /* MPS3_PLATFORM */ |
| 207 | |
| 208 | if (timer != NULL) { |
| 209 | *timer = current; |
| 210 | } |
| 211 | |
| 212 | return (current); |
| 213 | } |
| 214 | |
| 215 | #else /* #if defined(__ARMCC_VERSION) && (__ARMCC_VERSION >= 6010050) */ |
| 216 | |
| 217 | /******************************************************************************/ |
| 218 | /* Retarget functions for GNU Tools for ARM Embedded Processors */ |
| 219 | /******************************************************************************/ |
| 220 | #include <stdio.h> |
| 221 | #include <sys/stat.h> |
| 222 | |
| 223 | extern unsigned char UartPutc(unsigned char my_ch); |
| 224 | |
| 225 | __attribute__((used)) int _write(int fd, char *ptr, int len) |
| 226 | { |
| 227 | size_t i; |
| 228 | for (i = 0; i < len; i++) |
| 229 | { |
| 230 | UartPutc(ptr[i]); /* call character output function. */ |
| 231 | } |
| 232 | return len; |
| 233 | } |
| 234 | |
| 235 | #endif /* #if defined(__ARMCC_VERSION) && (__ARMCC_VERSION >= 6010050) */ |