Appiko
SEGGER_RTT.c
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 * 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 & Co. KG *
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.c
56 Purpose : Implementation of SEGGER real-time transfer (RTT) which
57  allows real-time communication on targets which support
58  debugger memory accesses while the CPU is running.
59 Revision: $Rev: 4351 $
60 
61 Additional information:
62  Type "int" is assumed to be 32-bits in size
63  H->T Host to target communication
64  T->H Target to host communication
65 
66  RTT channel 0 is always present and reserved for Terminal usage.
67  Name is fixed to "Terminal"
68 
69  Effective buffer size: SizeOfBuffer - 1
70 
71  WrOff == RdOff: Buffer is empty
72  WrOff == (RdOff - 1): Buffer is full
73  WrOff > RdOff: Free space includes wrap-around
74  WrOff < RdOff: Used space includes wrap-around
75  (WrOff == (SizeOfBuffer - 1)) && (RdOff == 0):
76  Buffer full and wrap-around after next byte
77 
78 
79 ----------------------------------------------------------------------
80 */
81 
82 #include "SEGGER_RTT.h"
83 
84 #include <string.h> // for memcpy
85 
86 /*********************************************************************
87 *
88 * Configuration, default values
89 *
90 **********************************************************************
91 */
92 
93 #ifndef BUFFER_SIZE_UP
94  #define BUFFER_SIZE_UP 1024 // Size of the buffer for terminal output of target, up to host
95 #endif
96 
97 #ifndef BUFFER_SIZE_DOWN
98  #define BUFFER_SIZE_DOWN 16 // Size of the buffer for terminal input to target from host (Usually keyboard input)
99 #endif
100 
101 #ifndef SEGGER_RTT_MAX_NUM_UP_BUFFERS
102  #define SEGGER_RTT_MAX_NUM_UP_BUFFERS 2 // Number of up-buffers (T->H) available on this target
103 #endif
104 
105 #ifndef SEGGER_RTT_MAX_NUM_DOWN_BUFFERS
106  #define SEGGER_RTT_MAX_NUM_DOWN_BUFFERS 2 // Number of down-buffers (H->T) available on this target
107 #endif
108 
109 #ifndef SEGGER_RTT_BUFFER_SECTION
110  #if defined(SEGGER_RTT_SECTION)
111  #define SEGGER_RTT_BUFFER_SECTION SEGGER_RTT_SECTION
112  #endif
113 #endif
114 
115 #ifndef SEGGER_RTT_ALIGNMENT
116  #define SEGGER_RTT_ALIGNMENT 0
117 #endif
118 
119 #ifndef SEGGER_RTT_BUFFER_ALIGNMENT
120  #define SEGGER_RTT_BUFFER_ALIGNMENT 0
121 #endif
122 
123 #ifndef SEGGER_RTT_MODE_DEFAULT
124  #define SEGGER_RTT_MODE_DEFAULT SEGGER_RTT_MODE_NO_BLOCK_SKIP
125 #endif
126 
127 #ifndef SEGGER_RTT_LOCK
128  #define SEGGER_RTT_LOCK()
129 #endif
130 
131 #ifndef SEGGER_RTT_UNLOCK
132  #define SEGGER_RTT_UNLOCK()
133 #endif
134 
135 #ifndef STRLEN
136  #define STRLEN(a) strlen((a))
137 #endif
138 
139 #ifndef MEMCPY
140  #define MEMCPY(pDest, pSrc, NumBytes) memcpy((pDest), (pSrc), (NumBytes))
141 #endif
142 
143 #ifndef MIN
144  #define MIN(a, b) (((a) < (b)) ? (a) : (b))
145 #endif
146 
147 #ifndef MAX
148  #define MAX(a, b) (((a) > (b)) ? (a) : (b))
149 #endif
150 //
151 // For some environments, NULL may not be defined until certain headers are included
152 //
153 #ifndef NULL
154  #define NULL 0
155 #endif
156 
157 /*********************************************************************
158 *
159 * Defines, fixed
160 *
161 **********************************************************************
162 */
163 #if (defined __ICCARM__) || (defined __ICCRX__)
164  #define RTT_PRAGMA(P) _Pragma(#P)
165 #endif
166 
167 #if SEGGER_RTT_ALIGNMENT || SEGGER_RTT_BUFFER_ALIGNMENT
168  #if (defined __GNUC__)
169  #define SEGGER_RTT_ALIGN(Var, Alignment) Var __attribute__ ((aligned (Alignment)))
170  #elif (defined __ICCARM__) || (defined __ICCRX__)
171  #define PRAGMA(A) _Pragma(#A)
172 #define SEGGER_RTT_ALIGN(Var, Alignment) RTT_PRAGMA(data_alignment=Alignment) \
173  Var
174  #elif (defined __CC_ARM__)
175  #define SEGGER_RTT_ALIGN(Var, Alignment) Var __attribute__ ((aligned (Alignment)))
176  #else
177  #error "Alignment not supported for this compiler."
178  #endif
179 #else
180  #define SEGGER_RTT_ALIGN(Var, Alignment) Var
181 #endif
182 
183 #if defined(SEGGER_RTT_SECTION) || defined (SEGGER_RTT_BUFFER_SECTION)
184  #if (defined __GNUC__)
185  #define SEGGER_RTT_PUT_SECTION(Var, Section) __attribute__ ((section (Section))) Var
186  #elif (defined __ICCARM__) || (defined __ICCRX__)
187 #define SEGGER_RTT_PUT_SECTION(Var, Section) RTT_PRAGMA(location=Section) \
188  Var
189  #elif (defined __CC_ARM__)
190  #define SEGGER_RTT_PUT_SECTION(Var, Section) __attribute__ ((section (Section), zero_init)) Var
191  #else
192  #error "Section placement not supported for this compiler."
193  #endif
194 #else
195  #define SEGGER_RTT_PUT_SECTION(Var, Section) Var
196 #endif
197 
198 
199 #if SEGGER_RTT_ALIGNMENT
200  #define SEGGER_RTT_CB_ALIGN(Var) SEGGER_RTT_ALIGN(Var, SEGGER_RTT_ALIGNMENT)
201 #else
202  #define SEGGER_RTT_CB_ALIGN(Var) Var
203 #endif
204 
205 #if SEGGER_RTT_BUFFER_ALIGNMENT
206  #define SEGGER_RTT_BUFFER_ALIGN(Var) SEGGER_RTT_ALIGN(Var, SEGGER_RTT_BUFFER_ALIGNMENT)
207 #else
208  #define SEGGER_RTT_BUFFER_ALIGN(Var) Var
209 #endif
210 
211 
212 #if defined(SEGGER_RTT_SECTION)
213  #define SEGGER_RTT_PUT_CB_SECTION(Var) SEGGER_RTT_PUT_SECTION(Var, SEGGER_RTT_SECTION)
214 #else
215  #define SEGGER_RTT_PUT_CB_SECTION(Var) Var
216 #endif
217 
218 #if defined(SEGGER_RTT_BUFFER_SECTION)
219  #define SEGGER_RTT_PUT_BUFFER_SECTION(Var) SEGGER_RTT_PUT_SECTION(Var, SEGGER_RTT_BUFFER_SECTION)
220 #else
221  #define SEGGER_RTT_PUT_BUFFER_SECTION(Var) Var
222 #endif
223 
224 /*********************************************************************
225 *
226 * Static const data
227 *
228 **********************************************************************
229 */
230 
231 static unsigned char _aTerminalId[16] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
232 
233 /*********************************************************************
234 *
235 * Static data
236 *
237 **********************************************************************
238 */
239 //
240 // RTT Control Block and allocate buffers for channel 0
241 //
242 SEGGER_RTT_PUT_CB_SECTION(SEGGER_RTT_CB_ALIGN(SEGGER_RTT_CB _SEGGER_RTT));
243 
244 SEGGER_RTT_PUT_BUFFER_SECTION(SEGGER_RTT_BUFFER_ALIGN(static char _acUpBuffer [BUFFER_SIZE_UP]));
245 SEGGER_RTT_PUT_BUFFER_SECTION(SEGGER_RTT_BUFFER_ALIGN(static char _acDownBuffer[BUFFER_SIZE_DOWN]));
246 
247 static char _ActiveTerminal;
248 
249 /*********************************************************************
250 *
251 * Static functions
252 *
253 **********************************************************************
254 */
255 
256 /*********************************************************************
257 *
258 * _DoInit()
259 *
260 * Function description
261 * Initializes the control block an buffers.
262 * May only be called via INIT() to avoid overriding settings.
263 *
264 */
265 #define INIT() do { \
266  if (_SEGGER_RTT.acID[0] == '\0') { _DoInit(); } \
267  } while (0)
268 static void _DoInit(void) {
269  SEGGER_RTT_CB* p;
270  //
271  // Initialize control block
272  //
273  p = &_SEGGER_RTT;
274  p->MaxNumUpBuffers = SEGGER_RTT_MAX_NUM_UP_BUFFERS;
275  p->MaxNumDownBuffers = SEGGER_RTT_MAX_NUM_DOWN_BUFFERS;
276  //
277  // Initialize up buffer 0
278  //
279  p->aUp[0].sName = "Terminal";
280  p->aUp[0].pBuffer = _acUpBuffer;
281  p->aUp[0].SizeOfBuffer = sizeof(_acUpBuffer);
282  p->aUp[0].RdOff = 0u;
283  p->aUp[0].WrOff = 0u;
284  p->aUp[0].Flags = SEGGER_RTT_MODE_DEFAULT;
285  //
286  // Initialize down buffer 0
287  //
288  p->aDown[0].sName = "Terminal";
289  p->aDown[0].pBuffer = _acDownBuffer;
290  p->aDown[0].SizeOfBuffer = sizeof(_acDownBuffer);
291  p->aDown[0].RdOff = 0u;
292  p->aDown[0].WrOff = 0u;
293  p->aDown[0].Flags = SEGGER_RTT_MODE_DEFAULT;
294  //
295  // Finish initialization of the control block.
296  // Copy Id string in three steps to make sure "SEGGER RTT" is not found
297  // in initializer memory (usually flash) by J-Link
298  //
299  strcpy(&p->acID[7], "RTT");
300  strcpy(&p->acID[0], "SEGGER");
301  p->acID[6] = ' ';
302 }
303 
304 /*********************************************************************
305 *
306 * _WriteBlocking()
307 *
308 * Function description
309 * Stores a specified number of characters in SEGGER RTT ring buffer
310 * and updates the associated write pointer which is periodically
311 * read by the host.
312 * The caller is responsible for managing the write chunk sizes as
313 * _WriteBlocking() will block until all data has been posted successfully.
314 *
315 * Parameters
316 * pRing Ring buffer to post to.
317 * pBuffer Pointer to character array. Does not need to point to a \0 terminated string.
318 * NumBytes Number of bytes to be stored in the SEGGER RTT control block.
319 *
320 * Return value
321 * >= 0 - Number of bytes written into buffer.
322 */
323 static unsigned _WriteBlocking(SEGGER_RTT_BUFFER_UP* pRing, const char* pBuffer, unsigned NumBytes) {
324  unsigned NumBytesToWrite;
325  unsigned NumBytesWritten;
326  unsigned RdOff;
327  unsigned WrOff;
328  //
329  // Write data to buffer and handle wrap-around if necessary
330  //
331  NumBytesWritten = 0u;
332  WrOff = pRing->WrOff;
333  do {
334  RdOff = pRing->RdOff; // May be changed by host (debug probe) in the meantime
335  if (RdOff > WrOff) {
336  NumBytesToWrite = RdOff - WrOff - 1u;
337  } else {
338  NumBytesToWrite = pRing->SizeOfBuffer - (WrOff - RdOff + 1u);
339  }
340  NumBytesToWrite = MIN(NumBytesToWrite, (pRing->SizeOfBuffer - WrOff)); // Number of bytes that can be written until buffer wrap-around
341  NumBytesToWrite = MIN(NumBytesToWrite, NumBytes);
342  memcpy(pRing->pBuffer + WrOff, pBuffer, NumBytesToWrite);
343  NumBytesWritten += NumBytesToWrite;
344  pBuffer += NumBytesToWrite;
345  NumBytes -= NumBytesToWrite;
346  WrOff += NumBytesToWrite;
347  if (WrOff == pRing->SizeOfBuffer) {
348  WrOff = 0u;
349  }
350  pRing->WrOff = WrOff;
351  } while (NumBytes);
352  //
353  return NumBytesWritten;
354 }
355 
356 /*********************************************************************
357 *
358 * _WriteNoCheck()
359 *
360 * Function description
361 * Stores a specified number of characters in SEGGER RTT ring buffer
362 * and updates the associated write pointer which is periodically
363 * read by the host.
364 * It is callers responsibility to make sure data actually fits in buffer.
365 *
366 * Parameters
367 * pRing Ring buffer to post to.
368 * pBuffer Pointer to character array. Does not need to point to a \0 terminated string.
369 * NumBytes Number of bytes to be stored in the SEGGER RTT control block.
370 *
371 * Notes
372 * (1) If there might not be enough space in the "Up"-buffer, call _WriteBlocking
373 */
374 static void _WriteNoCheck(SEGGER_RTT_BUFFER_UP* pRing, const char* pData, unsigned NumBytes) {
375  unsigned NumBytesAtOnce;
376  unsigned WrOff;
377  unsigned Rem;
378 
379  WrOff = pRing->WrOff;
380  Rem = pRing->SizeOfBuffer - WrOff;
381  if (Rem > NumBytes) {
382  //
383  // All data fits before wrap around
384  //
385  memcpy(pRing->pBuffer + WrOff, pData, NumBytes);
386  pRing->WrOff = WrOff + NumBytes;
387  } else {
388  //
389  // We reach the end of the buffer, so need to wrap around
390  //
391  NumBytesAtOnce = Rem;
392  memcpy(pRing->pBuffer + WrOff, pData, NumBytesAtOnce);
393  NumBytesAtOnce = NumBytes - Rem;
394  memcpy(pRing->pBuffer, pData + Rem, NumBytesAtOnce);
395  pRing->WrOff = NumBytesAtOnce;
396  }
397 }
398 
399 /*********************************************************************
400 *
401 * _PostTerminalSwitch()
402 *
403 * Function description
404 * Switch terminal to the given terminal ID. It is the caller's
405 * responsibility to ensure the terminal ID is correct and there is
406 * enough space in the buffer for this to complete successfully.
407 *
408 * Parameters
409 * pRing Ring buffer to post to.
410 * TerminalId Terminal ID to switch to.
411 */
412 static void _PostTerminalSwitch(SEGGER_RTT_BUFFER_UP* pRing, unsigned char TerminalId) {
413  char ac[2];
414 
415  ac[0] = 0xFFu;
416  ac[1] = _aTerminalId[TerminalId]; // Caller made already sure that TerminalId does not exceed our terminal limit
417  _WriteBlocking(pRing, ac, 2u);
418 }
419 
420 /*********************************************************************
421 *
422 * _GetAvailWriteSpace()
423 *
424 * Function description
425 * Returns the number of bytes that can be written to the ring
426 * buffer without blocking.
427 *
428 * Parameters
429 * pRing Ring buffer to check.
430 *
431 * Return value
432 * Number of bytes that are free in the buffer.
433 */
434 static unsigned _GetAvailWriteSpace(SEGGER_RTT_BUFFER_UP* pRing) {
435  unsigned RdOff;
436  unsigned WrOff;
437  unsigned r;
438  //
439  // Avoid warnings regarding volatile access order. It's not a problem
440  // in this case, but dampen compiler enthusiasm.
441  //
442  RdOff = pRing->RdOff;
443  WrOff = pRing->WrOff;
444  if (RdOff <= WrOff) {
445  r = pRing->SizeOfBuffer - 1u - WrOff + RdOff;
446  } else {
447  r = RdOff - WrOff - 1u;
448  }
449  return r;
450 }
451 
452 /*********************************************************************
453 *
454 * Public code
455 *
456 **********************************************************************
457 */
458 /*********************************************************************
459 *
460 * SEGGER_RTT_ReadNoLock()
461 *
462 * Function description
463 * Reads characters from SEGGER real-time-terminal control block
464 * which have been previously stored by the host.
465 * Do not lock against interrupts and multiple access.
466 *
467 * Parameters
468 * BufferIndex Index of Down-buffer to be used (e.g. 0 for "Terminal").
469 * pBuffer Pointer to buffer provided by target application, to copy characters from RTT-down-buffer to.
470 * BufferSize Size of the target application buffer.
471 *
472 * Return value
473 * Number of bytes that have been read.
474 */
475 unsigned SEGGER_RTT_ReadNoLock(unsigned BufferIndex, void* pData, unsigned BufferSize) {
476  unsigned NumBytesRem;
477  unsigned NumBytesRead;
478  unsigned RdOff;
479  unsigned WrOff;
480  unsigned char* pBuffer;
481  SEGGER_RTT_BUFFER_DOWN* pRing;
482  //
483  INIT();
484  pRing = &_SEGGER_RTT.aDown[BufferIndex];
485  pBuffer = (unsigned char*)pData;
486  RdOff = pRing->RdOff;
487  WrOff = pRing->WrOff;
488  NumBytesRead = 0u;
489  //
490  // Read from current read position to wrap-around of buffer, first
491  //
492  if (RdOff > WrOff) {
493  NumBytesRem = pRing->SizeOfBuffer - RdOff;
494  NumBytesRem = MIN(NumBytesRem, BufferSize);
495  memcpy(pBuffer, pRing->pBuffer + RdOff, NumBytesRem);
496  NumBytesRead += NumBytesRem;
497  pBuffer += NumBytesRem;
498  BufferSize -= NumBytesRem;
499  RdOff += NumBytesRem;
500  //
501  // Handle wrap-around of buffer
502  //
503  if (RdOff == pRing->SizeOfBuffer) {
504  RdOff = 0u;
505  }
506  }
507  //
508  // Read remaining items of buffer
509  //
510  NumBytesRem = WrOff - RdOff;
511  NumBytesRem = MIN(NumBytesRem, BufferSize);
512  if (NumBytesRem > 0u) {
513  memcpy(pBuffer, pRing->pBuffer + RdOff, NumBytesRem);
514  NumBytesRead += NumBytesRem;
515  pBuffer += NumBytesRem;
516  BufferSize -= NumBytesRem;
517  RdOff += NumBytesRem;
518  }
519  if (NumBytesRead) {
520  pRing->RdOff = RdOff;
521  }
522  //
523  return NumBytesRead;
524 }
525 
526 /*********************************************************************
527 *
528 * SEGGER_RTT_Read
529 *
530 * Function description
531 * Reads characters from SEGGER real-time-terminal control block
532 * which have been previously stored by the host.
533 *
534 * Parameters
535 * BufferIndex Index of Down-buffer to be used (e.g. 0 for "Terminal").
536 * pBuffer Pointer to buffer provided by target application, to copy characters from RTT-down-buffer to.
537 * BufferSize Size of the target application buffer.
538 *
539 * Return value
540 * Number of bytes that have been read.
541 */
542 unsigned SEGGER_RTT_Read(unsigned BufferIndex, void* pBuffer, unsigned BufferSize) {
543  unsigned NumBytesRead;
544  //
545  SEGGER_RTT_LOCK();
546  //
547  // Call the non-locking read function
548  //
549  NumBytesRead = SEGGER_RTT_ReadNoLock(BufferIndex, pBuffer, BufferSize);
550  //
551  // Finish up.
552  //
553  SEGGER_RTT_UNLOCK();
554  //
555  return NumBytesRead;
556 }
557 
558 /*********************************************************************
559 *
560 * SEGGER_RTT_WriteWithOverwriteNoLock
561 *
562 * Function description
563 * Stores a specified number of characters in SEGGER RTT
564 * control block.
565 * SEGGER_RTT_WriteWithOverwriteNoLock does not lock the application
566 * and overwrites data if the data does not fit into the buffer.
567 *
568 * Parameters
569 * BufferIndex Index of "Up"-buffer to be used (e.g. 0 for "Terminal").
570 * pBuffer Pointer to character array. Does not need to point to a \0 terminated string.
571 * NumBytes Number of bytes to be stored in the SEGGER RTT control block.
572 *
573 * Notes
574 * (1) If there is not enough space in the "Up"-buffer, data is overwritten.
575 * (2) For performance reasons this function does not call Init()
576 * and may only be called after RTT has been initialized.
577 * Either by calling SEGGER_RTT_Init() or calling another RTT API function first.
578 * (3) Do not use SEGGER_RTT_WriteWithOverwriteNoLock if a J-Link
579 * connection reads RTT data.
580 */
581 void SEGGER_RTT_WriteWithOverwriteNoLock(unsigned BufferIndex, const void* pBuffer, unsigned NumBytes) {
582  const char* pData;
583  SEGGER_RTT_BUFFER_UP* pRing;
584  unsigned Avail;
585 
586  pData = (const char *)pBuffer;
587  //
588  // Get "to-host" ring buffer and copy some elements into local variables.
589  //
590  pRing = &_SEGGER_RTT.aUp[BufferIndex];
591  //
592  // Check if we will overwrite data and need to adjust the RdOff.
593  //
594  if (pRing->WrOff == pRing->RdOff) {
595  Avail = pRing->SizeOfBuffer - 1u;
596  } else if ( pRing->WrOff < pRing->RdOff) {
597  Avail = pRing->RdOff - pRing->WrOff - 1u;
598  } else {
599  Avail = pRing->RdOff - pRing->WrOff - 1u + pRing->SizeOfBuffer;
600  }
601  if (NumBytes > Avail) {
602  pRing->RdOff += (NumBytes - Avail);
603  while (pRing->RdOff >= pRing->SizeOfBuffer) {
604  pRing->RdOff -= pRing->SizeOfBuffer;
605  }
606  }
607  //
608  // Write all data, no need to check the RdOff, but possibly handle multiple wrap-arounds
609  //
610  Avail = pRing->SizeOfBuffer - pRing->WrOff;
611  do {
612  if (Avail > NumBytes) {
613  //
614  // Last round
615  //
616 #if 1 // memcpy() is good for large amounts of data, but the overhead is too big for small amounts. Use a simple byte loop instead.
617  char* pDst;
618  pDst = pRing->pBuffer + pRing->WrOff;
619  pRing->WrOff += NumBytes;
620  do {
621  *pDst++ = *pData++;
622  } while (--NumBytes);
623 #else
624  memcpy(pRing->pBuffer + WrOff, pData, NumBytes);
625  pRing->WrOff += NumBytes;
626 #endif
627  break; //Alternatively: NumBytes = 0;
628  } else {
629  //
630  // Wrap-around necessary, write until wrap-around and reset WrOff
631  //
632  memcpy(pRing->pBuffer + pRing->WrOff, pData, Avail);
633  pData += Avail;
634  pRing->WrOff = 0;
635  NumBytes -= Avail;
636  Avail = (pRing->SizeOfBuffer - 1);
637  }
638  } while (NumBytes);
639 }
640 
641 /*********************************************************************
642 *
643 * SEGGER_RTT_WriteSkipNoLock
644 *
645 * Function description
646 * Stores a specified number of characters in SEGGER RTT
647 * control block which is then read by the host.
648 * SEGGER_RTT_WriteSkipNoLock does not lock the application and
649 * skips all data, if the data does not fit into the buffer.
650 *
651 * Parameters
652 * BufferIndex Index of "Up"-buffer to be used (e.g. 0 for "Terminal").
653 * pBuffer Pointer to character array. Does not need to point to a \0 terminated string.
654 * NumBytes Number of bytes to be stored in the SEGGER RTT control block.
655 *
656 * Return value
657 * Number of bytes which have been stored in the "Up"-buffer.
658 *
659 * Notes
660 * (1) If there is not enough space in the "Up"-buffer, all data is dropped.
661 * (2) For performance reasons this function does not call Init()
662 * and may only be called after RTT has been initialized.
663 * Either by calling SEGGER_RTT_Init() or calling another RTT API function first.
664 */
665 unsigned SEGGER_RTT_WriteSkipNoLock(unsigned BufferIndex, const void* pBuffer, unsigned NumBytes) {
666  const char* pData;
667  SEGGER_RTT_BUFFER_UP* pRing;
668  unsigned Avail;
669  unsigned RdOff;
670  unsigned WrOff;
671  unsigned Rem;
672 
673  pData = (const char *)pBuffer;
674  //
675  // Get "to-host" ring buffer and copy some elements into local variables.
676  //
677  pRing = &_SEGGER_RTT.aUp[BufferIndex];
678  RdOff = pRing->RdOff;
679  WrOff = pRing->WrOff;
680  //
681  // Handle the most common cases fastest.
682  // Which is:
683  // RdOff <= WrOff -> Space until wrap around is free.
684  // AND
685  // WrOff + NumBytes < SizeOfBuffer -> No Wrap around necessary.
686  //
687  // OR
688  //
689  // RdOff > WrOff -> Space until RdOff - 1 is free.
690  // AND
691  // WrOff + NumBytes < RdOff -> Data fits into buffer
692  //
693  if (RdOff <= WrOff) {
694  //
695  // Get space until WrOff will be at wrap around.
696  //
697  Avail = pRing->SizeOfBuffer - 1u - WrOff ;
698  if (Avail >= NumBytes) {
699 #if 1 // memcpy() is good for large amounts of data, but the overhead is too big for small amounts. Use a simple byte loop instead.
700  char* pDst;
701  pDst = pRing->pBuffer + WrOff;
702  WrOff += NumBytes;
703  do {
704  *pDst++ = *pData++;
705  } while (--NumBytes);
706  pRing->WrOff = WrOff + NumBytes;
707 #else
708  memcpy(pRing->pBuffer + WrOff, pData, NumBytes);
709  pRing->WrOff = WrOff + NumBytes;
710 #endif
711  return 1;
712  }
713  //
714  // If data did not fit into space until wrap around calculate complete space in buffer.
715  //
716  Avail += RdOff;
717  //
718  // If there is still no space for the whole of this output, don't bother.
719  //
720  if (Avail >= NumBytes) {
721  //
722  // OK, we have enough space in buffer. Copy in one or 2 chunks
723  //
724  Rem = pRing->SizeOfBuffer - WrOff; // Space until end of buffer
725  if (Rem > NumBytes) {
726  memcpy(pRing->pBuffer + WrOff, pData, NumBytes);
727  pRing->WrOff = WrOff + NumBytes;
728  } else {
729  //
730  // We reach the end of the buffer, so need to wrap around
731  //
732  memcpy(pRing->pBuffer + WrOff, pData, Rem);
733  memcpy(pRing->pBuffer, pData + Rem, NumBytes - Rem);
734  pRing->WrOff = NumBytes - Rem;
735  }
736  return 1;
737  }
738  } else {
739  Avail = RdOff - WrOff - 1u;
740  if (Avail >= NumBytes) {
741  memcpy(pRing->pBuffer + WrOff, pData, NumBytes);
742  pRing->WrOff = WrOff + NumBytes;
743  return 1;
744  }
745  }
746  //
747  // If we reach this point no data has been written
748  //
749  return 0;
750 }
751 
752 /*********************************************************************
753 *
754 * SEGGER_RTT_WriteNoLock
755 *
756 * Function description
757 * Stores a specified number of characters in SEGGER RTT
758 * control block which is then read by the host.
759 * SEGGER_RTT_WriteNoLock does not lock the application.
760 *
761 * Parameters
762 * BufferIndex Index of "Up"-buffer to be used (e.g. 0 for "Terminal").
763 * pBuffer Pointer to character array. Does not need to point to a \0 terminated string.
764 * NumBytes Number of bytes to be stored in the SEGGER RTT control block.
765 *
766 * Return value
767 * Number of bytes which have been stored in the "Up"-buffer.
768 *
769 * Notes
770 * (1) If there is not enough space in the "Up"-buffer, remaining characters of pBuffer are dropped.
771 * (2) For performance reasons this function does not call Init()
772 * and may only be called after RTT has been initialized.
773 * Either by calling SEGGER_RTT_Init() or calling another RTT API function first.
774 */
775 unsigned SEGGER_RTT_WriteNoLock(unsigned BufferIndex, const void* pBuffer, unsigned NumBytes) {
776  unsigned Status;
777  unsigned Avail;
778  const char* pData;
779  SEGGER_RTT_BUFFER_UP* pRing;
780 
781  pData = (const char *)pBuffer;
782  //
783  // Get "to-host" ring buffer.
784  //
785  pRing = &_SEGGER_RTT.aUp[BufferIndex];
786  //
787  // How we output depends upon the mode...
788  //
789  switch (pRing->Flags) {
790  case SEGGER_RTT_MODE_NO_BLOCK_SKIP:
791  //
792  // If we are in skip mode and there is no space for the whole
793  // of this output, don't bother.
794  //
795  Avail = _GetAvailWriteSpace(pRing);
796  if (Avail < NumBytes) {
797  Status = 0u;
798  } else {
799  Status = NumBytes;
800  _WriteNoCheck(pRing, pData, NumBytes);
801  }
802  break;
803  case SEGGER_RTT_MODE_NO_BLOCK_TRIM:
804  //
805  // If we are in trim mode, trim to what we can output without blocking.
806  //
807  Avail = _GetAvailWriteSpace(pRing);
808  Status = Avail < NumBytes ? Avail : NumBytes;
809  _WriteNoCheck(pRing, pData, Status);
810  break;
811  case SEGGER_RTT_MODE_BLOCK_IF_FIFO_FULL:
812  //
813  // If we are in blocking mode, output everything.
814  //
815  Status = _WriteBlocking(pRing, pData, NumBytes);
816  break;
817  default:
818  Status = 0u;
819  break;
820  }
821  //
822  // Finish up.
823  //
824  return Status;
825 }
826 
827 /*********************************************************************
828 *
829 * SEGGER_RTT_Write
830 *
831 * Function description
832 * Stores a specified number of characters in SEGGER RTT
833 * control block which is then read by the host.
834 *
835 * Parameters
836 * BufferIndex Index of "Up"-buffer to be used (e.g. 0 for "Terminal").
837 * pBuffer Pointer to character array. Does not need to point to a \0 terminated string.
838 * NumBytes Number of bytes to be stored in the SEGGER RTT control block.
839 *
840 * Return value
841 * Number of bytes which have been stored in the "Up"-buffer.
842 *
843 * Notes
844 * (1) If there is not enough space in the "Up"-buffer, remaining characters of pBuffer are dropped.
845 */
846 unsigned SEGGER_RTT_Write(unsigned BufferIndex, const void* pBuffer, unsigned NumBytes) {
847  unsigned Status;
848  //
849  INIT();
850  SEGGER_RTT_LOCK();
851  //
852  // Call the non-locking write function
853  //
854  Status = SEGGER_RTT_WriteNoLock(BufferIndex, pBuffer, NumBytes);
855  //
856  // Finish up.
857  //
858  SEGGER_RTT_UNLOCK();
859  //
860  return Status;
861 }
862 
863 /*********************************************************************
864 *
865 * SEGGER_RTT_WriteString
866 *
867 * Function description
868 * Stores string in SEGGER RTT control block.
869 * This data is read by the host.
870 *
871 * Parameters
872 * BufferIndex Index of "Up"-buffer to be used (e.g. 0 for "Terminal").
873 * s Pointer to string.
874 *
875 * Return value
876 * Number of bytes which have been stored in the "Up"-buffer.
877 *
878 * Notes
879 * (1) If there is not enough space in the "Up"-buffer, depending on configuration,
880 * remaining characters may be dropped or RTT module waits until there is more space in the buffer.
881 * (2) String passed to this function has to be \0 terminated
882 * (3) \0 termination character is *not* stored in RTT buffer
883 */
884 unsigned SEGGER_RTT_WriteString(unsigned BufferIndex, const char* s) {
885  unsigned Len;
886 
887  Len = STRLEN(s);
888  return SEGGER_RTT_Write(BufferIndex, s, Len);
889 }
890 
891 /*********************************************************************
892 *
893 * SEGGER_RTT_GetKey
894 *
895 * Function description
896 * Reads one character from the SEGGER RTT buffer.
897 * Host has previously stored data there.
898 *
899 * Return value
900 * < 0 - No character available (buffer empty).
901 * >= 0 - Character which has been read. (Possible values: 0 - 255)
902 *
903 * Notes
904 * (1) This function is only specified for accesses to RTT buffer 0.
905 */
906 int SEGGER_RTT_GetKey(void) {
907  char c;
908  int r;
909 
910  r = (int)SEGGER_RTT_Read(0u, &c, 1u);
911  if (r == 1) {
912  r = (int)(unsigned char)c;
913  } else {
914  r = -1;
915  }
916  return r;
917 }
918 
919 /*********************************************************************
920 *
921 * SEGGER_RTT_WaitKey
922 *
923 * Function description
924 * Waits until at least one character is avaible in the SEGGER RTT buffer.
925 * Once a character is available, it is read and this function returns.
926 *
927 * Return value
928 * >=0 - Character which has been read.
929 *
930 * Notes
931 * (1) This function is only specified for accesses to RTT buffer 0
932 * (2) This function is blocking if no character is present in RTT buffer
933 */
934 int SEGGER_RTT_WaitKey(void) {
935  int r;
936 
937  do {
938  r = SEGGER_RTT_GetKey();
939  } while (r < 0);
940  return r;
941 }
942 
943 /*********************************************************************
944 *
945 * SEGGER_RTT_HasKey
946 *
947 * Function description
948 * Checks if at least one character for reading is available in the SEGGER RTT buffer.
949 *
950 * Return value
951 * == 0 - No characters are available to read.
952 * == 1 - At least one character is available.
953 *
954 * Notes
955 * (1) This function is only specified for accesses to RTT buffer 0
956 */
957 int SEGGER_RTT_HasKey(void) {
958  unsigned RdOff;
959  int r;
960 
961  INIT();
962  RdOff = _SEGGER_RTT.aDown[0].RdOff;
963  if (RdOff != _SEGGER_RTT.aDown[0].WrOff) {
964  r = 1;
965  } else {
966  r = 0;
967  }
968  return r;
969 }
970 
971 /*********************************************************************
972 *
973 * SEGGER_RTT_HasData
974 *
975 * Function description
976 * Check if there is data from the host in the given buffer.
977 *
978 * Return value:
979 * ==0: No data
980 * !=0: Data in buffer
981 *
982 */
983 unsigned SEGGER_RTT_HasData(unsigned BufferIndex) {
984  SEGGER_RTT_BUFFER_DOWN* pRing;
985  unsigned v;
986 
987  pRing = &_SEGGER_RTT.aDown[BufferIndex];
988  v = pRing->WrOff;
989  return v - pRing->RdOff;
990 }
991 
992 /*********************************************************************
993 *
994 * SEGGER_RTT_AllocDownBuffer
995 *
996 * Function description
997 * Run-time configuration of the next down-buffer (H->T).
998 * The next buffer, which is not used yet is configured.
999 * This includes: Buffer address, size, name, flags, ...
1000 *
1001 * Parameters
1002 * sName Pointer to a constant name string.
1003 * pBuffer Pointer to a buffer to be used.
1004 * BufferSize Size of the buffer.
1005 * Flags Operating modes. Define behavior if buffer is full (not enough space for entire message).
1006 *
1007 * Return value
1008 * >= 0 - O.K. Buffer Index
1009 * < 0 - Error
1010 */
1011 int SEGGER_RTT_AllocDownBuffer(const char* sName, void* pBuffer, unsigned BufferSize, unsigned Flags) {
1012  int BufferIndex;
1013 
1014  INIT();
1015  SEGGER_RTT_LOCK();
1016  BufferIndex = 0;
1017  do {
1018  if (_SEGGER_RTT.aDown[BufferIndex].pBuffer == NULL) {
1019  break;
1020  }
1021  BufferIndex++;
1022  } while (BufferIndex < _SEGGER_RTT.MaxNumDownBuffers);
1023  if (BufferIndex < _SEGGER_RTT.MaxNumDownBuffers) {
1024  _SEGGER_RTT.aDown[BufferIndex].sName = sName;
1025  _SEGGER_RTT.aDown[BufferIndex].pBuffer = (char*)pBuffer;
1026  _SEGGER_RTT.aDown[BufferIndex].SizeOfBuffer = BufferSize;
1027  _SEGGER_RTT.aDown[BufferIndex].RdOff = 0u;
1028  _SEGGER_RTT.aDown[BufferIndex].WrOff = 0u;
1029  _SEGGER_RTT.aDown[BufferIndex].Flags = Flags;
1030  } else {
1031  BufferIndex = -1;
1032  }
1033  SEGGER_RTT_UNLOCK();
1034  return BufferIndex;
1035 }
1036 
1037 /*********************************************************************
1038 *
1039 * SEGGER_RTT_AllocUpBuffer
1040 *
1041 * Function description
1042 * Run-time configuration of the next up-buffer (T->H).
1043 * The next buffer, which is not used yet is configured.
1044 * This includes: Buffer address, size, name, flags, ...
1045 *
1046 * Parameters
1047 * sName Pointer to a constant name string.
1048 * pBuffer Pointer to a buffer to be used.
1049 * BufferSize Size of the buffer.
1050 * Flags Operating modes. Define behavior if buffer is full (not enough space for entire message).
1051 *
1052 * Return value
1053 * >= 0 - O.K. Buffer Index
1054 * < 0 - Error
1055 */
1056 int SEGGER_RTT_AllocUpBuffer(const char* sName, void* pBuffer, unsigned BufferSize, unsigned Flags) {
1057  int BufferIndex;
1058 
1059  INIT();
1060  SEGGER_RTT_LOCK();
1061  BufferIndex = 0;
1062  do {
1063  if (_SEGGER_RTT.aUp[BufferIndex].pBuffer == NULL) {
1064  break;
1065  }
1066  BufferIndex++;
1067  } while (BufferIndex < _SEGGER_RTT.MaxNumUpBuffers);
1068  if (BufferIndex < _SEGGER_RTT.MaxNumUpBuffers) {
1069  _SEGGER_RTT.aUp[BufferIndex].sName = sName;
1070  _SEGGER_RTT.aUp[BufferIndex].pBuffer = (char*)pBuffer;
1071  _SEGGER_RTT.aUp[BufferIndex].SizeOfBuffer = BufferSize;
1072  _SEGGER_RTT.aUp[BufferIndex].RdOff = 0u;
1073  _SEGGER_RTT.aUp[BufferIndex].WrOff = 0u;
1074  _SEGGER_RTT.aUp[BufferIndex].Flags = Flags;
1075  } else {
1076  BufferIndex = -1;
1077  }
1078  SEGGER_RTT_UNLOCK();
1079  return BufferIndex;
1080 }
1081 
1082 /*********************************************************************
1083 *
1084 * SEGGER_RTT_ConfigUpBuffer
1085 *
1086 * Function description
1087 * Run-time configuration of a specific up-buffer (T->H).
1088 * Buffer to be configured is specified by index.
1089 * This includes: Buffer address, size, name, flags, ...
1090 *
1091 * Parameters
1092 * BufferIndex Index of the buffer to configure.
1093 * sName Pointer to a constant name string.
1094 * pBuffer Pointer to a buffer to be used.
1095 * BufferSize Size of the buffer.
1096 * Flags Operating modes. Define behavior if buffer is full (not enough space for entire message).
1097 *
1098 * Return value
1099 * >= 0 - O.K.
1100 * < 0 - Error
1101 *
1102 * Additional information
1103 * Buffer 0 is configured on compile-time.
1104 * May only be called once per buffer.
1105 * Buffer name and flags can be reconfigured using the appropriate functions.
1106 */
1107 int SEGGER_RTT_ConfigUpBuffer(unsigned BufferIndex, const char* sName, void* pBuffer, unsigned BufferSize, unsigned Flags) {
1108  int r;
1109 
1110  INIT();
1111  if (BufferIndex < (unsigned)_SEGGER_RTT.MaxNumUpBuffers) {
1112  SEGGER_RTT_LOCK();
1113  if (BufferIndex > 0u) {
1114  _SEGGER_RTT.aUp[BufferIndex].sName = sName;
1115  _SEGGER_RTT.aUp[BufferIndex].pBuffer = (char*)pBuffer;
1116  _SEGGER_RTT.aUp[BufferIndex].SizeOfBuffer = BufferSize;
1117  _SEGGER_RTT.aUp[BufferIndex].RdOff = 0u;
1118  _SEGGER_RTT.aUp[BufferIndex].WrOff = 0u;
1119  }
1120  _SEGGER_RTT.aUp[BufferIndex].Flags = Flags;
1121  SEGGER_RTT_UNLOCK();
1122  r = 0;
1123  } else {
1124  r = -1;
1125  }
1126  return r;
1127 }
1128 
1129 /*********************************************************************
1130 *
1131 * SEGGER_RTT_ConfigDownBuffer
1132 *
1133 * Function description
1134 * Run-time configuration of a specific down-buffer (H->T).
1135 * Buffer to be configured is specified by index.
1136 * This includes: Buffer address, size, name, flags, ...
1137 *
1138 * Parameters
1139 * BufferIndex Index of the buffer to configure.
1140 * sName Pointer to a constant name string.
1141 * pBuffer Pointer to a buffer to be used.
1142 * BufferSize Size of the buffer.
1143 * Flags Operating modes. Define behavior if buffer is full (not enough space for entire message).
1144 *
1145 * Return value
1146 * >= 0 O.K.
1147 * < 0 Error
1148 *
1149 * Additional information
1150 * Buffer 0 is configured on compile-time.
1151 * May only be called once per buffer.
1152 * Buffer name and flags can be reconfigured using the appropriate functions.
1153 */
1154 int SEGGER_RTT_ConfigDownBuffer(unsigned BufferIndex, const char* sName, void* pBuffer, unsigned BufferSize, unsigned Flags) {
1155  int r;
1156 
1157  INIT();
1158  if (BufferIndex < (unsigned)_SEGGER_RTT.MaxNumDownBuffers) {
1159  SEGGER_RTT_LOCK();
1160  if (BufferIndex > 0u) {
1161  _SEGGER_RTT.aDown[BufferIndex].sName = sName;
1162  _SEGGER_RTT.aDown[BufferIndex].pBuffer = (char*)pBuffer;
1163  _SEGGER_RTT.aDown[BufferIndex].SizeOfBuffer = BufferSize;
1164  _SEGGER_RTT.aDown[BufferIndex].RdOff = 0u;
1165  _SEGGER_RTT.aDown[BufferIndex].WrOff = 0u;
1166  }
1167  _SEGGER_RTT.aDown[BufferIndex].Flags = Flags;
1168  SEGGER_RTT_UNLOCK();
1169  r = 0;
1170  } else {
1171  r = -1;
1172  }
1173  return r;
1174 }
1175 
1176 /*********************************************************************
1177 *
1178 * SEGGER_RTT_SetNameUpBuffer
1179 *
1180 * Function description
1181 * Run-time configuration of a specific up-buffer name (T->H).
1182 * Buffer to be configured is specified by index.
1183 *
1184 * Parameters
1185 * BufferIndex Index of the buffer to renamed.
1186 * sName Pointer to a constant name string.
1187 *
1188 * Return value
1189 * >= 0 O.K.
1190 * < 0 Error
1191 */
1192 int SEGGER_RTT_SetNameUpBuffer(unsigned BufferIndex, const char* sName) {
1193  int r;
1194 
1195  INIT();
1196  if (BufferIndex < (unsigned)_SEGGER_RTT.MaxNumUpBuffers) {
1197  SEGGER_RTT_LOCK();
1198  _SEGGER_RTT.aUp[BufferIndex].sName = sName;
1199  SEGGER_RTT_UNLOCK();
1200  r = 0;
1201  } else {
1202  r = -1;
1203  }
1204  return r;
1205 }
1206 
1207 /*********************************************************************
1208 *
1209 * SEGGER_RTT_SetNameDownBuffer
1210 *
1211 * Function description
1212 * Run-time configuration of a specific Down-buffer name (T->H).
1213 * Buffer to be configured is specified by index.
1214 *
1215 * Parameters
1216 * BufferIndex Index of the buffer to renamed.
1217 * sName Pointer to a constant name string.
1218 *
1219 * Return value
1220 * >= 0 O.K.
1221 * < 0 Error
1222 */
1223 int SEGGER_RTT_SetNameDownBuffer(unsigned BufferIndex, const char* sName) {
1224  int r;
1225 
1226  INIT();
1227  if (BufferIndex < (unsigned)_SEGGER_RTT.MaxNumDownBuffers) {
1228  SEGGER_RTT_LOCK();
1229  _SEGGER_RTT.aDown[BufferIndex].sName = sName;
1230  SEGGER_RTT_UNLOCK();
1231  r = 0;
1232  } else {
1233  r = -1;
1234  }
1235  return r;
1236 }
1237 
1238 /*********************************************************************
1239 *
1240 * SEGGER_RTT_SetFlagsUpBuffer
1241 *
1242 * Function description
1243 * Run-time configuration of specific up-buffer flags (T->H).
1244 * Buffer to be configured is specified by index.
1245 *
1246 * Parameters
1247 * BufferIndex Index of the buffer.
1248 * Flags Flags to set for the buffer.
1249 *
1250 * Return value
1251 * >= 0 O.K.
1252 * < 0 Error
1253 */
1254 int SEGGER_RTT_SetFlagsUpBuffer(unsigned BufferIndex, unsigned Flags) {
1255  int r;
1256 
1257  INIT();
1258  if (BufferIndex < (unsigned)_SEGGER_RTT.MaxNumUpBuffers) {
1259  SEGGER_RTT_LOCK();
1260  _SEGGER_RTT.aUp[BufferIndex].Flags = Flags;
1261  SEGGER_RTT_UNLOCK();
1262  r = 0;
1263  } else {
1264  r = -1;
1265  }
1266  return r;
1267 }
1268 
1269 /*********************************************************************
1270 *
1271 * SEGGER_RTT_SetFlagsDownBuffer
1272 *
1273 * Function description
1274 * Run-time configuration of specific Down-buffer flags (T->H).
1275 * Buffer to be configured is specified by index.
1276 *
1277 * Parameters
1278 * BufferIndex Index of the buffer to renamed.
1279 * Flags Flags to set for the buffer.
1280 *
1281 * Return value
1282 * >= 0 O.K.
1283 * < 0 Error
1284 */
1285 int SEGGER_RTT_SetFlagsDownBuffer(unsigned BufferIndex, unsigned Flags) {
1286  int r;
1287 
1288  INIT();
1289  if (BufferIndex < (unsigned)_SEGGER_RTT.MaxNumDownBuffers) {
1290  SEGGER_RTT_LOCK();
1291  _SEGGER_RTT.aDown[BufferIndex].Flags = Flags;
1292  SEGGER_RTT_UNLOCK();
1293  r = 0;
1294  } else {
1295  r = -1;
1296  }
1297  return r;
1298 }
1299 
1300 /*********************************************************************
1301 *
1302 * SEGGER_RTT_Init
1303 *
1304 * Function description
1305 * Initializes the RTT Control Block.
1306 * Should be used in RAM targets, at start of the application.
1307 *
1308 */
1309 void SEGGER_RTT_Init (void) {
1310  _DoInit();
1311 }
1312 
1313 /*********************************************************************
1314 *
1315 * SEGGER_RTT_SetTerminal
1316 *
1317 * Function description
1318 * Sets the terminal to be used for output on channel 0.
1319 *
1320 * Parameters
1321 * TerminalId Index of the terminal.
1322 *
1323 * Return value
1324 * >= 0 O.K.
1325 * < 0 Error (e.g. if RTT is configured for non-blocking mode and there was no space in the buffer to set the new terminal Id)
1326 */
1327 int SEGGER_RTT_SetTerminal (char TerminalId) {
1328  char ac[2];
1329  SEGGER_RTT_BUFFER_UP* pRing;
1330  unsigned Avail;
1331  int r;
1332  //
1333  INIT();
1334  //
1335  r = 0;
1336  ac[0] = 0xFFU;
1337  if ((unsigned char)TerminalId < (unsigned char)sizeof(_aTerminalId)) { // We only support a certain number of channels
1338  ac[1] = _aTerminalId[(unsigned char)TerminalId];
1339  pRing = &_SEGGER_RTT.aUp[0]; // Buffer 0 is always reserved for terminal I/O, so we can use index 0 here, fixed
1340  SEGGER_RTT_LOCK(); // Lock to make sure that no other task is writing into buffer, while we are and number of free bytes in buffer does not change downwards after checking and before writing
1341  if ((pRing->Flags & SEGGER_RTT_MODE_MASK) == SEGGER_RTT_MODE_BLOCK_IF_FIFO_FULL) {
1342  _ActiveTerminal = TerminalId;
1343  _WriteBlocking(pRing, ac, 2u);
1344  } else { // Skipping mode or trim mode? => We cannot trim this command so handling is the same for both modes
1345  Avail = _GetAvailWriteSpace(pRing);
1346  if (Avail >= 2) {
1347  _ActiveTerminal = TerminalId; // Only change active terminal in case of success
1348  _WriteNoCheck(pRing, ac, 2u);
1349  } else {
1350  r = -1;
1351  }
1352  }
1353  SEGGER_RTT_UNLOCK();
1354  } else {
1355  r = -1;
1356  }
1357  return r;
1358 }
1359 
1360 /*********************************************************************
1361 *
1362 * SEGGER_RTT_TerminalOut
1363 *
1364 * Function description
1365 * Writes a string to the given terminal
1366 * without changing the terminal for channel 0.
1367 *
1368 * Parameters
1369 * TerminalId Index of the terminal.
1370 * s String to be printed on the terminal.
1371 *
1372 * Return value
1373 * >= 0 - Number of bytes written.
1374 * < 0 - Error.
1375 *
1376 */
1377 int SEGGER_RTT_TerminalOut (char TerminalId, const char* s) {
1378  int Status;
1379  unsigned FragLen;
1380  unsigned Avail;
1381  SEGGER_RTT_BUFFER_UP* pRing;
1382  //
1383  INIT();
1384  //
1385  // Validate terminal ID.
1386  //
1387  if (TerminalId < (char)sizeof(_aTerminalId)) { // We only support a certain number of channels
1388  //
1389  // Get "to-host" ring buffer.
1390  //
1391  pRing = &_SEGGER_RTT.aUp[0];
1392  //
1393  // Need to be able to change terminal, write data, change back.
1394  // Compute the fixed and variable sizes.
1395  //
1396  FragLen = strlen(s);
1397  //
1398  // How we output depends upon the mode...
1399  //
1400  SEGGER_RTT_LOCK();
1401  Avail = _GetAvailWriteSpace(pRing);
1402  switch (pRing->Flags & SEGGER_RTT_MODE_MASK) {
1403  case SEGGER_RTT_MODE_NO_BLOCK_SKIP:
1404  //
1405  // If we are in skip mode and there is no space for the whole
1406  // of this output, don't bother switching terminals at all.
1407  //
1408  if (Avail < (FragLen + 4u)) {
1409  Status = 0;
1410  } else {
1411  _PostTerminalSwitch(pRing, TerminalId);
1412  Status = (int)_WriteBlocking(pRing, s, FragLen);
1413  _PostTerminalSwitch(pRing, _ActiveTerminal);
1414  }
1415  break;
1416  case SEGGER_RTT_MODE_NO_BLOCK_TRIM:
1417  //
1418  // If we are in trim mode and there is not enough space for everything,
1419  // trim the output but always include the terminal switch. If no room
1420  // for terminal switch, skip that totally.
1421  //
1422  if (Avail < 4u) {
1423  Status = -1;
1424  } else {
1425  _PostTerminalSwitch(pRing, TerminalId);
1426  Status = (int)_WriteBlocking(pRing, s, (FragLen < (Avail - 4u)) ? FragLen : (Avail - 4u));
1427  _PostTerminalSwitch(pRing, _ActiveTerminal);
1428  }
1429  break;
1430  case SEGGER_RTT_MODE_BLOCK_IF_FIFO_FULL:
1431  //
1432  // If we are in blocking mode, output everything.
1433  //
1434  _PostTerminalSwitch(pRing, TerminalId);
1435  Status = (int)_WriteBlocking(pRing, s, FragLen);
1436  _PostTerminalSwitch(pRing, _ActiveTerminal);
1437  break;
1438  default:
1439  Status = -1;
1440  break;
1441  }
1442  //
1443  // Finish up.
1444  //
1445  SEGGER_RTT_UNLOCK();
1446  } else {
1447  Status = -1;
1448  }
1449  return Status;
1450 }
1451 
1452 
1453 /*************************** End of file ****************************/
#define MIN(a, b)
Definition: common_util.h:32