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) && \
66     !(defined __CC_ARM)
67 
68 #include <reent.h> // required for _write_r
69 #include "SEGGER_RTT.h"
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 {
111     (void)file; /* Not used, avoid warning */
112     SEGGER_RTT_Write(0, ptr, len);
113     return len;
114 }
115 
116 /*********************************************************************
117  *
118  *       _write_r()
119  *
120  * Function description
121  *   Low-level reentrant write function.
122  *   libc subroutines will use this system routine for output to all files,
123  *   including stdout.
124  *   Write data via RTT.
125  */
_write_r(struct _reent * r,int file,const void * ptr,size_t len)126 _ssize_t _write_r(struct _reent *r, int file, const void *ptr, size_t len)
127 {
128     (void)file; /* Not used, avoid warning */
129     (void)r;    /* Not used, avoid warning */
130     SEGGER_RTT_Write(0, ptr, len);
131     return len;
132 }
133 
134 /*********************************************************************
135  *
136  *       _read()
137  *
138  * Function description
139  *   Low-level read function.
140  *   Standard library subroutines will use this system routine
141  *   for reading data via RTT.
142  */
_read(int handle,char * buffer,int size)143 _ssize_t _read(int handle, char *buffer, int size)
144 {
145     (void)handle; /* Not used, avoid warning */
146 
147     for (int i = 0; i < bufSize; i++)
148     {
149         *buf = SEGGER_RTT_WaitKey();
150         buf++;
151     }
152 
153     return bufSize;
154 }
155 
156 /*********************************************************************
157  *
158  *       __sys_write()
159  *
160  * Function description
161  *   Low-level write function used by REDLIB.
162  */
__sys_write(int handle,char * buffer,int size)163 int __sys_write(int handle, char *buffer, int size)
164 {
165     if (NULL == buffer)
166     {
167         /* return -1 if error. */
168         return -1;
169     }
170 
171     /* This function only writes to "standard out" and "standard err" for all other file handles it returns failure. */
172     if ((handle != 1) && (handle != 2))
173     {
174         return -1;
175     }
176 
177     /* Send data. */
178     SEGGER_RTT_Write(0, (const char *)buffer, size);
179 
180     return 0;
181 }
182 
183 /*********************************************************************
184  *
185  *       _read()
186  *
187  * Function description
188  *   Low-level read function, used by REDLIB.
189  *   Standard library subroutines will use this system routine
190  *   for reading data via RTT.
191  */
__sys_readc(void)192 int __sys_readc(void)
193 {
194     return SEGGER_RTT_WaitKey();
195 }
196 
197 #endif
198 
199 #endif /* SDK_DEBUGCONSOLE_UART */
200 /****** End Of File *************************************************/
201