SEGGER_RTT.c 41 KB

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