1 /*
2 * SPDX-FileCopyrightText: Copyright 2010-2021, 2024 Arm Limited and/or its affiliates <open-source-office@arm.com>
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 *
6 * Licensed under the Apache License, Version 2.0 (the License); you may
7 * not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 * www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an AS IS BASIS, WITHOUT
14 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 */
18
19 #if defined(__ARMCC_VERSION) && (__ARMCC_VERSION >= 6100100) && !defined(GCCCOMPILER)
20 #include <rt_misc.h>
21 #include <rt_sys.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <time.h>
26 #else
27 #include <errno.h>
28 #include <string.h>
29 #include <sys/stat.h>
30 #endif
31
32 #include "uart.h"
33
UartPutc(unsigned char ch)34 unsigned char UartPutc(unsigned char ch) { return uart_putc(ch); }
35
UartGetc(void)36 unsigned char UartGetc(void) { return uart_putc(uart_getc()); }
37
UartEndSimulation(int code)38 __attribute__((noreturn)) void UartEndSimulation(int code)
39 {
40 UartPutc((char)0x4); // End of simulation
41 UartPutc((char)code); // Exit code
42 while (1)
43 {
44 }
45 }
46
exit(int code)47 void exit(int code)
48 {
49 UartEndSimulation(code);
50 while (1)
51 {
52 }
53 }
54
55 #if defined(__ARMCC_VERSION) && (__ARMCC_VERSION >= 6100100) && !defined(GCCCOMPILER)
fputc(int ch,FILE * f)56 int fputc(int ch, FILE *f)
57 {
58 (void)(f);
59 return UartPutc(ch);
60 }
61
fgetc(FILE * f)62 int fgetc(FILE *f)
63 {
64 (void)f;
65 return UartPutc(UartGetc());
66 }
67 #else
SER_PutChar(int c)68 int SER_PutChar(int c) { return UartPutc(c); }
69
SER_GetChar(void)70 int SER_GetChar(void) { return UartPutc(UartGetc()); }
71 #endif
72
73 #if defined(__ARMCC_VERSION) && (__ARMCC_VERSION >= 6100100) && !defined(GCCCOMPILER)
74 /**
75 Copied from CMSIS/DSP/Platforms/FVP/ARMv81MML/system_ARMv81MML.c
76 */
77
78 #define FH_STDIN 0x8001
79 #define FH_STDOUT 0x8002
80 #define FH_STDERR 0x8003
81
82 const char __stdin_name[] = ":STDIN";
83 const char __stdout_name[] = ":STDOUT";
84 const char __stderr_name[] = ":STDERR";
85
86 /**
87 The following _sys_xxx functions are defined in rt_sys.h.
88 */
89
_sys_open(const char * name,int openmode)90 __attribute__((weak)) FILEHANDLE _sys_open(const char *name, int openmode)
91 {
92 (void)openmode;
93
94 if (name == NULL)
95 {
96 return (-1);
97 }
98
99 if (name[0] == ':')
100 {
101 if (strcmp(name, ":STDIN") == 0)
102 {
103 return (FH_STDIN);
104 }
105 if (strcmp(name, ":STDOUT") == 0)
106 {
107 return (FH_STDOUT);
108 }
109 if (strcmp(name, ":STDERR") == 0)
110 {
111 return (FH_STDERR);
112 }
113 return (-1);
114 }
115
116 return (-1);
117 }
118
_sys_close(FILEHANDLE fh)119 __attribute__((weak)) int _sys_close(FILEHANDLE fh)
120 {
121
122 switch (fh)
123 {
124 case FH_STDIN:
125 return (0);
126 case FH_STDOUT:
127 return (0);
128 case FH_STDERR:
129 return (0);
130 default:
131 return (-1);
132 }
133 }
134
_sys_write(FILEHANDLE fh,const uint8_t * buf,uint32_t len,int mode)135 __attribute__((weak)) int _sys_write(FILEHANDLE fh, const uint8_t *buf, uint32_t len, int mode)
136 {
137 (void)buf;
138 (void)len;
139 (void)mode;
140
141 switch (fh)
142 {
143 case FH_STDIN:
144 return (-1);
145 case FH_STDOUT:
146 return (0);
147 case FH_STDERR:
148 return (0);
149 default:
150 return (-1);
151 }
152 }
153
_sys_read(FILEHANDLE fh,uint8_t * buf,uint32_t len,int mode)154 __attribute__((weak)) int _sys_read(FILEHANDLE fh, uint8_t *buf, uint32_t len, int mode)
155 {
156 (void)buf;
157 (void)len;
158 (void)mode;
159
160 switch (fh)
161 {
162 case FH_STDIN:
163 return ((int)(len | 0x80000000U));
164 case FH_STDOUT:
165 return (-1);
166 case FH_STDERR:
167 return (-1);
168 default:
169 return (-1);
170 }
171 }
172
_sys_istty(FILEHANDLE fh)173 __attribute__((weak)) int _sys_istty(FILEHANDLE fh)
174 {
175
176 switch (fh)
177 {
178 case FH_STDIN:
179 return (1);
180 case FH_STDOUT:
181 return (1);
182 case FH_STDERR:
183 return (1);
184 default:
185 return (0);
186 }
187 }
188
_sys_seek(FILEHANDLE fh,long pos)189 __attribute__((weak)) int _sys_seek(FILEHANDLE fh, long pos)
190 {
191 (void)pos;
192
193 switch (fh)
194 {
195 case FH_STDIN:
196 return (-1);
197 case FH_STDOUT:
198 return (-1);
199 case FH_STDERR:
200 return (-1);
201 default:
202 return (-1);
203 }
204 }
205
_sys_flen(FILEHANDLE fh)206 __attribute__((weak)) long _sys_flen(FILEHANDLE fh)
207 {
208
209 switch (fh)
210 {
211 case FH_STDIN:
212 return (0);
213 case FH_STDOUT:
214 return (0);
215 case FH_STDERR:
216 return (0);
217 default:
218 return (0);
219 }
220 }
221
222 __attribute__((weak)) char *(_sys_command_string)(char *cmd, int len)
223 {
224 (void)len;
225
226 return cmd;
227 }
228
229 __attribute__((weak)) void(_sys_exit)(int return_code) { exit(return_code); }
230
231 #else
232 /**
233 Copied from CMSIS/DSP/DSP_Lib_TestSuite/Common/platform/GCC/Retarget.c
234 */
235
_open(const char * path,int flags,...)236 int _open(const char *path, int flags, ...)
237 {
238 (void)path;
239 (void)flags;
240 return (-1);
241 }
242
_close(int fd)243 int _close(int fd)
244 {
245 (void)fd;
246 return (-1);
247 }
248
_lseek(int fd,int ptr,int dir)249 int _lseek(int fd, int ptr, int dir)
250 {
251 (void)fd;
252 (void)ptr;
253 (void)dir;
254 return (0);
255 }
256
_fstat(int fd,struct stat * st)257 int __attribute__((weak)) _fstat(int fd, struct stat *st)
258 {
259 (void)fd;
260 memset(st, 0, sizeof(*st));
261 st->st_mode = S_IFCHR;
262 return (0);
263 }
264
_isatty(int fd)265 int _isatty(int fd)
266 {
267 (void)fd;
268 return (1);
269 }
270
_read(int fd,char * ptr,int len)271 int _read(int fd, char *ptr, int len)
272 {
273 (void)fd;
274 char c;
275 int i;
276
277 for (i = 0; i < len; i++)
278 {
279 c = SER_GetChar();
280 if (c == 0x0D)
281 break;
282 *ptr++ = c;
283 SER_PutChar(c);
284 }
285 return (len - i);
286 }
287
_write(int fd,char * ptr,int len)288 int _write(int fd, char *ptr, int len)
289 {
290 (void)fd;
291 int i;
292
293 for (i = 0; i < len; i++)
294 SER_PutChar(*ptr++);
295 return (i);
296 }
297 #endif
298