SEGGER_SYSVIEW.c 98 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102
  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.32 *
  46. * *
  47. **********************************************************************
  48. -------------------------- END-OF-HEADER -----------------------------
  49. File : SEGGER_SYSVIEW.c
  50. Purpose : System visualization API implementation.
  51. Revision: $Rev: 26232 $
  52. Additional information:
  53. Packet format:
  54. Packets with IDs 0..23 are standard packets with known structure.
  55. For efficiency, they do *NOT* contain a length field.
  56. <ID><Data><TimeStampDelta>
  57. Packets with IDs 24..31 are standard packets with extendible
  58. structure and contain a length field.
  59. <ID><Lenght><Data><TimeStampDelta>
  60. Packet ID 31 is used for SystemView extended events.
  61. <ID><Lenght><ID_EX><Data><TimeStampDelta>
  62. Packets with IDs >= 32 always contain a length field.
  63. <ID><Length><Data><TimeStampDelta>
  64. Packet IDs:
  65. 0.. 31 : Standard packets, known by SystemView.
  66. 32..1023 : OS-definable packets, described in a SystemView description file.
  67. 1024..2047 : User-definable packets, described in a SystemView description file.
  68. 2048..32767: Undefined.
  69. Data encoding:
  70. Basic types (int, short, char, ...):
  71. Basic types are encoded little endian with most-significant bit variant
  72. encoding.
  73. Each encoded byte contains 7 data bits [6:0] and the MSB continuation bit.
  74. The continuation bit indicates whether the next byte belongs to the data
  75. (bit set) or this is the last byte (bit clear).
  76. The most significant bits of data are encoded first, proceeding to the
  77. least significant bits in the final byte (little endian).
  78. Example encoding:
  79. Data: 0x1F4 (500)
  80. Encoded: 0xF4 (First 7 data bits 74 | Continuation bit)
  81. 0x03 (Second 7 data bits 03, no continuation)
  82. Data: 0xFFFFFFFF
  83. Encoded: 0xFF 0xFF 0xFF 0xFF 0x0F
  84. Data: 0xA2 (162), 0x03 (3), 0x7000
  85. Encoded: 0xA2 0x01 0x03 0x80 0xE0 0x01
  86. Byte arrays and strings:
  87. Byte arrays and strings are encoded as <NumBytes> followed by the raw data.
  88. NumBytes is encoded as a basic type with a theoretical maximum of 4G.
  89. Example encoding:
  90. Data: "Hello World\0" (0x48 0x65 0x6C 0x6C 0x6F 0x20 0x57 0x6F 0x72 0x6C 0x64 0x00)
  91. Encoded: 0x0B 0x48 0x65 0x6C 0x6C 0x6F 0x20 0x57 0x6F 0x72 0x6C 0x64
  92. Examples packets:
  93. 01 F4 03 80 80 10 // Overflow packet. Data is a single U32.
  94. This packet means: 500 packets lost, Timestamp is 0x40000
  95. 02 0F 50 // ISR(15) Enter. Timestamp 80 (0x50)
  96. 03 20 // ISR Exit. Timestamp 32 (0x20) (Shortest possible packet.)
  97. Sample code for user defined Packets:
  98. #define MY_ID 0x400 // Any value between 0x400 and 0x7FF
  99. void SendMyPacket(unsigned Para0, unsigned Para1, const char* s) {
  100. U8 aPacket[SEGGER_SYSVIEW_INFO_SIZE + 2 * SEGGER_SYSVIEW_QUANTA_U32 + MAX_STR_LEN + 1];
  101. U8* pPayload;
  102. //
  103. pPayload = SEGGER_SYSVIEW_PPREPARE_PACKET(aPacket); // Prepare the packet for SystemView
  104. pPayload = SEGGER_SYSVIEW_EncodeU32(pPayload, Para0); // Add the first parameter to the packet
  105. pPayload = SEGGER_SYSVIEW_EncodeU32(pPayload, Para1); // Add the second parameter to the packet
  106. pPayload = SEGGER_SYSVIEW_EncodeString(pPayload, s, MAX_STR_LEN); // Add the string to the packet
  107. //
  108. SEGGER_SYSVIEW_SendPacket(&aPacket[0], pPayload, MY_ID); // Send the packet with EventId = MY_ID
  109. }
  110. #define MY_ID_1 0x401
  111. void SendOnePara(unsigned Para0) {
  112. SEGGER_SYSVIEW_RecordU32(MY_ID_1, Para0);
  113. }
  114. */
  115. /*********************************************************************
  116. *
  117. * #include section
  118. *
  119. **********************************************************************
  120. */
  121. #define SEGGER_SYSVIEW_C // For EXTERN statements in SEGGER_SYSVIEW.h
  122. #include <string.h>
  123. #include <stdlib.h>
  124. #include <stdarg.h>
  125. #include "SEGGER_SYSVIEW_Int.h"
  126. #include "SEGGER_RTT.h"
  127. /*********************************************************************
  128. *
  129. * Defines, fixed
  130. *
  131. **********************************************************************
  132. */
  133. #if SEGGER_SYSVIEW_ID_SHIFT
  134. #define SHRINK_ID(Id) (((Id) - _SYSVIEW_Globals.RAMBaseAddress) >> SEGGER_SYSVIEW_ID_SHIFT)
  135. #else
  136. #define SHRINK_ID(Id) ((Id) - _SYSVIEW_Globals.RAMBaseAddress)
  137. #endif
  138. #if SEGGER_SYSVIEW_RTT_CHANNEL > 0
  139. #define CHANNEL_ID_UP SEGGER_SYSVIEW_RTT_CHANNEL
  140. #define CHANNEL_ID_DOWN SEGGER_SYSVIEW_RTT_CHANNEL
  141. #else
  142. #define CHANNEL_ID_UP _SYSVIEW_Globals.UpChannel
  143. #define CHANNEL_ID_DOWN _SYSVIEW_Globals.DownChannel
  144. #endif
  145. #if SEGGER_SYSVIEW_CPU_CACHE_LINE_SIZE
  146. #if (SEGGER_SYSVIEW_RTT_BUFFER_SIZE % SEGGER_SYSVIEW_CPU_CACHE_LINE_SIZE)
  147. #error "SEGGER_SYSVIEW_RTT_BUFFER_SIZE must be a multiple of SEGGER_SYSVIEW_CPU_CACHE_LINE_SIZE"
  148. #endif
  149. #endif
  150. /*********************************************************************
  151. *
  152. * Defines, configurable
  153. *
  154. **********************************************************************
  155. */
  156. // Timestamps may be less than full 32-bits, in which case we need to zero
  157. // the unused bits to properly handle overflows.
  158. // Note that this is a quite common scenario, as a 32-bit time such as
  159. // SysTick might be scaled down to reduce bandwith
  160. // or a 16-bit hardware time might be used.
  161. #if SEGGER_SYSVIEW_TIMESTAMP_BITS < 32 // Eliminate unused bits in case hardware timestamps are less than 32 bits
  162. #define MAKE_DELTA_32BIT(Delta) Delta <<= 32 - SEGGER_SYSVIEW_TIMESTAMP_BITS; \
  163. Delta >>= 32 - SEGGER_SYSVIEW_TIMESTAMP_BITS;
  164. #else
  165. #define MAKE_DELTA_32BIT(Delta)
  166. #endif
  167. #if SEGGER_SYSVIEW_SUPPORT_LONG_ID
  168. #define _MAX_ID_BYTES 5u
  169. #else
  170. #define _MAX_ID_BYTES 2u
  171. #endif
  172. #if SEGGER_SYSVIEW_SUPPORT_LONG_DATA
  173. #define _MAX_DATA_BYTES 5u
  174. #else
  175. #define _MAX_DATA_BYTES 2u
  176. #endif
  177. /*********************************************************************
  178. *
  179. * Defines, fixed
  180. *
  181. **********************************************************************
  182. */
  183. #define ENABLE_STATE_OFF 0
  184. #define ENABLE_STATE_ON 1
  185. #define ENABLE_STATE_DROPPING 2
  186. #define FORMAT_FLAG_LEFT_JUSTIFY (1u << 0)
  187. #define FORMAT_FLAG_PAD_ZERO (1u << 1)
  188. #define FORMAT_FLAG_PRINT_SIGN (1u << 2)
  189. #define FORMAT_FLAG_ALTERNATE (1u << 3)
  190. #define MODULE_EVENT_OFFSET (512)
  191. /*********************************************************************
  192. *
  193. * Types, local
  194. *
  195. **********************************************************************
  196. */
  197. typedef struct {
  198. U8* pBuffer;
  199. U8* pPayload;
  200. U8* pPayloadStart;
  201. U32 Options;
  202. unsigned Cnt;
  203. } SEGGER_SYSVIEW_PRINTF_DESC;
  204. typedef struct {
  205. U8 EnableState; // 0: Disabled, 1: Enabled, (2: Dropping)
  206. U8 UpChannel;
  207. U8 RecursionCnt;
  208. U32 SysFreq;
  209. U32 CPUFreq;
  210. U32 LastTxTimeStamp;
  211. U32 RAMBaseAddress;
  212. #if (SEGGER_SYSVIEW_POST_MORTEM_MODE == 1)
  213. U32 PacketCount;
  214. #else
  215. U32 DropCount;
  216. U8 DownChannel;
  217. #endif
  218. U32 DisabledEvents;
  219. const SEGGER_SYSVIEW_OS_API* pOSAPI;
  220. SEGGER_SYSVIEW_SEND_SYS_DESC_FUNC* pfSendSysDesc;
  221. } SEGGER_SYSVIEW_GLOBALS;
  222. /*********************************************************************
  223. *
  224. * Function prototypes, required
  225. *
  226. **********************************************************************
  227. */
  228. static void _SendPacket(U8* pStartPacket, U8* pEndPacket, unsigned int EventId);
  229. /*********************************************************************
  230. *
  231. * Static data
  232. *
  233. **********************************************************************
  234. */
  235. static const U8 _abSync[10] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
  236. #if SEGGER_SYSVIEW_CPU_CACHE_LINE_SIZE
  237. #ifdef SEGGER_SYSVIEW_SECTION
  238. //
  239. // Alignment + special section required
  240. //
  241. #if (defined __GNUC__)
  242. __attribute__ ((section (SEGGER_SYSVIEW_SECTION), aligned (SEGGER_SYSVIEW_CPU_CACHE_LINE_SIZE))) static char _UpBuffer [SEGGER_SYSVIEW_RTT_BUFFER_SIZE];
  243. #if (SEGGER_SYSVIEW_POST_MORTEM_MODE != 1)
  244. __attribute__ ((section (SEGGER_SYSVIEW_SECTION), aligned (SEGGER_SYSVIEW_CPU_CACHE_LINE_SIZE))) static char _DownBuffer[8]; // Small, fixed-size buffer, for back-channel comms
  245. #endif
  246. #elif (defined __ICCARM__) || (defined __ICCRX__)
  247. #pragma location=SEGGER_SYSVIEW_SECTION
  248. #pragma data_alignment=SEGGER_RTT_CPU_CACHE_LINE_SIZE
  249. static char _UpBuffer [SEGGER_SYSVIEW_RTT_BUFFER_SIZE];
  250. #if (SEGGER_SYSVIEW_POST_MORTEM_MODE != 1)
  251. #pragma location=SEGGER_SYSVIEW_SECTION
  252. #pragma data_alignment=SEGGER_RTT_CPU_CACHE_LINE_SIZE
  253. static char _DownBuffer[8]; // Small, fixed-size buffer, for back-channel comms
  254. #endif
  255. #elif (defined __CC_ARM)
  256. __attribute__ ((section (SEGGER_SYSVIEW_SECTION), aligned (SEGGER_SYSVIEW_CPU_CACHE_LINE_SIZE), zero_init)) static char _UpBuffer [SEGGER_SYSVIEW_RTT_BUFFER_SIZE];
  257. #if (SEGGER_SYSVIEW_POST_MORTEM_MODE != 1)
  258. __attribute__ ((section (SEGGER_SYSVIEW_SECTION), aligned (SEGGER_SYSVIEW_CPU_CACHE_LINE_SIZE), zero_init)) static char _DownBuffer[8]; // Small, fixed-size buffer, for back-channel comms
  259. #endif
  260. #else
  261. #error "Do not know how to place SystemView buffers in specific section"
  262. #endif
  263. #else
  264. //
  265. // Only alignment required
  266. //
  267. #if (defined __GNUC__)
  268. __attribute__ ((aligned (SEGGER_SYSVIEW_CPU_CACHE_LINE_SIZE))) static char _UpBuffer [SEGGER_SYSVIEW_RTT_BUFFER_SIZE];
  269. #if (SEGGER_SYSVIEW_POST_MORTEM_MODE != 1)
  270. __attribute__ ((aligned (SEGGER_SYSVIEW_CPU_CACHE_LINE_SIZE))) static char _DownBuffer[8]; // Small, fixed-size buffer, for back-channel comms
  271. #endif
  272. #elif (defined __ICCARM__) || (defined __ICCRX__)
  273. #pragma data_alignment=SEGGER_RTT_CPU_CACHE_LINE_SIZE
  274. static char _UpBuffer [SEGGER_SYSVIEW_RTT_BUFFER_SIZE];
  275. #if (SEGGER_SYSVIEW_POST_MORTEM_MODE != 1)
  276. #pragma data_alignment=SEGGER_RTT_CPU_CACHE_LINE_SIZE
  277. static char _DownBuffer[8]; // Small, fixed-size buffer, for back-channel comms
  278. #endif
  279. #elif (defined __CC_ARM)
  280. __attribute__ ((aligned (SEGGER_SYSVIEW_CPU_CACHE_LINE_SIZE), zero_init)) static char _UpBuffer [SEGGER_SYSVIEW_RTT_BUFFER_SIZE];
  281. #if (SEGGER_SYSVIEW_POST_MORTEM_MODE != 1)
  282. __attribute__ ((aligned (SEGGER_SYSVIEW_CPU_CACHE_LINE_SIZE), zero_init)) static char _DownBuffer[8]; // Small, fixed-size buffer, for back-channel comms
  283. #endif
  284. #else
  285. #error "Do not know how to align SystemView buffers to cache line size"
  286. #endif
  287. #endif
  288. #else
  289. #ifdef SEGGER_SYSVIEW_SECTION
  290. //
  291. // Only special section required
  292. //
  293. #if (defined __GNUC__)
  294. __attribute__ ((section (SEGGER_SYSVIEW_SECTION))) static char _UpBuffer [SEGGER_SYSVIEW_RTT_BUFFER_SIZE];
  295. #if (SEGGER_SYSVIEW_POST_MORTEM_MODE != 1)
  296. __attribute__ ((section (SEGGER_SYSVIEW_SECTION))) static char _DownBuffer[8]; // Small, fixed-size buffer, for back-channel comms
  297. #endif
  298. #elif (defined __ICCARM__) || (defined __ICCRX__)
  299. #pragma location=SEGGER_SYSVIEW_SECTION
  300. static char _UpBuffer [SEGGER_SYSVIEW_RTT_BUFFER_SIZE];
  301. #if (SEGGER_SYSVIEW_POST_MORTEM_MODE != 1)
  302. #pragma location=SEGGER_SYSVIEW_SECTION
  303. static char _DownBuffer[8]; // Small, fixed-size buffer, for back-channel comms
  304. #endif
  305. #elif (defined __CC_ARM)
  306. __attribute__ ((section (SEGGER_SYSVIEW_SECTION), zero_init)) static char _UpBuffer [SEGGER_SYSVIEW_RTT_BUFFER_SIZE];
  307. #if (SEGGER_SYSVIEW_POST_MORTEM_MODE != 1)
  308. __attribute__ ((section (SEGGER_SYSVIEW_SECTION), zero_init)) static char _DownBuffer[8]; // Small, fixed-size buffer, for back-channel comms
  309. #endif
  310. #else
  311. #error "Do not know how to place SystemView buffers in specific section"
  312. #endif
  313. #else
  314. //
  315. // Neither special section nor alignment required
  316. //
  317. static char _UpBuffer [SEGGER_SYSVIEW_RTT_BUFFER_SIZE];
  318. #if (SEGGER_SYSVIEW_POST_MORTEM_MODE != 1)
  319. static char _DownBuffer[8]; // Small, fixed-size buffer, for back-channel comms
  320. #endif
  321. #endif
  322. #endif
  323. static SEGGER_SYSVIEW_GLOBALS _SYSVIEW_Globals;
  324. static SEGGER_SYSVIEW_MODULE* _pFirstModule;
  325. static U8 _NumModules;
  326. /*********************************************************************
  327. *
  328. * Static code
  329. *
  330. **********************************************************************
  331. */
  332. #define ENCODE_U32(pDest, Value) { \
  333. U8* pSysviewPointer; \
  334. U32 SysViewData; \
  335. pSysviewPointer = pDest; \
  336. SysViewData = Value; \
  337. while(SysViewData > 0x7F) { \
  338. *pSysviewPointer++ = (U8)(SysViewData | 0x80); \
  339. SysViewData >>= 7; \
  340. }; \
  341. *pSysviewPointer++ = (U8)SysViewData; \
  342. pDest = pSysviewPointer; \
  343. };
  344. #if (SEGGER_SYSVIEW_USE_STATIC_BUFFER == 1)
  345. static U8 _aPacket[SEGGER_SYSVIEW_MAX_PACKET_SIZE];
  346. #define RECORD_START(PacketSize) SEGGER_SYSVIEW_LOCK(); \
  347. pPayloadStart = _PreparePacket(_aPacket);
  348. #define RECORD_END() SEGGER_SYSVIEW_UNLOCK()
  349. #else
  350. #define RECORD_START(PacketSize) U8 aPacket[(PacketSize)]; \
  351. pPayloadStart = _PreparePacket(aPacket); \
  352. #define RECORD_END()
  353. #endif
  354. /*********************************************************************
  355. *
  356. * _EncodeData()
  357. *
  358. * Function description
  359. * Encode a byte buffer in variable-length format.
  360. *
  361. * Parameters
  362. * pPayload - Pointer to where string will be encoded.
  363. * pSrc - Pointer to data buffer to be encoded.
  364. * NumBytes - Number of bytes in the buffer to be encoded.
  365. *
  366. * Return value
  367. * Pointer to the byte following the value, i.e. the first free
  368. * byte in the payload and the next position to store payload
  369. * content.
  370. *
  371. * Additional information
  372. * The data is encoded as a count byte followed by the contents
  373. * of the data buffer.
  374. * Make sure NumBytes + 1 bytes are free for the payload.
  375. */
  376. static U8* _EncodeData(U8* pPayload, const char* pSrc, unsigned int NumBytes) {
  377. unsigned int n;
  378. const U8* p;
  379. //
  380. n = 0;
  381. p = (const U8*)pSrc;
  382. //
  383. // Write Len
  384. //
  385. if (NumBytes < 255) {
  386. *pPayload++ = (U8)NumBytes;
  387. } else {
  388. *pPayload++ = 255;
  389. *pPayload++ = (NumBytes & 255);
  390. *pPayload++ = ((NumBytes >> 8) & 255);
  391. }
  392. while (n < NumBytes) {
  393. *pPayload++ = *p++;
  394. n++;
  395. }
  396. return pPayload;
  397. }
  398. /*********************************************************************
  399. *
  400. * _EncodeStr()
  401. *
  402. * Function description
  403. * Encode a string in variable-length format.
  404. *
  405. * Parameters
  406. * pPayload - Pointer to where string will be encoded.
  407. * pText - String to encode.
  408. * Limit - Maximum number of characters to encode from string.
  409. *
  410. * Return value
  411. * Pointer to the byte following the value, i.e. the first free
  412. * byte in the payload and the next position to store payload
  413. * content.
  414. *
  415. * Additional information
  416. * The string is encoded as a count byte followed by the contents
  417. * of the string.
  418. * No more than 1 + Limit bytes will be encoded to the payload.
  419. */
  420. static U8 *_EncodeStr(U8 *pPayload, const char *pText, unsigned int Limit) {
  421. unsigned int n;
  422. unsigned int Len;
  423. //
  424. // Compute string len
  425. //
  426. Len = 0;
  427. if (pText != NULL) {
  428. while(*(pText + Len) != 0) {
  429. Len++;
  430. }
  431. if (Len > Limit) {
  432. Len = Limit;
  433. }
  434. }
  435. //
  436. // Write Len
  437. //
  438. if (Len < 255) {
  439. *pPayload++ = (U8)Len;
  440. } else {
  441. *pPayload++ = 255;
  442. *pPayload++ = (Len & 255);
  443. *pPayload++ = ((Len >> 8) & 255);
  444. }
  445. //
  446. // copy string
  447. //
  448. n = 0;
  449. while (n < Len) {
  450. *pPayload++ = *pText++;
  451. n++;
  452. }
  453. return pPayload;
  454. }
  455. /*********************************************************************
  456. *
  457. * _PreparePacket()
  458. *
  459. * Function description
  460. * Prepare a SystemView event packet header.
  461. *
  462. * Parameters
  463. * pPacket - Pointer to start of packet to initialize.
  464. *
  465. * Return value
  466. * Pointer to first byte of packet payload.
  467. *
  468. * Additional information
  469. * The payload length and evnetId are not initialized.
  470. * PreparePacket only reserves space for them and they are
  471. * computed and filled in by the sending function.
  472. */
  473. static U8* _PreparePacket(U8* pPacket) {
  474. return pPacket + _MAX_ID_BYTES + _MAX_DATA_BYTES;
  475. }
  476. /*********************************************************************
  477. *
  478. * _HandleIncomingPacket()
  479. *
  480. * Function description
  481. * Read an incoming command from the down channel and process it.
  482. *
  483. * Additional information
  484. * This function is called each time after sending a packet.
  485. * Processing incoming packets is done asynchronous. SystemView might
  486. * already have sent event packets after the host has sent a command.
  487. */
  488. #if (SEGGER_SYSVIEW_POST_MORTEM_MODE != 1)
  489. static void _HandleIncomingPacket(void) {
  490. U8 Cmd;
  491. unsigned int Status;
  492. //
  493. Status = SEGGER_RTT_ReadNoLock(CHANNEL_ID_DOWN, &Cmd, 1);
  494. if (Status > 0) {
  495. switch (Cmd) {
  496. case SEGGER_SYSVIEW_COMMAND_ID_START:
  497. SEGGER_SYSVIEW_Start();
  498. break;
  499. case SEGGER_SYSVIEW_COMMAND_ID_STOP:
  500. SEGGER_SYSVIEW_Stop();
  501. break;
  502. case SEGGER_SYSVIEW_COMMAND_ID_GET_SYSTIME:
  503. SEGGER_SYSVIEW_RecordSystime();
  504. break;
  505. case SEGGER_SYSVIEW_COMMAND_ID_GET_TASKLIST:
  506. SEGGER_SYSVIEW_SendTaskList();
  507. break;
  508. case SEGGER_SYSVIEW_COMMAND_ID_GET_SYSDESC:
  509. SEGGER_SYSVIEW_GetSysDesc();
  510. break;
  511. case SEGGER_SYSVIEW_COMMAND_ID_GET_NUMMODULES:
  512. SEGGER_SYSVIEW_SendNumModules();
  513. break;
  514. case SEGGER_SYSVIEW_COMMAND_ID_GET_MODULEDESC:
  515. SEGGER_SYSVIEW_SendModuleDescription();
  516. break;
  517. case SEGGER_SYSVIEW_COMMAND_ID_GET_MODULE:
  518. Status = SEGGER_RTT_ReadNoLock(CHANNEL_ID_DOWN, &Cmd, 1);
  519. if (Status > 0) {
  520. SEGGER_SYSVIEW_SendModule(Cmd);
  521. }
  522. break;
  523. case SEGGER_SYSVIEW_COMMAND_ID_HEARTBEAT:
  524. break;
  525. default:
  526. if (Cmd >= 128) { // Unknown extended command. Dummy read its parameter.
  527. SEGGER_RTT_ReadNoLock(CHANNEL_ID_DOWN, &Cmd, 1);
  528. }
  529. break;
  530. }
  531. }
  532. }
  533. #endif // (SEGGER_SYSVIEW_POST_MORTEM_MODE != 1)
  534. /*********************************************************************
  535. *
  536. * _TrySendOverflowPacket()
  537. *
  538. * Function description
  539. * Try to transmit an SystemView Overflow packet containing the
  540. * number of dropped packets.
  541. *
  542. * Additional information
  543. * Format as follows:
  544. * 01 <DropCnt><TimeStamp> Max. packet len is 1 + 5 + 5 = 11
  545. *
  546. * Example packets sent
  547. * 01 20 40
  548. *
  549. * Return value
  550. * !=0: Success, Message sent (stored in RTT-Buffer)
  551. * ==0: Buffer full, Message *NOT* stored
  552. *
  553. */
  554. #if (SEGGER_SYSVIEW_POST_MORTEM_MODE != 1)
  555. static int _TrySendOverflowPacket(void) {
  556. U32 TimeStamp;
  557. I32 Delta;
  558. int Status;
  559. U8 aPacket[11];
  560. U8* pPayload;
  561. aPacket[0] = SYSVIEW_EVTID_OVERFLOW; // 1
  562. pPayload = &aPacket[1];
  563. ENCODE_U32(pPayload, _SYSVIEW_Globals.DropCount);
  564. //
  565. // Compute time stamp delta and append it to packet.
  566. //
  567. TimeStamp = SEGGER_SYSVIEW_GET_TIMESTAMP();
  568. Delta = TimeStamp - _SYSVIEW_Globals.LastTxTimeStamp;
  569. MAKE_DELTA_32BIT(Delta);
  570. ENCODE_U32(pPayload, Delta);
  571. //
  572. // Try to store packet in RTT buffer and update time stamp when this was successful
  573. //
  574. Status = (int)SEGGER_RTT_WriteSkipNoLock(CHANNEL_ID_UP, aPacket, (unsigned int)(pPayload - aPacket));
  575. SEGGER_SYSVIEW_ON_EVENT_RECORDED(pPayload - aPacket);
  576. if (Status) {
  577. _SYSVIEW_Globals.LastTxTimeStamp = TimeStamp;
  578. _SYSVIEW_Globals.EnableState--; // EnableState has been 2, will be 1. Always.
  579. } else {
  580. _SYSVIEW_Globals.DropCount++;
  581. }
  582. //
  583. return Status;
  584. }
  585. #endif // (SEGGER_SYSVIEW_POST_MORTEM_MODE != 1)
  586. /*********************************************************************
  587. *
  588. * _SendSyncInfo()
  589. *
  590. * Function description
  591. * Send SystemView sync packet and system information in
  592. * post mortem mode.
  593. *
  594. * Additional information
  595. * Sync is 10 * 0x00 without timestamp
  596. */
  597. #if (SEGGER_SYSVIEW_POST_MORTEM_MODE == 1)
  598. static void _SendSyncInfo(void) {
  599. //
  600. // Add sync packet ( 10 * 0x00)
  601. // Send system description
  602. // Send system time
  603. // Send task list
  604. // Send module description
  605. // Send module information
  606. //
  607. SEGGER_RTT_WriteWithOverwriteNoLock(CHANNEL_ID_UP, _abSync, 10);
  608. SEGGER_SYSVIEW_ON_EVENT_RECORDED(10);
  609. SEGGER_SYSVIEW_RecordVoid(SYSVIEW_EVTID_TRACE_START);
  610. {
  611. U8* pPayload;
  612. U8* pPayloadStart;
  613. RECORD_START(SEGGER_SYSVIEW_INFO_SIZE + 4 * SEGGER_SYSVIEW_QUANTA_U32);
  614. //
  615. pPayload = pPayloadStart;
  616. ENCODE_U32(pPayload, _SYSVIEW_Globals.SysFreq);
  617. ENCODE_U32(pPayload, _SYSVIEW_Globals.CPUFreq);
  618. ENCODE_U32(pPayload, _SYSVIEW_Globals.RAMBaseAddress);
  619. ENCODE_U32(pPayload, SEGGER_SYSVIEW_ID_SHIFT);
  620. _SendPacket(pPayloadStart, pPayload, SYSVIEW_EVTID_INIT);
  621. RECORD_END();
  622. }
  623. if (_SYSVIEW_Globals.pfSendSysDesc) {
  624. _SYSVIEW_Globals.pfSendSysDesc();
  625. }
  626. SEGGER_SYSVIEW_RecordSystime();
  627. SEGGER_SYSVIEW_SendTaskList();
  628. if (_NumModules > 0) {
  629. int n;
  630. SEGGER_SYSVIEW_SendNumModules();
  631. for (n = 0; n < _NumModules; n++) {
  632. SEGGER_SYSVIEW_SendModule(n);
  633. }
  634. SEGGER_SYSVIEW_SendModuleDescription();
  635. }
  636. }
  637. #endif // (SEGGER_SYSVIEW_POST_MORTEM_MODE == 1)
  638. /*********************************************************************
  639. *
  640. * _SendPacket()
  641. *
  642. * Function description
  643. * Send a SystemView packet over RTT. RTT channel and mode are
  644. * configured by macros when the SystemView component is initialized.
  645. * This function takes care of maintaining the packet drop count
  646. * and sending overflow packets when necessary.
  647. * The packet must be passed without Id and Length because this
  648. * function prepends it to the packet before transmission.
  649. *
  650. * Parameters
  651. * pStartPacket - Pointer to start of packet payload.
  652. * There must be at least 4 bytes free to prepend Id and Length.
  653. * pEndPacket - Pointer to end of packet payload.
  654. * EventId - Id of the event to send.
  655. *
  656. */
  657. static void _SendPacket(U8* pStartPacket, U8* pEndPacket, unsigned int EventId) {
  658. unsigned int NumBytes;
  659. U32 TimeStamp;
  660. U32 Delta;
  661. #if (SEGGER_SYSVIEW_POST_MORTEM_MODE != 1)
  662. unsigned int Status;
  663. #endif
  664. #if (SEGGER_SYSVIEW_USE_STATIC_BUFFER == 0)
  665. SEGGER_SYSVIEW_LOCK();
  666. #endif
  667. #if (SEGGER_SYSVIEW_POST_MORTEM_MODE == 1)
  668. if (_SYSVIEW_Globals.EnableState == 0) {
  669. goto SendDone;
  670. }
  671. #else
  672. if (_SYSVIEW_Globals.EnableState == 1) { // Enabled, no dropped packets remaining
  673. goto Send;
  674. }
  675. if (_SYSVIEW_Globals.EnableState == 0) {
  676. goto SendDone;
  677. }
  678. //
  679. // Handle buffer full situations:
  680. // Have packets been dropped before because buffer was full?
  681. // In this case try to send and overflow packet.
  682. //
  683. if (_SYSVIEW_Globals.EnableState == 2) {
  684. _TrySendOverflowPacket();
  685. if (_SYSVIEW_Globals.EnableState != 1) {
  686. goto SendDone;
  687. }
  688. }
  689. Send:
  690. #endif
  691. //
  692. // Check if event is disabled from being recorded.
  693. //
  694. if (EventId < 32) {
  695. if (_SYSVIEW_Globals.DisabledEvents & ((U32)1u << EventId)) {
  696. goto SendDone;
  697. }
  698. }
  699. //
  700. // Prepare actual packet.
  701. // If it is a known packet, prepend eventId only,
  702. // otherwise prepend packet length and eventId.
  703. //
  704. if (EventId < 24) {
  705. *--pStartPacket = (U8)EventId;
  706. } else {
  707. //
  708. // Get data length and prepend it.
  709. //
  710. NumBytes = (unsigned int)(pEndPacket - pStartPacket);
  711. #if SEGGER_SYSVIEW_SUPPORT_LONG_DATA
  712. if (NumBytes < 127) {
  713. *--pStartPacket = EventId;
  714. } else {
  715. //
  716. // Backwards U32 encode EventId.
  717. //
  718. if (NumBytes < (1u << 14)) { // Encodes in 2 bytes
  719. *--pStartPacket = (U8)(NumBytes >> 7);
  720. *--pStartPacket = (U8)(NumBytes | 0x80);
  721. } else if (NumBytes < (1u << 21)) { // Encodes in 3 bytes
  722. *--pStartPacket = (U8)(NumBytes >> 14);
  723. *--pStartPacket = (U8)((NumBytes >> 7) | 0x80);
  724. *--pStartPacket = (U8)(NumBytes | 0x80);
  725. } else if (NumBytes < (1u << 28)) { // Encodes in 4 bytes
  726. *--pStartPacket = (U8)(NumBytes >> 21);
  727. *--pStartPacket = (U8)((NumBytes >> 14) | 0x80);
  728. *--pStartPacket = (U8)((NumBytes >> 7) | 0x80);
  729. *--pStartPacket = (U8)(NumBytes | 0x80);
  730. } else { // Encodes in 5 bytes
  731. *--pStartPacket = (U8)(NumBytes >> 28);
  732. *--pStartPacket = (U8)((NumBytes >> 21) | 0x80);
  733. *--pStartPacket = (U8)((NumBytes >> 14) | 0x80);
  734. *--pStartPacket = (U8)((NumBytes >> 7) | 0x80);
  735. *--pStartPacket = (U8)(NumBytes | 0x80);
  736. }
  737. }
  738. #else
  739. if (NumBytes > 127) {
  740. *--pStartPacket = (U8)(NumBytes >> 7);
  741. *--pStartPacket = (U8)(NumBytes | 0x80);
  742. } else {
  743. *--pStartPacket = (U8)NumBytes;
  744. }
  745. #endif
  746. //
  747. // Prepend EventId.
  748. //
  749. #if SEGGER_SYSVIEW_SUPPORT_LONG_ID
  750. if (EventId < 127) {
  751. *--pStartPacket = (U8)EventId;
  752. } else {
  753. //
  754. // Backwards U32 encode EventId.
  755. //
  756. if (EventId < (1u << 14)) { // Encodes in 2 bytes
  757. *--pStartPacket = (U8)(EventId >> 7);
  758. *--pStartPacket = (U8)(EventId | 0x80);
  759. } else if (EventId < (1u << 21)) { // Encodes in 3 bytes
  760. *--pStartPacket = (U8)(EventId >> 14);
  761. *--pStartPacket = (U8)((EventId >> 7) | 0x80);
  762. *--pStartPacket = (U8)(EventId | 0x80);
  763. } else if (EventId < (1u << 28)) { // Encodes in 4 bytes
  764. *--pStartPacket = (U8)(EventId >> 21);
  765. *--pStartPacket = (U8)((EventId >> 14) | 0x80);
  766. *--pStartPacket = (U8)((EventId >> 7) | 0x80);
  767. *--pStartPacket = (U8)(EventId | 0x80);
  768. } else { // Encodes in 5 bytes
  769. *--pStartPacket = (U8)(EventId >> 28);
  770. *--pStartPacket = (U8)((EventId >> 21) | 0x80);
  771. *--pStartPacket = (U8)((EventId >> 14) | 0x80);
  772. *--pStartPacket = (U8)((EventId >> 7) | 0x80);
  773. *--pStartPacket = (U8)(EventId | 0x80);
  774. }
  775. }
  776. #else
  777. if (EventId > 127) {
  778. *--pStartPacket = (U8)(EventId >> 7);
  779. *--pStartPacket = (U8)(EventId | 0x80);
  780. } else {
  781. *--pStartPacket = (U8)EventId;
  782. }
  783. #endif
  784. }
  785. //
  786. // Compute time stamp delta and append it to packet.
  787. //
  788. TimeStamp = SEGGER_SYSVIEW_GET_TIMESTAMP();
  789. Delta = TimeStamp - _SYSVIEW_Globals.LastTxTimeStamp;
  790. MAKE_DELTA_32BIT(Delta);
  791. ENCODE_U32(pEndPacket, Delta);
  792. #if (SEGGER_SYSVIEW_POST_MORTEM_MODE == 1)
  793. //
  794. // Store packet in RTT buffer by overwriting old data and update time stamp
  795. //
  796. SEGGER_RTT_WriteWithOverwriteNoLock(CHANNEL_ID_UP, pStartPacket, pEndPacket - pStartPacket);
  797. SEGGER_SYSVIEW_ON_EVENT_RECORDED(pEndPacket - pStartPacket);
  798. _SYSVIEW_Globals.LastTxTimeStamp = TimeStamp;
  799. #else
  800. //
  801. // Try to store packet in RTT buffer and update time stamp when this was successful
  802. //
  803. Status = SEGGER_RTT_WriteSkipNoLock(CHANNEL_ID_UP, pStartPacket, (unsigned int)(pEndPacket - pStartPacket));
  804. SEGGER_SYSVIEW_ON_EVENT_RECORDED(pEndPacket - pStartPacket);
  805. if (Status) {
  806. _SYSVIEW_Globals.LastTxTimeStamp = TimeStamp;
  807. } else {
  808. _SYSVIEW_Globals.EnableState++; // EnableState has been 1, will be 2. Always.
  809. }
  810. #endif
  811. #if (SEGGER_SYSVIEW_POST_MORTEM_MODE == 1)
  812. //
  813. // Add sync and system information periodically if we are in post mortem mode
  814. //
  815. if (_SYSVIEW_Globals.RecursionCnt == 0) { // Avoid uncontrolled nesting. This way, this routine can call itself once, but no more often than that.
  816. _SYSVIEW_Globals.RecursionCnt = 1;
  817. if (_SYSVIEW_Globals.PacketCount++ & (1 << SEGGER_SYSVIEW_SYNC_PERIOD_SHIFT)) {
  818. _SendSyncInfo();
  819. _SYSVIEW_Globals.PacketCount = 0;
  820. }
  821. _SYSVIEW_Globals.RecursionCnt = 0;
  822. }
  823. SendDone:
  824. ; // Avoid "label at end of compound statement" error when using static buffer
  825. #else
  826. SendDone:
  827. //
  828. // Check if host is sending data which needs to be processed.
  829. // Note that since this code is called for every packet, it is very time critical, so we do
  830. // only what is really needed here, which is checking if there is any data
  831. //
  832. if (SEGGER_RTT_HASDATA(CHANNEL_ID_DOWN)) {
  833. if (_SYSVIEW_Globals.RecursionCnt == 0) { // Avoid uncontrolled nesting. This way, this routine can call itself once, but no more often than that.
  834. _SYSVIEW_Globals.RecursionCnt = 1;
  835. _HandleIncomingPacket();
  836. _SYSVIEW_Globals.RecursionCnt = 0;
  837. }
  838. }
  839. #endif
  840. //
  841. #if (SEGGER_SYSVIEW_USE_STATIC_BUFFER == 0)
  842. SEGGER_SYSVIEW_UNLOCK(); // We are done. Unlock and return
  843. #endif
  844. }
  845. #ifndef SEGGER_SYSVIEW_EXCLUDE_PRINTF // Define in project to avoid warnings about variable parameter list
  846. /*********************************************************************
  847. *
  848. * _VPrintHost()
  849. *
  850. * Function description
  851. * Send a format string and its parameters to the host.
  852. *
  853. * Parameters
  854. * s Pointer to format string.
  855. * Options Options to be sent to the host.
  856. * pParamList Pointer to the list of arguments for the format string.
  857. */
  858. static int _VPrintHost(const char* s, U32 Options, va_list* pParamList) {
  859. U32 aParas[SEGGER_SYSVIEW_MAX_ARGUMENTS];
  860. U32* pParas;
  861. U32 NumArguments;
  862. const char* p;
  863. char c;
  864. U8* pPayload;
  865. U8* pPayloadStart;
  866. #if SEGGER_SYSVIEW_PRINTF_IMPLICIT_FORMAT
  867. U8 HasNonScalar;
  868. HasNonScalar = 0;
  869. #endif
  870. //
  871. // Count number of arguments by counting '%' characters in string.
  872. // If enabled, check for non-scalar modifier flags to format string on the target.
  873. //
  874. p = s;
  875. NumArguments = 0;
  876. for (;;) {
  877. c = *p++;
  878. if (c == 0) {
  879. break;
  880. }
  881. if (c == '%') {
  882. c = *p;
  883. #if SEGGER_SYSVIEW_PRINTF_IMPLICIT_FORMAT == 0
  884. aParas[NumArguments++] = (U32)(va_arg(*pParamList, int));
  885. if (NumArguments == SEGGER_SYSVIEW_MAX_ARGUMENTS) {
  886. break;
  887. }
  888. #else
  889. if (c == 's') {
  890. HasNonScalar = 1;
  891. break;
  892. } else {
  893. aParas[NumArguments++] = (U32)(va_arg(*pParamList, int));
  894. if (NumArguments == SEGGER_SYSVIEW_MAX_ARGUMENTS) {
  895. break;
  896. }
  897. }
  898. #endif
  899. }
  900. }
  901. #if SEGGER_SYSVIEW_PRINTF_IMPLICIT_FORMAT
  902. if (HasNonScalar) {
  903. return -1;
  904. }
  905. #endif
  906. //
  907. // Send string and parameters to host
  908. //
  909. {
  910. RECORD_START(SEGGER_SYSVIEW_INFO_SIZE + SEGGER_SYSVIEW_MAX_STRING_LEN + 2 * SEGGER_SYSVIEW_QUANTA_U32 + SEGGER_SYSVIEW_MAX_ARGUMENTS * SEGGER_SYSVIEW_QUANTA_U32);
  911. pPayload = _EncodeStr(pPayloadStart, s, SEGGER_SYSVIEW_MAX_STRING_LEN);
  912. ENCODE_U32(pPayload, Options);
  913. ENCODE_U32(pPayload, NumArguments);
  914. pParas = aParas;
  915. while (NumArguments--) {
  916. ENCODE_U32(pPayload, (*pParas));
  917. pParas++;
  918. }
  919. _SendPacket(pPayloadStart, pPayload, SYSVIEW_EVTID_PRINT_FORMATTED);
  920. RECORD_END();
  921. }
  922. return 0;
  923. }
  924. /*********************************************************************
  925. *
  926. * _StoreChar()
  927. *
  928. * Function description
  929. * Stores a character in the printf-buffer and sends the buffer when
  930. * it is filled.
  931. *
  932. * Parameters
  933. * p Pointer to the buffer description.
  934. * c Character to be printed.
  935. */
  936. static void _StoreChar(SEGGER_SYSVIEW_PRINTF_DESC * p, char c) {
  937. unsigned int Cnt;
  938. U8* pPayload;
  939. U32 Options;
  940. Cnt = p->Cnt;
  941. if ((Cnt + 1u) <= SEGGER_SYSVIEW_MAX_STRING_LEN) {
  942. *(p->pPayload++) = (U8)c;
  943. p->Cnt = Cnt + 1u;
  944. }
  945. //
  946. // Write part of string, when the buffer is full
  947. //
  948. if (p->Cnt == SEGGER_SYSVIEW_MAX_STRING_LEN) {
  949. *(p->pPayloadStart) = (U8)p->Cnt;
  950. pPayload = p->pPayload;
  951. Options = p->Options;
  952. ENCODE_U32(pPayload, Options);
  953. ENCODE_U32(pPayload, 0);
  954. _SendPacket(p->pPayloadStart, pPayload, SYSVIEW_EVTID_PRINT_FORMATTED);
  955. p->pPayloadStart = _PreparePacket(p->pBuffer);
  956. p->pPayload = p->pPayloadStart + 1u;
  957. p->Cnt = 0u;
  958. }
  959. }
  960. /*********************************************************************
  961. *
  962. * _PrintUnsigned()
  963. *
  964. * Function description
  965. * Print an unsigned integer with the given formatting into the
  966. * formatted string.
  967. *
  968. * Parameters
  969. * pBufferDesc Pointer to the buffer description.
  970. * v Value to be printed.
  971. * Base Base of the value.
  972. * NumDigits Number of digits to be printed.
  973. * FieldWidth Width of the printed field.
  974. * FormatFlags Flags for formatting the value.
  975. */
  976. static void _PrintUnsigned(SEGGER_SYSVIEW_PRINTF_DESC * pBufferDesc, unsigned int v, unsigned int Base, unsigned int NumDigits, unsigned int FieldWidth, unsigned int FormatFlags) {
  977. static const char _aV2C[16] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
  978. unsigned int Div;
  979. unsigned int Digit;
  980. unsigned int Number;
  981. unsigned int Width;
  982. char c;
  983. Number = v;
  984. Digit = 1u;
  985. //
  986. // Get actual field width
  987. //
  988. Width = 1u;
  989. while (Number >= Base) {
  990. Number = (Number / Base);
  991. Width++;
  992. }
  993. if (NumDigits > Width) {
  994. Width = NumDigits;
  995. }
  996. //
  997. // Print leading chars if necessary
  998. //
  999. if ((FormatFlags & FORMAT_FLAG_LEFT_JUSTIFY) == 0u) {
  1000. if (FieldWidth != 0u) {
  1001. if (((FormatFlags & FORMAT_FLAG_PAD_ZERO) == FORMAT_FLAG_PAD_ZERO) && (NumDigits == 0u)) {
  1002. c = '0';
  1003. } else {
  1004. c = ' ';
  1005. }
  1006. while ((FieldWidth != 0u) && (Width < FieldWidth)) {
  1007. FieldWidth--;
  1008. _StoreChar(pBufferDesc, c);
  1009. }
  1010. }
  1011. }
  1012. //
  1013. // Compute Digit.
  1014. // Loop until Digit has the value of the highest digit required.
  1015. // Example: If the output is 345 (Base 10), loop 2 times until Digit is 100.
  1016. //
  1017. while (1) {
  1018. if (NumDigits > 1u) { // User specified a min number of digits to print? => Make sure we loop at least that often, before checking anything else (> 1 check avoids problems with NumDigits being signed / unsigned)
  1019. NumDigits--;
  1020. } else {
  1021. Div = v / Digit;
  1022. if (Div < Base) { // Is our divider big enough to extract the highest digit from value? => Done
  1023. break;
  1024. }
  1025. }
  1026. Digit *= Base;
  1027. }
  1028. //
  1029. // Output digits
  1030. //
  1031. do {
  1032. Div = v / Digit;
  1033. v -= Div * Digit;
  1034. _StoreChar(pBufferDesc, _aV2C[Div]);
  1035. Digit /= Base;
  1036. } while (Digit);
  1037. //
  1038. // Print trailing spaces if necessary
  1039. //
  1040. if ((FormatFlags & FORMAT_FLAG_LEFT_JUSTIFY) == FORMAT_FLAG_LEFT_JUSTIFY) {
  1041. if (FieldWidth != 0u) {
  1042. while ((FieldWidth != 0u) && (Width < FieldWidth)) {
  1043. FieldWidth--;
  1044. _StoreChar(pBufferDesc, ' ');
  1045. }
  1046. }
  1047. }
  1048. }
  1049. /*********************************************************************
  1050. *
  1051. * _PrintInt()
  1052. *
  1053. * Function description
  1054. * Print a signed integer with the given formatting into the
  1055. * formatted string.
  1056. *
  1057. * Parameters
  1058. * pBufferDesc Pointer to the buffer description.
  1059. * v Value to be printed.
  1060. * Base Base of the value.
  1061. * NumDigits Number of digits to be printed.
  1062. * FieldWidth Width of the printed field.
  1063. * FormatFlags Flags for formatting the value.
  1064. */
  1065. static void _PrintInt(SEGGER_SYSVIEW_PRINTF_DESC * pBufferDesc, int v, unsigned int Base, unsigned int NumDigits, unsigned int FieldWidth, unsigned int FormatFlags) {
  1066. unsigned int Width;
  1067. int Number;
  1068. Number = (v < 0) ? -v : v;
  1069. //
  1070. // Get actual field width
  1071. //
  1072. Width = 1u;
  1073. while (Number >= (int)Base) {
  1074. Number = (Number / (int)Base);
  1075. Width++;
  1076. }
  1077. if (NumDigits > Width) {
  1078. Width = NumDigits;
  1079. }
  1080. if ((FieldWidth > 0u) && ((v < 0) || ((FormatFlags & FORMAT_FLAG_PRINT_SIGN) == FORMAT_FLAG_PRINT_SIGN))) {
  1081. FieldWidth--;
  1082. }
  1083. //
  1084. // Print leading spaces if necessary
  1085. //
  1086. if ((((FormatFlags & FORMAT_FLAG_PAD_ZERO) == 0u) || (NumDigits != 0u)) && ((FormatFlags & FORMAT_FLAG_LEFT_JUSTIFY) == 0u)) {
  1087. if (FieldWidth != 0u) {
  1088. while ((FieldWidth != 0u) && (Width < FieldWidth)) {
  1089. FieldWidth--;
  1090. _StoreChar(pBufferDesc, ' ');
  1091. }
  1092. }
  1093. }
  1094. //
  1095. // Print sign if necessary
  1096. //
  1097. if (v < 0) {
  1098. v = -v;
  1099. _StoreChar(pBufferDesc, '-');
  1100. } else if ((FormatFlags & FORMAT_FLAG_PRINT_SIGN) == FORMAT_FLAG_PRINT_SIGN) {
  1101. _StoreChar(pBufferDesc, '+');
  1102. } else {
  1103. }
  1104. //
  1105. // Print leading zeros if necessary
  1106. //
  1107. if (((FormatFlags & FORMAT_FLAG_PAD_ZERO) == FORMAT_FLAG_PAD_ZERO) && ((FormatFlags & FORMAT_FLAG_LEFT_JUSTIFY) == 0u) && (NumDigits == 0u)) {
  1108. if (FieldWidth != 0u) {
  1109. while ((FieldWidth != 0u) && (Width < FieldWidth)) {
  1110. FieldWidth--;
  1111. _StoreChar(pBufferDesc, '0');
  1112. }
  1113. }
  1114. }
  1115. //
  1116. // Print number without sign
  1117. //
  1118. _PrintUnsigned(pBufferDesc, (unsigned int)v, Base, NumDigits, FieldWidth, FormatFlags);
  1119. }
  1120. /*********************************************************************
  1121. *
  1122. * _VPrintTarget()
  1123. *
  1124. * Function description
  1125. * Stores a formatted string.
  1126. * This data is read by the host.
  1127. *
  1128. * Parameters
  1129. * sFormat Pointer to format string.
  1130. * Options Options to be sent to the host.
  1131. * pParamList Pointer to the list of arguments for the format string.
  1132. */
  1133. static void _VPrintTarget(const char* sFormat, U32 Options, va_list* pParamList) {
  1134. SEGGER_SYSVIEW_PRINTF_DESC BufferDesc;
  1135. char c;
  1136. int v;
  1137. unsigned int NumDigits;
  1138. unsigned int FormatFlags;
  1139. unsigned int FieldWidth;
  1140. U8* pPayloadStart;
  1141. #if SEGGER_SYSVIEW_USE_STATIC_BUFFER == 0
  1142. RECORD_START(SEGGER_SYSVIEW_INFO_SIZE + SEGGER_SYSVIEW_MAX_STRING_LEN + 1 + 2 * SEGGER_SYSVIEW_QUANTA_U32);
  1143. SEGGER_SYSVIEW_LOCK();
  1144. #else
  1145. RECORD_START(SEGGER_SYSVIEW_INFO_SIZE + SEGGER_SYSVIEW_MAX_STRING_LEN + 1 + 2 * SEGGER_SYSVIEW_QUANTA_U32);
  1146. #endif
  1147. #if SEGGER_SYSVIEW_USE_STATIC_BUFFER == 0
  1148. BufferDesc.pBuffer = aPacket;
  1149. #else
  1150. BufferDesc.pBuffer = _aPacket;
  1151. #endif
  1152. BufferDesc.Cnt = 0u;
  1153. BufferDesc.pPayloadStart = pPayloadStart;
  1154. BufferDesc.pPayload = BufferDesc.pPayloadStart + 1u;
  1155. BufferDesc.Options = Options;
  1156. do {
  1157. c = *sFormat;
  1158. sFormat++;
  1159. if (c == 0u) {
  1160. break;
  1161. }
  1162. if (c == '%') {
  1163. //
  1164. // Filter out flags
  1165. //
  1166. FormatFlags = 0u;
  1167. v = 1;
  1168. do {
  1169. c = *sFormat;
  1170. switch (c) {
  1171. case '-': FormatFlags |= FORMAT_FLAG_LEFT_JUSTIFY; sFormat++; break;
  1172. case '0': FormatFlags |= FORMAT_FLAG_PAD_ZERO; sFormat++; break;
  1173. case '+': FormatFlags |= FORMAT_FLAG_PRINT_SIGN; sFormat++; break;
  1174. case '#': FormatFlags |= FORMAT_FLAG_ALTERNATE; sFormat++; break;
  1175. default: v = 0; break;
  1176. }
  1177. } while (v);
  1178. //
  1179. // filter out field with
  1180. //
  1181. FieldWidth = 0u;
  1182. do {
  1183. c = *sFormat;
  1184. if ((c < '0') || (c > '9')) {
  1185. break;
  1186. }
  1187. sFormat++;
  1188. FieldWidth = (FieldWidth * 10u) + ((unsigned int)c - '0');
  1189. } while (1);
  1190. //
  1191. // Filter out precision (number of digits to display)
  1192. //
  1193. NumDigits = 0u;
  1194. c = *sFormat;
  1195. if (c == '.') {
  1196. sFormat++;
  1197. do {
  1198. c = *sFormat;
  1199. if ((c < '0') || (c > '9')) {
  1200. break;
  1201. }
  1202. sFormat++;
  1203. NumDigits = NumDigits * 10u + ((unsigned int)c - '0');
  1204. } while (1);
  1205. }
  1206. //
  1207. // Filter out length modifier
  1208. //
  1209. c = *sFormat;
  1210. do {
  1211. if ((c == 'l') || (c == 'h')) {
  1212. c = *sFormat;
  1213. sFormat++;
  1214. } else {
  1215. break;
  1216. }
  1217. } while (1);
  1218. //
  1219. // Handle specifiers
  1220. //
  1221. switch (c) {
  1222. case 'c': {
  1223. char c0;
  1224. v = va_arg(*pParamList, int);
  1225. c0 = (char)v;
  1226. _StoreChar(&BufferDesc, c0);
  1227. break;
  1228. }
  1229. case 'd':
  1230. v = va_arg(*pParamList, int);
  1231. _PrintInt(&BufferDesc, v, 10u, NumDigits, FieldWidth, FormatFlags);
  1232. break;
  1233. case 'u':
  1234. v = va_arg(*pParamList, int);
  1235. _PrintUnsigned(&BufferDesc, (unsigned int)v, 10u, NumDigits, FieldWidth, FormatFlags);
  1236. break;
  1237. case 'x':
  1238. case 'X':
  1239. v = va_arg(*pParamList, int);
  1240. _PrintUnsigned(&BufferDesc, (unsigned int)v, 16u, NumDigits, FieldWidth, FormatFlags);
  1241. break;
  1242. case 'p':
  1243. v = va_arg(*pParamList, int);
  1244. _PrintUnsigned(&BufferDesc, (unsigned int)v, 16u, 8u, 8u, 0u);
  1245. break;
  1246. case '%':
  1247. _StoreChar(&BufferDesc, '%');
  1248. break;
  1249. default:
  1250. break;
  1251. }
  1252. sFormat++;
  1253. } else {
  1254. _StoreChar(&BufferDesc, c);
  1255. }
  1256. } while (*sFormat);
  1257. //
  1258. // Write remaining data, if any
  1259. //
  1260. if (BufferDesc.Cnt != 0u) {
  1261. *(BufferDesc.pPayloadStart) = (U8)BufferDesc.Cnt;
  1262. ENCODE_U32(BufferDesc.pPayload, BufferDesc.Options);
  1263. ENCODE_U32(BufferDesc.pPayload, 0);
  1264. _SendPacket(BufferDesc.pPayloadStart, BufferDesc.pPayload, SYSVIEW_EVTID_PRINT_FORMATTED);
  1265. }
  1266. #if SEGGER_SYSVIEW_USE_STATIC_BUFFER == 0
  1267. SEGGER_SYSVIEW_UNLOCK();
  1268. RECORD_END();
  1269. #else
  1270. RECORD_END();
  1271. #endif
  1272. }
  1273. #endif // SEGGER_SYSVIEW_EXCLUDE_PRINTF
  1274. /*********************************************************************
  1275. *
  1276. * Public code
  1277. *
  1278. **********************************************************************
  1279. */
  1280. /*********************************************************************
  1281. *
  1282. * SEGGER_SYSVIEW_Init()
  1283. *
  1284. * Function description
  1285. * Initializes the SYSVIEW module.
  1286. * Must be called before the Systemview Application connects to
  1287. * the system.
  1288. *
  1289. * Parameters
  1290. * SysFreq - Frequency of timestamp, usually CPU core clock frequency.
  1291. * CPUFreq - CPU core clock frequency.
  1292. * pOSAPI - Pointer to the API structure for OS-specific functions.
  1293. * pfSendSysDesc - Pointer to record system description callback function.
  1294. *
  1295. * Additional information
  1296. * This function initializes the RTT channel used to transport
  1297. * SEGGER SystemView packets.
  1298. * The channel is assigned the label "SysView" for client software
  1299. * to identify the SystemView channel.
  1300. *
  1301. * The channel is configured with the macro SEGGER_SYSVIEW_RTT_CHANNEL.
  1302. */
  1303. void SEGGER_SYSVIEW_Init(U32 SysFreq, U32 CPUFreq, const SEGGER_SYSVIEW_OS_API *pOSAPI, SEGGER_SYSVIEW_SEND_SYS_DESC_FUNC pfSendSysDesc) {
  1304. #ifdef SEGGER_RTT_SECTION
  1305. //
  1306. // Explicitly initialize the RTT Control Block if it is in its dedicated section.
  1307. //
  1308. SEGGER_RTT_Init();
  1309. #endif
  1310. #if (SEGGER_SYSVIEW_POST_MORTEM_MODE == 1)
  1311. #if SEGGER_SYSVIEW_RTT_CHANNEL > 0
  1312. SEGGER_RTT_ConfigUpBuffer(SEGGER_SYSVIEW_RTT_CHANNEL, "SysView", &_UpBuffer[0], sizeof(_UpBuffer), SEGGER_RTT_MODE_NO_BLOCK_SKIP);
  1313. #else
  1314. _SYSVIEW_Globals.UpChannel = (U8)SEGGER_RTT_AllocUpBuffer ("SysView", &_UpBuffer[0], sizeof(_UpBuffer), SEGGER_RTT_MODE_NO_BLOCK_SKIP);
  1315. #endif
  1316. _SYSVIEW_Globals.RAMBaseAddress = SEGGER_SYSVIEW_ID_BASE;
  1317. _SYSVIEW_Globals.LastTxTimeStamp = SEGGER_SYSVIEW_GET_TIMESTAMP();
  1318. _SYSVIEW_Globals.pOSAPI = pOSAPI;
  1319. _SYSVIEW_Globals.SysFreq = SysFreq;
  1320. _SYSVIEW_Globals.CPUFreq = CPUFreq;
  1321. _SYSVIEW_Globals.pfSendSysDesc = pfSendSysDesc;
  1322. _SYSVIEW_Globals.EnableState = 0;
  1323. _SYSVIEW_Globals.PacketCount = 0;
  1324. #else // (SEGGER_SYSVIEW_POST_MORTEM_MODE == 1)
  1325. #if SEGGER_SYSVIEW_RTT_CHANNEL > 0
  1326. SEGGER_RTT_ConfigUpBuffer (SEGGER_SYSVIEW_RTT_CHANNEL, "SysView", &_UpBuffer[0], sizeof(_UpBuffer), SEGGER_RTT_MODE_NO_BLOCK_SKIP);
  1327. SEGGER_RTT_ConfigDownBuffer (SEGGER_SYSVIEW_RTT_CHANNEL, "SysView", &_DownBuffer[0], sizeof(_DownBuffer), SEGGER_RTT_MODE_NO_BLOCK_SKIP);
  1328. #else
  1329. _SYSVIEW_Globals.UpChannel = (U8)SEGGER_RTT_AllocUpBuffer ("SysView", &_UpBuffer[0], sizeof(_UpBuffer), SEGGER_RTT_MODE_NO_BLOCK_SKIP);
  1330. _SYSVIEW_Globals.DownChannel = _SYSVIEW_Globals.UpChannel;
  1331. SEGGER_RTT_ConfigDownBuffer (_SYSVIEW_Globals.DownChannel, "SysView", &_DownBuffer[0], sizeof(_DownBuffer), SEGGER_RTT_MODE_NO_BLOCK_SKIP);
  1332. #endif
  1333. _SYSVIEW_Globals.RAMBaseAddress = SEGGER_SYSVIEW_ID_BASE;
  1334. _SYSVIEW_Globals.LastTxTimeStamp = SEGGER_SYSVIEW_GET_TIMESTAMP();
  1335. _SYSVIEW_Globals.pOSAPI = pOSAPI;
  1336. _SYSVIEW_Globals.SysFreq = SysFreq;
  1337. _SYSVIEW_Globals.CPUFreq = CPUFreq;
  1338. _SYSVIEW_Globals.pfSendSysDesc = pfSendSysDesc;
  1339. _SYSVIEW_Globals.EnableState = 0;
  1340. #endif // (SEGGER_SYSVIEW_POST_MORTEM_MODE == 1)
  1341. }
  1342. /*********************************************************************
  1343. *
  1344. * SEGGER_SYSVIEW_SetRAMBase()
  1345. *
  1346. * Function description
  1347. * Sets the RAM base address, which is subtracted from IDs in order
  1348. * to save bandwidth.
  1349. *
  1350. * Parameters
  1351. * RAMBaseAddress - Lowest RAM Address. (i.e. 0x20000000 on most Cortex-M)
  1352. */
  1353. void SEGGER_SYSVIEW_SetRAMBase(U32 RAMBaseAddress) {
  1354. _SYSVIEW_Globals.RAMBaseAddress = RAMBaseAddress;
  1355. }
  1356. /*********************************************************************
  1357. *
  1358. * SEGGER_SYSVIEW_RecordVoid()
  1359. *
  1360. * Function description
  1361. * Formats and sends a SystemView packet with an empty payload.
  1362. *
  1363. * Parameters
  1364. * EventID - SystemView event ID.
  1365. */
  1366. void SEGGER_SYSVIEW_RecordVoid(unsigned int EventID) {
  1367. U8* pPayloadStart;
  1368. RECORD_START(SEGGER_SYSVIEW_INFO_SIZE);
  1369. //
  1370. _SendPacket(pPayloadStart, pPayloadStart, EventID);
  1371. RECORD_END();
  1372. }
  1373. /*********************************************************************
  1374. *
  1375. * SEGGER_SYSVIEW_RecordU32()
  1376. *
  1377. * Function description
  1378. * Formats and sends a SystemView packet containing a single U32
  1379. * parameter payload.
  1380. *
  1381. * Parameters
  1382. * EventID - SystemView event ID.
  1383. * Value - The 32-bit parameter encoded to SystemView packet payload.
  1384. */
  1385. void SEGGER_SYSVIEW_RecordU32(unsigned int EventID, U32 Value) {
  1386. U8* pPayload;
  1387. U8* pPayloadStart;
  1388. RECORD_START(SEGGER_SYSVIEW_INFO_SIZE + SEGGER_SYSVIEW_QUANTA_U32);
  1389. //
  1390. pPayload = pPayloadStart;
  1391. ENCODE_U32(pPayload, Value);
  1392. _SendPacket(pPayloadStart, pPayload, EventID);
  1393. RECORD_END();
  1394. }
  1395. /*********************************************************************
  1396. *
  1397. * SEGGER_SYSVIEW_RecordU32x2()
  1398. *
  1399. * Function description
  1400. * Formats and sends a SystemView packet containing 2 U32 parameter payload.
  1401. *
  1402. * Parameters
  1403. * EventID - SystemView event ID.
  1404. * Para0 - The 32-bit parameter encoded to SystemView packet payload.
  1405. * Para1 - The 32-bit parameter encoded to SystemView packet payload.
  1406. */
  1407. void SEGGER_SYSVIEW_RecordU32x2(unsigned int EventID, U32 Para0, U32 Para1) {
  1408. U8* pPayload;
  1409. U8* pPayloadStart;
  1410. RECORD_START(SEGGER_SYSVIEW_INFO_SIZE + 2 * SEGGER_SYSVIEW_QUANTA_U32);
  1411. //
  1412. pPayload = pPayloadStart;
  1413. ENCODE_U32(pPayload, Para0);
  1414. ENCODE_U32(pPayload, Para1);
  1415. _SendPacket(pPayloadStart, pPayload, EventID);
  1416. RECORD_END();
  1417. }
  1418. /*********************************************************************
  1419. *
  1420. * SEGGER_SYSVIEW_RecordU32x3()
  1421. *
  1422. * Function description
  1423. * Formats and sends a SystemView packet containing 3 U32 parameter payload.
  1424. *
  1425. * Parameters
  1426. * EventID - SystemView event ID.
  1427. * Para0 - The 32-bit parameter encoded to SystemView packet payload.
  1428. * Para1 - The 32-bit parameter encoded to SystemView packet payload.
  1429. * Para2 - The 32-bit parameter encoded to SystemView packet payload.
  1430. */
  1431. void SEGGER_SYSVIEW_RecordU32x3(unsigned int EventID, U32 Para0, U32 Para1, U32 Para2) {
  1432. U8* pPayload;
  1433. U8* pPayloadStart;
  1434. RECORD_START(SEGGER_SYSVIEW_INFO_SIZE + 3 * SEGGER_SYSVIEW_QUANTA_U32);
  1435. //
  1436. pPayload = pPayloadStart;
  1437. ENCODE_U32(pPayload, Para0);
  1438. ENCODE_U32(pPayload, Para1);
  1439. ENCODE_U32(pPayload, Para2);
  1440. _SendPacket(pPayloadStart, pPayload, EventID);
  1441. RECORD_END();
  1442. }
  1443. /*********************************************************************
  1444. *
  1445. * SEGGER_SYSVIEW_RecordU32x4()
  1446. *
  1447. * Function description
  1448. * Formats and sends a SystemView packet containing 4 U32 parameter payload.
  1449. *
  1450. * Parameters
  1451. * EventID - SystemView event ID.
  1452. * Para0 - The 32-bit parameter encoded to SystemView packet payload.
  1453. * Para1 - The 32-bit parameter encoded to SystemView packet payload.
  1454. * Para2 - The 32-bit parameter encoded to SystemView packet payload.
  1455. * Para3 - The 32-bit parameter encoded to SystemView packet payload.
  1456. */
  1457. void SEGGER_SYSVIEW_RecordU32x4(unsigned int EventID, U32 Para0, U32 Para1, U32 Para2, U32 Para3) {
  1458. U8* pPayload;
  1459. U8* pPayloadStart;
  1460. RECORD_START(SEGGER_SYSVIEW_INFO_SIZE + 4 * SEGGER_SYSVIEW_QUANTA_U32);
  1461. //
  1462. pPayload = pPayloadStart;
  1463. ENCODE_U32(pPayload, Para0);
  1464. ENCODE_U32(pPayload, Para1);
  1465. ENCODE_U32(pPayload, Para2);
  1466. ENCODE_U32(pPayload, Para3);
  1467. _SendPacket(pPayloadStart, pPayload, EventID);
  1468. RECORD_END();
  1469. }
  1470. /*********************************************************************
  1471. *
  1472. * SEGGER_SYSVIEW_RecordU32x5()
  1473. *
  1474. * Function description
  1475. * Formats and sends a SystemView packet containing 5 U32 parameter payload.
  1476. *
  1477. * Parameters
  1478. * EventID - SystemView event ID.
  1479. * Para0 - The 32-bit parameter encoded to SystemView packet payload.
  1480. * Para1 - The 32-bit parameter encoded to SystemView packet payload.
  1481. * Para2 - The 32-bit parameter encoded to SystemView packet payload.
  1482. * Para3 - The 32-bit parameter encoded to SystemView packet payload.
  1483. * Para4 - The 32-bit parameter encoded to SystemView packet payload.
  1484. */
  1485. void SEGGER_SYSVIEW_RecordU32x5(unsigned int EventID, U32 Para0, U32 Para1, U32 Para2, U32 Para3, U32 Para4) {
  1486. U8* pPayload;
  1487. U8* pPayloadStart;
  1488. RECORD_START(SEGGER_SYSVIEW_INFO_SIZE + 5 * SEGGER_SYSVIEW_QUANTA_U32);
  1489. //
  1490. pPayload = pPayloadStart;
  1491. ENCODE_U32(pPayload, Para0);
  1492. ENCODE_U32(pPayload, Para1);
  1493. ENCODE_U32(pPayload, Para2);
  1494. ENCODE_U32(pPayload, Para3);
  1495. ENCODE_U32(pPayload, Para4);
  1496. _SendPacket(pPayloadStart, pPayload, EventID);
  1497. RECORD_END();
  1498. }
  1499. /*********************************************************************
  1500. *
  1501. * SEGGER_SYSVIEW_RecordU32x6()
  1502. *
  1503. * Function description
  1504. * Formats and sends a SystemView packet containing 6 U32 parameter payload.
  1505. *
  1506. * Parameters
  1507. * EventID - SystemView event ID.
  1508. * Para0 - The 32-bit parameter encoded to SystemView packet payload.
  1509. * Para1 - The 32-bit parameter encoded to SystemView packet payload.
  1510. * Para2 - The 32-bit parameter encoded to SystemView packet payload.
  1511. * Para3 - The 32-bit parameter encoded to SystemView packet payload.
  1512. * Para4 - The 32-bit parameter encoded to SystemView packet payload.
  1513. * Para5 - The 32-bit parameter encoded to SystemView packet payload.
  1514. */
  1515. void SEGGER_SYSVIEW_RecordU32x6(unsigned int EventID, U32 Para0, U32 Para1, U32 Para2, U32 Para3, U32 Para4, U32 Para5) {
  1516. U8* pPayload;
  1517. U8* pPayloadStart;
  1518. RECORD_START(SEGGER_SYSVIEW_INFO_SIZE + 6 * SEGGER_SYSVIEW_QUANTA_U32);
  1519. //
  1520. pPayload = pPayloadStart;
  1521. ENCODE_U32(pPayload, Para0);
  1522. ENCODE_U32(pPayload, Para1);
  1523. ENCODE_U32(pPayload, Para2);
  1524. ENCODE_U32(pPayload, Para3);
  1525. ENCODE_U32(pPayload, Para4);
  1526. ENCODE_U32(pPayload, Para5);
  1527. _SendPacket(pPayloadStart, pPayload, EventID);
  1528. RECORD_END();
  1529. }
  1530. /*********************************************************************
  1531. *
  1532. * SEGGER_SYSVIEW_RecordU32x7()
  1533. *
  1534. * Function description
  1535. * Formats and sends a SystemView packet containing 7 U32 parameter payload.
  1536. *
  1537. * Parameters
  1538. * EventID - SystemView event ID.
  1539. * Para0 - The 32-bit parameter encoded to SystemView packet payload.
  1540. * Para1 - The 32-bit parameter encoded to SystemView packet payload.
  1541. * Para2 - The 32-bit parameter encoded to SystemView packet payload.
  1542. * Para3 - The 32-bit parameter encoded to SystemView packet payload.
  1543. * Para4 - The 32-bit parameter encoded to SystemView packet payload.
  1544. * Para5 - The 32-bit parameter encoded to SystemView packet payload.
  1545. * Para6 - The 32-bit parameter encoded to SystemView packet payload.
  1546. */
  1547. void SEGGER_SYSVIEW_RecordU32x7(unsigned int EventID, U32 Para0, U32 Para1, U32 Para2, U32 Para3, U32 Para4, U32 Para5, U32 Para6) {
  1548. U8* pPayload;
  1549. U8* pPayloadStart;
  1550. RECORD_START(SEGGER_SYSVIEW_INFO_SIZE + 7 * SEGGER_SYSVIEW_QUANTA_U32);
  1551. //
  1552. pPayload = pPayloadStart;
  1553. ENCODE_U32(pPayload, Para0);
  1554. ENCODE_U32(pPayload, Para1);
  1555. ENCODE_U32(pPayload, Para2);
  1556. ENCODE_U32(pPayload, Para3);
  1557. ENCODE_U32(pPayload, Para4);
  1558. ENCODE_U32(pPayload, Para5);
  1559. ENCODE_U32(pPayload, Para6);
  1560. _SendPacket(pPayloadStart, pPayload, EventID);
  1561. RECORD_END();
  1562. }
  1563. /*********************************************************************
  1564. *
  1565. * SEGGER_SYSVIEW_RecordU32x8()
  1566. *
  1567. * Function description
  1568. * Formats and sends a SystemView packet containing 8 U32 parameter payload.
  1569. *
  1570. * Parameters
  1571. * EventID - SystemView event ID.
  1572. * Para0 - The 32-bit parameter encoded to SystemView packet payload.
  1573. * Para1 - The 32-bit parameter encoded to SystemView packet payload.
  1574. * Para2 - The 32-bit parameter encoded to SystemView packet payload.
  1575. * Para3 - The 32-bit parameter encoded to SystemView packet payload.
  1576. * Para4 - The 32-bit parameter encoded to SystemView packet payload.
  1577. * Para5 - The 32-bit parameter encoded to SystemView packet payload.
  1578. * Para6 - The 32-bit parameter encoded to SystemView packet payload.
  1579. * Para7 - The 32-bit parameter encoded to SystemView packet payload.
  1580. */
  1581. void SEGGER_SYSVIEW_RecordU32x8(unsigned int EventID, U32 Para0, U32 Para1, U32 Para2, U32 Para3, U32 Para4, U32 Para5, U32 Para6, U32 Para7) {
  1582. U8* pPayload;
  1583. U8* pPayloadStart;
  1584. RECORD_START(SEGGER_SYSVIEW_INFO_SIZE + 8 * SEGGER_SYSVIEW_QUANTA_U32);
  1585. //
  1586. pPayload = pPayloadStart;
  1587. ENCODE_U32(pPayload, Para0);
  1588. ENCODE_U32(pPayload, Para1);
  1589. ENCODE_U32(pPayload, Para2);
  1590. ENCODE_U32(pPayload, Para3);
  1591. ENCODE_U32(pPayload, Para4);
  1592. ENCODE_U32(pPayload, Para5);
  1593. ENCODE_U32(pPayload, Para6);
  1594. ENCODE_U32(pPayload, Para7);
  1595. _SendPacket(pPayloadStart, pPayload, EventID);
  1596. RECORD_END();
  1597. }
  1598. /*********************************************************************
  1599. *
  1600. * SEGGER_SYSVIEW_RecordU32x9()
  1601. *
  1602. * Function description
  1603. * Formats and sends a SystemView packet containing 9 U32 parameter payload.
  1604. *
  1605. * Parameters
  1606. * EventID - SystemView event ID.
  1607. * Para0 - The 32-bit parameter encoded to SystemView packet payload.
  1608. * Para1 - The 32-bit parameter encoded to SystemView packet payload.
  1609. * Para2 - The 32-bit parameter encoded to SystemView packet payload.
  1610. * Para3 - The 32-bit parameter encoded to SystemView packet payload.
  1611. * Para4 - The 32-bit parameter encoded to SystemView packet payload.
  1612. * Para5 - The 32-bit parameter encoded to SystemView packet payload.
  1613. * Para6 - The 32-bit parameter encoded to SystemView packet payload.
  1614. * Para7 - The 32-bit parameter encoded to SystemView packet payload.
  1615. * Para8 - The 32-bit parameter encoded to SystemView packet payload.
  1616. */
  1617. void SEGGER_SYSVIEW_RecordU32x9(unsigned int EventID, U32 Para0, U32 Para1, U32 Para2, U32 Para3, U32 Para4, U32 Para5, U32 Para6, U32 Para7, U32 Para8) {
  1618. U8* pPayload;
  1619. U8* pPayloadStart;
  1620. RECORD_START(SEGGER_SYSVIEW_INFO_SIZE + 9 * SEGGER_SYSVIEW_QUANTA_U32);
  1621. //
  1622. pPayload = pPayloadStart;
  1623. ENCODE_U32(pPayload, Para0);
  1624. ENCODE_U32(pPayload, Para1);
  1625. ENCODE_U32(pPayload, Para2);
  1626. ENCODE_U32(pPayload, Para3);
  1627. ENCODE_U32(pPayload, Para4);
  1628. ENCODE_U32(pPayload, Para5);
  1629. ENCODE_U32(pPayload, Para6);
  1630. ENCODE_U32(pPayload, Para7);
  1631. ENCODE_U32(pPayload, Para8);
  1632. _SendPacket(pPayloadStart, pPayload, EventID);
  1633. RECORD_END();
  1634. }
  1635. /*********************************************************************
  1636. *
  1637. * SEGGER_SYSVIEW_RecordU32x10()
  1638. *
  1639. * Function description
  1640. * Formats and sends a SystemView packet containing 10 U32 parameter payload.
  1641. *
  1642. * Parameters
  1643. * EventID - SystemView event ID.
  1644. * Para0 - The 32-bit parameter encoded to SystemView packet payload.
  1645. * Para1 - The 32-bit parameter encoded to SystemView packet payload.
  1646. * Para2 - The 32-bit parameter encoded to SystemView packet payload.
  1647. * Para3 - The 32-bit parameter encoded to SystemView packet payload.
  1648. * Para4 - The 32-bit parameter encoded to SystemView packet payload.
  1649. * Para5 - The 32-bit parameter encoded to SystemView packet payload.
  1650. * Para6 - The 32-bit parameter encoded to SystemView packet payload.
  1651. * Para7 - The 32-bit parameter encoded to SystemView packet payload.
  1652. * Para8 - The 32-bit parameter encoded to SystemView packet payload.
  1653. * Para9 - The 32-bit parameter encoded to SystemView packet payload.
  1654. */
  1655. void SEGGER_SYSVIEW_RecordU32x10(unsigned int EventID, U32 Para0, U32 Para1, U32 Para2, U32 Para3, U32 Para4, U32 Para5, U32 Para6, U32 Para7, U32 Para8, U32 Para9) {
  1656. U8* pPayload;
  1657. U8* pPayloadStart;
  1658. RECORD_START(SEGGER_SYSVIEW_INFO_SIZE + 10 * SEGGER_SYSVIEW_QUANTA_U32);
  1659. //
  1660. pPayload = pPayloadStart;
  1661. ENCODE_U32(pPayload, Para0);
  1662. ENCODE_U32(pPayload, Para1);
  1663. ENCODE_U32(pPayload, Para2);
  1664. ENCODE_U32(pPayload, Para3);
  1665. ENCODE_U32(pPayload, Para4);
  1666. ENCODE_U32(pPayload, Para5);
  1667. ENCODE_U32(pPayload, Para6);
  1668. ENCODE_U32(pPayload, Para7);
  1669. ENCODE_U32(pPayload, Para8);
  1670. ENCODE_U32(pPayload, Para9);
  1671. _SendPacket(pPayloadStart, pPayload, EventID);
  1672. RECORD_END();
  1673. }
  1674. /*********************************************************************
  1675. *
  1676. * SEGGER_SYSVIEW_RecordString()
  1677. *
  1678. * Function description
  1679. * Formats and sends a SystemView packet containing a string.
  1680. *
  1681. * Parameters
  1682. * EventID - SystemView event ID.
  1683. * pString - The string to be sent in the SystemView packet payload.
  1684. *
  1685. * Additional information
  1686. * The string is encoded as a count byte followed by the contents
  1687. * of the string.
  1688. * No more than SEGGER_SYSVIEW_MAX_STRING_LEN bytes will be encoded to the payload.
  1689. */
  1690. void SEGGER_SYSVIEW_RecordString(unsigned int EventID, const char* pString) {
  1691. U8* pPayload;
  1692. U8* pPayloadStart;
  1693. RECORD_START(SEGGER_SYSVIEW_INFO_SIZE + 1 + SEGGER_SYSVIEW_MAX_STRING_LEN);
  1694. //
  1695. pPayload = _EncodeStr(pPayloadStart, pString, SEGGER_SYSVIEW_MAX_STRING_LEN);
  1696. _SendPacket(pPayloadStart, pPayload, EventID);
  1697. RECORD_END();
  1698. }
  1699. /*********************************************************************
  1700. *
  1701. * SEGGER_SYSVIEW_Start()
  1702. *
  1703. * Function description
  1704. * Start recording SystemView events.
  1705. *
  1706. * This function is triggered by the SystemView Application on connect.
  1707. * For single-shot or post-mortem mode recording, it needs to be called
  1708. * by the application.
  1709. *
  1710. * Additional information
  1711. * This function enables transmission of SystemView packets recorded
  1712. * by subsequent trace calls and records a SystemView Start event.
  1713. *
  1714. * As part of start, a SystemView Init packet is sent, containing the system
  1715. * frequency. The list of current tasks, the current system time and the
  1716. * system description string is sent, too.
  1717. *
  1718. * Notes
  1719. * SEGGER_SYSVIEW_Start and SEGGER_SYSVIEW_Stop do not nest.
  1720. * When SEGGER_SYSVIEW_CAN_RESTART is 1, each received start command
  1721. * records the system information. This is required to enable restart
  1722. * of recordings when SystemView unexpectedly disconnects without sending
  1723. * a stop command before.
  1724. */
  1725. void SEGGER_SYSVIEW_Start(void) {
  1726. #if (SEGGER_SYSVIEW_CAN_RESTART == 0)
  1727. if (_SYSVIEW_Globals.EnableState == 0) {
  1728. #endif
  1729. _SYSVIEW_Globals.EnableState = 1;
  1730. #if (SEGGER_SYSVIEW_POST_MORTEM_MODE == 1)
  1731. _SendSyncInfo();
  1732. #else
  1733. SEGGER_SYSVIEW_LOCK();
  1734. SEGGER_RTT_WriteSkipNoLock(CHANNEL_ID_UP, _abSync, 10);
  1735. SEGGER_SYSVIEW_UNLOCK();
  1736. SEGGER_SYSVIEW_ON_EVENT_RECORDED(10);
  1737. SEGGER_SYSVIEW_RecordVoid(SYSVIEW_EVTID_TRACE_START);
  1738. {
  1739. U8* pPayload;
  1740. U8* pPayloadStart;
  1741. RECORD_START(SEGGER_SYSVIEW_INFO_SIZE + 4 * SEGGER_SYSVIEW_QUANTA_U32);
  1742. //
  1743. pPayload = pPayloadStart;
  1744. ENCODE_U32(pPayload, _SYSVIEW_Globals.SysFreq);
  1745. ENCODE_U32(pPayload, _SYSVIEW_Globals.CPUFreq);
  1746. ENCODE_U32(pPayload, _SYSVIEW_Globals.RAMBaseAddress);
  1747. ENCODE_U32(pPayload, SEGGER_SYSVIEW_ID_SHIFT);
  1748. _SendPacket(pPayloadStart, pPayload, SYSVIEW_EVTID_INIT);
  1749. RECORD_END();
  1750. }
  1751. if (_SYSVIEW_Globals.pfSendSysDesc) {
  1752. _SYSVIEW_Globals.pfSendSysDesc();
  1753. }
  1754. SEGGER_SYSVIEW_RecordSystime();
  1755. SEGGER_SYSVIEW_SendTaskList();
  1756. SEGGER_SYSVIEW_SendNumModules();
  1757. #endif
  1758. #if (SEGGER_SYSVIEW_CAN_RESTART == 0)
  1759. }
  1760. #endif
  1761. }
  1762. /*********************************************************************
  1763. *
  1764. * SEGGER_SYSVIEW_Stop()
  1765. *
  1766. * Function description
  1767. * Stop recording SystemView events.
  1768. *
  1769. * This function is triggered by the SystemView Application on disconnect.
  1770. * For single-shot or post-mortem mode recording, it can be called
  1771. * by the application.
  1772. *
  1773. * Additional information
  1774. * This function disables transmission of SystemView packets recorded
  1775. * by subsequent trace calls. If transmission is enabled when
  1776. * this function is called, a single SystemView Stop event is recorded
  1777. * to the trace, send, and then trace transmission is halted.
  1778. */
  1779. void SEGGER_SYSVIEW_Stop(void) {
  1780. U8* pPayloadStart;
  1781. RECORD_START(SEGGER_SYSVIEW_INFO_SIZE);
  1782. //
  1783. if (_SYSVIEW_Globals.EnableState) {
  1784. _SendPacket(pPayloadStart, pPayloadStart, SYSVIEW_EVTID_TRACE_STOP);
  1785. _SYSVIEW_Globals.EnableState = 0;
  1786. }
  1787. RECORD_END();
  1788. }
  1789. /*********************************************************************
  1790. *
  1791. * SEGGER_SYSVIEW_GetChannelID()
  1792. *
  1793. * Function description
  1794. * Returns the RTT <Up> / <Down> channel ID used by SystemView.
  1795. */
  1796. int SEGGER_SYSVIEW_GetChannelID(void) {
  1797. return CHANNEL_ID_UP;
  1798. }
  1799. /*********************************************************************
  1800. *
  1801. * SEGGER_SYSVIEW_GetSysDesc()
  1802. *
  1803. * Function description
  1804. * Triggers a send of the system information and description.
  1805. *
  1806. */
  1807. void SEGGER_SYSVIEW_GetSysDesc(void) {
  1808. U8* pPayload;
  1809. U8* pPayloadStart;
  1810. RECORD_START(SEGGER_SYSVIEW_INFO_SIZE + 4 * SEGGER_SYSVIEW_QUANTA_U32);
  1811. //
  1812. pPayload = pPayloadStart;
  1813. ENCODE_U32(pPayload, _SYSVIEW_Globals.SysFreq);
  1814. ENCODE_U32(pPayload, _SYSVIEW_Globals.CPUFreq);
  1815. ENCODE_U32(pPayload, _SYSVIEW_Globals.RAMBaseAddress);
  1816. ENCODE_U32(pPayload, SEGGER_SYSVIEW_ID_SHIFT);
  1817. _SendPacket(pPayloadStart, pPayload, SYSVIEW_EVTID_INIT);
  1818. RECORD_END();
  1819. if (_SYSVIEW_Globals.pfSendSysDesc) {
  1820. _SYSVIEW_Globals.pfSendSysDesc();
  1821. }
  1822. }
  1823. /*********************************************************************
  1824. *
  1825. * SEGGER_SYSVIEW_SendTaskInfo()
  1826. *
  1827. * Function description
  1828. * Send a Task Info Packet, containing TaskId for identification,
  1829. * task priority and task name.
  1830. *
  1831. * Parameters
  1832. * pInfo - Pointer to task information to send.
  1833. */
  1834. void SEGGER_SYSVIEW_SendTaskInfo(const SEGGER_SYSVIEW_TASKINFO *pInfo) {
  1835. U8* pPayload;
  1836. U8* pPayloadStart;
  1837. RECORD_START(SEGGER_SYSVIEW_INFO_SIZE + SEGGER_SYSVIEW_QUANTA_U32 + 1 + 32);
  1838. //
  1839. pPayload = pPayloadStart;
  1840. ENCODE_U32(pPayload, SHRINK_ID(pInfo->TaskID));
  1841. ENCODE_U32(pPayload, pInfo->Prio);
  1842. pPayload = _EncodeStr(pPayload, pInfo->sName, 32);
  1843. _SendPacket(pPayloadStart, pPayload, SYSVIEW_EVTID_TASK_INFO);
  1844. //
  1845. pPayload = pPayloadStart;
  1846. ENCODE_U32(pPayload, SHRINK_ID(pInfo->TaskID));
  1847. ENCODE_U32(pPayload, pInfo->StackBase);
  1848. ENCODE_U32(pPayload, pInfo->StackSize);
  1849. ENCODE_U32(pPayload, 0); // Stack End, future use
  1850. _SendPacket(pPayloadStart, pPayload, SYSVIEW_EVTID_STACK_INFO);
  1851. RECORD_END();
  1852. }
  1853. /*********************************************************************
  1854. *
  1855. * SEGGER_SYSVIEW_SendTaskList()
  1856. *
  1857. * Function description
  1858. * Send all tasks descriptors to the host.
  1859. */
  1860. void SEGGER_SYSVIEW_SendTaskList(void) {
  1861. if (_SYSVIEW_Globals.pOSAPI && _SYSVIEW_Globals.pOSAPI->pfSendTaskList) {
  1862. _SYSVIEW_Globals.pOSAPI->pfSendTaskList();
  1863. }
  1864. }
  1865. /*********************************************************************
  1866. *
  1867. * SEGGER_SYSVIEW_SendSysDesc()
  1868. *
  1869. * Function description
  1870. * Send the system description string to the host.
  1871. * The system description is used by the Systemview Application
  1872. * to identify the current application and handle events accordingly.
  1873. *
  1874. * The system description is usually called by the system description
  1875. * callback, to ensure it is only sent when the SystemView Application
  1876. * is connected.
  1877. *
  1878. * Parameters
  1879. * sSysDesc - Pointer to the 0-terminated system description string.
  1880. *
  1881. * Additional information
  1882. * One system description string may not exceed SEGGER_SYSVIEW_MAX_STRING_LEN characters.
  1883. * Multiple description strings can be recorded.
  1884. *
  1885. * The Following items can be described in a system description string.
  1886. * Each item is identified by its identifier, followed by '=' and the value.
  1887. * Items are separated by ','.
  1888. */
  1889. void SEGGER_SYSVIEW_SendSysDesc(const char *sSysDesc) {
  1890. U8* pPayload;
  1891. U8* pPayloadStart;
  1892. RECORD_START(SEGGER_SYSVIEW_INFO_SIZE + 1 + SEGGER_SYSVIEW_MAX_STRING_LEN);
  1893. //
  1894. pPayload = _EncodeStr(pPayloadStart, sSysDesc, SEGGER_SYSVIEW_MAX_STRING_LEN);
  1895. _SendPacket(pPayloadStart, pPayload, SYSVIEW_EVTID_SYSDESC);
  1896. RECORD_END();
  1897. }
  1898. /*********************************************************************
  1899. *
  1900. * SEGGER_SYSVIEW_RecordSystime()
  1901. *
  1902. * Function description
  1903. * Formats and sends a SystemView Systime containing a single U64 or U32
  1904. * parameter payload.
  1905. */
  1906. void SEGGER_SYSVIEW_RecordSystime(void) {
  1907. U64 Systime;
  1908. if (_SYSVIEW_Globals.pOSAPI && _SYSVIEW_Globals.pOSAPI->pfGetTime) {
  1909. Systime = _SYSVIEW_Globals.pOSAPI->pfGetTime();
  1910. SEGGER_SYSVIEW_RecordU32x2(SYSVIEW_EVTID_SYSTIME_US,
  1911. (U32)(Systime),
  1912. (U32)(Systime >> 32));
  1913. } else {
  1914. SEGGER_SYSVIEW_RecordU32(SYSVIEW_EVTID_SYSTIME_CYCLES, SEGGER_SYSVIEW_GET_TIMESTAMP());
  1915. }
  1916. }
  1917. /*********************************************************************
  1918. *
  1919. * SEGGER_SYSVIEW_RecordEnterISR()
  1920. *
  1921. * Function description
  1922. * Format and send an ISR entry event.
  1923. *
  1924. * Additional information
  1925. * Example packets sent
  1926. * 02 0F 50 // ISR(15) Enter. Timestamp is 80 (0x50)
  1927. */
  1928. void SEGGER_SYSVIEW_RecordEnterISR(void) {
  1929. unsigned v;
  1930. U8* pPayload;
  1931. U8* pPayloadStart;
  1932. RECORD_START(SEGGER_SYSVIEW_INFO_SIZE + SEGGER_SYSVIEW_QUANTA_U32);
  1933. //
  1934. pPayload = pPayloadStart;
  1935. v = SEGGER_SYSVIEW_GET_INTERRUPT_ID();
  1936. ENCODE_U32(pPayload, v);
  1937. _SendPacket(pPayloadStart, pPayload, SYSVIEW_EVTID_ISR_ENTER);
  1938. RECORD_END();
  1939. }
  1940. /*********************************************************************
  1941. *
  1942. * SEGGER_SYSVIEW_RecordExitISR()
  1943. *
  1944. * Function description
  1945. * Format and send an ISR exit event.
  1946. *
  1947. * Additional information
  1948. * Format as follows:
  1949. * 03 <TimeStamp> // Max. packet len is 6
  1950. *
  1951. * Example packets sent
  1952. * 03 20 // ISR Exit. Timestamp is 32 (0x20)
  1953. */
  1954. void SEGGER_SYSVIEW_RecordExitISR(void) {
  1955. U8* pPayloadStart;
  1956. RECORD_START(SEGGER_SYSVIEW_INFO_SIZE);
  1957. //
  1958. _SendPacket(pPayloadStart, pPayloadStart, SYSVIEW_EVTID_ISR_EXIT);
  1959. RECORD_END();
  1960. }
  1961. /*********************************************************************
  1962. *
  1963. * SEGGER_SYSVIEW_RecordExitISRToScheduler()
  1964. *
  1965. * Function description
  1966. * Format and send an ISR exit into scheduler event.
  1967. *
  1968. * Additional information
  1969. * Format as follows:
  1970. * 18 <TimeStamp> // Max. packet len is 6
  1971. *
  1972. * Example packets sent
  1973. * 18 20 // ISR Exit to Scheduler. Timestamp is 32 (0x20)
  1974. */
  1975. void SEGGER_SYSVIEW_RecordExitISRToScheduler(void) {
  1976. U8* pPayloadStart;
  1977. RECORD_START(SEGGER_SYSVIEW_INFO_SIZE);
  1978. //
  1979. _SendPacket(pPayloadStart, pPayloadStart, SYSVIEW_EVTID_ISR_TO_SCHEDULER);
  1980. RECORD_END();
  1981. }
  1982. /*********************************************************************
  1983. *
  1984. * SEGGER_SYSVIEW_RecordEnterTimer()
  1985. *
  1986. * Function description
  1987. * Format and send a Timer entry event.
  1988. *
  1989. * Parameters
  1990. * TimerId - Id of the timer which starts.
  1991. */
  1992. void SEGGER_SYSVIEW_RecordEnterTimer(U32 TimerId) {
  1993. U8* pPayload;
  1994. U8* pPayloadStart;
  1995. RECORD_START(SEGGER_SYSVIEW_INFO_SIZE + SEGGER_SYSVIEW_QUANTA_U32);
  1996. //
  1997. pPayload = pPayloadStart;
  1998. ENCODE_U32(pPayload, SHRINK_ID(TimerId));
  1999. _SendPacket(pPayloadStart, pPayload, SYSVIEW_EVTID_TIMER_ENTER);
  2000. RECORD_END();
  2001. }
  2002. /*********************************************************************
  2003. *
  2004. * SEGGER_SYSVIEW_RecordExitTimer()
  2005. *
  2006. * Function description
  2007. * Format and send a Timer exit event.
  2008. */
  2009. void SEGGER_SYSVIEW_RecordExitTimer(void) {
  2010. U8* pPayloadStart;
  2011. RECORD_START(SEGGER_SYSVIEW_INFO_SIZE);
  2012. //
  2013. _SendPacket(pPayloadStart, pPayloadStart, SYSVIEW_EVTID_TIMER_EXIT);
  2014. RECORD_END();
  2015. }
  2016. /*********************************************************************
  2017. *
  2018. * SEGGER_SYSVIEW_RecordEndCall()
  2019. *
  2020. * Function description
  2021. * Format and send an End API Call event without return value.
  2022. *
  2023. * Parameters
  2024. * EventID - Id of API function which ends.
  2025. */
  2026. void SEGGER_SYSVIEW_RecordEndCall(unsigned int EventID) {
  2027. U8* pPayload;
  2028. U8* pPayloadStart;
  2029. RECORD_START(SEGGER_SYSVIEW_INFO_SIZE + SEGGER_SYSVIEW_QUANTA_U32);
  2030. //
  2031. pPayload = pPayloadStart;
  2032. ENCODE_U32(pPayload, EventID);
  2033. _SendPacket(pPayloadStart, pPayload, SYSVIEW_EVTID_END_CALL);
  2034. RECORD_END();
  2035. }
  2036. /*********************************************************************
  2037. *
  2038. * SEGGER_SYSVIEW_RecordEndCallU32()
  2039. *
  2040. * Function description
  2041. * Format and send an End API Call event with return value.
  2042. *
  2043. * Parameters
  2044. * EventID - Id of API function which ends.
  2045. * Para0 - Return value which will be returned by the API function.
  2046. */
  2047. void SEGGER_SYSVIEW_RecordEndCallU32(unsigned int EventID, U32 Para0) {
  2048. U8* pPayload;
  2049. U8* pPayloadStart;
  2050. RECORD_START(SEGGER_SYSVIEW_INFO_SIZE + 2 * SEGGER_SYSVIEW_QUANTA_U32);
  2051. //
  2052. pPayload = pPayloadStart;
  2053. ENCODE_U32(pPayload, EventID);
  2054. ENCODE_U32(pPayload, Para0);
  2055. _SendPacket(pPayloadStart, pPayload, SYSVIEW_EVTID_END_CALL);
  2056. RECORD_END();
  2057. }
  2058. /*********************************************************************
  2059. *
  2060. * SEGGER_SYSVIEW_OnIdle()
  2061. *
  2062. * Function description
  2063. * Record an Idle event.
  2064. */
  2065. void SEGGER_SYSVIEW_OnIdle(void) {
  2066. U8* pPayloadStart;
  2067. RECORD_START(SEGGER_SYSVIEW_INFO_SIZE);
  2068. //
  2069. _SendPacket(pPayloadStart, pPayloadStart, SYSVIEW_EVTID_IDLE);
  2070. RECORD_END();
  2071. }
  2072. /*********************************************************************
  2073. *
  2074. * SEGGER_SYSVIEW_OnTaskCreate()
  2075. *
  2076. * Function description
  2077. * Record a Task Create event. The Task Create event corresponds
  2078. * to creating a task in the OS.
  2079. *
  2080. * Parameters
  2081. * TaskId - Task ID of created task.
  2082. */
  2083. void SEGGER_SYSVIEW_OnTaskCreate(U32 TaskId) {
  2084. U8* pPayload;
  2085. U8* pPayloadStart;
  2086. RECORD_START(SEGGER_SYSVIEW_INFO_SIZE + SEGGER_SYSVIEW_QUANTA_U32);
  2087. //
  2088. pPayload = pPayloadStart;
  2089. TaskId = SHRINK_ID(TaskId);
  2090. ENCODE_U32(pPayload, TaskId);
  2091. _SendPacket(pPayloadStart, pPayload, SYSVIEW_EVTID_TASK_CREATE);
  2092. RECORD_END();
  2093. }
  2094. /*********************************************************************
  2095. *
  2096. * SEGGER_SYSVIEW_OnTaskTerminate()
  2097. *
  2098. * Function description
  2099. * Record a Task termination event.
  2100. * The Task termination event corresponds to terminating a task in
  2101. * the OS. If the TaskId is the currently active task,
  2102. * SEGGER_SYSVIEW_OnTaskStopExec may be used, either.
  2103. *
  2104. * Parameters
  2105. * TaskId - Task ID of terminated task.
  2106. */
  2107. void SEGGER_SYSVIEW_OnTaskTerminate(U32 TaskId) {
  2108. U8* pPayload;
  2109. U8* pPayloadStart;
  2110. RECORD_START(SEGGER_SYSVIEW_INFO_SIZE + SEGGER_SYSVIEW_QUANTA_U32);
  2111. //
  2112. pPayload = pPayloadStart;
  2113. TaskId = SHRINK_ID(TaskId);
  2114. ENCODE_U32(pPayload, TaskId);
  2115. _SendPacket(pPayloadStart, pPayload, SYSVIEW_EVTID_TASK_TERMINATE);
  2116. RECORD_END();
  2117. }
  2118. /*********************************************************************
  2119. *
  2120. * SEGGER_SYSVIEW_OnTaskStartExec()
  2121. *
  2122. * Function description
  2123. * Record a Task Start Execution event. The Task Start event
  2124. * corresponds to when a task has started to execute rather than
  2125. * when it is ready to execute.
  2126. *
  2127. * Parameters
  2128. * TaskId - Task ID of task that started to execute.
  2129. */
  2130. void SEGGER_SYSVIEW_OnTaskStartExec(U32 TaskId) {
  2131. U8* pPayload;
  2132. U8* pPayloadStart;
  2133. RECORD_START(SEGGER_SYSVIEW_INFO_SIZE + SEGGER_SYSVIEW_QUANTA_U32);
  2134. //
  2135. pPayload = pPayloadStart;
  2136. TaskId = SHRINK_ID(TaskId);
  2137. ENCODE_U32(pPayload, TaskId);
  2138. _SendPacket(pPayloadStart, pPayload, SYSVIEW_EVTID_TASK_START_EXEC);
  2139. RECORD_END();
  2140. }
  2141. /*********************************************************************
  2142. *
  2143. * SEGGER_SYSVIEW_OnTaskStopExec()
  2144. *
  2145. * Function description
  2146. * Record a Task Stop Execution event. The Task Stop event
  2147. * corresponds to when a task stops executing and terminates.
  2148. */
  2149. void SEGGER_SYSVIEW_OnTaskStopExec(void) {
  2150. U8* pPayloadStart;
  2151. RECORD_START(SEGGER_SYSVIEW_INFO_SIZE);
  2152. //
  2153. _SendPacket(pPayloadStart, pPayloadStart, SYSVIEW_EVTID_TASK_STOP_EXEC);
  2154. RECORD_END();
  2155. }
  2156. /*********************************************************************
  2157. *
  2158. * SEGGER_SYSVIEW_OnTaskStartReady()
  2159. *
  2160. * Function description
  2161. * Record a Task Start Ready event.
  2162. *
  2163. * Parameters
  2164. * TaskId - Task ID of task that started to execute.
  2165. */
  2166. void SEGGER_SYSVIEW_OnTaskStartReady(U32 TaskId) {
  2167. U8* pPayload;
  2168. U8* pPayloadStart;
  2169. RECORD_START(SEGGER_SYSVIEW_INFO_SIZE + SEGGER_SYSVIEW_QUANTA_U32);
  2170. //
  2171. pPayload = pPayloadStart;
  2172. TaskId = SHRINK_ID(TaskId);
  2173. ENCODE_U32(pPayload, TaskId);
  2174. _SendPacket(pPayloadStart, pPayload, SYSVIEW_EVTID_TASK_START_READY);
  2175. RECORD_END();
  2176. }
  2177. /*********************************************************************
  2178. *
  2179. * SEGGER_SYSVIEW_OnTaskStopReady()
  2180. *
  2181. * Function description
  2182. * Record a Task Stop Ready event.
  2183. *
  2184. * Parameters
  2185. * TaskId - Task ID of task that completed execution.
  2186. * Cause - Reason for task to stop (i.e. Idle/Sleep)
  2187. */
  2188. void SEGGER_SYSVIEW_OnTaskStopReady(U32 TaskId, unsigned int Cause) {
  2189. U8* pPayload;
  2190. U8* pPayloadStart;
  2191. RECORD_START(SEGGER_SYSVIEW_INFO_SIZE + 2 * SEGGER_SYSVIEW_QUANTA_U32);
  2192. //
  2193. pPayload = pPayloadStart;
  2194. TaskId = SHRINK_ID(TaskId);
  2195. ENCODE_U32(pPayload, TaskId);
  2196. ENCODE_U32(pPayload, Cause);
  2197. _SendPacket(pPayloadStart, pPayload, SYSVIEW_EVTID_TASK_STOP_READY);
  2198. RECORD_END();
  2199. }
  2200. /*********************************************************************
  2201. *
  2202. * SEGGER_SYSVIEW_MarkStart()
  2203. *
  2204. * Function description
  2205. * Record a Performance Marker Start event to start measuring runtime.
  2206. *
  2207. * Parameters
  2208. * MarkerId - User defined ID for the marker.
  2209. */
  2210. void SEGGER_SYSVIEW_MarkStart(unsigned MarkerId) {
  2211. U8* pPayload;
  2212. U8* pPayloadStart;
  2213. RECORD_START(SEGGER_SYSVIEW_INFO_SIZE + SEGGER_SYSVIEW_QUANTA_U32);
  2214. //
  2215. pPayload = pPayloadStart;
  2216. ENCODE_U32(pPayload, MarkerId);
  2217. _SendPacket(pPayloadStart, pPayload, SYSVIEW_EVTID_MARK_START);
  2218. RECORD_END();
  2219. }
  2220. /*********************************************************************
  2221. *
  2222. * SEGGER_SYSVIEW_MarkStop()
  2223. *
  2224. * Function description
  2225. * Record a Performance Marker Stop event to stop measuring runtime.
  2226. *
  2227. * Parameters
  2228. * MarkerId - User defined ID for the marker.
  2229. */
  2230. void SEGGER_SYSVIEW_MarkStop(unsigned MarkerId) {
  2231. U8 * pPayload;
  2232. U8 * pPayloadStart;
  2233. RECORD_START(SEGGER_SYSVIEW_INFO_SIZE + SEGGER_SYSVIEW_QUANTA_U32);
  2234. //
  2235. pPayload = pPayloadStart;
  2236. ENCODE_U32(pPayload, MarkerId);
  2237. _SendPacket(pPayloadStart, pPayload, SYSVIEW_EVTID_MARK_STOP);
  2238. RECORD_END();
  2239. }
  2240. /*********************************************************************
  2241. *
  2242. * SEGGER_SYSVIEW_Mark()
  2243. *
  2244. * Function description
  2245. * Record a Performance Marker intermediate event.
  2246. *
  2247. * Parameters
  2248. * MarkerId - User defined ID for the marker.
  2249. */
  2250. void SEGGER_SYSVIEW_Mark(unsigned int MarkerId) {
  2251. U8* pPayload;
  2252. U8* pPayloadStart;
  2253. RECORD_START(SEGGER_SYSVIEW_INFO_SIZE + 2 * SEGGER_SYSVIEW_QUANTA_U32);
  2254. //
  2255. pPayload = pPayloadStart;
  2256. ENCODE_U32(pPayload, SYSVIEW_EVTID_EX_MARK);
  2257. ENCODE_U32(pPayload, MarkerId);
  2258. _SendPacket(pPayloadStart, pPayload, SYSVIEW_EVTID_EX);
  2259. RECORD_END();
  2260. }
  2261. /*********************************************************************
  2262. *
  2263. * SEGGER_SYSVIEW_NameMarker()
  2264. *
  2265. * Function description
  2266. * Send the name of a Performance Marker to be displayed in SystemView.
  2267. *
  2268. * Marker names are usually set in the system description
  2269. * callback, to ensure it is only sent when the SystemView Application
  2270. * is connected.
  2271. *
  2272. * Parameters
  2273. * MarkerId - User defined ID for the marker.
  2274. * sName - Pointer to the marker name. (Max. SEGGER_SYSVIEW_MAX_STRING_LEN Bytes)
  2275. */
  2276. void SEGGER_SYSVIEW_NameMarker(unsigned int MarkerId, const char* sName) {
  2277. U8* pPayload;
  2278. U8* pPayloadStart;
  2279. RECORD_START(SEGGER_SYSVIEW_INFO_SIZE + 2 * SEGGER_SYSVIEW_QUANTA_U32 + 1 + SEGGER_SYSVIEW_MAX_STRING_LEN);
  2280. //
  2281. pPayload = pPayloadStart;
  2282. ENCODE_U32(pPayload, SYSVIEW_EVTID_EX_NAME_MARKER);
  2283. ENCODE_U32(pPayload, MarkerId);
  2284. pPayload = _EncodeStr(pPayload, sName, SEGGER_SYSVIEW_MAX_STRING_LEN);
  2285. _SendPacket(pPayloadStart, pPayload, SYSVIEW_EVTID_EX);
  2286. RECORD_END();
  2287. }
  2288. /*********************************************************************
  2289. *
  2290. * SEGGER_SYSVIEW_NameResource()
  2291. *
  2292. * Function description
  2293. * Send the name of a resource to be displayed in SystemView.
  2294. *
  2295. * Marker names are usually set in the system description
  2296. * callback, to ensure it is only sent when the SystemView Application
  2297. * is connected.
  2298. *
  2299. * Parameters
  2300. * ResourceId - Id of the resource to be named. i.e. its address.
  2301. * sName - Pointer to the resource name. (Max. SEGGER_SYSVIEW_MAX_STRING_LEN Bytes)
  2302. */
  2303. void SEGGER_SYSVIEW_NameResource(U32 ResourceId, const char* sName) {
  2304. U8* pPayload;
  2305. U8* pPayloadStart;
  2306. RECORD_START(SEGGER_SYSVIEW_INFO_SIZE + SEGGER_SYSVIEW_QUANTA_U32 + 1 + SEGGER_SYSVIEW_MAX_STRING_LEN);
  2307. //
  2308. pPayload = pPayloadStart;
  2309. ENCODE_U32(pPayload, SHRINK_ID(ResourceId));
  2310. pPayload = _EncodeStr(pPayload, sName, SEGGER_SYSVIEW_MAX_STRING_LEN);
  2311. _SendPacket(pPayloadStart, pPayload, SYSVIEW_EVTID_NAME_RESOURCE);
  2312. RECORD_END();
  2313. }
  2314. /*********************************************************************
  2315. *
  2316. * SEGGER_SYSVIEW_SendPacket()
  2317. *
  2318. * Function description
  2319. * Send an event packet.
  2320. *
  2321. * Parameters
  2322. * pPacket - Pointer to the start of the packet.
  2323. * pPayloadEnd - Pointer to the end of the payload.
  2324. * Make sure there are at least 5 bytes free after the payload.
  2325. * EventId - Id of the event packet.
  2326. *
  2327. * Return value
  2328. * !=0: Success, Message sent.
  2329. * ==0: Buffer full, Message *NOT* sent.
  2330. */
  2331. int SEGGER_SYSVIEW_SendPacket(U8* pPacket, U8* pPayloadEnd, unsigned int EventId) {
  2332. #if (SEGGER_SYSVIEW_USE_STATIC_BUFFER == 1)
  2333. SEGGER_SYSVIEW_LOCK();
  2334. #endif
  2335. _SendPacket(pPacket + 4, pPayloadEnd, EventId);
  2336. #if (SEGGER_SYSVIEW_USE_STATIC_BUFFER == 1)
  2337. SEGGER_SYSVIEW_UNLOCK();
  2338. #endif
  2339. return 0;
  2340. }
  2341. /*********************************************************************
  2342. *
  2343. * SEGGER_SYSVIEW_EncodeU32()
  2344. *
  2345. * Function description
  2346. * Encode a U32 in variable-length format.
  2347. *
  2348. * Parameters
  2349. * pPayload - Pointer to where U32 will be encoded.
  2350. * Value - The 32-bit value to be encoded.
  2351. *
  2352. * Return value
  2353. * Pointer to the byte following the value, i.e. the first free
  2354. * byte in the payload and the next position to store payload
  2355. * content.
  2356. */
  2357. U8* SEGGER_SYSVIEW_EncodeU32(U8* pPayload, U32 Value) {
  2358. ENCODE_U32(pPayload, Value);
  2359. return pPayload;
  2360. }
  2361. /*********************************************************************
  2362. *
  2363. * SEGGER_SYSVIEW_EncodeString()
  2364. *
  2365. * Function description
  2366. * Encode a string in variable-length format.
  2367. *
  2368. * Parameters
  2369. * pPayload - Pointer to where string will be encoded.
  2370. * s - String to encode.
  2371. * MaxLen - Maximum number of characters to encode from string.
  2372. *
  2373. * Return value
  2374. * Pointer to the byte following the value, i.e. the first free
  2375. * byte in the payload and the next position to store payload
  2376. * content.
  2377. *
  2378. * Additional information
  2379. * The string is encoded as a count byte followed by the contents
  2380. * of the string.
  2381. * No more than 1 + MaxLen bytes will be encoded to the payload.
  2382. */
  2383. U8* SEGGER_SYSVIEW_EncodeString(U8* pPayload, const char* s, unsigned int MaxLen) {
  2384. return _EncodeStr(pPayload, s, MaxLen);
  2385. }
  2386. /*********************************************************************
  2387. *
  2388. * SEGGER_SYSVIEW_EncodeData()
  2389. *
  2390. * Function description
  2391. * Encode a byte buffer in variable-length format.
  2392. *
  2393. * Parameters
  2394. * pPayload - Pointer to where string will be encoded.
  2395. * pSrc - Pointer to data buffer to be encoded.
  2396. * NumBytes - Number of bytes in the buffer to be encoded.
  2397. *
  2398. * Return value
  2399. * Pointer to the byte following the value, i.e. the first free
  2400. * byte in the payload and the next position to store payload
  2401. * content.
  2402. *
  2403. * Additional information
  2404. * The data is encoded as a count byte followed by the contents
  2405. * of the data buffer.
  2406. * Make sure NumBytes + 1 bytes are free for the payload.
  2407. */
  2408. U8* SEGGER_SYSVIEW_EncodeData(U8 *pPayload, const char* pSrc, unsigned int NumBytes) {
  2409. return _EncodeData(pPayload, pSrc, NumBytes);
  2410. }
  2411. /*********************************************************************
  2412. *
  2413. * SEGGER_SYSVIEW_EncodeId()
  2414. *
  2415. * Function description
  2416. * Encode a 32-bit Id in shrunken variable-length format.
  2417. *
  2418. * Parameters
  2419. * pPayload - Pointer to where the Id will be encoded.
  2420. * Id - The 32-bit value to be encoded.
  2421. *
  2422. * Return value
  2423. * Pointer to the byte following the value, i.e. the first free
  2424. * byte in the payload and the next position to store payload
  2425. * content.
  2426. *
  2427. * Additional information
  2428. * The parameters to shrink an Id can be configured in
  2429. * SEGGER_SYSVIEW_Conf.h and via SEGGER_SYSVIEW_SetRAMBase().
  2430. * SEGGER_SYSVIEW_ID_BASE: Lowest Id reported by the application.
  2431. * (i.e. 0x20000000 when all Ids are an address in this RAM)
  2432. * SEGGER_SYSVIEW_ID_SHIFT: Number of bits to shift the Id to
  2433. * save bandwidth. (i.e. 2 when Ids are 4 byte aligned)
  2434. */
  2435. U8* SEGGER_SYSVIEW_EncodeId(U8* pPayload, U32 Id) {
  2436. Id = SHRINK_ID(Id);
  2437. ENCODE_U32(pPayload, Id);
  2438. return pPayload;
  2439. }
  2440. /*********************************************************************
  2441. *
  2442. * SEGGER_SYSVIEW_ShrinkId()
  2443. *
  2444. * Function description
  2445. * Get the shrunken value of an Id for further processing like in
  2446. * SEGGER_SYSVIEW_NameResource().
  2447. *
  2448. * Parameters
  2449. * Id - The 32-bit value to be shrunken.
  2450. *
  2451. * Return value
  2452. * Shrunken Id.
  2453. *
  2454. * Additional information
  2455. * The parameters to shrink an Id can be configured in
  2456. * SEGGER_SYSVIEW_Conf.h and via SEGGER_SYSVIEW_SetRAMBase().
  2457. * SEGGER_SYSVIEW_ID_BASE: Lowest Id reported by the application.
  2458. * (i.e. 0x20000000 when all Ids are an address in this RAM)
  2459. * SEGGER_SYSVIEW_ID_SHIFT: Number of bits to shift the Id to
  2460. * save bandwidth. (i.e. 2 when Ids are 4 byte aligned)
  2461. */
  2462. U32 SEGGER_SYSVIEW_ShrinkId(U32 Id) {
  2463. return SHRINK_ID(Id);
  2464. }
  2465. /*********************************************************************
  2466. *
  2467. * SEGGER_SYSVIEW_RegisterModule()
  2468. *
  2469. * Function description
  2470. * Register a middleware module for recording its events.
  2471. *
  2472. * Parameters
  2473. * pModule - The middleware module information.
  2474. *
  2475. * Additional information
  2476. * SEGGER_SYSVIEW_MODULE elements:
  2477. * sDescription - Pointer to a string containing the module name and optionally the module event description.
  2478. * NumEvents - Number of events the module wants to register.
  2479. * EventOffset - Offset to be added to the event Ids. Out parameter, set by this function. Do not modify after calling this function.
  2480. * pfSendModuleDesc - Callback function pointer to send more detailed module description to SystemView Application.
  2481. * pNext - Pointer to next registered module. Out parameter, set by this function. Do not modify after calling this function.
  2482. */
  2483. void SEGGER_SYSVIEW_RegisterModule(SEGGER_SYSVIEW_MODULE* pModule) {
  2484. SEGGER_SYSVIEW_LOCK();
  2485. if (_pFirstModule == 0) {
  2486. //
  2487. // No module registered, yet.
  2488. // Start list with new module.
  2489. // EventOffset is the base offset for modules
  2490. //
  2491. pModule->EventOffset = MODULE_EVENT_OFFSET;
  2492. pModule->pNext = 0;
  2493. _pFirstModule = pModule;
  2494. _NumModules = 1;
  2495. } else {
  2496. //
  2497. // Registreded module(s) present.
  2498. // Prepend new module in list.
  2499. // EventOffset set from number of events and offset of previous module.
  2500. //
  2501. pModule->EventOffset = _pFirstModule->EventOffset + _pFirstModule->NumEvents;
  2502. pModule->pNext = _pFirstModule;
  2503. _pFirstModule = pModule;
  2504. _NumModules++;
  2505. }
  2506. SEGGER_SYSVIEW_SendModule(0);
  2507. if (pModule->pfSendModuleDesc) {
  2508. pModule->pfSendModuleDesc();
  2509. }
  2510. SEGGER_SYSVIEW_UNLOCK();
  2511. }
  2512. /*********************************************************************
  2513. *
  2514. * SEGGER_SYSVIEW_RecordModuleDescription()
  2515. *
  2516. * Function description
  2517. * Sends detailed information of a registered module to the host.
  2518. *
  2519. * Parameters
  2520. * pModule - Pointer to the described module.
  2521. * sDescription - Pointer to a description string.
  2522. */
  2523. void SEGGER_SYSVIEW_RecordModuleDescription(const SEGGER_SYSVIEW_MODULE* pModule, const char* sDescription) {
  2524. U8 ModuleId;
  2525. SEGGER_SYSVIEW_MODULE* p;
  2526. p = _pFirstModule;
  2527. ModuleId = 0;
  2528. do {
  2529. if (p == pModule) {
  2530. break;
  2531. }
  2532. ModuleId++;
  2533. p = p->pNext;
  2534. } while (p);
  2535. {
  2536. U8* pPayload;
  2537. U8* pPayloadStart;
  2538. RECORD_START(SEGGER_SYSVIEW_INFO_SIZE + 2 * SEGGER_SYSVIEW_QUANTA_U32 + 1 + SEGGER_SYSVIEW_MAX_STRING_LEN);
  2539. //
  2540. pPayload = pPayloadStart;
  2541. //
  2542. // Send module description
  2543. // Send event offset and number of events
  2544. //
  2545. ENCODE_U32(pPayload, ModuleId);
  2546. ENCODE_U32(pPayload, (pModule->EventOffset));
  2547. pPayload = _EncodeStr(pPayload, sDescription, SEGGER_SYSVIEW_MAX_STRING_LEN);
  2548. _SendPacket(pPayloadStart, pPayload, SYSVIEW_EVTID_MODULEDESC);
  2549. RECORD_END();
  2550. }
  2551. }
  2552. /*********************************************************************
  2553. *
  2554. * SEGGER_SYSVIEW_SendModule()
  2555. *
  2556. * Function description
  2557. * Sends the information of a registered module to the host.
  2558. *
  2559. * Parameters
  2560. * ModuleId - Id of the requested module.
  2561. */
  2562. void SEGGER_SYSVIEW_SendModule(U8 ModuleId) {
  2563. SEGGER_SYSVIEW_MODULE* pModule;
  2564. U32 n;
  2565. if (_pFirstModule != 0) {
  2566. pModule = _pFirstModule;
  2567. for (n = 0; n < ModuleId; n++) {
  2568. pModule = pModule->pNext;
  2569. if (pModule == 0) {
  2570. break;
  2571. }
  2572. }
  2573. if (pModule != 0) {
  2574. U8* pPayload;
  2575. U8* pPayloadStart;
  2576. RECORD_START(SEGGER_SYSVIEW_INFO_SIZE + 2 * SEGGER_SYSVIEW_QUANTA_U32 + 1 + SEGGER_SYSVIEW_MAX_STRING_LEN);
  2577. //
  2578. pPayload = pPayloadStart;
  2579. //
  2580. // Send module description
  2581. // Send event offset and number of events
  2582. //
  2583. ENCODE_U32(pPayload, ModuleId);
  2584. ENCODE_U32(pPayload, (pModule->EventOffset));
  2585. pPayload = _EncodeStr(pPayload, pModule->sModule, SEGGER_SYSVIEW_MAX_STRING_LEN);
  2586. _SendPacket(pPayloadStart, pPayload, SYSVIEW_EVTID_MODULEDESC);
  2587. RECORD_END();
  2588. }
  2589. }
  2590. }
  2591. /*********************************************************************
  2592. *
  2593. * SEGGER_SYSVIEW_SendModuleDescription()
  2594. *
  2595. * Function description
  2596. * Triggers a send of the registered module descriptions.
  2597. *
  2598. */
  2599. void SEGGER_SYSVIEW_SendModuleDescription(void) {
  2600. SEGGER_SYSVIEW_MODULE* pModule;
  2601. if (_pFirstModule != 0) {
  2602. pModule = _pFirstModule;
  2603. do {
  2604. if (pModule->pfSendModuleDesc) {
  2605. pModule->pfSendModuleDesc();
  2606. }
  2607. pModule = pModule->pNext;
  2608. } while (pModule);
  2609. }
  2610. }
  2611. /*********************************************************************
  2612. *
  2613. * SEGGER_SYSVIEW_SendNumModules()
  2614. *
  2615. * Function description
  2616. * Send the number of registered modules to the host.
  2617. */
  2618. void SEGGER_SYSVIEW_SendNumModules(void) {
  2619. U8* pPayload;
  2620. U8* pPayloadStart;
  2621. RECORD_START(SEGGER_SYSVIEW_INFO_SIZE + 2*SEGGER_SYSVIEW_QUANTA_U32);
  2622. pPayload = pPayloadStart;
  2623. ENCODE_U32(pPayload, _NumModules);
  2624. _SendPacket(pPayloadStart, pPayload, SYSVIEW_EVTID_NUMMODULES);
  2625. RECORD_END();
  2626. }
  2627. #ifndef SEGGER_SYSVIEW_EXCLUDE_PRINTF // Define in project to avoid warnings about variable parameter list
  2628. /*********************************************************************
  2629. *
  2630. * SEGGER_SYSVIEW_PrintfHostEx()
  2631. *
  2632. * Function description
  2633. * Print a string which is formatted on the host by the SystemView Application
  2634. * with Additional information.
  2635. *
  2636. * Parameters
  2637. * s - String to be formatted.
  2638. * Options - Options for the string. i.e. Log level.
  2639. *
  2640. * Additional information
  2641. * All format arguments are treated as 32-bit scalar values.
  2642. */
  2643. void SEGGER_SYSVIEW_PrintfHostEx(const char* s, U32 Options, ...) {
  2644. va_list ParamList;
  2645. #if SEGGER_SYSVIEW_PRINTF_IMPLICIT_FORMAT
  2646. int r;
  2647. va_start(ParamList, Options);
  2648. r = _VPrintHost(s, Options, &ParamList);
  2649. va_end(ParamList);
  2650. if (r == -1) {
  2651. va_start(ParamList, Options);
  2652. _VPrintTarget(s, Options, &ParamList);
  2653. va_end(ParamList);
  2654. }
  2655. #else
  2656. va_start(ParamList, Options);
  2657. _VPrintHost(s, Options, &ParamList);
  2658. va_end(ParamList);
  2659. #endif
  2660. }
  2661. /*********************************************************************
  2662. *
  2663. * SEGGER_SYSVIEW_PrintfHost()
  2664. *
  2665. * Function description
  2666. * Print a string which is formatted on the host by the SystemView Application.
  2667. *
  2668. * Parameters
  2669. * s - String to be formatted.
  2670. *
  2671. * Additional information
  2672. * All format arguments are treated as 32-bit scalar values.
  2673. */
  2674. void SEGGER_SYSVIEW_PrintfHost(const char* s, ...) {
  2675. va_list ParamList;
  2676. #if SEGGER_SYSVIEW_PRINTF_IMPLICIT_FORMAT
  2677. int r;
  2678. va_start(ParamList, s);
  2679. r = _VPrintHost(s, SEGGER_SYSVIEW_LOG, &ParamList);
  2680. va_end(ParamList);
  2681. if (r == -1) {
  2682. va_start(ParamList, s);
  2683. _VPrintTarget(s, SEGGER_SYSVIEW_LOG, &ParamList);
  2684. va_end(ParamList);
  2685. }
  2686. #else
  2687. va_start(ParamList, s);
  2688. _VPrintHost(s, SEGGER_SYSVIEW_LOG, &ParamList);
  2689. va_end(ParamList);
  2690. #endif
  2691. }
  2692. /*********************************************************************
  2693. *
  2694. * SEGGER_SYSVIEW_WarnfHost()
  2695. *
  2696. * Function description
  2697. * Print a warning string which is formatted on the host by
  2698. * the SystemView Application.
  2699. *
  2700. * Parameters
  2701. * s - String to be formatted.
  2702. *
  2703. * Additional information
  2704. * All format arguments are treated as 32-bit scalar values.
  2705. */
  2706. void SEGGER_SYSVIEW_WarnfHost(const char* s, ...) {
  2707. va_list ParamList;
  2708. #if SEGGER_SYSVIEW_PRINTF_IMPLICIT_FORMAT
  2709. int r;
  2710. va_start(ParamList, s);
  2711. r = _VPrintHost(s, SEGGER_SYSVIEW_WARNING, &ParamList);
  2712. va_end(ParamList);
  2713. if (r == -1) {
  2714. va_start(ParamList, s);
  2715. _VPrintTarget(s, SEGGER_SYSVIEW_WARNING, &ParamList);
  2716. va_end(ParamList);
  2717. }
  2718. #else
  2719. va_start(ParamList, s);
  2720. _VPrintHost(s, SEGGER_SYSVIEW_WARNING, &ParamList);
  2721. va_end(ParamList);
  2722. #endif
  2723. }
  2724. /*********************************************************************
  2725. *
  2726. * SEGGER_SYSVIEW_ErrorfHost()
  2727. *
  2728. * Function description
  2729. * Print an error string which is formatted on the host by
  2730. * the SystemView Application.
  2731. *
  2732. * Parameters
  2733. * s - String to be formatted.
  2734. *
  2735. * Additional information
  2736. * All format arguments are treated as 32-bit scalar values.
  2737. */
  2738. void SEGGER_SYSVIEW_ErrorfHost(const char* s, ...) {
  2739. va_list ParamList;
  2740. #if SEGGER_SYSVIEW_PRINTF_IMPLICIT_FORMAT
  2741. int r;
  2742. va_start(ParamList, s);
  2743. r = _VPrintHost(s, SEGGER_SYSVIEW_ERROR, &ParamList);
  2744. va_end(ParamList);
  2745. if (r == -1) {
  2746. va_start(ParamList, s);
  2747. _VPrintTarget(s, SEGGER_SYSVIEW_ERROR, &ParamList);
  2748. va_end(ParamList);
  2749. }
  2750. #else
  2751. va_start(ParamList, s);
  2752. _VPrintHost(s, SEGGER_SYSVIEW_ERROR, &ParamList);
  2753. va_end(ParamList);
  2754. #endif
  2755. }
  2756. /*********************************************************************
  2757. *
  2758. * SEGGER_SYSVIEW_PrintfTargetEx()
  2759. *
  2760. * Function description
  2761. * Print a string which is formatted on the target before sent to
  2762. * the host with Additional information.
  2763. *
  2764. * Parameters
  2765. * s - String to be formatted.
  2766. * Options - Options for the string. i.e. Log level.
  2767. */
  2768. void SEGGER_SYSVIEW_PrintfTargetEx(const char* s, U32 Options, ...) {
  2769. va_list ParamList;
  2770. va_start(ParamList, Options);
  2771. _VPrintTarget(s, Options, &ParamList);
  2772. va_end(ParamList);
  2773. }
  2774. /*********************************************************************
  2775. *
  2776. * SEGGER_SYSVIEW_PrintfTarget()
  2777. *
  2778. * Function description
  2779. * Print a string which is formatted on the target before sent to
  2780. * the host.
  2781. *
  2782. * Parameters
  2783. * s - String to be formatted.
  2784. */
  2785. void SEGGER_SYSVIEW_PrintfTarget(const char* s, ...) {
  2786. va_list ParamList;
  2787. va_start(ParamList, s);
  2788. _VPrintTarget(s, SEGGER_SYSVIEW_LOG, &ParamList);
  2789. va_end(ParamList);
  2790. }
  2791. /*********************************************************************
  2792. *
  2793. * SEGGER_SYSVIEW_WarnfTarget()
  2794. *
  2795. * Function description
  2796. * Print a warning string which is formatted on the target before
  2797. * sent to the host.
  2798. *
  2799. * Parameters
  2800. * s - String to be formatted.
  2801. */
  2802. void SEGGER_SYSVIEW_WarnfTarget(const char* s, ...) {
  2803. va_list ParamList;
  2804. va_start(ParamList, s);
  2805. _VPrintTarget(s, SEGGER_SYSVIEW_WARNING, &ParamList);
  2806. va_end(ParamList);
  2807. }
  2808. /*********************************************************************
  2809. *
  2810. * SEGGER_SYSVIEW_ErrorfTarget()
  2811. *
  2812. * Function description
  2813. * Print an error string which is formatted on the target before
  2814. * sent to the host.
  2815. *
  2816. * Parameters
  2817. * s - String to be formatted.
  2818. */
  2819. void SEGGER_SYSVIEW_ErrorfTarget(const char* s, ...) {
  2820. va_list ParamList;
  2821. va_start(ParamList, s);
  2822. _VPrintTarget(s, SEGGER_SYSVIEW_ERROR, &ParamList);
  2823. va_end(ParamList);
  2824. }
  2825. #endif // SEGGER_SYSVIEW_EXCLUDE_PRINTF
  2826. /*********************************************************************
  2827. *
  2828. * SEGGER_SYSVIEW_Print()
  2829. *
  2830. * Function description
  2831. * Print a string to the host.
  2832. *
  2833. * Parameters
  2834. * s - String to sent.
  2835. */
  2836. void SEGGER_SYSVIEW_Print(const char* s) {
  2837. U8* pPayload;
  2838. U8* pPayloadStart;
  2839. RECORD_START(SEGGER_SYSVIEW_INFO_SIZE + 2 * SEGGER_SYSVIEW_QUANTA_U32 + SEGGER_SYSVIEW_MAX_STRING_LEN);
  2840. //
  2841. pPayload = _EncodeStr(pPayloadStart, s, SEGGER_SYSVIEW_MAX_STRING_LEN);
  2842. ENCODE_U32(pPayload, SEGGER_SYSVIEW_LOG);
  2843. ENCODE_U32(pPayload, 0);
  2844. _SendPacket(pPayloadStart, pPayload, SYSVIEW_EVTID_PRINT_FORMATTED);
  2845. RECORD_END();
  2846. }
  2847. /*********************************************************************
  2848. *
  2849. * SEGGER_SYSVIEW_Warn()
  2850. *
  2851. * Function description
  2852. * Print a warning string to the host.
  2853. *
  2854. * Parameters
  2855. * s - String to sent.
  2856. */
  2857. void SEGGER_SYSVIEW_Warn(const char* s) {
  2858. U8* pPayload;
  2859. U8* pPayloadStart;
  2860. RECORD_START(SEGGER_SYSVIEW_INFO_SIZE + 2 * SEGGER_SYSVIEW_QUANTA_U32 + SEGGER_SYSVIEW_MAX_STRING_LEN);
  2861. //
  2862. pPayload = _EncodeStr(pPayloadStart, s, SEGGER_SYSVIEW_MAX_STRING_LEN);
  2863. ENCODE_U32(pPayload, SEGGER_SYSVIEW_WARNING);
  2864. ENCODE_U32(pPayload, 0);
  2865. _SendPacket(pPayloadStart, pPayload, SYSVIEW_EVTID_PRINT_FORMATTED);
  2866. RECORD_END();
  2867. }
  2868. /*********************************************************************
  2869. *
  2870. * SEGGER_SYSVIEW_Error()
  2871. *
  2872. * Function description
  2873. * Print an error string to the host.
  2874. *
  2875. * Parameters
  2876. * s - String to sent.
  2877. */
  2878. void SEGGER_SYSVIEW_Error(const char* s) {
  2879. U8* pPayload;
  2880. U8* pPayloadStart;
  2881. RECORD_START(SEGGER_SYSVIEW_INFO_SIZE + 2 * SEGGER_SYSVIEW_QUANTA_U32 + SEGGER_SYSVIEW_MAX_STRING_LEN);
  2882. //
  2883. pPayload = _EncodeStr(pPayloadStart, s, SEGGER_SYSVIEW_MAX_STRING_LEN);
  2884. ENCODE_U32(pPayload, SEGGER_SYSVIEW_ERROR);
  2885. ENCODE_U32(pPayload, 0);
  2886. _SendPacket(pPayloadStart, pPayload, SYSVIEW_EVTID_PRINT_FORMATTED);
  2887. RECORD_END();
  2888. }
  2889. /*********************************************************************
  2890. *
  2891. * SEGGER_SYSVIEW_EnableEvents()
  2892. *
  2893. * Function description
  2894. * Enable standard SystemView events to be generated.
  2895. *
  2896. * Parameters
  2897. * EnableMask - Events to be enabled.
  2898. */
  2899. void SEGGER_SYSVIEW_EnableEvents(U32 EnableMask) {
  2900. _SYSVIEW_Globals.DisabledEvents &= ~EnableMask;
  2901. }
  2902. /*********************************************************************
  2903. *
  2904. * SEGGER_SYSVIEW_DisableEvents()
  2905. *
  2906. * Function description
  2907. * Disable standard SystemView events to not be generated.
  2908. *
  2909. * Parameters
  2910. * DisableMask - Events to be disabled.
  2911. */
  2912. void SEGGER_SYSVIEW_DisableEvents(U32 DisableMask) {
  2913. _SYSVIEW_Globals.DisabledEvents |= DisableMask;
  2914. }
  2915. /*********************************************************************
  2916. *
  2917. * SEGGER_SYSVIEW_IsStarted()
  2918. *
  2919. * Function description
  2920. * Handle incoming packets if any and check if recording is started.
  2921. *
  2922. * Return value
  2923. * 0: Recording not started.
  2924. * > 0: Recording started.
  2925. */
  2926. int SEGGER_SYSVIEW_IsStarted(void) {
  2927. #if (SEGGER_SYSVIEW_POST_MORTEM_MODE != 1)
  2928. //
  2929. // Check if host is sending data which needs to be processed.
  2930. //
  2931. if (SEGGER_RTT_HASDATA(CHANNEL_ID_DOWN)) {
  2932. if (_SYSVIEW_Globals.RecursionCnt == 0) { // Avoid uncontrolled nesting. This way, this routine can call itself once, but no more often than that.
  2933. _SYSVIEW_Globals.RecursionCnt = 1;
  2934. _HandleIncomingPacket();
  2935. _SYSVIEW_Globals.RecursionCnt = 0;
  2936. }
  2937. }
  2938. #endif
  2939. return _SYSVIEW_Globals.EnableState;
  2940. }
  2941. /*************************** End of file ****************************/