1 /*******************************************************************************
\r
2 * Tracealyzer v2.7.0 Recorder Library
\r
3 * Percepio AB, www.percepio.com
\r
7 * Contains together with trcHardwarePort.c all hardware portability issues of
\r
8 * the trace recorder library.
\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 * Tabs are used for indent in this file (1 tab = 4 spaces)
\r
37 * Copyright Percepio AB, 2014.
\r
39 ******************************************************************************/
\r
49 #define _WIN32_WINNT 0x0600
\r
51 /* Standard includes. */
\r
53 #include <windows.h>
\r
56 /*******************************************************************************
\r
57 * The Win32 port by default saves the trace to file and then kills the
\r
58 * program when the recorder is stopped, to facilitate quick, simple tests
\r
60 ******************************************************************************/
\r
61 #define WIN32_PORT_SAVE_WHEN_STOPPED 1
\r
62 #define WIN32_PORT_EXIT_WHEN_STOPPED 1
\r
66 #define DIRECTION_INCREMENTING 1
\r
67 #define DIRECTION_DECREMENTING 2
\r
69 /******************************************************************************
\r
72 * PORT_HWIndependent
\r
73 * A hardware independent fallback option for event timestamping. Provides low
\r
74 * resolution timestamps based on the OS tick.
\r
75 * This may be used on the Win32 port, but may also be used on embedded hardware
\r
76 * platforms. All time durations will be truncated to the OS tick frequency,
\r
77 * typically 1 KHz. This means that a task or ISR that executes in less than
\r
78 * 1 ms get an execution time of zero.
\r
80 * PORT_APPLICATION_DEFINED
\r
81 * Allows for defining the port macros in other source code files.
\r
84 * "Accurate" timestamping based on the Windows performance counter for Win32
\r
85 * builds. Note that this gives the host machine time, not the kernel time.
\r
87 * Hardware specific ports
\r
88 * To get accurate timestamping, a hardware timer is necessary. Below are the
\r
89 * available ports. Some of these are "unofficial", meaning that
\r
90 * they have not yet been verified by Percepio but have been contributed by
\r
91 * external developers. They should work, otherwise let us know by emailing
\r
92 * support@percepio.com. Some work on any OS platform, while other are specific
\r
93 * to a certain operating system.
\r
94 *****************************************************************************/
\r
96 /****** Port Name ********************** Code ***** Official ** OS Platform *********/
\r
97 #define PORT_APPLICATION_DEFINED -2 /* - - */
\r
98 #define PORT_NOT_SET -1 /* - - */
\r
99 #define PORT_HWIndependent 0 /* Yes Any */
\r
100 #define PORT_Win32 1 /* Yes FreeRTOS on Win32 */
\r
101 #define PORT_Atmel_AT91SAM7 2 /* No Any */
\r
102 #define PORT_Atmel_UC3A0 3 /* No Any */
\r
103 #define PORT_ARM_CortexM 4 /* Yes Any */
\r
104 #define PORT_Renesas_RX600 5 /* Yes Any */
\r
105 #define PORT_Microchip_dsPIC_AND_PIC24 6 /* Yes Any */
\r
106 #define PORT_TEXAS_INSTRUMENTS_TMS570 7 /* No Any */
\r
107 #define PORT_TEXAS_INSTRUMENTS_MSP430 8 /* No Any */
\r
108 #define PORT_MICROCHIP_PIC32MX 9 /* Yes Any */
\r
109 #define PORT_XILINX_PPC405 10 /* No FreeRTOS */
\r
110 #define PORT_XILINX_PPC440 11 /* No FreeRTOS */
\r
111 #define PORT_XILINX_MICROBLAZE 12 /* No Any */
\r
112 #define PORT_NXP_LPC210X 13 /* No Any */
\r
113 #define PORT_MICROCHIP_PIC32MZ 14 /* Yes Any */
\r
114 #define PORT_ARM_CORTEX_A9 15 /* No Any */
\r
116 #include "trcConfig.h"
\r
118 /*******************************************************************************
\r
119 * IRQ_PRIORITY_ORDER
\r
121 * Macro which should be defined as an integer of 0 or 1.
\r
123 * This should be 0 if lower IRQ priority values implies higher priority
\r
124 * levels, such as on ARM Cortex M. If the opposite scheme is used, i.e.,
\r
125 * if higher IRQ priority values means higher priority, this should be 1.
\r
127 * This setting is not critical. It is used only to sort and colorize the
\r
128 * interrupts in priority order, in case you record interrupts using
\r
129 * the vTraceStoreISRBegin and vTraceStoreISREnd routines.
\r
131 ******************************************************************************
\r
135 * These four HWTC macros provides a hardware isolation layer representing a
\r
136 * generic hardware timer/counter used for driving the operating system tick,
\r
137 * such as the SysTick feature of ARM Cortex M3/M4, or the PIT of the Atmel
\r
140 * HWTC_COUNT: The current value of the counter. This is expected to be reset
\r
141 * a each tick interrupt. Thus, when the tick handler starts, the counter has
\r
144 * HWTC_COUNT_DIRECTION: Should be one of:
\r
145 * - DIRECTION_INCREMENTING - for hardware timer/counters of incrementing type
\r
146 * such as the PIT on Atmel AT91SAM7X.
\r
147 * When the counter value reach HWTC_PERIOD, it is reset to zero and the
\r
148 * interrupt is signaled.
\r
149 * - DIRECTION_DECREMENTING - for hardware timer/counters of decrementing type
\r
150 * such as the SysTick on ARM Cortex M3/M4 chips.
\r
151 * When the counter value reach 0, it is reset to HWTC_PERIOD and the
\r
152 * interrupt is signaled.
\r
154 * HWTC_PERIOD: The number of increments or decrements of HWTC_COUNT between
\r
155 * two OS tick interrupts. This should preferably be mapped to the reload
\r
156 * register of the hardware timer, to make it more portable between chips in the
\r
157 * same family. The macro should in most cases be (reload register + 1).
\r
158 * For FreeRTOS, this can in most cases be defined as
\r
159 * #define HWTC_PERIOD (configCPU_CLOCK_HZ / configTICK_RATE_HZ)
\r
161 * HWTC_DIVISOR: If the timer frequency is very high, like on the Cortex M chips
\r
162 * (where the SysTick runs at the core clock frequency), the "differential
\r
163 * timestamping" used in the recorder will more frequently insert extra XTS
\r
164 * events to store the timestamps, which increases the event buffer usage.
\r
165 * In such cases, to reduce the number of XTS events and thereby get longer
\r
166 * traces, you use HWTC_DIVISOR to scale down the timestamps and frequency.
\r
167 * Assuming a OS tick rate of 1 KHz, it is suggested to keep the effective timer
\r
168 * frequency below 65 MHz to avoid an excessive amount of XTS events. Thus, a
\r
169 * Cortex M chip running at 72 MHZ should use a HWTC_DIVISOR of 2, while a
\r
170 * faster chip require a higher HWTC_DIVISOR value.
\r
172 * The HWTC macros and vTracePortGetTimeStamp is the main porting issue
\r
173 * or the trace recorder library. Typically you should not need to change
\r
174 * the code of vTracePortGetTimeStamp if using the HWTC macros.
\r
176 ******************************************************************************/
\r
178 #if (SELECTED_PORT == PORT_Win32)
\r
179 // This can be used as a template for any free-running 32-bit counter
\r
180 #define HWTC_COUNT_DIRECTION DIRECTION_INCREMENTING
\r
181 #define HWTC_COUNT (ulGetRunTimeCounterValue())
\r
182 #define HWTC_PERIOD 0
\r
183 #define HWTC_DIVISOR 1
\r
185 // Please update according to your system...
\r
186 #define IRQ_PRIORITY_ORDER 1
\r
188 #elif (SELECTED_PORT == PORT_HWIndependent)
\r
189 // OS Tick only (typically 1 ms resolution)
\r
190 #define HWTC_COUNT_DIRECTION DIRECTION_INCREMENTING
\r
191 #define HWTC_COUNT 0
\r
192 #define HWTC_PERIOD 1
\r
193 #define HWTC_DIVISOR 1
\r
195 // Please update according to your system...
\r
196 #define IRQ_PRIORITY_ORDER NOT_SET
\r
199 #elif (SELECTED_PORT == PORT_ARM_CortexM)
\r
201 void prvTraceInitCortexM(void);
\r
203 #define REG_DEMCR (*(volatile unsigned int*)0xE000EDFC)
\r
204 #define REG_DWT_CTRL (*(volatile unsigned int*)0xE0001000)
\r
205 #define REG_DWT_CYCCNT (*(volatile unsigned int*)0xE0001004)
\r
206 #define REG_DWT_EXCCNT (*(volatile unsigned int*)0xE000100C)
\r
208 /* Bit mask for TRCENA bit in DEMCR - Global enable for DWT and ITM */
\r
209 #define DEMCR_TRCENA (1 << 24)
\r
211 /* Bit mask for NOPRFCNT bit in DWT_CTRL. If 1, DWT_EXCCNT is not supported */
\r
212 #define DWT_CTRL_NOPRFCNT (1 << 24)
\r
214 /* Bit mask for NOCYCCNT bit in DWT_CTRL. If 1, DWT_CYCCNT is not supported */
\r
215 #define DWT_CTRL_NOCYCCNT (1 << 25)
\r
217 /* Bit mask for EXCEVTENA_ bit in DWT_CTRL. Set to 1 to enable DWT_EXCCNT */
\r
218 #define DWT_CTRL_EXCEVTENA (1 << 18)
\r
220 /* Bit mask for EXCEVTENA_ bit in DWT_CTRL. Set to 1 to enable DWT_CYCCNT */
\r
221 #define DWT_CTRL_CYCCNTENA (1)
\r
223 #define PORT_SPECIFIC_INIT() prvTraceInitCortexM()
\r
225 extern uint32_t DWT_CYCLES_ADDED;
\r
227 #define HWTC_COUNT_DIRECTION DIRECTION_INCREMENTING
\r
228 #define HWTC_COUNT (REG_DWT_CYCCNT + DWT_CYCLES_ADDED)
\r
229 #define HWTC_PERIOD 0
\r
230 #define HWTC_DIVISOR 4
\r
232 #define IRQ_PRIORITY_ORDER 0 // lower IRQ priority values are more significant
\r
234 #elif (SELECTED_PORT == PORT_Renesas_RX600)
\r
236 #include "iodefine.h"
\r
238 #define HWTC_COUNT_DIRECTION DIRECTION_INCREMENTING
\r
239 #define HWTC_COUNT (CMT0.CMCNT)
\r
240 #define HWTC_PERIOD (CMT0.CMCOR + 1)
\r
241 #define HWTC_DIVISOR 1
\r
242 #define IRQ_PRIORITY_ORDER 1 // higher IRQ priority values are more significant
\r
244 #elif (SELECTED_PORT == PORT_MICROCHIP_PIC32MX || SELECTED_PORT == PORT_MICROCHIP_PIC32MZ)
\r
246 #define HWTC_COUNT_DIRECTION DIRECTION_INCREMENTING
\r
247 #define HWTC_COUNT (TMR1)
\r
248 #define HWTC_PERIOD (PR1 + 1)
\r
249 #define HWTC_DIVISOR 1
\r
250 #define IRQ_PRIORITY_ORDER 0 // lower IRQ priority values are more significant
\r
252 #elif (SELECTED_PORT == PORT_Microchip_dsPIC_AND_PIC24)
\r
254 /* For Microchip PIC24 and dsPIC (16 bit) */
\r
256 /* Note: The trace library is designed for 32-bit MCUs and is slower than
\r
257 intended on 16-bit MCUs. Storing an event on a PIC24 takes about 70 usec.
\r
258 In comparison, this is 10-20 times faster on a 32-bit MCU... */
\r
260 #define HWTC_COUNT_DIRECTION DIRECTION_INCREMENTING
\r
261 #define HWTC_COUNT (TMR1)
\r
262 #define HWTC_PERIOD (PR1+1)
\r
263 #define HWTC_DIVISOR 1
\r
264 #define IRQ_PRIORITY_ORDER 0 // lower IRQ priority values are more significant
\r
266 #elif (SELECTED_PORT == PORT_Atmel_AT91SAM7)
\r
268 /* UNOFFICIAL PORT - NOT YET VERIFIED BY PERCEPIO */
\r
270 #define HWTC_COUNT_DIRECTION DIRECTION_INCREMENTING
\r
271 #define HWTC_COUNT ((uint32_t)(AT91C_BASE_PITC->PITC_PIIR & 0xFFFFF))
\r
272 #define HWTC_PERIOD ((uint32_t)(AT91C_BASE_PITC->PITC_PIMR + 1))
\r
273 #define HWTC_DIVISOR 1
\r
274 #define IRQ_PRIORITY_ORDER 1 // higher IRQ priority values are more significant
\r
276 #elif (SELECTED_PORT == PORT_Atmel_UC3A0)
\r
278 /* UNOFFICIAL PORT - NOT YET VERIFIED BY PERCEPIO */
\r
279 /* For Atmel AVR32 (AT32UC3A).*/
\r
281 #define HWTC_COUNT_DIRECTION DIRECTION_INCREMENTING
\r
282 #define HWTC_COUNT ((uint32_t)sysreg_read(AVR32_COUNT))
\r
283 #define HWTC_PERIOD ((uint32_t)(sysreg_read(AVR32_COMPARE) + 1))
\r
284 #define HWTC_DIVISOR 1
\r
285 #define IRQ_PRIORITY_ORDER 1 // higher IRQ priority values are more significant
\r
287 #elif (SELECTED_PORT == PORT_NXP_LPC210X)
\r
289 /* UNOFFICIAL PORT - NOT YET VERIFIED BY PERCEPIO */
\r
290 /* Tested with LPC2106, but should work with most LPC21XX chips. */
\r
292 #define HWTC_COUNT_DIRECTION DIRECTION_INCREMENTING
\r
293 #define HWTC_COUNT *((uint32_t *)0xE0004008 )
\r
294 #define HWTC_PERIOD *((uint32_t *)0xE0004018 )
\r
295 #define HWTC_DIVISOR 1
\r
296 #define IRQ_PRIORITY_ORDER 0 // lower IRQ priority values are more significant
\r
298 #elif (SELECTED_PORT == PORT_TEXAS_INSTRUMENTS_TMS570)
\r
300 /* UNOFFICIAL PORT - NOT YET VERIFIED BY PERCEPIO */
\r
302 #define RTIFRC0 *((uint32_t *)0xFFFFFC10)
\r
303 #define RTICOMP0 *((uint32_t *)0xFFFFFC50)
\r
304 #define RTIUDCP0 *((uint32_t *)0xFFFFFC54)
\r
305 #define HWTC_COUNT_DIRECTION DIRECTION_INCREMENTING
\r
306 #define HWTC_COUNT (RTIFRC0 - (RTICOMP0 - RTIUDCP0))
\r
307 #define HWTC_PERIOD (RTIUDCP0)
\r
308 #define HWTC_DIVISOR 1
\r
310 #define IRQ_PRIORITY_ORDER 0 // lower IRQ priority values are more significant
\r
312 #elif (SELECTED_PORT == PORT_TEXAS_INSTRUMENTS_MSP430)
\r
314 /* UNOFFICIAL PORT - NOT YET VERIFIED BY PERCEPIO */
\r
316 #define HWTC_COUNT_DIRECTION DIRECTION_INCREMENTING
\r
317 #define HWTC_COUNT (TA0R)
\r
318 #define HWTC_PERIOD (((uint16_t)TACCR0)+1)
\r
319 #define HWTC_DIVISOR 1
\r
320 #define IRQ_PRIORITY_ORDER 1 // higher IRQ priority values are more significant
\r
322 #elif (SELECTED_PORT == PORT_XILINX_PPC405)
\r
324 /* UNOFFICIAL PORT - NOT YET VERIFIED BY PERCEPIO */
\r
326 #define HWTC_COUNT_DIRECTION DIRECTION_DECREMENTING
\r
327 #define HWTC_COUNT mfspr(0x3db)
\r
328 #if (defined configCPU_CLOCK_HZ && defined configTICK_RATE_HZ) // Check if FreeRTOS
\r
329 /* For FreeRTOS only - found no generic OS independent solution for the PPC405 architecture. */
\r
330 #define HWTC_PERIOD ( configCPU_CLOCK_HZ / configTICK_RATE_HZ ) // Same as in port.c for PPC405
\r
332 /* Not defined for other operating systems yet */
\r
333 #error HWTC_PERIOD must be defined to give the number of hardware timer ticks per OS tick.
\r
335 #define HWTC_DIVISOR 1
\r
336 #define IRQ_PRIORITY_ORDER 0 // lower IRQ priority values are more significant
\r
338 #elif (SELECTED_PORT == PORT_XILINX_PPC440)
\r
340 /* UNOFFICIAL PORT - NOT YET VERIFIED BY PERCEPIO */
\r
341 /* This should work with most PowerPC chips */
\r
343 #define HWTC_COUNT_DIRECTION DIRECTION_DECREMENTING
\r
344 #define HWTC_COUNT mfspr(0x016)
\r
345 #if (defined configCPU_CLOCK_HZ && defined configTICK_RATE_HZ) // Check if FreeRTOS
\r
346 /* For FreeRTOS only - found no generic OS independent solution for the PPC440 architecture. */
\r
347 #define HWTC_PERIOD ( configCPU_CLOCK_HZ / configTICK_RATE_HZ ) // Same as in port.c for PPC440
\r
349 /* Not defined for other operating systems yet */
\r
350 #error HWTC_PERIOD must be defined to give the number of hardware timer ticks per OS tick.
\r
352 #define HWTC_DIVISOR 1
\r
353 #define IRQ_PRIORITY_ORDER 0 // lower IRQ priority values are more significant
\r
355 #elif (SELECTED_PORT == PORT_XILINX_MICROBLAZE)
\r
357 /* UNOFFICIAL PORT - NOT YET VERIFIED BY PERCEPIO */
\r
359 /* This should work with most Microblaze configurations.
\r
360 * It uses the AXI Timer 0 - the tick interrupt source.
\r
361 * If an AXI Timer 0 peripheral is available on your hardware platform, no modifications are required.
\r
363 #include "xtmrctr_l.h"
\r
365 #define HWTC_COUNT_DIRECTION DIRECTION_DECREMENTING
\r
366 #define HWTC_COUNT XTmrCtr_GetTimerCounterReg( XPAR_TMRCTR_0_BASEADDR, 0 )
\r
367 #define HWTC_PERIOD (XTmrCtr_mGetLoadReg( XPAR_TMRCTR_0_BASEADDR, 0) + 1)
\r
368 #define HWTC_DIVISOR 16
\r
369 #define IRQ_PRIORITY_ORDER 0 // lower IRQ priority values are more significant
\r
371 #elif (SELECTED_PORT == PORT_ARM_CORTEX_A9)
\r
373 /* UNOFFICIAL PORT - NOT YET VERIFIED BY PERCEPIO */
\r
375 #define CA9_MPCORE_PRIVCTR_CONTROL_PRESCALER_MASK 0x0000FF00
\r
376 #define CA9_MPCORE_PRIVCTR_CONTROL_PRESCALER_SHIFT 8
\r
378 #define CA9_MPCORE_PRIVCTR_PERIOD_REG (*(volatile uint32_t*)(0xF8F00600 + 0))
\r
379 #define CA9_MPCORE_PRIVCTR_COUNTER_REG (*(volatile uint32_t*)(0xF8F00600 + 4))
\r
380 #define CA9_MPCORE_PRIVCTR_CONTROL_REG (*(volatile uint32_t*)(0xF8F00600 + 8))
\r
382 #define CA9_MPCORE_PRIVCTR_PRESCALER (((CA9_MPCORE_PRIVCTR_CONTROL_REG & CA9_MPCORE_PRIVCTR_CONTROL_PRESCALER_MASK) >> CA9_MPCORE_PRIVCTR_CONTROL_PRESCALER_SHIFT) + 1)
\r
385 #define HWTC_COUNT_DIRECTION DIRECTION_DECREMENTING
\r
386 #define HWTC_COUNT CA9_MPCORE_PRIVCTR_COUNTER_REG
\r
387 #define HWTC_PERIOD ((CA9_MPCORE_PRIVCTR_PERIOD_REG * CA9_MPCORE_PRIVCTR_PRESCALER) + 1)
\r
389 //NOTE: The private timer ticks with a very high frequency (half the core-clock usually),
\r
390 //but offers the possibility to apply a prescaler. Depending on the prescaler you set the
\r
391 //HWTC_DIVISOR may need to be raised. Refer to the notes at the beginning of this file
\r
392 //for more information.
\r
393 #define HWTC_DIVISOR 1
\r
395 #define IRQ_PRIORITY_ORDER 0 // lower IRQ priority values are more significant
\r
397 #elif (SELECTED_PORT == PORT_APPLICATION_DEFINED)
\r
399 #if !( defined (HWTC_COUNT_DIRECTION) && defined (HWTC_COUNT) && defined (HWTC_PERIOD) && defined (HWTC_DIVISOR) && defined (IRQ_PRIORITY_ORDER) )
\r
400 #error SELECTED_PORT is PORT_APPLICATION_DEFINED but not all of the necessary constants have been defined.
\r
403 #elif (SELECTED_PORT != PORT_NOT_SET)
\r
405 #error "SELECTED_PORT had unsupported value!"
\r
406 #define SELECTED_PORT PORT_NOT_SET
\r
410 #if (SELECTED_PORT != PORT_NOT_SET)
\r
412 #ifndef HWTC_COUNT_DIRECTION
\r
413 #error "HWTC_COUNT_DIRECTION is not set!"
\r
417 #error "HWTC_COUNT is not set!"
\r
420 #ifndef HWTC_PERIOD
\r
421 #error "HWTC_PERIOD is not set!"
\r
424 #ifndef HWTC_DIVISOR
\r
425 #error "HWTC_DIVISOR is not set!"
\r
428 #ifndef IRQ_PRIORITY_ORDER
\r
429 #error "IRQ_PRIORITY_ORDER is not set!"
\r
430 #elif (IRQ_PRIORITY_ORDER != 0) && (IRQ_PRIORITY_ORDER != 1)
\r
431 #error "IRQ_PRIORITY_ORDER has bad value!"
\r
434 #if (HWTC_DIVISOR < 1)
\r
435 #error "HWTC_DIVISOR must be a non-zero positive value!"
\r
439 /*******************************************************************************
\r
440 * vTraceConsoleMessage
\r
442 * A wrapper for your system-specific console "printf" console output function.
\r
443 * This needs to be correctly defined to see status reports from the trace
\r
444 * status monitor task (this is defined in trcUser.c).
\r
445 ******************************************************************************/
\r
446 #define vTraceConsoleMessage(x)
\r
448 /*******************************************************************************
\r
449 * vTracePortGetTimeStamp
\r
451 * Returns the current time based on the HWTC macros which provide a hardware
\r
452 * isolation layer towards the hardware timer/counter.
\r
454 * The HWTC macros and vTracePortGetTimeStamp is the main porting issue
\r
455 * or the trace recorder library. Typically you should not need to change
\r
456 * the code of vTracePortGetTimeStamp if using the HWTC macros.
\r
458 ******************************************************************************/
\r
459 void vTracePortGetTimeStamp(uint32_t *puiTimestamp);
\r