Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

SEGGER_RTT.c 73KB

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