SEGGER_RTT.c 52 KB

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