1 /*******************************************************************************
\r
2 * FreeRTOS+Trace v2.3.0 Recorder Library
\r
3 * Percepio AB, www.percepio.com
\r
7 * Contains all portability issues of the trace recorder library.
\r
8 * See also trcPort.h, where port-specific macros are defined.
\r
11 * This software is copyright Percepio AB. The recorder library is free for
\r
12 * use together with Percepio products. You may distribute the recorder library
\r
13 * in its original form, including modifications in trcPort.c and trcPort.h
\r
14 * given that these modification are clearly marked as your own modifications
\r
15 * and documented in the initial comment section of these source files.
\r
16 * This software is the intellectual property of Percepio AB and may not be
\r
17 * sold or in other ways commercially redistributed without explicit written
\r
18 * permission by Percepio AB.
\r
21 * The trace tool and recorder library is being delivered to you AS IS and
\r
22 * Percepio AB makes no warranty as to its use or performance. Percepio AB does
\r
23 * not and cannot warrant the performance or results you may obtain by using the
\r
24 * software or documentation. Percepio AB make no warranties, express or
\r
25 * implied, as to noninfringement of third party rights, merchantability, or
\r
26 * fitness for any particular purpose. In no event will Percepio AB, its
\r
27 * technology partners, or distributors be liable to you for any consequential,
\r
28 * incidental or special damages, including any lost profits or lost savings,
\r
29 * even if a representative of Percepio AB has been advised of the possibility
\r
30 * of such damages, or for any claim by any third party. Some jurisdictions do
\r
31 * not allow the exclusion or limitation of incidental, consequential or special
\r
32 * damages, or the exclusion of implied warranties or limitations on how long an
\r
33 * implied warranty may last, so the above limitations may not apply to you.
\r
35 * OFFER FROM PERCEPIO:
\r
36 * For silicon companies and non-corporate FreeRTOS users (researchers, students
\r
37 * , hobbyists or early-phase startups) we have an attractive offer:
\r
38 * Provide a hardware timer port and get a FREE single-user licence for
\r
39 * FreeRTOS+Trace Professional Edition. Read more about this offer at
\r
40 * www.percepio.com or contact us directly at support@percepio.com.
\r
42 * FreeRTOS+Trace is available as Free Edition and in two premium editions.
\r
43 * You may use the premium features during 30 days for evaluation.
\r
44 * Download FreeRTOS+Trace at http://www.percepio.com/products/downloads/
\r
46 * Copyright Percepio AB, 2012.
\r
48 ******************************************************************************/
\r
50 #include "trcUser.h"
\r
52 #if (configUSE_TRACE_FACILITY == 1)
\r
54 #if (INCLUDE_SAVE_TO_FILE == 1)
\r
55 static char* prvFileName = NULL;
\r
59 /*******************************************************************************
\r
62 * This variable is updated by the traceTASK_INCREMENT_TICK macro in the
\r
63 * FreeRTOS tick handler. This does not need to be modified when developing a
\r
64 * new timer port. It is prefered to keep any timer port changes in the HWTC
\r
65 * macro definitions, which typically give sufficient flexibility.
\r
66 ******************************************************************************/
\r
67 uint32_t uiTraceTickCount = 0;
\r
69 /******************************************************************************
\r
70 * uiTracePortGetTimeStamp
\r
72 * Returns the current time based on the HWTC macros which provide a hardware
\r
73 * isolation layer towards the hardware timer/counter.
\r
75 * The HWTC macros and uiTracePortGetTimeStamp is the main porting issue
\r
76 * or the trace recorder library. Typically you should not need to change
\r
77 * the code of uiTracePortGetTimeStamp if using the HWTC macros.
\r
79 * OFFER FROM PERCEPIO:
\r
80 * For silicon companies and non-corporate FreeRTOS users (researchers, students
\r
81 * , hobbyists or early-phase startups) we have an attractive offer:
\r
82 * Provide a hardware timer port and get a FREE single-user license for
\r
83 * FreeRTOS+Trace Professional Edition. Read more about this offer at
\r
84 * www.percepio.com or contact us directly at support@percepio.com.
\r
85 ******************************************************************************/
\r
86 void uiTracePortGetTimeStamp(uint32_t *pTimestamp)
\r
88 static uint32_t last_traceTickCount = 0;
\r
89 static uint32_t last_hwtc_count = 0;
\r
90 uint32_t traceTickCount = 0;
\r
91 uint32_t hwtc_count = 0;
\r
93 /* Retrieve HWTC_COUNT only once since the same value should be used all throughout this function. */
\r
94 #if (HWTC_COUNT_DIRECTION == DIRECTION_INCREMENTING)
\r
95 hwtc_count = HWTC_COUNT;
\r
96 #elif (HWTC_COUNT_DIRECTION == DIRECTION_DECREMENTING)
\r
97 hwtc_count = HWTC_PERIOD - HWTC_COUNT;
\r
99 Junk text to cause compiler error - HWTC_COUNT_DIRECTION is not set correctly!
\r
100 Should be DIRECTION_INCREMENTING or DIRECTION_DECREMENTING
\r
103 if (last_traceTickCount - uiTraceTickCount - 1 < 0x80000000)
\r
105 /* This means last_traceTickCount is higher than uiTraceTickCount,
\r
106 so we have previously compensated for a missed tick.
\r
107 Therefore we use the last stored value because that is more accurate. */
\r
108 traceTickCount = last_traceTickCount;
\r
112 /* Business as usual */
\r
113 traceTickCount = uiTraceTickCount;
\r
116 /* Check for overflow. May occur if the update of uiTraceTickCount has been
\r
117 delayed due to disabled interrupts. */
\r
118 if (traceTickCount == last_traceTickCount && hwtc_count < last_hwtc_count)
\r
120 /* A trace tick has occurred but not been executed by the kernel, so we compensate manually. */
\r
124 /* Check if the return address is OK, then we perform the calculation. */
\r
127 /* Get timestamp from trace ticks. Scale down the period to avoid unwanted overflows. */
\r
128 *pTimestamp = traceTickCount * (HWTC_PERIOD / HWTC_DIVISOR);
\r
129 /* Increase timestamp by (hwtc_count + "lost hardware ticks from scaling down period") / HWTC_DIVISOR. */
\r
130 *pTimestamp += (hwtc_count + traceTickCount * (HWTC_PERIOD % HWTC_DIVISOR)) / HWTC_DIVISOR;
\r
133 /* Store the previous values. */
\r
134 last_traceTickCount = traceTickCount;
\r
135 last_hwtc_count = hwtc_count;
\r
138 /*******************************************************************************
\r
141 * This function is called by the monitor when a recorder stop is detected.
\r
142 * This is used by the Win32 port to store the trace to a file. The file path is
\r
143 * set using vTracePortSetOutFile.
\r
144 ******************************************************************************/
\r
145 void vTracePortEnd()
\r
147 vTraceConsoleMessage("\n\r[FreeRTOS+Trace] Running vTracePortEnd.\n\r");
\r
149 #if (WIN32_PORT_SAVE_WHEN_STOPPED == 1)
\r
153 #if (WIN32_PORT_EXIT_WHEN_STOPPED == 1)
\r
154 /* In the FreeRTOS/Win32 demo, this allows for killing the application
\r
155 when the recorder is stopped (e.g., when the buffer is full) */
\r
161 #if (INCLUDE_SAVE_TO_FILE == 1)
\r
162 /*******************************************************************************
\r
163 * vTracePortSetOutFile
\r
165 * Sets the filename/path used in vTracePortSave.
\r
166 * This is set in a separate function, since the Win32 port calls vTracePortSave
\r
167 * in vTracePortEnd if WIN32_PORT_SAVE_WHEN_STOPPED is set.
\r
168 ******************************************************************************/
\r
169 void vTracePortSetOutFile(char* path)
\r
171 prvFileName = path;
\r
174 /*******************************************************************************
\r
177 * Saves the trace to a file on a local file system. The path is set in a
\r
178 * separate function, vTracePortSetOutFile, since the Win32 port calls
\r
179 * vTracePortSave in vTracePortEnd if WIN32_PORT_SAVE_WHEN_STOPPED is set.
\r
180 ******************************************************************************/
\r
181 void vTracePortSave()
\r
186 if (prvFileName == NULL)
\r
188 prvFileName = "FreeRTOSPlusTrace.dump";
\r
189 sprintf(buf, "No filename specified, using default \"%s\".", prvFileName);
\r
190 vTraceConsoleMessage(buf);
\r
193 fopen_s(&f, prvFileName, "wb");
\r
196 fwrite(RecorderDataPtr, sizeof(RecorderDataType), 1, f);
\r
198 sprintf(buf, "\n\r[FreeRTOS+Trace] Saved in: %s\n\r", prvFileName);
\r
199 vTraceConsoleMessage(buf);
\r
203 sprintf(buf, "\n\r[FreeRTOS+Trace] Failed to write to output file!\n\r");
\r
204 vTraceConsoleMessage(buf);
\r