SEGGER_RTT.c 33 KB

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