1 /*********************************************************************
2 *                    SEGGER Microcontroller GmbH                     *
3 *                        The Embedded Experts                        *
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    : 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: 12826 $
60 ----------------------------------------------------------------------
61 */
62 
63 #ifndef SEGGER_RTT_H
64 #define SEGGER_RTT_H
65 
66 #ifdef SEGGER_RTT_CONFIG_H
67 #include SEGGER_RTT_CONFIG_H
68 #else
69 #include "SEGGER_RTT_Conf.h"
70 #endif
71 
72 #ifndef SEGGER_RTT_ASM  // defined when SEGGER_RTT.h is included from assembly file
73 #include <stdlib.h>
74 #include <stdarg.h>
75 
76 /*********************************************************************
77 *
78 *       Defines, fixed
79 *
80 **********************************************************************
81 */
82 
83 /*********************************************************************
84 *
85 *       Types
86 *
87 **********************************************************************
88 */
89 
90 //
91 // Description for a circular buffer (also called "ring buffer")
92 // which is used as up-buffer (T->H)
93 //
94 typedef struct {
95   const     char*    sName;         // Optional name. Standard names so far are: "Terminal", "SysView", "J-Scope_t4i4"
96             char*    pBuffer;       // Pointer to start of buffer
97             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.
98             unsigned WrOff;         // Position of next item to be written by either target.
99   volatile  unsigned RdOff;         // Position of next item to be read by host. Must be volatile since it may be modified by host.
100             unsigned Flags;         // Contains configuration flags
101 } SEGGER_RTT_BUFFER_UP;
102 
103 //
104 // Description for a circular buffer (also called "ring buffer")
105 // which is used as down-buffer (H->T)
106 //
107 typedef struct {
108   const     char*    sName;         // Optional name. Standard names so far are: "Terminal", "SysView", "J-Scope_t4i4"
109             char*    pBuffer;       // Pointer to start of buffer
110             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.
111   volatile  unsigned WrOff;         // Position of next item to be written by host. Must be volatile since it may be modified by host.
112             unsigned RdOff;         // Position of next item to be read by target (down-buffer).
113             unsigned Flags;         // Contains configuration flags
114 } SEGGER_RTT_BUFFER_DOWN;
115 
116 //
117 // RTT control block which describes the number of buffers available
118 // as well as the configuration for each buffer
119 //
120 //
121 typedef struct {
122   char                    acID[16];                                 // Initialized to "SEGGER RTT"
123   int                     MaxNumUpBuffers;                          // Initialized to SEGGER_RTT_MAX_NUM_UP_BUFFERS (type. 2)
124   int                     MaxNumDownBuffers;                        // Initialized to SEGGER_RTT_MAX_NUM_DOWN_BUFFERS (type. 2)
125   SEGGER_RTT_BUFFER_UP    aUp[SEGGER_RTT_MAX_NUM_UP_BUFFERS];       // Up buffers, transferring information up from target via debug probe to host
126   SEGGER_RTT_BUFFER_DOWN  aDown[SEGGER_RTT_MAX_NUM_DOWN_BUFFERS];   // Down buffers, transferring information down from host via debug probe to target
127 } SEGGER_RTT_CB;
128 
129 /*********************************************************************
130 *
131 *       Global data
132 *
133 **********************************************************************
134 */
135 extern SEGGER_RTT_CB _SEGGER_RTT;
136 
137 /*********************************************************************
138 *
139 *       RTT API functions
140 *
141 **********************************************************************
142 */
143 #ifdef __cplusplus
144   extern "C" {
145 #endif
146 int          SEGGER_RTT_AllocDownBuffer         (const char* sName, void* pBuffer, unsigned BufferSize, unsigned Flags);
147 int          SEGGER_RTT_AllocUpBuffer           (const char* sName, void* pBuffer, unsigned BufferSize, unsigned Flags);
148 int          SEGGER_RTT_ConfigUpBuffer          (unsigned BufferIndex, const char* sName, void* pBuffer, unsigned BufferSize, unsigned Flags);
149 int          SEGGER_RTT_ConfigDownBuffer        (unsigned BufferIndex, const char* sName, void* pBuffer, unsigned BufferSize, unsigned Flags);
150 int          SEGGER_RTT_GetKey                  (void);
151 unsigned     SEGGER_RTT_HasData                 (unsigned BufferIndex);
152 int          SEGGER_RTT_HasKey                  (void);
153 unsigned     SEGGER_RTT_HasDataUp               (unsigned BufferIndex);
154 void         SEGGER_RTT_Init                    (void);
155 unsigned     SEGGER_RTT_Read                    (unsigned BufferIndex,       void* pBuffer, unsigned BufferSize);
156 unsigned     SEGGER_RTT_ReadNoLock              (unsigned BufferIndex,       void* pData,   unsigned BufferSize);
157 int          SEGGER_RTT_SetNameDownBuffer       (unsigned BufferIndex, const char* sName);
158 int          SEGGER_RTT_SetNameUpBuffer         (unsigned BufferIndex, const char* sName);
159 int          SEGGER_RTT_SetFlagsDownBuffer      (unsigned BufferIndex, unsigned Flags);
160 int          SEGGER_RTT_SetFlagsUpBuffer        (unsigned BufferIndex, unsigned Flags);
161 int          SEGGER_RTT_WaitKey                 (void);
162 unsigned     SEGGER_RTT_Write                   (unsigned BufferIndex, const void* pBuffer, unsigned NumBytes);
163 unsigned     SEGGER_RTT_WriteNoLock             (unsigned BufferIndex, const void* pBuffer, unsigned NumBytes);
164 unsigned     SEGGER_RTT_WriteSkipNoLock         (unsigned BufferIndex, const void* pBuffer, unsigned NumBytes);
165 unsigned     SEGGER_RTT_WriteString             (unsigned BufferIndex, const char* s);
166 void         SEGGER_RTT_WriteWithOverwriteNoLock(unsigned BufferIndex, const void* pBuffer, unsigned NumBytes);
167 unsigned     SEGGER_RTT_PutChar                 (unsigned BufferIndex, char c);
168 unsigned     SEGGER_RTT_PutCharSkip             (unsigned BufferIndex, char c);
169 unsigned     SEGGER_RTT_PutCharSkipNoLock       (unsigned BufferIndex, char c);
170 //
171 // Function macro for performance optimization
172 //
173 #define      SEGGER_RTT_HASDATA(n)       (_SEGGER_RTT.aDown[n].WrOff - _SEGGER_RTT.aDown[n].RdOff)
174 
175 /*********************************************************************
176 *
177 *       RTT "Terminal" API functions
178 *
179 **********************************************************************
180 */
181 int     SEGGER_RTT_SetTerminal        (char TerminalId);
182 int     SEGGER_RTT_TerminalOut        (char TerminalId, const char* s);
183 
184 /*********************************************************************
185 *
186 *       RTT printf functions (require SEGGER_RTT_printf.c)
187 *
188 **********************************************************************
189 */
190 int SEGGER_RTT_printf(unsigned BufferIndex, const char * sFormat, ...);
191 int SEGGER_RTT_vprintf(unsigned BufferIndex, const char * sFormat, va_list * pParamList);
192 
193 #ifdef __cplusplus
194   }
195 #endif
196 
197 #endif // SEGGER_RTT_ASM
198 
199 /*********************************************************************
200 *
201 *       Defines
202 *
203 **********************************************************************
204 */
205 
206 //
207 // Operating modes. Define behavior if buffer is full (not enough space for entire message)
208 //
209 #define SEGGER_RTT_MODE_NO_BLOCK_SKIP         (0)     // Skip. Do not block, output nothing. (Default)
210 #define SEGGER_RTT_MODE_NO_BLOCK_TRIM         (1)     // Trim: Do not block, output as much as fits.
211 #define SEGGER_RTT_MODE_BLOCK_IF_FIFO_FULL    (2)     // Block: Wait until there is space in the buffer.
212 #define SEGGER_RTT_MODE_MASK                  (3)
213 
214 //
215 // Control sequences, based on ANSI.
216 // Can be used to control color, and clear the screen
217 //
218 #define RTT_CTRL_RESET                "\x1B[0m"         // Reset to default colors
219 #define RTT_CTRL_CLEAR                "\x1B[2J"         // Clear screen, reposition cursor to top left
220 
221 #define RTT_CTRL_TEXT_BLACK           "\x1B[2;30m"
222 #define RTT_CTRL_TEXT_RED             "\x1B[2;31m"
223 #define RTT_CTRL_TEXT_GREEN           "\x1B[2;32m"
224 #define RTT_CTRL_TEXT_YELLOW          "\x1B[2;33m"
225 #define RTT_CTRL_TEXT_BLUE            "\x1B[2;34m"
226 #define RTT_CTRL_TEXT_MAGENTA         "\x1B[2;35m"
227 #define RTT_CTRL_TEXT_CYAN            "\x1B[2;36m"
228 #define RTT_CTRL_TEXT_WHITE           "\x1B[2;37m"
229 
230 #define RTT_CTRL_TEXT_BRIGHT_BLACK    "\x1B[1;30m"
231 #define RTT_CTRL_TEXT_BRIGHT_RED      "\x1B[1;31m"
232 #define RTT_CTRL_TEXT_BRIGHT_GREEN    "\x1B[1;32m"
233 #define RTT_CTRL_TEXT_BRIGHT_YELLOW   "\x1B[1;33m"
234 #define RTT_CTRL_TEXT_BRIGHT_BLUE     "\x1B[1;34m"
235 #define RTT_CTRL_TEXT_BRIGHT_MAGENTA  "\x1B[1;35m"
236 #define RTT_CTRL_TEXT_BRIGHT_CYAN     "\x1B[1;36m"
237 #define RTT_CTRL_TEXT_BRIGHT_WHITE    "\x1B[1;37m"
238 
239 #define RTT_CTRL_BG_BLACK             "\x1B[24;40m"
240 #define RTT_CTRL_BG_RED               "\x1B[24;41m"
241 #define RTT_CTRL_BG_GREEN             "\x1B[24;42m"
242 #define RTT_CTRL_BG_YELLOW            "\x1B[24;43m"
243 #define RTT_CTRL_BG_BLUE              "\x1B[24;44m"
244 #define RTT_CTRL_BG_MAGENTA           "\x1B[24;45m"
245 #define RTT_CTRL_BG_CYAN              "\x1B[24;46m"
246 #define RTT_CTRL_BG_WHITE             "\x1B[24;47m"
247 
248 #define RTT_CTRL_BG_BRIGHT_BLACK      "\x1B[4;40m"
249 #define RTT_CTRL_BG_BRIGHT_RED        "\x1B[4;41m"
250 #define RTT_CTRL_BG_BRIGHT_GREEN      "\x1B[4;42m"
251 #define RTT_CTRL_BG_BRIGHT_YELLOW     "\x1B[4;43m"
252 #define RTT_CTRL_BG_BRIGHT_BLUE       "\x1B[4;44m"
253 #define RTT_CTRL_BG_BRIGHT_MAGENTA    "\x1B[4;45m"
254 #define RTT_CTRL_BG_BRIGHT_CYAN       "\x1B[4;46m"
255 #define RTT_CTRL_BG_BRIGHT_WHITE      "\x1B[4;47m"
256 
257 
258 #endif
259 
260 /*************************** End of file ****************************/
261