xref: /btstack/3rd-party/segger-rtt/SEGGER_RTT.h (revision d58a1b5f11ada8ddf896c41fff5a35e7f140c37e)
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 * 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    : SEGGER_RTT.h
56 Purpose : Implementation of SEGGER real-time transfer which allows
57           real-time communication on targets which support debugger
58           memory accesses while the CPU is running.
59 Revision: $Rev: 16714 $
60 ----------------------------------------------------------------------
61 */
62 
63 #ifndef SEGGER_RTT_H
64 #define SEGGER_RTT_H
65 
66 #include "SEGGER_RTT_Conf.h"
67 
68 
69 
70 /*********************************************************************
71 *
72 *       Defines, defaults
73 *
74 **********************************************************************
75 */
76 #ifndef RTT_USE_ASM
77   #if (defined __SES_ARM)                       // SEGGER Embedded Studio
78     #define _CC_HAS_RTT_ASM_SUPPORT 1
79   #elif (defined __CROSSWORKS_ARM)              // Rowley Crossworks
80     #define _CC_HAS_RTT_ASM_SUPPORT 1
81   #elif (defined __GNUC__)                      // GCC
82     #define _CC_HAS_RTT_ASM_SUPPORT 1
83   #elif (defined __clang__)                     // Clang compiler
84     #define _CC_HAS_RTT_ASM_SUPPORT 1
85   #elif (defined __IASMARM__)                   // IAR assembler
86     #define _CC_HAS_RTT_ASM_SUPPORT 1
87   #elif (defined __ICCARM__)                    // IAR compiler
88     #define _CC_HAS_RTT_ASM_SUPPORT 1
89   #else
90     #define _CC_HAS_RTT_ASM_SUPPORT 0
91   #endif
92   #if (defined __ARM_ARCH_7M__)                 // Cortex-M3/4
93     #define _CORE_HAS_RTT_ASM_SUPPORT 1
94   #elif (defined __ARM_ARCH_7EM__)              // Cortex-M7
95     #define _CORE_HAS_RTT_ASM_SUPPORT 1
96   #elif (defined __ARM_ARCH_8M_MAIN__)          // Cortex-M33
97     #define _CORE_HAS_RTT_ASM_SUPPORT 1
98   #elif (defined __ARM7M__)                     // IAR Cortex-M3/4
99     #if (__CORE__ == __ARM7M__)
100       #define _CORE_HAS_RTT_ASM_SUPPORT 1
101     #else
102       #define _CORE_HAS_RTT_ASM_SUPPORT 0
103     #endif
104   #elif (defined __ARM7EM__)                    // IAR Cortex-M7
105     #if (__CORE__ == __ARM7EM__)
106       #define _CORE_HAS_RTT_ASM_SUPPORT 1
107     #else
108       #define _CORE_HAS_RTT_ASM_SUPPORT 0
109     #endif
110   #else
111     #define _CORE_HAS_RTT_ASM_SUPPORT 0
112   #endif
113   //
114   // If IDE and core support the ASM version, enable ASM version by default
115   //
116   #if (_CC_HAS_RTT_ASM_SUPPORT && _CORE_HAS_RTT_ASM_SUPPORT)
117     #define RTT_USE_ASM                           (1)
118   #else
119     #define RTT_USE_ASM                           (0)
120   #endif
121 #endif
122 
123 #ifndef SEGGER_RTT_ASM  // defined when SEGGER_RTT.h is included from assembly file
124 #include <stdlib.h>
125 #include <stdarg.h>
126 
127 /*********************************************************************
128 *
129 *       Defines, fixed
130 *
131 **********************************************************************
132 */
133 
134 /*********************************************************************
135 *
136 *       Types
137 *
138 **********************************************************************
139 */
140 
141 //
142 // Description for a circular buffer (also called "ring buffer")
143 // which is used as up-buffer (T->H)
144 //
145 typedef struct {
146   const     char*    sName;         // Optional name. Standard names so far are: "Terminal", "SysView", "J-Scope_t4i4"
147             char*    pBuffer;       // Pointer to start of buffer
148             unsigned SizeOfBuffer;  // Buffer size in bytes. Note that one byte is lost, as this implementation does not fill up the buffer in order to avoid the problem of being unable to distinguish between full and empty.
149             unsigned WrOff;         // Position of next item to be written by either target.
150   volatile  unsigned RdOff;         // Position of next item to be read by host. Must be volatile since it may be modified by host.
151             unsigned Flags;         // Contains configuration flags
152 } SEGGER_RTT_BUFFER_UP;
153 
154 //
155 // Description for a circular buffer (also called "ring buffer")
156 // which is used as down-buffer (H->T)
157 //
158 typedef struct {
159   const     char*    sName;         // Optional name. Standard names so far are: "Terminal", "SysView", "J-Scope_t4i4"
160             char*    pBuffer;       // Pointer to start of buffer
161             unsigned SizeOfBuffer;  // Buffer size in bytes. Note that one byte is lost, as this implementation does not fill up the buffer in order to avoid the problem of being unable to distinguish between full and empty.
162   volatile  unsigned WrOff;         // Position of next item to be written by host. Must be volatile since it may be modified by host.
163             unsigned RdOff;         // Position of next item to be read by target (down-buffer).
164             unsigned Flags;         // Contains configuration flags
165 } SEGGER_RTT_BUFFER_DOWN;
166 
167 //
168 // RTT control block which describes the number of buffers available
169 // as well as the configuration for each buffer
170 //
171 //
172 typedef struct {
173   char                    acID[16];                                 // Initialized to "SEGGER RTT"
174   int                     MaxNumUpBuffers;                          // Initialized to SEGGER_RTT_MAX_NUM_UP_BUFFERS (type. 2)
175   int                     MaxNumDownBuffers;                        // Initialized to SEGGER_RTT_MAX_NUM_DOWN_BUFFERS (type. 2)
176   SEGGER_RTT_BUFFER_UP    aUp[SEGGER_RTT_MAX_NUM_UP_BUFFERS];       // Up buffers, transferring information up from target via debug probe to host
177   SEGGER_RTT_BUFFER_DOWN  aDown[SEGGER_RTT_MAX_NUM_DOWN_BUFFERS];   // Down buffers, transferring information down from host via debug probe to target
178 } SEGGER_RTT_CB;
179 
180 /*********************************************************************
181 *
182 *       Global data
183 *
184 **********************************************************************
185 */
186 extern SEGGER_RTT_CB _SEGGER_RTT;
187 
188 /*********************************************************************
189 *
190 *       RTT API functions
191 *
192 **********************************************************************
193 */
194 #ifdef __cplusplus
195   extern "C" {
196 #endif
197 int          SEGGER_RTT_AllocDownBuffer         (const char* sName, void* pBuffer, unsigned BufferSize, unsigned Flags);
198 int          SEGGER_RTT_AllocUpBuffer           (const char* sName, void* pBuffer, unsigned BufferSize, unsigned Flags);
199 int          SEGGER_RTT_ConfigUpBuffer          (unsigned BufferIndex, const char* sName, void* pBuffer, unsigned BufferSize, unsigned Flags);
200 int          SEGGER_RTT_ConfigDownBuffer        (unsigned BufferIndex, const char* sName, void* pBuffer, unsigned BufferSize, unsigned Flags);
201 int          SEGGER_RTT_GetKey                  (void);
202 unsigned     SEGGER_RTT_HasData                 (unsigned BufferIndex);
203 int          SEGGER_RTT_HasKey                  (void);
204 unsigned     SEGGER_RTT_HasDataUp               (unsigned BufferIndex);
205 void         SEGGER_RTT_Init                    (void);
206 unsigned     SEGGER_RTT_Read                    (unsigned BufferIndex,       void* pBuffer, unsigned BufferSize);
207 unsigned     SEGGER_RTT_ReadNoLock              (unsigned BufferIndex,       void* pData,   unsigned BufferSize);
208 int          SEGGER_RTT_SetNameDownBuffer       (unsigned BufferIndex, const char* sName);
209 int          SEGGER_RTT_SetNameUpBuffer         (unsigned BufferIndex, const char* sName);
210 int          SEGGER_RTT_SetFlagsDownBuffer      (unsigned BufferIndex, unsigned Flags);
211 int          SEGGER_RTT_SetFlagsUpBuffer        (unsigned BufferIndex, unsigned Flags);
212 int          SEGGER_RTT_WaitKey                 (void);
213 unsigned     SEGGER_RTT_Write                   (unsigned BufferIndex, const void* pBuffer, unsigned NumBytes);
214 unsigned     SEGGER_RTT_WriteNoLock             (unsigned BufferIndex, const void* pBuffer, unsigned NumBytes);
215 unsigned     SEGGER_RTT_WriteSkipNoLock         (unsigned BufferIndex, const void* pBuffer, unsigned NumBytes);
216 unsigned     SEGGER_RTT_ASM_WriteSkipNoLock     (unsigned BufferIndex, const void* pBuffer, unsigned NumBytes);
217 unsigned     SEGGER_RTT_WriteString             (unsigned BufferIndex, const char* s);
218 void         SEGGER_RTT_WriteWithOverwriteNoLock(unsigned BufferIndex, const void* pBuffer, unsigned NumBytes);
219 unsigned     SEGGER_RTT_PutChar                 (unsigned BufferIndex, char c);
220 unsigned     SEGGER_RTT_PutCharSkip             (unsigned BufferIndex, char c);
221 unsigned     SEGGER_RTT_PutCharSkipNoLock       (unsigned BufferIndex, char c);
222 
223 // BK addition
224 unsigned     SEGGER_RTT_GetAvailWriteSpace      (unsigned BufferIndex);
225 
226 
227 //
228 // Function macro for performance optimization
229 //
230 #define      SEGGER_RTT_HASDATA(n)       (_SEGGER_RTT.aDown[n].WrOff - _SEGGER_RTT.aDown[n].RdOff)
231 
232 #if RTT_USE_ASM
233   #define SEGGER_RTT_WriteSkipNoLock  SEGGER_RTT_ASM_WriteSkipNoLock
234 #endif
235 
236 /*********************************************************************
237 *
238 *       RTT "Terminal" API functions
239 *
240 **********************************************************************
241 */
242 int     SEGGER_RTT_SetTerminal        (unsigned char TerminalId);
243 int     SEGGER_RTT_TerminalOut        (unsigned char TerminalId, const char* s);
244 
245 /*********************************************************************
246 *
247 *       RTT printf functions (require SEGGER_RTT_printf.c)
248 *
249 **********************************************************************
250 */
251 int SEGGER_RTT_printf(unsigned BufferIndex, const char * sFormat, ...);
252 int SEGGER_RTT_vprintf(unsigned BufferIndex, const char * sFormat, va_list * pParamList);
253 
254 #ifdef __cplusplus
255   }
256 #endif
257 
258 #endif // ifndef(SEGGER_RTT_ASM)
259 
260 /*********************************************************************
261 *
262 *       Defines
263 *
264 **********************************************************************
265 */
266 
267 //
268 // Operating modes. Define behavior if buffer is full (not enough space for entire message)
269 //
270 #define SEGGER_RTT_MODE_NO_BLOCK_SKIP         (0)     // Skip. Do not block, output nothing. (Default)
271 #define SEGGER_RTT_MODE_NO_BLOCK_TRIM         (1)     // Trim: Do not block, output as much as fits.
272 #define SEGGER_RTT_MODE_BLOCK_IF_FIFO_FULL    (2)     // Block: Wait until there is space in the buffer.
273 #define SEGGER_RTT_MODE_MASK                  (3)
274 
275 //
276 // Control sequences, based on ANSI.
277 // Can be used to control color, and clear the screen
278 //
279 #define RTT_CTRL_RESET                "\x1B[0m"         // Reset to default colors
280 #define RTT_CTRL_CLEAR                "\x1B[2J"         // Clear screen, reposition cursor to top left
281 
282 #define RTT_CTRL_TEXT_BLACK           "\x1B[2;30m"
283 #define RTT_CTRL_TEXT_RED             "\x1B[2;31m"
284 #define RTT_CTRL_TEXT_GREEN           "\x1B[2;32m"
285 #define RTT_CTRL_TEXT_YELLOW          "\x1B[2;33m"
286 #define RTT_CTRL_TEXT_BLUE            "\x1B[2;34m"
287 #define RTT_CTRL_TEXT_MAGENTA         "\x1B[2;35m"
288 #define RTT_CTRL_TEXT_CYAN            "\x1B[2;36m"
289 #define RTT_CTRL_TEXT_WHITE           "\x1B[2;37m"
290 
291 #define RTT_CTRL_TEXT_BRIGHT_BLACK    "\x1B[1;30m"
292 #define RTT_CTRL_TEXT_BRIGHT_RED      "\x1B[1;31m"
293 #define RTT_CTRL_TEXT_BRIGHT_GREEN    "\x1B[1;32m"
294 #define RTT_CTRL_TEXT_BRIGHT_YELLOW   "\x1B[1;33m"
295 #define RTT_CTRL_TEXT_BRIGHT_BLUE     "\x1B[1;34m"
296 #define RTT_CTRL_TEXT_BRIGHT_MAGENTA  "\x1B[1;35m"
297 #define RTT_CTRL_TEXT_BRIGHT_CYAN     "\x1B[1;36m"
298 #define RTT_CTRL_TEXT_BRIGHT_WHITE    "\x1B[1;37m"
299 
300 #define RTT_CTRL_BG_BLACK             "\x1B[24;40m"
301 #define RTT_CTRL_BG_RED               "\x1B[24;41m"
302 #define RTT_CTRL_BG_GREEN             "\x1B[24;42m"
303 #define RTT_CTRL_BG_YELLOW            "\x1B[24;43m"
304 #define RTT_CTRL_BG_BLUE              "\x1B[24;44m"
305 #define RTT_CTRL_BG_MAGENTA           "\x1B[24;45m"
306 #define RTT_CTRL_BG_CYAN              "\x1B[24;46m"
307 #define RTT_CTRL_BG_WHITE             "\x1B[24;47m"
308 
309 #define RTT_CTRL_BG_BRIGHT_BLACK      "\x1B[4;40m"
310 #define RTT_CTRL_BG_BRIGHT_RED        "\x1B[4;41m"
311 #define RTT_CTRL_BG_BRIGHT_GREEN      "\x1B[4;42m"
312 #define RTT_CTRL_BG_BRIGHT_YELLOW     "\x1B[4;43m"
313 #define RTT_CTRL_BG_BRIGHT_BLUE       "\x1B[4;44m"
314 #define RTT_CTRL_BG_BRIGHT_MAGENTA    "\x1B[4;45m"
315 #define RTT_CTRL_BG_BRIGHT_CYAN       "\x1B[4;46m"
316 #define RTT_CTRL_BG_BRIGHT_WHITE      "\x1B[4;47m"
317 
318 
319 #endif
320 
321 /*************************** End of file ****************************/
322