trcUser.h
Go to the documentation of this file.
1 /*******************************************************************************
2  * Tracealyzer v3.0.2 Recorder Library
3  * Percepio AB, www.percepio.com
4  *
5  * trcUser.h
6  * The public API of the trace recorder library.
7  *
8  * Terms of Use
9  * This software is copyright Percepio AB. The recorder library is free for
10  * use together with Percepio products. You may distribute the recorder library
11  * in its original form, including modifications in trcHardwarePort.c/.h
12  * given that these modification are clearly marked as your own modifications
13  * and documented in the initial comment section of these source files.
14  * This software is the intellectual property of Percepio AB and may not be
15  * sold or in other ways commercially redistributed without explicit written
16  * permission by Percepio AB.
17  *
18  * Disclaimer
19  * The trace tool and recorder library is being delivered to you AS IS and
20  * Percepio AB makes no warranty as to its use or performance. Percepio AB does
21  * not and cannot warrant the performance or results you may obtain by using the
22  * software or documentation. Percepio AB make no warranties, express or
23  * implied, as to noninfringement of third party rights, merchantability, or
24  * fitness for any particular purpose. In no event will Percepio AB, its
25  * technology partners, or distributors be liable to you for any consequential,
26  * incidental or special damages, including any lost profits or lost savings,
27  * even if a representative of Percepio AB has been advised of the possibility
28  * of such damages, or for any claim by any third party. Some jurisdictions do
29  * not allow the exclusion or limitation of incidental, consequential or special
30  * damages, or the exclusion of implied warranties or limitations on how long an
31  * implied warranty may last, so the above limitations may not apply to you.
32  *
33  * Tabs are used for indent in this file (1 tab = 4 spaces)
34  *
35  * Copyright Percepio AB, 2014.
36  * www.percepio.com
37  ******************************************************************************/
38 
39 #ifndef TRCUSER_H
40 #define TRCUSER_H
41 
42 #ifdef __cplusplus
43 extern "C" {
44 #endif
45 
46 #include "trcKernelPort.h"
47 
48 #if (USE_TRACEALYZER_RECORDER == 1)
49 
50 #ifndef USE_SEPARATE_USER_EVENT_BUFFER
51 #define USE_SEPARATE_USER_EVENT_BUFFER 0
52 #endif
53 
54 /*******************************************************************************
55  * TRACE_STOP_HOOK - Hook Pointer Data Type
56  *
57  * Declares a data type for a call back function that will be invoked whenever
58  * the recorder is stopped.
59  ******************************************************************************/
60 typedef void (*TRACE_STOP_HOOK)(void);
61 
62 /*******************************************************************************
63  * vTraceStopHookPtr
64  *
65  * Points to a call back function that is called from vTraceStop().
66  ******************************************************************************/
67 extern TRACE_STOP_HOOK vTraceStopHookPtr;
68 
69 /*******************************************************************************
70  * vTraceInitTraceData
71  *
72  * Allocates, if necessary, and initializes the recorder data structure, based
73  * on the constants in trcConfig.h.
74  ******************************************************************************/
75 void vTraceInitTraceData(void);
76 
77 /*******************************************************************************
78  * vTraceSetRecorderData
79  *
80  * If custom allocation is used, this function must be called so the recorder
81  * library knows where to save the trace data.
82  ******************************************************************************/
83 #if (TRACE_DATA_ALLOCATION == TRACE_DATA_ALLOCATION_CUSTOM)
84 void vTraceSetRecorderData(void* pRecorderData);
85 #endif
86 
87 /*******************************************************************************
88  * vTraceSetStopHook
89  *
90  * Sets a function to be called when the recorder is stopped.
91  ******************************************************************************/
92 void vTraceSetStopHook(TRACE_STOP_HOOK stopHookFunction);
93 
94 /*******************************************************************************
95  * uiTraceStart
96  *
97  * Starts the recorder. The recorder will not be started if an error has been
98  * indicated using vTraceError, e.g. if any of the Nx constants in trcConfig.h
99  * has a too small value (NTASK, NQUEUE, etc).
100  *
101  * Returns 1 if the recorder was started successfully.
102  * Returns 0 if the recorder start was prevented due to a previous internal
103  * error. In that case, check vTraceGetLastError to get the error message.
104  * Any error message is also presented when opening a trace file.
105  *
106  ******************************************************************************/
107 uint32_t uiTraceStart(void);
108 
109 /*******************************************************************************
110  * vTraceStart
111  *
112  * Starts the recorder. The recorder will not be started if an error has been
113  * indicated using vTraceError, e.g. if any of the Nx constants in trcConfig.h
114  * has a too small value (NTASK, NQUEUE, etc).
115  *
116  * This function is obsolete, but has been saved for backwards compatibility.
117  * We recommend using uiTraceStart instead.
118  ******************************************************************************/
119 void vTraceStart(void);
120 
121 /*******************************************************************************
122  * vTraceStop
123  *
124  * Stops the recorder. The recording can be resumed by calling vTraceStart.
125  * This does not reset the recorder. Use vTraceClear is that is desired.
126  ******************************************************************************/
127 void vTraceStop(void);
128 
129 /*******************************************************************************
130  * xTraceGetLastError
131  *
132  * Gives the last error message, if any. NULL if no error message is stored.
133  * Any error message is also presented when opening a trace file.
134  ******************************************************************************/
135 char* xTraceGetLastError(void);
136 
137 /*******************************************************************************
138  * vTraceClear
139  *
140  * Resets the recorder. Only necessary if a restart is desired - this is not
141  * needed in the startup initialization.
142  ******************************************************************************/
143 void vTraceClear(void);
144 
145 /*******************************************************************************
146 * vTraceClearError
147 *
148 * Removes any previous error message generated by recorder calling vTraceError.
149 * By calling this function, it may be possible to start/restart the trace
150 * despite errors in the recorder, but there is no guarantee that the trace
151 * recorder will work correctly in that case, depending on the type of error.
152 ******************************************************************************/
153 void vTraceClearError(int resetErrorMessage);
154 
155 #if (INCLUDE_ISR_TRACING == 1)
156 
157 /*******************************************************************************
158  * vTraceSetISRProperties
159  *
160  * Registers an Interrupt Service Routine in the recorder library, This must be
161  * called before using vTraceStoreISRBegin to store ISR events. This is
162  * typically called in the startup of the system, before the scheduler is
163  * started.
164  *
165  * Example:
166  * #define ID_ISR_TIMER1 1 // lowest valid ID is 1
167  * #define PRIO_OF_ISR_TIMER1 3 // the hardware priority of the interrupt
168  * ...
169  * vTraceSetISRProperties(ID_ISR_TIMER1, "ISRTimer1", PRIO_OF_ISR_TIMER1);
170  * ...
171  * void ISR_handler()
172  * {
173  * vTraceStoreISRBegin(ID_OF_ISR_TIMER1);
174  * ...
175  * vTraceStoreISREnd(0);
176  * }
177  ******************************************************************************/
178 void vTraceSetISRProperties(objectHandleType handle, const char* name, char priority);
179 
180 /*******************************************************************************
181  * vTraceStoreISRBegin
182  *
183  * Registers the beginning of an Interrupt Service Routine.
184  * If allowing nested ISRs, this must be called with interrupts disabled.
185  *
186  * Example:
187  * #define ID_ISR_TIMER1 1 // lowest valid ID is 1
188  * #define PRIO_OF_ISR_TIMER1 3 // the hardware priority of the interrupt
189  * ...
190  * vTraceSetISRProperties(ID_ISR_TIMER1, "ISRTimer1", PRIO_OF_ISR_TIMER1);
191  * ...
192  * void ISR_handler()
193  * {
194  * vTraceStoreISRBegin(ID_OF_ISR_TIMER1);
195  * ...
196  * vTraceStoreISREnd(0);
197  * }
198  *
199  ******************************************************************************/
201 
202 /*******************************************************************************
203  * vTraceStoreISREnd
204  *
205  * Registers the end of an Interrupt Service Routine.
206  *
207  * The parameter pendingISR indicates if the interrupt has requested a
208  * task-switch (= 1) or if the interrupt returns to the earlier context (= 0)
209  *
210  * Example:
211  * #define ID_ISR_TIMER1 1 // lowest valid ID is 1
212  * #define PRIO_OF_ISR_TIMER1 3 // the hardware priority of the interrupt
213  * ...
214  * vTraceSetISRProperties(ID_ISR_TIMER1, "ISRTimer1", PRIO_OF_ISR_TIMER1);
215  * ...
216  * void ISR_handler()
217  * {
218  * vTraceStoreISRBegin(ID_OF_ISR_TIMER1);
219  * ...
220  * vTraceStoreISREnd(0);
221  * }
222  *
223  ******************************************************************************/
224 void vTraceStoreISREnd(int pendingISR);
225 
226 #else
227  /* If not including the ISR recording */
228 
229 void vTraceIncreaseISRActive(void);
230 
231 void vTraceDecreaseISRActive(void);
232 
233 #define vTraceSetISRProperties(handle, name, priority)
234 #define vTraceStoreISRBegin(id) vTraceIncreaseISRActive()
235 #define vTraceStoreISREnd() vTraceDecreaseISRActive()
236 
237 #endif
238 
239 
240 /******************************************************************************
241  * vTraceTaskInstanceFinish(void)
242  *
243  * Marks the current task instance as finished on the next kernel call.
244  *
245  * If that kernel call is blocking, the instance ends after the blocking event
246  * and the corresponding return event is then the start of the next instance.
247  * If the kernel call is not blocking, the viewer instead splits the current
248  * fragment right before the kernel call, which makes this call the first event
249  * of the next instance.
250  *
251  * See also USE_IMPLICIT_IFE_RULES in trcConfig.h
252  *
253  * Example:
254  *
255  * while(1)
256  * {
257  * xQueueReceive(CommandQueue, &command, timeoutDuration);
258  * processCommand(command);
259  * vTraceInstanceFinish();
260  * }
261  *
262  *****************************************************************************/
263 void vTraceTaskInstanceFinish(void);
264 
265 /******************************************************************************
266  * vTraceTaskInstanceFinishDirect(void)
267  *
268  * Marks the current task instance as finished at this very instant.
269  * This makes the viewer to splits the current fragment at this point and begin
270  * a new actor instance.
271  *
272  * See also USE_IMPLICIT_IFE_RULES in trcConfig.h
273  *
274  * Example:
275  *
276  * This example will generate two instances for each loop iteration.
277  * The first instance ends at vTraceInstanceFinishDirect(), while the second
278  * instance ends at the next xQueueReceive call.
279  *
280  * while (1)
281  * {
282  * xQueueReceive(CommandQueue, &command, timeoutDuration);
283  * ProcessCommand(command);
284  * vTraceInstanceFinishDirect();
285  * DoSometingElse();
286  * vTraceInstanceFinish();
287  * }
288  *
289  *
290  *****************************************************************************/
291 void vTraceTaskInstanceFinishDirect(void);
292 
293 /*******************************************************************************
294  * vTraceGetTraceBuffer
295  *
296  * Returns a pointer to the recorder data structure. Use this together with
297  * uiTraceGetTraceBufferSize if you wish to implement an own store/upload
298  * solution, e.g., in case a debugger connection is not available for uploading
299  * the data.
300  ******************************************************************************/
301 void* vTraceGetTraceBuffer(void);
302 
303 /*******************************************************************************
304  * uiTraceGetTraceBufferSize
305  *
306  * Gets the size of the recorder data structure. For use together with
307  * vTraceGetTraceBuffer if you wish to implement an own store/upload solution,
308  * e.g., in case a debugger connection is not available for uploading the data.
309  ******************************************************************************/
310 uint32_t uiTraceGetTraceBufferSize(void);
311 
312 #if (INCLUDE_USER_EVENTS == 1)
313 
314 /*******************************************************************************
315  * xTraceOpenLabel
316  *
317  * Creates user event labels for user event channels or for individual events.
318  * User events can be used to log application events and data for display in
319  * the visualization tool. A user event is identified by a label, i.e., a string,
320  * which is stored in the recorder's symbol table.
321  * When logging a user event, a numeric handle (reference) to this string is
322  * used to identify the event. This is obtained by calling
323  *
324  * xTraceOpenLabel()
325  *
326  * whihc adds the string to the symbol table (if not already present)
327  * and returns the corresponding handle.
328  *
329  * This can be used in two ways:
330  *
331  * 1. The handle is looked up every time, when storing the user event.
332  *
333  * Example:
334  * vTraceUserEvent(xTraceOpenLabel("MyUserEvent"));
335  *
336  * 2. The label is registered just once, with the handle stored in an
337  * application variable - much like using a file handle.
338  *
339  * Example:
340  * myEventHandle = xTraceOpenLabel("MyUserEvent");
341  * ...
342  * vTraceUserEvent(myEventHandle);
343  *
344  * The second option is faster since no lookup is required on each event, and
345  * therefore recommended for user events that are frequently
346  * executed and/or located in time-critical code. The lookup operation is
347  * however fairly fast due to the design of the symbol table.
348  ******************************************************************************/
349 traceLabel xTraceOpenLabel(const char* label);
350 
351  /******************************************************************************
352  * vTraceUserEvent
353  *
354  * Basic user event (Standard and Professional Edition only)
355  *
356  * Generates a User Event with a text label. The label is created/looked up
357  * in the symbol table using xTraceOpenLabel.
358  ******************************************************************************/
359 void vTraceUserEvent(traceLabel eventLabel);
360 
361  /******************************************************************************
362  * vTracePrintF
363  *
364  * Advanced user events (Professional Edition only)
365  *
366  * Generates User Event with formatted text and data, similar to a "printf".
367  * It is very fast compared to a normal "printf" since this function only
368  * stores the arguments. The actual formatting is done
369  * on the host PC when the trace is displayed in the viewer tool.
370  *
371  * User Event labels are created using xTraceOpenLabel.
372  * Example:
373  *
374  * traceLabel adc_uechannel = xTraceOpenLabel("ADC User Events");
375  * ...
376  * vTracePrint(adc_uechannel,
377  * "ADC channel %d: %lf volts",
378  * ch, (double)adc_reading/(double)scale);
379  *
380  * This can be combined into one line, if desired, but this is slower:
381  *
382  * vTracePrint(xTraceOpenLabel("ADC User Events"),
383  * "ADC channel %d: %lf volts",
384  * ch, (double)adc_reading/(double)scale);
385  *
386  * Calling xTraceOpenLabel multiple times will not create duplicate entries, but
387  * it is of course faster to just do it once, and then keep the handle for later
388  * use. If you don�t have any data arguments, only a text label/string, it is
389  * better to use vTraceUserEvent - it is faster.
390  *
391  * Format specifiers supported:
392  * %d - 32 bit signed integer
393  * %u - 32 bit unsigned integer
394  * %f - 32 bit float
395  * %s - string (is copied to the recorder symbol table)
396  * %hd - 16 bit signed integer
397  * %hu - 16 bit unsigned integer
398  * %bd - 8 bit signed integer
399  * %bu - 8 bit unsigned integer
400  * %lf - double-precision float (Note! See below...)
401  *
402  * Up to 15 data arguments are allowed, with a total size of maximum 32 byte.
403  * In case this is exceeded, the user event is changed into an error message.
404  *
405  * The data is stored in trace buffer, and is packed to allow storing multiple
406  * smaller data entries in the same 4-byte record, e.g., four 8-bit values.
407  * A string requires two bytes, as the symbol table is limited to 64K. Storing
408  * a double (%lf) uses two records, so this is quite costly. Use float (%f)
409  * unless the higher precision is really necessary.
410  *
411  * Note that the double-precision float (%lf) assumes a 64 bit double
412  * representation. This does not seem to be the case on e.g. PIC24 and PIC32.
413  * Before using a %lf argument on a 16-bit MCU, please verify that
414  * "sizeof(double)" actually gives 8 as expected. If not, use %f instead.
415  ******************************************************************************/
416 void vTracePrintF(traceLabel eventLabel, const char* formatStr, ...);
417 
418 #if (USE_SEPARATE_USER_EVENT_BUFFER == 1)
419 UserEventChannel xTraceRegisterChannelFormat(traceLabel channel, traceLabel formatStr);
420 void vTraceChannelPrintF(UserEventChannel channel, ...);
421 void vTraceChannelUserEvent(UserEventChannel channel);
422 #endif
423 
424 #else
425 
426 #define vTracePrintF(eventLabel, formatStr, ...);
427 #define xTraceOpenLabel(label) 0
428 #define vTraceUserEvent(eventLabel)
429 
430 #endif
431 
432 #else
433 
434 /* Empty defines for user functions to avoid compiler errors if trace is not to be used */
435 
436 #define vTraceInitTraceData()
437 #define uiTraceStart() (1) // Fake "success", if used when recorder is excluded from build
438 #define vTraceStart()
439 #define vTraceStop()
440 #define vTraceClear()
441 #define vTraceStartStatusMonitor()
442 #define vTraceGetTraceBuffer() ((void*)0)
443 #define uiTraceGetTraceBufferSize() 0
444 #define xTraceOpenLabel(label) 0
445 #define vTraceUserEvent(eventLabel)
446 #define vTracePrintF(eventLabel,formatStr,...)
447 #define vTraceExcludeTaskFromSchedulingTrace(name)
448 
449 #define vTraceSetISRProperties(handle, name, priority)
450 #define vTraceStoreISRBegin(id)
451 #define vTraceStoreISREnd(flag)
452 #define vTraceExcludeTaskFromTrace(handle)
453 #define vTraceSetQueueName(a, b)
454 #define vTraceSetMutexName(a, b)
455 #define vTraceSetSemaphoreName(a, b)
456 #define vTraceSetEventGroupName(a, b)
457 
458 #define vTraceSetStopHook(a)
459 #endif
460 
461 #ifdef __cplusplus
462 }
463 #endif
464 
465 #endif
#define uiTraceGetTraceBufferSize()
Definition: trcUser.h:443
#define vTraceStoreISREnd(flag)
Definition: trcUser.h:451
#define uiTraceStart()
Definition: trcUser.h:437
#define vTraceGetTraceBuffer()
Definition: trcUser.h:442
#define vTraceStart()
Definition: trcUser.h:438
#define vTraceStoreISRBegin(id)
Definition: trcUser.h:450
uint8_t objectHandleType
Definition: trcTypes.h:53
#define vTraceInitTraceData()
Definition: trcUser.h:436
#define vTraceClear()
Definition: trcUser.h:440
#define vTracePrintF(eventLabel, formatStr,...)
Definition: trcUser.h:446
#define vTraceSetStopHook(a)
Definition: trcUser.h:458
uint8_t UserEventChannel
Definition: trcTypes.h:48
#define xTraceOpenLabel(label)
Definition: trcUser.h:444
#define vTraceUserEvent(eventLabel)
Definition: trcUser.h:445
uint16_t traceLabel
Definition: trcTypes.h:46
#define vTraceSetISRProperties(handle, name, priority)
Definition: trcUser.h:449
#define vTraceStop()
Definition: trcUser.h:439