SEGGER_RTT.c 73 KB

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