xref: /btstack/3rd-party/segger-rtt/SEGGER_RTT_Syscalls_KEIL.c (revision ce6f85e79d1d141c1b45dfa16b2671762457cbb4)
1 /*********************************************************************
2 *                    SEGGER Microcontroller GmbH                     *
3 *                        The Embedded Experts                        *
4 **********************************************************************
5 *                                                                    *
6 *            (c) 1995 - 2019 SEGGER Microcontroller GmbH             *
7 *                                                                    *
8 *       www.segger.com     Support: [email protected]               *
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 ---------------------------END-OF-HEADER------------------------------
45 File    : RTT_Syscalls_KEIL.c
46 Purpose : Retargeting module for KEIL MDK-CM3.
47           Low-level functions for using printf() via RTT
48 Revision: $Rev: 24316 $
49 Notes   : (1) https://wiki.segger.com/Keil_MDK-ARM#RTT_in_uVision
50 ----------------------------------------------------------------------
51 */
52 #if (defined __CC_ARM) || (defined __ARMCC_VERSION)
53 
54 #include <stdio.h>
55 #include <stdlib.h>
56 #include <string.h>
57 #include <rt_sys.h>
58 #include <rt_misc.h>
59 
60 #include "SEGGER_RTT.h"
61 /*********************************************************************
62 *
63 *       #pragmas
64 *
65 **********************************************************************
66 */
67 #if __ARMCC_VERSION < 6000000
68 #pragma import(__use_no_semihosting)
69 #endif
70 
71 #ifdef _MICROLIB
72   #pragma import(__use_full_stdio)
73 #endif
74 
75 /*********************************************************************
76 *
77 *       Defines non-configurable
78 *
79 **********************************************************************
80 */
81 
82 /* Standard IO device handles - arbitrary, but any real file system handles must be
83    less than 0x8000. */
84 #define STDIN             0x8001    // Standard Input Stream
85 #define STDOUT            0x8002    // Standard Output Stream
86 #define STDERR            0x8003    // Standard Error Stream
87 
88 /*********************************************************************
89 *
90 *       Public const
91 *
92 **********************************************************************
93 */
94 #if __ARMCC_VERSION < 5000000
95 //const char __stdin_name[]  = "STDIN";
96 const char __stdout_name[] = "STDOUT";
97 const char __stderr_name[] = "STDERR";
98 #endif
99 
100 /*********************************************************************
101 *
102 *       Public code
103 *
104 **********************************************************************
105 */
106 
107 /*********************************************************************
108 *
109 *       _ttywrch
110 *
111 *  Function description:
112 *    Outputs a character to the console
113 *
114 *  Parameters:
115 *    c    - character to output
116 *
117 */
_ttywrch(int c)118 void _ttywrch(int c) {
119   fputc(c, stdout); // stdout
120   fflush(stdout);
121 }
122 
123 /*********************************************************************
124 *
125 *       _sys_open
126 *
127 *  Function description:
128 *    Opens the device/file in order to do read/write operations
129 *
130 *  Parameters:
131 *    sName        - sName of the device/file to open
132 *    OpenMode    - This parameter is currently ignored
133 *
134 *  Return value:
135 *    != 0     - Handle to the object to open, otherwise
136 *    == 0     -"device" is not handled by this module
137 *
138 */
_sys_open(const char * sName,int OpenMode)139 FILEHANDLE _sys_open(const char * sName, int OpenMode) {
140   (void)OpenMode;
141   // Register standard Input Output devices.
142   if (strcmp(sName, __stdout_name) == 0) {
143     return (STDOUT);
144   } else if (strcmp(sName, __stderr_name) == 0) {
145     return (STDERR);
146   } else
147   return (0);  // Not implemented
148 }
149 
150 /*********************************************************************
151 *
152 *       _sys_close
153 *
154 *  Function description:
155 *    Closes the handle to the open device/file
156 *
157 *  Parameters:
158 *    hFile    - Handle to a file opened via _sys_open
159 *
160 *  Return value:
161 *    0     - device/file closed
162 *
163 */
_sys_close(FILEHANDLE hFile)164 int _sys_close(FILEHANDLE hFile) {
165   (void)hFile;
166   return 0;  // Not implemented
167 }
168 
169 /*********************************************************************
170 *
171 *       _sys_write
172 *
173 *  Function description:
174 *    Writes the data to an open handle.
175 *    Currently this function only outputs data to the console
176 *
177 *  Parameters:
178 *    hFile    - Handle to a file opened via _sys_open
179 *    pBuffer  - Pointer to the data that shall be written
180 *    NumBytes      - Number of bytes to write
181 *    Mode     - The Mode that shall be used
182 *
183 *  Return value:
184 *    Number of bytes *not* written to the file/device
185 *
186 */
_sys_write(FILEHANDLE hFile,const unsigned char * pBuffer,unsigned NumBytes,int Mode)187 int _sys_write(FILEHANDLE hFile, const unsigned char * pBuffer, unsigned NumBytes, int Mode) {
188   int r = 0;
189 
190   (void)Mode;
191   if (hFile == STDOUT) {
192     SEGGER_RTT_Write(0, (const char*)pBuffer, NumBytes);
193 		return 0;
194   }
195   return r;
196 }
197 
198 /*********************************************************************
199 *
200 *       _sys_read
201 *
202 *  Function description:
203 *    Reads data from an open handle.
204 *    Currently this modules does nothing.
205 *
206 *  Parameters:
207 *    hFile    - Handle to a file opened via _sys_open
208 *    pBuffer  - Pointer to buffer to store the read data
209 *    NumBytes      - Number of bytes to read
210 *    Mode     - The Mode that shall be used
211 *
212 *  Return value:
213 *    Number of bytes read from the file/device
214 *
215 */
_sys_read(FILEHANDLE hFile,unsigned char * pBuffer,unsigned NumBytes,int Mode)216 int _sys_read(FILEHANDLE hFile, unsigned char * pBuffer, unsigned NumBytes, int Mode) {
217   (void)hFile;
218   (void)pBuffer;
219   (void)NumBytes;
220   (void)Mode;
221   return (0);  // Not implemented
222 }
223 
224 /*********************************************************************
225 *
226 *       _sys_istty
227 *
228 *  Function description:
229 *    This function shall return whether the opened file
230 *    is a console device or not.
231 *
232 *  Parameters:
233 *    hFile    - Handle to a file opened via _sys_open
234 *
235 *  Return value:
236 *    1       - Device is     a console
237 *    0       - Device is not a console
238 *
239 */
_sys_istty(FILEHANDLE hFile)240 int _sys_istty(FILEHANDLE hFile) {
241   if (hFile > 0x8000) {
242     return (1);
243   }
244   return (0);  // Not implemented
245 }
246 
247 /*********************************************************************
248 *
249 *       _sys_seek
250 *
251 *  Function description:
252 *    Seeks via the file to a specific position
253 *
254 *  Parameters:
255 *    hFile  - Handle to a file opened via _sys_open
256 *    Pos    -
257 *
258 *  Return value:
259 *    int       -
260 *
261 */
_sys_seek(FILEHANDLE hFile,long Pos)262 int _sys_seek(FILEHANDLE hFile, long Pos) {
263   (void)hFile;
264   (void)Pos;
265   return (0);  // Not implemented
266 }
267 
268 /*********************************************************************
269 *
270 *       _sys_ensure
271 *
272 *  Function description:
273 *
274 *
275 *  Parameters:
276 *    hFile    - Handle to a file opened via _sys_open
277 *
278 *  Return value:
279 *    int       -
280 *
281 */
_sys_ensure(FILEHANDLE hFile)282 int _sys_ensure(FILEHANDLE hFile) {
283   (void)hFile;
284   return (-1);  // Not implemented
285 }
286 
287 /*********************************************************************
288 *
289 *       _sys_flen
290 *
291 *  Function description:
292 *    Returns the length of the opened file handle
293 *
294 *  Parameters:
295 *    hFile    - Handle to a file opened via _sys_open
296 *
297 *  Return value:
298 *    Length of the file
299 *
300 */
_sys_flen(FILEHANDLE hFile)301 long _sys_flen(FILEHANDLE hFile) {
302   (void)hFile;
303   return (0);  // Not implemented
304 }
305 
306 /*********************************************************************
307 *
308 *       _sys_tmpnam
309 *
310 *  Function description:
311 *    This function converts the file number fileno for a temporary
312 *    file to a unique filename, for example, tmp0001.
313 *
314 *  Parameters:
315 *    pBuffer    - Pointer to a buffer to store the name
316 *    FileNum    - file number to convert
317 *    MaxLen     - Size of the buffer
318 *
319 *  Return value:
320 *     1 - Error
321 *     0 - Success
322 *
323 */
_sys_tmpnam(char * pBuffer,int FileNum,unsigned MaxLen)324 int _sys_tmpnam(char * pBuffer, int FileNum, unsigned MaxLen) {
325   (void)pBuffer;
326   (void)FileNum;
327   (void)MaxLen;
328   return (1);  // Not implemented
329 }
330 
331 /*********************************************************************
332 *
333 *       _sys_command_string
334 *
335 *  Function description:
336 *    This function shall execute a system command.
337 *
338 *  Parameters:
339 *    cmd    - Pointer to the command string
340 *    len    - Length of the string
341 *
342 *  Return value:
343 *    == NULL - Command was not successfully executed
344 *    == sCmd - Command was passed successfully
345 *
346 */
_sys_command_string(char * cmd,int len)347 char * _sys_command_string(char * cmd, int len) {
348   (void)len;
349   return cmd;  // Not implemented
350 }
351 
352 /*********************************************************************
353 *
354 *       _sys_exit
355 *
356 *  Function description:
357 *    This function is called when the application returns from main
358 *
359 *  Parameters:
360 *    ReturnCode    - Return code from the main function
361 *
362 *
363 */
_sys_exit(int ReturnCode)364 void _sys_exit(int ReturnCode) {
365   (void)ReturnCode;
366   while (1);  // Not implemented
367 }
368 
369 #if __ARMCC_VERSION >= 5000000
370 /*********************************************************************
371 *
372 *       stdout_putchar
373 *
374 *  Function description:
375 *    Put a character to the stdout
376 *
377 *  Parameters:
378 *    ch    - Character to output
379 *
380 *
381 */
stdout_putchar(int ch)382 int stdout_putchar(int ch) {
383   (void)ch;
384   return ch;  // Not implemented
385 }
386 #endif
387 
388 #endif
389 /*************************** End of file ****************************/
390