SEGGER_RTT.c 75 KB

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