blob: dfef62c5c091e8228a7cdcd3850f7ded92203c22 [file] [log] [blame]
alexander3c798932021-03-26 21:42:19 +00001/*
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 */
alexander3c798932021-03-26 21:42:19 +000017
18#include "uart_stdout.h"
alexander3c798932021-03-26 21:42:19 +000019
alexander3c798932021-03-26 21:42:19 +000020#include <stdio.h>
21#include <string.h>
22#include <time.h>
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +010023
24#if defined(__ARMCC_VERSION) && (__ARMCC_VERSION >= 6100100)
25/* Arm compiler re-targeting */
26
alexander3c798932021-03-26 21:42:19 +000027#include <rt_misc.h>
28#include <rt_sys.h>
29
30
31/* Standard IO device handles. */
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +010032#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 */
45typedef int FILEHANDLE;
46
47/*
48 * Open a file. May return -1 if the file failed to open.
49 */
50extern 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
alexander3c798932021-03-26 21:42:19 +000060
61/* Standard IO device name defines. */
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +010062const char __stdin_name[] __attribute__((aligned(4))) = "STDIN";
63const char __stdout_name[] __attribute__((aligned(4))) = "STDOUT";
64const char __stderr_name[] __attribute__((aligned(4))) = "STDERR";
alexander3c798932021-03-26 21:42:19 +000065
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +010066void _ttywrch(int ch) {
67 (void)fputc(ch, stdout);
alexander3c798932021-03-26 21:42:19 +000068}
69
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +010070FILEHANDLE RETARGET(_open)(const char *name, int openmode)
alexander3c798932021-03-26 21:42:19 +000071{
alexander31ae9f02022-02-10 16:15:54 +000072 (void)(openmode);
alexander3c798932021-03-26 21:42:19 +000073
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +010074 if (strcmp(name, __stdin_name) == 0) {
alexander3c798932021-03-26 21:42:19 +000075 return (STDIN);
76 }
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +010077
78 if (strcmp(name, __stdout_name) == 0) {
alexander3c798932021-03-26 21:42:19 +000079 return (STDOUT);
80 }
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +010081
82 if (strcmp(name, __stderr_name) == 0) {
alexander3c798932021-03-26 21:42:19 +000083 return (STDERR);
84 }
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +010085
86 return -1;
alexander3c798932021-03-26 21:42:19 +000087}
88
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +010089int RETARGET(_write)(FILEHANDLE fh, const unsigned char *buf, unsigned int len, int mode)
alexander3c798932021-03-26 21:42:19 +000090{
alexander31ae9f02022-02-10 16:15:54 +000091 (void)(mode);
alexander3c798932021-03-26 21:42:19 +000092
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +010093 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;
alexander3c798932021-03-26 21:42:19 +0000106 }
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +0100107 default:
108 return EOF;
109 }
alexander3c798932021-03-26 21:42:19 +0000110}
111
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +0100112int RETARGET(_read)(FILEHANDLE fh, unsigned char *buf, unsigned int len, int mode)
alexander3c798932021-03-26 21:42:19 +0000113{
alexander31ae9f02022-02-10 16:15:54 +0000114 (void)(mode);
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +0100115
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;
alexander3c798932021-03-26 21:42:19 +0000127 }
alexander3c798932021-03-26 21:42:19 +0000128
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +0100129 return 0;
alexander3c798932021-03-26 21:42:19 +0000130 }
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +0100131 default:
132 return EOF;
133 }
alexander3c798932021-03-26 21:42:19 +0000134}
135
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +0100136int RETARGET(_istty)(FILEHANDLE fh)
alexander3c798932021-03-26 21:42:19 +0000137{
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +0100138 switch (fh) {
139 case STDIN:
140 case STDOUT:
141 case STDERR:
142 return 1;
143 default:
144 return 0;
alexander3c798932021-03-26 21:42:19 +0000145 }
alexander3c798932021-03-26 21:42:19 +0000146}
147
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +0100148int RETARGET(_close)(FILEHANDLE fh)
alexander3c798932021-03-26 21:42:19 +0000149{
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +0100150 if (RETARGET(_istty(fh))) {
151 return 0;
152 }
153
154 return -1;
155}
156
157int RETARGET(_seek)(FILEHANDLE fh, long pos)
158{
alexander31ae9f02022-02-10 16:15:54 +0000159 (void)(fh);
160 (void)(pos);
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +0100161
162 return -1;
alexander3c798932021-03-26 21:42:19 +0000163}
164
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +0100165int RETARGET(_ensure)(FILEHANDLE fh)
alexander3c798932021-03-26 21:42:19 +0000166{
alexander31ae9f02022-02-10 16:15:54 +0000167 (void)(fh);
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +0100168
169 return -1;
alexander3c798932021-03-26 21:42:19 +0000170}
171
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +0100172long RETARGET(_flen)(FILEHANDLE fh)
alexander3c798932021-03-26 21:42:19 +0000173{
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +0100174 if (RETARGET(_istty)(fh)) {
175 return 0;
alexander3c798932021-03-26 21:42:19 +0000176 }
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +0100177
178 return -1;
alexander3c798932021-03-26 21:42:19 +0000179}
180
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +0100181int RETARGET(_tmpnam)(char *name, int sig, unsigned int maxlen)
alexander3c798932021-03-26 21:42:19 +0000182{
alexander31ae9f02022-02-10 16:15:54 +0000183 (void)(name);
184 (void)(sig);
185 (void)(maxlen);
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +0100186
187 return 1;
alexander3c798932021-03-26 21:42:19 +0000188}
189
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +0100190char *RETARGET(_command_string)(char *cmd, int len)
alexander3c798932021-03-26 21:42:19 +0000191{
alexander31ae9f02022-02-10 16:15:54 +0000192 (void)(len);
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +0100193
194 return cmd;
alexander3c798932021-03-26 21:42:19 +0000195}
196
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +0100197void RETARGET(_exit)(int return_code)
alexander3c798932021-03-26 21:42:19 +0000198{
199 UartEndSimulation(return_code);
alexander31ae9f02022-02-10 16:15:54 +0000200 while(1);
alexander3c798932021-03-26 21:42:19 +0000201}
202
203int system(const char *cmd)
204{
alexander31ae9f02022-02-10 16:15:54 +0000205 (void)(cmd);
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +0100206
207 return 0;
alexander3c798932021-03-26 21:42:19 +0000208}
209
210time_t time(time_t *timer)
211{
212 time_t current;
213
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +0100214 current = 0; // To Do !! No RTC implemented
alexander3c798932021-03-26 21:42:19 +0000215
216 if (timer != NULL) {
217 *timer = current;
218 }
219
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +0100220 return current;
alexander3c798932021-03-26 21:42:19 +0000221}
222
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +0100223void _clock_init(void) {}
alexander3c798932021-03-26 21:42:19 +0000224
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +0100225clock_t clock(void)
alexander3c798932021-03-26 21:42:19 +0000226{
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +0100227 return (clock_t)-1;
alexander3c798932021-03-26 21:42:19 +0000228}
229
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +0100230int remove(const char *arg) {
alexander31ae9f02022-02-10 16:15:54 +0000231 (void)(arg);
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +0100232
233 return 0;
234}
235
236int rename(const char *oldn, const char *newn)
237{
alexander31ae9f02022-02-10 16:15:54 +0000238 (void)(oldn);
239 (void)(newn);
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +0100240
241 return 0;
242}
243
244int fputc(int ch, FILE *f)
245{
alexander31ae9f02022-02-10 16:15:54 +0000246 (void)(f);
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +0100247
248 return UartPutc(ch);
249}
250
251int fgetc(FILE *f)
252{
alexander31ae9f02022-02-10 16:15:54 +0000253 (void)(f);
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +0100254
255 return UartPutc(UartGetc());
256}
257
258#ifndef ferror
259
260/* arm-none-eabi-gcc with newlib uses a define for ferror */
261int ferror(FILE *f)
262{
alexander31ae9f02022-02-10 16:15:54 +0000263 (void)(f);
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +0100264
265 return EOF;
266}
267
268#endif /* #ifndef ferror */