]> git.sur5r.net Git - freertos/blob - FreeRTOS-Plus/FreeRTOS-Plus-Trace/trcPort.c
Add FreeRTOS-Plus directory.
[freertos] / FreeRTOS-Plus / FreeRTOS-Plus-Trace / trcPort.c
1 /*******************************************************************************\r
2  * FreeRTOS+Trace v2.2.3 Recorder Library\r
3  * Percepio AB, www.percepio.se\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.se or contact us directly at support@percepio.se.\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.se/index.php?page=downloads\r
45  *\r
46  * Copyright Percepio AB, 2012.\r
47  * www.percepio.se\r
48  ******************************************************************************/\r
49 #include "FreeRTOS.h"\r
50 #include "trcPort.h"\r
51 #include "trcUser.h"\r
52 \r
53 #if (configUSE_TRACE_FACILITY == 1)\r
54 \r
55 #if (INCLUDE_SAVE_TO_FILE == 1)\r
56 static char* prvFileName = NULL;\r
57 #endif\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 licence for\r
83  * FreeRTOS+Trace Professional Edition. Read more about this offer at \r
84  * www.percepio.se or contact us directly at support@percepio.se.\r
85  ******************************************************************************/\r
86 uint32_t uiTracePortGetTimeStamp()\r
87 {\r
88     /* Keep these static to avoid using more stack than necessary */\r
89     static uint32_t last_timestamp = 0;\r
90     static uint32_t timestamp;\r
91 \r
92 #if (HWTC_COUNT_DIRECTION == DIRECTION_INCREMENTING)\r
93     timestamp = ((uiTraceTickCount * HWTC_PERIOD) + HWTC_COUNT) / HWTC_DIVISOR;\r
94 #else\r
95 #if (HWTC_COUNT_DIRECTION == DIRECTION_DECREMENTING)\r
96     timestamp = ((uiTraceTickCount * HWTC_PERIOD) + (HWTC_PERIOD - HWTC_COUNT)) / HWTC_DIVISOR;\r
97 #else\r
98     Junk text to cause compiler error - HWTC_COUNT_DIRECTION is not set correctly!\r
99     Should be DIRECTION_INCREMENTING or DIRECTION_DECREMENTING\r
100 #endif\r
101 #endif\r
102 \r
103     /* May occur due to overflow, if the update of uiTraceTickCount has been \r
104     delayed due to disabled interrupts. */\r
105     if (timestamp < last_timestamp)\r
106     {\r
107         timestamp += (HWTC_PERIOD / HWTC_DIVISOR);\r
108     }\r
109 \r
110     last_timestamp = timestamp;\r
111 \r
112     return timestamp;\r
113 }\r
114 \r
115 /*******************************************************************************\r
116  * vTracePortEnd\r
117  * \r
118  * This function is called by the monitor when a recorder stop is detected.\r
119  * This is used by the Win32 port to store the trace to a file. The file path is\r
120  * set using vTracePortSetOutFile.\r
121  ******************************************************************************/\r
122 void vTracePortEnd()\r
123 {\r
124     vTraceConsoleMessage("\n\r[FreeRTOS+Trace] Running vTracePortEnd.\n\r");\r
125 \r
126     #if (WIN32_PORT_SAVE_WHEN_STOPPED == 1)\r
127     vTracePortSave();\r
128     #endif\r
129 \r
130     #if (WIN32_PORT_EXIT_WHEN_STOPPED == 1)\r
131     /* In the FreeRTOS/Win32 demo, this allows for killing the application \r
132     when the recorder is stopped (e.g., when the buffer is full) */\r
133     system("pause");\r
134     exit(0);\r
135     #endif\r
136 }\r
137 \r
138 #if (INCLUDE_SAVE_TO_FILE == 1)\r
139 /*******************************************************************************\r
140  * vTracePortSetOutFile\r
141  *\r
142  * Sets the filename/path used in vTracePortSave.\r
143  * This is set in a separate function, since the Win32 port calls vTracePortSave\r
144  * in vTracePortEnd if WIN32_PORT_SAVE_WHEN_STOPPED is set.\r
145  ******************************************************************************/\r
146 void vTracePortSetOutFile(char* path)\r
147 {\r
148     prvFileName = path;\r
149 }\r
150 \r
151 /*******************************************************************************\r
152  * vTracePortSave\r
153  *\r
154  * Saves the trace to a file on a local file system. The path is set in a \r
155  * separate function, vTracePortSetOutFile, since the Win32 port calls \r
156  * vTracePortSave in vTracePortEnd if WIN32_PORT_SAVE_WHEN_STOPPED is set.\r
157  ******************************************************************************/\r
158 void vTracePortSave()\r
159 {\r
160     char buf[180];\r
161     FILE* f;\r
162 \r
163     if (prvFileName == NULL)\r
164     {\r
165         prvFileName = "FreeRTOSPlusTrace.dump";\r
166         sprintf(buf, "No filename specified, using default \"%s\".", prvFileName);\r
167         vTraceConsoleMessage(buf);\r
168     }\r
169 \r
170     fopen_s(&f, prvFileName, "wb");\r
171     if (f)\r
172     {\r
173         fwrite(RecorderDataPtr, sizeof(RecorderDataType), 1, f);\r
174         fclose(f);\r
175         sprintf(buf, "\n\r[FreeRTOS+Trace] Saved in: %s\n\r", prvFileName);\r
176         vTraceConsoleMessage(buf);\r
177     }\r
178     else\r
179     {\r
180         sprintf(buf, "\n\r[FreeRTOS+Trace] Failed to write to output file!\n\r");\r
181         vTraceConsoleMessage(buf);\r
182     }\r
183 }\r
184 #endif\r
185 #endif