blob: 15984271dda48de9089968425628bc73951e97e8 [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) {
186 exit(return_code);
187}
188
189int system(const char *cmd) {
190 (void)cmd;
191
192 return 0;
193}
194
195time_t time(time_t *timer) {
196 time_t current;
197
198 current = 0; // To Do !! No RTC implemented
199
200 if (timer != NULL) {
201 *timer = current;
202 }
203
204 return current;
205}
206
207void _clock_init(void) {
208#if 0
209 // Example implementation based on SysTick
210 // For instance, use a counting var in a SysTick interrupt handler
211 // for clock() to use
212 SysTick->LOAD = (uint32_t) ((SystemCoreClock/100)-1UL);
213 NVIC_SetPriority (SysTick_IRQn, (1UL << __NVIC_PRIO_BITS) - 1UL);
214 SysTick->VAL = 0UL;
215 SysTick->CTRL = SysTick_CTRL_CLKSOURCE_Msk | SysTick_CTRL_TICKINT_Msk | SysTick_CTRL_ENABLE_Msk;
216#endif
217}
218
219clock_t clock(void) {
220 return (clock_t)-1;
221}
222
223int remove(const char *arg) {
224 (void)arg;
225 return 0;
226}
227
228int rename(const char *oldn, const char *newn) {
229 (void)oldn;
230 (void)newn;
231 return 0;
232}
233
234void exit(int code) {
235 uart_putc((char)0x4);
236 uart_putc((char)code);
237 while (1) {}
238}
239
240int fputc(int ch, FILE *f) {
241 (void)(f);
242 return uart_putc(ch);
243}
244
245int fgetc(FILE *f) {
246 (void)f;
247 return uart_putc(uart_getc());
248}
249
250#ifndef ferror
251/* arm-none-eabi-gcc with newlib uses a define for ferror */
252int ferror(FILE *f) {
253 (void)f;
254 return EOF;
255}
256#endif