SEGGER_RTT.c 70 KB

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