VSF Documented
__retarget_io.c
Go to the documentation of this file.
1/*****************************************************************************
2 * Copyright(C)2009-2022 by VSF Team *
3 * *
4 * Licensed under the Apache License, Version 2.0 (the "License"); *
5 * you may not use this file except in compliance with the License. *
6 * You may obtain a copy of the License at *
7 * *
8 * http://www.apache.org/licenses/LICENSE-2.0 *
9 * *
10 * Unless required by applicable law or agreed to in writing, software *
11 * distributed under the License is distributed on an "AS IS" BASIS, *
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. *
13 * See the License for the specific language governing permissions and *
14 * limitations under the License. *
15 * *
16 ****************************************************************************/
17
18#ifdef __USE_COMMON_RETARGET_IO_C__
19#undef __USE_COMMON_RETARGET_IO_C__
20
21#include <stdio.h>
24// for vsf_trace only
26
27#ifndef VSF_UNUSED_PARAM
28# define VSF_UNUSED_PARAM(__VAL) (void)(__VAL)
29#endif
30
31#if VSF_USE_POSIX == ENABLED
32#include <sys/types.h>
33#include <sys/stat.h>
34#include <unistd.h>
35
36void vsf_stdio_init(void)
37{
38
39}
40
41VSF_CAL_SECTION(".vsf.utilities.stdio.__vsf_stdio_write")
42size_t __vsf_stdio_write(int handle, const unsigned char *buf, size_t buf_size)
43{
44 return write(handle, (void *)buf, buf_size);
45}
46
47VSF_CAL_SECTION(".vsf.utilities.stdio.iar.__vsf_stdio_read")
48size_t __vsf_stdio_read(int handle, unsigned char *buf, size_t buf_size)
49{
50 return read(handle, buf, buf_size);
51}
52
53#else
54
55extern int vsf_stdout_putchar(char ch);
56extern int vsf_stderr_putchar(char ch);
57
58extern void vsf_stdout_init(void);
59extern void vsf_stdin_init(void);
60
61VSF_CAL_WEAK(vsf_stdin_getchar)
62int vsf_stdin_getchar(void)
63{
64 while(1);
65}
66
67VSF_CAL_WEAK(vsf_stdout_putchar)
68int vsf_stdout_putchar(char ch)
69{
70 return 0;
71}
72
73VSF_CAL_WEAK(vsf_stderr_putchar)
74int vsf_stderr_putchar(char ch)
75{
76 return vsf_stdout_putchar(ch);
77}
78
79void vsf_stdio_init(void)
80{
83}
84
85VSF_CAL_SECTION(".vsf.utilities.stdio.__vsf_stdio_write")
86size_t __vsf_stdio_write(int handle, const unsigned char *buf, size_t buf_size)
87{
88 size_t nChars = 0;
89 /* Check for the command to flush all handles */
90 if (handle == -1) {
91 return 0;
92 }
93 /* Check for stdout and stderr
94 (only necessary if FILE descriptors are enabled.) */
95 if (handle != 1 && handle != 2) {
96 return 0;
97 }
98 for (/* Empty */; buf_size > 0; --buf_size) {
99 vsf_stdout_putchar(*buf++);
100 ++nChars;
101 }
102 return nChars;
103}
104
105VSF_CAL_SECTION(".vsf.utilities.stdio.__vsf_stdio_read")
106size_t __vsf_stdio_read(int handle, unsigned char *buf, size_t buf_size)
107{
108 size_t nChars = 0;
109 /* Check for stdin
110 (only necessary if FILE descriptors are enabled) */
111 if (handle != 0) {
112 return 0;
113 }
114 for (/*Empty*/; buf_size > 0; --buf_size) {
115 uint8_t c = vsf_stdin_getchar();
116 if (c == 0) { break; }
117 *buf++ = c;
118 ++nChars;
119 }
120 return nChars;
121}
122
123#endif
124
125#if __IS_COMPILER_IAR__
126# define __USE_COMMON_RETARGET_IO_IAR_C__
128#elif __IS_COMPILER_GCC__ || __IS_COMPILER_LLVM__
129# define __USE_COMMON_RETARGET_IO_GCC_LLVM_C__
131#endif
132
133#endif //__VSF_USE_COMMON_RETARGET_IO_C__
#define VSF_CAL_SECTION(__SEC)
Definition __compiler.h:181
#define vsf_stdio_init(...)
Definition default_compiler.h:53
unsigned int size_t
Definition types.h:51
unsigned char uint8_t
Definition stdint.h:5
#define read
Definition unistd.h:122
#define write
Definition unistd.h:123
void vsf_stdout_init(void)
Definition vsf_trace.c:455
void vsf_stdin_init(void)
Definition vsf_trace.c:467
int vsf_stderr_putchar(char ch)
Definition vsf_trace.c:479
int vsf_stdout_putchar(char ch)
Definition vsf_trace.c:473