]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/PIC24_MPLAB/timertest.c
Roll up the minor changes checked into svn since V10.0.0 into new V10.0.1 ready for...
[freertos] / FreeRTOS / Demo / PIC24_MPLAB / timertest.c
1 /*\r
2  * FreeRTOS Kernel V10.0.1\r
3  * Copyright (C) 2017 Amazon.com, Inc. or its affiliates.  All Rights Reserved.\r
4  *\r
5  * Permission is hereby granted, free of charge, to any person obtaining a copy of\r
6  * this software and associated documentation files (the "Software"), to deal in\r
7  * the Software without restriction, including without limitation the rights to\r
8  * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of\r
9  * the Software, and to permit persons to whom the Software is furnished to do so,\r
10  * subject to the following conditions:\r
11  *\r
12  * The above copyright notice and this permission notice shall be included in all\r
13  * copies or substantial portions of the Software.\r
14  *\r
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\r
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS\r
17  * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR\r
18  * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER\r
19  * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN\r
20  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\r
21  *\r
22  * http://www.FreeRTOS.org\r
23  * http://aws.amazon.com/freertos\r
24  *\r
25  * 1 tab == 4 spaces!\r
26  */\r
27 \r
28 /* High speed timer test as described in main.c. */\r
29 \r
30 \r
31 /* Scheduler includes. */\r
32 #include "FreeRTOS.h"\r
33 \r
34 /* Demo includes. */\r
35 #include "partest.h"\r
36 \r
37 /* The number of interrupts to pass before we start looking at the jitter. */\r
38 #define timerSETTLE_TIME                        5\r
39 \r
40 /* The maximum value the 16bit timer can contain. */\r
41 #define timerMAX_COUNT                          0xffff\r
42 \r
43 /*-----------------------------------------------------------*/\r
44 \r
45 /*\r
46  * Measure the time between this interrupt and the previous interrupt to \r
47  * calculate the timing jitter.  Remember the maximum value the jitter has\r
48  * ever been calculated to be.\r
49  */\r
50 static void prvCalculateAndStoreJitter( void );\r
51 \r
52 /*-----------------------------------------------------------*/\r
53 \r
54 /* The maximum time (in processor clocks) between two consecutive timer\r
55 interrupts so far. */\r
56 unsigned short usMaxJitter = 0;\r
57 \r
58 /*-----------------------------------------------------------*/\r
59 \r
60 void vSetupTimerTest( unsigned short usFrequencyHz )\r
61 {\r
62         /* T2 is used to generate interrupts.  T4 is used to provide an accurate\r
63         time measurement. */\r
64         T2CON = 0;\r
65         T4CON = 0;\r
66         TMR2 = 0;\r
67         TMR4 = 0;\r
68 \r
69         /* Timer 2 is going to interrupt at usFrequencyHz Hz. */\r
70         PR2 = ( unsigned short ) ( configCPU_CLOCK_HZ / ( unsigned long ) usFrequencyHz );\r
71 \r
72         /* Timer 4 is going to free run from minimum to maximum value. */\r
73         PR4 = ( unsigned short ) timerMAX_COUNT;\r
74 \r
75         /* Setup timer 2 interrupt priority to be above the kernel priority so \r
76         the timer jitter is not effected by the kernel activity. */\r
77         IPC1bits.T2IP = configKERNEL_INTERRUPT_PRIORITY + 1;\r
78 \r
79         /* Clear the interrupt as a starting condition. */\r
80         IFS0bits.T2IF = 0;\r
81 \r
82         /* Enable the interrupt. */\r
83         IEC0bits.T2IE = 1;\r
84 \r
85         /* Start both timers. */\r
86         T2CONbits.TON = 1;\r
87         T4CONbits.TON = 1;\r
88 }\r
89 /*-----------------------------------------------------------*/\r
90 \r
91 static void prvCalculateAndStoreJitter( void )\r
92 {\r
93 static unsigned short usLastCount = 0, usSettleCount = 0;\r
94 unsigned short usThisCount, usDifference;\r
95 \r
96         /* Capture the timer value as we enter the interrupt. */\r
97         usThisCount = TMR4;\r
98 \r
99         if( usSettleCount >= timerSETTLE_TIME )\r
100         {\r
101                 /* What is the difference between the timer value in this interrupt\r
102                 and the value from the last interrupt. */\r
103                 usDifference = usThisCount - usLastCount;\r
104 \r
105                 /* Store the difference in the timer values if it is larger than the\r
106                 currently stored largest value.  The difference over and above the \r
107                 expected difference will give the 'jitter' in the processing of these\r
108                 interrupts. */\r
109                 if( usDifference > usMaxJitter )\r
110                 {\r
111                         usMaxJitter = usDifference;\r
112                 }\r
113         }\r
114         else\r
115         {\r
116                 /* Don't bother storing any values for the first couple of \r
117                 interrupts. */\r
118                 usSettleCount++;\r
119         }\r
120 \r
121         /* Remember what the timer value was this time through, so we can calculate\r
122         the difference the next time through. */\r
123         usLastCount = usThisCount;\r
124 }\r
125 /*-----------------------------------------------------------*/\r
126 \r
127 void __attribute__((__interrupt__, auto_psv)) _T2Interrupt( void )\r
128 {\r
129         /* Work out the time between this and the previous interrupt. */\r
130         prvCalculateAndStoreJitter();\r
131 \r
132         /* Clear the timer interrupt. */\r
133         IFS0bits.T2IF = 0;\r
134 }\r
135 \r
136 \r