SEGGER_SYSVIEW.c 98 KB

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