1 /*********************************************************************
2 * SEGGER Microcontroller GmbH *
3 * The Embedded Experts *
4 **********************************************************************
5 * *
6 * (c) 1995 - 2021 SEGGER Microcontroller GmbH *
7 * *
8 * www.segger.com Support: support@segger.com *
9 * *
10 **********************************************************************
11 * *
12 * SEGGER RTT * Real Time Transfer for embedded targets *
13 * *
14 **********************************************************************
15 * *
16 * All rights reserved. *
17 * *
18 * SEGGER strongly recommends to not make any changes *
19 * to or modify the source code of this software in order to stay *
20 * compatible with the RTT protocol and J-Link. *
21 * *
22 * Redistribution and use in source and binary forms, with or *
23 * without modification, are permitted provided that the following *
24 * condition is met: *
25 * *
26 * o Redistributions of source code must retain the above copyright *
27 * notice, this condition and the following disclaimer. *
28 * *
29 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND *
30 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, *
31 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF *
32 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE *
33 * DISCLAIMED. IN NO EVENT SHALL SEGGER Microcontroller BE LIABLE FOR *
34 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR *
35 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT *
36 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; *
37 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF *
38 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT *
39 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE *
40 * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH *
41 * DAMAGE. *
42 * *
43 **********************************************************************
44 * *
45 * RTT version: 7.22 *
46 * *
47 **********************************************************************
48
49 ---------------------------END-OF-HEADER------------------------------
50 File : SEGGER_RTT_Syscalls_GCC.c
51 Purpose : Low-level functions for using printf() via RTT in GCC.
52 To use RTT for printf output, include this file in your
53 application.
54 Revision: $Rev: 20755 $
55 ----------------------------------------------------------------------
56 */
57
58 /*
59 * This is to be compatible with other MSDK debug_console.
60 *
61 * When SDK_DEBUGCONSOLE_UART is defined, the toolchain IO functions like
62 * printf, scanf are redirected to RTT.
63 */
64 #if defined(SDK_DEBUGCONSOLE_UART)
65 #if (defined __GNUC__) && !(defined __SES_ARM) && !(defined __CROSSWORKS_ARM) && !(defined __ARMCC_VERSION) && !(defined __CC_ARM)
66
67 #include <reent.h> // required for _write_r
68 #include "SEGGER_RTT.h"
69
70
71 /*********************************************************************
72 *
73 * Types
74 *
75 **********************************************************************
76 */
77 //
78 // If necessary define the _reent struct
79 // to match the one passed by the used standard library.
80 //
81 struct _reent;
82
83 /*********************************************************************
84 *
85 * Function prototypes
86 *
87 **********************************************************************
88 */
89 _ssize_t _write (int file, const void *ptr, size_t len);
90 _ssize_t _write_r(struct _reent *r, int file, const void *ptr, size_t len);
91
92 /*********************************************************************
93 *
94 * Global functions
95 *
96 **********************************************************************
97 */
98
99 /*********************************************************************
100 *
101 * _write()
102 *
103 * Function description
104 * Low-level write function.
105 * libc subroutines will use this system routine for output to all files,
106 * including stdout.
107 * Write data via RTT.
108 */
_write(int file,const void * ptr,size_t len)109 _ssize_t _write(int file, const void *ptr, size_t len) {
110 (void) file; /* Not used, avoid warning */
111 SEGGER_RTT_Write(0, ptr, len);
112 return len;
113 }
114
115 /*********************************************************************
116 *
117 * _write_r()
118 *
119 * Function description
120 * Low-level reentrant write function.
121 * libc subroutines will use this system routine for output to all files,
122 * including stdout.
123 * Write data via RTT.
124 */
_write_r(struct _reent * r,int file,const void * ptr,size_t len)125 _ssize_t _write_r(struct _reent *r, int file, const void *ptr, size_t len) {
126 (void) file; /* Not used, avoid warning */
127 (void) r; /* Not used, avoid warning */
128 SEGGER_RTT_Write(0, ptr, len);
129 return len;
130 }
131
132 /*********************************************************************
133 *
134 * _read()
135 *
136 * Function description
137 * Low-level read function.
138 * Standard library subroutines will use this system routine
139 * for reading data via RTT.
140 */
_read(int handle,char * buffer,int size)141 _ssize_t _read(int handle, char *buffer, int size)
142 {
143 (void) handle; /* Not used, avoid warning */
144
145 for (int i = 0; i < bufSize; i++)
146 {
147 *buf = SEGGER_RTT_WaitKey() ;
148 buf++;
149 }
150
151 return bufSize;
152 }
153
154 /*********************************************************************
155 *
156 * __sys_write()
157 *
158 * Function description
159 * Low-level write function used by REDLIB.
160 */
__sys_write(int handle,char * buffer,int size)161 int __sys_write(int handle, char *buffer, int size)
162 {
163 if (NULL == buffer)
164 {
165 /* return -1 if error. */
166 return -1;
167 }
168
169 /* This function only writes to "standard out" and "standard err" for all other file handles it returns failure. */
170 if ((handle != 1) && (handle != 2))
171 {
172 return -1;
173 }
174
175 /* Send data. */
176 SEGGER_RTT_Write(0, (const char*)buffer, size);
177
178 return 0;
179 }
180
181 /*********************************************************************
182 *
183 * _read()
184 *
185 * Function description
186 * Low-level read function, used by REDLIB.
187 * Standard library subroutines will use this system routine
188 * for reading data via RTT.
189 */
__sys_readc(void)190 int __sys_readc(void)
191 {
192 return SEGGER_RTT_WaitKey() ;
193 }
194
195 #endif
196
197 #endif /* SDK_DEBUGCONSOLE_UART */
198 /****** End Of File *************************************************/
199