1 /*******************************************************************************
\r
2 * FreeRTOS+Trace v2.2.2 Recorder Library
\r
3 * Percepio AB, www.percepio.se
\r
7 * Contains together with trcPort.c all portability issues of the trace recorder
\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 * FreeRTOS+Trace is available as Free Edition and in two premium editions.
\r
36 * You may use the premium features during 30 days for evaluation.
\r
37 * Download FreeRTOS+Trace at http://www.percepio.se/index.php?page=downloads
\r
39 * Copyright Percepio AB, 2012.
\r
41 ******************************************************************************/
\r
46 #include "trcBase.h"
\r
48 /* If FreeRTOS Win32 port */
\r
52 #define _WIN32_WINNT 0x0600
\r
54 /* Standard includes. */
\r
56 #include <windows.h>
\r
59 /*******************************************************************************
\r
60 * The Win32 port by default saves the trace to file and then kills the
\r
61 * program when the recorder is stopped, to facilitate quick, simple tests
\r
63 ******************************************************************************/
\r
64 #define WIN32_PORT_SAVE_WHEN_STOPPED 1
\r
65 #define WIN32_PORT_EXIT_WHEN_STOPPED 1
\r
69 #define DIRECTION_INCREMENTING 1
\r
70 #define DIRECTION_DECREMENTING 2
\r
72 /******************************************************************************
\r
75 * PORT_HWIndependent
\r
76 * A hardware independent fallback option for event timestamping. Provides low
\r
77 * resolution timestamps based on the OS tick.
\r
78 * This may be used on the Win32 port, but may also be used on embedded hardware
\r
79 * platforms. Note that this gives suboptimal display in FreeRTOS+Trace. All
\r
80 * time durations will be truncated to the OS tick frequency, typically 1 KHz.
\r
81 * This means that a task or ISR that executes in less than 1 ms get an exection
\r
82 * time of zero. They are however still visible in FreeRTOS+Trace.
\r
85 * "Accurate" timestamping based on the Windows permance counter. Note that
\r
86 * this gives the host machine time, not the simulated FreeRTOS time (tick
\r
87 * count). The timing of the Win32 FreeRTOS build is not real-time, since it
\r
88 * depends on the scheduling and tick rate of Windows, which is very slow.
\r
90 * Officially supported hardware specific ports included are:
\r
91 * - PORT_Atmel_AT91SAM7
\r
92 * - PORT_ARM_CortexM
\r
93 * - PORT_Renesas_RX600
\r
95 * We also provide several "unofficial" hardware-specific ports. There have
\r
96 * been developed by external contributors, and have not yet been verified
\r
97 * by Percepio AB. Let us know if you have problems getting these to work.
\r
99 * Unoffical hardware specific ports provided are:
\r
100 * - PORT_TEXAS_INSTRUMENTS_TMS570
\r
101 * - PORT_TEXAS_INSTRUMENTS_MSP430
\r
102 * - PORT_MICROCHIP_PIC32
\r
103 * - PORT_MICROCHIP_dsPIC_AND_PIC24
\r
104 * - PORT_XILINX_PPC405
\r
105 * - PORT_XILINX_PPC440
\r
107 ******************************************************************************/
\r
109 #define PORT_NOT_SET -1
\r
111 /* Officially supported ports */
\r
112 #define PORT_HWIndependent 0
\r
113 #define PORT_Win32 1
\r
114 #define PORT_Atmel_AT91SAM7 2
\r
115 #define PORT_ARM_CortexM 3
\r
116 #define PORT_Renesas_RX600 4
\r
118 /* Unofficial ports, provided by external developers and not yet verified */
\r
119 #define PORT_TEXAS_INSTRUMENTS_TMS570 6
\r
120 #define PORT_TEXAS_INSTRUMENTS_MSP430 7
\r
121 #define PORT_MICROCHIP_PIC32 8
\r
122 #define PORT_MICROCHIP_dsPIC_AND_PIC24 9
\r
123 #define PORT_XILINX_PPC405 10
\r
124 #define PORT_XILINX_PPC440 11
\r
126 /*** Select your port here! **************************************************/
\r
127 #define SELECTED_PORT PORT_Win32
\r
128 /*****************************************************************************/
\r
130 /*******************************************************************************
\r
131 * IRQ_PRIORITY_ORDER
\r
133 * Macro which should be defined as an integer of 0 or 1.
\r
135 * This should be 0 if lower irq priority values implies higher priority
\r
136 * levels, such as on ARM Cortex M. If the opposite scheme is used, i.e.,
\r
137 * if higher irq priority values means higher priority, this should be 1.
\r
139 * This setting is not critical. It is used only to sort and colorize the
\r
140 * interrupts in priority order, in case you record interrupts using
\r
141 * the vTraceStoreISRBegin and vTraceStoreISREnd routines.
\r
143 * We provide this setting for some hardware architectures below:
\r
144 * - ARM Cortex M: 0 (lower irq priority values are more significant)
\r
145 * - Atmel AT91SAM7x: 1 (higher irq priority values are more significant)
\r
146 * - Renesas RX62N: 1 (higher irq priority values are more significant)
\r
147 * - Microchip PIC24: 0 (lower irq priority values are more significant)
\r
148 * - Microchip dsPIC: 0 (lower irq priority values are more significant)
\r
149 * - TI TMS570 (ARM Cortex R4F): 0 (lower irq priority values are more significant)
\r
150 * - Freescale HCS08: 0 (lower irq priority values are more significant)
\r
151 * - Freescale HCS12: 0 (lower irq priority values are more significant)
\r
152 * - Freescale ColdFire: 1 (higher irq priority values are more significant)
\r
154 * If your chip is not on the above list, and you perhaps know this detail by
\r
155 * heart, please inform us by e-mail to support@percepio.se.
\r
156 ******************************************************************************/
\r
157 #define IRQ_PRIORITY_ORDER 0
\r
159 /*******************************************************************************
\r
162 * These four macros provides a hardware isolation layer, representing a
\r
163 * generic hardware timer/counter used for driving the operating system tick,
\r
164 * such as the SysTick feature of ARM Cortex M3/M4, or the PIT of the Atmel
\r
167 * HWTC_COUNT: The current value of the counter. This is expected to be reset
\r
168 * a each tick interrupt. Thus, when the tick handler starts, the counter has
\r
171 * HWTC_COUNT_DIRECTION: Should be one of:
\r
172 * - DIRECTION_INCREMENTING - for hardware timer/counters of incrementing type
\r
173 * such as the PIT on Atmel AT91SAM7X.
\r
174 * When the counter value reach HWTC_PERIOD, it is reset to zero and the
\r
175 * interrupt is signaled.
\r
176 * - DIRECTION_DECREMENTING - for hardware timer/counters of decrementing type
\r
177 * such as the SysTick on ARM Cortex M3/M4 chips.
\r
178 * When the counter value reach 0, it is reset to HWTC_PERIOD and the
\r
179 * interrupt is signaled.
\r
181 * HWTC_PERIOD: The number of increments or decrements of HWTC_COUNT between
\r
182 * two tick interrupts. This should preferably be mapped to the reload
\r
183 * register of the hardware timer, to make it more portable between chips in the
\r
184 * same family. The macro should in most cases be (reload register + 1).
\r
186 * HWTC_DIVISOR: If the timer frequency is very high, like on the Cortex M chips
\r
187 * (where the SysTick runs at the core clock frequency), the "differential
\r
188 * timestamping" used in the recorder will more frequently insert extra XTS
\r
189 * events to store the timestamps, which increases the event buffer usage.
\r
190 * In such cases, to reduce the number of XTS events and thereby get a longer
\r
191 * traces, you use HWTC_DIVISOR to scale down the timestamps and frequency.
\r
192 * Assuming a OS tick rate of 1 KHz, it is suggested to keep the effective timer
\r
193 * frequency below 65 MHz to avoid an excessive amount of XTS events. Thus, a
\r
194 * Cortex M chip running at 72 MHZ should use a HWTC_DIVISOR of 2, while a
\r
195 * faster chip require a higher HWTC_DIVISOR value.
\r
197 * The HWTC macros and uiTracePortGetTimeStamp is the main porting issue
\r
198 * or the trace recorder library. Typically you should not need to change
\r
199 * the code of uiTracePortGetTimeStamp if using the HWTC macros.
\r
201 * OFFER FROM PERCEPIO:
\r
202 * For silicon companies and non-corporate FreeRTOS users (researchers, students,
\r
203 * hobbyists or early-phase startups) we have an attractive offer:
\r
204 * Provide a hardware timer port and get a FREE single-user licence for
\r
205 * FreeRTOS+Trace Professional Edition. Read more about this offer at
\r
206 * www.percepio.se or contact us directly at support@percepio.se.
\r
208 ******************************************************************************/
\r
210 #if (SELECTED_PORT == PORT_Win32)
\r
212 #define HWTC_COUNT_DIRECTION DIRECTION_INCREMENTING
\r
213 #define HWTC_COUNT (ulGetRunTimeCounterValue())
\r
214 #define HWTC_PERIOD 0
\r
215 #define HWTC_DIVISOR 1
\r
217 #elif (SELECTED_PORT == PORT_HWIndependent)
\r
219 #define HWTC_COUNT_DIRECTION DIRECTION_INCREMENTING
\r
220 #define HWTC_COUNT 0
\r
221 #define HWTC_PERIOD 1
\r
222 #define HWTC_DIVISOR 1
\r
224 #elif (SELECTED_PORT == PORT_Atmel_AT91SAM7)
\r
226 /* HWTC_PERIOD is hardcoded for AT91SAM7X256-EK Board (48 MHz)
\r
227 A more generic solution is to get the period from pxPIT->PITC_PIMR */
\r
229 #define HWTC_COUNT_DIRECTION DIRECTION_INCREMENTING
\r
230 #define HWTC_COUNT (AT91C_BASE_PITC->PITC_PIIR & 0xFFFFF)
\r
231 #define HWTC_PERIOD 2995
\r
232 #define HWTC_DIVISOR 1
\r
234 #elif (SELECTED_PORT == PORT_ARM_CortexM)
\r
236 /* For all chips using ARM Cortex M cores */
\r
238 #define HWTC_COUNT_DIRECTION DIRECTION_DECREMENTING
\r
239 #define HWTC_COUNT (*((uint32_t*)0xE000E018))
\r
240 #define HWTC_PERIOD ((*(uint32_t*)0xE000E014) + 1)
\r
241 #define HWTC_DIVISOR 2
\r
243 #elif (SELECTED_PORT == PORT_Renesas_RX600)
\r
245 #include "iodefine.h"
\r
247 #define HWTC_COUNT_DIRECTION DIRECTION_INCREMENTING
\r
248 #define HWTC_COUNT (CMT0.CMCNT)
\r
249 #define HWTC_PERIOD ((((configPERIPHERAL_CLOCK_HZ/configTICK_RATE_HZ)-1)/8))
\r
250 #define HWTC_DIVISOR 1
\r
252 #elif (SELECTED_PORT == PORT_TEXAS_INSTRUMENTS_TMS570) /* UNOFFICIAL PORT - NOT YET VERIFIED BY PERCEPIO */
\r
254 #define RTIFRC0 *((uint32_t *)0xFFFFFC10)
\r
255 #define RTICOMP0 *((uint32_t *)0xFFFFFC50)
\r
256 #define RTIUDCP0 *((uint32_t *)0xFFFFFC54)
\r
257 #define HWTC_COUNT_DIRECTION DIRECTION_INCREMENTING
\r
258 #define HWTC_COUNT (RTIFRC0 - (RTICOMP0 - RTIUDCP0))
\r
259 #define HWTC_PERIOD (RTIUDCP0)
\r
260 #define HWTC_DIVISOR 1
\r
262 #elif (SELECTED_PORT == PORT_TEXAS_INSTRUMENTS_MSP430)
\r
263 /* UNOFFICIAL PORT - NOT YET VERIFIED BY PERCEPIO */
\r
265 #define HWTC_COUNT_DIRECTION DIRECTION_INCREMENTING
\r
266 #define HWTC_COUNT (TA0R)
\r
267 #define HWTC_PERIOD configCPU_CLOCKS_PER_TICK
\r
268 #define HWTC_DIVISOR 1
\r
270 #elif (SELECTED_PORT == PORT_MICROCHIP_PIC32)
\r
271 /* UNOFFICIAL PORT - NOT YET VERIFIED BY PERCEPIO */
\r
273 #define HWTC_COUNT_DIRECTION DIRECTION_INCREMENTING
\r
274 #define HWTC_COUNT (ReadTimer1()) /* Should be available in BSP */
\r
275 #define HWTC_PERIOD (ReadPeriod1()+1) /* Should be available in BSP */
\r
276 #define HWTC_DIVISOR 1
\r
278 #elif (SELECTED_PORT == PORT_MICROCHIP_dsPIC_AND_PIC24)
\r
279 /* UNOFFICIAL PORT - NOT YET VERIFIED BY PERCEPIO */
\r
281 #define HWTC_COUNT_DIRECTION DIRECTION_INCREMENTING
\r
282 #define HWTC_COUNT (PR1)
\r
283 #define HWTC_PERIOD ((configCPU_CLOCK_HZ/portTIMER_PRESCALE)/configTICK_RATE_HZ)
\r
284 #define HWTC_DIVISOR 1
\r
286 #elif (SELECTED_PORT == PORT_XILINX_PPC405)
\r
287 /* UNOFFICIAL PORT - NOT YET VERIFIED BY PERCEPIO */
\r
289 #define HWTC_COUNT_DIRECTION DIRECTION_DECREMENTING
\r
290 #define HWTC_COUNT mfspr( 0x3db)
\r
291 #define HWTC_PERIOD ( configCPU_CLOCK_HZ / configTICK_RATE_HZ )
\r
292 #define HWTC_DIVISOR 1
\r
294 #elif (SELECTED_PORT == PORT_XILINX_PPC440)
\r
295 /* UNOFFICIAL PORT - NOT YET VERIFIED BY PERCEPIO */
\r
297 /* This should work with most PowerPC chips */
\r
299 #define HWTC_COUNT_DIRECTION DIRECTION_DECREMENTING
\r
300 #define HWTC_COUNT mfspr( 0x016 )
\r
301 #define HWTC_PERIOD ( configCPU_CLOCK_HZ / configTICK_RATE_HZ )
\r
302 #define HWTC_DIVISOR 1
\r
305 SELECTED_PORT is not set, or had unsupported value!
\r
306 (This is to intentionally cause a compiler error.)
\r
309 #ifndef HWTC_COUNT_DIRECTION
\r
310 HWTC_COUNT_DIRECTION is not set!
\r
311 (This is to intentionally cause a compiler error.)
\r
315 HWTC_COUNT is not set!
\r
316 (This is to intentionally cause a compiler error.)
\r
319 #ifndef HWTC_PERIOD
\r
320 HWTC_PERIOD is not set!
\r
321 (This is to intentionally cause a compiler error.)
\r
324 #ifndef HWTC_DIVISOR
\r
325 HWTC_DIVISOR is not set!
\r
326 (This is to intentionally cause a compiler error.)
\r
329 #ifndef IRQ_PRIORITY_ORDER
\r
330 IRQ_PRIORITY_ORDER is not set!
\r
331 (This is to intentionally cause a compiler error.)
\r
334 #if (IRQ_PRIORITY_ORDER != 0) && (IRQ_PRIORITY_ORDER != 1)
\r
335 IRQ_PRIORITY_ORDER has bad value!
\r
336 (This is to intentionally cause a compiler error.)
\r
339 #if (HWTC_DIVISOR < 1)
\r
340 HWTC_DIVISOR must be a non-zero positive value!
\r
341 (This is to intentionally cause a compiler error.)
\r
344 #if ((IRQ_PRIORITY_ORDER != 0) && (IRQ_PRIORITY_ORDER != 1))
\r
345 IRQ_PRIORITY_ORDER not set!
\r
346 (This is to intentionally cause a compiler error.)
\r
349 /*******************************************************************************
\r
350 * vTraceConsoleMessage
\r
352 * A wrapper for your system-specific console "printf" console output function.
\r
353 * This needs to be correctly defined to see status reports from the trace
\r
354 * status monitor task (this is defined in trcUser.c).
\r
355 ******************************************************************************/
\r
356 #define vTraceConsoleMessage printf
\r
358 /*******************************************************************************
\r
359 * uiTracePortGetTimeStamp
\r
361 * Returns the current time based on the HWTC macros which provide a hardware
\r
362 * isolation layer towards the hardware timer/counter.
\r
364 * The HWTC macros and uiTracePortGetTimeStamp is the main porting issue
\r
365 * or the trace recorder library. Typically you should not need to change
\r
366 * the code of uiTracePortGetTimeStamp if using the HWTC macros.
\r
368 * OFFER FROM PERCEPIO:
\r
369 * For silicon companies and non-corporate FreeRTOS users (researchers,
\r
370 * students, hobbyists or early-phase startups) we have an attractive offer:
\r
371 * Provide a hardware timer port and get a FREE single-user licence for
\r
372 * FreeRTOS+Trace Professional Edition. Read more about this offer at
\r
373 * www.percepio.se or contact us directly at support@percepio.se.
\r
374 ******************************************************************************/
\r
375 uint32_t uiTracePortGetTimeStamp(void);
\r
377 /*******************************************************************************
\r
378 * vTracePortSetFrequency
\r
380 * Registers the frequency of the timer used. This is normally calculated
\r
381 * automatically from the HWTC macros, but the Win32 port requires a special
\r
382 * solution where the frequency can be set independently of the HWTC macros.
\r
383 * This is called from main in the Win32 demo program.
\r
384 ******************************************************************************/
\r
385 void vTracePortSetFrequency(uint32_t freq);
\r
387 /*******************************************************************************
\r
390 * This function is called when the recorder is stopped due to full buffer.
\r
391 * Mainly intended to show a message in the console.
\r
392 * This is used by the Win32 port to store the trace to a file. The file path is
\r
393 * set using vTracePortSetFileName.
\r
394 ******************************************************************************/
\r
395 void vTracePortEnd(void);
\r
397 #if (INCLUDE_SAVE_TO_FILE == 1)
\r
399 /*******************************************************************************
\r
400 * vTracePortSetOutFile
\r
402 * Sets the filename/path used in vTracePortSave.
\r
403 * This is set in a separate function, since the Win32 port calls vTracePortSave
\r
404 * in vTracePortEnd if WIN32_PORT_SAVE_WHEN_STOPPED is set.
\r
405 ******************************************************************************/
\r
406 void vTracePortSetOutFile(char* path);
\r
408 /******************************************************************************
\r
411 * Saves the trace to a file on a target-side file system. The path is set in a
\r
412 * separate function, vTracePortSetOutFile, since the Win32 port may call
\r
413 * vTracePortSave in vTracePortEnd, if using WIN32_PORT_SAVE_WHEN_STOPPED.
\r
414 ******************************************************************************/
\r
415 void vTracePortSave(void);
\r