2 * FreeRTOS Kernel V10.0.0
\r
3 * Copyright (C) 2017 Amazon.com, Inc. or its affiliates. All Rights Reserved.
\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
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
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
23 * http://www.FreeRTOS.org
\r
24 * http://aws.amazon.com/freertos
\r
26 * 1 tab == 4 spaces!
\r
30 * High frequency timer test as described in main.c.
\r
33 /* Scheduler includes. */
\r
34 #include "FreeRTOS.h"
\r
36 /* Hardware specifics. */
\r
37 #include "iodefine.h"
\r
39 /* The set frequency of the interrupt. Deviations from this are measured as
\r
41 #define timerINTERRUPT_FREQUENCY ( 20000UL )
\r
43 /* The expected time between each of the timer interrupts - if the jitter was
\r
45 #define timerEXPECTED_DIFFERENCE_VALUE ( ( unsigned short ) ( ( configPERIPHERAL_CLOCK_HZ / 8UL ) / timerINTERRUPT_FREQUENCY ) )
\r
47 /* The highest available interrupt priority. */
\r
48 #define timerHIGHEST_PRIORITY ( 15 )
\r
51 #define timerTIMER_3_COUNT_VALUE ( *( ( unsigned short * ) 0x8801a ) ) /*( CMT3.CMCNT )*/
\r
53 /*-----------------------------------------------------------*/
\r
55 /* Interrupt handler in which the jitter is measured. */
\r
56 void vTimer2_ISR_Handler( void ) __attribute__((interrupt));
\r
58 /* Stores the value of the maximum recorded jitter between interrupts. This is
\r
59 displayed on one of the served web pages. */
\r
60 volatile unsigned short usMaxJitter = 0;
\r
62 /* Counts the number of high frequency interrupts - used to generate the run
\r
64 volatile unsigned long ulHighFrequencyTickCount = 0UL;
\r
66 /*-----------------------------------------------------------*/
\r
68 void vSetupHighFrequencyTimer( void )
\r
70 /* Timer CMT2 is used to generate the interrupts, and CMT3 is used
\r
71 to measure the jitter. */
\r
73 /* Enable compare match timer 2 and 3. */
\r
77 /* Interrupt on compare match. */
\r
78 CMT2.CMCR.BIT.CMIE = 1;
\r
80 /* Set the compare match value. */
\r
81 CMT2.CMCOR = ( unsigned short ) ( ( ( configPERIPHERAL_CLOCK_HZ / timerINTERRUPT_FREQUENCY ) -1 ) / 8 );
\r
83 /* Divide the PCLK by 8. */
\r
84 CMT2.CMCR.BIT.CKS = 0;
\r
85 CMT3.CMCR.BIT.CKS = 0;
\r
87 /* Enable the interrupt... */
\r
88 _IEN( _CMT2_CMI2 ) = 1;
\r
90 /* ...and set its priority to the maximum possible, this is above the priority
\r
91 set by configMAX_SYSCALL_INTERRUPT_PRIORITY so will nest. */
\r
92 _IPR( _CMT2_CMI2 ) = timerHIGHEST_PRIORITY;
\r
94 /* Start the timers. */
\r
95 CMT.CMSTR1.BIT.STR2 = 1;
\r
96 CMT.CMSTR1.BIT.STR3 = 1;
\r
98 /*-----------------------------------------------------------*/
\r
100 void vTimer2_ISR_Handler( void )
\r
102 volatile unsigned short usCurrentCount;
\r
103 static unsigned short usMaxCount = 0;
\r
104 static unsigned long ulErrorCount = 0UL;
\r
106 /* This is the highest priority interrupt in the system, so there is no
\r
107 advantage to re-enabling interrupts here.
\r
109 We use the timer 1 counter value to measure the clock cycles between
\r
110 the timer 0 interrupts. First stop the clock. */
\r
111 CMT.CMSTR1.BIT.STR3 = 0;
\r
114 usCurrentCount = timerTIMER_3_COUNT_VALUE;
\r
116 /* Is this the largest count we have measured yet? */
\r
117 if( usCurrentCount > usMaxCount )
\r
119 if( usCurrentCount > timerEXPECTED_DIFFERENCE_VALUE )
\r
121 usMaxJitter = usCurrentCount - timerEXPECTED_DIFFERENCE_VALUE;
\r
125 /* This should not happen! */
\r
129 usMaxCount = usCurrentCount;
\r
132 /* Used to generate the run time stats. */
\r
133 ulHighFrequencyTickCount++;
\r
135 /* Clear the timer. */
\r
136 timerTIMER_3_COUNT_VALUE = 0;
\r
138 /* Then start the clock again. */
\r
139 CMT.CMSTR1.BIT.STR3 = 1;
\r