1 /*******************************************************************************
\r
2 * Tracealyzer v2.7.0 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 * Tabs are used for indent in this file (1 tab = 4 spaces)
\r
36 * Copyright Percepio AB, 2014.
\r
38 ******************************************************************************/
\r
39 #include "FreeRTOS.h"
\r
42 #include "trcUser.h"
\r
44 #if (USE_TRACEALYZER_RECORDER == 1)
\r
50 TRACE_STOP_HOOK vTraceStopHookPtr = (TRACE_STOP_HOOK)0;
\r
52 extern uint8_t inExcludedTask;
\r
53 extern uint8_t nISRactive;
\r
54 extern objectHandleType handle_of_last_logged_task;
\r
55 extern uint32_t dts_min;
\r
56 extern uint32_t hwtc_count_max_after_tick;
\r
57 extern uint32_t hwtc_count_sum_after_tick;
\r
58 extern uint32_t hwtc_count_sum_after_tick_counter;
\r
59 extern char* traceErrorMessage;
\r
61 /*** Private functions *******************************************************/
\r
62 void vTracePrintF_Helper(traceLabel eventLabel, const char* formatStr, va_list vl);
\r
64 #if (USE_SEPARATE_USER_EVENT_BUFFER == 1)
\r
65 void vTraceChannelPrintF_Helper(UserEventChannel channelPair, va_list vl);
\r
66 static void prvTraceUserEventHelper1(UserEventChannel channel, traceLabel eventLabel, traceLabel formatLabel, va_list vl);
\r
67 static void prvTraceUserEventHelper2(UserEventChannel channel, uint32_t* data, uint32_t noOfSlots);
\r
70 static void prvTraceTaskInstanceFinish(int8_t direct);
\r
73 /*******************************************************************************
\r
74 * vTraceInitTraceData
\r
76 * Allocates, if necessary, and initializes the recorder data structure, based
\r
77 * on the constants in trcConfig.h.
\r
78 ******************************************************************************/
\r
79 void vTraceInitTraceData(void)
\r
81 prvTraceInitTraceData();
\r
84 /*******************************************************************************
\r
85 * vTraceSetRecorderData
\r
87 * If custom allocation is used, this function must be called so the recorder
\r
88 * library knows where to save the trace data.
\r
89 ******************************************************************************/
\r
90 #if TRACE_DATA_ALLOCATION == TRACE_DATA_ALLOCATION_CUSTOM
\r
91 void vTraceSetRecorderData(void* pRecorderData)
\r
93 TRACE_ASSERT(pRecorderData != NULL, "vTraceSetTraceData, pRecorderData == NULL", );
\r
94 RecorderDataPtr = pRecorderData;
\r
98 /*******************************************************************************
\r
101 * Sets a function to be called when the recorder is stopped.
\r
102 ******************************************************************************/
\r
103 void vTraceSetStopHook(TRACE_STOP_HOOK stopHookFunction)
\r
105 vTraceStopHookPtr = stopHookFunction;
\r
108 /*******************************************************************************
\r
111 * Resets the recorder. Only necessary if a restart is desired - this is not
\r
112 * needed in the startup initialization.
\r
113 ******************************************************************************/
\r
114 void vTraceClear(void)
\r
116 TRACE_SR_ALLOC_CRITICAL_SECTION();
\r
117 trcCRITICAL_SECTION_BEGIN();
\r
119 RecorderDataPtr->absTimeLastEventSecond = 0;
\r
121 RecorderDataPtr->absTimeLastEvent = 0;
\r
122 RecorderDataPtr->nextFreeIndex = 0;
\r
123 RecorderDataPtr->numEvents = 0;
\r
124 RecorderDataPtr->bufferIsFull = 0;
\r
125 traceErrorMessage = NULL;
\r
126 RecorderDataPtr->internalErrorOccured = 0;
\r
128 memset(RecorderDataPtr->eventData, 0, RecorderDataPtr->maxEvents * 4);
\r
130 trcCRITICAL_SECTION_END();
\r
134 /*******************************************************************************
\r
137 * Starts the recorder. The recorder will not be started if an error has been
\r
138 * indicated using vTraceError, e.g. if any of the Nx constants in trcConfig.h
\r
139 * has a too small value (NTASK, NQUEUE, etc).
\r
141 * Returns 1 if the recorder was started successfully.
\r
142 * Returns 0 if the recorder start was prevented due to a previous internal
\r
143 * error. In that case, check vTraceGetLastError to get the error message.
\r
144 * Any error message is also presented when opening a trace file.
\r
145 ******************************************************************************/
\r
147 uint32_t uiTraceStart(void)
\r
149 objectHandleType handle;
\r
150 TRACE_SR_ALLOC_CRITICAL_SECTION();
\r
154 if (RecorderDataPtr == NULL)
\r
156 vTraceError("RecorderDataPtr is NULL. Call vTraceInitTraceData() before starting trace.");
\r
160 if (traceErrorMessage == NULL)
\r
162 trcCRITICAL_SECTION_BEGIN();
\r
163 RecorderDataPtr->recorderActive = 1;
\r
165 handle = TRACE_GET_TASK_NUMBER(TRACE_GET_CURRENT_TASK());
\r
168 /* This occurs if the scheduler is not yet started.
\r
169 This creates a dummy "(startup)" task entry internally in the
\r
171 handle = xTraceGetObjectHandle(TRACE_CLASS_TASK);
\r
172 vTraceSetObjectName(TRACE_CLASS_TASK, handle, "(startup)");
\r
174 vTraceSetPriorityProperty(TRACE_CLASS_TASK, handle, 0);
\r
177 vTraceStoreTaskswitch(handle); /* Register the currently running task */
\r
178 trcCRITICAL_SECTION_END();
\r
181 return RecorderDataPtr->recorderActive;
\r
184 /*******************************************************************************
\r
187 * Starts the recorder. The recorder will not be started if an error has been
\r
188 * indicated using vTraceError, e.g. if any of the Nx constants in trcConfig.h
\r
189 * has a too small value (NTASK, NQUEUE, etc).
\r
191 * This function is obsolete, but has been saved for backwards compatibility.
\r
192 * We recommend using uiTraceStart instead.
\r
193 ******************************************************************************/
\r
194 void vTraceStart(void)
\r
196 (void)uiTraceStart();
\r
199 /*******************************************************************************
\r
202 * Stops the recorder. The recording can be resumed by calling vTraceStart.
\r
203 * This does not reset the recorder. Use vTraceClear if that is desired.
\r
204 ******************************************************************************/
\r
205 void vTraceStop(void)
\r
207 RecorderDataPtr->recorderActive = 0;
\r
209 if (vTraceStopHookPtr != (TRACE_STOP_HOOK)0)
\r
211 (*vTraceStopHookPtr)(); /* An application call-back function. */
\r
215 /*******************************************************************************
\r
216 * xTraceGetLastError
\r
218 * Gives the last error message, if any. NULL if no error message is stored.
\r
219 * Any error message is also presented when opening a trace file.
\r
220 ******************************************************************************/
\r
221 char* xTraceGetLastError(void)
\r
223 return traceErrorMessage;
\r
226 /*******************************************************************************
\r
229 * Removes any previous error message generated by recorder calling vTraceError.
\r
230 * By calling this function, it may be possible to start/restart the trace
\r
231 * despite errors in the recorder, but there is no guarantee that the trace
\r
232 * recorder will work correctly in that case, depending on the type of error.
\r
233 ******************************************************************************/
\r
234 void vTraceClearError(int resetErrorMessage)
\r
236 ( void ) resetErrorMessage;
\r
237 traceErrorMessage = NULL;
\r
238 RecorderDataPtr->internalErrorOccured = 0;
\r
241 /*******************************************************************************
\r
242 * vTraceGetTraceBuffer
\r
244 * Returns a pointer to the recorder data structure. Use this together with
\r
245 * uiTraceGetTraceBufferSize if you wish to implement an own store/upload
\r
246 * solution, e.g., in case a debugger connection is not available for uploading
\r
248 ******************************************************************************/
\r
249 void* vTraceGetTraceBuffer(void)
\r
251 return RecorderDataPtr;
\r
254 /*******************************************************************************
\r
255 * uiTraceGetTraceBufferSize
\r
257 * Gets the size of the recorder data structure. For use together with
\r
258 * vTraceGetTraceBuffer if you wish to implement an own store/upload solution,
\r
259 * e.g., in case a debugger connection is not available for uploading the data.
\r
260 ******************************************************************************/
\r
261 uint32_t uiTraceGetTraceBufferSize(void)
\r
263 return sizeof(RecorderDataType);
\r
266 /******************************************************************************
\r
267 * prvTraceTaskInstanceFinish.
\r
269 * Private common function for the vTraceTaskInstanceFinishXXX functions.
\r
271 *****************************************************************************/
\r
272 void prvTraceTaskInstanceFinish(int8_t direct)
\r
274 TaskInstanceStatusEvent* tis;
\r
277 TRACE_SR_ALLOC_CRITICAL_SECTION();
\r
279 trcCRITICAL_SECTION_BEGIN();
\r
280 if (RecorderDataPtr->recorderActive && (! inExcludedTask || nISRactive) && handle_of_last_logged_task)
\r
282 dts45 = (uint8_t)prvTraceGetDTS(0xFF);
\r
283 tis = (TaskInstanceStatusEvent*) xTraceNextFreeEventBufferSlot();
\r
287 tis->type = TASK_INSTANCE_FINISHED_NEXT_KSE;
\r
289 tis->type = TASK_INSTANCE_FINISHED_DIRECT;
\r
292 prvTraceUpdateCounters();
\r
295 trcCRITICAL_SECTION_END();
\r
298 /******************************************************************************
\r
299 * vTraceTaskInstanceFinish(void)
\r
301 * Marks the current task instance as finished on the next kernel call.
\r
303 * If that kernel call is blocking, the instance ends after the blocking event
\r
304 * and the corresponding return event is then the start of the next instance.
\r
305 * If the kernel call is not blocking, the viewer instead splits the current
\r
306 * fragment right before the kernel call, which makes this call the first event
\r
307 * of the next instance.
\r
309 * See also USE_IMPLICIT_IFE_RULES in trcConfig.h
\r
315 * xQueueReceive(CommandQueue, &command, timeoutDuration);
\r
316 * processCommand(command);
\r
317 * vTraceInstanceFinish();
\r
320 * Note: This is only supported in Tracealyzer tools v2.7 or later
\r
322 *****************************************************************************/
\r
323 void vTraceTaskInstanceFinish(void)
\r
325 prvTraceTaskInstanceFinish(0);
\r
328 /******************************************************************************
\r
329 * vTraceTaskInstanceFinishDirect(void)
\r
331 * Marks the current task instance as finished at this very instant.
\r
332 * This makes the viewer to splits the current fragment at this point and begin
\r
333 * a new actor instance.
\r
335 * See also USE_IMPLICIT_IFE_RULES in trcConfig.h
\r
339 * This example will generate two instances for each loop iteration.
\r
340 * The first instance ends at vTraceInstanceFinishDirect(), while the second
\r
341 * instance ends at the next xQueueReceive call.
\r
345 * xQueueReceive(CommandQueue, &command, timeoutDuration);
\r
346 * ProcessCommand(command);
\r
347 * vTraceInstanceFinishDirect();
\r
348 * DoSometingElse();
\r
349 * vTraceInstanceFinish();
\r
352 * Note: This is only supported in Tracealyzer tools v2.7 or later
\r
354 *****************************************************************************/
\r
355 void vTraceTaskInstanceFinishDirect(void)
\r
357 prvTraceTaskInstanceFinish(1);
\r
360 /*******************************************************************************
\r
361 * Interrupt recording functions
\r
362 ******************************************************************************/
\r
364 #if (INCLUDE_ISR_TRACING == 1)
\r
366 #define MAX_ISR_NESTING 16
\r
367 static uint8_t isrstack[MAX_ISR_NESTING];
\r
369 /*******************************************************************************
\r
370 * vTraceSetISRProperties
\r
372 * Registers an Interrupt Service Routine in the recorder library, This must be
\r
373 * called before using vTraceStoreISRBegin to store ISR events. This is
\r
374 * typically called in the startup of the system, before the scheduler is
\r
378 * #define ID_ISR_TIMER1 1 // lowest valid ID is 1
\r
379 * #define PRIO_OF_ISR_TIMER1 3 // the hardware priority of the interrupt
\r
381 * vTraceSetISRProperties(ID_ISR_TIMER1, "ISRTimer1", PRIO_OF_ISR_TIMER1);
\r
383 * void ISR_handler()
\r
385 * vTraceStoreISRBegin(ID_OF_ISR_TIMER1);
\r
387 * vTraceStoreISREnd(0);
\r
390 * NOTE: To safely record ISRs, you need to make sure that all traced
\r
391 * interrupts actually are disabled by trcCRITICAL_SECTION_BEGIN(). However,
\r
392 * in some ports this does not disable high priority interrupts!
\r
393 * If an ISR calls vTraceStoreISRBegin while the recorder is busy, it will
\r
394 * stop the recording and give an error message.
\r
395 ******************************************************************************/
\r
396 void vTraceSetISRProperties(objectHandleType handle, const char* name, char priority)
\r
398 TRACE_ASSERT(handle <= RecorderDataPtr->ObjectPropertyTable.NumberOfObjectsPerClass[TRACE_CLASS_ISR], "vTraceSetISRProperties: Invalid value for handle", );
\r
399 TRACE_ASSERT(name != NULL, "vTraceSetISRProperties: name == NULL", );
\r
401 vTraceSetObjectName(TRACE_CLASS_ISR, handle, name);
\r
402 vTraceSetPriorityProperty(TRACE_CLASS_ISR, handle, priority);
\r
405 #if (SELECTED_PORT == PORT_ARM_CortexM)
\r
406 /******************************************************************************
\r
409 * ISR_TAILCHAINING_THRESHOLD (For Cortex-M devices only)
\r
411 * ARM Cortex-M devices may execute ISRs back-to-back (tail-chained) without
\r
412 * resuming the previous context in between. Since this is decided in
\r
413 * hardware, we can only detect this indirectly, in the following manner:
\r
415 * When entering vTraceStoreISRBegin, we check the number of CPU cycles since
\r
416 * the last return of vTraceStoreISREnd. If less or equal to the constant
\r
417 * ISR_TAILCHAINING_THRESHOLD it is assumed that the ISRs executed back-to-back
\r
418 * (tail-chained). In that case, the previously stored RESUME event
\r
419 * (pointed to by ptrLastISRExitEvent) is then deleted to avoid showing a
\r
420 * fragment of the previous context in between the ISR events. The delete is
\r
421 * made by replacing the event code with a XTS16L event, that serves to keep
\r
422 * the differential timestamp from the earlier event.
\r
424 * The value of ISR_TAILCHAINING_THRESHOLD depends on the interrupt latency of
\r
425 * the processor, on the compiler and on the compiler settings, but should be
\r
426 * around 70 cycles. The default value is 66 cycles, which should be correct when
\r
427 * using GCC with optimizations disabled (-O0) and Cortex-M3/M4.
\r
429 * To measure this value, see MEASURE_ISR_TAILCHAINING_THRESHOLD below.
\r
431 * If this value set too low, tail-chained ISRs will incorrectly be shown
\r
432 * separated, with a short fragment of the previous task or ISR in between.
\r
433 * If this value is set too high, you get the opposite effect - separate ISRs
\r
434 * will appear to execute tail-chained and will appear to have higher execution
\r
435 * time and response time (maximum ISR_TAILCHAINING_THRESHOLD cycles more).
\r
437 * Read the blog post explaining this on our website:
\r
438 * http://percepio.com/2014/05/06/sw-based-exc-tracing-arm-cortex-m/
\r
440 *****************************************************************************/
\r
441 #define ISR_TAILCHAINING_THRESHOLD 66
\r
443 uint8_t* ptrLastISRExitEvent = NULL;
\r
444 uint32_t DWTCycleCountAtLastISRExit = 0;
\r
446 /******************************************************************************
\r
449 * MEASURE_ISR_TAILCHAINING_THRESHOLD (For Cortex-M devices only)
\r
451 * Allows for calibrating the value of ISR_TAILCHAINING_THRESHOLD (see above).
\r
453 * This is intended to measure the minimum number of clock cycles from the end
\r
454 * of vTraceStoreISREnd to the beginning of the following vTraceStoreISRBegin.
\r
455 * For this purpose, we assume a test setup using the SysTick interrupt, which
\r
456 * is available on most Cortex-M devices and typically used by the RTOS kernel.
\r
457 * To do the measurement, follow these steps:
\r
459 * 1. Make sure MEASURE_ISR_TAILCHAINING_THRESHOLD is enabled (defined as 1)
\r
461 * 2. Temporarily replace your SysTick handler with the following:
\r
463 * void xPortSysTickHandler( void )
\r
465 * vTraceStoreISRBegin(1);
\r
466 * vTraceStoreISREnd(0);
\r
469 * 3. To make sure that the ISRs execute back-to-back, increase the OS tick
\r
470 * frequency to a very high level so that the OS tick interrupt execute
\r
471 * continuously with no application tasks in between, e.g. 10 MHz.
\r
473 * 4. Put a breakpoint in the highest priority task and make sure it is not
\r
474 * reached. This means that the SysTick handler is executing at maximum rate
\r
475 * and thereby tail-chained, where the interrupt latency is 6 cycles.
\r
477 * 5. Let the system run without breakpoints and inspect the value of
\r
478 * threshold_low_watermark. This is the minimum total latency observed.
\r
479 * The hardware latency is 6 clock cycles due to the tail-chaining, so the
\r
480 * software latency (SL) is then SL = threshold_low_watermark - 6.
\r
482 * The threshold value ISR_TAILCHAINING_THRESHOLD should be SL + 2 * HL, where
\r
483 * HL is the normal hardware interrupt latency, i.e., the number of CPU
\r
484 * cycles to enter or exit the exception handler for an exception in task
\r
485 * context. The HL value is 12-16 depending on core, as shown below.
\r
487 * Values for ISR_TAILCHAINING_THRESHOLD, assuming SL = 42
\r
488 * Cortex-M3 and M4 (HL = 12): 66 cycles
\r
489 * Cortex-M0 (HL = 16): 74 cycles
\r
490 * Cortex-M0+ (HL = 15): 72 cycles
\r
492 * If the ISR_TAILCHAINING_THRESHOLD value is set too low, some tail-chained
\r
493 * ISRs be shown separated, with a short fragment of the previous context
\r
494 * in between. On the other hand, if the value is set too high, ISRs that
\r
495 * actually are separated may appear to execute back-to-back (tail-chained).
\r
497 * Read the blog post explaining this on our website:
\r
498 * http://percepio.com/2014/05/06/sw-based-exc-tracing-arm-cortex-m/
\r
500 *****************************************************************************/
\r
501 #define MEASURE_ISR_TAILCHAINING_THRESHOLD 1
\r
503 #if (MEASURE_ISR_TAILCHAINING_THRESHOLD == 1)
\r
504 volatile uint32_t threshold_low_watermark = 2000000000;
\r
509 /*******************************************************************************
\r
510 * vTraceStoreISRBegin
\r
512 * Registers the beginning of an Interrupt Service Routine.
\r
515 * #define ID_ISR_TIMER1 1 // lowest valid ID is 1
\r
516 * #define PRIO_OF_ISR_TIMER1 3 // the hardware priority of the interrupt
\r
518 * vTraceSetISRProperties(ID_ISR_TIMER1, "ISRTimer1", PRIO_OF_ISR_TIMER1);
\r
520 * void ISR_handler()
\r
522 * vTraceStoreISRBegin(ID_OF_ISR_TIMER1);
\r
524 * vTraceStoreISREnd(0);
\r
527 ******************************************************************************/
\r
528 void vTraceStoreISRBegin(objectHandleType handle)
\r
531 #if (SELECTED_PORT == PORT_ARM_CortexM)
\r
532 uint32_t CPUCyclesSinceLastISRExit = REG_DWT_CYCCNT - DWTCycleCountAtLastISRExit;
\r
535 TRACE_SR_ALLOC_CRITICAL_SECTION();
\r
539 #if (SELECTED_PORT == PORT_ARM_CortexM)
\r
540 if (DWTCycleCountAtLastISRExit > 0)
\r
542 #if (MEASURE_ISR_TAILCHAINING_THRESHOLD == 1)
\r
543 /* Allows for verifying the value of ISR_TAILCHAINING_THRESHOLD */
\r
544 if (CPUCyclesSinceLastISRExit < threshold_low_watermark)
\r
546 threshold_low_watermark = CPUCyclesSinceLastISRExit;
\r
550 if (CPUCyclesSinceLastISRExit <= ISR_TAILCHAINING_THRESHOLD)
\r
552 /* This is judged to be a case of ISR tail-chaining since the
\r
553 number of cycles since the last vTraceStoreISREnd is shorter or equal to
\r
554 the threshold (ISR_TAILCHAINING_THRESHOLD) */
\r
556 if (ptrLastISRExitEvent != NULL)
\r
558 /* Overwrite the last ISR exit event with a "neutral" event that only
\r
559 accounts for the time passed */
\r
560 *ptrLastISRExitEvent = XTS16L;
\r
569 vTraceError("Illegal call to vTraceStoreISRBegin, recorder busy!");
\r
572 trcCRITICAL_SECTION_BEGIN();
\r
573 if (RecorderDataPtr->recorderActive && handle_of_last_logged_task)
\r
576 TRACE_ASSERT(handle <= RecorderDataPtr->ObjectPropertyTable.NumberOfObjectsPerClass[TRACE_CLASS_ISR], "vTraceStoreISRBegin: Invalid value for handle", );
\r
578 dts4 = (uint16_t)prvTraceGetDTS(0xFFFF);
\r
580 if (RecorderDataPtr->recorderActive) /* Need to repeat this check! */
\r
582 if (nISRactive < MAX_ISR_NESTING)
\r
584 uint8_t hnd8 = prvTraceGet8BitHandle(handle);
\r
585 isrstack[nISRactive] = handle;
\r
587 ts = (TSEvent*)xTraceNextFreeEventBufferSlot();
\r
590 ts->type = TS_ISR_BEGIN;
\r
592 ts->objHandle = hnd8;
\r
593 prvTraceUpdateCounters();
\r
598 /* This should not occur unless something is very wrong */
\r
599 vTraceError("Too many nested interrupts!");
\r
603 trcCRITICAL_SECTION_END();
\r
606 /*******************************************************************************
\r
607 * vTraceStoreISREnd
\r
609 * Registers the end of an Interrupt Service Routine.
\r
611 * The parameter pendingISR indicates if the interrupt has requested a
\r
612 * task-switch (= 1) or if the interrupt returns to the earlier context (= 0)
\r
616 * #define ID_ISR_TIMER1 1 // lowest valid ID is 1
\r
617 * #define PRIO_OF_ISR_TIMER1 3 // the hardware priority of the interrupt
\r
619 * vTraceSetISRProperties(ID_ISR_TIMER1, "ISRTimer1", PRIO_OF_ISR_TIMER1);
\r
621 * void ISR_handler()
\r
623 * vTraceStoreISRBegin(ID_OF_ISR_TIMER1);
\r
625 * vTraceStoreISREnd(0);
\r
628 ******************************************************************************/
\r
629 void vTraceStoreISREnd(int pendingISR)
\r
633 TRACE_SR_ALLOC_CRITICAL_SECTION();
\r
637 vTraceError("Illegal call to vTraceStoreISREnd, recorder busy!");
\r
641 trcCRITICAL_SECTION_BEGIN();
\r
642 if (pendingISR == 0)
\r
644 if (RecorderDataPtr->recorderActive && handle_of_last_logged_task)
\r
646 uint8_t hnd8, type;
\r
647 dts5 = (uint16_t)prvTraceGetDTS(0xFFFF);
\r
649 if (nISRactive > 1)
\r
651 /* return to another isr */
\r
652 type = TS_ISR_RESUME;
\r
653 hnd8 = prvTraceGet8BitHandle(isrstack[nISRactive]);
\r
657 /* return to task */
\r
658 type = TS_TASK_RESUME;
\r
659 hnd8 = prvTraceGet8BitHandle(handle_of_last_logged_task);
\r
662 ts = (TSEvent*)xTraceNextFreeEventBufferSlot();
\r
666 ts->objHandle = hnd8;
\r
668 prvTraceUpdateCounters();
\r
671 #if (SELECTED_PORT == PORT_ARM_CortexM)
\r
672 /* Remember the last ISR exit event, as the event needs to be modified in case of a following ISR entry (if tail-chained ISRs) */
\r
673 ptrLastISRExitEvent = (uint8_t*)ts;
\r
679 #if (SELECTED_PORT == PORT_ARM_CortexM)
\r
680 DWTCycleCountAtLastISRExit = REG_DWT_CYCCNT;
\r
683 trcCRITICAL_SECTION_END();
\r
688 /* ISR tracing is turned off */
\r
689 void vTraceIncreaseISRActive(void)
\r
691 if (RecorderDataPtr->recorderActive && handle_of_last_logged_task)
\r
695 void vTraceDecreaseISRActive(void)
\r
697 if (RecorderDataPtr->recorderActive && handle_of_last_logged_task)
\r
703 /********************************************************************************/
\r
704 /* User Event functions */
\r
705 /********************************************************************************/
\r
707 #if (INCLUDE_USER_EVENTS == 1)
\r
709 #define MAX_ARG_SIZE (4+32)
\r
710 /*** Locally used in vTracePrintF ***/
\r
711 static uint8_t writeInt8(void * buffer, uint8_t i, uint8_t value)
\r
713 TRACE_ASSERT(buffer != NULL, "writeInt8: buffer == NULL", 0);
\r
715 if (i >= MAX_ARG_SIZE)
\r
720 ((uint8_t*)buffer)[i] = value;
\r
722 if (i + 1 > MAX_ARG_SIZE)
\r
730 /*** Locally used in vTracePrintF ***/
\r
731 static uint8_t writeInt16(void * buffer, uint8_t i, uint16_t value)
\r
733 TRACE_ASSERT(buffer != NULL, "writeInt16: buffer == NULL", 0);
\r
735 /* Align to multiple of 2 */
\r
736 while ((i % 2) != 0)
\r
738 if (i >= MAX_ARG_SIZE)
\r
743 ((uint8_t*)buffer)[i] = 0;
\r
747 if (i + 2 > MAX_ARG_SIZE)
\r
752 ((uint16_t*)buffer)[i/2] = value;
\r
757 /*** Locally used in vTracePrintF ***/
\r
758 static uint8_t writeInt32(void * buffer, uint8_t i, uint32_t value)
\r
760 TRACE_ASSERT(buffer != NULL, "writeInt32: buffer == NULL", 0);
\r
762 /* A 32 bit value should begin at an even 4-byte address */
\r
763 while ((i % 4) != 0)
\r
765 if (i >= MAX_ARG_SIZE)
\r
770 ((uint8_t*)buffer)[i] = 0;
\r
774 if (i + 4 > MAX_ARG_SIZE)
\r
779 ((uint32_t*)buffer)[i/4] = value;
\r
784 #if (INCLUDE_FLOAT_SUPPORT)
\r
786 /*** Locally used in vTracePrintF ***/
\r
787 static uint8_t writeFloat(void * buffer, uint8_t i, float value)
\r
789 TRACE_ASSERT(buffer != NULL, "writeFloat: buffer == NULL", 0);
\r
791 /* A 32 bit value should begin at an even 4-byte address */
\r
792 while ((i % 4) != 0)
\r
794 if (i >= MAX_ARG_SIZE)
\r
799 ((uint8_t*)buffer)[i] = 0;
\r
803 if (i + 4 > MAX_ARG_SIZE)
\r
808 ((float*)buffer)[i/4] = value;
\r
813 /*** Locally used in vTracePrintF ***/
\r
814 static uint8_t writeDouble(void * buffer, uint8_t i, double value)
\r
817 uint32_t * src = (uint32_t*)&value;
\r
819 TRACE_ASSERT(buffer != NULL, "writeDouble: buffer == NULL", 0);
\r
821 /* The double is written as two 32 bit values, and should begin at an even
\r
822 4-byte address (to avoid having to align with 8 byte) */
\r
825 if (i >= MAX_ARG_SIZE)
\r
830 ((uint8_t*)buffer)[i] = 0;
\r
834 if (i + 8 > MAX_ARG_SIZE)
\r
839 dest = &(((uint32_t *)buffer)[i/4]);
\r
849 /*******************************************************************************
\r
850 * prvTraceUserEventFormat
\r
852 * Parses the format string and stores the arguments in the buffer.
\r
853 ******************************************************************************/
\r
854 static uint8_t prvTraceUserEventFormat(const char* formatStr, va_list vl, uint8_t* buffer, uint8_t byteOffset)
\r
856 uint16_t formatStrIndex = 0;
\r
857 uint8_t argCounter = 0;
\r
858 uint8_t i = byteOffset;
\r
860 while (formatStr[formatStrIndex] != '\0')
\r
862 if (formatStr[formatStrIndex] == '%')
\r
866 if (argCounter > 15)
\r
868 vTraceError("vTracePrintF - Too many arguments, max 15 allowed!");
\r
872 /*******************************************************************************
\r
873 * These below code writes raw data (primitive datatypes) in the event buffer,
\r
874 * instead of the normal event structs (where byte 0 is event type).
\r
875 * These data entries must never be interpreted as real event data, as the type
\r
876 * field would be misleading since used for payload data.
\r
878 * The correctness of this encoding depends on two mechanisms:
\r
880 * 1. An initial USER_EVENT, which type code tells the number of 32-bit data
\r
881 * entires that follows. (code - USER_EVENT = number of data entries).
\r
882 * Note that a data entry corresponds to the slots that normally corresponds to
\r
883 * one (1) event, i.e., 32 bits. vTracePrintF may encode several pieces of data
\r
884 * in one data entry, e.g., two 16-bit values or four 8-bit values, one 16-bit
\r
885 * value followed by two 8-bit values, etc.
\r
887 * 2. A two-phase commit procedure, where the USER_EVENT and data entries are
\r
888 * written to a local buffer at first, and when all checks are OK then copied to
\r
889 * the main event buffer using a fast memcpy. The event code is finalized as the
\r
890 * very last step. Before that step, the event code indicates an unfinished
\r
891 * event, which causes it to be ignored and stop the loading of the file (since
\r
892 * an unfinished event is the last event in the trace).
\r
893 *******************************************************************************/
\r
896 while ((formatStr[formatStrIndex] >= '0' && formatStr[formatStrIndex] <= '9') || formatStr[formatStrIndex] == '#' || formatStr[formatStrIndex] == '.')
\r
899 if (formatStr[formatStrIndex] != '\0')
\r
901 switch (formatStr[formatStrIndex])
\r
903 case 'd': i = writeInt32( buffer,
\r
905 (uint32_t)va_arg(vl, uint32_t));
\r
909 case 'u': i = writeInt32( buffer,
\r
911 (uint32_t)va_arg(vl, uint32_t));
\r
913 case 's': i = writeInt16( buffer,
\r
915 (uint16_t)xTraceOpenLabel((char*)va_arg(vl, char*)));
\r
918 #if (INCLUDE_FLOAT_SUPPORT)
\r
919 /* Yes, "double" as type also in the float
\r
920 case. This since "float" is promoted into "double"
\r
921 by the va_arg stuff. */
\r
922 case 'f': i = writeFloat( buffer,
\r
924 (float)va_arg(vl, double));
\r
927 /* No support for floats, but attempt to store a float user event
\r
928 avoid a possible crash due to float reference. Instead store the
\r
929 data on uint_32 format (will not be displayed anyway). This is just
\r
930 to keep va_arg and i consistent. */
\r
932 case 'f': i = writeInt32( buffer,
\r
934 (uint32_t)va_arg(vl, double));
\r
939 switch (formatStr[formatStrIndex])
\r
941 #if (INCLUDE_FLOAT_SUPPORT)
\r
942 case 'f': i = writeDouble(buffer,
\r
944 (double)va_arg(vl, double));
\r
947 /* No support for floats, but attempt to store a float user event
\r
948 avoid a possible crash due to float reference. Instead store the
\r
949 data on uint_32 format (will not be displayed anyway). This is just
\r
950 to keep va_arg and i consistent. */
\r
951 case 'f': i = writeInt32( buffer, /* In this case, the value will not be shown anyway */
\r
953 (uint32_t)va_arg(vl, double));
\r
955 i = writeInt32( buffer, /* Do it twice, to write in total 8 bytes */
\r
957 (uint32_t)va_arg(vl, double));
\r
965 switch (formatStr[formatStrIndex])
\r
967 case 'd': i = writeInt16( buffer,
\r
969 (uint16_t)va_arg(vl, uint32_t));
\r
971 case 'u': i = writeInt16( buffer,
\r
973 (uint16_t)va_arg(vl, uint32_t));
\r
979 switch (formatStr[formatStrIndex])
\r
981 case 'd': i = writeInt8( buffer,
\r
983 (uint8_t)va_arg(vl, uint32_t));
\r
986 case 'u': i = writeInt8( buffer,
\r
988 (uint8_t)va_arg(vl, uint32_t));
\r
1000 vTraceError("vTracePrintF - Too large arguments, max 32 byte allowed!");
\r
1007 #if (USE_SEPARATE_USER_EVENT_BUFFER == 1)
\r
1009 /*******************************************************************************
\r
1010 * prvTraceClearChannelBuffer
\r
1012 * Clears a number of items in the channel buffer, starting from nextSlotToWrite.
\r
1013 ******************************************************************************/
\r
1014 static void prvTraceClearChannelBuffer(uint32_t count)
\r
1018 TRACE_ASSERT(USER_EVENT_BUFFER_SIZE >= count,
\r
1019 "prvTraceClearChannelBuffer: USER_EVENT_BUFFER_SIZE is too small to handle this event.", );
\r
1021 /* Check if we're close to the end of the buffer */
\r
1022 if (RecorderDataPtr->userEventBuffer.nextSlotToWrite + count > USER_EVENT_BUFFER_SIZE)
\r
1024 slots = USER_EVENT_BUFFER_SIZE - RecorderDataPtr->userEventBuffer.nextSlotToWrite; /* Number of slots before end of buffer */
\r
1025 (void)memset(&RecorderDataPtr->userEventBuffer.channelBuffer[RecorderDataPtr->userEventBuffer.nextSlotToWrite], 0, slots);
\r
1026 (void)memset(&RecorderDataPtr->userEventBuffer.channelBuffer[0], 0, (count - slots));
\r
1029 (void)memset(&RecorderDataPtr->userEventBuffer.channelBuffer[RecorderDataPtr->userEventBuffer.nextSlotToWrite], 0, count);
\r
1032 /*******************************************************************************
\r
1033 * prvTraceCopyToDataBuffer
\r
1035 * Copies a number of items to the data buffer, starting from nextSlotToWrite.
\r
1036 ******************************************************************************/
\r
1037 static void prvTraceCopyToDataBuffer(uint32_t* data, uint32_t count)
\r
1039 TRACE_ASSERT(data != NULL,
\r
1040 "prvTraceCopyToDataBuffer: data == NULL.", );
\r
1041 TRACE_ASSERT(count <= USER_EVENT_BUFFER_SIZE,
\r
1042 "prvTraceCopyToDataBuffer: USER_EVENT_BUFFER_SIZE is too small to handle this event.", );
\r
1045 /* Check if we're close to the end of the buffer */
\r
1046 if (RecorderDataPtr->userEventBuffer.nextSlotToWrite + count > USER_EVENT_BUFFER_SIZE)
\r
1048 slots = USER_EVENT_BUFFER_SIZE - RecorderDataPtr->userEventBuffer.nextSlotToWrite; /* Number of slots before end of buffer */
\r
1049 (void)memcpy(&RecorderDataPtr->userEventBuffer.dataBuffer[RecorderDataPtr->userEventBuffer.nextSlotToWrite * 4], data, slots * 4);
\r
1050 (void)memcpy(&RecorderDataPtr->userEventBuffer.dataBuffer[0], data + slots, (count - slots) * 4);
\r
1054 (void)memcpy(&RecorderDataPtr->userEventBuffer.dataBuffer[RecorderDataPtr->userEventBuffer.nextSlotToWrite * 4], data, count * 4);
\r
1058 /*******************************************************************************
\r
1059 * prvTraceUserEventHelper1
\r
1061 * Calls on prvTraceUserEventFormat() to do the actual formatting, then goes on
\r
1062 * to the next helper function.
\r
1063 ******************************************************************************/
\r
1064 static void prvTraceUserEventHelper1(UserEventChannel channel, traceLabel eventLabel, traceLabel formatLabel, va_list vl)
\r
1066 uint32_t data[(3 + MAX_ARG_SIZE) / 4];
\r
1067 uint8_t byteOffset = 4; /* Need room for timestamp */
\r
1068 uint8_t noOfSlots;
\r
1072 /* We are dealing with an unknown channel format pair */
\r
1073 byteOffset += 4; /* Also need room for channel and format */
\r
1074 ((uint16_t*)data)[2] = eventLabel;
\r
1075 ((uint16_t*)data)[3] = formatLabel;
\r
1078 noOfSlots = prvTraceUserEventFormat((char*)&(RecorderDataPtr->SymbolTable.symbytes[formatLabel+4]), vl, (uint8_t*)data, byteOffset);
\r
1080 prvTraceUserEventHelper2(channel, data, noOfSlots);
\r
1083 /*******************************************************************************
\r
1084 * prvTraceUserEventHelper2
\r
1086 * This function simply copies the data buffer to the actual user event buffer.
\r
1087 ******************************************************************************/
\r
1088 static void prvTraceUserEventHelper2(UserEventChannel channel, uint32_t* data, uint32_t noOfSlots)
\r
1090 static uint32_t old_timestamp = 0;
\r
1091 uint32_t old_nextSlotToWrite = 0;
\r
1093 TRACE_ASSERT(USER_EVENT_BUFFER_SIZE >= noOfSlots, "vTracePrintF: USER_EVENT_BUFFER_SIZE is too small to handle this event.", );
\r
1095 trcCRITICAL_SECTION_BEGIN();
\r
1096 /* Store the timestamp */
\r
1097 vTracePortGetTimeStamp(data);
\r
1099 if (*data < old_timestamp)
\r
1101 RecorderDataPtr->userEventBuffer.wraparoundCounter++;
\r
1104 old_timestamp = *data;
\r
1106 /* Start by erasing any information in the channel buffer */
\r
1107 prvTraceClearChannelBuffer(noOfSlots);
\r
1109 prvTraceCopyToDataBuffer(data, noOfSlots); /* Will wrap around the data if necessary */
\r
1111 old_nextSlotToWrite = RecorderDataPtr->userEventBuffer.nextSlotToWrite; /* Save the index that we want to write the channel data at when we're done */
\r
1112 RecorderDataPtr->userEventBuffer.nextSlotToWrite = (RecorderDataPtr->userEventBuffer.nextSlotToWrite + noOfSlots) % USER_EVENT_BUFFER_SIZE; /* Make sure we never end up outside the buffer */
\r
1114 /* Write to the channel buffer to indicate that this user event is ready to be used */
\r
1117 RecorderDataPtr->userEventBuffer.channelBuffer[old_nextSlotToWrite] = channel;
\r
1121 /* 0xFF indicates that this is not a normal channel id */
\r
1122 RecorderDataPtr->userEventBuffer.channelBuffer[old_nextSlotToWrite] = (UserEventChannel)0xFF;
\r
1124 trcCRITICAL_SECTION_END();
\r
1127 /*******************************************************************************
\r
1128 * xTraceRegisterChannelFormat
\r
1130 * Attempts to create a pair of the channel and format string.
\r
1132 * Note: This is only available if USE_SEPARATE_USER_EVENT_BUFFER is enabled in
\r
1134 ******************************************************************************/
\r
1135 UserEventChannel xTraceRegisterChannelFormat(traceLabel channel, traceLabel formatStr)
\r
1138 UserEventChannel retVal = 0;
\r
1140 TRACE_ASSERT(formatStr != 0, "vTraceRegisterChannelFormat: formatStr == 0", (UserEventChannel)0);
\r
1142 trcCRITICAL_SECTION_BEGIN();
\r
1143 for (i = 1; i <= CHANNEL_FORMAT_PAIRS; i++) /* Size of the channels buffer is CHANNEL_FORMAT_PAIRS + 1. Index 0 is unused. */
\r
1145 if(RecorderDataPtr->userEventBuffer.channels[i].name == 0 && RecorderDataPtr->userEventBuffer.channels[i].defaultFormat == 0)
\r
1147 /* Found empty slot */
\r
1148 RecorderDataPtr->userEventBuffer.channels[i].name = channel;
\r
1149 RecorderDataPtr->userEventBuffer.channels[i].defaultFormat = formatStr;
\r
1150 retVal = (UserEventChannel)i;
\r
1154 if (RecorderDataPtr->userEventBuffer.channels[i].name == channel && RecorderDataPtr->userEventBuffer.channels[i].defaultFormat == formatStr)
\r
1156 /* Found a match */
\r
1157 retVal = (UserEventChannel)i;
\r
1161 trcCRITICAL_SECTION_END();
\r
1165 /******************************************************************************
\r
1166 * vTraceChannelPrintF
\r
1168 * Slightly faster version of vTracePrintF() due to no lookups.
\r
1170 * Note: This is only available if USE_SEPARATE_USER_EVENT_BUFFER is enabled in
\r
1173 ******************************************************************************/
\r
1174 void vTraceChannelPrintF(UserEventChannel channelPair, ...)
\r
1176 #if (TRACE_SCHEDULING_ONLY == 0)
\r
1179 va_start(vl, channelPair);
\r
1180 vTraceChannelPrintF_Helper(channelPair, vl);
\r
1182 #endif /* TRACE_SCHEDULING_ONLY */
\r
1185 void vTraceChannelPrintF_Helper(UserEventChannel channelPair, va_list vl)
\r
1187 traceLabel channel;
\r
1188 traceLabel formatStr;
\r
1190 TRACE_ASSERT(channelPair != 0, "vTraceChannelPrintF: channelPair == 0", );
\r
1191 TRACE_ASSERT(channelPair <= CHANNEL_FORMAT_PAIRS, "vTraceChannelPrintF: ", );
\r
1193 channel = RecorderDataPtr->userEventBuffer.channels[channelPair].name;
\r
1194 formatStr = RecorderDataPtr->userEventBuffer.channels[channelPair].defaultFormat;
\r
1196 prvTraceUserEventHelper1(channelPair, channel, formatStr, vl);
\r
1199 /******************************************************************************
\r
1200 * vTraceChannelUserEvent
\r
1202 * Slightly faster version of vTraceUserEvent() due to no lookups.
\r
1203 ******************************************************************************/
\r
1204 void vTraceChannelUserEvent(UserEventChannel channelPair)
\r
1206 #if (TRACE_SCHEDULING_ONLY == 0)
\r
1207 uint32_t data[(3 + MAX_ARG_SIZE) / 4];
\r
1209 TRACE_ASSERT(channelPair != 0, "vTraceChannelPrintF: channelPair == 0", );
\r
1210 TRACE_ASSERT(channelPair <= CHANNEL_FORMAT_PAIRS, "vTraceChannelPrintF: ", );
\r
1212 prvTraceUserEventHelper2(channelPair, data, 1); /* Only need one slot for timestamp */
\r
1213 #endif /* TRACE_SCHEDULING_ONLY */
\r
1215 #endif /* USE_SEPARATE_USER_EVENT_BUFFER == 1 */
\r
1217 /******************************************************************************
\r
1220 * Advanced user events (Professional Edition only)
\r
1222 * Generates User Event with formatted text and data, similar to a "printf".
\r
1223 * It is very fast compared to a normal "printf" since this function only
\r
1224 * stores the arguments. The actual formatting is done
\r
1225 * on the host PC when the trace is displayed in the viewer tool.
\r
1227 * User Event labels are created using xTraceOpenLabel.
\r
1230 * traceLabel adc_uechannel = xTraceOpenLabel("ADC User Events");
\r
1232 * vTracePrint(adc_uechannel,
\r
1233 * "ADC channel %d: %lf volts",
\r
1234 * ch, (double)adc_reading/(double)scale);
\r
1236 * This can be combined into one line, if desired, but this is slower:
\r
1238 * vTracePrint(xTraceOpenLabel("ADC User Events"),
\r
1239 * "ADC channel %d: %lf volts",
\r
1240 * ch, (double)adc_reading/(double)scale);
\r
1242 * Calling xTraceOpenLabel multiple times will not create duplicate entries, but
\r
1243 * it is of course faster to just do it once, and then keep the handle for later
\r
1244 * use. If you don´t have any data arguments, only a text label/string, it is
\r
1245 * better to use vTraceUserEvent - it is faster.
\r
1247 * Format specifiers supported:
\r
1248 * %d - 32 bit signed integer
\r
1249 * %u - 32 bit unsigned integer
\r
1250 * %f - 32 bit float
\r
1251 * %s - string (is copied to the recorder symbol table)
\r
1252 * %hd - 16 bit signed integer
\r
1253 * %hu - 16 bit unsigned integer
\r
1254 * %bd - 8 bit signed integer
\r
1255 * %bu - 8 bit unsigned integer
\r
1256 * %lf - double-precision float (Note! See below...)
\r
1258 * Up to 15 data arguments are allowed, with a total size of maximum 32 byte.
\r
1259 * In case this is exceeded, the user event is changed into an error message.
\r
1261 * The data is stored in trace buffer, and is packed to allow storing multiple
\r
1262 * smaller data entries in the same 4-byte record, e.g., four 8-bit values.
\r
1263 * A string requires two bytes, as the symbol table is limited to 64K. Storing
\r
1264 * a double (%lf) uses two records, so this is quite costly. Use float (%f)
\r
1265 * unless the higher precision is really necessary.
\r
1267 * Note that the double-precision float (%lf) assumes a 64 bit double
\r
1268 * representation. This does not seem to be the case on e.g. PIC24 and PIC32.
\r
1269 * Before using a %lf argument on a 16-bit MCU, please verify that
\r
1270 * "sizeof(double)" actually gives 8 as expected. If not, use %f instead.
\r
1271 ******************************************************************************/
\r
1273 void vTracePrintF(traceLabel eventLabel, const char* formatStr, ...)
\r
1275 #if (TRACE_SCHEDULING_ONLY == 0)
\r
1278 va_start(vl, formatStr);
\r
1279 vTracePrintF_Helper(eventLabel, formatStr, vl);
\r
1281 #endif /* TRACE_SCHEDULING_ONLY */
\r
1284 void vTracePrintF_Helper(traceLabel eventLabel, const char* formatStr, va_list vl)
\r
1286 #if (USE_SEPARATE_USER_EVENT_BUFFER == 0)
\r
1287 uint32_t noOfSlots;
\r
1289 uint32_t tempDataBuffer[(3 + MAX_ARG_SIZE) / 4];
\r
1290 TRACE_SR_ALLOC_CRITICAL_SECTION();
\r
1292 /**************************************************************************
\r
1293 * The array tempDataBuffer is a local buffer used in a two-phase commit of
\r
1294 * the event data, since a vTracePrintF may span over multiple slots in the
\r
1296 * This buffer can be made larger, of course, but remember the risk for
\r
1297 * stack overflow. Note: This should be a LOCAL buffer, must not be made
\r
1298 * global. That would cause data corruption when two calls to vTracePrintF
\r
1299 * from different tasks overlaps (interrupts are only disabled in a small
\r
1300 * part of this function, otherwise enabled)
\r
1301 ***************************************************************************/
\r
1303 TRACE_ASSERT(formatStr != NULL, "vTracePrintF: formatStr == NULL", );
\r
1305 trcCRITICAL_SECTION_BEGIN();
\r
1307 if (RecorderDataPtr->recorderActive && (! inExcludedTask || nISRactive) && handle_of_last_logged_task)
\r
1309 /* First, write the "primary" user event entry in the local buffer, but
\r
1310 let the event type be "EVENT_BEING_WRITTEN" for now...*/
\r
1312 ue1 = (UserEvent*)(&tempDataBuffer[0]);
\r
1314 ue1->type = EVENT_BEING_WRITTEN; /* Update this as the last step */
\r
1316 noOfSlots = prvTraceUserEventFormat(formatStr, vl, (uint8_t*)tempDataBuffer, 4);
\r
1318 /* Store the format string, with a reference to the channel symbol */
\r
1319 ue1->payload = prvTraceOpenSymbol(formatStr, eventLabel);
\r
1321 ue1->dts = (uint8_t)prvTraceGetDTS(0xFF);
\r
1323 /* prvTraceGetDTS might stop the recorder in some cases... */
\r
1324 if (RecorderDataPtr->recorderActive)
\r
1327 /* If the data does not fit in the remaining main buffer, wrap around to
\r
1328 0 if allowed, otherwise stop the recorder and quit). */
\r
1329 if (RecorderDataPtr->nextFreeIndex + noOfSlots > RecorderDataPtr->maxEvents)
\r
1331 #if (TRACE_RECORDER_STORE_MODE == TRACE_STORE_MODE_RING_BUFFER)
\r
1332 (void)memset(& RecorderDataPtr->eventData[RecorderDataPtr->nextFreeIndex * 4],
\r
1334 (RecorderDataPtr->maxEvents - RecorderDataPtr->nextFreeIndex)*4);
\r
1335 RecorderDataPtr->nextFreeIndex = 0;
\r
1336 RecorderDataPtr->bufferIsFull = 1;
\r
1339 /* Stop recorder, since the event data will not fit in the
\r
1340 buffer and not circular buffer in this case... */
\r
1345 /* Check if recorder has been stopped (i.e., vTraceStop above) */
\r
1346 if (RecorderDataPtr->recorderActive)
\r
1348 /* Check that the buffer to be overwritten does not contain any user
\r
1349 events that would be partially overwritten. If so, they must be "killed"
\r
1350 by replacing the user event and following data with NULL events (i.e.,
\r
1351 using a memset to zero).*/
\r
1352 #if (TRACE_RECORDER_STORE_MODE == TRACE_STORE_MODE_RING_BUFFER)
\r
1353 prvCheckDataToBeOverwrittenForMultiEntryEvents((uint8_t)noOfSlots);
\r
1355 /* Copy the local buffer to the main buffer */
\r
1356 (void)memcpy(& RecorderDataPtr->eventData[RecorderDataPtr->nextFreeIndex * 4],
\r
1360 /* Update the event type, i.e., number of data entries following the
\r
1361 main USER_EVENT entry (Note: important that this is after the memcpy,
\r
1362 but within the critical section!)*/
\r
1363 RecorderDataPtr->eventData[RecorderDataPtr->nextFreeIndex * 4] =
\r
1364 (uint8_t) ( USER_EVENT + noOfSlots - 1 );
\r
1366 /* Update the main buffer event index (already checked that it fits in
\r
1367 the buffer, so no need to check for wrapping)*/
\r
1369 RecorderDataPtr->nextFreeIndex += noOfSlots;
\r
1370 RecorderDataPtr->numEvents += noOfSlots;
\r
1372 if (RecorderDataPtr->nextFreeIndex >= EVENT_BUFFER_SIZE)
\r
1374 #if (TRACE_RECORDER_STORE_MODE == TRACE_STORE_MODE_RING_BUFFER)
\r
1375 /* We have reached the end, but this is a ring buffer. Start from the beginning again. */
\r
1376 RecorderDataPtr->bufferIsFull = 1;
\r
1377 RecorderDataPtr->nextFreeIndex = 0;
\r
1379 /* We have reached the end so we stop. */
\r
1385 #if (TRACE_RECORDER_STORE_MODE == TRACE_STORE_MODE_RING_BUFFER)
\r
1386 /* Make sure the next entry is cleared correctly */
\r
1387 prvCheckDataToBeOverwrittenForMultiEntryEvents(1);
\r
1392 trcCRITICAL_SECTION_END();
\r
1394 #elif (USE_SEPARATE_USER_EVENT_BUFFER == 1)
\r
1395 /* Use the separate user event buffer */
\r
1396 traceLabel formatLabel;
\r
1397 UserEventChannel channel;
\r
1399 if (RecorderDataPtr->recorderActive && (! inExcludedTask || nISRactive) && handle_of_last_logged_task)
\r
1401 formatLabel = xTraceOpenLabel(formatStr);
\r
1403 channel = xTraceRegisterChannelFormat(eventLabel, formatLabel);
\r
1405 prvTraceUserEventHelper1(channel, eventLabel, formatLabel, vl);
\r
1410 /******************************************************************************
\r
1413 * Basic user event (Standard and Professional Edition only)
\r
1415 * Generates a User Event with a text label. The label is created/looked up
\r
1416 * in the symbol table using xTraceOpenLabel.
\r
1417 ******************************************************************************/
\r
1418 void vTraceUserEvent(traceLabel eventLabel)
\r
1420 #if (TRACE_SCHEDULING_ONLY == 0)
\r
1421 #if (USE_SEPARATE_USER_EVENT_BUFFER == 0)
\r
1424 TRACE_SR_ALLOC_CRITICAL_SECTION();
\r
1426 TRACE_ASSERT(eventLabel > 0, "vTraceUserEvent: Invalid value for eventLabel", );
\r
1428 trcCRITICAL_SECTION_BEGIN();
\r
1429 if (RecorderDataPtr->recorderActive && (! inExcludedTask || nISRactive) && handle_of_last_logged_task)
\r
1431 dts1 = (uint8_t)prvTraceGetDTS(0xFF);
\r
1432 ue = (UserEvent*) xTraceNextFreeEventBufferSlot();
\r
1436 ue->type = USER_EVENT;
\r
1437 ue->payload = eventLabel;
\r
1438 prvTraceUpdateCounters();
\r
1441 trcCRITICAL_SECTION_END();
\r
1443 #elif (USE_SEPARATE_USER_EVENT_BUFFER == 1)
\r
1444 UserEventChannel channel;
\r
1445 uint32_t noOfSlots = 1;
\r
1446 uint32_t tempDataBuffer[(3 + MAX_ARG_SIZE) / 4];
\r
1447 if (RecorderDataPtr->recorderActive && (! inExcludedTask || nISRactive) && handle_of_last_logged_task)
\r
1449 channel = xTraceRegisterChannelFormat(0, eventLabel);
\r
1453 /* We are dealing with an unknown channel format pair */
\r
1454 noOfSlots++; /* Also need room for channel and format */
\r
1455 ((uint16_t*)tempDataBuffer)[2] = 0;
\r
1456 ((uint16_t*)tempDataBuffer)[3] = eventLabel;
\r
1459 prvTraceUserEventHelper2(channel, tempDataBuffer, noOfSlots);
\r
1462 #endif /* TRACE_SCHEDULING_ONLY */
\r
1465 /*******************************************************************************
\r
1468 * Creates user event labels for user event channels or for individual events.
\r
1469 * User events can be used to log application events and data for display in
\r
1470 * the visualization tool. A user event is identified by a label, i.e., a string,
\r
1471 * which is stored in the recorder's symbol table.
\r
1472 * When logging a user event, a numeric handle (reference) to this string is
\r
1473 * used to identify the event. This is obtained by calling
\r
1475 * xTraceOpenLabel()
\r
1477 * which adds the string to the symbol table (if not already present)
\r
1478 * and returns the corresponding handle.
\r
1480 * This can be used in two ways:
\r
1482 * 1. The handle is looked up every time, when storing the user event.
\r
1485 * vTraceUserEvent(xTraceOpenLabel("MyUserEvent"));
\r
1487 * 2. The label is registered just once, with the handle stored in an
\r
1488 * application variable - much like using a file handle.
\r
1491 * myEventHandle = xTraceOpenLabel("MyUserEvent");
\r
1493 * vTraceUserEvent(myEventHandle);
\r
1495 * The second option is faster since no lookup is required on each event, and
\r
1496 * therefore recommended for user events that are frequently
\r
1497 * executed and/or located in time-critical code. The lookup operation is
\r
1498 * however fairly fast due to the design of the symbol table.
\r
1499 ******************************************************************************/
\r
1500 traceLabel xTraceOpenLabel(const char* label)
\r
1502 TRACE_ASSERT(label != NULL, "xTraceOpenLabel: label == NULL", (traceLabel)0);
\r
1504 return prvTraceOpenSymbol(label, 0);
\r