SEGGER_RTT.c 72 KB

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