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