SEGGER_RTT.c 73 KB

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