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