blob: cf31a531e3943f01f068432b9675775c094da133 [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 */
17#if defined(__ARMCC_VERSION) && (__ARMCC_VERSION >= 6010050)
18
19#include "uart_stdout.h"
20#include "bsp_core_log.h"
21
22#if defined (MPS3_PLATFORM)
23#include "smm_mps3.h"
24#endif /* MPS3_PLATFORM */
25
26#include <stdio.h>
27#include <string.h>
28#include <time.h>
29#include <rt_misc.h>
30#include <rt_sys.h>
31
32
33/* Standard IO device handles. */
34#define STDIN 0x8001
35#define STDOUT 0x8002
36#define STDERR 0x8003
37
38/* Standard IO device name defines. */
39const char __stdin_name[] = "STDIN";
40const char __stdout_name[] = "STDOUT";
41const char __stderr_name[] = "STDERR";
42
43int fputc(int ch, FILE *f)
44{
45 UNUSED(f);
46 return (UartPutc(ch));
47}
48
49int fgetc(FILE *f)
50{
51 UNUSED(f);
52 return (UartPutc(UartGetc()));
53}
54
55int ferror(FILE *f)
56{
57 UNUSED(f);
58 /* Your implementation of ferror */
59 return EOF;
60}
61
62void _ttywrch(int ch)
63{
64 UartPutc(ch);
65}
66
67FILEHANDLE _sys_open(const char *name, int openmode)
68{
69 UNUSED(openmode);
70
71 /* Register standard Input Output devices. */
72 if (strcmp(name, "STDIN") == 0)
73 {
74 return (STDIN);
75 }
76 if (strcmp(name, "STDOUT") == 0)
77 {
78 return (STDOUT);
79 }
80 if (strcmp(name, "STDERR") == 0)
81 {
82 return (STDERR);
83 }
84 return (-1);
85}
86
87int _sys_close(FILEHANDLE fh)
88{
89 if (fh > 0x8000)
90 {
91 return (0);
92 }
93 return (-1);
94}
95
96int _sys_write(FILEHANDLE fh, const unsigned char *buf, unsigned int len, int mode)
97{
98 UNUSED(mode);
99 if (fh == STDOUT || fh == STDERR )
100 {
101 /* Standard Output device. */
102 for (; len; len--)
103 {
104 UartPutc(*buf++);
105 }
106 return (0);
107 }
108
109 if (fh > 0x8000)
110 {
111 return (-1);
112 }
113 return (-1);
114}
115
116int _sys_read(FILEHANDLE fh, unsigned char *buf, unsigned int len, int mode)
117{
118 UNUSED(mode);
119 if (fh == STDIN)
120 {
121 /* Standard Input device. */
122 for (; len; len--)
123 {
124 *buf++ = UartGetc();
125 }
126 return (0);
127 }
128
129 if (fh > 0x8000)
130 {
131 return (-1);
132 }
133 return (-1);
134}
135
136int _sys_istty(FILEHANDLE fh)
137{
138 if (fh > 0x8000)
139 {
140 return (1);
141 }
142 return (0);
143}
144
145int _sys_seek(FILEHANDLE fh, long pos)
146{
147 UNUSED(pos);
148 if (fh > 0x8000)
149 {
150 return (-1);
151 }
152 return (-1);
153}
154
155int _sys_ensure(FILEHANDLE fh)
156{
157 if (fh > 0x8000)
158 {
159 return (-1);
160 }
161 return (-1);
162}
163
164long _sys_flen(FILEHANDLE fh)
165{
166 if (fh > 0x8000)
167 {
168 return (0);
169 }
170 return (-1);
171}
172
173int _sys_tmpnam(char *name, int sig, unsigned maxlen)
174{
175 UNUSED(name);
176 UNUSED(sig);
177 UNUSED(maxlen);
178 return (1);
179}
180
181char *_sys_command_string(char *cmd, int len)
182{
183 UNUSED(len);
184 return (cmd);
185}
186
187void _sys_exit(int return_code)
188{
189 UartEndSimulation(return_code);
190}
191
192int system(const char *cmd)
193{
194 UNUSED(cmd);
195 return (0);
196}
197
198time_t time(time_t *timer)
199{
200 time_t current;
201
202#if defined (MPS3_PLATFORM)
203 current = MPS3_FPGAIO->COUNTER;
204#else /* MPS3_PLATFORM */
205 current = 0; /* No RTC implementation available. */
206#endif /* MPS3_PLATFORM */
207
208 if (timer != NULL) {
209 *timer = current;
210 }
211
212 return (current);
213}
214
215#else /* #if defined(__ARMCC_VERSION) && (__ARMCC_VERSION >= 6010050) */
216
217/******************************************************************************/
218/* Retarget functions for GNU Tools for ARM Embedded Processors */
219/******************************************************************************/
220#include <stdio.h>
221#include <sys/stat.h>
222
223extern unsigned char UartPutc(unsigned char my_ch);
224
225__attribute__((used)) int _write(int fd, char *ptr, int len)
226{
227 size_t i;
228 for (i = 0; i < len; i++)
229 {
230 UartPutc(ptr[i]); /* call character output function. */
231 }
232 return len;
233}
234
235#endif /* #if defined(__ARMCC_VERSION) && (__ARMCC_VERSION >= 6010050) */