blob: 9ed3004a7bc8436cab450287d7c9d9216c416986 [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 */
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 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{
alexander31ae9f02022-02-10 16:15:54 +000073 (void)(openmode);
alexander3c798932021-03-26 21:42:19 +000074
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{
alexander31ae9f02022-02-10 16:15:54 +000092 (void)(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{
alexander31ae9f02022-02-10 16:15:54 +0000115 (void)(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{
alexander31ae9f02022-02-10 16:15:54 +0000160 (void)(fh);
161 (void)(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{
alexander31ae9f02022-02-10 16:15:54 +0000168 (void)(fh);
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 +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{
alexander31ae9f02022-02-10 16:15:54 +0000184 (void)(name);
185 (void)(sig);
186 (void)(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{
alexander31ae9f02022-02-10 16:15:54 +0000193 (void)(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);
alexander31ae9f02022-02-10 16:15:54 +0000201 while(1);
alexander3c798932021-03-26 21:42:19 +0000202}
203
204int system(const char *cmd)
205{
alexander31ae9f02022-02-10 16:15:54 +0000206 (void)(cmd);
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +0100207
208 return 0;
alexander3c798932021-03-26 21:42:19 +0000209}
210
211time_t time(time_t *timer)
212{
213 time_t current;
214
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +0100215 current = 0; // To Do !! No RTC implemented
alexander3c798932021-03-26 21:42:19 +0000216
217 if (timer != NULL) {
218 *timer = current;
219 }
220
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +0100221 return current;
alexander3c798932021-03-26 21:42:19 +0000222}
223
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +0100224void _clock_init(void) {}
alexander3c798932021-03-26 21:42:19 +0000225
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +0100226clock_t clock(void)
alexander3c798932021-03-26 21:42:19 +0000227{
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +0100228 return (clock_t)-1;
alexander3c798932021-03-26 21:42:19 +0000229}
230
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +0100231int remove(const char *arg) {
alexander31ae9f02022-02-10 16:15:54 +0000232 (void)(arg);
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +0100233
234 return 0;
235}
236
237int rename(const char *oldn, const char *newn)
238{
alexander31ae9f02022-02-10 16:15:54 +0000239 (void)(oldn);
240 (void)(newn);
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +0100241
242 return 0;
243}
244
245int fputc(int ch, FILE *f)
246{
alexander31ae9f02022-02-10 16:15:54 +0000247 (void)(f);
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +0100248
249 return UartPutc(ch);
250}
251
252int fgetc(FILE *f)
253{
alexander31ae9f02022-02-10 16:15:54 +0000254 (void)(f);
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +0100255
256 return UartPutc(UartGetc());
257}
258
259#ifndef ferror
260
261/* arm-none-eabi-gcc with newlib uses a define for ferror */
262int ferror(FILE *f)
263{
alexander31ae9f02022-02-10 16:15:54 +0000264 (void)(f);
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +0100265
266 return EOF;
267}
268
269#endif /* #ifndef ferror */
Kshitij Sisodiaacc6b852022-03-01 10:23:11 +0000270
271#endif /* !defined(USE_SEMIHOSTING) */