]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/CORTEX_LM3Sxxxx_Rowley/timertest.c
Update to MIT licensed FreeRTOS V10.0.0 - see https://www.freertos.org/History.txt
[freertos] / FreeRTOS / Demo / CORTEX_LM3Sxxxx_Rowley / 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 \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                ( * ( ( unsigned long * ) ( TIMER1_BASE + 0x48 ) ) )\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 = 0UL;\r
64 \r
65 /* Counts the total number of times that the high frequency timer has 'ticked'.\r
66 This value is used by the run time stats function to work out what percentage\r
67 of CPU time each task is taking. */\r
68 volatile unsigned long ulHighFrequencyTimerTicks = 0UL;\r
69 /*-----------------------------------------------------------*/\r
70 \r
71 void vSetupHighFrequencyTimer( void )\r
72 {\r
73 unsigned long ulFrequency;\r
74 \r
75         /* Timer zero is used to generate the interrupts, and timer 1 is used\r
76         to measure the jitter. */\r
77         SysCtlPeripheralEnable( SYSCTL_PERIPH_TIMER0 );\r
78     SysCtlPeripheralEnable( SYSCTL_PERIPH_TIMER1 );\r
79     TimerConfigure( TIMER0_BASE, TIMER_CFG_32_BIT_PER );\r
80     TimerConfigure( TIMER1_BASE, TIMER_CFG_32_BIT_PER );\r
81 \r
82         /* Set the timer interrupt to be above the kernel - highest. */\r
83         IntPrioritySet( INT_TIMER0A, timerHIGHEST_PRIORITY );\r
84 \r
85         /* Just used to measure time. */\r
86     TimerLoadSet(TIMER1_BASE, TIMER_A, timerMAX_32BIT_VALUE );\r
87 \r
88         /* Ensure interrupts do not start until the scheduler is running. */\r
89         portDISABLE_INTERRUPTS();\r
90 \r
91         /* The rate at which the timer will interrupt. */\r
92         ulFrequency = configCPU_CLOCK_HZ / timerINTERRUPT_FREQUENCY;\r
93     TimerLoadSet( TIMER0_BASE, TIMER_A, ulFrequency );\r
94     IntEnable( INT_TIMER0A );\r
95     TimerIntEnable( TIMER0_BASE, TIMER_TIMA_TIMEOUT );\r
96 \r
97         /* Enable both timers. */\r
98     TimerEnable( TIMER0_BASE, TIMER_A );\r
99     TimerEnable( TIMER1_BASE, TIMER_A );\r
100 }\r
101 /*-----------------------------------------------------------*/\r
102 \r
103 void Timer0IntHandler( void )\r
104 {\r
105 unsigned long ulDifference;\r
106 volatile unsigned long ulCurrentCount;\r
107 static unsigned long ulMaxDifference = 0, ulLastCount = 0;\r
108 \r
109         /* We use the timer 1 counter value to measure the clock cycles between\r
110         the timer 0 interrupts. */\r
111         ulCurrentCount = timerTIMER_1_COUNT_VALUE;\r
112 \r
113         TimerIntClear( TIMER0_BASE, TIMER_TIMA_TIMEOUT );\r
114 \r
115         if( ulCurrentCount < ulLastCount )\r
116         {\r
117                 /* How many times has timer 1 counted since the last interrupt? */\r
118                 ulDifference =  ulLastCount - ulCurrentCount;\r
119 \r
120                 /* Is this the largest difference we have measured yet? */\r
121                 if( ulDifference > ulMaxDifference )\r
122                 {\r
123                         ulMaxDifference = ulDifference;\r
124                         ulMaxJitter = ulMaxDifference - timerEXPECTED_DIFFERENCE_VALUE;\r
125                 }\r
126         }\r
127 \r
128         ulLastCount = ulCurrentCount;\r
129 \r
130         /* Keep a count of the total number of 20KHz ticks.  This is used by the\r
131         run time stats functionality to calculate how much CPU time is used by\r
132         each task. */\r
133         ulHighFrequencyTimerTicks++;\r
134 }\r
135 \r
136 \r
137 \r
138 \r
139 \r