]> git.sur5r.net Git - freertos/blob - Demo/CORTEX_LM3Sxxxx_IAR_Keil/timertest.c
Prepare for V5.0.4 release.
[freertos] / Demo / CORTEX_LM3Sxxxx_IAR_Keil / timertest.c
1 /*\r
2         FreeRTOS.org V5.0.4 - Copyright (C) 2003-2008 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\r
7         it under the terms of the GNU General Public License as published by\r
8         the Free Software Foundation; either version 2 of the License, or\r
9         (at your option) any later version.\r
10 \r
11         FreeRTOS.org is distributed in the hope that it will be useful,\r
12         but WITHOUT ANY WARRANTY; without even the implied warranty of\r
13         MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
14         GNU General Public License for more details.\r
15 \r
16         You should have received a copy of the GNU General Public License\r
17         along with FreeRTOS.org; if not, write to the Free Software\r
18         Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA\r
19 \r
20         A special exception to the GPL can be applied should you wish to distribute\r
21         a combined work that includes FreeRTOS.org, without being obliged to provide\r
22         the source code for any proprietary components.  See the licensing section\r
23         of http://www.FreeRTOS.org for full details of how and when the exception\r
24         can be applied.\r
25 \r
26     ***************************************************************************\r
27     ***************************************************************************\r
28     *                                                                         *\r
29     * SAVE TIME AND MONEY!  We can port FreeRTOS.org to your own hardware,    *\r
30     * and even write all or part of your application on your behalf.          *\r
31     * See http://www.OpenRTOS.com for details of the services we provide to   *\r
32     * expedite your project.                                                  *\r
33     *                                                                         *\r
34     ***************************************************************************\r
35     ***************************************************************************\r
36 \r
37         Please ensure to read the configuration and relevant port sections of the\r
38         online documentation.\r
39 \r
40         http://www.FreeRTOS.org - Documentation, latest information, license and \r
41         contact details.\r
42 \r
43         http://www.SafeRTOS.com - A version that is certified for use in safety \r
44         critical systems.\r
45 \r
46         http://www.OpenRTOS.com - Commercial support, development, porting, \r
47         licensing and training services.\r
48 */\r
49 \r
50 /* High speed timer test as described in main.c. */\r
51 \r
52 /* Scheduler includes. */\r
53 #include "FreeRTOS.h"\r
54 \r
55 /* Library includes. */\r
56 #include "hw_ints.h"\r
57 #include "hw_memmap.h"\r
58 #include "hw_types.h"\r
59 #include "interrupt.h"\r
60 #include "sysctl.h"\r
61 #include "lmi_timer.h"\r
62 #include "hw_timer.h"\r
63 \r
64 /* The set frequency of the interrupt.  Deviations from this are measured as\r
65 the jitter. */\r
66 #define timerINTERRUPT_FREQUENCY                ( 20000UL )\r
67 \r
68 /* The expected time between each of the timer interrupts - if the jitter was\r
69 zero. */\r
70 #define timerEXPECTED_DIFFERENCE_VALUE  ( configCPU_CLOCK_HZ / timerINTERRUPT_FREQUENCY )\r
71 \r
72 /* The highest available interrupt priority. */\r
73 #define timerHIGHEST_PRIORITY                   ( 0 )\r
74 \r
75 /* Misc defines. */\r
76 #define timerMAX_32BIT_VALUE                    ( 0xffffffffUL )\r
77 #define timerTIMER_1_COUNT_VALUE                ( * ( ( volatile unsigned long * ) ( ( unsigned portLONG ) TIMER1_BASE + 0x48UL ) ) )\r
78 \r
79 /*-----------------------------------------------------------*/\r
80 \r
81 /* Interrupt handler in which the jitter is measured. */\r
82 void Timer0IntHandler( void );\r
83 \r
84 /* Stores the value of the maximum recorded jitter between interrupts. */\r
85 volatile unsigned portLONG ulMaxJitter = 0;\r
86 \r
87 /*-----------------------------------------------------------*/\r
88 \r
89 void vSetupHighFrequencyTimer( void )\r
90 {\r
91 unsigned long ulFrequency;\r
92 \r
93         /* Timer zero is used to generate the interrupts, and timer 1 is used\r
94         to measure the jitter. */\r
95         SysCtlPeripheralEnable( SYSCTL_PERIPH_TIMER0 );\r
96     SysCtlPeripheralEnable( SYSCTL_PERIPH_TIMER1 );\r
97     TimerConfigure( TIMER0_BASE, TIMER_CFG_32_BIT_PER );\r
98     TimerConfigure( TIMER1_BASE, TIMER_CFG_32_BIT_PER );\r
99         \r
100         /* Set the timer interrupt to be above the kernel - highest. */\r
101         IntPrioritySet( INT_TIMER0A, timerHIGHEST_PRIORITY );\r
102 \r
103         /* Just used to measure time. */\r
104     TimerLoadSet(TIMER1_BASE, TIMER_A, timerMAX_32BIT_VALUE );\r
105         \r
106         /* Ensure interrupts do not start until the scheduler is running. */\r
107         portDISABLE_INTERRUPTS();\r
108         \r
109         /* The rate at which the timer will interrupt. */\r
110         ulFrequency = configCPU_CLOCK_HZ / timerINTERRUPT_FREQUENCY;    \r
111     TimerLoadSet( TIMER0_BASE, TIMER_A, ulFrequency );\r
112     IntEnable( INT_TIMER0A );\r
113     TimerIntEnable( TIMER0_BASE, TIMER_TIMA_TIMEOUT );\r
114 \r
115         /* Enable both timers. */       \r
116     TimerEnable( TIMER0_BASE, TIMER_A );\r
117     TimerEnable( TIMER1_BASE, TIMER_A );\r
118 }\r
119 /*-----------------------------------------------------------*/\r
120 \r
121 void Timer0IntHandler( void )\r
122 {\r
123 unsigned portLONG ulDifference;\r
124 volatile unsigned portLONG ulCurrentCount;\r
125 static unsigned portLONG ulMaxDifference = 0, ulLastCount = 0;\r
126 \r
127         /* We use the timer 1 counter value to measure the clock cycles between\r
128         the timer 0 interrupts. */\r
129         ulCurrentCount = timerTIMER_1_COUNT_VALUE;\r
130 \r
131         TimerIntClear( TIMER0_BASE, TIMER_TIMA_TIMEOUT );\r
132 \r
133         if( ulCurrentCount < ulLastCount )\r
134         {       \r
135                 /* How many times has timer 1 counted since the last interrupt? */\r
136                 ulDifference =  ulLastCount - ulCurrentCount;\r
137         \r
138                 /* Is this the largest difference we have measured yet? */\r
139                 if( ulDifference > ulMaxDifference )\r
140                 {\r
141                         ulMaxDifference = ulDifference;\r
142                         ulMaxJitter = ulMaxDifference - timerEXPECTED_DIFFERENCE_VALUE;\r
143                 }\r
144         }\r
145         \r
146         ulLastCount = ulCurrentCount;\r
147 }\r
148 \r
149 \r
150 \r
151 \r
152 \r
153 \r
154 \r