]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/CORTEX_LM3Sxxxx_IAR_Keil/timertest.c
Update copyright date ready for tagging V10.1.0.
[freertos] / FreeRTOS / Demo / CORTEX_LM3Sxxxx_IAR_Keil / timertest.c
1 /*\r
2  * FreeRTOS Kernel V10.1.0\r
3  * Copyright (C) 2018 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 /* Scheduler includes. */\r
31 #include "FreeRTOS.h"\r
32 \r
33 /* Library includes. */\r
34 #include "hw_ints.h"\r
35 #include "hw_memmap.h"\r
36 #include "hw_types.h"\r
37 #include "interrupt.h"\r
38 #include "sysctl.h"\r
39 #include "lmi_timer.h"\r
40 #include "hw_timer.h"\r
41 \r
42 /* The set frequency of the interrupt.  Deviations from this are measured as\r
43 the jitter. */\r
44 #define timerINTERRUPT_FREQUENCY                ( 20000UL )\r
45 \r
46 /* The expected time between each of the timer interrupts - if the jitter was\r
47 zero. */\r
48 #define timerEXPECTED_DIFFERENCE_VALUE  ( configCPU_CLOCK_HZ / timerINTERRUPT_FREQUENCY )\r
49 \r
50 /* The highest available interrupt priority. */\r
51 #define timerHIGHEST_PRIORITY                   ( 0 )\r
52 \r
53 /* Misc defines. */\r
54 #define timerMAX_32BIT_VALUE                    ( 0xffffffffUL )\r
55 #define timerTIMER_1_COUNT_VALUE                ( * ( ( volatile unsigned long * ) ( ( unsigned long ) TIMER1_BASE + 0x48UL ) ) )\r
56 \r
57 /*-----------------------------------------------------------*/\r
58 \r
59 /* Interrupt handler in which the jitter is measured. */\r
60 void Timer0IntHandler( void );\r
61 \r
62 /* Stores the value of the maximum recorded jitter between interrupts. */\r
63 volatile unsigned long ulMaxJitter = 0;\r
64 \r
65 /*-----------------------------------------------------------*/\r
66 \r
67 void vSetupHighFrequencyTimer( void )\r
68 {\r
69 unsigned long ulFrequency;\r
70 \r
71         /* Timer zero is used to generate the interrupts, and timer 1 is used\r
72         to measure the jitter. */\r
73         SysCtlPeripheralEnable( SYSCTL_PERIPH_TIMER0 );\r
74     SysCtlPeripheralEnable( SYSCTL_PERIPH_TIMER1 );\r
75     TimerConfigure( TIMER0_BASE, TIMER_CFG_32_BIT_PER );\r
76     TimerConfigure( TIMER1_BASE, TIMER_CFG_32_BIT_PER );\r
77         \r
78         /* Set the timer interrupt to be above the kernel - highest. */\r
79         IntPrioritySet( INT_TIMER0A, timerHIGHEST_PRIORITY );\r
80 \r
81         /* Just used to measure time. */\r
82     TimerLoadSet(TIMER1_BASE, TIMER_A, timerMAX_32BIT_VALUE );\r
83         \r
84         /* Ensure interrupts do not start until the scheduler is running. */\r
85         portDISABLE_INTERRUPTS();\r
86         \r
87         /* The rate at which the timer will interrupt. */\r
88         ulFrequency = configCPU_CLOCK_HZ / timerINTERRUPT_FREQUENCY;    \r
89     TimerLoadSet( TIMER0_BASE, TIMER_A, ulFrequency );\r
90     IntEnable( INT_TIMER0A );\r
91     TimerIntEnable( TIMER0_BASE, TIMER_TIMA_TIMEOUT );\r
92 \r
93         /* Enable both timers. */       \r
94     TimerEnable( TIMER0_BASE, TIMER_A );\r
95     TimerEnable( TIMER1_BASE, TIMER_A );\r
96 }\r
97 /*-----------------------------------------------------------*/\r
98 \r
99 void Timer0IntHandler( void )\r
100 {\r
101 unsigned long ulDifference;\r
102 volatile unsigned long ulCurrentCount;\r
103 static unsigned long ulMaxDifference = 0, ulLastCount = 0;\r
104 \r
105         /* We use the timer 1 counter value to measure the clock cycles between\r
106         the timer 0 interrupts. */\r
107         ulCurrentCount = timerTIMER_1_COUNT_VALUE;\r
108 \r
109         TimerIntClear( TIMER0_BASE, TIMER_TIMA_TIMEOUT );\r
110 \r
111         if( ulCurrentCount < ulLastCount )\r
112         {       \r
113                 /* How many times has timer 1 counted since the last interrupt? */\r
114                 ulDifference =  ulLastCount - ulCurrentCount;\r
115         \r
116                 /* Is this the largest difference we have measured yet? */\r
117                 if( ulDifference > ulMaxDifference )\r
118                 {\r
119                         ulMaxDifference = ulDifference;\r
120                         ulMaxJitter = ulMaxDifference - timerEXPECTED_DIFFERENCE_VALUE;\r
121                 }\r
122         }\r
123         \r
124         ulLastCount = ulCurrentCount;\r
125 }\r
126 \r
127 \r
128 \r
129 \r
130 \r
131 \r
132 \r