blob: 29c2023d1714db8db2a013fbbedcced49c32a6a6 [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"
19#include "bsp_core_log.h"
20
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 Sisodiaf9c19ea2021-05-07 16:08:14 +010067void _ttywrch(int ch) {
68 (void)fputc(ch, stdout);
alexander3c798932021-03-26 21:42:19 +000069}
70
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +010071FILEHANDLE RETARGET(_open)(const char *name, int openmode)
alexander3c798932021-03-26 21:42:19 +000072{
73 UNUSED(openmode);
74
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +010075 if (strcmp(name, __stdin_name) == 0) {
alexander3c798932021-03-26 21:42:19 +000076 return (STDIN);
77 }
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +010078
79 if (strcmp(name, __stdout_name) == 0) {
alexander3c798932021-03-26 21:42:19 +000080 return (STDOUT);
81 }
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +010082
83 if (strcmp(name, __stderr_name) == 0) {
alexander3c798932021-03-26 21:42:19 +000084 return (STDERR);
85 }
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +010086
87 return -1;
alexander3c798932021-03-26 21:42:19 +000088}
89
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +010090int RETARGET(_write)(FILEHANDLE fh, const unsigned char *buf, unsigned int len, int mode)
alexander3c798932021-03-26 21:42:19 +000091{
92 UNUSED(mode);
alexander3c798932021-03-26 21:42:19 +000093
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +010094 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;
alexander3c798932021-03-26 21:42:19 +0000107 }
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +0100108 default:
109 return EOF;
110 }
alexander3c798932021-03-26 21:42:19 +0000111}
112
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +0100113int RETARGET(_read)(FILEHANDLE fh, unsigned char *buf, unsigned int len, int mode)
alexander3c798932021-03-26 21:42:19 +0000114{
115 UNUSED(mode);
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +0100116
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;
alexander3c798932021-03-26 21:42:19 +0000128 }
alexander3c798932021-03-26 21:42:19 +0000129
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +0100130 return 0;
alexander3c798932021-03-26 21:42:19 +0000131 }
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +0100132 default:
133 return EOF;
134 }
alexander3c798932021-03-26 21:42:19 +0000135}
136
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +0100137int RETARGET(_istty)(FILEHANDLE fh)
alexander3c798932021-03-26 21:42:19 +0000138{
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +0100139 switch (fh) {
140 case STDIN:
141 case STDOUT:
142 case STDERR:
143 return 1;
144 default:
145 return 0;
alexander3c798932021-03-26 21:42:19 +0000146 }
alexander3c798932021-03-26 21:42:19 +0000147}
148
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +0100149int RETARGET(_close)(FILEHANDLE fh)
alexander3c798932021-03-26 21:42:19 +0000150{
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +0100151 if (RETARGET(_istty(fh))) {
152 return 0;
153 }
154
155 return -1;
156}
157
158int RETARGET(_seek)(FILEHANDLE fh, long pos)
159{
160 UNUSED(fh);
alexander3c798932021-03-26 21:42:19 +0000161 UNUSED(pos);
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +0100162
163 return -1;
alexander3c798932021-03-26 21:42:19 +0000164}
165
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +0100166int RETARGET(_ensure)(FILEHANDLE fh)
alexander3c798932021-03-26 21:42:19 +0000167{
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +0100168 UNUSED(fh);
169
170 return -1;
alexander3c798932021-03-26 21:42:19 +0000171}
172
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +0100173long RETARGET(_flen)(FILEHANDLE fh)
alexander3c798932021-03-26 21:42:19 +0000174{
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +0100175 if (RETARGET(_istty)(fh)) {
176 return 0;
alexander3c798932021-03-26 21:42:19 +0000177 }
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +0100178
179 return -1;
alexander3c798932021-03-26 21:42:19 +0000180}
181
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +0100182int RETARGET(_tmpnam)(char *name, int sig, unsigned int maxlen)
alexander3c798932021-03-26 21:42:19 +0000183{
184 UNUSED(name);
185 UNUSED(sig);
186 UNUSED(maxlen);
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +0100187
188 return 1;
alexander3c798932021-03-26 21:42:19 +0000189}
190
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +0100191char *RETARGET(_command_string)(char *cmd, int len)
alexander3c798932021-03-26 21:42:19 +0000192{
193 UNUSED(len);
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +0100194
195 return cmd;
alexander3c798932021-03-26 21:42:19 +0000196}
197
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +0100198void RETARGET(_exit)(int return_code)
alexander3c798932021-03-26 21:42:19 +0000199{
200 UartEndSimulation(return_code);
201}
202
203int system(const char *cmd)
204{
205 UNUSED(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) {
231 UNUSED(arg);
232
233 return 0;
234}
235
236int rename(const char *oldn, const char *newn)
237{
238 UNUSED(oldn);
239 UNUSED(newn);
240
241 return 0;
242}
243
244int fputc(int ch, FILE *f)
245{
246 UNUSED(f);
247
248 return UartPutc(ch);
249}
250
251int 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 */
261int ferror(FILE *f)
262{
263 UNUSED(f);
264
265 return EOF;
266}
267
268#endif /* #ifndef ferror */