]> git.sur5r.net Git - freertos/blob - FreeRTOS-Plus/Source/FreeRTOS-Plus-Trace/trcUser.c
f2d212c3ec81098c199e8df0de2efb344f944bcb
[freertos] / FreeRTOS-Plus / Source / FreeRTOS-Plus-Trace / trcUser.c
1 /*******************************************************************************\r
2  * Tracealyzer v2.7.7 Recorder Library\r
3  * Percepio AB, www.percepio.com\r
4  *\r
5  * trcUser.c\r
6  *\r
7  * The public API of the trace recorder library.\r
8  *\r
9  * Terms of Use\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
18  *\r
19  * Disclaimer\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
33  *\r
34  * Tabs are used for indent in this file (1 tab = 4 spaces)\r
35  *\r
36  * Copyright Percepio AB, 2012-2015.\r
37  * www.percepio.com\r
38  ******************************************************************************/\r
39 #include "FreeRTOS.h"\r
40 #include "task.h"\r
41 \r
42 #include "trcUser.h"\r
43 \r
44 #if (USE_TRACEALYZER_RECORDER == 1)\r
45 \r
46 #include <string.h>\r
47 #include <stdarg.h>\r
48 #include <stdint.h>\r
49 \r
50 TRACE_STOP_HOOK vTraceStopHookPtr = (TRACE_STOP_HOOK)0;\r
51 \r
52 extern uint8_t inExcludedTask;\r
53 extern int8_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
60 \r
61 /*** Private functions *******************************************************/\r
62 void vTracePrintF_Helper(traceLabel eventLabel, const char* formatStr, va_list vl);\r
63 \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
68 #endif\r
69 \r
70 static void prvTraceTaskInstanceFinish(int8_t direct);\r
71 \r
72 \r
73 /*******************************************************************************\r
74  * vTraceInitTraceData\r
75  *\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
80 {\r
81         prvTraceInitTraceData();\r
82 }\r
83 \r
84 /*******************************************************************************\r
85  * vTraceSetRecorderData\r
86  *\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
92 {\r
93         TRACE_ASSERT(pRecorderData != NULL, "vTraceSetTraceData, pRecorderData == NULL", );\r
94         RecorderDataPtr = pRecorderData;\r
95 }\r
96 #endif\r
97 \r
98 /*******************************************************************************\r
99  * vTraceSetStopHook\r
100  *\r
101  * Sets a function to be called when the recorder is stopped.\r
102  ******************************************************************************/\r
103 void vTraceSetStopHook(TRACE_STOP_HOOK stopHookFunction)\r
104 {\r
105         vTraceStopHookPtr = stopHookFunction;\r
106 }\r
107 \r
108 /*******************************************************************************\r
109  * vTraceClear\r
110  *\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
115 {\r
116         TRACE_SR_ALLOC_CRITICAL_SECTION();\r
117         trcCRITICAL_SECTION_BEGIN();\r
118 \r
119         RecorderDataPtr->absTimeLastEventSecond = 0;\r
120 \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
127 \r
128         memset(RecorderDataPtr->eventData, 0, RecorderDataPtr->maxEvents * 4);\r
129 \r
130         handle_of_last_logged_task = 0;\r
131         \r
132         trcCRITICAL_SECTION_END();\r
133 \r
134 }\r
135 \r
136 /*******************************************************************************\r
137  * uiTraceStart\r
138  *\r
139  * Starts the recorder. The recorder will not be started if an error has been\r
140  * indicated using vTraceError, e.g. if any of the Nx constants in trcConfig.h\r
141  * has a too small value (NTASK, NQUEUE, etc).\r
142  *\r
143  * Returns 1 if the recorder was started successfully.\r
144  * Returns 0 if the recorder start was prevented due to a previous internal\r
145  * error. In that case, check vTraceGetLastError to get the error message.\r
146  * Any error message is also presented when opening a trace file.\r
147  ******************************************************************************/\r
148 \r
149 uint32_t uiTraceStart(void)\r
150 {\r
151         objectHandleType handle;\r
152         TRACE_SR_ALLOC_CRITICAL_SECTION();\r
153 \r
154         handle = 0;\r
155 \r
156         if (RecorderDataPtr == NULL)\r
157         {\r
158                 vTraceError("RecorderDataPtr is NULL. Call vTraceInitTraceData() before starting trace.");\r
159                 return 0;\r
160         }\r
161 \r
162         if (traceErrorMessage == NULL)\r
163         {\r
164                 trcCRITICAL_SECTION_BEGIN();\r
165                 RecorderDataPtr->recorderActive = 1;\r
166 \r
167                 handle = TRACE_GET_TASK_NUMBER(TRACE_GET_CURRENT_TASK());\r
168                 if (handle == 0)\r
169                 {\r
170                         /* This occurs if the scheduler is not yet started.\r
171                         This creates a dummy "(startup)" task entry internally in the\r
172                         recorder */\r
173                         handle = xTraceGetObjectHandle(TRACE_CLASS_TASK);\r
174                         vTraceSetObjectName(TRACE_CLASS_TASK, handle, "(startup)");\r
175 \r
176                         vTraceSetPriorityProperty(TRACE_CLASS_TASK, handle, 0);\r
177                 }\r
178 \r
179                 vTraceStoreTaskswitch(handle); /* Register the currently running task */\r
180                 trcCRITICAL_SECTION_END();\r
181         }\r
182 \r
183         return RecorderDataPtr->recorderActive;\r
184 }\r
185 \r
186 /*******************************************************************************\r
187  * vTraceStart\r
188  *\r
189  * Starts the recorder. The recorder will not be started if an error has been\r
190  * indicated using vTraceError, e.g. if any of the Nx constants in trcConfig.h\r
191  * has a too small value (NTASK, NQUEUE, etc).\r
192  *\r
193  * This function is obsolete, but has been saved for backwards compatibility.\r
194  * We recommend using uiTraceStart instead.\r
195  ******************************************************************************/\r
196 void vTraceStart(void)\r
197 {\r
198         (void)uiTraceStart();\r
199 }\r
200 \r
201 /*******************************************************************************\r
202  * vTraceStop\r
203  *\r
204  * Stops the recorder. The recording can be resumed by calling vTraceStart.\r
205  * This does not reset the recorder. Use vTraceClear if that is desired.\r
206  ******************************************************************************/\r
207 void vTraceStop(void)\r
208 {\r
209         RecorderDataPtr->recorderActive = 0;\r
210 \r
211         if (vTraceStopHookPtr != (TRACE_STOP_HOOK)0)\r
212         {\r
213                 (*vTraceStopHookPtr)();                 /* An application call-back function. */\r
214         }\r
215 }\r
216 \r
217 /*******************************************************************************\r
218  * xTraceGetLastError\r
219  *\r
220  * Gives the last error message, if any. NULL if no error message is stored.\r
221  * Any error message is also presented when opening a trace file.\r
222  ******************************************************************************/\r
223 char* xTraceGetLastError(void)\r
224 {\r
225         return traceErrorMessage;\r
226 }\r
227 \r
228 /*******************************************************************************\r
229 * vTraceClearError\r
230 *\r
231 * Removes any previous error message generated by recorder calling vTraceError.\r
232 * By calling this function, it may be possible to start/restart the trace\r
233 * despite errors in the recorder, but there is no guarantee that the trace\r
234 * recorder will work correctly in that case, depending on the type of error.\r
235 ******************************************************************************/\r
236 void vTraceClearError(int resetErrorMessage)\r
237 {\r
238         ( void ) resetErrorMessage;\r
239         traceErrorMessage = NULL;\r
240         RecorderDataPtr->internalErrorOccured = 0;\r
241 }\r
242 \r
243 /*******************************************************************************\r
244  * vTraceGetTraceBuffer\r
245  *\r
246  * Returns a pointer to the recorder data structure. Use this together with\r
247  * uiTraceGetTraceBufferSize if you wish to implement an own store/upload\r
248  * solution, e.g., in case a debugger connection is not available for uploading\r
249  * the data.\r
250  ******************************************************************************/\r
251 void* vTraceGetTraceBuffer(void)\r
252 {\r
253         return RecorderDataPtr;\r
254 }\r
255 \r
256 /*******************************************************************************\r
257  * uiTraceGetTraceBufferSize\r
258  *\r
259  * Gets the size of the recorder data structure. For use together with\r
260  * vTraceGetTraceBuffer if you wish to implement an own store/upload solution,\r
261  * e.g., in case a debugger connection is not available for uploading the data.\r
262  ******************************************************************************/\r
263 uint32_t uiTraceGetTraceBufferSize(void)\r
264 {\r
265         return sizeof(RecorderDataType);\r
266 }\r
267 \r
268 /******************************************************************************\r
269  * prvTraceTaskInstanceFinish.\r
270  *\r
271  * Private common function for the vTraceTaskInstanceFinishXXX functions.\r
272  * \r
273  *****************************************************************************/\r
274 void prvTraceTaskInstanceFinish(int8_t direct)\r
275 {\r
276         TaskInstanceStatusEvent* tis;\r
277         uint8_t dts45;\r
278 \r
279         TRACE_SR_ALLOC_CRITICAL_SECTION();\r
280 \r
281         trcCRITICAL_SECTION_BEGIN();\r
282         if (RecorderDataPtr->recorderActive && (! inExcludedTask || nISRactive) && handle_of_last_logged_task)\r
283         {\r
284                 dts45 = (uint8_t)prvTraceGetDTS(0xFF);\r
285                 tis = (TaskInstanceStatusEvent*) xTraceNextFreeEventBufferSlot();\r
286                 if (tis != NULL)\r
287                 {\r
288                         if (direct == 0)\r
289                                 tis->type = TASK_INSTANCE_FINISHED_NEXT_KSE;\r
290                         else\r
291                                 tis->type = TASK_INSTANCE_FINISHED_DIRECT;\r
292 \r
293                         tis->dts = dts45;\r
294                         prvTraceUpdateCounters();\r
295                 }\r
296         }\r
297         trcCRITICAL_SECTION_END();\r
298 }\r
299 \r
300 /******************************************************************************\r
301  * vTraceTaskInstanceFinish(void)\r
302  *\r
303  * Marks the current task instance as finished on the next kernel call.\r
304  *\r
305  * If that kernel call is blocking, the instance ends after the blocking event\r
306  * and the corresponding return event is then the start of the next instance.\r
307  * If the kernel call is not blocking, the viewer instead splits the current\r
308  * fragment right before the kernel call, which makes this call the first event\r
309  * of the next instance.\r
310  *\r
311  * See also USE_IMPLICIT_IFE_RULES in trcConfig.h\r
312  *\r
313  * Example:\r
314  *\r
315  *              while(1)\r
316  *              {\r
317  *                      xQueueReceive(CommandQueue, &command, timeoutDuration);\r
318  *                      processCommand(command);\r
319  *          vTraceInstanceFinish();\r
320  *              }\r
321  *\r
322  * Note: This is only supported in Tracealyzer tools v2.7 or later\r
323  *\r
324  *****************************************************************************/\r
325 void vTraceTaskInstanceFinish(void)\r
326 {\r
327     prvTraceTaskInstanceFinish(0);\r
328 }\r
329 \r
330 /******************************************************************************\r
331  * vTraceTaskInstanceFinishDirect(void)\r
332  *\r
333  * Marks the current task instance as finished at this very instant.\r
334  * This makes the viewer to splits the current fragment at this point and begin\r
335  * a new actor instance.\r
336  *\r
337  * See also USE_IMPLICIT_IFE_RULES in trcConfig.h\r
338  *\r
339  * Example:\r
340  *\r
341  *              This example will generate two instances for each loop iteration.\r
342  *              The first instance ends at vTraceInstanceFinishDirect(), while the second\r
343  *      instance ends at the next xQueueReceive call.\r
344  *\r
345  *              while (1)\r
346  *              {\r
347  *          xQueueReceive(CommandQueue, &command, timeoutDuration);\r
348  *                      ProcessCommand(command);\r
349  *                      vTraceInstanceFinishDirect();\r
350  *                      DoSometingElse();\r
351  *          vTraceInstanceFinish();\r
352  *      }\r
353  *\r
354  * Note: This is only supported in Tracealyzer tools v2.7 or later\r
355  *\r
356  *****************************************************************************/\r
357 void vTraceTaskInstanceFinishDirect(void)\r
358 {\r
359         prvTraceTaskInstanceFinish(1);\r
360 }\r
361 \r
362 /*******************************************************************************\r
363  * Interrupt recording functions\r
364  ******************************************************************************/\r
365 \r
366 #if (INCLUDE_ISR_TRACING == 1)\r
367 \r
368 #define MAX_ISR_NESTING 16\r
369 static uint8_t isrstack[MAX_ISR_NESTING];\r
370 \r
371 /*******************************************************************************\r
372  * vTraceSetISRProperties\r
373  *\r
374  * Registers an Interrupt Service Routine in the recorder library, This must be\r
375  * called before using vTraceStoreISRBegin to store ISR events. This is\r
376  * typically called in the startup of the system, before the scheduler is\r
377  * started.\r
378  *\r
379  * Example:\r
380  *       #define ID_ISR_TIMER1 1                // lowest valid ID is 1\r
381  *       #define PRIO_OF_ISR_TIMER1 3 // the hardware priority of the interrupt\r
382  *       ...\r
383  *       vTraceSetISRProperties(ID_ISR_TIMER1, "ISRTimer1", PRIO_OF_ISR_TIMER1);\r
384  *       ...\r
385  *       void ISR_handler()\r
386  *       {\r
387  *               vTraceStoreISRBegin(ID_OF_ISR_TIMER1);\r
388  *               ...\r
389  *               vTraceStoreISREnd(0);\r
390  *       }\r
391  *\r
392  * NOTE: To safely record ISRs, you need to make sure that all traced\r
393  * interrupts actually are disabled by trcCRITICAL_SECTION_BEGIN(). However,\r
394  * in some ports this does not disable high priority interrupts!\r
395  * If an ISR calls vTraceStoreISRBegin while the recorder is busy, it will\r
396  * stop the recording and give an error message.\r
397  ******************************************************************************/\r
398 void vTraceSetISRProperties(objectHandleType handle, const char* name, char priority)\r
399 {\r
400         TRACE_ASSERT(handle <= RecorderDataPtr->ObjectPropertyTable.NumberOfObjectsPerClass[TRACE_CLASS_ISR], "vTraceSetISRProperties: Invalid value for handle", );\r
401         TRACE_ASSERT(name != NULL, "vTraceSetISRProperties: name == NULL", );\r
402 \r
403         vTraceSetObjectName(TRACE_CLASS_ISR, handle, name);\r
404         vTraceSetPriorityProperty(TRACE_CLASS_ISR, handle, priority);\r
405 }\r
406 \r
407 #if (SELECTED_PORT == PORT_ARM_CortexM)\r
408 /******************************************************************************\r
409  * (Advanced...)\r
410  *\r
411  * ISR_TAILCHAINING_THRESHOLD (For Cortex-M devices only)\r
412  *\r
413  * ARM Cortex-M devices may execute ISRs back-to-back (tail-chained) without\r
414  * resuming the previous context in between. Since this is decided in\r
415  * hardware, we can only detect this indirectly, in the following manner:\r
416  *\r
417  * When entering vTraceStoreISRBegin, we check the number of CPU cycles since\r
418  * the last return of vTraceStoreISREnd. If less or equal to the constant\r
419  * ISR_TAILCHAINING_THRESHOLD it is assumed that the ISRs executed back-to-back\r
420  * (tail-chained). In that case, the previously stored RESUME event\r
421  * (pointed to by ptrLastISRExitEvent) is then deleted to avoid showing a\r
422  * fragment of the previous context in between the ISR events. The delete is\r
423  * made by replacing the event code with a XTS16L event, that serves to keep\r
424  * the differential timestamp from the earlier event.\r
425  *\r
426  * The value of ISR_TAILCHAINING_THRESHOLD depends on the interrupt latency of\r
427  * the processor, on the compiler and on the compiler settings, but should be\r
428  * around 70 cycles. The default value is 66 cycles, which should be correct when\r
429  * using GCC with optimizations disabled (-O0) and Cortex-M3/M4.\r
430  *\r
431  * To measure this value, see MEASURE_ISR_TAILCHAINING_THRESHOLD below.\r
432  *\r
433  * If this value set too low, tail-chained ISRs will incorrectly be shown\r
434  * separated, with a short fragment of the previous task or ISR in between.\r
435  * If this value is set too high, you get the opposite effect - separate ISRs\r
436  * will appear to execute tail-chained and will appear to have higher execution\r
437  * time and response time (maximum ISR_TAILCHAINING_THRESHOLD cycles more).\r
438  *\r
439  * Read the blog post explaining this on our website:\r
440  * http://percepio.com/2014/05/06/sw-based-exc-tracing-arm-cortex-m/\r
441  *\r
442  *****************************************************************************/\r
443 #define ISR_TAILCHAINING_THRESHOLD 66\r
444 \r
445 uint8_t* ptrLastISRExitEvent = NULL;\r
446 uint32_t DWTCycleCountAtLastISRExit = 0;\r
447 \r
448 /******************************************************************************\r
449  * (Advanced...)\r
450  *\r
451  * MEASURE_ISR_TAILCHAINING_THRESHOLD (For Cortex-M devices only)\r
452  *\r
453  * Allows for calibrating the value of ISR_TAILCHAINING_THRESHOLD (see above).\r
454  *\r
455  * This is intended to measure the minimum number of clock cycles from the end\r
456  * of vTraceStoreISREnd to the beginning of the following vTraceStoreISRBegin.\r
457  * For this purpose, we assume a test setup using the SysTick interrupt, which\r
458  * is available on most Cortex-M devices and typically used by the RTOS kernel.\r
459  * To do the measurement, follow these steps:\r
460  *\r
461  * 1. Make sure MEASURE_ISR_TAILCHAINING_THRESHOLD is enabled (defined as 1)\r
462  *\r
463  * 2. Temporarily replace your SysTick handler with the following:\r
464  *\r
465  *      void xPortSysTickHandler( void )\r
466  *      {\r
467  *              vTraceStoreISRBegin(1);\r
468  *              vTraceStoreISREnd(0);\r
469  *      }\r
470  *\r
471  * 3. To make sure that the ISRs execute back-to-back, increase the OS tick\r
472  *      frequency to a very high level so that the OS tick interrupt execute\r
473  *      continuously with no application tasks in between, e.g. 10 MHz.\r
474  *\r
475  * 4. Put a breakpoint in the highest priority task and make sure it is not\r
476  *      reached. This means that the SysTick handler is executing at maximum rate\r
477  *      and thereby tail-chained, where the interrupt latency is 6 cycles.\r
478  *\r
479  * 5. Let the system run without breakpoints and inspect the value of\r
480  *      threshold_low_watermark. This is the minimum total latency observed.\r
481  *      The hardware latency is 6 clock cycles due to the tail-chaining, so the\r
482  *      software latency (SL) is then SL = threshold_low_watermark - 6.\r
483  *\r
484  * The threshold value ISR_TAILCHAINING_THRESHOLD should be SL + 2 * HL, where\r
485  * HL is the normal hardware interrupt latency, i.e., the number of CPU\r
486  * cycles to enter or exit the exception handler for an exception in task\r
487  * context. The HL value is 12-16 depending on core, as shown below.\r
488  *\r
489  * Values for ISR_TAILCHAINING_THRESHOLD, assuming SL = 42\r
490  *      Cortex-M3 and M4 (HL = 12):     66 cycles\r
491  *      Cortex-M0 (HL = 16):            74 cycles\r
492  *      Cortex-M0+ (HL = 15):           72 cycles\r
493  *\r
494  * If the ISR_TAILCHAINING_THRESHOLD value is set too low, some tail-chained\r
495  * ISRs be shown separated, with a short fragment of the previous context\r
496  * in between. On the other hand, if the value is set too high, ISRs that \r
497  * actually are separated may appear to execute back-to-back (tail-chained).\r
498  *\r
499  * Read the blog post explaining this on our website:\r
500  * http://percepio.com/2014/05/06/sw-based-exc-tracing-arm-cortex-m/\r
501  *\r
502  *****************************************************************************/\r
503 #define MEASURE_ISR_TAILCHAINING_THRESHOLD 1\r
504 \r
505 #if (MEASURE_ISR_TAILCHAINING_THRESHOLD == 1)\r
506 volatile uint32_t threshold_low_watermark = 2000000000;\r
507 #endif\r
508 \r
509 #endif\r
510 \r
511 /*******************************************************************************\r
512  * vTraceStoreISRBegin\r
513  *\r
514  * Registers the beginning of an Interrupt Service Routine.\r
515  *\r
516  * Example:\r
517  *       #define ID_ISR_TIMER1 1                // lowest valid ID is 1\r
518  *       #define PRIO_OF_ISR_TIMER1 3 // the hardware priority of the interrupt\r
519  *       ...\r
520  *       vTraceSetISRProperties(ID_ISR_TIMER1, "ISRTimer1", PRIO_OF_ISR_TIMER1);\r
521  *       ...\r
522  *       void ISR_handler()\r
523  *       {\r
524  *               vTraceStoreISRBegin(ID_OF_ISR_TIMER1);\r
525  *               ...\r
526  *               vTraceStoreISREnd(0);\r
527  *       }\r
528  *\r
529  ******************************************************************************/\r
530 void vTraceStoreISRBegin(objectHandleType handle)\r
531 {\r
532         uint16_t dts4;\r
533         #if (SELECTED_PORT == PORT_ARM_CortexM)\r
534         uint32_t CPUCyclesSinceLastISRExit = REG_DWT_CYCCNT - DWTCycleCountAtLastISRExit;\r
535         #endif\r
536         TSEvent* ts;\r
537         TRACE_SR_ALLOC_CRITICAL_SECTION();\r
538 \r
539         ts = NULL;\r
540 \r
541 #if (SELECTED_PORT == PORT_ARM_CortexM)\r
542         if (DWTCycleCountAtLastISRExit > 0)\r
543         {\r
544                 #if (MEASURE_ISR_TAILCHAINING_THRESHOLD == 1)\r
545                 /* Allows for verifying the value of ISR_TAILCHAINING_THRESHOLD */\r
546                 if (CPUCyclesSinceLastISRExit < threshold_low_watermark)\r
547                 {\r
548                         threshold_low_watermark = CPUCyclesSinceLastISRExit;\r
549                 }\r
550                 #endif\r
551 \r
552                 if (CPUCyclesSinceLastISRExit <= ISR_TAILCHAINING_THRESHOLD)\r
553                 {\r
554                         /* This is judged to be a case of ISR tail-chaining since the\r
555                         number of cycles since the last vTraceStoreISREnd is shorter or equal to\r
556                         the threshold (ISR_TAILCHAINING_THRESHOLD) */\r
557 \r
558                         if (ptrLastISRExitEvent != NULL)\r
559                         {\r
560                                 /* Overwrite the last ISR exit event with a "neutral" event that only\r
561                                         accounts for the time passed */\r
562                                 *ptrLastISRExitEvent = XTS16L;\r
563                         }\r
564                 }\r
565 \r
566         }\r
567 #endif\r
568 \r
569         if (recorder_busy)\r
570         {\r
571          vTraceError("Illegal call to vTraceStoreISRBegin, recorder busy!");\r
572          return;\r
573         }\r
574         trcCRITICAL_SECTION_BEGIN();\r
575         if (RecorderDataPtr->recorderActive && handle_of_last_logged_task)\r
576         {\r
577 \r
578                 TRACE_ASSERT(handle <= RecorderDataPtr->ObjectPropertyTable.NumberOfObjectsPerClass[TRACE_CLASS_ISR], "vTraceStoreISRBegin: Invalid value for handle", );\r
579 \r
580                 dts4 = (uint16_t)prvTraceGetDTS(0xFFFF);\r
581 \r
582                 if (RecorderDataPtr->recorderActive) /* Need to repeat this check! */\r
583                 {\r
584                         if (nISRactive < MAX_ISR_NESTING)\r
585                         {\r
586                                 uint8_t hnd8 = prvTraceGet8BitHandle(handle);\r
587                                 isrstack[nISRactive] = handle;\r
588                                 nISRactive++;\r
589                                 ts = (TSEvent*)xTraceNextFreeEventBufferSlot();\r
590                                 if (ts != NULL)\r
591                                 {\r
592                                         ts->type = TS_ISR_BEGIN;\r
593                                         ts->dts = dts4;\r
594                                         ts->objHandle = hnd8;\r
595                                         prvTraceUpdateCounters();\r
596                                 }\r
597                         }\r
598                         else\r
599                         {\r
600                                 /* This should not occur unless something is very wrong */\r
601                                 vTraceError("Too many nested interrupts!");\r
602                         }\r
603                 }\r
604         }\r
605         trcCRITICAL_SECTION_END();\r
606 }\r
607 \r
608 /*******************************************************************************\r
609  * vTraceStoreISREnd\r
610  *\r
611  * Registers the end of an Interrupt Service Routine.\r
612  *\r
613  * The parameter pendingISR indicates if the interrupt has requested a\r
614  * task-switch (= 1) or if the interrupt returns to the earlier context (= 0)\r
615  *\r
616  * Example:\r
617  *\r
618  *       #define ID_ISR_TIMER1 1                // lowest valid ID is 1\r
619  *       #define PRIO_OF_ISR_TIMER1 3 // the hardware priority of the interrupt\r
620  *       ...\r
621  *       vTraceSetISRProperties(ID_ISR_TIMER1, "ISRTimer1", PRIO_OF_ISR_TIMER1);\r
622  *       ...\r
623  *       void ISR_handler()\r
624  *       {\r
625  *               vTraceStoreISRBegin(ID_OF_ISR_TIMER1);\r
626  *               ...\r
627  *               vTraceStoreISREnd(0);\r
628  *       }\r
629  *\r
630  ******************************************************************************/\r
631 void vTraceStoreISREnd(int pendingISR)\r
632 {\r
633         TSEvent* ts;\r
634         uint16_t dts5;\r
635         TRACE_SR_ALLOC_CRITICAL_SECTION();\r
636 \r
637         if (! RecorderDataPtr->recorderActive ||  ! handle_of_last_logged_task)\r
638         {\r
639                 return;\r
640         }\r
641 \r
642         if (recorder_busy)\r
643         {\r
644                 vTraceError("Illegal call to vTraceStoreISREnd, recorder busy!");\r
645                 return;\r
646         }\r
647         \r
648         if (nISRactive == 0)\r
649         {\r
650                 vTraceError("Unmatched call to vTraceStoreISREnd (nISRactive == 0, expected > 0)");\r
651                 return;\r
652         }\r
653 \r
654         trcCRITICAL_SECTION_BEGIN();\r
655         if (pendingISR == 0)\r
656         {\r
657                 uint8_t hnd8, type;\r
658                 dts5 = (uint16_t)prvTraceGetDTS(0xFFFF);\r
659 \r
660                 if (nISRactive > 1)\r
661                 {\r
662                         /* return to another isr */\r
663                         type = TS_ISR_RESUME;\r
664                         hnd8 = prvTraceGet8BitHandle(isrstack[nISRactive]);\r
665                 }\r
666                 else\r
667                 {\r
668                         /* return to task */\r
669                         type = TS_TASK_RESUME;\r
670                         hnd8 = prvTraceGet8BitHandle(handle_of_last_logged_task);\r
671                 }\r
672 \r
673                 ts = (TSEvent*)xTraceNextFreeEventBufferSlot();\r
674                 if (ts != NULL)\r
675                 {\r
676                         ts->type = type;\r
677                         ts->objHandle = hnd8;\r
678                         ts->dts = dts5;\r
679                         prvTraceUpdateCounters();\r
680                 }\r
681 \r
682                 #if (SELECTED_PORT == PORT_ARM_CortexM)\r
683                 /* 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
684                 ptrLastISRExitEvent = (uint8_t*)ts;\r
685                 #endif          \r
686         }\r
687         nISRactive--;\r
688 \r
689         #if (SELECTED_PORT == PORT_ARM_CortexM)\r
690         DWTCycleCountAtLastISRExit = REG_DWT_CYCCNT;\r
691         #endif\r
692 \r
693         trcCRITICAL_SECTION_END();\r
694 }\r
695 \r
696 #else\r
697 \r
698 /* ISR tracing is turned off */\r
699 void vTraceIncreaseISRActive(void)\r
700 {\r
701         if (RecorderDataPtr->recorderActive && handle_of_last_logged_task)\r
702                 nISRactive++;\r
703 }\r
704 \r
705 void vTraceDecreaseISRActive(void)\r
706 {\r
707         if (RecorderDataPtr->recorderActive && handle_of_last_logged_task)\r
708                 nISRactive--;\r
709 }\r
710 #endif\r
711 \r
712 \r
713 /********************************************************************************/\r
714 /* User Event functions                                                                                                                 */\r
715 /********************************************************************************/\r
716 \r
717 #if (INCLUDE_USER_EVENTS == 1)\r
718 \r
719 #define MAX_ARG_SIZE (4+32)\r
720 /*** Locally used in vTracePrintF ***/\r
721 static uint8_t writeInt8(void * buffer, uint8_t i, uint8_t value)\r
722 {\r
723         TRACE_ASSERT(buffer != NULL, "writeInt8: buffer == NULL", 0);\r
724 \r
725         if (i >= MAX_ARG_SIZE)\r
726         {\r
727                 return 255;\r
728         }\r
729 \r
730         ((uint8_t*)buffer)[i] = value;\r
731 \r
732         if (i + 1 > MAX_ARG_SIZE)\r
733         {\r
734                 return 255;\r
735         }\r
736 \r
737         return i + 1;\r
738 }\r
739 \r
740 /*** Locally used in vTracePrintF ***/\r
741 static uint8_t writeInt16(void * buffer, uint8_t i, uint16_t value)\r
742 {\r
743         TRACE_ASSERT(buffer != NULL, "writeInt16: buffer == NULL", 0);\r
744 \r
745         /* Align to multiple of 2 */\r
746         while ((i % 2) != 0)\r
747         {\r
748                 if (i >= MAX_ARG_SIZE)\r
749                 {\r
750                         return 255;\r
751                 }\r
752 \r
753                 ((uint8_t*)buffer)[i] = 0;\r
754                 i++;\r
755         }\r
756 \r
757         if (i + 2 > MAX_ARG_SIZE)\r
758         {\r
759                 return 255;\r
760         }\r
761 \r
762         ((uint16_t*)buffer)[i/2] = value;\r
763 \r
764         return i + 2;\r
765 }\r
766 \r
767 /*** Locally used in vTracePrintF ***/\r
768 static uint8_t writeInt32(void * buffer, uint8_t i, uint32_t value)\r
769 {\r
770         TRACE_ASSERT(buffer != NULL, "writeInt32: buffer == NULL", 0);\r
771 \r
772         /* A 32 bit value should begin at an even 4-byte address */\r
773         while ((i % 4) != 0)\r
774         {\r
775                 if (i >= MAX_ARG_SIZE)\r
776                 {\r
777                         return 255;\r
778                 }\r
779 \r
780                 ((uint8_t*)buffer)[i] = 0;\r
781                 i++;\r
782         }\r
783 \r
784         if (i + 4 > MAX_ARG_SIZE)\r
785         {\r
786                 return 255;\r
787         }\r
788 \r
789         ((uint32_t*)buffer)[i/4] = value;\r
790 \r
791         return i + 4;\r
792 }\r
793 \r
794 #if (INCLUDE_FLOAT_SUPPORT)\r
795 \r
796 /*** Locally used in vTracePrintF ***/\r
797 static uint8_t writeFloat(void * buffer, uint8_t i, float value)\r
798 {\r
799         TRACE_ASSERT(buffer != NULL, "writeFloat: buffer == NULL", 0);\r
800 \r
801         /* A 32 bit value should begin at an even 4-byte address */\r
802         while ((i % 4) != 0)\r
803         {\r
804                 if (i >= MAX_ARG_SIZE)\r
805                 {\r
806                         return 255;\r
807                 }\r
808 \r
809                 ((uint8_t*)buffer)[i] = 0;\r
810                 i++;\r
811         }\r
812 \r
813         if (i + 4 > MAX_ARG_SIZE)\r
814         {\r
815                 return 255;\r
816         }\r
817 \r
818         ((float*)buffer)[i/4] = value;\r
819 \r
820         return i + 4;\r
821 }\r
822 \r
823 /*** Locally used in vTracePrintF ***/\r
824 static uint8_t writeDouble(void * buffer, uint8_t i, double value)\r
825 {\r
826         uint32_t * dest;\r
827         uint32_t * src = (uint32_t*)&value;\r
828 \r
829         TRACE_ASSERT(buffer != NULL, "writeDouble: buffer == NULL", 0);\r
830 \r
831         /* The double is written as two 32 bit values, and should begin at an even\r
832         4-byte address (to avoid having to align with 8 byte) */\r
833         while (i % 4 != 0)\r
834         {\r
835                 if (i >= MAX_ARG_SIZE)\r
836                 {\r
837                         return 255;\r
838                 }\r
839 \r
840                 ((uint8_t*)buffer)[i] = 0;\r
841                 i++;\r
842         }\r
843 \r
844         if (i + 8 > MAX_ARG_SIZE)\r
845         {\r
846                 return 255;\r
847         }\r
848 \r
849         dest = &(((uint32_t *)buffer)[i/4]);\r
850 \r
851         dest[0] = src[0];\r
852         dest[1] = src[1];\r
853 \r
854         return i + 8;\r
855 }\r
856 \r
857 #endif\r
858 \r
859 /*******************************************************************************\r
860  * prvTraceUserEventFormat\r
861  *\r
862  * Parses the format string and stores the arguments in the buffer.\r
863  ******************************************************************************/\r
864 static uint8_t prvTraceUserEventFormat(const char* formatStr, va_list vl, uint8_t* buffer, uint8_t byteOffset)\r
865 {\r
866         uint16_t formatStrIndex = 0;\r
867         uint8_t argCounter = 0;\r
868         uint8_t i = byteOffset;\r
869 \r
870         while (formatStr[formatStrIndex] != '\0')\r
871         {\r
872                 if (formatStr[formatStrIndex] == '%')\r
873                 {\r
874                         argCounter++;\r
875 \r
876                         if (argCounter > 15)\r
877                         {\r
878                                 vTraceError("vTracePrintF - Too many arguments, max 15 allowed!");\r
879                                 return 0;\r
880                         }\r
881 \r
882                         /*******************************************************************************\r
883                         * These below code writes raw data (primitive datatypes) in the event buffer,\r
884                         * instead of the normal event structs (where byte 0 is event type).\r
885                         * These data entries must never be interpreted as real event data, as the type\r
886                         * field would be misleading since used for payload data.\r
887                         *\r
888                         * The correctness of this encoding depends on two mechanisms:\r
889                         *\r
890                         * 1. An initial USER_EVENT, which type code tells the number of 32-bit data\r
891                         * entires that follows. (code - USER_EVENT = number of data entries).\r
892                         * Note that a data entry corresponds to the slots that normally corresponds to\r
893                         * one (1) event, i.e., 32 bits. vTracePrintF may encode several pieces of data\r
894                         * in one data entry, e.g., two 16-bit values or four 8-bit values, one 16-bit\r
895                         * value followed by two 8-bit values, etc.\r
896                         *\r
897                         * 2. A two-phase commit procedure, where the USER_EVENT and data entries are\r
898                         * written to a local buffer at first, and when all checks are OK then copied to\r
899                         * the main event buffer using a fast memcpy. The event code is finalized as the\r
900                         * very last step. Before that step, the event code indicates an unfinished\r
901                         * event, which causes it to be ignored and stop the loading of the file (since\r
902                         * an unfinished event is the last event in the trace).\r
903                         *******************************************************************************/\r
904                         formatStrIndex++;\r
905 \r
906                         while ((formatStr[formatStrIndex] >= '0' && formatStr[formatStrIndex] <= '9') || formatStr[formatStrIndex] == '#' || formatStr[formatStrIndex] == '.')\r
907                                 formatStrIndex++;\r
908 \r
909                         if (formatStr[formatStrIndex] != '\0')\r
910                         {\r
911                                 switch (formatStr[formatStrIndex])\r
912                                 {\r
913                                         case 'd':       i = writeInt32( buffer,\r
914                                                                                                 i,\r
915                                                                                                 (uint32_t)va_arg(vl, uint32_t));\r
916                                                                 break;\r
917                                         case 'x':\r
918                                         case 'X':\r
919                                         case 'u':       i = writeInt32( buffer,\r
920                                                                                                 i,\r
921                                                                                                 (uint32_t)va_arg(vl, uint32_t));\r
922                                                                 break;\r
923                                         case 's':       i = writeInt16( buffer,\r
924                                                                                                 i,\r
925                                                                                                 (uint16_t)xTraceOpenLabel((char*)va_arg(vl, char*)));\r
926                                                                 break;\r
927 \r
928 #if (INCLUDE_FLOAT_SUPPORT)\r
929                                         /* Yes, "double" as type also in the float\r
930                                         case. This since "float" is promoted into "double"\r
931                                         by the va_arg stuff. */\r
932                                         case 'f':       i = writeFloat( buffer,\r
933                                                                                                 i,\r
934                                                                                                 (float)va_arg(vl, double));\r
935                                                                 break;\r
936 #else\r
937                                         /* No support for floats, but attempt to store a float user event\r
938                                         avoid a possible crash due to float reference. Instead store the\r
939                                         data on uint_32 format (will not be displayed anyway). This is just\r
940                                         to keep va_arg and i consistent. */\r
941 \r
942                                         case 'f':       i = writeInt32( buffer,\r
943                                                                                                 i,\r
944                                                                                                 (uint32_t)va_arg(vl, double));\r
945                                                                 break;\r
946 #endif\r
947                                         case 'l':\r
948                                                                 formatStrIndex++;\r
949                                                                 switch (formatStr[formatStrIndex])\r
950                                                                 {\r
951 #if (INCLUDE_FLOAT_SUPPORT)\r
952                                                                         case 'f':       i = writeDouble(buffer,\r
953                                                                                                                                 i,\r
954                                                                                                                                 (double)va_arg(vl, double));\r
955                                                                                                 break;\r
956 #else\r
957                                                                         /* No support for floats, but attempt to store a float user event\r
958                                                                         avoid a possible crash due to float reference. Instead store the\r
959                                                                         data on uint_32 format (will not be displayed anyway). This is just\r
960                                                                         to keep va_arg and i consistent. */\r
961                                                                         case 'f':       i = writeInt32( buffer, /* In this case, the value will not be shown anyway */\r
962                                                                                                                                 i,\r
963                                                                                                                                 (uint32_t)va_arg(vl, double));\r
964 \r
965                                                                                                 i = writeInt32( buffer, /* Do it twice, to write in total 8 bytes */\r
966                                                                                                                                 i,\r
967                                                                                                                                 (uint32_t)va_arg(vl, double));\r
968                                                                                 break;\r
969 #endif\r
970 \r
971                                                                 }\r
972                                                                 break;\r
973                                         case 'h':\r
974                                                                 formatStrIndex++;\r
975                                                                 switch (formatStr[formatStrIndex])\r
976                                                                 {\r
977                                                                         case 'd':       i = writeInt16( buffer,\r
978                                                                                                                                 i,\r
979                                                                                                                                 (uint16_t)va_arg(vl, uint32_t));\r
980                                                                                                 break;\r
981                                                                         case 'u':       i = writeInt16( buffer,\r
982                                                                                                                                 i,\r
983                                                                                                                                 (uint16_t)va_arg(vl, uint32_t));\r
984                                                                                                 break;\r
985                                                                 }\r
986                                                                 break;\r
987                                         case 'b':\r
988                                                                 formatStrIndex++;\r
989                                                                 switch (formatStr[formatStrIndex])\r
990                                                                 {\r
991                                                                         case 'd':       i = writeInt8(  buffer,\r
992                                                                                                                                 i,\r
993                                                                                                                                 (uint8_t)va_arg(vl, uint32_t));\r
994                                                                                                 break;\r
995 \r
996                                                                         case 'u':       i = writeInt8(  buffer,\r
997                                                                                                                                 i,\r
998                                                                                                                                 (uint8_t)va_arg(vl, uint32_t));\r
999                                                                                                 break;\r
1000                                                                 }\r
1001                                                                 break;\r
1002                                 }\r
1003                         }\r
1004                         else\r
1005                                 break;\r
1006                 }\r
1007                 formatStrIndex++;\r
1008                 if (i == 255)\r
1009                 {\r
1010                         vTraceError("vTracePrintF - Too large arguments, max 32 byte allowed!");\r
1011                         return 0;\r
1012                 }\r
1013         }\r
1014         return (i+3)/4;\r
1015 }\r
1016 \r
1017 #if (USE_SEPARATE_USER_EVENT_BUFFER == 1)\r
1018 \r
1019 /*******************************************************************************\r
1020  * prvTraceClearChannelBuffer\r
1021  *\r
1022  * Clears a number of items in the channel buffer, starting from nextSlotToWrite.\r
1023  ******************************************************************************/\r
1024 static void prvTraceClearChannelBuffer(uint32_t count)\r
1025 {\r
1026         uint32_t slots;\r
1027 \r
1028         TRACE_ASSERT(USER_EVENT_BUFFER_SIZE >= count,\r
1029                 "prvTraceClearChannelBuffer: USER_EVENT_BUFFER_SIZE is too small to handle this event.", );\r
1030 \r
1031         /* Check if we're close to the end of the buffer */\r
1032         if (RecorderDataPtr->userEventBuffer.nextSlotToWrite + count > USER_EVENT_BUFFER_SIZE)\r
1033         {\r
1034                 slots = USER_EVENT_BUFFER_SIZE - RecorderDataPtr->userEventBuffer.nextSlotToWrite; /* Number of slots before end of buffer */\r
1035                 (void)memset(&RecorderDataPtr->userEventBuffer.channelBuffer[RecorderDataPtr->userEventBuffer.nextSlotToWrite], 0, slots);\r
1036                 (void)memset(&RecorderDataPtr->userEventBuffer.channelBuffer[0], 0, (count - slots));\r
1037         }\r
1038         else\r
1039                 (void)memset(&RecorderDataPtr->userEventBuffer.channelBuffer[RecorderDataPtr->userEventBuffer.nextSlotToWrite], 0, count);\r
1040 }\r
1041 \r
1042 /*******************************************************************************\r
1043  * prvTraceCopyToDataBuffer\r
1044  *\r
1045  * Copies a number of items to the data buffer, starting from nextSlotToWrite.\r
1046  ******************************************************************************/\r
1047 static void prvTraceCopyToDataBuffer(uint32_t* data, uint32_t count)\r
1048 {\r
1049         TRACE_ASSERT(data != NULL,\r
1050                 "prvTraceCopyToDataBuffer: data == NULL.", );\r
1051         TRACE_ASSERT(count <= USER_EVENT_BUFFER_SIZE,\r
1052                 "prvTraceCopyToDataBuffer: USER_EVENT_BUFFER_SIZE is too small to handle this event.", );\r
1053 \r
1054         uint32_t slots;\r
1055         /* Check if we're close to the end of the buffer */\r
1056         if (RecorderDataPtr->userEventBuffer.nextSlotToWrite + count > USER_EVENT_BUFFER_SIZE)\r
1057         {\r
1058                 slots = USER_EVENT_BUFFER_SIZE - RecorderDataPtr->userEventBuffer.nextSlotToWrite; /* Number of slots before end of buffer */\r
1059                 (void)memcpy(&RecorderDataPtr->userEventBuffer.dataBuffer[RecorderDataPtr->userEventBuffer.nextSlotToWrite * 4], data, slots * 4);\r
1060                 (void)memcpy(&RecorderDataPtr->userEventBuffer.dataBuffer[0], data + slots, (count - slots) * 4);\r
1061         }\r
1062         else\r
1063         {\r
1064                 (void)memcpy(&RecorderDataPtr->userEventBuffer.dataBuffer[RecorderDataPtr->userEventBuffer.nextSlotToWrite * 4], data, count * 4);\r
1065         }\r
1066 }\r
1067 \r
1068 /*******************************************************************************\r
1069  * prvTraceUserEventHelper1\r
1070  *\r
1071  * Calls on prvTraceUserEventFormat() to do the actual formatting, then goes on\r
1072  * to the next helper function.\r
1073  ******************************************************************************/\r
1074 static void prvTraceUserEventHelper1(UserEventChannel channel, traceLabel eventLabel, traceLabel formatLabel, va_list vl)\r
1075 {\r
1076         uint32_t data[(3 + MAX_ARG_SIZE) / 4];\r
1077         uint8_t byteOffset = 4; /* Need room for timestamp */\r
1078         uint8_t noOfSlots;\r
1079 \r
1080         if (channel == 0)\r
1081         {\r
1082                 /* We are dealing with an unknown channel format pair */\r
1083                 byteOffset += 4; /* Also need room for channel and format */\r
1084                 ((uint16_t*)data)[2] = eventLabel;\r
1085                 ((uint16_t*)data)[3] = formatLabel;\r
1086         }\r
1087 \r
1088         noOfSlots = prvTraceUserEventFormat((char*)&(RecorderDataPtr->SymbolTable.symbytes[formatLabel+4]), vl, (uint8_t*)data, byteOffset);\r
1089 \r
1090         prvTraceUserEventHelper2(channel, data, noOfSlots);\r
1091 }\r
1092 \r
1093 /*******************************************************************************\r
1094  * prvTraceUserEventHelper2\r
1095  *\r
1096  * This function simply copies the data buffer to the actual user event buffer.\r
1097  ******************************************************************************/\r
1098 static void prvTraceUserEventHelper2(UserEventChannel channel, uint32_t* data, uint32_t noOfSlots)\r
1099 {\r
1100         static uint32_t old_timestamp = 0;\r
1101         uint32_t old_nextSlotToWrite = 0;\r
1102 \r
1103         TRACE_ASSERT(USER_EVENT_BUFFER_SIZE >= noOfSlots, "vTracePrintF: USER_EVENT_BUFFER_SIZE is too small to handle this event.", );\r
1104 \r
1105         trcCRITICAL_SECTION_BEGIN();\r
1106         /* Store the timestamp */\r
1107         vTracePortGetTimeStamp(data);\r
1108 \r
1109         if (*data < old_timestamp)\r
1110         {\r
1111                 RecorderDataPtr->userEventBuffer.wraparoundCounter++;\r
1112         }\r
1113 \r
1114         old_timestamp = *data;\r
1115 \r
1116         /* Start by erasing any information in the channel buffer */\r
1117         prvTraceClearChannelBuffer(noOfSlots);\r
1118 \r
1119         prvTraceCopyToDataBuffer(data, noOfSlots); /* Will wrap around the data if necessary */\r
1120 \r
1121         old_nextSlotToWrite = RecorderDataPtr->userEventBuffer.nextSlotToWrite; /* Save the index that we want to write the channel data at when we're done */\r
1122         RecorderDataPtr->userEventBuffer.nextSlotToWrite = (RecorderDataPtr->userEventBuffer.nextSlotToWrite + noOfSlots) % USER_EVENT_BUFFER_SIZE; /* Make sure we never end up outside the buffer */\r
1123 \r
1124         /* Write to the channel buffer to indicate that this user event is ready to be used */\r
1125         if (channel != 0)\r
1126         {\r
1127                 RecorderDataPtr->userEventBuffer.channelBuffer[old_nextSlotToWrite] = channel;\r
1128         }\r
1129         else\r
1130         {\r
1131                 /* 0xFF indicates that this is not a normal channel id */\r
1132                 RecorderDataPtr->userEventBuffer.channelBuffer[old_nextSlotToWrite] = (UserEventChannel)0xFF;\r
1133         }\r
1134         trcCRITICAL_SECTION_END();\r
1135 }\r
1136 \r
1137 /*******************************************************************************\r
1138  * xTraceRegisterChannelFormat\r
1139  *\r
1140  * Attempts to create a pair of the channel and format string.\r
1141  *\r
1142  * Note: This is only available if USE_SEPARATE_USER_EVENT_BUFFER is enabled in\r
1143  * trcConfig.h\r
1144  ******************************************************************************/\r
1145 UserEventChannel xTraceRegisterChannelFormat(traceLabel channel, traceLabel formatStr)\r
1146 {\r
1147         uint8_t i;\r
1148         UserEventChannel retVal = 0;\r
1149 \r
1150         TRACE_ASSERT(formatStr != 0, "vTraceRegisterChannelFormat: formatStr == 0", (UserEventChannel)0);\r
1151 \r
1152         trcCRITICAL_SECTION_BEGIN();\r
1153         for (i = 1; i <= CHANNEL_FORMAT_PAIRS; i++) /* Size of the channels buffer is CHANNEL_FORMAT_PAIRS + 1. Index 0 is unused. */\r
1154         {\r
1155                 if(RecorderDataPtr->userEventBuffer.channels[i].name == 0 && RecorderDataPtr->userEventBuffer.channels[i].defaultFormat == 0)\r
1156                 {\r
1157                         /* Found empty slot */\r
1158                         RecorderDataPtr->userEventBuffer.channels[i].name = channel;\r
1159                         RecorderDataPtr->userEventBuffer.channels[i].defaultFormat = formatStr;\r
1160                         retVal = (UserEventChannel)i;\r
1161                         break;\r
1162                 }\r
1163 \r
1164                 if (RecorderDataPtr->userEventBuffer.channels[i].name == channel && RecorderDataPtr->userEventBuffer.channels[i].defaultFormat == formatStr)\r
1165                 {\r
1166                         /* Found a match */\r
1167                         retVal = (UserEventChannel)i;\r
1168                         break;\r
1169                 }\r
1170         }\r
1171         trcCRITICAL_SECTION_END();\r
1172         return retVal;\r
1173 }\r
1174 \r
1175 /******************************************************************************\r
1176  * vTraceChannelPrintF\r
1177  *\r
1178  * Slightly faster version of vTracePrintF() due to no lookups.\r
1179  *\r
1180  * Note: This is only available if USE_SEPARATE_USER_EVENT_BUFFER is enabled in\r
1181  * trcConfig.h\r
1182  *\r
1183  ******************************************************************************/\r
1184 void vTraceChannelPrintF(UserEventChannel channelPair, ...)\r
1185 {\r
1186 #if (TRACE_SCHEDULING_ONLY == 0)\r
1187         va_list vl;\r
1188 \r
1189         va_start(vl, channelPair);\r
1190         vTraceChannelPrintF_Helper(channelPair, vl);\r
1191         va_end(vl);\r
1192 #endif /* TRACE_SCHEDULING_ONLY */\r
1193 }\r
1194 \r
1195 void vTraceChannelPrintF_Helper(UserEventChannel channelPair, va_list vl)\r
1196 {\r
1197         traceLabel channel;\r
1198         traceLabel formatStr;\r
1199 \r
1200         TRACE_ASSERT(channelPair != 0, "vTraceChannelPrintF: channelPair == 0", );\r
1201         TRACE_ASSERT(channelPair <= CHANNEL_FORMAT_PAIRS, "vTraceChannelPrintF: ", );\r
1202 \r
1203         channel = RecorderDataPtr->userEventBuffer.channels[channelPair].name;\r
1204         formatStr = RecorderDataPtr->userEventBuffer.channels[channelPair].defaultFormat;\r
1205 \r
1206         prvTraceUserEventHelper1(channelPair, channel, formatStr, vl);\r
1207 }\r
1208 \r
1209 /******************************************************************************\r
1210  * vTraceChannelUserEvent\r
1211  *\r
1212  * Slightly faster version of vTraceUserEvent() due to no lookups.\r
1213  ******************************************************************************/\r
1214 void vTraceChannelUserEvent(UserEventChannel channelPair)\r
1215 {\r
1216 #if (TRACE_SCHEDULING_ONLY == 0)\r
1217         uint32_t data[(3 + MAX_ARG_SIZE) / 4];\r
1218 \r
1219         TRACE_ASSERT(channelPair != 0, "vTraceChannelPrintF: channelPair == 0", );\r
1220         TRACE_ASSERT(channelPair <= CHANNEL_FORMAT_PAIRS, "vTraceChannelPrintF: ", );\r
1221 \r
1222         prvTraceUserEventHelper2(channelPair, data, 1); /* Only need one slot for timestamp */\r
1223 #endif /* TRACE_SCHEDULING_ONLY */\r
1224 }\r
1225 #endif /* USE_SEPARATE_USER_EVENT_BUFFER == 1 */\r
1226 \r
1227 /******************************************************************************\r
1228  * vTracePrintF\r
1229  *\r
1230  * Advanced user events (Professional Edition only)\r
1231  *\r
1232  * Generates User Event with formatted text and data, similar to a "printf".\r
1233  * It is very fast compared to a normal "printf" since this function only\r
1234  * stores the arguments. The actual formatting is done\r
1235  * on the host PC when the trace is displayed in the viewer tool.\r
1236  *\r
1237  * User Event labels are created using xTraceOpenLabel.\r
1238  * Example:\r
1239  *\r
1240  *       traceLabel adc_uechannel = xTraceOpenLabel("ADC User Events");\r
1241  *       ...\r
1242  *       vTracePrint(adc_uechannel,\r
1243  *                               "ADC channel %d: %lf volts",\r
1244  *                               ch, (double)adc_reading/(double)scale);\r
1245  *\r
1246  * This can be combined into one line, if desired, but this is slower:\r
1247  *\r
1248  *       vTracePrint(xTraceOpenLabel("ADC User Events"),\r
1249  *                               "ADC channel %d: %lf volts",\r
1250  *                               ch, (double)adc_reading/(double)scale);\r
1251  *\r
1252  * Calling xTraceOpenLabel multiple times will not create duplicate entries, but\r
1253  * it is of course faster to just do it once, and then keep the handle for later\r
1254  * use. If you don´t have any data arguments, only a text label/string, it is\r
1255  * better to use vTraceUserEvent - it is faster.\r
1256  *\r
1257  * Format specifiers supported:\r
1258  * %d - 32 bit signed integer\r
1259  * %u - 32 bit unsigned integer\r
1260  * %f - 32 bit float\r
1261  * %s - string (is copied to the recorder symbol table)\r
1262  * %hd - 16 bit signed integer\r
1263  * %hu - 16 bit unsigned integer\r
1264  * %bd - 8 bit signed integer\r
1265  * %bu - 8 bit unsigned integer\r
1266  * %lf - double-precision float (Note! See below...)\r
1267  *\r
1268  * Up to 15 data arguments are allowed, with a total size of maximum 32 byte.\r
1269  * In case this is exceeded, the user event is changed into an error message.\r
1270  *\r
1271  * The data is stored in trace buffer, and is packed to allow storing multiple\r
1272  * smaller data entries in the same 4-byte record, e.g., four 8-bit values.\r
1273  * A string requires two bytes, as the symbol table is limited to 64K. Storing\r
1274  * a double (%lf) uses two records, so this is quite costly. Use float (%f)\r
1275  * unless the higher precision is really necessary.\r
1276  *\r
1277  * Note that the double-precision float (%lf) assumes a 64 bit double\r
1278  * representation. This does not seem to be the case on e.g. PIC24 and PIC32.\r
1279  * Before using a %lf argument on a 16-bit MCU, please verify that\r
1280  * "sizeof(double)" actually gives 8 as expected. If not, use %f instead.\r
1281  ******************************************************************************/\r
1282 \r
1283 void vTracePrintF(traceLabel eventLabel, const char* formatStr, ...)\r
1284 {\r
1285 #if (TRACE_SCHEDULING_ONLY == 0)\r
1286         va_list vl;\r
1287 \r
1288         va_start(vl, formatStr);\r
1289         vTracePrintF_Helper(eventLabel, formatStr, vl);\r
1290         va_end(vl);\r
1291 #endif /* TRACE_SCHEDULING_ONLY */\r
1292 }\r
1293 \r
1294 void vTracePrintF_Helper(traceLabel eventLabel, const char* formatStr, va_list vl)\r
1295 {\r
1296 #if (USE_SEPARATE_USER_EVENT_BUFFER == 0)\r
1297         uint32_t noOfSlots;\r
1298         UserEvent* ue1;\r
1299         uint32_t tempDataBuffer[(3 + MAX_ARG_SIZE) / 4];\r
1300         TRACE_SR_ALLOC_CRITICAL_SECTION();\r
1301 \r
1302         /**************************************************************************\r
1303         * The array tempDataBuffer is a local buffer used in a two-phase commit of\r
1304         * the event data, since a vTracePrintF may span over multiple slots in the\r
1305         * buffer.\r
1306         * This buffer can be made larger, of course, but remember the risk for\r
1307         * stack overflow. Note: This should be a LOCAL buffer, must not be made\r
1308         * global. That would cause data corruption when two calls to vTracePrintF\r
1309         * from different tasks overlaps (interrupts are only disabled in a small\r
1310         * part of this function, otherwise enabled)\r
1311         ***************************************************************************/\r
1312 \r
1313         TRACE_ASSERT(formatStr != NULL, "vTracePrintF: formatStr == NULL", );\r
1314 \r
1315         trcCRITICAL_SECTION_BEGIN();\r
1316 \r
1317         if (RecorderDataPtr->recorderActive && (! inExcludedTask || nISRactive) && handle_of_last_logged_task)\r
1318         {\r
1319                 /* First, write the "primary" user event entry in the local buffer, but\r
1320                 let the event type be "EVENT_BEING_WRITTEN" for now...*/\r
1321 \r
1322                 ue1 = (UserEvent*)(&tempDataBuffer[0]);\r
1323 \r
1324                 ue1->type = EVENT_BEING_WRITTEN;         /* Update this as the last step */\r
1325 \r
1326                 noOfSlots = prvTraceUserEventFormat(formatStr, vl, (uint8_t*)tempDataBuffer, 4);\r
1327 \r
1328                 /* Store the format string, with a reference to the channel symbol */\r
1329                 ue1->payload = prvTraceOpenSymbol(formatStr, eventLabel);\r
1330 \r
1331                 ue1->dts = (uint8_t)prvTraceGetDTS(0xFF);\r
1332 \r
1333                  /* prvTraceGetDTS might stop the recorder in some cases... */\r
1334                 if (RecorderDataPtr->recorderActive)\r
1335                 {\r
1336 \r
1337                         /* If the data does not fit in the remaining main buffer, wrap around to\r
1338                         0 if allowed, otherwise stop the recorder and quit). */\r
1339                         if (RecorderDataPtr->nextFreeIndex + noOfSlots > RecorderDataPtr->maxEvents)\r
1340                         {\r
1341                                 #if (TRACE_RECORDER_STORE_MODE == TRACE_STORE_MODE_RING_BUFFER)\r
1342                                 (void)memset(& RecorderDataPtr->eventData[RecorderDataPtr->nextFreeIndex * 4],\r
1343                                                 0,\r
1344                                                 (RecorderDataPtr->maxEvents - RecorderDataPtr->nextFreeIndex)*4);\r
1345                                 RecorderDataPtr->nextFreeIndex = 0;\r
1346                                 RecorderDataPtr->bufferIsFull = 1;\r
1347                                 #else\r
1348 \r
1349                                 /* Stop recorder, since the event data will not fit in the\r
1350                                 buffer and not circular buffer in this case... */\r
1351                                 vTraceStop();\r
1352                                 #endif\r
1353                         }\r
1354 \r
1355                         /* Check if recorder has been stopped (i.e., vTraceStop above) */\r
1356                         if (RecorderDataPtr->recorderActive)\r
1357                         {\r
1358                                 /* Check that the buffer to be overwritten does not contain any user\r
1359                                 events that would be partially overwritten. If so, they must be "killed"\r
1360                                 by replacing the user event and following data with NULL events (i.e.,\r
1361                                 using a memset to zero).*/\r
1362                                 #if (TRACE_RECORDER_STORE_MODE == TRACE_STORE_MODE_RING_BUFFER)\r
1363                                 prvCheckDataToBeOverwrittenForMultiEntryEvents((uint8_t)noOfSlots);\r
1364                                 #endif\r
1365                                 /* Copy the local buffer to the main buffer */\r
1366                                 (void)memcpy(& RecorderDataPtr->eventData[RecorderDataPtr->nextFreeIndex * 4],\r
1367                                                 tempDataBuffer,\r
1368                                                 noOfSlots * 4);\r
1369 \r
1370                                 /* Update the event type, i.e., number of data entries following the\r
1371                                 main USER_EVENT entry (Note: important that this is after the memcpy,\r
1372                                 but within the critical section!)*/\r
1373                                 RecorderDataPtr->eventData[RecorderDataPtr->nextFreeIndex * 4] =\r
1374                                  (uint8_t) ( USER_EVENT + noOfSlots - 1 );\r
1375 \r
1376                                 /* Update the main buffer event index (already checked that it fits in\r
1377                                 the buffer, so no need to check for wrapping)*/\r
1378 \r
1379                                 RecorderDataPtr->nextFreeIndex += noOfSlots;\r
1380                                 RecorderDataPtr->numEvents += noOfSlots;\r
1381 \r
1382                                 if (RecorderDataPtr->nextFreeIndex >= EVENT_BUFFER_SIZE)\r
1383                                 {\r
1384                                         #if (TRACE_RECORDER_STORE_MODE == TRACE_STORE_MODE_RING_BUFFER)\r
1385                                         /* We have reached the end, but this is a ring buffer. Start from the beginning again. */\r
1386                                         RecorderDataPtr->bufferIsFull = 1;\r
1387                                         RecorderDataPtr->nextFreeIndex = 0;\r
1388                                         #else\r
1389                                         /* We have reached the end so we stop. */\r
1390                                         vTraceStop();\r
1391                                         #endif\r
1392                                 }\r
1393                         }\r
1394 \r
1395                         #if (TRACE_RECORDER_STORE_MODE == TRACE_STORE_MODE_RING_BUFFER)\r
1396                         /* Make sure the next entry is cleared correctly */\r
1397                         prvCheckDataToBeOverwrittenForMultiEntryEvents(1);\r
1398                         #endif\r
1399 \r
1400                 }\r
1401         }\r
1402         trcCRITICAL_SECTION_END();\r
1403 \r
1404 #elif (USE_SEPARATE_USER_EVENT_BUFFER == 1)\r
1405         /* Use the separate user event buffer */\r
1406         traceLabel formatLabel;\r
1407         UserEventChannel channel;\r
1408 \r
1409         if (RecorderDataPtr->recorderActive && (! inExcludedTask || nISRactive) && handle_of_last_logged_task)\r
1410         {\r
1411                 formatLabel = xTraceOpenLabel(formatStr);\r
1412 \r
1413                 channel = xTraceRegisterChannelFormat(eventLabel, formatLabel);\r
1414 \r
1415                 prvTraceUserEventHelper1(channel, eventLabel, formatLabel, vl);\r
1416         }\r
1417 #endif\r
1418 }\r
1419 \r
1420 /******************************************************************************\r
1421  * vTraceUserEvent\r
1422  *\r
1423  * Basic user event (Standard and Professional Edition only)\r
1424  *\r
1425  * Generates a User Event with a text label. The label is created/looked up\r
1426  * in the symbol table using xTraceOpenLabel.\r
1427  ******************************************************************************/\r
1428 void vTraceUserEvent(traceLabel eventLabel)\r
1429 {\r
1430 #if (TRACE_SCHEDULING_ONLY == 0)\r
1431 #if (USE_SEPARATE_USER_EVENT_BUFFER == 0)\r
1432         UserEvent* ue;\r
1433         uint8_t dts1;\r
1434         TRACE_SR_ALLOC_CRITICAL_SECTION();\r
1435 \r
1436         TRACE_ASSERT(eventLabel > 0, "vTraceUserEvent: Invalid value for eventLabel", );\r
1437 \r
1438         trcCRITICAL_SECTION_BEGIN();\r
1439         if (RecorderDataPtr->recorderActive && (! inExcludedTask || nISRactive) && handle_of_last_logged_task)\r
1440         {\r
1441                 dts1 = (uint8_t)prvTraceGetDTS(0xFF);\r
1442                 ue = (UserEvent*) xTraceNextFreeEventBufferSlot();\r
1443                 if (ue != NULL)\r
1444                 {\r
1445                         ue->dts = dts1;\r
1446                         ue->type = USER_EVENT;\r
1447                         ue->payload = eventLabel;\r
1448                         prvTraceUpdateCounters();\r
1449                 }\r
1450         }\r
1451         trcCRITICAL_SECTION_END();\r
1452 \r
1453 #elif (USE_SEPARATE_USER_EVENT_BUFFER == 1)\r
1454         UserEventChannel channel;\r
1455         uint32_t noOfSlots = 1;\r
1456         uint32_t tempDataBuffer[(3 + MAX_ARG_SIZE) / 4];\r
1457         if (RecorderDataPtr->recorderActive && (! inExcludedTask || nISRactive) && handle_of_last_logged_task)\r
1458         {\r
1459                 channel = xTraceRegisterChannelFormat(0, eventLabel);\r
1460 \r
1461                 if (channel == 0)\r
1462                 {\r
1463                         /* We are dealing with an unknown channel format pair */\r
1464                         noOfSlots++; /* Also need room for channel and format */\r
1465                         ((uint16_t*)tempDataBuffer)[2] = 0;\r
1466                         ((uint16_t*)tempDataBuffer)[3] = eventLabel;\r
1467                 }\r
1468 \r
1469                 prvTraceUserEventHelper2(channel, tempDataBuffer, noOfSlots);\r
1470         }\r
1471 #endif\r
1472 #endif /* TRACE_SCHEDULING_ONLY */\r
1473 }\r
1474 \r
1475 /*******************************************************************************\r
1476  * xTraceOpenLabel\r
1477  *\r
1478  * Creates user event labels for user event channels or for individual events.\r
1479  * User events can be used to log application events and data for display in\r
1480  * the visualization tool. A user event is identified by a label, i.e., a string,\r
1481  * which is stored in the recorder's symbol table.\r
1482  * When logging a user event, a numeric handle (reference) to this string is\r
1483  * used to identify the event. This is obtained by calling\r
1484  *\r
1485  *       xTraceOpenLabel()\r
1486  *\r
1487  * which adds the string to the symbol table (if not already present)\r
1488  * and returns the corresponding handle.\r
1489  *\r
1490  * This can be used in two ways:\r
1491  *\r
1492  * 1. The handle is looked up every time, when storing the user event.\r
1493  *\r
1494  * Example:\r
1495  *       vTraceUserEvent(xTraceOpenLabel("MyUserEvent"));\r
1496  *\r
1497  * 2. The label is registered just once, with the handle stored in an\r
1498  * application variable - much like using a file handle.\r
1499  *\r
1500  * Example:\r
1501  *       myEventHandle = xTraceOpenLabel("MyUserEvent");\r
1502  *       ...\r
1503  *       vTraceUserEvent(myEventHandle);\r
1504  *\r
1505  * The second option is faster since no lookup is required on each event, and\r
1506  * therefore recommended for user events that are frequently\r
1507  * executed and/or located in time-critical code. The lookup operation is\r
1508  * however fairly fast due to the design of the symbol table.\r
1509  ******************************************************************************/\r
1510 traceLabel xTraceOpenLabel(const char* label)\r
1511 {\r
1512         TRACE_ASSERT(label != NULL, "xTraceOpenLabel: label == NULL", (traceLabel)0);\r
1513 \r
1514         return prvTraceOpenSymbol(label, 0);\r
1515 }\r
1516 \r
1517 #endif\r
1518 \r
1519 #endif\r