1 /*********************************************************************
2 *                    SEGGER Microcontroller GmbH                     *
3 *       Solutions for real time microcontroller applications         *
4 **********************************************************************
5 *                                                                    *
6 *            (c) 1995 - 2018 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 * conditions are met:                                                *
25 *                                                                    *
26 * o Redistributions of source code must retain the above copyright   *
27 *   notice, this list of conditions and the following disclaimer.    *
28 *                                                                    *
29 * o Redistributions in binary form must reproduce the above          *
30 *   copyright notice, this list of conditions and the following      *
31 *   disclaimer in the documentation and/or other materials provided  *
32 *   with the distribution.                                           *
33 *                                                                    *
34 * o Neither the name of SEGGER Microcontroller GmbH         *
35 *   nor the names of its contributors may be used to endorse or      *
36 *   promote products derived from this software without specific     *
37 *   prior written permission.                                        *
38 *                                                                    *
39 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND             *
40 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,        *
41 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF           *
42 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE           *
43 * DISCLAIMED. IN NO EVENT SHALL SEGGER Microcontroller BE LIABLE FOR *
44 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR           *
45 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT  *
46 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;    *
47 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF      *
48 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT          *
49 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE  *
50 * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH   *
51 * DAMAGE.                                                            *
52 *                                                                    *
53 **********************************************************************
54 ---------------------------END-OF-HEADER------------------------------
55 File    : RTT_Syscalls_KEIL.c
56 Purpose : Retargeting module for KEIL MDK-CM3.
57           Low-level functions for using printf() via RTT
58 Revision: $Rev: 9599 $
59 ----------------------------------------------------------------------
60 */
61 #ifdef __CC_ARM
62 
63 #include <stdio.h>
64 #include <stdlib.h>
65 #include <string.h>
66 #include <rt_sys.h>
67 #include <rt_misc.h>
68 
69 #include "SEGGER_RTT.h"
70 /*********************************************************************
71 *
72 *       #pragmas
73 *
74 **********************************************************************
75 */
76 #pragma import(__use_no_semihosting)
77 
78 #ifdef _MICROLIB
79   #pragma import(__use_full_stdio)
80 #endif
81 
82 /*********************************************************************
83 *
84 *       Defines non-configurable
85 *
86 **********************************************************************
87 */
88 
89 /* Standard IO device handles - arbitrary, but any real file system handles must be
90    less than 0x8000. */
91 #define STDIN             0x8001    // Standard Input Stream
92 #define STDOUT            0x8002    // Standard Output Stream
93 #define STDERR            0x8003    // Standard Error Stream
94 
95 /*********************************************************************
96 *
97 *       Public const
98 *
99 **********************************************************************
100 */
101 //const char __stdin_name[]  = "STDIN";
102 const char __stdout_name[] = "STDOUT";
103 const char __stderr_name[] = "STDERR";
104 
105 /*********************************************************************
106 *
107 *       Public code
108 *
109 **********************************************************************
110 */
111 
112 /*********************************************************************
113 *
114 *       _ttywrch
115 *
116 *  Function description:
117 *    Outputs a character to the console
118 *
119 *  Parameters:
120 *    c    - character to output
121 *
122 */
_ttywrch(int c)123 void _ttywrch(int c) {
124   fputc(c, stdout); // stdout
125   fflush(stdout);
126 }
127 
128 /*********************************************************************
129 *
130 *       _sys_open
131 *
132 *  Function description:
133 *    Opens the device/file in order to do read/write operations
134 *
135 *  Parameters:
136 *    sName        - sName of the device/file to open
137 *    OpenMode    - This parameter is currently ignored
138 *
139 *  Return value:
140 *    != 0     - Handle to the object to open, otherwise
141 *    == 0     -"device" is not handled by this module
142 *
143 */
_sys_open(const char * sName,int OpenMode)144 FILEHANDLE _sys_open(const char * sName, int OpenMode) {
145   (void)OpenMode;
146   // Register standard Input Output devices.
147   if (strcmp(sName, __stdout_name) == 0) {
148     return (STDOUT);
149   } else if (strcmp(sName, __stderr_name) == 0) {
150     return (STDERR);
151   } else
152   return (0);  // Not implemented
153 }
154 
155 /*********************************************************************
156 *
157 *       _sys_close
158 *
159 *  Function description:
160 *    Closes the handle to the open device/file
161 *
162 *  Parameters:
163 *    hFile    - Handle to a file opened via _sys_open
164 *
165 *  Return value:
166 *    0     - device/file closed
167 *
168 */
_sys_close(FILEHANDLE hFile)169 int _sys_close(FILEHANDLE hFile) {
170   (void)hFile;
171   return 0;  // Not implemented
172 }
173 
174 /*********************************************************************
175 *
176 *       _sys_write
177 *
178 *  Function description:
179 *    Writes the data to an open handle.
180 *    Currently this function only outputs data to the console
181 *
182 *  Parameters:
183 *    hFile    - Handle to a file opened via _sys_open
184 *    pBuffer  - Pointer to the data that shall be written
185 *    NumBytes      - Number of bytes to write
186 *    Mode     - The Mode that shall be used
187 *
188 *  Return value:
189 *    Number of bytes *not* written to the file/device
190 *
191 */
_sys_write(FILEHANDLE hFile,const unsigned char * pBuffer,unsigned NumBytes,int Mode)192 int _sys_write(FILEHANDLE hFile, const unsigned char * pBuffer, unsigned NumBytes, int Mode) {
193   int r = 0;
194 
195   (void)Mode;
196   if (hFile == STDOUT) {
197     return NumBytes - SEGGER_RTT_Write(0, (const char*)pBuffer, NumBytes);
198   }
199   return r;
200 }
201 
202 /*********************************************************************
203 *
204 *       _sys_read
205 *
206 *  Function description:
207 *    Reads data from an open handle.
208 *    Currently this modules does nothing.
209 *
210 *  Parameters:
211 *    hFile    - Handle to a file opened via _sys_open
212 *    pBuffer  - Pointer to buffer to store the read data
213 *    NumBytes      - Number of bytes to read
214 *    Mode     - The Mode that shall be used
215 *
216 *  Return value:
217 *    Number of bytes read from the file/device
218 *
219 */
_sys_read(FILEHANDLE hFile,unsigned char * pBuffer,unsigned NumBytes,int Mode)220 int _sys_read(FILEHANDLE hFile, unsigned char * pBuffer, unsigned NumBytes, int Mode) {
221   (void)hFile;
222   (void)pBuffer;
223   (void)NumBytes;
224   (void)Mode;
225   return (0);  // Not implemented
226 }
227 
228 /*********************************************************************
229 *
230 *       _sys_istty
231 *
232 *  Function description:
233 *    This function shall return whether the opened file
234 *    is a console device or not.
235 *
236 *  Parameters:
237 *    hFile    - Handle to a file opened via _sys_open
238 *
239 *  Return value:
240 *    1       - Device is     a console
241 *    0       - Device is not a console
242 *
243 */
_sys_istty(FILEHANDLE hFile)244 int _sys_istty(FILEHANDLE hFile) {
245   if (hFile > 0x8000) {
246     return (1);
247   }
248   return (0);  // Not implemented
249 }
250 
251 /*********************************************************************
252 *
253 *       _sys_seek
254 *
255 *  Function description:
256 *    Seeks via the file to a specific position
257 *
258 *  Parameters:
259 *    hFile  - Handle to a file opened via _sys_open
260 *    Pos    -
261 *
262 *  Return value:
263 *    int       -
264 *
265 */
_sys_seek(FILEHANDLE hFile,long Pos)266 int _sys_seek(FILEHANDLE hFile, long Pos) {
267   (void)hFile;
268   (void)Pos;
269   return (0);  // Not implemented
270 }
271 
272 /*********************************************************************
273 *
274 *       _sys_ensure
275 *
276 *  Function description:
277 *
278 *
279 *  Parameters:
280 *    hFile    - Handle to a file opened via _sys_open
281 *
282 *  Return value:
283 *    int       -
284 *
285 */
_sys_ensure(FILEHANDLE hFile)286 int _sys_ensure(FILEHANDLE hFile) {
287   (void)hFile;
288   return (-1);  // Not implemented
289 }
290 
291 /*********************************************************************
292 *
293 *       _sys_flen
294 *
295 *  Function description:
296 *    Returns the length of the opened file handle
297 *
298 *  Parameters:
299 *    hFile    - Handle to a file opened via _sys_open
300 *
301 *  Return value:
302 *    Length of the file
303 *
304 */
_sys_flen(FILEHANDLE hFile)305 long _sys_flen(FILEHANDLE hFile) {
306   (void)hFile;
307   return (0);  // Not implemented
308 }
309 
310 /*********************************************************************
311 *
312 *       _sys_tmpnam
313 *
314 *  Function description:
315 *    This function converts the file number fileno for a temporary
316 *    file to a unique filename, for example, tmp0001.
317 *
318 *  Parameters:
319 *    pBuffer    - Pointer to a buffer to store the name
320 *    FileNum    - file number to convert
321 *    MaxLen     - Size of the buffer
322 *
323 *  Return value:
324 *     1 - Error
325 *     0 - Success
326 *
327 */
_sys_tmpnam(char * pBuffer,int FileNum,unsigned MaxLen)328 int _sys_tmpnam(char * pBuffer, int FileNum, unsigned MaxLen) {
329   (void)pBuffer;
330   (void)FileNum;
331   (void)MaxLen;
332   return (1);  // Not implemented
333 }
334 
335 /*********************************************************************
336 *
337 *       _sys_command_string
338 *
339 *  Function description:
340 *    This function shall execute a system command.
341 *
342 *  Parameters:
343 *    cmd    - Pointer to the command string
344 *    len    - Length of the string
345 *
346 *  Return value:
347 *    == NULL - Command was not successfully executed
348 *    == sCmd - Command was passed successfully
349 *
350 */
_sys_command_string(char * cmd,int len)351 char * _sys_command_string(char * cmd, int len) {
352   (void)len;
353   return cmd;  // Not implemented
354 }
355 
356 /*********************************************************************
357 *
358 *       _sys_exit
359 *
360 *  Function description:
361 *    This function is called when the application returns from main
362 *
363 *  Parameters:
364 *    ReturnCode    - Return code from the main function
365 *
366 *
367 */
_sys_exit(int ReturnCode)368 void _sys_exit(int ReturnCode) {
369   (void)ReturnCode;
370   while (1);  // Not implemented
371 }
372 
373 #endif
374 /*************************** End of file ****************************/
375