]> git.sur5r.net Git - freertos/blob - FreeRTOS-Plus/Source/FreeRTOS-Plus-Trace/trcPort.c
Update the FreeRTOS+UDP LPC18xx MAC driver to use the LPCOpen drivers.
[freertos] / FreeRTOS-Plus / Source / FreeRTOS-Plus-Trace / trcPort.c
1 /*******************************************************************************\r
2  * FreeRTOS+Trace v2.3.0 Recorder Library\r
3  * Percepio AB, www.percepio.com\r
4  *\r
5  * trcPort.c\r
6  *\r
7  * Contains all portability issues of the trace recorder library. \r
8  * See also trcPort.h, where port-specific macros are defined.\r
9  *\r
10  * Terms of Use\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
19  *\r
20  * Disclaimer \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
34  *\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
41  *\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
45  *\r
46  * Copyright Percepio AB, 2012.\r
47  * www.percepio.com\r
48  ******************************************************************************/\r
49 \r
50 #include "trcUser.h"\r
51 \r
52 #if (configUSE_TRACE_FACILITY == 1)\r
53 \r
54 #if (INCLUDE_SAVE_TO_FILE == 1)\r
55 static char* prvFileName = NULL;\r
56 #endif\r
57 \r
58 \r
59 /*******************************************************************************\r
60  * uiTraceTickCount\r
61  *\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
68 \r
69 /******************************************************************************\r
70  * uiTracePortGetTimeStamp\r
71  *\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
74  *\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
78  *\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
87 {\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
92     \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
98 #else\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
101 #endif\r
102     \r
103     if (last_traceTickCount - uiTraceTickCount - 1 < 0x80000000)\r
104     {\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
109     }\r
110     else\r
111     {\r
112         /* Business as usual */\r
113         traceTickCount = uiTraceTickCount;\r
114     }\r
115 \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
119     {\r
120         /* A trace tick has occurred but not been executed by the kernel, so we compensate manually. */\r
121         traceTickCount++;\r
122     }\r
123     \r
124     /* Check if the return address is OK, then we perform the calculation. */\r
125     if (pTimestamp)\r
126     {\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
131     }\r
132     \r
133     /* Store the previous values. */\r
134     last_traceTickCount = traceTickCount;\r
135     last_hwtc_count = hwtc_count;\r
136 }\r
137 \r
138 /*******************************************************************************\r
139  * vTracePortEnd\r
140  * \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
146 {\r
147     vTraceConsoleMessage("\n\r[FreeRTOS+Trace] Running vTracePortEnd.\n\r");\r
148 \r
149     #if (WIN32_PORT_SAVE_WHEN_STOPPED == 1)\r
150     vTracePortSave();\r
151     #endif\r
152 \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
156     system("pause");\r
157     exit(0);\r
158     #endif\r
159 }\r
160 \r
161 #if (INCLUDE_SAVE_TO_FILE == 1)\r
162 /*******************************************************************************\r
163  * vTracePortSetOutFile\r
164  *\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
170 {\r
171     prvFileName = path;\r
172 }\r
173 \r
174 /*******************************************************************************\r
175  * vTracePortSave\r
176  *\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
182 {\r
183     char buf[180];\r
184     FILE* f;\r
185 \r
186     if (prvFileName == NULL)\r
187     {\r
188         prvFileName = "FreeRTOSPlusTrace.dump";\r
189         sprintf(buf, "No filename specified, using default \"%s\".", prvFileName);\r
190         vTraceConsoleMessage(buf);\r
191     }\r
192 \r
193     fopen_s(&f, prvFileName, "wb");\r
194     if (f)\r
195     {\r
196         fwrite(RecorderDataPtr, sizeof(RecorderDataType), 1, f);\r
197         fclose(f);\r
198         sprintf(buf, "\n\r[FreeRTOS+Trace] Saved in: %s\n\r", prvFileName);\r
199         vTraceConsoleMessage(buf);\r
200     }\r
201     else\r
202     {\r
203         sprintf(buf, "\n\r[FreeRTOS+Trace] Failed to write to output file!\n\r");\r
204         vTraceConsoleMessage(buf);\r
205     }\r
206 }\r
207 #endif\r
208 #endif