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