1 /*********************************************************************
2 *               SEGGER MICROCONTROLLER GmbH & Co. KG                 *
3 *       Solutions for real time microcontroller applications         *
4 **********************************************************************
5 *                                                                    *
6 *       (c) 2014 - 2016  SEGGER Microcontroller GmbH & Co. KG        *
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 * * This software may in its unmodified form be freely redistributed *
19 *   in source, linkable, or executable form.                         *
20 * * The source code may be modified, provided the source code        *
21 *   retains the above copyright notice, this list of conditions and  *
22 *   the following disclaimer.                                        *
23 * * Modified versions of this software in source, executable, or     *
24 *   linkable form may not be distributed without prior consent of    *
25 *   SEGGER.                                                          *
26 * * This software may only be used for communication with SEGGER     *
27 *   J-Link debug probes.                                             *
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 *       RTT version: 6.00e                                           *
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: 4079 $
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, fixed
65 *
66 **********************************************************************
67 */
68 
69 /*********************************************************************
70 *
71 *       Types
72 *
73 **********************************************************************
74 */
75 
76 //
77 // Description for a circular buffer (also called "ring buffer")
78 // which is used as up-buffer (T->H)
79 //
80 typedef struct {
81   const     char*    sName;         // Optional name. Standard names so far are: "Terminal", "SysView", "J-Scope_t4i4"
82             char*    pBuffer;       // Pointer to start of buffer
83             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.
84             unsigned WrOff;         // Position of next item to be written by either target.
85   volatile  unsigned RdOff;         // Position of next item to be read by host. Must be volatile since it may be modified by host.
86             unsigned Flags;         // Contains configuration flags
87 } SEGGER_RTT_BUFFER_UP;
88 
89 //
90 // Description for a circular buffer (also called "ring buffer")
91 // which is used as down-buffer (H->T)
92 //
93 typedef struct {
94   const     char*    sName;         // Optional name. Standard names so far are: "Terminal", "SysView", "J-Scope_t4i4"
95             char*    pBuffer;       // Pointer to start of buffer
96             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.
97   volatile  unsigned WrOff;         // Position of next item to be written by host. Must be volatile since it may be modified by host.
98             unsigned RdOff;         // Position of next item to be read by target (down-buffer).
99             unsigned Flags;         // Contains configuration flags
100 } SEGGER_RTT_BUFFER_DOWN;
101 
102 //
103 // RTT control block which describes the number of buffers available
104 // as well as the configuration for each buffer
105 //
106 //
107 typedef struct {
108   char                    acID[16];                                 // Initialized to "SEGGER RTT"
109   int                     MaxNumUpBuffers;                          // Initialized to SEGGER_RTT_MAX_NUM_UP_BUFFERS (type. 2)
110   int                     MaxNumDownBuffers;                        // Initialized to SEGGER_RTT_MAX_NUM_DOWN_BUFFERS (type. 2)
111   SEGGER_RTT_BUFFER_UP    aUp[SEGGER_RTT_MAX_NUM_UP_BUFFERS];       // Up buffers, transferring information up from target via debug probe to host
112   SEGGER_RTT_BUFFER_DOWN  aDown[SEGGER_RTT_MAX_NUM_DOWN_BUFFERS];   // Down buffers, transferring information down from host via debug probe to target
113 } SEGGER_RTT_CB;
114 
115 /*********************************************************************
116 *
117 *       Global data
118 *
119 **********************************************************************
120 */
121 extern SEGGER_RTT_CB _SEGGER_RTT;
122 
123 /*********************************************************************
124 *
125 *       RTT API functions
126 *
127 **********************************************************************
128 */
129 #ifdef __cplusplus
130   extern "C" {
131 #endif
132 int          SEGGER_RTT_AllocDownBuffer         (const char* sName, void* pBuffer, unsigned BufferSize, unsigned Flags);
133 int          SEGGER_RTT_AllocUpBuffer           (const char* sName, void* pBuffer, unsigned BufferSize, unsigned Flags);
134 int          SEGGER_RTT_ConfigUpBuffer          (unsigned BufferIndex, const char* sName, void* pBuffer, unsigned BufferSize, unsigned Flags);
135 int          SEGGER_RTT_ConfigDownBuffer        (unsigned BufferIndex, const char* sName, void* pBuffer, unsigned BufferSize, unsigned Flags);
136 int          SEGGER_RTT_GetKey                  (void);
137 unsigned     SEGGER_RTT_HasData                 (unsigned BufferIndex);
138 int          SEGGER_RTT_HasKey                  (void);
139 void         SEGGER_RTT_Init                    (void);
140 unsigned     SEGGER_RTT_Read                    (unsigned BufferIndex,       void* pBuffer, unsigned BufferSize);
141 unsigned     SEGGER_RTT_ReadNoLock              (unsigned BufferIndex,       void* pData,   unsigned BufferSize);
142 int          SEGGER_RTT_SetNameDownBuffer       (unsigned BufferIndex, const char* sName);
143 int          SEGGER_RTT_SetNameUpBuffer         (unsigned BufferIndex, const char* sName);
144 int          SEGGER_RTT_SetFlagsDownBuffer      (unsigned BufferIndex, unsigned Flags);
145 int          SEGGER_RTT_SetFlagsUpBuffer        (unsigned BufferIndex, unsigned Flags);
146 int          SEGGER_RTT_WaitKey                 (void);
147 unsigned     SEGGER_RTT_Write                   (unsigned BufferIndex, const void* pBuffer, unsigned NumBytes);
148 unsigned     SEGGER_RTT_WriteNoLock             (unsigned BufferIndex, const void* pBuffer, unsigned NumBytes);
149 unsigned     SEGGER_RTT_WriteSkipNoLock         (unsigned BufferIndex, const void* pBuffer, unsigned NumBytes);
150 unsigned     SEGGER_RTT_WriteString             (unsigned BufferIndex, const char* s);
151 void         SEGGER_RTT_WriteWithOverwriteNoLock(unsigned BufferIndex, const void* pBuffer, unsigned NumBytes);
152 //
153 // Function macro for performance optimization
154 //
155 #define      SEGGER_RTT_HASDATA(n)       (_SEGGER_RTT.aDown[n].WrOff - _SEGGER_RTT.aDown[n].RdOff)
156 
157 /*********************************************************************
158 *
159 *       RTT "Terminal" API functions
160 *
161 **********************************************************************
162 */
163 int     SEGGER_RTT_SetTerminal        (char TerminalId);
164 int     SEGGER_RTT_TerminalOut        (char TerminalId, const char* s);
165 
166 /*********************************************************************
167 *
168 *       RTT printf functions (require SEGGER_RTT_printf.c)
169 *
170 **********************************************************************
171 */
172 int SEGGER_RTT_printf(unsigned BufferIndex, const char * sFormat, ...);
173 #ifdef __cplusplus
174   }
175 #endif
176 
177 /*********************************************************************
178 *
179 *       Defines
180 *
181 **********************************************************************
182 */
183 
184 //
185 // Operating modes. Define behavior if buffer is full (not enough space for entire message)
186 //
187 #define SEGGER_RTT_MODE_NO_BLOCK_SKIP         (0U)     // Skip. Do not block, output nothing. (Default)
188 #define SEGGER_RTT_MODE_NO_BLOCK_TRIM         (1U)     // Trim: Do not block, output as much as fits.
189 #define SEGGER_RTT_MODE_BLOCK_IF_FIFO_FULL    (2U)     // Block: Wait until there is space in the buffer.
190 #define SEGGER_RTT_MODE_MASK                  (3U)
191 
192 //
193 // Control sequences, based on ANSI.
194 // Can be used to control color, and clear the screen
195 //
196 #define RTT_CTRL_RESET                ""         // Reset to default colors
197 #define RTT_CTRL_CLEAR                ""         // Clear screen, reposition cursor to top left
198 
199 #define RTT_CTRL_TEXT_BLACK           ""
200 #define RTT_CTRL_TEXT_RED             ""
201 #define RTT_CTRL_TEXT_GREEN           ""
202 #define RTT_CTRL_TEXT_YELLOW          ""
203 #define RTT_CTRL_TEXT_BLUE            ""
204 #define RTT_CTRL_TEXT_MAGENTA         ""
205 #define RTT_CTRL_TEXT_CYAN            ""
206 #define RTT_CTRL_TEXT_WHITE           ""
207 
208 #define RTT_CTRL_TEXT_BRIGHT_BLACK    ""
209 #define RTT_CTRL_TEXT_BRIGHT_RED      ""
210 #define RTT_CTRL_TEXT_BRIGHT_GREEN    ""
211 #define RTT_CTRL_TEXT_BRIGHT_YELLOW   ""
212 #define RTT_CTRL_TEXT_BRIGHT_BLUE     ""
213 #define RTT_CTRL_TEXT_BRIGHT_MAGENTA  ""
214 #define RTT_CTRL_TEXT_BRIGHT_CYAN     ""
215 #define RTT_CTRL_TEXT_BRIGHT_WHITE    ""
216 
217 #define RTT_CTRL_BG_BLACK             ""
218 #define RTT_CTRL_BG_RED               ""
219 #define RTT_CTRL_BG_GREEN             ""
220 #define RTT_CTRL_BG_YELLOW            ""
221 #define RTT_CTRL_BG_BLUE              ""
222 #define RTT_CTRL_BG_MAGENTA           ""
223 #define RTT_CTRL_BG_CYAN              ""
224 #define RTT_CTRL_BG_WHITE             ""
225 
226 #define RTT_CTRL_BG_BRIGHT_BLACK      ""
227 #define RTT_CTRL_BG_BRIGHT_RED        ""
228 #define RTT_CTRL_BG_BRIGHT_GREEN      ""
229 #define RTT_CTRL_BG_BRIGHT_YELLOW     ""
230 #define RTT_CTRL_BG_BRIGHT_BLUE       ""
231 #define RTT_CTRL_BG_BRIGHT_MAGENTA    ""
232 #define RTT_CTRL_BG_BRIGHT_CYAN       ""
233 #define RTT_CTRL_BG_BRIGHT_WHITE      ""
234 
235 
236 #endif
237 
238 /*************************** End of file ****************************/
239