]> git.sur5r.net Git - freertos/blob - FreeRTOS-Plus/Source/FreeRTOS-Plus-Trace/trcUser.c
Update FreeRTOS+Trace recorder code.
[freertos] / FreeRTOS-Plus / Source / FreeRTOS-Plus-Trace / trcUser.c
1 /*******************************************************************************\r
2  * Tracealyzer v2.5.0 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  * Copyright Percepio AB, 2013.\r
35  * www.percepio.com\r
36  ******************************************************************************/\r
37 \r
38 #include "trcUser.h"\r
39 \r
40 #if (USE_TRACEALYZER_RECORDER == 1)\r
41 \r
42 #include <string.h>\r
43 #include <stdarg.h>\r
44 #include <stdint.h>\r
45 \r
46 TRACE_STOP_HOOK vTraceStopHookPtr = (TRACE_STOP_HOOK)0;\r
47 \r
48 extern uint8_t inExcludedTask;\r
49 extern uint8_t nISRactive;\r
50 extern uint8_t handle_of_last_logged_task;\r
51 extern uint32_t dts_min;\r
52 extern uint32_t hwtc_count_max_after_tick;\r
53 extern uint32_t hwtc_count_sum_after_tick;\r
54 extern uint32_t hwtc_count_sum_after_tick_counter;\r
55 extern char* traceErrorMessage;\r
56 \r
57 /*** private functions *******************************************************/\r
58 void vTracePrintF_Helper(traceLabel eventLabel, const char* formatStr, va_list vl);\r
59 \r
60 #if (USE_SEPARATE_USER_EVENT_BUFFER == 1)\r
61 void vTraceChannelPrintF_Helper(UserEventChannel channelPair, va_list vl);\r
62 static void prvTraceUserEventHelper1(UserEventChannel channel, traceLabel eventLabel, traceLabel formatLabel, va_list vl);\r
63 static void prvTraceUserEventHelper2(UserEventChannel channel, uint32_t* data, uint32_t noOfSlots);\r
64 #endif\r
65 /*****************************************************************************/\r
66 \r
67 \r
68 \r
69 /*******************************************************************************\r
70  * vTraceInitTraceData\r
71  *\r
72  * Allocates, if necessary, and initializes the recorder data structure, based\r
73  * on the constants in trcConfig.h.\r
74  ******************************************************************************/\r
75 void vTraceInitTraceData(void)\r
76 {\r
77         prvTraceInitTraceData();\r
78 }\r
79 \r
80 /*******************************************************************************\r
81  * vTraceSetRecorderData\r
82  *\r
83  * If custom allocation is used, this function must be called so the recorder\r
84  * library knows where to save the trace data.\r
85  ******************************************************************************/\r
86 #if TRACE_DATA_ALLOCATION == TRACE_DATA_ALLOCATION_CUSTOM\r
87 void vTraceSetRecorderData(void* pRecorderData)\r
88 {\r
89         TRACE_ASSERT(pRecorderData != NULL, "vTraceSetTraceData, pRecorderData == NULL", );\r
90         RecorderDataPtr = pRecorderData;\r
91 }\r
92 #endif\r
93 \r
94 /*******************************************************************************\r
95  * vTraceClear\r
96  *\r
97  * Resets the recorder. Only necessary if a restart is desired - this is not\r
98  * needed in the startup initialization.\r
99  ******************************************************************************/\r
100 void vTraceClear(void)\r
101 {\r
102     trcCRITICAL_SECTION_BEGIN();\r
103 \r
104     RecorderDataPtr->absTimeLastEvent = 0;\r
105     RecorderDataPtr->nextFreeIndex = 0;\r
106     RecorderDataPtr->numEvents = 0;\r
107     RecorderDataPtr->bufferIsFull = 0;\r
108 \r
109     trcCRITICAL_SECTION_END();\r
110 \r
111 }\r
112 \r
113 /*******************************************************************************\r
114  * uiTraceStart\r
115  *\r
116  * Starts the recorder. The recorder will not be started if an error has been\r
117  * indicated using vTraceError, e.g. if any of the Nx constants in trcConfig.h\r
118  * has a too small value (NTASK, NQUEUE, etc).\r
119  *\r
120  * Returns 1 if the recorder was started successfully.\r
121  * Returns 0 if the recorder start was prevented due to a previous internal\r
122  * error. In that case, check vTraceGetLastError to get the error message.\r
123  * Any error message is also presented when opening a trace file.\r
124  ******************************************************************************/\r
125 \r
126 uint32_t uiTraceStart(void)\r
127 {\r
128         objectHandleType handle = 0;\r
129 \r
130         if (RecorderDataPtr == NULL)\r
131         {\r
132                 vTraceError("RecorderDataPtr is NULL. Call vTraceInitTraceData() before starting trace.");\r
133                 return 0;\r
134         }\r
135     if (traceErrorMessage == NULL)\r
136     {\r
137         trcCRITICAL_SECTION_BEGIN();\r
138         RecorderDataPtr->recorderActive = 1;\r
139 \r
140                 handle = TRACE_GET_TASK_NUMBER(TRACE_GET_CURRENT_TASK());\r
141                 if (handle == 0)\r
142                 {\r
143                         /* This occurs if the scheduler is not yet started.\r
144                         This creates a dummy "(startup)" task entry internally in the\r
145                         recorder */\r
146                         handle = xTraceGetObjectHandle(TRACE_CLASS_TASK);\r
147                         vTraceSetObjectName(TRACE_CLASS_TASK, handle, "(startup)");\r
148 \r
149                         vTraceSetPriorityProperty(TRACE_CLASS_TASK, handle, 0);\r
150                 }\r
151 \r
152         vTraceStoreTaskswitch(handle); /* Register the currently running task */\r
153         trcCRITICAL_SECTION_END();\r
154     }\r
155 \r
156     return RecorderDataPtr->recorderActive;\r
157 }\r
158 \r
159 /*******************************************************************************\r
160  * vTraceStart\r
161  *\r
162  * Starts the recorder. The recorder will not be started if an error has been\r
163  * indicated using vTraceError, e.g. if any of the Nx constants in trcConfig.h\r
164  * has a too small value (NTASK, NQUEUE, etc).\r
165  *\r
166  * This function is obsolete, but has been saved for backwards compatibility.\r
167  * We recommend using uiTraceStart instead.\r
168  ******************************************************************************/\r
169 void vTraceStart(void)\r
170 {\r
171     (void)uiTraceStart();\r
172 }\r
173 \r
174 /*******************************************************************************\r
175  * vTraceStop\r
176  *\r
177  * Stops the recorder. The recording can be resumed by calling vTraceStart.\r
178  * This does not reset the recorder. Use vTraceClear if that is desired.\r
179  ******************************************************************************/\r
180 void vTraceStop(void)\r
181 {\r
182     RecorderDataPtr->recorderActive = 0;\r
183         \r
184         if (vTraceStopHookPtr != (TRACE_STOP_HOOK)0)\r
185         {\r
186                 (*vTraceStopHookPtr)();                      /* Call an application level call back function. */\r
187         }\r
188 }\r
189 \r
190 /*******************************************************************************\r
191  * xTraceGetLastError\r
192  *\r
193  * Gives the last error message, if any. NULL if no error message is stored.\r
194  * Any error message is also presented when opening a trace file.\r
195  ******************************************************************************/\r
196 char* xTraceGetLastError(void)\r
197 {\r
198         return traceErrorMessage;\r
199 }\r
200 \r
201 /*******************************************************************************\r
202 * vTraceClearError\r
203 *\r
204 * Removes any previous error message generated by recorder calling vTraceError. \r
205 * By calling this function, it may be possible to start/restart the trace \r
206 * despite errors in the recorder, but there is no guarantee that the trace \r
207 * recorder will work correctly in that case, depending on the type of error.\r
208 ******************************************************************************/\r
209 void vTraceClearError(int resetErrorMessage)\r
210 {\r
211         traceErrorMessage = NULL;       \r
212         RecorderDataPtr->internalErrorOccured = 0;\r
213 }\r
214 \r
215 /*******************************************************************************\r
216  * vTraceGetTraceBuffer\r
217  *\r
218  * Returns a pointer to the recorder data structure. Use this together with\r
219  * uiTraceGetTraceBufferSize if you wish to implement an own store/upload\r
220  * solution, e.g., in case a debugger connection is not available for uploading\r
221  * the data.\r
222  ******************************************************************************/\r
223 void* vTraceGetTraceBuffer(void)\r
224 {\r
225     return RecorderDataPtr;\r
226 }\r
227 \r
228 /*******************************************************************************\r
229  * uiTraceGetTraceBufferSize\r
230  *\r
231  * Gets the size of the recorder data structure. For use together with\r
232  * vTraceGetTraceBuffer if you wish to implement an own store/upload solution,\r
233  * e.g., in case a debugger connection is not available for uploading the data.\r
234  ******************************************************************************/\r
235 uint32_t uiTraceGetTraceBufferSize(void)\r
236 {\r
237     return sizeof(RecorderDataType);\r
238 }\r
239 \r
240 /******************************************************************************\r
241  * vTraceTaskInstanceIsFinished\r
242  *\r
243  * This defines an explicit Instance Finish Event for the current task. It tells\r
244  * the recorder that the current instance of this task is finished at the \r
245  * context-switch. This function should be called right before the API function \r
246  * call considered to be the Instance Finish Event.\r
247  *****************************************************************************/\r
248 void vTraceTaskInstanceIsFinished()\r
249 {\r
250     if (handle_of_last_logged_task)\r
251     {\r
252                 TRACE_PROPERTY_OBJECT_STATE(TRACE_CLASS_TASK, handle_of_last_logged_task) = 0;\r
253     }\r
254 }\r
255 \r
256 /*******************************************************************************\r
257  * Interrupt recording functions\r
258  ******************************************************************************/\r
259 \r
260 #if (INCLUDE_ISR_TRACING == 1)\r
261 \r
262 #define MAX_ISR_NESTING 16\r
263 static uint8_t isrstack[MAX_ISR_NESTING];\r
264 \r
265 /*******************************************************************************\r
266  * vTraceSetISRProperties\r
267  *\r
268  * Registers an Interrupt Service Routine in the recorder library, This must be\r
269  * called before using vTraceStoreISRBegin to store ISR events. This is\r
270  * typically called in the startup of the system, before the scheduler is\r
271  * started.\r
272  *\r
273  * Example:\r
274  *     #define ID_ISR_TIMER1 1       // lowest valid ID is 1\r
275  *     #define PRIO_OF_ISR_TIMER1 3  // the hardware priority of the interrupt\r
276  *     ...\r
277  *     vTraceSetISRProperties(ID_ISR_TIMER1, "ISRTimer1", PRIO_OF_ISR_TIMER1);\r
278  *     ...\r
279  *     void ISR_handler()\r
280  *     {\r
281  *         vTraceStoreISRBegin(ID_OF_ISR_TIMER1);\r
282  *         ...\r
283  *         vTraceStoreISREnd();\r
284  *     }\r
285  *\r
286  * NOTE: To safely record ISRs, you need to make sure that all traced\r
287  * interrupts actually are disabled by trcCRITICAL_SECTION_BEGIN(). However,\r
288  * in some ports this does not disable high priority interrupts!\r
289  * If an ISR calls vTraceStoreISRBegin while the recorder is busy, it will\r
290  * stop the recording and give an error message.\r
291  ******************************************************************************/\r
292 void vTraceSetISRProperties(objectHandleType handle, const char* name, char priority)\r
293 {\r
294         TRACE_ASSERT(handle <= RecorderDataPtr->ObjectPropertyTable.NumberOfObjectsPerClass[TRACE_CLASS_ISR], "vTraceSetISRProperties: Invalid value for handle", );\r
295         TRACE_ASSERT(name != NULL, "vTraceSetISRProperties: name == NULL", );\r
296         TRACE_ASSERT(priority >= 0, "vTraceSetISRProperties: Invalid value for priority", );\r
297 \r
298     vTraceSetObjectName(TRACE_CLASS_ISR, handle, name);\r
299     vTraceSetPriorityProperty(TRACE_CLASS_ISR, handle, priority);\r
300 }\r
301 \r
302 /*******************************************************************************\r
303  * vTraceStoreISRBegin\r
304  *\r
305  * Registers the beginning of an Interrupt Service Routine.\r
306  *\r
307  * Example:\r
308  *     #define ID_ISR_TIMER1 1       // lowest valid ID is 1\r
309  *     #define PRIO_OF_ISR_TIMER1 3  // the hardware priority of the interrupt\r
310  *     ...\r
311  *     vTraceSetISRProperties(ID_ISR_TIMER1, "ISRTimer1", PRIO_OF_ISR_TIMER1);\r
312  *     ...\r
313  *     void ISR_handler()\r
314  *     {\r
315  *         vTraceStoreISRBegin(ID_OF_ISR_TIMER1);\r
316  *         ...\r
317  *         vTraceStoreISREnd();\r
318  *     }\r
319  *\r
320  * NOTE: You need to make sure that any traced interrupts actually are\r
321  * disabled by trcCRITICAL_SECTION_BEGIN().\r
322  * If an invalid call to vTraceStoreISRBegin is detected (i.e., that preempted\r
323  * a critical section of the recorder) this will generate a recorder error\r
324  * using vTraceError.\r
325  ******************************************************************************/\r
326 void vTraceStoreISRBegin(objectHandleType handle)\r
327 {\r
328     uint16_t dts4;\r
329     TSEvent* ts = NULL;\r
330 \r
331     TRACE_ASSERT(handle <= RecorderDataPtr->ObjectPropertyTable.NumberOfObjectsPerClass[TRACE_CLASS_ISR], "vTraceStoreISRBegin: Invalid value for handle", );\r
332 \r
333     if (recorder_busy)\r
334     {\r
335       vTraceError("Illegal call to vTraceStoreISRBegin, recorder busy!");\r
336       return;\r
337     }\r
338     if (RecorderDataPtr->recorderActive && handle_of_last_logged_task)\r
339     {\r
340         trcCRITICAL_SECTION_BEGIN();\r
341         dts4 = (uint16_t)prvTraceGetDTS(0xFFFF);\r
342 \r
343         if (RecorderDataPtr->recorderActive) /* Need to repeat this check! */\r
344         {\r
345 \r
346             if (nISRactive < MAX_ISR_NESTING)\r
347             {\r
348                 isrstack[nISRactive] = handle;\r
349                 nISRactive++;\r
350                 ts = (TSEvent*)xTraceNextFreeEventBufferSlot();\r
351                 if (ts != NULL)\r
352                 {\r
353                     ts->type = TS_ISR_BEGIN;\r
354                     ts->dts = dts4;\r
355                     ts->objHandle = handle;\r
356                     prvTraceUpdateCounters();\r
357                 }\r
358             }\r
359             else\r
360             {\r
361                 /* This should not occur unless something is very wrong */\r
362                 vTraceError("Too many nested interrupts!");\r
363             }\r
364         }\r
365         trcCRITICAL_SECTION_END();\r
366     }\r
367 }\r
368 \r
369 \r
370 #if (SELECTED_PORT == PORT_ARM_CortexM)\r
371 \r
372 static int tailchain_irq_pending(void);\r
373 \r
374 /*******************************************************************************\r
375  * tailchain_irq_pending\r
376  *\r
377  * For Cortex-M chips only. Returns 1 if an interrupt is pending, by checking\r
378  * the 8 NVIC IRQ pend registers at 0xE000E200 to 0xE000E21C. Returns 0 if no\r
379  * interrupt is pending. This is used to predict tailchaining of ISRs.\r
380  ******************************************************************************/\r
381 static int tailchain_irq_pending(void)\r
382 {\r
383   uint32_t* pend_reg = ((uint32_t*)0xE000E200);\r
384   int i;\r
385 \r
386   for (i=0; i<8; i++)\r
387   {\r
388     if (pend_reg[i] != 0)\r
389     {\r
390       return 1;\r
391     }\r
392   }\r
393   return 0;\r
394 }\r
395 \r
396 #endif\r
397 \r
398 /*******************************************************************************\r
399  * vTraceStoreISREnd\r
400  *\r
401  * Registers the end of an Interrupt Service Routine.\r
402  *\r
403  * Example:\r
404  *     #define ID_ISR_TIMER1 1       // lowest valid ID is 1\r
405  *     #define PRIO_OF_ISR_TIMER1 3  // the hardware priority of the interrupt\r
406  *     ...\r
407  *     vTraceSetISRProperties(ID_ISR_TIMER1, "ISRTimer1", PRIO_OF_ISR_TIMER1);\r
408  *     ...\r
409  *     void ISR_handler()\r
410  *     {\r
411  *         vTraceStoreISRBegin(ID_OF_ISR_TIMER1);\r
412  *         ...\r
413  *         vTraceStoreISREnd();\r
414  *     }\r
415  *\r
416  * NOTE: You need to make sure that any traced interrupts actually are\r
417  * disabled by trcCRITICAL_SECTION_BEGIN().\r
418  * If an invalid call to vTraceStoreISREnd is detected (i.e., that preempted\r
419  * a critical section of the recorder) this will generate a recorder error\r
420  * using vTraceError.\r
421  ******************************************************************************/\r
422 void vTraceStoreISREnd(void)\r
423 {\r
424     TSEvent* ts;\r
425     uint16_t dts5;\r
426 \r
427     if (recorder_busy)\r
428     {\r
429       vTraceError("Illegal call to vTraceStoreISREnd, recorder busy!");\r
430       return;\r
431     }\r
432 \r
433     if (RecorderDataPtr->recorderActive && handle_of_last_logged_task)\r
434     {\r
435         #if (SELECTED_PORT == PORT_ARM_CortexM)\r
436         if (tailchain_irq_pending() > 0)\r
437         {\r
438             nISRactive--; /* If an IRQ strikes exactly here, the resulting\r
439             ISR tailchaining is not detected. The trace instead shows a very\r
440             short fragment of the earlier preempted task/ISR, and then the new\r
441             ISR begins. */\r
442             return;\r
443         }\r
444         #endif\r
445 \r
446         trcCRITICAL_SECTION_BEGIN();\r
447         dts5 = (uint16_t)prvTraceGetDTS(0xFFFF);\r
448 \r
449         if (RecorderDataPtr->recorderActive) /* Need to repeat this check! */\r
450         {\r
451             ts = (TSEvent*)xTraceNextFreeEventBufferSlot();\r
452             if (ts != NULL)\r
453             {\r
454                 if (nISRactive > 1)\r
455                 {\r
456                     /* return to another isr */\r
457                     ts->type = TS_ISR_RESUME;\r
458                     ts->objHandle = isrstack[nISRactive];\r
459                 }\r
460                 else\r
461                 {\r
462                     /* return to task */\r
463                     ts->type = TS_TASK_RESUME;\r
464                     ts->objHandle = handle_of_last_logged_task;\r
465                 }\r
466                 ts->dts = dts5;\r
467                 nISRactive--;\r
468                 prvTraceUpdateCounters();\r
469             }\r
470         }\r
471         trcCRITICAL_SECTION_END();\r
472     }\r
473 }\r
474 \r
475 #else\r
476 \r
477 /* ISR tracing is turned off */\r
478 void vTraceIncreaseISRActive(void)\r
479 {\r
480     if (RecorderDataPtr->recorderActive && handle_of_last_logged_task)\r
481         nISRactive++;\r
482 }\r
483 \r
484 void vTraceDecreaseISRActive(void)\r
485 {\r
486     if (RecorderDataPtr->recorderActive && handle_of_last_logged_task)\r
487         nISRactive--;\r
488 }\r
489 #endif\r
490 \r
491 \r
492 /*******************************************************************************\r
493  * User Event functions\r
494  ******************************************************************************/\r
495 \r
496 #if (INCLUDE_USER_EVENTS == 1)\r
497 \r
498 #define MAX_ARG_SIZE (4+32)\r
499 /*** Locally used in vTracePrintF ***/\r
500 static uint8_t writeInt8(void * buffer, uint8_t i, uint8_t value)\r
501 {\r
502         TRACE_ASSERT(buffer != NULL, "writeInt8: buffer == NULL", 0);\r
503 \r
504     if (i >= MAX_ARG_SIZE)\r
505     {\r
506         return 255;\r
507     }\r
508 \r
509     ((uint8_t*)buffer)[i] = value;\r
510 \r
511         if (i + 1 > MAX_ARG_SIZE)\r
512         {\r
513                 return 255;\r
514         }\r
515 \r
516     return i + 1;\r
517 }\r
518 \r
519 /*** Locally used in vTracePrintF ***/\r
520 static uint8_t writeInt16(void * buffer, uint8_t i, uint16_t value)\r
521 {\r
522         TRACE_ASSERT(buffer != NULL, "writeInt16: buffer == NULL", 0);\r
523 \r
524     /* Align to multiple of 2 */\r
525     while ((i % 2) != 0)\r
526     {\r
527                 if (i >= MAX_ARG_SIZE)\r
528                 {\r
529                         return 255;\r
530                 }\r
531 \r
532         ((uint8_t*)buffer)[i] = 0;\r
533         i++;\r
534     }\r
535 \r
536     if (i + 2 > MAX_ARG_SIZE)\r
537     {\r
538         return 255;\r
539     }\r
540 \r
541     ((uint16_t*)buffer)[i/2] = value;\r
542 \r
543     return i + 2;\r
544 }\r
545 \r
546 /*** Locally used in vTracePrintF ***/\r
547 static uint8_t writeInt32(void * buffer, uint8_t i, uint32_t value)\r
548 {\r
549         TRACE_ASSERT(buffer != NULL, "writeInt32: buffer == NULL", 0);\r
550 \r
551     /* A 32 bit value should begin at an even 4-byte address */\r
552     while ((i % 4) != 0)\r
553     {\r
554                 if (i >= MAX_ARG_SIZE)\r
555                 {\r
556                         return 255;\r
557                 }\r
558 \r
559         ((uint8_t*)buffer)[i] = 0;\r
560         i++;\r
561     }\r
562 \r
563     if (i + 4 > MAX_ARG_SIZE)\r
564     {\r
565         return 255;\r
566     }\r
567 \r
568     ((uint32_t*)buffer)[i/4] = value;\r
569 \r
570     return i + 4;\r
571 }\r
572 \r
573 #if (INCLUDE_FLOAT_SUPPORT)\r
574 \r
575 /*** Locally used in vTracePrintF ***/\r
576 static uint8_t writeFloat(void * buffer, uint8_t i, float value)\r
577 {\r
578         TRACE_ASSERT(buffer != NULL, "writeFloat: buffer == NULL", 0);\r
579 \r
580     /* A 32 bit value should begin at an even 4-byte address */\r
581     while ((i % 4) != 0)\r
582     {\r
583                 if (i >= MAX_ARG_SIZE)\r
584                 {\r
585                         return 255;\r
586                 }\r
587 \r
588         ((uint8_t*)buffer)[i] = 0;\r
589         i++;\r
590     }\r
591 \r
592     if (i + 4 > MAX_ARG_SIZE)\r
593     {\r
594         return 255;\r
595     }\r
596 \r
597     ((float*)buffer)[i/4] = value;\r
598 \r
599     return i + 4;\r
600 }\r
601 \r
602 /*** Locally used in vTracePrintF ***/\r
603 static uint8_t writeDouble(void * buffer, uint8_t i, double value)\r
604 {\r
605         TRACE_ASSERT(buffer != NULL, "writeDouble: buffer == NULL", 0);\r
606 \r
607     uint32_t * dest = buffer;\r
608     uint32_t * src = (void*)&value;\r
609     /* The double is written as two 32 bit values, and should begin at an even\r
610     4-byte address (to avoid having to align with 8 byte) */\r
611     while (i % 4 != 0)\r
612     {\r
613                 if (i >= MAX_ARG_SIZE)\r
614                 {\r
615                         return 255;\r
616                 }\r
617 \r
618         ((uint8_t*)buffer)[i] = 0;\r
619         i++;\r
620     }\r
621 \r
622     if (i + 8 > MAX_ARG_SIZE)\r
623     {\r
624         return 255;\r
625     }\r
626 \r
627     dest[i/4+0] = src[0];\r
628     dest[i/4+1] = src[1];\r
629 \r
630     return i + 8;\r
631 }\r
632 \r
633 #endif\r
634 \r
635 /*******************************************************************************\r
636  * prvTraceUserEventFormat\r
637  *\r
638  * Parses the format string and stores the arguments in the buffer.\r
639  ******************************************************************************/\r
640 static uint8_t prvTraceUserEventFormat(const char* formatStr, va_list vl, uint8_t* buffer, uint8_t byteOffset)\r
641 {\r
642         uint16_t formatStrIndex = 0;\r
643         uint8_t argCounter = 0;\r
644         uint8_t i = byteOffset;\r
645 \r
646         while (formatStr[formatStrIndex] != '\0')\r
647     {\r
648         if (formatStr[formatStrIndex] == '%')\r
649         {\r
650             argCounter++;\r
651 \r
652             if (argCounter > 15)\r
653             {\r
654                 vTraceError("vTracePrintF - Too many arguments, max 15 allowed!");\r
655                 return 0;\r
656             }\r
657 \r
658 /*******************************************************************************\r
659 * These below code writes raw data (primitive datatypes) in the event buffer,\r
660 * instead of the normal event structs (where byte 0 is event type).\r
661 * These data entries must never be interpreted as real event data, as the type\r
662 * field would be misleading since used for payload data.\r
663 *\r
664 * The correctness of this encoding depends on two mechanisms:\r
665 *\r
666 * 1. An initial USER_EVENT, which type code tells the number of 32-bit data\r
667 * entires that follows. (code - USER_EVENT = number of data entries).\r
668 * Note that a data entry corresponds to the slots that normally corresponds to\r
669 * one (1) event, i.e., 32 bits. vTracePrintF may encode several pieces of data\r
670 * in one data entry, e.g., two 16-bit values or four 8-bit values, one 16-bit\r
671 * value followed by two 8-bit values, etc.\r
672 *\r
673 * 2. A two-phase commit procedure, where the USER_EVENT and data entries are\r
674 * written to a local buffer at first, and when all checks are OK then copied to\r
675 * the main event buffer using a fast memcpy. The event code is finalized as the\r
676 * very last step. Before that step, the event code indicates an unfinished\r
677 * event, which causes it to be ignored and stop the loading of the file (since\r
678 * an unfinished event is the last event in the trace).\r
679 *******************************************************************************/\r
680             formatStrIndex++;\r
681 \r
682                         while ((formatStr[formatStrIndex] >= '0' && formatStr[formatStrIndex] <= '9') || formatStr[formatStrIndex] == '#' || formatStr[formatStrIndex] == '.')\r
683                                 formatStrIndex++;\r
684 \r
685                         if (formatStr[formatStrIndex] != '\0')\r
686                         {\r
687                                 switch (formatStr[formatStrIndex])\r
688                                 {\r
689                                 case 'd':    i = writeInt32(buffer,\r
690                                                                                                 i,\r
691                                                                                                 (uint32_t)va_arg(vl, uint32_t));\r
692                                                                 break;\r
693                                 case 'x':\r
694                                 case 'X':\r
695                                 case 'u':    i = writeInt32(buffer,\r
696                                                                                                 i,\r
697                                                                                                 (uint32_t)va_arg(vl, uint32_t));\r
698                                                                 break;\r
699                                 case 's':    i = writeInt16(buffer,\r
700                                                                                                 i,\r
701                                                                                                 (uint16_t)xTraceOpenLabel((char*)va_arg(vl, char*)));\r
702                                                                 break;\r
703 \r
704 #if (INCLUDE_FLOAT_SUPPORT)\r
705                                                                 /* Yes, "double" as type also in the float\r
706                                                                 case. This since "float" is promoted into "double"\r
707                                                                 by the va_arg stuff. */\r
708                                 case 'f':    i = writeFloat(buffer,\r
709                                                                                                 i,\r
710                                                                                                 (float)va_arg(vl, double));\r
711                                                                 break;\r
712 #else\r
713         /* No support for floats, but attempt to store a float user event\r
714         avoid a possible crash due to float reference. Instead store the\r
715         data on uint_32 format (will not be displayed anyway). This is just\r
716         to keep va_arg and i consistent. */\r
717 \r
718                                 case 'f':    i = writeInt32(buffer,\r
719                                                                                                 i,\r
720                                                                                                 (uint32_t)va_arg(vl, double));\r
721                                                                 break;\r
722 #endif\r
723                                 case 'l':\r
724                                         formatStrIndex++;\r
725                                         switch (formatStr[formatStrIndex])\r
726                                         {\r
727 #if (INCLUDE_FLOAT_SUPPORT)\r
728                                         case 'f':     i = writeDouble(buffer,\r
729                                                                                                                 i,\r
730                                                                                                                 (double)va_arg(vl, double));\r
731                                                                 break;\r
732 #else\r
733         /* No support for floats, but attempt to store a float user event\r
734         avoid a possible crash due to float reference. Instead store the\r
735         data on uint_32 format (will not be displayed anyway). This is just\r
736         to keep va_arg and i consistent. */\r
737                                         case 'f':    i = writeInt32(buffer, /* In this case, the value will not be shown anyway */\r
738                                                                                                         i,\r
739                                                                                                         (uint32_t)va_arg(vl, double));\r
740                                                                         i = writeInt32(buffer, /* Do it twice, to write in total 8 bytes */\r
741                                                                                                         i,\r
742                                                                                                         (uint32_t)va_arg(vl, double));\r
743                                                                 break;\r
744 #endif\r
745 \r
746                                         }\r
747                                         break;\r
748                                 case 'h':\r
749                                         formatStrIndex++;\r
750                                         switch (formatStr[formatStrIndex])\r
751                                         {\r
752                                         case 'd':    i = writeInt16(buffer,\r
753                                                                                                         i,\r
754                                                                                                         (uint16_t)va_arg(vl, uint32_t));\r
755                                                                         break;\r
756                                         case 'u':    i = writeInt16(buffer,\r
757                                                                                                         i,\r
758                                                                                                         (uint16_t)va_arg(vl, uint32_t));\r
759                                                                         break;\r
760                                         }\r
761                                         break;\r
762                                 case 'b':\r
763                                         formatStrIndex++;\r
764                                         switch (formatStr[formatStrIndex])\r
765                                         {\r
766                                         case 'd':    i = writeInt8(buffer,\r
767                                                                                                         i,\r
768                                                                                                         (uint8_t)va_arg(vl, uint32_t));\r
769                                                                         break;\r
770                                         case 'u':    i = writeInt8(buffer,\r
771                                                                                                         i,\r
772                                                                                                         (uint8_t)va_arg(vl, uint32_t));\r
773                                                                         break;\r
774                                         }\r
775                                         break;\r
776                                 }\r
777                         }\r
778                         else\r
779                                 break;\r
780         }\r
781         formatStrIndex++;\r
782         if (i == 255)\r
783         {\r
784             vTraceError("vTracePrintF - Too large arguments, max 32 byte allowed!");\r
785             return 0;\r
786         }\r
787     }\r
788         return (i+3)/4;\r
789 }\r
790 \r
791 #if (USE_SEPARATE_USER_EVENT_BUFFER == 1)\r
792 \r
793 /*******************************************************************************\r
794  * prvTraceClearChannelBuffer\r
795  *\r
796  * Clears a number of items in the channel buffer, starting from nextSlotToWrite.\r
797  ******************************************************************************/\r
798 static void prvTraceClearChannelBuffer(uint32_t count)\r
799 {\r
800         uint32_t slots;\r
801 \r
802         TRACE_ASSERT(USER_EVENT_BUFFER_SIZE >= count, "prvTraceClearChannelBuffer: USER_EVENT_BUFFER_SIZE is too small to handle this event.", );\r
803 \r
804         /* Check if we're close to the end of the buffer */\r
805         if (RecorderDataPtr->userEventBuffer.nextSlotToWrite + count > USER_EVENT_BUFFER_SIZE)\r
806         {\r
807                 slots = USER_EVENT_BUFFER_SIZE - RecorderDataPtr->userEventBuffer.nextSlotToWrite; /* Number of slots before end of buffer */\r
808                 (void)memset(&RecorderDataPtr->userEventBuffer.channelBuffer[RecorderDataPtr->userEventBuffer.nextSlotToWrite], 0, slots);\r
809                 (void)memset(&RecorderDataPtr->userEventBuffer.channelBuffer[0], 0, (count - slots));\r
810         }\r
811         else\r
812                 (void)memset(&RecorderDataPtr->userEventBuffer.channelBuffer[RecorderDataPtr->userEventBuffer.nextSlotToWrite], 0, count);\r
813 }\r
814 \r
815 /*******************************************************************************\r
816  * prvTraceCopyToDataBuffer\r
817  *\r
818  * Copies a number of items to the data buffer, starting from nextSlotToWrite.\r
819  ******************************************************************************/\r
820 static void prvTraceCopyToDataBuffer(uint32_t* data, uint32_t count)\r
821 {\r
822         TRACE_ASSERT(data != NULL, "prvTraceCopyToDataBuffer: data == NULL.", );\r
823         TRACE_ASSERT(count <= USER_EVENT_BUFFER_SIZE, "prvTraceCopyToDataBuffer: USER_EVENT_BUFFER_SIZE is too small to handle this event.", );\r
824 \r
825         uint32_t slots;\r
826         /* Check if we're close to the end of the buffer */\r
827         if (RecorderDataPtr->userEventBuffer.nextSlotToWrite + count > USER_EVENT_BUFFER_SIZE)\r
828         {\r
829                 slots = USER_EVENT_BUFFER_SIZE - RecorderDataPtr->userEventBuffer.nextSlotToWrite; /* Number of slots before end of buffer */\r
830                 (void)memcpy(&RecorderDataPtr->userEventBuffer.dataBuffer[RecorderDataPtr->userEventBuffer.nextSlotToWrite * 4], data, slots * 4);\r
831                 (void)memcpy(&RecorderDataPtr->userEventBuffer.dataBuffer[0], data + slots, (count - slots) * 4);\r
832         }\r
833         else\r
834         {\r
835                 (void)memcpy(&RecorderDataPtr->userEventBuffer.dataBuffer[RecorderDataPtr->userEventBuffer.nextSlotToWrite * 4], data, count * 4);\r
836         }\r
837 }\r
838 \r
839 /*******************************************************************************\r
840  * prvTraceUserEventHelper1\r
841  *\r
842  * Calls on prvTraceUserEventFormat() to do the actual formatting, then goes on to the next helper function.\r
843  ******************************************************************************/\r
844 static void prvTraceUserEventHelper1(UserEventChannel channel, traceLabel eventLabel, traceLabel formatLabel, va_list vl)\r
845 {\r
846         uint32_t data[(3 + MAX_ARG_SIZE) / 4];\r
847         uint8_t byteOffset = 4; /* Need room for timestamp */\r
848         uint8_t noOfSlots;\r
849 \r
850         if (channel == 0)\r
851         {\r
852                 /* We are dealing with an unknown channel format pair */\r
853                 byteOffset += 4; /* Also need room for channel and format */\r
854                 ((uint16_t*)data)[2] = eventLabel;\r
855                 ((uint16_t*)data)[3] = formatLabel;\r
856         }\r
857 \r
858         noOfSlots = prvTraceUserEventFormat((char*)&(RecorderDataPtr->SymbolTable.symbytes[formatLabel+4]), vl, (uint8_t*)data, byteOffset);\r
859 \r
860         prvTraceUserEventHelper2(channel, data, noOfSlots);\r
861 }\r
862 \r
863 /*******************************************************************************\r
864  * prvTraceUserEventHelper2\r
865  *\r
866  * This function simply copies the data buffer to the actual user event buffer.\r
867  ******************************************************************************/\r
868 static void prvTraceUserEventHelper2(UserEventChannel channel, uint32_t* data, uint32_t noOfSlots)\r
869 {\r
870         static uint32_t old_timestamp = 0;\r
871         uint32_t old_nextSlotToWrite = 0;\r
872 \r
873         TRACE_ASSERT(USER_EVENT_BUFFER_SIZE >= noOfSlots, "vTracePrintF: USER_EVENT_BUFFER_SIZE is too small to handle this event.", );\r
874 \r
875         trcCRITICAL_SECTION_BEGIN();\r
876         /* Store the timestamp */\r
877         vTracePortGetTimeStamp(data);\r
878 \r
879         if (*data < old_timestamp)\r
880                 RecorderDataPtr->userEventBuffer.wraparoundCounter++;\r
881         old_timestamp = *data;\r
882 \r
883         /* Start by erasing any information in the channel buffer */\r
884         prvTraceClearChannelBuffer(noOfSlots);\r
885 \r
886         prvTraceCopyToDataBuffer(data, noOfSlots); /* Will wrap around the data if necessary */\r
887 \r
888         old_nextSlotToWrite = RecorderDataPtr->userEventBuffer.nextSlotToWrite; /* Save the index that we want to write the channel data at when we're done */\r
889         RecorderDataPtr->userEventBuffer.nextSlotToWrite = (RecorderDataPtr->userEventBuffer.nextSlotToWrite + noOfSlots) % USER_EVENT_BUFFER_SIZE; /* Make sure we never end up outside the buffer */\r
890 \r
891         /* Write to the channel buffer to indicate that this user event is ready to be used */\r
892         if (channel != 0)\r
893                 RecorderDataPtr->userEventBuffer.channelBuffer[old_nextSlotToWrite] = channel;\r
894         else\r
895                 RecorderDataPtr->userEventBuffer.channelBuffer[old_nextSlotToWrite] = (UserEventChannel)0xFF;   /* 0xFF indicates that this is not a normal channel id */\r
896         trcCRITICAL_SECTION_END();\r
897 }\r
898 \r
899 /*******************************************************************************\r
900  * xTraceRegisterChannelFormat\r
901  *\r
902  * Attempts to create a pair of the channel and format string.\r
903  *\r
904  * Note: This is only available if USE_SEPARATE_USER_EVENT_BUFFER is enabled in \r
905  * trcConfig.h\r
906  ******************************************************************************/\r
907 UserEventChannel xTraceRegisterChannelFormat(traceLabel channel, traceLabel formatStr)\r
908 {\r
909         uint8_t i;\r
910         UserEventChannel retVal = 0;\r
911 \r
912         TRACE_ASSERT(formatStr != 0, "vTraceRegisterChannelFormat: formatStr == 0", (UserEventChannel)0);\r
913 \r
914         trcCRITICAL_SECTION_BEGIN();\r
915         for (i = 1; i <= CHANNEL_FORMAT_PAIRS; i++) /* Size of the channels buffer is CHANNEL_FORMAT_PAIRS + 1. Index 0 is unused. */\r
916         {\r
917                 if(RecorderDataPtr->userEventBuffer.channels[i].name == 0 && RecorderDataPtr->userEventBuffer.channels[i].defaultFormat == 0)\r
918                 {\r
919                         /* Found empty slot */\r
920                         RecorderDataPtr->userEventBuffer.channels[i].name = channel;\r
921                         RecorderDataPtr->userEventBuffer.channels[i].defaultFormat = formatStr;\r
922                         retVal = (UserEventChannel)i;\r
923                         break;\r
924                 }\r
925 \r
926                 if (RecorderDataPtr->userEventBuffer.channels[i].name == channel && RecorderDataPtr->userEventBuffer.channels[i].defaultFormat == formatStr)\r
927                 {\r
928                         /* Found a match */\r
929                         retVal = (UserEventChannel)i;\r
930                         break;\r
931                 }\r
932         }\r
933         trcCRITICAL_SECTION_END();\r
934         return retVal;\r
935 }\r
936 \r
937 /******************************************************************************\r
938  * vTraceChannelPrintF\r
939  *\r
940  * Slightly faster version of vTracePrintF() due to no lookups.\r
941  *\r
942  * Note: This is only available if USE_SEPARATE_USER_EVENT_BUFFER is enabled in\r
943  * trcConfig.h\r
944  *\r
945  ******************************************************************************/\r
946 void vTraceChannelPrintF(UserEventChannel channelPair, ...)\r
947 {\r
948         va_list vl;\r
949 \r
950         va_start(vl, channelPair);\r
951         vTraceChannelPrintF_Helper(channelPair, vl);\r
952         va_end(vl);\r
953 }\r
954 \r
955 void vTraceChannelPrintF_Helper(UserEventChannel channelPair, va_list vl)\r
956 {\r
957         traceLabel channel;\r
958         traceLabel formatStr;\r
959 \r
960         TRACE_ASSERT(channelPair != 0, "vTraceChannelPrintF: channelPair == 0", );\r
961         TRACE_ASSERT(channelPair <= CHANNEL_FORMAT_PAIRS, "vTraceChannelPrintF: ", );\r
962 \r
963         channel = RecorderDataPtr->userEventBuffer.channels[channelPair].name;\r
964         formatStr = RecorderDataPtr->userEventBuffer.channels[channelPair].defaultFormat;\r
965 \r
966         prvTraceUserEventHelper1(channelPair, channel, formatStr, vl);\r
967 }\r
968 \r
969 /******************************************************************************\r
970  * vTraceChannelUserEvent\r
971  *\r
972  * Slightly faster version of vTraceUserEvent() due to no lookups.\r
973  ******************************************************************************/\r
974 void vTraceChannelUserEvent(UserEventChannel channelPair)\r
975 {\r
976         uint32_t data[(3 + MAX_ARG_SIZE) / 4];\r
977 \r
978         TRACE_ASSERT(channelPair != 0, "vTraceChannelPrintF: channelPair == 0", );\r
979         TRACE_ASSERT(channelPair <= CHANNEL_FORMAT_PAIRS, "vTraceChannelPrintF: ", );\r
980 \r
981         prvTraceUserEventHelper2(channelPair, data, 1); /* Only need one slot for timestamp */\r
982 }\r
983 #endif /* USE_SEPARATE_USER_EVENT_BUFFER == 1 */\r
984 \r
985  /******************************************************************************\r
986  * vTracePrintF\r
987  *\r
988  * Advanced user events (Professional Edition only)\r
989  *\r
990  * Generates User Event with formatted text and data, similar to a "printf".\r
991  * It is very fast compared to a normal "printf" since this function only\r
992  * stores the arguments. The actual formatting is done\r
993  * on the host PC when the trace is displayed in the viewer tool.\r
994  *\r
995  * User Event labels are created using xTraceOpenLabel.\r
996  * Example:\r
997  *\r
998  *     traceLabel adc_uechannel = xTraceOpenLabel("ADC User Events");\r
999  *     ...\r
1000  *     vTracePrint(adc_uechannel,\r
1001  *                 "ADC channel %d: %lf volts",\r
1002  *                 ch, (double)adc_reading/(double)scale);\r
1003  *\r
1004  * This can be combined into one line, if desired, but this is slower:\r
1005  *\r
1006  *     vTracePrint(xTraceOpenLabel("ADC User Events"),\r
1007  *                 "ADC channel %d: %lf volts",\r
1008  *                 ch, (double)adc_reading/(double)scale);\r
1009  *\r
1010  * Calling xTraceOpenLabel multiple times will not create duplicate entries, but\r
1011  * it is of course faster to just do it once, and then keep the handle for later\r
1012  * use. If you don´t have any data arguments, only a text label/string, it is\r
1013  * better to use vTraceUserEvent - it is faster.\r
1014  *\r
1015  * Format specifiers supported:\r
1016  *  %d - 32 bit signed integer\r
1017  *  %u - 32 bit unsigned integer\r
1018  *  %f - 32 bit float\r
1019  *  %s - string (is copied to the recorder symbol table)\r
1020  *  %hd - 16 bit signed integer\r
1021  *  %hu - 16 bit unsigned integer\r
1022  *  %bd - 8 bit signed integer\r
1023  *  %bu - 8 bit unsigned integer\r
1024  *  %lf - double-precision float\r
1025  *\r
1026  * Up to 15 data arguments are allowed, with a total size of maximum 32 byte.\r
1027  * In case this is exceeded, the user event is changed into an error message.\r
1028  *\r
1029  * The data is stored in trace buffer, and is packed to allow storing multiple\r
1030  * smaller data entries in the same 4-byte record, e.g., four 8-bit values.\r
1031  * A string requires two bytes, as the symbol table is limited to 64K. Storing a\r
1032  * double (%lf) uses two records, so this is quite costly. Use float (%f) unless\r
1033  * the higher precision is really necessary.\r
1034  ******************************************************************************/\r
1035 \r
1036 void vTracePrintF(traceLabel eventLabel, const char* formatStr, ...)\r
1037 {\r
1038         va_list vl;\r
1039 \r
1040         va_start(vl, formatStr);\r
1041         vTracePrintF_Helper(eventLabel, formatStr, vl);\r
1042         va_end(vl);\r
1043 }\r
1044 \r
1045 void vTracePrintF_Helper(traceLabel eventLabel, const char* formatStr, va_list vl)\r
1046 {\r
1047 #if (USE_SEPARATE_USER_EVENT_BUFFER == 0)\r
1048         uint32_t noOfSlots;\r
1049     UserEvent* ue1;\r
1050     uint32_t tempDataBuffer[(3 + MAX_ARG_SIZE) / 4];\r
1051 \r
1052     /**************************************************************************\r
1053     * The array tempDataBuffer is a local buffer used in a two-phase commit of\r
1054     * the event data, since a vTracePrintF may span over multiple slots in the\r
1055     * buffer.\r
1056     * This buffer can be made larger, of course, but remember the risk for\r
1057     * stack overflow. Note: This should be a LOCAL buffer, must not be made\r
1058     * global. That would cause data corruption when two calls to vTracePrintF\r
1059     * from different tasks overlaps (interrupts are only disabled in a small\r
1060     * part of this function, otherwise enabled)\r
1061     ***************************************************************************/\r
1062 \r
1063         TRACE_ASSERT(formatStr != NULL, "vTracePrintF: formatStr == NULL", );\r
1064 \r
1065     if (RecorderDataPtr->recorderActive && (! inExcludedTask || nISRactive) && handle_of_last_logged_task)\r
1066     {\r
1067         /* First, write the "primary" user event entry in the local buffer, but\r
1068         let the event type be "EVENT_BEING_WRITTEN" for now...*/\r
1069 \r
1070         ue1 = (UserEvent*)(&tempDataBuffer[0]);\r
1071         ue1->type = EVENT_BEING_WRITTEN;      /* Update this as the last step */\r
1072 \r
1073         noOfSlots = prvTraceUserEventFormat(formatStr, vl, (uint8_t*)tempDataBuffer, 4);\r
1074 \r
1075         /* Store the format string, with a reference to the channel symbol */\r
1076         ue1->payload = prvTraceOpenSymbol(formatStr, eventLabel);\r
1077 \r
1078         trcCRITICAL_SECTION_BEGIN();\r
1079 \r
1080         ue1->dts = (uint8_t)prvTraceGetDTS(0xFF);\r
1081         if (! RecorderDataPtr->recorderActive)\r
1082         {\r
1083 \r
1084             /* Abort, since an XTS event (created by prvTraceGetDTS) filled the\r
1085             buffer, and the recorder stopped since not circular buffer. */\r
1086             trcCRITICAL_SECTION_END();\r
1087 \r
1088             return;\r
1089         }\r
1090 \r
1091         /* If the data does not fit in the remaining main buffer, wrap around to\r
1092         0 if allowed, otherwise stop the recorder and quit). */\r
1093         if (RecorderDataPtr->nextFreeIndex + noOfSlots > RecorderDataPtr->maxEvents)\r
1094         {\r
1095 #if (TRACE_RECORDER_STORE_MODE == TRACE_STORE_MODE_RING_BUFFER)\r
1096             (void)memset(& RecorderDataPtr->eventData[RecorderDataPtr->nextFreeIndex * 4],\r
1097                    0,\r
1098                    (RecorderDataPtr->maxEvents - RecorderDataPtr->nextFreeIndex)*4);\r
1099             RecorderDataPtr->nextFreeIndex = 0;\r
1100             RecorderDataPtr->bufferIsFull = 1;\r
1101 #else\r
1102             /* Abort and stop recorder, since the event data will not fit in the\r
1103             buffer and not circular buffer in this case... */\r
1104             trcCRITICAL_SECTION_END();\r
1105             vTraceStop();\r
1106 \r
1107 \r
1108             return;\r
1109 #endif\r
1110         }\r
1111 \r
1112 #if (TRACE_RECORDER_STORE_MODE == TRACE_STORE_MODE_RING_BUFFER)\r
1113         /* Check that the buffer to be overwritten does not contain any user\r
1114         events that would be partially overwritten. If so, they must be "killed"\r
1115         by replacing the user event and following data with NULL events (i.e.,\r
1116         using a memset to zero).*/\r
1117         prvCheckDataToBeOverwrittenForMultiEntryEvents((uint8_t)noOfSlots);\r
1118 #endif\r
1119         /* Copy the local buffer to the main buffer */\r
1120         (void)memcpy(& RecorderDataPtr->eventData[RecorderDataPtr->nextFreeIndex * 4],\r
1121                tempDataBuffer,\r
1122                noOfSlots * 4);\r
1123 \r
1124         /* Update the event type, i.e., number of data entries following the\r
1125         main USER_EVENT entry (Note: important that this is after the memcpy,\r
1126         but within the critical section!)*/\r
1127         RecorderDataPtr->eventData[RecorderDataPtr->nextFreeIndex * 4] =\r
1128           (uint8_t) ( USER_EVENT + noOfSlots - 1 );\r
1129 \r
1130         /* Update the main buffer event index (already checked that it fits in\r
1131         the buffer, so no need to check for wrapping)*/\r
1132 \r
1133         RecorderDataPtr->nextFreeIndex += noOfSlots;\r
1134         RecorderDataPtr->numEvents += noOfSlots;\r
1135 \r
1136 \r
1137 \r
1138                 if (RecorderDataPtr->nextFreeIndex >= EVENT_BUFFER_SIZE)\r
1139                 {\r
1140 #if (TRACE_RECORDER_STORE_MODE == TRACE_STORE_MODE_RING_BUFFER)\r
1141                         /* We have reached the end, but this is a ring buffer. Start from the beginning again. */\r
1142                         RecorderDataPtr->bufferIsFull = 1;\r
1143                         RecorderDataPtr->nextFreeIndex = 0;\r
1144 #else\r
1145                         /* We have reached the end so we stop. */\r
1146                         vTraceStop();\r
1147 #endif\r
1148                 }\r
1149 \r
1150 #if (TRACE_RECORDER_STORE_MODE == TRACE_STORE_MODE_RING_BUFFER)\r
1151                 /* Make sure the next entry is cleared correctly */\r
1152                 prvCheckDataToBeOverwrittenForMultiEntryEvents(1);\r
1153 #endif\r
1154 \r
1155 #ifdef STOP_AFTER_N_EVENTS\r
1156 #if (STOP_AFTER_N_EVENTS > -1)\r
1157                 /* Check if we have reached the desired number of events */\r
1158                 if (RecorderDataPtr->numEvents >= STOP_AFTER_N_EVENTS)\r
1159                 {\r
1160                         vTraceStop();\r
1161                 }\r
1162 #endif\r
1163 #endif\r
1164 \r
1165         trcCRITICAL_SECTION_END();\r
1166     }\r
1167 \r
1168 #elif (USE_SEPARATE_USER_EVENT_BUFFER == 1)\r
1169         /* Use the separate user event buffer */\r
1170         traceLabel formatLabel;\r
1171         UserEventChannel channel;\r
1172 \r
1173         if (RecorderDataPtr->recorderActive && (! inExcludedTask || nISRactive) && handle_of_last_logged_task)\r
1174     {\r
1175             formatLabel = xTraceOpenLabel(formatStr);\r
1176 \r
1177             channel = xTraceRegisterChannelFormat(eventLabel, formatLabel);\r
1178 \r
1179                 prvTraceUserEventHelper1(channel, eventLabel, formatLabel, vl);\r
1180         }\r
1181 #endif\r
1182 }\r
1183 \r
1184 /******************************************************************************\r
1185  * vTraceUserEvent\r
1186  *\r
1187  * Basic user event (Standard and Professional Edition only)\r
1188  *\r
1189  * Generates a User Event with a text label. The label is created/looked up\r
1190  * in the symbol table using xTraceOpenLabel.\r
1191  ******************************************************************************/\r
1192 void vTraceUserEvent(traceLabel eventLabel)\r
1193 {\r
1194 #if (USE_SEPARATE_USER_EVENT_BUFFER == 0)\r
1195     UserEvent* ue;\r
1196     uint8_t dts1;\r
1197 \r
1198         TRACE_ASSERT(eventLabel > 0, "vTraceUserEvent: Invalid value for eventLabel", );\r
1199 \r
1200     if (RecorderDataPtr->recorderActive && (! inExcludedTask || nISRactive) && handle_of_last_logged_task)\r
1201     {\r
1202         trcCRITICAL_SECTION_BEGIN();\r
1203 \r
1204         dts1 = (uint8_t)prvTraceGetDTS(0xFF);\r
1205 \r
1206         if (RecorderDataPtr->recorderActive) /* Need to repeat this check! */\r
1207         {\r
1208             ue = (UserEvent*) xTraceNextFreeEventBufferSlot();\r
1209             if (ue != NULL)\r
1210             {\r
1211                 ue->dts = dts1;\r
1212                 ue->type = USER_EVENT;\r
1213                 ue->payload = eventLabel;\r
1214                 prvTraceUpdateCounters();\r
1215             }\r
1216         }\r
1217         trcCRITICAL_SECTION_END();\r
1218     }\r
1219 #elif (USE_SEPARATE_USER_EVENT_BUFFER == 1)\r
1220         UserEventChannel channel;\r
1221         uint32_t noOfSlots = 1;\r
1222         uint32_t tempDataBuffer[(3 + MAX_ARG_SIZE) / 4];\r
1223 \r
1224         if (RecorderDataPtr->recorderActive && (! inExcludedTask || nISRactive) && handle_of_last_logged_task)\r
1225         {\r
1226                 channel = xTraceRegisterChannelFormat(0, eventLabel);\r
1227 \r
1228                 if (channel == 0)\r
1229                 {\r
1230                         /* We are dealing with an unknown channel format pair */\r
1231                         noOfSlots++; /* Also need room for channel and format */\r
1232                         ((uint16_t*)tempDataBuffer)[2] = 0;\r
1233                         ((uint16_t*)tempDataBuffer)[3] = eventLabel;\r
1234                 }\r
1235 \r
1236                 prvTraceUserEventHelper2(channel, tempDataBuffer, noOfSlots);\r
1237         }\r
1238 #endif\r
1239 }\r
1240 \r
1241 /*******************************************************************************\r
1242  * xTraceOpenLabel\r
1243  *\r
1244  * Creates user event labels for user event channels or for individual events.\r
1245  * User events can be used to log application events and data for display in\r
1246  * the visualization tool. A user event is identified by a label, i.e., a string,\r
1247  * which is stored in the recorder's symbol table.\r
1248  * When logging a user event, a numeric handle (reference) to this string is\r
1249  * used to identify the event. This is obtained by calling\r
1250  *\r
1251  *     xTraceOpenLabel()\r
1252  *\r
1253  * which adds the string to the symbol table (if not already present)\r
1254  * and returns the corresponding handle.\r
1255  *\r
1256  * This can be used in two ways:\r
1257  *\r
1258  * 1. The handle is looked up every time, when storing the user event.\r
1259  *\r
1260  * Example:\r
1261  *     vTraceUserEvent(xTraceOpenLabel("MyUserEvent"));\r
1262  *\r
1263  * 2. The label is registered just once, with the handle stored in an\r
1264  *  application variable - much like using a file handle.\r
1265  *\r
1266  * Example:\r
1267  *     myEventHandle = xTraceOpenLabel("MyUserEvent");\r
1268  *     ...\r
1269  *     vTraceUserEvent(myEventHandle);\r
1270  *\r
1271  * The second option is faster since no lookup is required on each event, and\r
1272  * therefore recommended for user events that are frequently\r
1273  * executed and/or located in time-critical code. The lookup operation is\r
1274  * however fairly fast due to the design of the symbol table.\r
1275  ******************************************************************************/\r
1276 traceLabel xTraceOpenLabel(const char* label)\r
1277 {\r
1278         TRACE_ASSERT(label != NULL, "xTraceOpenLabel: label == NULL", (traceLabel)0);\r
1279 \r
1280     return prvTraceOpenSymbol(label, 0);\r
1281 }\r
1282 \r
1283 #endif\r
1284 \r
1285 #endif