]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/CORTEX_LM3Sxxxx_IAR_Keil/timertest.c
Update to MIT licensed FreeRTOS V10.0.0 - see https://www.freertos.org/History.txt
[freertos] / FreeRTOS / Demo / CORTEX_LM3Sxxxx_IAR_Keil / timertest.c
1 /*\r
2  * FreeRTOS Kernel V10.0.0\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. If you wish to use our Amazon\r
14  * FreeRTOS name, please do so in a fair use way that does not cause confusion.\r
15  *\r
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\r
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS\r
18  * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR\r
19  * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER\r
20  * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN\r
21  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\r
22  *\r
23  * http://www.FreeRTOS.org\r
24  * http://aws.amazon.com/freertos\r
25  *\r
26  * 1 tab == 4 spaces!\r
27  */\r
28 \r
29 /* High speed timer test as described in main.c. */\r
30 \r
31 /* Scheduler includes. */\r
32 #include "FreeRTOS.h"\r
33 \r
34 /* Library includes. */\r
35 #include "hw_ints.h"\r
36 #include "hw_memmap.h"\r
37 #include "hw_types.h"\r
38 #include "interrupt.h"\r
39 #include "sysctl.h"\r
40 #include "lmi_timer.h"\r
41 #include "hw_timer.h"\r
42 \r
43 /* The set frequency of the interrupt.  Deviations from this are measured as\r
44 the jitter. */\r
45 #define timerINTERRUPT_FREQUENCY                ( 20000UL )\r
46 \r
47 /* The expected time between each of the timer interrupts - if the jitter was\r
48 zero. */\r
49 #define timerEXPECTED_DIFFERENCE_VALUE  ( configCPU_CLOCK_HZ / timerINTERRUPT_FREQUENCY )\r
50 \r
51 /* The highest available interrupt priority. */\r
52 #define timerHIGHEST_PRIORITY                   ( 0 )\r
53 \r
54 /* Misc defines. */\r
55 #define timerMAX_32BIT_VALUE                    ( 0xffffffffUL )\r
56 #define timerTIMER_1_COUNT_VALUE                ( * ( ( volatile unsigned long * ) ( ( unsigned long ) TIMER1_BASE + 0x48UL ) ) )\r
57 \r
58 /*-----------------------------------------------------------*/\r
59 \r
60 /* Interrupt handler in which the jitter is measured. */\r
61 void Timer0IntHandler( void );\r
62 \r
63 /* Stores the value of the maximum recorded jitter between interrupts. */\r
64 volatile unsigned long ulMaxJitter = 0;\r
65 \r
66 /*-----------------------------------------------------------*/\r
67 \r
68 void vSetupHighFrequencyTimer( void )\r
69 {\r
70 unsigned long ulFrequency;\r
71 \r
72         /* Timer zero is used to generate the interrupts, and timer 1 is used\r
73         to measure the jitter. */\r
74         SysCtlPeripheralEnable( SYSCTL_PERIPH_TIMER0 );\r
75     SysCtlPeripheralEnable( SYSCTL_PERIPH_TIMER1 );\r
76     TimerConfigure( TIMER0_BASE, TIMER_CFG_32_BIT_PER );\r
77     TimerConfigure( TIMER1_BASE, TIMER_CFG_32_BIT_PER );\r
78         \r
79         /* Set the timer interrupt to be above the kernel - highest. */\r
80         IntPrioritySet( INT_TIMER0A, timerHIGHEST_PRIORITY );\r
81 \r
82         /* Just used to measure time. */\r
83     TimerLoadSet(TIMER1_BASE, TIMER_A, timerMAX_32BIT_VALUE );\r
84         \r
85         /* Ensure interrupts do not start until the scheduler is running. */\r
86         portDISABLE_INTERRUPTS();\r
87         \r
88         /* The rate at which the timer will interrupt. */\r
89         ulFrequency = configCPU_CLOCK_HZ / timerINTERRUPT_FREQUENCY;    \r
90     TimerLoadSet( TIMER0_BASE, TIMER_A, ulFrequency );\r
91     IntEnable( INT_TIMER0A );\r
92     TimerIntEnable( TIMER0_BASE, TIMER_TIMA_TIMEOUT );\r
93 \r
94         /* Enable both timers. */       \r
95     TimerEnable( TIMER0_BASE, TIMER_A );\r
96     TimerEnable( TIMER1_BASE, TIMER_A );\r
97 }\r
98 /*-----------------------------------------------------------*/\r
99 \r
100 void Timer0IntHandler( void )\r
101 {\r
102 unsigned long ulDifference;\r
103 volatile unsigned long ulCurrentCount;\r
104 static unsigned long ulMaxDifference = 0, ulLastCount = 0;\r
105 \r
106         /* We use the timer 1 counter value to measure the clock cycles between\r
107         the timer 0 interrupts. */\r
108         ulCurrentCount = timerTIMER_1_COUNT_VALUE;\r
109 \r
110         TimerIntClear( TIMER0_BASE, TIMER_TIMA_TIMEOUT );\r
111 \r
112         if( ulCurrentCount < ulLastCount )\r
113         {       \r
114                 /* How many times has timer 1 counted since the last interrupt? */\r
115                 ulDifference =  ulLastCount - ulCurrentCount;\r
116         \r
117                 /* Is this the largest difference we have measured yet? */\r
118                 if( ulDifference > ulMaxDifference )\r
119                 {\r
120                         ulMaxDifference = ulDifference;\r
121                         ulMaxJitter = ulMaxDifference - timerEXPECTED_DIFFERENCE_VALUE;\r
122                 }\r
123         }\r
124         \r
125         ulLastCount = ulCurrentCount;\r
126 }\r
127 \r
128 \r
129 \r
130 \r
131 \r
132 \r
133 \r