blob: b83ac408161205299a0d2de3a66945e84b155dca [file] [log] [blame]
Kristofer Jonsson43ce4912020-11-20 09:42:53 +01001/*
2 * Copyright (c) 2019-2020 Arm Limited. All rights reserved.
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 *
6 * Licensed under the Apache License, Version 2.0 (the License); you may
7 * not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 * www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an AS IS BASIS, WITHOUT
14 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 */
18
19#include <stdio.h>
20#include <stdlib.h>
21#include <string.h>
22#include <time.h>
23
24#include "uart.h"
25
26// armclang retargeting
27#if defined(__ARMCC_VERSION) && (__ARMCC_VERSION >= 6100100)
28#include <rt_misc.h>
29#include <rt_sys.h>
30
31/* Standard IO device handles. */
32#define STDIN 0x8001
33#define STDOUT 0x8002
34#define STDERR 0x8003
35
36#define RETARGET(fun) _sys##fun
37
38#else
39/*
40 * This type is used by the _ I/O functions to denote an open
41 * file.
42 */
43typedef int FILEHANDLE;
44
45/*
46 * Open a file. May return -1 if the file failed to open.
47 */
48extern FILEHANDLE _open(const char * /*name*/, int /*openmode*/);
49
50/* Standard IO device handles. */
51#define STDIN 0x00
52#define STDOUT 0x01
53#define STDERR 0x02
54
55#define RETARGET(fun) fun
56
57#endif
58
59/* Standard IO device name defines. */
60const char __stdin_name[] __attribute__((aligned(4))) = "STDIN";
61const char __stdout_name[] __attribute__((aligned(4))) = "STDOUT";
62const char __stderr_name[] __attribute__((aligned(4))) = "STDERR";
63
64void _ttywrch(int ch) {
65 (void)fputc(ch, stdout);
66}
67
68FILEHANDLE RETARGET(_open)(const char *name, int openmode) {
69 (void)openmode;
70
71 if (strcmp(name, __stdin_name) == 0) {
72 return (STDIN);
73 }
74
75 if (strcmp(name, __stdout_name) == 0) {
76 return (STDOUT);
77 }
78
79 if (strcmp(name, __stderr_name) == 0) {
80 return (STDERR);
81 }
82
83 return -1;
84}
85
86int RETARGET(_write)(FILEHANDLE fh, const unsigned char *buf, unsigned int len, int mode) {
87 (void)mode;
88
89 switch (fh) {
90 case STDOUT:
91 case STDERR: {
92 int c;
93
94 while (len-- > 0) {
95 c = fputc(*buf++, stdout);
96 if (c == EOF) {
97 return EOF;
98 }
99 }
100
101 return 0;
102 }
103 default:
104 return EOF;
105 }
106}
107
108int RETARGET(_read)(FILEHANDLE fh, unsigned char *buf, unsigned int len, int mode) {
109 (void)mode;
110
111 switch (fh) {
112 case STDIN: {
113 int c;
114
115 while (len-- > 0) {
116 c = fgetc(stdin);
117 if (c == EOF) {
118 return EOF;
119 }
120
121 *buf++ = (unsigned char)c;
122 }
123
124 return 0;
125 }
126 default:
127 return EOF;
128 }
129}
130
131int RETARGET(_istty)(FILEHANDLE fh) {
132 switch (fh) {
133 case STDIN:
134 case STDOUT:
135 case STDERR:
136 return 1;
137 default:
138 return 0;
139 }
140}
141
142int RETARGET(_close)(FILEHANDLE fh) {
143 if (RETARGET(_istty(fh))) {
144 return 0;
145 }
146
147 return -1;
148}
149
150int RETARGET(_seek)(FILEHANDLE fh, long pos) {
151 (void)fh;
152 (void)pos;
153
154 return -1;
155}
156
157int RETARGET(_ensure)(FILEHANDLE fh) {
158 (void)fh;
159
160 return -1;
161}
162
163long RETARGET(_flen)(FILEHANDLE fh) {
164 if (RETARGET(_istty)(fh)) {
165 return 0;
166 }
167
168 return -1;
169}
170
171int RETARGET(_tmpnam)(char *name, int sig, unsigned maxlen) {
172 (void)name;
173 (void)sig;
174 (void)maxlen;
175
176 return 1;
177}
178
179char *RETARGET(_command_string)(char *cmd, int len) {
180 (void)len;
181
182 return cmd;
183}
184
185void RETARGET(_exit)(int return_code) {
Kristofer Jonssonf62c3d72021-01-21 17:39:03 +0100186 char exit_code_buffer[64] = {0};
Anton Moberg1a679c42021-02-10 08:45:39 +0100187 const char *p = exit_code_buffer;
Kristofer Jonssonf62c3d72021-01-21 17:39:03 +0100188
189 /* Print out the exit code on the uart so any reader know how we exit. */
190 /* By appending 0x04, ASCII for end-of-transmission the FVP model exits,
191 * if the configuration parameter shutdown_on_eot on the uart is enabled.
192 * For some versions of FVP, the shutdown_on_eot is broken, but the same
193 * behaviour can be created by passing specifying a shutdown_tag= for the
194 * uart when starting the model so that is added last as well.
195 */
196
Anton Moberg1a679c42021-02-10 08:45:39 +0100197 snprintf(exit_code_buffer,
198 sizeof(exit_code_buffer),
199 "Application exit code: %d.\n" // Let the readers know how we exit
200 "\04\n" // end-of-transmission
201 "EXITTHESIM\n", // shutdown_tag
202 return_code);
Kristofer Jonssonf62c3d72021-01-21 17:39:03 +0100203
204 while (*p != '\0') {
205 uart_putc(*p++);
206 }
207
208 while (1) {}
Kristofer Jonsson43ce4912020-11-20 09:42:53 +0100209}
210
211int system(const char *cmd) {
212 (void)cmd;
213
214 return 0;
215}
216
217time_t time(time_t *timer) {
218 time_t current;
219
220 current = 0; // To Do !! No RTC implemented
221
222 if (timer != NULL) {
223 *timer = current;
224 }
225
226 return current;
227}
228
229void _clock_init(void) {
230#if 0
231 // Example implementation based on SysTick
232 // For instance, use a counting var in a SysTick interrupt handler
233 // for clock() to use
234 SysTick->LOAD = (uint32_t) ((SystemCoreClock/100)-1UL);
235 NVIC_SetPriority (SysTick_IRQn, (1UL << __NVIC_PRIO_BITS) - 1UL);
236 SysTick->VAL = 0UL;
237 SysTick->CTRL = SysTick_CTRL_CLKSOURCE_Msk | SysTick_CTRL_TICKINT_Msk | SysTick_CTRL_ENABLE_Msk;
238#endif
239}
240
241clock_t clock(void) {
242 return (clock_t)-1;
243}
244
245int remove(const char *arg) {
246 (void)arg;
247 return 0;
248}
249
250int rename(const char *oldn, const char *newn) {
251 (void)oldn;
252 (void)newn;
253 return 0;
254}
255
Kristofer Jonsson43ce4912020-11-20 09:42:53 +0100256int fputc(int ch, FILE *f) {
257 (void)(f);
258 return uart_putc(ch);
259}
260
261int fgetc(FILE *f) {
262 (void)f;
263 return uart_putc(uart_getc());
264}
265
266#ifndef ferror
267/* arm-none-eabi-gcc with newlib uses a define for ferror */
268int ferror(FILE *f) {
269 (void)f;
270 return EOF;
271}
272#endif