SEGGER_RTT.h 15 KB

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