]> git.sur5r.net Git - freertos/blob - Demo/PIC24_MPLAB/timertest.c
Ready for V5.2.0 release.
[freertos] / Demo / PIC24_MPLAB / timertest.c
1 /*\r
2         FreeRTOS.org V5.2.0 - Copyright (C) 2003-2009 Richard Barry.\r
3 \r
4         This file is part of the FreeRTOS.org distribution.\r
5 \r
6         FreeRTOS.org is free software; you can redistribute it and/or modify it \r
7         under the terms of the GNU General Public License (version 2) as published\r
8         by the Free Software Foundation and modified by the FreeRTOS exception.\r
9 \r
10         FreeRTOS.org is distributed in the hope that it will be useful, but WITHOUT\r
11         ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or \r
12         FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for \r
13         more details.\r
14 \r
15         You should have received a copy of the GNU General Public License along \r
16         with FreeRTOS.org; if not, write to the Free Software Foundation, Inc., 59 \r
17         Temple Place, Suite 330, Boston, MA  02111-1307  USA.\r
18 \r
19         A special exception to the GPL is included to allow you to distribute a \r
20         combined work that includes FreeRTOS.org without being obliged to provide\r
21         the source code for any proprietary components.  See the licensing section\r
22         of http://www.FreeRTOS.org for full details.\r
23 \r
24 \r
25         ***************************************************************************\r
26         *                                                                         *\r
27         * Get the FreeRTOS eBook!  See http://www.FreeRTOS.org/Documentation      *\r
28         *                                                                         *\r
29         * This is a concise, step by step, 'hands on' guide that describes both   *\r
30         * general multitasking concepts and FreeRTOS specifics. It presents and   *\r
31         * explains numerous examples that are written using the FreeRTOS API.     *\r
32         * Full source code for all the examples is provided in an accompanying    *\r
33         * .zip file.                                                              *\r
34         *                                                                         *\r
35         ***************************************************************************\r
36 \r
37         1 tab == 4 spaces!\r
38 \r
39         Please ensure to read the configuration and relevant port sections of the\r
40         online documentation.\r
41 \r
42         http://www.FreeRTOS.org - Documentation, latest information, license and\r
43         contact details.\r
44 \r
45         http://www.SafeRTOS.com - A version that is certified for use in safety\r
46         critical systems.\r
47 \r
48         http://www.OpenRTOS.com - Commercial support, development, porting,\r
49         licensing and training services.\r
50 */\r
51 \r
52 /* High speed timer test as described in main.c. */\r
53 \r
54 \r
55 /* Scheduler includes. */\r
56 #include "FreeRTOS.h"\r
57 \r
58 /* Demo includes. */\r
59 #include "partest.h"\r
60 \r
61 /* The number of interrupts to pass before we start looking at the jitter. */\r
62 #define timerSETTLE_TIME                        5\r
63 \r
64 /* The maximum value the 16bit timer can contain. */\r
65 #define timerMAX_COUNT                          0xffff\r
66 \r
67 /*-----------------------------------------------------------*/\r
68 \r
69 /*\r
70  * Measure the time between this interrupt and the previous interrupt to \r
71  * calculate the timing jitter.  Remember the maximum value the jitter has\r
72  * ever been calculated to be.\r
73  */\r
74 static void prvCalculateAndStoreJitter( void );\r
75 \r
76 /*-----------------------------------------------------------*/\r
77 \r
78 /* The maximum time (in processor clocks) between two consecutive timer\r
79 interrupts so far. */\r
80 unsigned portSHORT usMaxJitter = 0;\r
81 \r
82 /*-----------------------------------------------------------*/\r
83 \r
84 void vSetupTimerTest( unsigned portSHORT usFrequencyHz )\r
85 {\r
86         /* T2 is used to generate interrupts.  T4 is used to provide an accurate\r
87         time measurement. */\r
88         T2CON = 0;\r
89         T4CON = 0;\r
90         TMR2 = 0;\r
91         TMR4 = 0;\r
92 \r
93         /* Timer 2 is going to interrupt at usFrequencyHz Hz. */\r
94         PR2 = ( unsigned portSHORT ) ( configCPU_CLOCK_HZ / ( unsigned portLONG ) usFrequencyHz );\r
95 \r
96         /* Timer 4 is going to free run from minimum to maximum value. */\r
97         PR4 = ( unsigned portSHORT ) timerMAX_COUNT;\r
98 \r
99         /* Setup timer 2 interrupt priority to be above the kernel priority so \r
100         the timer jitter is not effected by the kernel activity. */\r
101         IPC1bits.T2IP = configKERNEL_INTERRUPT_PRIORITY + 1;\r
102 \r
103         /* Clear the interrupt as a starting condition. */\r
104         IFS0bits.T2IF = 0;\r
105 \r
106         /* Enable the interrupt. */\r
107         IEC0bits.T2IE = 1;\r
108 \r
109         /* Start both timers. */\r
110         T2CONbits.TON = 1;\r
111         T4CONbits.TON = 1;\r
112 }\r
113 /*-----------------------------------------------------------*/\r
114 \r
115 static void prvCalculateAndStoreJitter( void )\r
116 {\r
117 static unsigned portSHORT usLastCount = 0, usSettleCount = 0;\r
118 unsigned portSHORT usThisCount, usDifference;\r
119 \r
120         /* Capture the timer value as we enter the interrupt. */\r
121         usThisCount = TMR4;\r
122 \r
123         if( usSettleCount >= timerSETTLE_TIME )\r
124         {\r
125                 /* What is the difference between the timer value in this interrupt\r
126                 and the value from the last interrupt. */\r
127                 usDifference = usThisCount - usLastCount;\r
128 \r
129                 /* Store the difference in the timer values if it is larger than the\r
130                 currently stored largest value.  The difference over and above the \r
131                 expected difference will give the 'jitter' in the processing of these\r
132                 interrupts. */\r
133                 if( usDifference > usMaxJitter )\r
134                 {\r
135                         usMaxJitter = usDifference;\r
136                 }\r
137         }\r
138         else\r
139         {\r
140                 /* Don't bother storing any values for the first couple of \r
141                 interrupts. */\r
142                 usSettleCount++;\r
143         }\r
144 \r
145         /* Remember what the timer value was this time through, so we can calculate\r
146         the difference the next time through. */\r
147         usLastCount = usThisCount;\r
148 }\r
149 /*-----------------------------------------------------------*/\r
150 \r
151 void __attribute__((__interrupt__, auto_psv)) _T2Interrupt( void )\r
152 {\r
153         /* Work out the time between this and the previous interrupt. */\r
154         prvCalculateAndStoreJitter();\r
155 \r
156         /* Clear the timer interrupt. */\r
157         IFS0bits.T2IF = 0;\r
158 }\r
159 \r
160 \r