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    : SEGGER_RTT.h
50 Purpose : Implementation of SEGGER real-time transfer which allows
51           real-time communication on targets which support debugger
52           memory accesses while the CPU is running.
53 Revision: $Rev: 20869 $
54 ----------------------------------------------------------------------
55 */
56 
57 #ifndef SEGGER_RTT_H
58 #define SEGGER_RTT_H
59 
60 #include "SEGGER_RTT_Conf.h"
61 
62 /*********************************************************************
63 *
64 *       Defines, defaults
65 *
66 **********************************************************************
67 */
68 #ifndef RTT_USE_ASM
69   #if (defined __SES_ARM)                       // SEGGER Embedded Studio
70     #define _CC_HAS_RTT_ASM_SUPPORT 1
71   #elif (defined __CROSSWORKS_ARM)              // Rowley Crossworks
72     #define _CC_HAS_RTT_ASM_SUPPORT 1
73   #elif (defined __ARMCC_VERSION)               // ARM compiler
74 		#if (__ARMCC_VERSION >= 6000000)						// ARM compiler V6.0 and later is clang based
75       #define _CC_HAS_RTT_ASM_SUPPORT 1
76 		#else
77       #define _CC_HAS_RTT_ASM_SUPPORT 0
78 		#endif
79   #elif (defined __GNUC__)                      // GCC
80     #define _CC_HAS_RTT_ASM_SUPPORT 1
81   #elif (defined __clang__)                     // Clang compiler
82     #define _CC_HAS_RTT_ASM_SUPPORT 1
83   #elif ((defined __IASMARM__) || (defined __ICCARM__))  // IAR assembler/compiler
84     #define _CC_HAS_RTT_ASM_SUPPORT 1
85   #else
86     #define _CC_HAS_RTT_ASM_SUPPORT 0
87   #endif
88   #if ((defined __IASMARM__) || (defined __ICCARM__))  // IAR assembler/compiler
89     //
90     // IAR assembler / compiler
91     //
92     #if (__VER__ < 6300000)
93       #define VOLATILE
94     #else
95       #define VOLATILE volatile
96     #endif
97     #if (defined __ARM7M__)                            // Needed for old versions that do not know the define yet
98       #if (__CORE__ == __ARM7M__)                      // Cortex-M3
99         #define _CORE_HAS_RTT_ASM_SUPPORT 1
100       #endif
101     #endif
102     #if (defined __ARM7EM__)                           // Needed for old versions that do not know the define yet
103       #if (__CORE__ == __ARM7EM__)                     // Cortex-M4/M7
104         #define _CORE_HAS_RTT_ASM_SUPPORT 1
105         #define _CORE_NEEDS_DMB 1
106         #define RTT__DMB() asm VOLATILE ("DMB");
107       #endif
108     #endif
109     #if (defined __ARM8M_BASELINE__)                   // Needed for old versions that do not know the define yet
110       #if (__CORE__ == __ARM8M_BASELINE__)             // Cortex-M23
111         #define _CORE_HAS_RTT_ASM_SUPPORT 0
112         #define _CORE_NEEDS_DMB 1
113         #define RTT__DMB() asm VOLATILE ("DMB");
114       #endif
115     #endif
116     #if (defined __ARM8M_MAINLINE__)                   // Needed for old versions that do not know the define yet
117       #if (__CORE__ == __ARM8M_MAINLINE__)             // Cortex-M33
118         #define _CORE_HAS_RTT_ASM_SUPPORT 1
119         #define _CORE_NEEDS_DMB 1
120         #define RTT__DMB() asm VOLATILE ("DMB");
121       #endif
122     #endif
123   #else
124     //
125     // GCC / Clang
126     //
127     #if (defined __ARM_ARCH_7M__)                 // Cortex-M3
128       #define _CORE_HAS_RTT_ASM_SUPPORT 1
129     #elif (defined __ARM_ARCH_7EM__)              // Cortex-M4/M7
130       #define _CORE_HAS_RTT_ASM_SUPPORT 1
131       #define _CORE_NEEDS_DMB           1
132       #define RTT__DMB() __asm volatile ("dmb\n" : : :);
133     #elif (defined __ARM_ARCH_8M_BASE__)          // Cortex-M23
134       #define _CORE_HAS_RTT_ASM_SUPPORT 0
135       #define _CORE_NEEDS_DMB           1
136       #define RTT__DMB() __asm volatile ("dmb\n" : : :);
137     #elif (defined __ARM_ARCH_8M_MAIN__)          // Cortex-M33
138       #define _CORE_HAS_RTT_ASM_SUPPORT 1
139       #define _CORE_NEEDS_DMB           1
140       #define RTT__DMB() __asm volatile ("dmb\n" : : :);
141     #else
142       #define _CORE_HAS_RTT_ASM_SUPPORT 0
143     #endif
144   #endif
145   //
146   // If IDE and core support the ASM version, enable ASM version by default
147   //
148   #ifndef _CORE_HAS_RTT_ASM_SUPPORT
149     #define _CORE_HAS_RTT_ASM_SUPPORT 0              // Default for unknown cores
150   #endif
151   #if (_CC_HAS_RTT_ASM_SUPPORT && _CORE_HAS_RTT_ASM_SUPPORT)
152     #define RTT_USE_ASM                           (1)
153   #else
154     #define RTT_USE_ASM                           (0)
155   #endif
156 #endif
157 
158 //
159 // We need to know if a DMB is needed to make sure that on Cortex-M7 etc.
160 // the order of accesses to the ring buffers is guaranteed
161 // Needed for: Cortex-M7, Cortex-M23, Cortex-M33
162 //
163 #ifndef _CORE_NEEDS_DMB
164   #define _CORE_NEEDS_DMB 0
165 #endif
166 
167 #ifndef RTT__DMB
168   #if _CORE_NEEDS_DMB
169     #error "Don't know how to place inline assembly for DMB"
170   #else
171     #define RTT__DMB()
172   #endif
173 #endif
174 
175 #ifndef SEGGER_RTT_CPU_CACHE_LINE_SIZE
176   #define SEGGER_RTT_CPU_CACHE_LINE_SIZE (0)   // On most target systems where RTT is used, we do not have a CPU cache, therefore 0 is a good default here
177 #endif
178 
179 #ifndef SEGGER_RTT_UNCACHED_OFF
180   #if SEGGER_RTT_CPU_CACHE_LINE_SIZE
181     #error "SEGGER_RTT_UNCACHED_OFF must be defined when setting SEGGER_RTT_CPU_CACHE_LINE_SIZE != 0"
182   #else
183     #define SEGGER_RTT_UNCACHED_OFF (0)
184   #endif
185 #endif
186 #if RTT_USE_ASM
187   #if SEGGER_RTT_CPU_CACHE_LINE_SIZE
188     #error "RTT_USE_ASM is not available if SEGGER_RTT_CPU_CACHE_LINE_SIZE != 0"
189   #endif
190 #endif
191 
192 #ifndef SEGGER_RTT_ASM  // defined when SEGGER_RTT.h is included from assembly file
193 #include <stdlib.h>
194 #include <stdarg.h>
195 
196 /*********************************************************************
197 *
198 *       Defines, fixed
199 *
200 **********************************************************************
201 */
202 
203 //
204 // Determine how much we must pad the control block to make it a multiple of a cache line in size
205 // Assuming: U8 = 1B
206 //           U16 = 2B
207 //           U32 = 4B
208 //           U8/U16/U32* = 4B
209 //
210 #if SEGGER_RTT_CPU_CACHE_LINE_SIZE    // Avoid division by zero in case we do not have any cache
211   #define SEGGER_RTT__ROUND_UP_2_CACHE_LINE_SIZE(NumBytes) (((NumBytes + SEGGER_RTT_CPU_CACHE_LINE_SIZE - 1) / SEGGER_RTT_CPU_CACHE_LINE_SIZE) * SEGGER_RTT_CPU_CACHE_LINE_SIZE)
212 #else
213   #define SEGGER_RTT__ROUND_UP_2_CACHE_LINE_SIZE(NumBytes) (NumBytes)
214 #endif
215 #define SEGGER_RTT__CB_SIZE                              (16 + 4 + 4 + (SEGGER_RTT_MAX_NUM_UP_BUFFERS * 24) + (SEGGER_RTT_MAX_NUM_DOWN_BUFFERS * 24))
216 #define SEGGER_RTT__CB_PADDING                           (SEGGER_RTT__ROUND_UP_2_CACHE_LINE_SIZE(SEGGER_RTT__CB_SIZE) - SEGGER_RTT__CB_SIZE)
217 
218 /*********************************************************************
219 *
220 *       Types
221 *
222 **********************************************************************
223 */
224 
225 //
226 // Description for a circular buffer (also called "ring buffer")
227 // which is used as up-buffer (T->H)
228 //
229 typedef struct {
230   const     char*    sName;         // Optional name. Standard names so far are: "Terminal", "SysView", "J-Scope_t4i4"
231             char*    pBuffer;       // Pointer to start of buffer
232             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.
233             unsigned WrOff;         // Position of next item to be written by either target.
234   volatile  unsigned RdOff;         // Position of next item to be read by host. Must be volatile since it may be modified by host.
235             unsigned Flags;         // Contains configuration flags
236 } SEGGER_RTT_BUFFER_UP;
237 
238 //
239 // Description for a circular buffer (also called "ring buffer")
240 // which is used as down-buffer (H->T)
241 //
242 typedef struct {
243   const     char*    sName;         // Optional name. Standard names so far are: "Terminal", "SysView", "J-Scope_t4i4"
244             char*    pBuffer;       // Pointer to start of buffer
245             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.
246   volatile  unsigned WrOff;         // Position of next item to be written by host. Must be volatile since it may be modified by host.
247             unsigned RdOff;         // Position of next item to be read by target (down-buffer).
248             unsigned Flags;         // Contains configuration flags
249 } SEGGER_RTT_BUFFER_DOWN;
250 
251 //
252 // RTT control block which describes the number of buffers available
253 // as well as the configuration for each buffer
254 //
255 //
256 typedef struct {
257   char                    acID[16];                                 // Initialized to "SEGGER RTT"
258   int                     MaxNumUpBuffers;                          // Initialized to SEGGER_RTT_MAX_NUM_UP_BUFFERS (type. 2)
259   int                     MaxNumDownBuffers;                        // Initialized to SEGGER_RTT_MAX_NUM_DOWN_BUFFERS (type. 2)
260   SEGGER_RTT_BUFFER_UP    aUp[SEGGER_RTT_MAX_NUM_UP_BUFFERS];       // Up buffers, transferring information up from target via debug probe to host
261   SEGGER_RTT_BUFFER_DOWN  aDown[SEGGER_RTT_MAX_NUM_DOWN_BUFFERS];   // Down buffers, transferring information down from host via debug probe to target
262 #if SEGGER_RTT__CB_PADDING
263   unsigned char           aDummy[SEGGER_RTT__CB_PADDING];
264 #endif
265 } SEGGER_RTT_CB;
266 
267 /*********************************************************************
268 *
269 *       Global data
270 *
271 **********************************************************************
272 */
273 extern SEGGER_RTT_CB _SEGGER_RTT;
274 
275 /*********************************************************************
276 *
277 *       RTT API functions
278 *
279 **********************************************************************
280 */
281 #ifdef __cplusplus
282   extern "C" {
283 #endif
284 int          SEGGER_RTT_AllocDownBuffer         (const char* sName, void* pBuffer, unsigned BufferSize, unsigned Flags);
285 int          SEGGER_RTT_AllocUpBuffer           (const char* sName, void* pBuffer, unsigned BufferSize, unsigned Flags);
286 int          SEGGER_RTT_ConfigUpBuffer          (unsigned BufferIndex, const char* sName, void* pBuffer, unsigned BufferSize, unsigned Flags);
287 int          SEGGER_RTT_ConfigDownBuffer        (unsigned BufferIndex, const char* sName, void* pBuffer, unsigned BufferSize, unsigned Flags);
288 int          SEGGER_RTT_GetKey                  (void);
289 unsigned     SEGGER_RTT_HasData                 (unsigned BufferIndex);
290 int          SEGGER_RTT_HasKey                  (void);
291 unsigned     SEGGER_RTT_HasDataUp               (unsigned BufferIndex);
292 void         SEGGER_RTT_Init                    (void);
293 unsigned     SEGGER_RTT_Read                    (unsigned BufferIndex,       void* pBuffer, unsigned BufferSize);
294 unsigned     SEGGER_RTT_ReadNoLock              (unsigned BufferIndex,       void* pData,   unsigned BufferSize);
295 int          SEGGER_RTT_SetNameDownBuffer       (unsigned BufferIndex, const char* sName);
296 int          SEGGER_RTT_SetNameUpBuffer         (unsigned BufferIndex, const char* sName);
297 int          SEGGER_RTT_SetFlagsDownBuffer      (unsigned BufferIndex, unsigned Flags);
298 int          SEGGER_RTT_SetFlagsUpBuffer        (unsigned BufferIndex, unsigned Flags);
299 int          SEGGER_RTT_WaitKey                 (void);
300 unsigned     SEGGER_RTT_Write                   (unsigned BufferIndex, const void* pBuffer, unsigned NumBytes);
301 unsigned     SEGGER_RTT_WriteNoLock             (unsigned BufferIndex, const void* pBuffer, unsigned NumBytes);
302 unsigned     SEGGER_RTT_WriteSkipNoLock         (unsigned BufferIndex, const void* pBuffer, unsigned NumBytes);
303 unsigned     SEGGER_RTT_ASM_WriteSkipNoLock     (unsigned BufferIndex, const void* pBuffer, unsigned NumBytes);
304 unsigned     SEGGER_RTT_WriteString             (unsigned BufferIndex, const char* s);
305 void         SEGGER_RTT_WriteWithOverwriteNoLock(unsigned BufferIndex, const void* pBuffer, unsigned NumBytes);
306 unsigned     SEGGER_RTT_PutChar                 (unsigned BufferIndex, char c);
307 unsigned     SEGGER_RTT_PutCharSkip             (unsigned BufferIndex, char c);
308 unsigned     SEGGER_RTT_PutCharSkipNoLock       (unsigned BufferIndex, char c);
309 unsigned     SEGGER_RTT_GetAvailWriteSpace      (unsigned BufferIndex);
310 unsigned     SEGGER_RTT_GetBytesInBuffer        (unsigned BufferIndex);
311 //
312 // Function macro for performance optimization
313 //
314 #define      SEGGER_RTT_HASDATA(n)       (((SEGGER_RTT_BUFFER_DOWN*)((char*)&_SEGGER_RTT.aDown[n] + SEGGER_RTT_UNCACHED_OFF))->WrOff - ((SEGGER_RTT_BUFFER_DOWN*)((char*)&_SEGGER_RTT.aDown[n] + SEGGER_RTT_UNCACHED_OFF))->RdOff)
315 
316 #if RTT_USE_ASM
317   #define SEGGER_RTT_WriteSkipNoLock  SEGGER_RTT_ASM_WriteSkipNoLock
318 #endif
319 
320 /*********************************************************************
321 *
322 *       RTT transfer functions to send RTT data via other channels.
323 *
324 **********************************************************************
325 */
326 unsigned     SEGGER_RTT_ReadUpBuffer            (unsigned BufferIndex, void* pBuffer, unsigned BufferSize);
327 unsigned     SEGGER_RTT_ReadUpBufferNoLock      (unsigned BufferIndex, void* pData, unsigned BufferSize);
328 unsigned     SEGGER_RTT_WriteDownBuffer         (unsigned BufferIndex, const void* pBuffer, unsigned NumBytes);
329 unsigned     SEGGER_RTT_WriteDownBufferNoLock   (unsigned BufferIndex, const void* pBuffer, unsigned NumBytes);
330 
331 #define      SEGGER_RTT_HASDATA_UP(n)    (((SEGGER_RTT_BUFFER_UP*)((char*)&_SEGGER_RTT.aUp[n] + SEGGER_RTT_UNCACHED_OFF))->WrOff - ((SEGGER_RTT_BUFFER_UP*)((char*)&_SEGGER_RTT.aUp[n] + SEGGER_RTT_UNCACHED_OFF))->RdOff)   // Access uncached to make sure we see changes made by the J-Link side and all of our changes go into HW directly
332 
333 /*********************************************************************
334 *
335 *       RTT "Terminal" API functions
336 *
337 **********************************************************************
338 */
339 int     SEGGER_RTT_SetTerminal        (unsigned char TerminalId);
340 int     SEGGER_RTT_TerminalOut        (unsigned char TerminalId, const char* s);
341 
342 /*********************************************************************
343 *
344 *       RTT printf functions (require SEGGER_RTT_printf.c)
345 *
346 **********************************************************************
347 */
348 int SEGGER_RTT_printf(unsigned BufferIndex, const char * sFormat, ...);
349 int SEGGER_RTT_vprintf(unsigned BufferIndex, const char * sFormat, va_list * pParamList);
350 
351 #ifdef __cplusplus
352   }
353 #endif
354 
355 #endif // ifndef(SEGGER_RTT_ASM)
356 
357 /*********************************************************************
358 *
359 *       Defines
360 *
361 **********************************************************************
362 */
363 
364 //
365 // Operating modes. Define behavior if buffer is full (not enough space for entire message)
366 //
367 #define SEGGER_RTT_MODE_NO_BLOCK_SKIP         (0)     // Skip. Do not block, output nothing. (Default)
368 #define SEGGER_RTT_MODE_NO_BLOCK_TRIM         (1)     // Trim: Do not block, output as much as fits.
369 #define SEGGER_RTT_MODE_BLOCK_IF_FIFO_FULL    (2)     // Block: Wait until there is space in the buffer.
370 #define SEGGER_RTT_MODE_MASK                  (3)
371 
372 //
373 // Control sequences, based on ANSI.
374 // Can be used to control color, and clear the screen
375 //
376 #define RTT_CTRL_RESET                "\x1B[0m"         // Reset to default colors
377 #define RTT_CTRL_CLEAR                "\x1B[2J"         // Clear screen, reposition cursor to top left
378 
379 #define RTT_CTRL_TEXT_BLACK           "\x1B[2;30m"
380 #define RTT_CTRL_TEXT_RED             "\x1B[2;31m"
381 #define RTT_CTRL_TEXT_GREEN           "\x1B[2;32m"
382 #define RTT_CTRL_TEXT_YELLOW          "\x1B[2;33m"
383 #define RTT_CTRL_TEXT_BLUE            "\x1B[2;34m"
384 #define RTT_CTRL_TEXT_MAGENTA         "\x1B[2;35m"
385 #define RTT_CTRL_TEXT_CYAN            "\x1B[2;36m"
386 #define RTT_CTRL_TEXT_WHITE           "\x1B[2;37m"
387 
388 #define RTT_CTRL_TEXT_BRIGHT_BLACK    "\x1B[1;30m"
389 #define RTT_CTRL_TEXT_BRIGHT_RED      "\x1B[1;31m"
390 #define RTT_CTRL_TEXT_BRIGHT_GREEN    "\x1B[1;32m"
391 #define RTT_CTRL_TEXT_BRIGHT_YELLOW   "\x1B[1;33m"
392 #define RTT_CTRL_TEXT_BRIGHT_BLUE     "\x1B[1;34m"
393 #define RTT_CTRL_TEXT_BRIGHT_MAGENTA  "\x1B[1;35m"
394 #define RTT_CTRL_TEXT_BRIGHT_CYAN     "\x1B[1;36m"
395 #define RTT_CTRL_TEXT_BRIGHT_WHITE    "\x1B[1;37m"
396 
397 #define RTT_CTRL_BG_BLACK             "\x1B[24;40m"
398 #define RTT_CTRL_BG_RED               "\x1B[24;41m"
399 #define RTT_CTRL_BG_GREEN             "\x1B[24;42m"
400 #define RTT_CTRL_BG_YELLOW            "\x1B[24;43m"
401 #define RTT_CTRL_BG_BLUE              "\x1B[24;44m"
402 #define RTT_CTRL_BG_MAGENTA           "\x1B[24;45m"
403 #define RTT_CTRL_BG_CYAN              "\x1B[24;46m"
404 #define RTT_CTRL_BG_WHITE             "\x1B[24;47m"
405 
406 #define RTT_CTRL_BG_BRIGHT_BLACK      "\x1B[4;40m"
407 #define RTT_CTRL_BG_BRIGHT_RED        "\x1B[4;41m"
408 #define RTT_CTRL_BG_BRIGHT_GREEN      "\x1B[4;42m"
409 #define RTT_CTRL_BG_BRIGHT_YELLOW     "\x1B[4;43m"
410 #define RTT_CTRL_BG_BRIGHT_BLUE       "\x1B[4;44m"
411 #define RTT_CTRL_BG_BRIGHT_MAGENTA    "\x1B[4;45m"
412 #define RTT_CTRL_BG_BRIGHT_CYAN       "\x1B[4;46m"
413 #define RTT_CTRL_BG_BRIGHT_WHITE      "\x1B[4;47m"
414 
415 
416 #endif
417 
418 /*************************** End of file ****************************/
419