SEGGER_RTT.c 60 KB

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