blob: ac9b282df93ea12180daa29222fe72b9d5baec9f [file] [log] [blame]
alexander3c798932021-03-26 21:42:19 +00001/*
Kshitij Sisodiad5679cc2022-03-03 16:30:07 +00002 * Copyright (c) 2022 Arm Limited. All rights reserved.
alexander3c798932021-03-26 21:42:19 +00003 * 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 Sisodiaacc6b852022-03-01 10:23:11 +000017#if !defined(USE_SEMIHOSTING)
alexander3c798932021-03-26 21:42:19 +000018
19#include "uart_stdout.h"
alexander3c798932021-03-26 21:42:19 +000020
alexander3c798932021-03-26 21:42:19 +000021#include <stdio.h>
22#include <string.h>
23#include <time.h>
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +010024
25#if defined(__ARMCC_VERSION) && (__ARMCC_VERSION >= 6100100)
26/* Arm compiler re-targeting */
27
alexander3c798932021-03-26 21:42:19 +000028#include <rt_misc.h>
29#include <rt_sys.h>
30
31
32/* Standard IO device handles. */
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +010033#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 */
46typedef int FILEHANDLE;
47
48/*
49 * Open a file. May return -1 if the file failed to open.
50 */
51extern 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
alexander3c798932021-03-26 21:42:19 +000061
62/* Standard IO device name defines. */
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +010063const char __stdin_name[] __attribute__((aligned(4))) = "STDIN";
64const char __stdout_name[] __attribute__((aligned(4))) = "STDOUT";
65const char __stderr_name[] __attribute__((aligned(4))) = "STDERR";
alexander3c798932021-03-26 21:42:19 +000066
Kshitij Sisodiad5679cc2022-03-03 16:30:07 +000067__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 Sisodiaf9c19ea2021-05-07 16:08:14 +010074void _ttywrch(int ch) {
75 (void)fputc(ch, stdout);
alexander3c798932021-03-26 21:42:19 +000076}
77
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +010078FILEHANDLE RETARGET(_open)(const char *name, int openmode)
alexander3c798932021-03-26 21:42:19 +000079{
alexander31ae9f02022-02-10 16:15:54 +000080 (void)(openmode);
alexander3c798932021-03-26 21:42:19 +000081
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +010082 if (strcmp(name, __stdin_name) == 0) {
alexander3c798932021-03-26 21:42:19 +000083 return (STDIN);
84 }
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +010085
86 if (strcmp(name, __stdout_name) == 0) {
alexander3c798932021-03-26 21:42:19 +000087 return (STDOUT);
88 }
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +010089
90 if (strcmp(name, __stderr_name) == 0) {
alexander3c798932021-03-26 21:42:19 +000091 return (STDERR);
92 }
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +010093
94 return -1;
alexander3c798932021-03-26 21:42:19 +000095}
96
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +010097int RETARGET(_write)(FILEHANDLE fh, const unsigned char *buf, unsigned int len, int mode)
alexander3c798932021-03-26 21:42:19 +000098{
alexander31ae9f02022-02-10 16:15:54 +000099 (void)(mode);
alexander3c798932021-03-26 21:42:19 +0000100
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +0100101 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;
alexander3c798932021-03-26 21:42:19 +0000114 }
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +0100115 default:
116 return EOF;
117 }
alexander3c798932021-03-26 21:42:19 +0000118}
119
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +0100120int RETARGET(_read)(FILEHANDLE fh, unsigned char *buf, unsigned int len, int mode)
alexander3c798932021-03-26 21:42:19 +0000121{
alexander31ae9f02022-02-10 16:15:54 +0000122 (void)(mode);
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +0100123
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;
alexander3c798932021-03-26 21:42:19 +0000135 }
alexander3c798932021-03-26 21:42:19 +0000136
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +0100137 return 0;
alexander3c798932021-03-26 21:42:19 +0000138 }
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +0100139 default:
140 return EOF;
141 }
alexander3c798932021-03-26 21:42:19 +0000142}
143
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +0100144int RETARGET(_istty)(FILEHANDLE fh)
alexander3c798932021-03-26 21:42:19 +0000145{
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +0100146 switch (fh) {
147 case STDIN:
148 case STDOUT:
149 case STDERR:
150 return 1;
151 default:
152 return 0;
alexander3c798932021-03-26 21:42:19 +0000153 }
alexander3c798932021-03-26 21:42:19 +0000154}
155
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +0100156int RETARGET(_close)(FILEHANDLE fh)
alexander3c798932021-03-26 21:42:19 +0000157{
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +0100158 if (RETARGET(_istty(fh))) {
159 return 0;
160 }
161
162 return -1;
163}
164
165int RETARGET(_seek)(FILEHANDLE fh, long pos)
166{
alexander31ae9f02022-02-10 16:15:54 +0000167 (void)(fh);
168 (void)(pos);
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +0100169
170 return -1;
alexander3c798932021-03-26 21:42:19 +0000171}
172
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +0100173int RETARGET(_ensure)(FILEHANDLE fh)
alexander3c798932021-03-26 21:42:19 +0000174{
alexander31ae9f02022-02-10 16:15:54 +0000175 (void)(fh);
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +0100176
177 return -1;
alexander3c798932021-03-26 21:42:19 +0000178}
179
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +0100180long RETARGET(_flen)(FILEHANDLE fh)
alexander3c798932021-03-26 21:42:19 +0000181{
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +0100182 if (RETARGET(_istty)(fh)) {
183 return 0;
alexander3c798932021-03-26 21:42:19 +0000184 }
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +0100185
186 return -1;
alexander3c798932021-03-26 21:42:19 +0000187}
188
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +0100189int RETARGET(_tmpnam)(char *name, int sig, unsigned int maxlen)
alexander3c798932021-03-26 21:42:19 +0000190{
alexander31ae9f02022-02-10 16:15:54 +0000191 (void)(name);
192 (void)(sig);
193 (void)(maxlen);
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +0100194
195 return 1;
alexander3c798932021-03-26 21:42:19 +0000196}
197
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +0100198char *RETARGET(_command_string)(char *cmd, int len)
alexander3c798932021-03-26 21:42:19 +0000199{
alexander31ae9f02022-02-10 16:15:54 +0000200 (void)(len);
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +0100201
202 return cmd;
alexander3c798932021-03-26 21:42:19 +0000203}
204
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +0100205void RETARGET(_exit)(int return_code)
alexander3c798932021-03-26 21:42:19 +0000206{
207 UartEndSimulation(return_code);
alexander31ae9f02022-02-10 16:15:54 +0000208 while(1);
alexander3c798932021-03-26 21:42:19 +0000209}
210
211int system(const char *cmd)
212{
alexander31ae9f02022-02-10 16:15:54 +0000213 (void)(cmd);
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +0100214
215 return 0;
alexander3c798932021-03-26 21:42:19 +0000216}
217
218time_t time(time_t *timer)
219{
220 time_t current;
221
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +0100222 current = 0; // To Do !! No RTC implemented
alexander3c798932021-03-26 21:42:19 +0000223
224 if (timer != NULL) {
225 *timer = current;
226 }
227
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +0100228 return current;
alexander3c798932021-03-26 21:42:19 +0000229}
230
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +0100231void _clock_init(void) {}
alexander3c798932021-03-26 21:42:19 +0000232
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +0100233clock_t clock(void)
alexander3c798932021-03-26 21:42:19 +0000234{
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +0100235 return (clock_t)-1;
alexander3c798932021-03-26 21:42:19 +0000236}
237
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +0100238int remove(const char *arg) {
alexander31ae9f02022-02-10 16:15:54 +0000239 (void)(arg);
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +0100240
241 return 0;
242}
243
244int rename(const char *oldn, const char *newn)
245{
alexander31ae9f02022-02-10 16:15:54 +0000246 (void)(oldn);
247 (void)(newn);
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +0100248
249 return 0;
250}
251
252int fputc(int ch, FILE *f)
253{
alexander31ae9f02022-02-10 16:15:54 +0000254 (void)(f);
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +0100255
256 return UartPutc(ch);
257}
258
259int fgetc(FILE *f)
260{
alexander31ae9f02022-02-10 16:15:54 +0000261 (void)(f);
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +0100262
263 return UartPutc(UartGetc());
264}
265
266#ifndef ferror
267
268/* arm-none-eabi-gcc with newlib uses a define for ferror */
269int ferror(FILE *f)
270{
alexander31ae9f02022-02-10 16:15:54 +0000271 (void)(f);
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +0100272
273 return EOF;
274}
275
276#endif /* #ifndef ferror */
Kshitij Sisodiaacc6b852022-03-01 10:23:11 +0000277
278#endif /* !defined(USE_SEMIHOSTING) */