blob: d06a67740ac008dbf72b217bb3ddc2089fa4ab74 [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
alexander3c798932021-03-26 21:42:19 +000031/* 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
Kshitij Sisodia7fbce742022-10-31 15:09:46 +000038#if __ARMCLIB_VERSION >= 6190004
39#define TMPNAM_FUNCTION RETARGET(_tmpnam2)
40#else
41#define TMPNAM_FUNCTION RETARGET(_tmpnam)
42#endif
43
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +010044#else
45/* GNU compiler re-targeting */
46
47/*
48 * This type is used by the _ I/O functions to denote an open
49 * file.
50 */
51typedef int FILEHANDLE;
52
53/*
54 * Open a file. May return -1 if the file failed to open.
55 */
Kshitij Sisodia7fbce742022-10-31 15:09:46 +000056extern FILEHANDLE _open(const char* /*name*/, int /*openmode*/);
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +010057
58/* Standard IO device handles. */
59#define STDIN 0x00
60#define STDOUT 0x01
61#define STDERR 0x02
62
63#define RETARGET(fun) fun
64
Kshitij Sisodia7fbce742022-10-31 15:09:46 +000065#define TMPNAM_FUNCTION RETARGET(_tmpnam)
66
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +010067#endif
alexander3c798932021-03-26 21:42:19 +000068
69/* Standard IO device name defines. */
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +010070const char __stdin_name[] __attribute__((aligned(4))) = "STDIN";
71const char __stdout_name[] __attribute__((aligned(4))) = "STDOUT";
72const char __stderr_name[] __attribute__((aligned(4))) = "STDERR";
alexander3c798932021-03-26 21:42:19 +000073
Kshitij Sisodiad5679cc2022-03-03 16:30:07 +000074__attribute__((noreturn)) static void UartEndSimulation(int code)
75{
Kshitij Sisodia7fbce742022-10-31 15:09:46 +000076 UartPutc((char)0x4); // End of simulation
77 UartPutc((char)code); // Exit code
78 while (1)
79 ;
Kshitij Sisodiad5679cc2022-03-03 16:30:07 +000080}
81
Kshitij Sisodia7fbce742022-10-31 15:09:46 +000082void _ttywrch(int ch)
83{
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +010084 (void)fputc(ch, stdout);
alexander3c798932021-03-26 21:42:19 +000085}
86
Kshitij Sisodia7fbce742022-10-31 15:09:46 +000087FILEHANDLE RETARGET(_open)(const char* name, int openmode)
alexander3c798932021-03-26 21:42:19 +000088{
alexander31ae9f02022-02-10 16:15:54 +000089 (void)(openmode);
alexander3c798932021-03-26 21:42:19 +000090
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +010091 if (strcmp(name, __stdin_name) == 0) {
alexander3c798932021-03-26 21:42:19 +000092 return (STDIN);
93 }
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +010094
95 if (strcmp(name, __stdout_name) == 0) {
alexander3c798932021-03-26 21:42:19 +000096 return (STDOUT);
97 }
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +010098
99 if (strcmp(name, __stderr_name) == 0) {
alexander3c798932021-03-26 21:42:19 +0000100 return (STDERR);
101 }
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +0100102
103 return -1;
alexander3c798932021-03-26 21:42:19 +0000104}
105
Kshitij Sisodia7fbce742022-10-31 15:09:46 +0000106int RETARGET(_write)(FILEHANDLE fh, const unsigned char* buf, unsigned int len, int mode)
alexander3c798932021-03-26 21:42:19 +0000107{
alexander31ae9f02022-02-10 16:15:54 +0000108 (void)(mode);
alexander3c798932021-03-26 21:42:19 +0000109
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +0100110 switch (fh) {
111 case STDOUT:
112 case STDERR: {
113 int c;
114
115 while (len-- > 0) {
116 c = fputc(*buf++, stdout);
117 if (c == EOF) {
118 return EOF;
119 }
120 }
121
122 return 0;
alexander3c798932021-03-26 21:42:19 +0000123 }
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +0100124 default:
125 return EOF;
126 }
alexander3c798932021-03-26 21:42:19 +0000127}
128
Kshitij Sisodia7fbce742022-10-31 15:09:46 +0000129int RETARGET(_read)(FILEHANDLE fh, unsigned char* buf, unsigned int len, int mode)
alexander3c798932021-03-26 21:42:19 +0000130{
alexander31ae9f02022-02-10 16:15:54 +0000131 (void)(mode);
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +0100132
133 switch (fh) {
134 case STDIN: {
135 int c;
136
137 while (len-- > 0) {
138 c = fgetc(stdin);
139 if (c == EOF) {
140 return EOF;
141 }
142
143 *buf++ = (unsigned char)c;
alexander3c798932021-03-26 21:42:19 +0000144 }
alexander3c798932021-03-26 21:42:19 +0000145
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +0100146 return 0;
alexander3c798932021-03-26 21:42:19 +0000147 }
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +0100148 default:
149 return EOF;
150 }
alexander3c798932021-03-26 21:42:19 +0000151}
152
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +0100153int RETARGET(_istty)(FILEHANDLE fh)
alexander3c798932021-03-26 21:42:19 +0000154{
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +0100155 switch (fh) {
156 case STDIN:
157 case STDOUT:
158 case STDERR:
159 return 1;
160 default:
161 return 0;
alexander3c798932021-03-26 21:42:19 +0000162 }
alexander3c798932021-03-26 21:42:19 +0000163}
164
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +0100165int RETARGET(_close)(FILEHANDLE fh)
alexander3c798932021-03-26 21:42:19 +0000166{
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +0100167 if (RETARGET(_istty(fh))) {
168 return 0;
169 }
170
171 return -1;
172}
173
174int RETARGET(_seek)(FILEHANDLE fh, long pos)
175{
alexander31ae9f02022-02-10 16:15:54 +0000176 (void)(fh);
177 (void)(pos);
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(_ensure)(FILEHANDLE fh)
alexander3c798932021-03-26 21:42:19 +0000183{
alexander31ae9f02022-02-10 16:15:54 +0000184 (void)(fh);
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 +0100189long RETARGET(_flen)(FILEHANDLE fh)
alexander3c798932021-03-26 21:42:19 +0000190{
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +0100191 if (RETARGET(_istty)(fh)) {
192 return 0;
alexander3c798932021-03-26 21:42:19 +0000193 }
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +0100194
195 return -1;
alexander3c798932021-03-26 21:42:19 +0000196}
197
Kshitij Sisodia7fbce742022-10-31 15:09:46 +0000198int TMPNAM_FUNCTION(char* name, int sig, unsigned int maxlen)
alexander3c798932021-03-26 21:42:19 +0000199{
alexander31ae9f02022-02-10 16:15:54 +0000200 (void)(name);
201 (void)(sig);
202 (void)(maxlen);
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +0100203
204 return 1;
alexander3c798932021-03-26 21:42:19 +0000205}
206
Kshitij Sisodia7fbce742022-10-31 15:09:46 +0000207char* RETARGET(_command_string)(char* cmd, int len)
alexander3c798932021-03-26 21:42:19 +0000208{
alexander31ae9f02022-02-10 16:15:54 +0000209 (void)(len);
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +0100210
211 return cmd;
alexander3c798932021-03-26 21:42:19 +0000212}
213
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +0100214void RETARGET(_exit)(int return_code)
alexander3c798932021-03-26 21:42:19 +0000215{
216 UartEndSimulation(return_code);
Kshitij Sisodia7fbce742022-10-31 15:09:46 +0000217 while (1)
218 ;
alexander3c798932021-03-26 21:42:19 +0000219}
220
Kshitij Sisodia7fbce742022-10-31 15:09:46 +0000221int system(const char* cmd)
alexander3c798932021-03-26 21:42:19 +0000222{
alexander31ae9f02022-02-10 16:15:54 +0000223 (void)(cmd);
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +0100224
225 return 0;
alexander3c798932021-03-26 21:42:19 +0000226}
227
Kshitij Sisodia7fbce742022-10-31 15:09:46 +0000228time_t time(time_t* timer)
alexander3c798932021-03-26 21:42:19 +0000229{
230 time_t current;
231
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +0100232 current = 0; // To Do !! No RTC implemented
alexander3c798932021-03-26 21:42:19 +0000233
234 if (timer != NULL) {
235 *timer = current;
236 }
237
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +0100238 return current;
alexander3c798932021-03-26 21:42:19 +0000239}
240
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +0100241void _clock_init(void) {}
alexander3c798932021-03-26 21:42:19 +0000242
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +0100243clock_t clock(void)
alexander3c798932021-03-26 21:42:19 +0000244{
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +0100245 return (clock_t)-1;
alexander3c798932021-03-26 21:42:19 +0000246}
247
Kshitij Sisodia7fbce742022-10-31 15:09:46 +0000248int remove(const char* arg)
249{
alexander31ae9f02022-02-10 16:15:54 +0000250 (void)(arg);
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +0100251
252 return 0;
253}
254
Kshitij Sisodia7fbce742022-10-31 15:09:46 +0000255int rename(const char* oldn, const char* newn)
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +0100256{
alexander31ae9f02022-02-10 16:15:54 +0000257 (void)(oldn);
258 (void)(newn);
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +0100259
260 return 0;
261}
262
Kshitij Sisodia7fbce742022-10-31 15:09:46 +0000263int fputc(int ch, FILE* f)
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +0100264{
alexander31ae9f02022-02-10 16:15:54 +0000265 (void)(f);
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +0100266
267 return UartPutc(ch);
268}
269
Kshitij Sisodia7fbce742022-10-31 15:09:46 +0000270int fgetc(FILE* f)
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +0100271{
alexander31ae9f02022-02-10 16:15:54 +0000272 (void)(f);
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +0100273
274 return UartPutc(UartGetc());
275}
276
277#ifndef ferror
278
279/* arm-none-eabi-gcc with newlib uses a define for ferror */
Kshitij Sisodia7fbce742022-10-31 15:09:46 +0000280int ferror(FILE* f)
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +0100281{
alexander31ae9f02022-02-10 16:15:54 +0000282 (void)(f);
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +0100283
284 return EOF;
285}
286
287#endif /* #ifndef ferror */
Kshitij Sisodiaacc6b852022-03-01 10:23:11 +0000288
289#endif /* !defined(USE_SEMIHOSTING) */