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 SystemView * Real-time application analysis           *
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 SystemView and 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 *       SystemView version: 3.30                                    *
46 *                                                                    *
47 **********************************************************************
48 ---------------------------END-OF-HEADER------------------------------
49 File    : RTT_Syscalls_KEIL.c
50 Purpose : Retargeting module for KEIL MDK-CM3.
51           Low-level functions for using printf() via RTT
52 Revision: $Rev: 20754 $
53 Notes   : (1) https://wiki.segger.com/Keil_MDK-ARM#RTT_in_uVision
54 ----------------------------------------------------------------------
55 */
56 #if (defined __CC_ARM) || (defined __ARMCC_VERSION)
57 
58 #include <stdio.h>
59 #include <stdlib.h>
60 #include <string.h>
61 #include <rt_sys.h>
62 #include <rt_misc.h>
63 
64 #include "SEGGER_RTT.h"
65 /*********************************************************************
66 *
67 *       #pragmas
68 *
69 **********************************************************************
70 */
71 #if __ARMCC_VERSION < 6000000
72 #pragma import(__use_no_semihosting)
73 #endif
74 
75 #ifdef _MICROLIB
76   #pragma import(__use_full_stdio)
77 #endif
78 
79 /*********************************************************************
80 *
81 *       Defines non-configurable
82 *
83 **********************************************************************
84 */
85 
86 /* Standard IO device handles - arbitrary, but any real file system handles must be
87    less than 0x8000. */
88 #define STDIN             0x8001    // Standard Input Stream
89 #define STDOUT            0x8002    // Standard Output Stream
90 #define STDERR            0x8003    // Standard Error Stream
91 
92 /*********************************************************************
93 *
94 *       Public const
95 *
96 **********************************************************************
97 */
98 #if __ARMCC_VERSION < 5000000
99 //const char __stdin_name[]  = "STDIN";
100 const char __stdout_name[] = "STDOUT";
101 const char __stderr_name[] = "STDERR";
102 #endif
103 
104 /*********************************************************************
105 *
106 *       Public code
107 *
108 **********************************************************************
109 */
110 
111 /*********************************************************************
112 *
113 *       _ttywrch
114 *
115 *  Function description:
116 *    Outputs a character to the console
117 *
118 *  Parameters:
119 *    c    - character to output
120 *
121 */
_ttywrch(int c)122 void _ttywrch(int c) {
123   fputc(c, stdout); // stdout
124   fflush(stdout);
125 }
126 
127 /*********************************************************************
128 *
129 *       _sys_open
130 *
131 *  Function description:
132 *    Opens the device/file in order to do read/write operations
133 *
134 *  Parameters:
135 *    sName        - sName of the device/file to open
136 *    OpenMode    - This parameter is currently ignored
137 *
138 *  Return value:
139 *    != 0     - Handle to the object to open, otherwise
140 *    == 0     -"device" is not handled by this module
141 *
142 */
_sys_open(const char * sName,int OpenMode)143 FILEHANDLE _sys_open(const char * sName, int OpenMode) {
144   (void)OpenMode;
145   // Register standard Input Output devices.
146   if (strcmp(sName, __stdout_name) == 0) {
147     return (STDOUT);
148   } else if (strcmp(sName, __stderr_name) == 0) {
149     return (STDERR);
150   } else
151   return (0);  // Not implemented
152 }
153 
154 /*********************************************************************
155 *
156 *       _sys_close
157 *
158 *  Function description:
159 *    Closes the handle to the open device/file
160 *
161 *  Parameters:
162 *    hFile    - Handle to a file opened via _sys_open
163 *
164 *  Return value:
165 *    0     - device/file closed
166 *
167 */
_sys_close(FILEHANDLE hFile)168 int _sys_close(FILEHANDLE hFile) {
169   (void)hFile;
170   return 0;  // Not implemented
171 }
172 
173 /*********************************************************************
174 *
175 *       _sys_write
176 *
177 *  Function description:
178 *    Writes the data to an open handle.
179 *    Currently this function only outputs data to the console
180 *
181 *  Parameters:
182 *    hFile    - Handle to a file opened via _sys_open
183 *    pBuffer  - Pointer to the data that shall be written
184 *    NumBytes      - Number of bytes to write
185 *    Mode     - The Mode that shall be used
186 *
187 *  Return value:
188 *    Number of bytes *not* written to the file/device
189 *
190 */
_sys_write(FILEHANDLE hFile,const unsigned char * pBuffer,unsigned NumBytes,int Mode)191 int _sys_write(FILEHANDLE hFile, const unsigned char * pBuffer, unsigned NumBytes, int Mode) {
192   int r = 0;
193 
194   (void)Mode;
195   if (hFile == STDOUT) {
196     SEGGER_RTT_Write(0, (const char*)pBuffer, NumBytes);
197 		return 0;
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 #if __ARMCC_VERSION >= 5000000
374 /*********************************************************************
375 *
376 *       stdout_putchar
377 *
378 *  Function description:
379 *    Put a character to the stdout
380 *
381 *  Parameters:
382 *    ch    - Character to output
383 *
384 *
385 */
stdout_putchar(int ch)386 int stdout_putchar(int ch) {
387   (void)ch;
388   return ch;  // Not implemented
389 }
390 #endif
391 
392 #endif
393 /*************************** End of file ****************************/
394