1 /*******************************************************************************
\r
2 * Tracealyzer v2.4.1 Recorder Library
\r
3 * Percepio AB, www.percepio.com
\r
7 * The public API of the trace recorder library.
\r
10 * This software is copyright Percepio AB. The recorder library is free for
\r
11 * use together with Percepio products. You may distribute the recorder library
\r
12 * in its original form, including modifications in trcHardwarePort.c/.h
\r
13 * given that these modification are clearly marked as your own modifications
\r
14 * and documented in the initial comment section of these source files.
\r
15 * This software is the intellectual property of Percepio AB and may not be
\r
16 * sold or in other ways commercially redistributed without explicit written
\r
17 * permission by Percepio AB.
\r
20 * The trace tool and recorder library is being delivered to you AS IS and
\r
21 * Percepio AB makes no warranty as to its use or performance. Percepio AB does
\r
22 * not and cannot warrant the performance or results you may obtain by using the
\r
23 * software or documentation. Percepio AB make no warranties, express or
\r
24 * implied, as to noninfringement of third party rights, merchantability, or
\r
25 * fitness for any particular purpose. In no event will Percepio AB, its
\r
26 * technology partners, or distributors be liable to you for any consequential,
\r
27 * incidental or special damages, including any lost profits or lost savings,
\r
28 * even if a representative of Percepio AB has been advised of the possibility
\r
29 * of such damages, or for any claim by any third party. Some jurisdictions do
\r
30 * not allow the exclusion or limitation of incidental, consequential or special
\r
31 * damages, or the exclusion of implied warranties or limitations on how long an
\r
32 * implied warranty may last, so the above limitations may not apply to you.
\r
34 * Copyright Percepio AB, 2013.
\r
36 ******************************************************************************/
\r
38 #include "trcUser.h"
\r
40 #if (USE_TRACEALYZER_RECORDER == 1)
\r
46 TRACE_STOP_HOOK vTraceStopHookPtr = (TRACE_STOP_HOOK)0;
\r
48 extern uint8_t inExcludedTask;
\r
49 extern uint8_t nISRactive;
\r
50 extern uint8_t handle_of_last_logged_task;
\r
51 extern uint32_t dts_min;
\r
52 extern uint32_t hwtc_count_max_after_tick;
\r
53 extern uint32_t hwtc_count_sum_after_tick;
\r
54 extern uint32_t hwtc_count_sum_after_tick_counter;
\r
55 extern char* traceErrorMessage;
\r
57 /*** private functions *******************************************************/
\r
58 void vTracePrintF_Helper(traceLabel eventLabel, const char* formatStr, va_list vl);
\r
60 #if (USE_SEPARATE_USER_EVENT_BUFFER == 1)
\r
61 void vTraceChannelPrintF_Helper(UserEventChannel channelPair, va_list vl);
\r
62 static void prvTraceUserEventHelper1(UserEventChannel channel, traceLabel eventLabel, traceLabel formatLabel, va_list vl);
\r
63 static void prvTraceUserEventHelper2(UserEventChannel channel, uint32_t* data, uint32_t noOfSlots);
\r
65 /*****************************************************************************/
\r
69 /*******************************************************************************
\r
70 * vTraceInitTraceData
\r
72 * Allocates, if necessary, and initializes the recorder data structure, based
\r
73 * on the constants in trcConfig.h.
\r
74 ******************************************************************************/
\r
75 void vTraceInitTraceData(void)
\r
77 prvTraceInitTraceData();
\r
80 /*******************************************************************************
\r
81 * vTraceSetRecorderData
\r
83 * If custom allocation is used, this function must be called so the recorder
\r
84 * library knows where to save the trace data.
\r
85 ******************************************************************************/
\r
86 #if TRACE_DATA_ALLOCATION == TRACE_DATA_ALLOCATION_CUSTOM
\r
87 void vTraceSetRecorderData(void* pRecorderData)
\r
89 TRACE_ASSERT(pRecorderData != NULL, "vTraceSetTraceData, pRecorderData == NULL", );
\r
90 RecorderDataPtr = pRecorderData;
\r
94 /*******************************************************************************
\r
97 * Resets the recorder. Only necessary if a restart is desired - this is not
\r
98 * needed in the startup initialization.
\r
99 ******************************************************************************/
\r
100 void vTraceClear(void)
\r
102 trcCRITICAL_SECTION_BEGIN();
\r
104 RecorderDataPtr->absTimeLastEvent = 0;
\r
105 RecorderDataPtr->nextFreeIndex = 0;
\r
106 RecorderDataPtr->numEvents = 0;
\r
107 RecorderDataPtr->bufferIsFull = 0;
\r
109 trcCRITICAL_SECTION_END();
\r
113 /*******************************************************************************
\r
116 * Starts the recorder. The recorder will not be started if an error has been
\r
117 * indicated using vTraceError, e.g. if any of the Nx constants in trcConfig.h
\r
118 * has a too small value (NTASK, NQUEUE, etc).
\r
120 * Returns 1 if the recorder was started successfully.
\r
121 * Returns 0 if the recorder start was prevented due to a previous internal
\r
122 * error. In that case, check vTraceGetLastError to get the error message.
\r
123 * Any error message is also presented when opening a trace file.
\r
124 ******************************************************************************/
\r
126 uint32_t uiTraceStart(void)
\r
128 objectHandleType handle = 0;
\r
130 if (RecorderDataPtr == NULL)
\r
132 vTraceError("RecorderDataPtr is NULL. Call vTraceInitTraceData() before starting trace.");
\r
135 if (traceErrorMessage == NULL)
\r
137 trcCRITICAL_SECTION_BEGIN();
\r
138 RecorderDataPtr->recorderActive = 1;
\r
140 handle = TRACE_GET_TASK_NUMBER(TRACE_GET_CURRENT_TASK());
\r
143 /* This occurs if the scheduler is not yet started.
\r
144 This creates a dummy "(startup)" task entry internally in the
\r
146 handle = xTraceGetObjectHandle(TRACE_CLASS_TASK);
\r
147 vTraceSetObjectName(TRACE_CLASS_TASK, handle, "(startup)");
\r
149 vTraceSetPriorityProperty(TRACE_CLASS_TASK, handle, 0);
\r
152 vTraceStoreTaskswitch(handle); /* Register the currently running task */
\r
153 trcCRITICAL_SECTION_END();
\r
156 return RecorderDataPtr->recorderActive;
\r
159 /*******************************************************************************
\r
162 * Starts the recorder. The recorder will not be started if an error has been
\r
163 * indicated using vTraceError, e.g. if any of the Nx constants in trcConfig.h
\r
164 * has a too small value (NTASK, NQUEUE, etc).
\r
166 * This function is obsolete, but has been saved for backwards compatibility.
\r
167 * We recommend using uiTraceStart instead.
\r
168 ******************************************************************************/
\r
169 void vTraceStart(void)
\r
171 (void)uiTraceStart();
\r
174 /*******************************************************************************
\r
177 * Stops the recorder. The recording can be resumed by calling vTraceStart.
\r
178 * This does not reset the recorder. Use vTraceClear if that is desired.
\r
179 ******************************************************************************/
\r
180 void vTraceStop(void)
\r
182 RecorderDataPtr->recorderActive = 0;
\r
184 if (vTraceStopHookPtr != (TRACE_STOP_HOOK)0)
\r
186 (*vTraceStopHookPtr)(); /* Call an application level call back function. */
\r
190 /*******************************************************************************
\r
191 * xTraceGetLastError
\r
193 * Gives the last error message, if any. NULL if no error message is stored.
\r
194 * The message is cleared on read.
\r
195 * Any error message is also presented when opening a trace file.
\r
196 ******************************************************************************/
\r
197 char* xTraceGetLastError(void)
\r
199 return traceErrorMessage;
\r
202 /*******************************************************************************
\r
203 * vTraceGetTraceBuffer
\r
205 * Returns a pointer to the recorder data structure. Use this together with
\r
206 * uiTraceGetTraceBufferSize if you wish to implement an own store/upload
\r
207 * solution, e.g., in case a debugger connection is not available for uploading
\r
209 ******************************************************************************/
\r
210 void* vTraceGetTraceBuffer(void)
\r
212 return RecorderDataPtr;
\r
215 /*******************************************************************************
\r
216 * uiTraceGetTraceBufferSize
\r
218 * Gets the size of the recorder data structure. For use together with
\r
219 * vTraceGetTraceBuffer if you wish to implement an own store/upload solution,
\r
220 * e.g., in case a debugger connection is not available for uploading the data.
\r
221 ******************************************************************************/
\r
222 uint32_t uiTraceGetTraceBufferSize(void)
\r
224 return sizeof(RecorderDataType);
\r
227 /******************************************************************************
\r
228 * vTraceTaskInstanceIsFinished
\r
230 * This defines an explicit Instance Finish Event for the current task. It tells
\r
231 * the recorder that the current instance of this task is finished at the
\r
232 * context-switch. This function should be called right before the API function
\r
233 * call considered to be the Instance Finish Event.
\r
234 *****************************************************************************/
\r
235 void vTraceTaskInstanceIsFinished()
\r
237 if (handle_of_last_logged_task)
\r
239 TRACE_PROPERTY_OBJECT_STATE(TRACE_CLASS_TASK, handle_of_last_logged_task) = 0;
\r
243 /*******************************************************************************
\r
244 * Interrupt recording functions
\r
245 ******************************************************************************/
\r
247 #if (INCLUDE_ISR_TRACING == 1)
\r
249 #define MAX_ISR_NESTING 16
\r
250 static uint8_t isrstack[MAX_ISR_NESTING];
\r
252 /*******************************************************************************
\r
253 * vTraceSetISRProperties
\r
255 * Registers an Interrupt Service Routine in the recorder library, This must be
\r
256 * called before using vTraceStoreISRBegin to store ISR events. This is
\r
257 * typically called in the startup of the system, before the scheduler is
\r
261 * #define ID_ISR_TIMER1 1 // lowest valid ID is 1
\r
262 * #define PRIO_OF_ISR_TIMER1 3 // the hardware priority of the interrupt
\r
264 * vTraceSetISRProperties(ID_ISR_TIMER1, "ISRTimer1", PRIO_OF_ISR_TIMER1);
\r
266 * void ISR_handler()
\r
268 * vTraceStoreISRBegin(ID_OF_ISR_TIMER1);
\r
270 * vTraceStoreISREnd();
\r
273 * NOTE: To safely record ISRs, you need to make sure that all traced
\r
274 * interrupts actually are disabled by trcCRITICAL_SECTION_BEGIN(). However,
\r
275 * in some ports this does not disable high priority interrupts!
\r
276 * If an ISR calls vTraceStoreISRBegin while the recorder is busy, it will
\r
277 * stop the recording and give an error message.
\r
278 ******************************************************************************/
\r
279 void vTraceSetISRProperties(objectHandleType handle, const char* name, char priority)
\r
281 TRACE_ASSERT(handle <= RecorderDataPtr->ObjectPropertyTable.NumberOfObjectsPerClass[TRACE_CLASS_ISR], "vTraceSetISRProperties: Invalid value for handle", );
\r
282 TRACE_ASSERT(name != NULL, "vTraceSetISRProperties: name == NULL", );
\r
283 TRACE_ASSERT(priority >= 0, "vTraceSetISRProperties: Invalid value for priority", );
\r
285 vTraceSetObjectName(TRACE_CLASS_ISR, handle, name);
\r
286 vTraceSetPriorityProperty(TRACE_CLASS_ISR, handle, priority);
\r
289 /*******************************************************************************
\r
290 * vTraceStoreISRBegin
\r
292 * Registers the beginning of an Interrupt Service Routine.
\r
295 * #define ID_ISR_TIMER1 1 // lowest valid ID is 1
\r
296 * #define PRIO_OF_ISR_TIMER1 3 // the hardware priority of the interrupt
\r
298 * vTraceSetISRProperties(ID_ISR_TIMER1, "ISRTimer1", PRIO_OF_ISR_TIMER1);
\r
300 * void ISR_handler()
\r
302 * vTraceStoreISRBegin(ID_OF_ISR_TIMER1);
\r
304 * vTraceStoreISREnd();
\r
307 * NOTE: You need to make sure that any traced interrupts actually are
\r
308 * disabled by trcCRITICAL_SECTION_BEGIN().
\r
309 * If an invalid call to vTraceStoreISRBegin is detected (i.e., that preempted
\r
310 * a critical section of the recorder) this will generate a recorder error
\r
311 * using vTraceError.
\r
312 ******************************************************************************/
\r
313 void vTraceStoreISRBegin(objectHandleType handle)
\r
316 TSEvent* ts = NULL;
\r
318 TRACE_ASSERT(handle <= RecorderDataPtr->ObjectPropertyTable.NumberOfObjectsPerClass[TRACE_CLASS_ISR], "vTraceStoreISRBegin: Invalid value for handle", );
\r
322 vTraceError("Illegal call to vTraceStoreISRBegin, recorder busy!");
\r
325 if (RecorderDataPtr->recorderActive && handle_of_last_logged_task)
\r
327 trcCRITICAL_SECTION_BEGIN();
\r
328 dts4 = (uint16_t)prvTraceGetDTS(0xFFFF);
\r
330 if (RecorderDataPtr->recorderActive) /* Need to repeat this check! */
\r
333 if (nISRactive < MAX_ISR_NESTING)
\r
335 isrstack[nISRactive] = handle;
\r
337 ts = (TSEvent*)xTraceNextFreeEventBufferSlot();
\r
340 ts->type = TS_ISR_BEGIN;
\r
342 ts->objHandle = handle;
\r
343 prvTraceUpdateCounters();
\r
348 /* This should not occur unless something is very wrong */
\r
349 vTraceError("Too many nested interrupts!");
\r
352 trcCRITICAL_SECTION_END();
\r
357 #if (SELECTED_PORT == PORT_ARM_CortexM)
\r
359 static int tailchain_irq_pending(void);
\r
361 /*******************************************************************************
\r
362 * tailchain_irq_pending
\r
364 * For Cortex-M chips only. Returns 1 if an interrupt is pending, by checking
\r
365 * the 8 NVIC IRQ pend registers at 0xE000E200 to 0xE000E21C. Returns 0 if no
\r
366 * interrupt is pending. This is used to predict tailchaining of ISRs.
\r
367 ******************************************************************************/
\r
368 static int tailchain_irq_pending(void)
\r
370 uint32_t* pend_reg = ((uint32_t*)0xE000E200);
\r
373 for (i=0; i<8; i++)
\r
375 if (pend_reg[i] != 0)
\r
385 /*******************************************************************************
\r
386 * vTraceStoreISREnd
\r
388 * Registers the end of an Interrupt Service Routine.
\r
391 * #define ID_ISR_TIMER1 1 // lowest valid ID is 1
\r
392 * #define PRIO_OF_ISR_TIMER1 3 // the hardware priority of the interrupt
\r
394 * vTraceSetISRProperties(ID_ISR_TIMER1, "ISRTimer1", PRIO_OF_ISR_TIMER1);
\r
396 * void ISR_handler()
\r
398 * vTraceStoreISRBegin(ID_OF_ISR_TIMER1);
\r
400 * vTraceStoreISREnd();
\r
403 * NOTE: You need to make sure that any traced interrupts actually are
\r
404 * disabled by trcCRITICAL_SECTION_BEGIN().
\r
405 * If an invalid call to vTraceStoreISREnd is detected (i.e., that preempted
\r
406 * a critical section of the recorder) this will generate a recorder error
\r
407 * using vTraceError.
\r
408 ******************************************************************************/
\r
409 void vTraceStoreISREnd(void)
\r
416 vTraceError("Illegal call to vTraceStoreISREnd, recorder busy!");
\r
420 if (RecorderDataPtr->recorderActive && handle_of_last_logged_task)
\r
422 #if (SELECTED_PORT == PORT_ARM_CortexM)
\r
423 if (tailchain_irq_pending() > 0)
\r
425 nISRactive--; /* If an IRQ strikes exactly here, the resulting
\r
426 ISR tailchaining is not detected. The trace instead shows a very
\r
427 short fragment of the earlier preempted task/ISR, and then the new
\r
433 trcCRITICAL_SECTION_BEGIN();
\r
434 dts5 = (uint16_t)prvTraceGetDTS(0xFFFF);
\r
436 if (RecorderDataPtr->recorderActive) /* Need to repeat this check! */
\r
438 ts = (TSEvent*)xTraceNextFreeEventBufferSlot();
\r
441 if (nISRactive > 1)
\r
443 /* return to another isr */
\r
444 ts->type = TS_ISR_RESUME;
\r
445 ts->objHandle = isrstack[nISRactive];
\r
449 /* return to task */
\r
450 ts->type = TS_TASK_RESUME;
\r
451 ts->objHandle = handle_of_last_logged_task;
\r
455 prvTraceUpdateCounters();
\r
458 trcCRITICAL_SECTION_END();
\r
464 /* ISR tracing is turned off */
\r
465 void vTraceIncreaseISRActive(void)
\r
467 if (RecorderDataPtr->recorderActive && handle_of_last_logged_task)
\r
471 void vTraceDecreaseISRActive(void)
\r
473 if (RecorderDataPtr->recorderActive && handle_of_last_logged_task)
\r
479 /*******************************************************************************
\r
480 * User Event functions
\r
481 ******************************************************************************/
\r
483 #if (INCLUDE_USER_EVENTS == 1)
\r
485 #define MAX_ARG_SIZE (4+32)
\r
486 /*** Locally used in vTracePrintF ***/
\r
487 static uint8_t writeInt8(void * buffer, uint8_t i, uint8_t value)
\r
489 TRACE_ASSERT(buffer != NULL, "writeInt8: buffer == NULL", 0);
\r
491 if (i >= MAX_ARG_SIZE)
\r
496 ((uint8_t*)buffer)[i] = value;
\r
498 if (i + 1 > MAX_ARG_SIZE)
\r
506 /*** Locally used in vTracePrintF ***/
\r
507 static uint8_t writeInt16(void * buffer, uint8_t i, uint16_t value)
\r
509 TRACE_ASSERT(buffer != NULL, "writeInt16: buffer == NULL", 0);
\r
511 /* Align to multiple of 2 */
\r
512 while ((i % 2) != 0)
\r
514 if (i >= MAX_ARG_SIZE)
\r
519 ((uint8_t*)buffer)[i] = 0;
\r
523 if (i + 2 > MAX_ARG_SIZE)
\r
528 ((uint16_t*)buffer)[i/2] = value;
\r
533 /*** Locally used in vTracePrintF ***/
\r
534 static uint8_t writeInt32(void * buffer, uint8_t i, uint32_t value)
\r
536 TRACE_ASSERT(buffer != NULL, "writeInt32: buffer == NULL", 0);
\r
538 /* A 32 bit value should begin at an even 4-byte address */
\r
539 while ((i % 4) != 0)
\r
541 if (i >= MAX_ARG_SIZE)
\r
546 ((uint8_t*)buffer)[i] = 0;
\r
550 if (i + 4 > MAX_ARG_SIZE)
\r
555 ((uint32_t*)buffer)[i/4] = value;
\r
560 #if (INCLUDE_FLOAT_SUPPORT)
\r
562 /*** Locally used in vTracePrintF ***/
\r
563 static uint8_t writeFloat(void * buffer, uint8_t i, float value)
\r
565 TRACE_ASSERT(buffer != NULL, "writeFloat: buffer == NULL", 0);
\r
567 /* A 32 bit value should begin at an even 4-byte address */
\r
568 while ((i % 4) != 0)
\r
570 if (i >= MAX_ARG_SIZE)
\r
575 ((uint8_t*)buffer)[i] = 0;
\r
579 if (i + 4 > MAX_ARG_SIZE)
\r
584 ((float*)buffer)[i/4] = value;
\r
589 /*** Locally used in vTracePrintF ***/
\r
590 static uint8_t writeDouble(void * buffer, uint8_t i, double value)
\r
592 TRACE_ASSERT(buffer != NULL, "writeDouble: buffer == NULL", 0);
\r
594 uint32_t * dest = buffer;
\r
595 uint32_t * src = (void*)&value;
\r
596 /* The double is written as two 32 bit values, and should begin at an even
\r
597 4-byte address (to avoid having to align with 8 byte) */
\r
600 if (i >= MAX_ARG_SIZE)
\r
605 ((uint8_t*)buffer)[i] = 0;
\r
609 if (i + 8 > MAX_ARG_SIZE)
\r
614 dest[i/4+0] = src[0];
\r
615 dest[i/4+1] = src[1];
\r
622 /*******************************************************************************
\r
623 * prvTraceUserEventFormat
\r
625 * Parses the format string and stores the arguments in the buffer.
\r
626 ******************************************************************************/
\r
627 static uint8_t prvTraceUserEventFormat(const char* formatStr, va_list vl, uint8_t* buffer, uint8_t byteOffset)
\r
629 uint16_t formatStrIndex = 0;
\r
630 uint8_t argCounter = 0;
\r
631 uint8_t i = byteOffset;
\r
633 while (formatStr[formatStrIndex] != '\0')
\r
635 if (formatStr[formatStrIndex] == '%')
\r
639 if (argCounter > 15)
\r
641 vTraceError("vTracePrintF - Too many arguments, max 15 allowed!");
\r
645 /*******************************************************************************
\r
646 * These below code writes raw data (primitive datatypes) in the event buffer,
\r
647 * instead of the normal event structs (where byte 0 is event type).
\r
648 * These data entries must never be interpreted as real event data, as the type
\r
649 * field would be misleading since used for payload data.
\r
651 * The correctness of this encoding depends on two mechanisms:
\r
653 * 1. An initial USER_EVENT, which type code tells the number of 32-bit data
\r
654 * entires that follows. (code - USER_EVENT = number of data entries).
\r
655 * Note that a data entry corresponds to the slots that normally corresponds to
\r
656 * one (1) event, i.e., 32 bits. vTracePrintF may encode several pieces of data
\r
657 * in one data entry, e.g., two 16-bit values or four 8-bit values, one 16-bit
\r
658 * value followed by two 8-bit values, etc.
\r
660 * 2. A two-phase commit procedure, where the USER_EVENT and data entries are
\r
661 * written to a local buffer at first, and when all checks are OK then copied to
\r
662 * the main event buffer using a fast memcpy. The event code is finalized as the
\r
663 * very last step. Before that step, the event code indicates an unfinished
\r
664 * event, which causes it to be ignored and stop the loading of the file (since
\r
665 * an unfinished event is the last event in the trace).
\r
666 *******************************************************************************/
\r
669 while ((formatStr[formatStrIndex] >= '0' && formatStr[formatStrIndex] <= '9') || formatStr[formatStrIndex] == '#' || formatStr[formatStrIndex] == '.')
\r
672 if (formatStr[formatStrIndex] != '\0')
\r
674 switch (formatStr[formatStrIndex])
\r
676 case 'd': i = writeInt32(buffer,
\r
678 (uint32_t)va_arg(vl, uint32_t));
\r
682 case 'u': i = writeInt32(buffer,
\r
684 (uint32_t)va_arg(vl, uint32_t));
\r
686 case 's': i = writeInt16(buffer,
\r
688 (uint16_t)xTraceOpenLabel((char*)va_arg(vl, char*)));
\r
691 #if (INCLUDE_FLOAT_SUPPORT)
\r
692 /* Yes, "double" as type also in the float
\r
693 case. This since "float" is promoted into "double"
\r
694 by the va_arg stuff. */
\r
695 case 'f': i = writeFloat(buffer,
\r
697 (float)va_arg(vl, double));
\r
700 /* No support for floats, but attempt to store a float user event
\r
701 avoid a possible crash due to float reference. Instead store the
\r
702 data on uint_32 format (will not be displayed anyway). This is just
\r
703 to keep va_arg and i consistent. */
\r
705 case 'f': i = writeInt32(buffer,
\r
707 (uint32_t)va_arg(vl, double));
\r
712 switch (formatStr[formatStrIndex])
\r
714 #if (INCLUDE_FLOAT_SUPPORT)
\r
715 case 'f': i = writeDouble(buffer,
\r
717 (double)va_arg(vl, double));
\r
720 /* No support for floats, but attempt to store a float user event
\r
721 avoid a possible crash due to float reference. Instead store the
\r
722 data on uint_32 format (will not be displayed anyway). This is just
\r
723 to keep va_arg and i consistent. */
\r
724 case 'f': i = writeInt32(buffer, /* In this case, the value will not be shown anyway */
\r
726 (uint32_t)va_arg(vl, double));
\r
727 i = writeInt32(buffer, /* Do it twice, to write in total 8 bytes */
\r
729 (uint32_t)va_arg(vl, double));
\r
737 switch (formatStr[formatStrIndex])
\r
739 case 'd': i = writeInt16(buffer,
\r
741 (uint16_t)va_arg(vl, uint32_t));
\r
743 case 'u': i = writeInt16(buffer,
\r
745 (uint16_t)va_arg(vl, uint32_t));
\r
751 switch (formatStr[formatStrIndex])
\r
753 case 'd': i = writeInt8(buffer,
\r
755 (uint8_t)va_arg(vl, uint32_t));
\r
757 case 'u': i = writeInt8(buffer,
\r
759 (uint8_t)va_arg(vl, uint32_t));
\r
771 vTraceError("vTracePrintF - Too large arguments, max 32 byte allowed!");
\r
778 #if (USE_SEPARATE_USER_EVENT_BUFFER == 1)
\r
780 /*******************************************************************************
\r
781 * prvTraceClearChannelBuffer
\r
783 * Clears a number of items in the channel buffer, starting from nextSlotToWrite.
\r
784 ******************************************************************************/
\r
785 static void prvTraceClearChannelBuffer(uint32_t count)
\r
789 TRACE_ASSERT(USER_EVENT_BUFFER_SIZE >= count, "prvTraceClearChannelBuffer: USER_EVENT_BUFFER_SIZE is too small to handle this event.", );
\r
791 /* Check if we're close to the end of the buffer */
\r
792 if (RecorderDataPtr->userEventBuffer.nextSlotToWrite + count > USER_EVENT_BUFFER_SIZE)
\r
794 slots = USER_EVENT_BUFFER_SIZE - RecorderDataPtr->userEventBuffer.nextSlotToWrite; /* Number of slots before end of buffer */
\r
795 (void)memset(&RecorderDataPtr->userEventBuffer.channelBuffer[RecorderDataPtr->userEventBuffer.nextSlotToWrite], 0, slots);
\r
796 (void)memset(&RecorderDataPtr->userEventBuffer.channelBuffer[0], 0, (count - slots));
\r
799 (void)memset(&RecorderDataPtr->userEventBuffer.channelBuffer[RecorderDataPtr->userEventBuffer.nextSlotToWrite], 0, count);
\r
802 /*******************************************************************************
\r
803 * prvTraceCopyToDataBuffer
\r
805 * Copies a number of items to the data buffer, starting from nextSlotToWrite.
\r
806 ******************************************************************************/
\r
807 static void prvTraceCopyToDataBuffer(uint32_t* data, uint32_t count)
\r
809 TRACE_ASSERT(data != NULL, "prvTraceCopyToDataBuffer: data == NULL.", );
\r
810 TRACE_ASSERT(count <= USER_EVENT_BUFFER_SIZE, "prvTraceCopyToDataBuffer: USER_EVENT_BUFFER_SIZE is too small to handle this event.", );
\r
813 /* Check if we're close to the end of the buffer */
\r
814 if (RecorderDataPtr->userEventBuffer.nextSlotToWrite + count > USER_EVENT_BUFFER_SIZE)
\r
816 slots = USER_EVENT_BUFFER_SIZE - RecorderDataPtr->userEventBuffer.nextSlotToWrite; /* Number of slots before end of buffer */
\r
817 (void)memcpy(&RecorderDataPtr->userEventBuffer.dataBuffer[RecorderDataPtr->userEventBuffer.nextSlotToWrite * 4], data, slots * 4);
\r
818 (void)memcpy(&RecorderDataPtr->userEventBuffer.dataBuffer[0], data + slots, (count - slots) * 4);
\r
822 (void)memcpy(&RecorderDataPtr->userEventBuffer.dataBuffer[RecorderDataPtr->userEventBuffer.nextSlotToWrite * 4], data, count * 4);
\r
826 /*******************************************************************************
\r
827 * prvTraceUserEventHelper1
\r
829 * Calls on prvTraceUserEventFormat() to do the actual formatting, then goes on to the next helper function.
\r
830 ******************************************************************************/
\r
831 static void prvTraceUserEventHelper1(UserEventChannel channel, traceLabel eventLabel, traceLabel formatLabel, va_list vl)
\r
833 uint32_t data[(3 + MAX_ARG_SIZE) / 4];
\r
834 uint8_t byteOffset = 4; /* Need room for timestamp */
\r
839 /* We are dealing with an unknown channel format pair */
\r
840 byteOffset += 4; /* Also need room for channel and format */
\r
841 ((uint16_t*)data)[2] = eventLabel;
\r
842 ((uint16_t*)data)[3] = formatLabel;
\r
845 noOfSlots = prvTraceUserEventFormat((char*)&(RecorderDataPtr->SymbolTable.symbytes[formatLabel+4]), vl, (uint8_t*)data, byteOffset);
\r
847 prvTraceUserEventHelper2(channel, data, noOfSlots);
\r
850 /*******************************************************************************
\r
851 * prvTraceUserEventHelper2
\r
853 * This function simply copies the data buffer to the actual user event buffer.
\r
854 ******************************************************************************/
\r
855 static void prvTraceUserEventHelper2(UserEventChannel channel, uint32_t* data, uint32_t noOfSlots)
\r
857 static uint32_t old_timestamp = 0;
\r
858 uint32_t old_nextSlotToWrite = 0;
\r
860 TRACE_ASSERT(USER_EVENT_BUFFER_SIZE >= noOfSlots, "vTracePrintF: USER_EVENT_BUFFER_SIZE is too small to handle this event.", );
\r
862 trcCRITICAL_SECTION_BEGIN();
\r
863 /* Store the timestamp */
\r
864 vTracePortGetTimeStamp(data);
\r
866 if (*data < old_timestamp)
\r
867 RecorderDataPtr->userEventBuffer.wraparoundCounter++;
\r
868 old_timestamp = *data;
\r
870 /* Start by erasing any information in the channel buffer */
\r
871 prvTraceClearChannelBuffer(noOfSlots);
\r
873 prvTraceCopyToDataBuffer(data, noOfSlots); /* Will wrap around the data if necessary */
\r
875 old_nextSlotToWrite = RecorderDataPtr->userEventBuffer.nextSlotToWrite; /* Save the index that we want to write the channel data at when we're done */
\r
876 RecorderDataPtr->userEventBuffer.nextSlotToWrite = (RecorderDataPtr->userEventBuffer.nextSlotToWrite + noOfSlots) % USER_EVENT_BUFFER_SIZE; /* Make sure we never end up outside the buffer */
\r
878 /* Write to the channel buffer to indicate that this user event is ready to be used */
\r
880 RecorderDataPtr->userEventBuffer.channelBuffer[old_nextSlotToWrite] = channel;
\r
882 RecorderDataPtr->userEventBuffer.channelBuffer[old_nextSlotToWrite] = (UserEventChannel)0xFF; /* 0xFF indicates that this is not a normal channel id */
\r
883 trcCRITICAL_SECTION_END();
\r
886 /*******************************************************************************
\r
887 * xTraceRegisterChannelFormat
\r
889 * Attempts to create a pair of the channel and format string.
\r
891 * Note: This is only available if USE_SEPARATE_USER_EVENT_BUFFER is enabled in
\r
893 ******************************************************************************/
\r
894 UserEventChannel xTraceRegisterChannelFormat(traceLabel channel, traceLabel formatStr)
\r
897 UserEventChannel retVal = 0;
\r
899 TRACE_ASSERT(formatStr != 0, "vTraceRegisterChannelFormat: formatStr == 0", (UserEventChannel)0);
\r
901 trcCRITICAL_SECTION_BEGIN();
\r
902 for (i = 1; i <= CHANNEL_FORMAT_PAIRS; i++) /* Size of the channels buffer is CHANNEL_FORMAT_PAIRS + 1. Index 0 is unused. */
\r
904 if(RecorderDataPtr->userEventBuffer.channels[i].name == 0 && RecorderDataPtr->userEventBuffer.channels[i].defaultFormat == 0)
\r
906 /* Found empty slot */
\r
907 RecorderDataPtr->userEventBuffer.channels[i].name = channel;
\r
908 RecorderDataPtr->userEventBuffer.channels[i].defaultFormat = formatStr;
\r
909 retVal = (UserEventChannel)i;
\r
913 if (RecorderDataPtr->userEventBuffer.channels[i].name == channel && RecorderDataPtr->userEventBuffer.channels[i].defaultFormat == formatStr)
\r
915 /* Found a match */
\r
916 retVal = (UserEventChannel)i;
\r
920 trcCRITICAL_SECTION_END();
\r
924 /******************************************************************************
\r
925 * vTraceChannelPrintF
\r
927 * Slightly faster version of vTracePrintF() due to no lookups.
\r
929 * Note: This is only available if USE_SEPARATE_USER_EVENT_BUFFER is enabled in
\r
932 ******************************************************************************/
\r
933 void vTraceChannelPrintF(UserEventChannel channelPair, ...)
\r
937 va_start(vl, channelPair);
\r
938 vTraceChannelPrintF_Helper(channelPair, vl);
\r
942 void vTraceChannelPrintF_Helper(UserEventChannel channelPair, va_list vl)
\r
944 traceLabel channel;
\r
945 traceLabel formatStr;
\r
947 TRACE_ASSERT(channelPair != 0, "vTraceChannelPrintF: channelPair == 0", );
\r
948 TRACE_ASSERT(channelPair <= CHANNEL_FORMAT_PAIRS, "vTraceChannelPrintF: ", );
\r
950 channel = RecorderDataPtr->userEventBuffer.channels[channelPair].name;
\r
951 formatStr = RecorderDataPtr->userEventBuffer.channels[channelPair].defaultFormat;
\r
953 prvTraceUserEventHelper1(channelPair, channel, formatStr, vl);
\r
956 /******************************************************************************
\r
957 * vTraceChannelUserEvent
\r
959 * Slightly faster version of vTraceUserEvent() due to no lookups.
\r
960 ******************************************************************************/
\r
961 void vTraceChannelUserEvent(UserEventChannel channelPair)
\r
963 uint32_t data[(3 + MAX_ARG_SIZE) / 4];
\r
965 TRACE_ASSERT(channelPair != 0, "vTraceChannelPrintF: channelPair == 0", );
\r
966 TRACE_ASSERT(channelPair <= CHANNEL_FORMAT_PAIRS, "vTraceChannelPrintF: ", );
\r
968 prvTraceUserEventHelper2(channelPair, data, 1); /* Only need one slot for timestamp */
\r
970 #endif /* USE_SEPARATE_USER_EVENT_BUFFER == 1 */
\r
972 /******************************************************************************
\r
975 * Advanced user events (Professional Edition only)
\r
977 * Generates User Event with formatted text and data, similar to a "printf".
\r
978 * It is very fast compared to a normal "printf" since this function only
\r
979 * stores the arguments. The actual formatting is done
\r
980 * on the host PC when the trace is displayed in the viewer tool.
\r
982 * User Event labels are created using xTraceOpenLabel.
\r
985 * traceLabel adc_uechannel = xTraceOpenLabel("ADC User Events");
\r
987 * vTracePrint(adc_uechannel,
\r
988 * "ADC channel %d: %lf volts",
\r
989 * ch, (double)adc_reading/(double)scale);
\r
991 * This can be combined into one line, if desired, but this is slower:
\r
993 * vTracePrint(xTraceOpenLabel("ADC User Events"),
\r
994 * "ADC channel %d: %lf volts",
\r
995 * ch, (double)adc_reading/(double)scale);
\r
997 * Calling xTraceOpenLabel multiple times will not create duplicate entries, but
\r
998 * it is of course faster to just do it once, and then keep the handle for later
\r
999 * use. If you don´t have any data arguments, only a text label/string, it is
\r
1000 * better to use vTraceUserEvent - it is faster.
\r
1002 * Format specifiers supported:
\r
1003 * %d - 32 bit signed integer
\r
1004 * %u - 32 bit unsigned integer
\r
1005 * %f - 32 bit float
\r
1006 * %s - string (is copied to the recorder symbol table)
\r
1007 * %hd - 16 bit signed integer
\r
1008 * %hu - 16 bit unsigned integer
\r
1009 * %bd - 8 bit signed integer
\r
1010 * %bu - 8 bit unsigned integer
\r
1011 * %lf - double-precision float
\r
1013 * Up to 15 data arguments are allowed, with a total size of maximum 32 byte.
\r
1014 * In case this is exceeded, the user event is changed into an error message.
\r
1016 * The data is stored in trace buffer, and is packed to allow storing multiple
\r
1017 * smaller data entries in the same 4-byte record, e.g., four 8-bit values.
\r
1018 * A string requires two bytes, as the symbol table is limited to 64K. Storing a
\r
1019 * double (%lf) uses two records, so this is quite costly. Use float (%f) unless
\r
1020 * the higher precision is really necessary.
\r
1021 ******************************************************************************/
\r
1023 void vTracePrintF(traceLabel eventLabel, const char* formatStr, ...)
\r
1027 va_start(vl, formatStr);
\r
1028 vTracePrintF_Helper(eventLabel, formatStr, vl);
\r
1032 void vTracePrintF_Helper(traceLabel eventLabel, const char* formatStr, va_list vl)
\r
1034 #if (USE_SEPARATE_USER_EVENT_BUFFER == 0)
\r
1035 uint32_t noOfSlots;
\r
1037 uint32_t tempDataBuffer[(3 + MAX_ARG_SIZE) / 4];
\r
1039 /**************************************************************************
\r
1040 * The array tempDataBuffer is a local buffer used in a two-phase commit of
\r
1041 * the event data, since a vTracePrintF may span over multiple slots in the
\r
1043 * This buffer can be made larger, of course, but remember the risk for
\r
1044 * stack overflow. Note: This should be a LOCAL buffer, must not be made
\r
1045 * global. That would cause data corruption when two calls to vTracePrintF
\r
1046 * from different tasks overlaps (interrupts are only disabled in a small
\r
1047 * part of this function, otherwise enabled)
\r
1048 ***************************************************************************/
\r
1050 TRACE_ASSERT(formatStr != NULL, "vTracePrintF: formatStr == NULL", );
\r
1052 if (RecorderDataPtr->recorderActive && (! inExcludedTask || nISRactive) && handle_of_last_logged_task)
\r
1054 /* First, write the "primary" user event entry in the local buffer, but
\r
1055 let the event type be "EVENT_BEING_WRITTEN" for now...*/
\r
1057 ue1 = (UserEvent*)(&tempDataBuffer[0]);
\r
1058 ue1->type = EVENT_BEING_WRITTEN; /* Update this as the last step */
\r
1060 noOfSlots = prvTraceUserEventFormat(formatStr, vl, (uint8_t*)tempDataBuffer, 4);
\r
1062 /* Store the format string, with a reference to the channel symbol */
\r
1063 ue1->payload = prvTraceOpenSymbol(formatStr, eventLabel);
\r
1065 trcCRITICAL_SECTION_BEGIN();
\r
1067 ue1->dts = (uint8_t)prvTraceGetDTS(0xFF);
\r
1068 if (! RecorderDataPtr->recorderActive)
\r
1071 /* Abort, since an XTS event (created by prvTraceGetDTS) filled the
\r
1072 buffer, and the recorder stopped since not circular buffer. */
\r
1073 trcCRITICAL_SECTION_END();
\r
1078 /* If the data does not fit in the remaining main buffer, wrap around to
\r
1079 0 if allowed, otherwise stop the recorder and quit). */
\r
1080 if (RecorderDataPtr->nextFreeIndex + noOfSlots > RecorderDataPtr->maxEvents)
\r
1082 #if (TRACE_RECORDER_STORE_MODE == TRACE_STORE_MODE_RING_BUFFER)
\r
1083 (void)memset(& RecorderDataPtr->eventData[RecorderDataPtr->nextFreeIndex * 4],
\r
1085 (RecorderDataPtr->maxEvents - RecorderDataPtr->nextFreeIndex)*4);
\r
1086 RecorderDataPtr->nextFreeIndex = 0;
\r
1087 RecorderDataPtr->bufferIsFull = 1;
\r
1089 /* Abort and stop recorder, since the event data will not fit in the
\r
1090 buffer and not circular buffer in this case... */
\r
1091 trcCRITICAL_SECTION_END();
\r
1099 #if (TRACE_RECORDER_STORE_MODE == TRACE_STORE_MODE_RING_BUFFER)
\r
1100 /* Check that the buffer to be overwritten does not contain any user
\r
1101 events that would be partially overwritten. If so, they must be "killed"
\r
1102 by replacing the user event and following data with NULL events (i.e.,
\r
1103 using a memset to zero).*/
\r
1104 prvCheckDataToBeOverwrittenForMultiEntryEvents((uint8_t)noOfSlots);
\r
1106 /* Copy the local buffer to the main buffer */
\r
1107 (void)memcpy(& RecorderDataPtr->eventData[RecorderDataPtr->nextFreeIndex * 4],
\r
1111 /* Update the event type, i.e., number of data entries following the
\r
1112 main USER_EVENT entry (Note: important that this is after the memcpy,
\r
1113 but within the critical section!)*/
\r
1114 RecorderDataPtr->eventData[RecorderDataPtr->nextFreeIndex * 4] =
\r
1115 (uint8_t) ( USER_EVENT + noOfSlots - 1 );
\r
1117 /* Update the main buffer event index (already checked that it fits in
\r
1118 the buffer, so no need to check for wrapping)*/
\r
1120 RecorderDataPtr->nextFreeIndex += noOfSlots;
\r
1121 RecorderDataPtr->numEvents += noOfSlots;
\r
1125 if (RecorderDataPtr->nextFreeIndex >= EVENT_BUFFER_SIZE)
\r
1127 #if (TRACE_RECORDER_STORE_MODE == TRACE_STORE_MODE_RING_BUFFER)
\r
1128 /* We have reached the end, but this is a ring buffer. Start from the beginning again. */
\r
1129 RecorderDataPtr->bufferIsFull = 1;
\r
1130 RecorderDataPtr->nextFreeIndex = 0;
\r
1132 /* We have reached the end so we stop. */
\r
1137 #if (TRACE_RECORDER_STORE_MODE == TRACE_STORE_MODE_RING_BUFFER)
\r
1138 /* Make sure the next entry is cleared correctly */
\r
1139 prvCheckDataToBeOverwrittenForMultiEntryEvents(1);
\r
1142 #ifdef STOP_AFTER_N_EVENTS
\r
1143 #if (STOP_AFTER_N_EVENTS > -1)
\r
1144 /* Check if we have reached the desired number of events */
\r
1145 if (RecorderDataPtr->numEvents >= STOP_AFTER_N_EVENTS)
\r
1152 trcCRITICAL_SECTION_END();
\r
1155 #elif (USE_SEPARATE_USER_EVENT_BUFFER == 1)
\r
1156 /* Use the separate user event buffer */
\r
1157 traceLabel formatLabel;
\r
1158 UserEventChannel channel;
\r
1160 if (RecorderDataPtr->recorderActive && (! inExcludedTask || nISRactive) && handle_of_last_logged_task)
\r
1162 formatLabel = xTraceOpenLabel(formatStr);
\r
1164 channel = xTraceRegisterChannelFormat(eventLabel, formatLabel);
\r
1166 prvTraceUserEventHelper1(channel, eventLabel, formatLabel, vl);
\r
1171 /******************************************************************************
\r
1174 * Basic user event (Standard and Professional Edition only)
\r
1176 * Generates a User Event with a text label. The label is created/looked up
\r
1177 * in the symbol table using xTraceOpenLabel.
\r
1178 ******************************************************************************/
\r
1179 void vTraceUserEvent(traceLabel eventLabel)
\r
1181 #if (USE_SEPARATE_USER_EVENT_BUFFER == 0)
\r
1185 TRACE_ASSERT(eventLabel > 0, "vTraceUserEvent: Invalid value for eventLabel", );
\r
1187 if (RecorderDataPtr->recorderActive && (! inExcludedTask || nISRactive) && handle_of_last_logged_task)
\r
1189 trcCRITICAL_SECTION_BEGIN();
\r
1191 dts1 = (uint8_t)prvTraceGetDTS(0xFF);
\r
1193 if (RecorderDataPtr->recorderActive) /* Need to repeat this check! */
\r
1195 ue = (UserEvent*) xTraceNextFreeEventBufferSlot();
\r
1199 ue->type = USER_EVENT;
\r
1200 ue->payload = eventLabel;
\r
1201 prvTraceUpdateCounters();
\r
1204 trcCRITICAL_SECTION_END();
\r
1206 #elif (USE_SEPARATE_USER_EVENT_BUFFER == 1)
\r
1207 UserEventChannel channel;
\r
1208 uint32_t noOfSlots = 1;
\r
1209 uint32_t tempDataBuffer[(3 + MAX_ARG_SIZE) / 4];
\r
1211 if (RecorderDataPtr->recorderActive && (! inExcludedTask || nISRactive) && handle_of_last_logged_task)
\r
1213 channel = xTraceRegisterChannelFormat(0, eventLabel);
\r
1217 /* We are dealing with an unknown channel format pair */
\r
1218 noOfSlots++; /* Also need room for channel and format */
\r
1219 ((uint16_t*)tempDataBuffer)[2] = 0;
\r
1220 ((uint16_t*)tempDataBuffer)[3] = eventLabel;
\r
1223 prvTraceUserEventHelper2(channel, tempDataBuffer, noOfSlots);
\r
1228 /*******************************************************************************
\r
1231 * Creates user event labels for user event channels or for individual events.
\r
1232 * User events can be used to log application events and data for display in
\r
1233 * the visualization tool. A user event is identified by a label, i.e., a string,
\r
1234 * which is stored in the recorder's symbol table.
\r
1235 * When logging a user event, a numeric handle (reference) to this string is
\r
1236 * used to identify the event. This is obtained by calling
\r
1238 * xTraceOpenLabel()
\r
1240 * which adds the string to the symbol table (if not already present)
\r
1241 * and returns the corresponding handle.
\r
1243 * This can be used in two ways:
\r
1245 * 1. The handle is looked up every time, when storing the user event.
\r
1248 * vTraceUserEvent(xTraceOpenLabel("MyUserEvent"));
\r
1250 * 2. The label is registered just once, with the handle stored in an
\r
1251 * application variable - much like using a file handle.
\r
1254 * myEventHandle = xTraceOpenLabel("MyUserEvent");
\r
1256 * vTraceUserEvent(myEventHandle);
\r
1258 * The second option is faster since no lookup is required on each event, and
\r
1259 * therefore recommended for user events that are frequently
\r
1260 * executed and/or located in time-critical code. The lookup operation is
\r
1261 * however fairly fast due to the design of the symbol table.
\r
1262 ******************************************************************************/
\r
1263 traceLabel xTraceOpenLabel(const char* label)
\r
1265 TRACE_ASSERT(label != NULL, "xTraceOpenLabel: label == NULL", (traceLabel)0);
\r
1267 return prvTraceOpenSymbol(label, 0);
\r