]> begriffs open source - cmsis-freertos/blob - Demo/CORTEX_LM3Sxxxx_Rowley/timertest.c
Set error state if no delay or already expired
[cmsis-freertos] / Demo / CORTEX_LM3Sxxxx_Rowley / timertest.c
1 /*
2  * FreeRTOS Kernel V10.1.1
3  * Copyright (C) 2018 Amazon.com, Inc. or its affiliates.  All Rights Reserved.
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a copy of
6  * this software and associated documentation files (the "Software"), to deal in
7  * the Software without restriction, including without limitation the rights to
8  * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
9  * the Software, and to permit persons to whom the Software is furnished to do so,
10  * subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice shall be included in all
13  * copies or substantial portions of the Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
17  * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
18  * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
19  * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21  *
22  * http://www.FreeRTOS.org
23  * http://aws.amazon.com/freertos
24  *
25  * 1 tab == 4 spaces!
26  */
27
28 /* High speed timer test as described in main.c. */
29
30 /* Scheduler includes. */
31 #include "FreeRTOS.h"
32
33 /* Library includes. */
34 #include "hw_ints.h"
35 #include "hw_memmap.h"
36 #include "hw_types.h"
37 #include "interrupt.h"
38 #include "sysctl.h"
39 #include "lmi_timer.h"
40
41 /* The set frequency of the interrupt.  Deviations from this are measured as
42 the jitter. */
43 #define timerINTERRUPT_FREQUENCY                ( 20000UL )
44
45 /* The expected time between each of the timer interrupts - if the jitter was
46 zero. */
47 #define timerEXPECTED_DIFFERENCE_VALUE  ( configCPU_CLOCK_HZ / timerINTERRUPT_FREQUENCY )
48
49 /* The highest available interrupt priority. */
50 #define timerHIGHEST_PRIORITY                   ( 0 )
51
52 /* Misc defines. */
53 #define timerMAX_32BIT_VALUE                    ( 0xffffffffUL )
54 #define timerTIMER_1_COUNT_VALUE                ( * ( ( unsigned long * ) ( TIMER1_BASE + 0x48 ) ) )
55
56 /*-----------------------------------------------------------*/
57
58 /* Interrupt handler in which the jitter is measured. */
59 void Timer0IntHandler( void );
60
61 /* Stores the value of the maximum recorded jitter between interrupts. */
62 volatile unsigned long ulMaxJitter = 0UL;
63
64 /* Counts the total number of times that the high frequency timer has 'ticked'.
65 This value is used by the run time stats function to work out what percentage
66 of CPU time each task is taking. */
67 volatile unsigned long ulHighFrequencyTimerTicks = 0UL;
68 /*-----------------------------------------------------------*/
69
70 void vSetupHighFrequencyTimer( void )
71 {
72 unsigned long ulFrequency;
73
74         /* Timer zero is used to generate the interrupts, and timer 1 is used
75         to measure the jitter. */
76         SysCtlPeripheralEnable( SYSCTL_PERIPH_TIMER0 );
77     SysCtlPeripheralEnable( SYSCTL_PERIPH_TIMER1 );
78     TimerConfigure( TIMER0_BASE, TIMER_CFG_32_BIT_PER );
79     TimerConfigure( TIMER1_BASE, TIMER_CFG_32_BIT_PER );
80
81         /* Set the timer interrupt to be above the kernel - highest. */
82         IntPrioritySet( INT_TIMER0A, timerHIGHEST_PRIORITY );
83
84         /* Just used to measure time. */
85     TimerLoadSet(TIMER1_BASE, TIMER_A, timerMAX_32BIT_VALUE );
86
87         /* Ensure interrupts do not start until the scheduler is running. */
88         portDISABLE_INTERRUPTS();
89
90         /* The rate at which the timer will interrupt. */
91         ulFrequency = configCPU_CLOCK_HZ / timerINTERRUPT_FREQUENCY;
92     TimerLoadSet( TIMER0_BASE, TIMER_A, ulFrequency );
93     IntEnable( INT_TIMER0A );
94     TimerIntEnable( TIMER0_BASE, TIMER_TIMA_TIMEOUT );
95
96         /* Enable both timers. */
97     TimerEnable( TIMER0_BASE, TIMER_A );
98     TimerEnable( TIMER1_BASE, TIMER_A );
99 }
100 /*-----------------------------------------------------------*/
101
102 void Timer0IntHandler( void )
103 {
104 unsigned long ulDifference;
105 volatile unsigned long ulCurrentCount;
106 static unsigned long ulMaxDifference = 0, ulLastCount = 0;
107
108         /* We use the timer 1 counter value to measure the clock cycles between
109         the timer 0 interrupts. */
110         ulCurrentCount = timerTIMER_1_COUNT_VALUE;
111
112         TimerIntClear( TIMER0_BASE, TIMER_TIMA_TIMEOUT );
113
114         if( ulCurrentCount < ulLastCount )
115         {
116                 /* How many times has timer 1 counted since the last interrupt? */
117                 ulDifference =  ulLastCount - ulCurrentCount;
118
119                 /* Is this the largest difference we have measured yet? */
120                 if( ulDifference > ulMaxDifference )
121                 {
122                         ulMaxDifference = ulDifference;
123                         ulMaxJitter = ulMaxDifference - timerEXPECTED_DIFFERENCE_VALUE;
124                 }
125         }
126
127         ulLastCount = ulCurrentCount;
128
129         /* Keep a count of the total number of 20KHz ticks.  This is used by the
130         run time stats functionality to calculate how much CPU time is used by
131         each task. */
132         ulHighFrequencyTimerTicks++;
133 }
134
135
136
137
138