]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/RX600_RX62N-RSK_GNURX/RTOSDemo/HighFrequencyTimerTest.c
Update version numbers in preparation for a new release.
[freertos] / FreeRTOS / Demo / RX600_RX62N-RSK_GNURX / RTOSDemo / HighFrequencyTimerTest.c
1 /*\r
2  * FreeRTOS Kernel V10.1.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.\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 /* \r
29  * High frequency timer test as described in main.c. \r
30  */\r
31 \r
32 /* Scheduler includes. */\r
33 #include "FreeRTOS.h"\r
34 \r
35 /* Hardware specifics. */\r
36 #include "iodefine.h"\r
37 \r
38 /* The set frequency of the interrupt.  Deviations from this are measured as\r
39 the jitter. */\r
40 #define timerINTERRUPT_FREQUENCY                ( 20000UL )\r
41 \r
42 /* The expected time between each of the timer interrupts - if the jitter was\r
43 zero. */\r
44 #define timerEXPECTED_DIFFERENCE_VALUE  ( ( unsigned short ) ( ( configPERIPHERAL_CLOCK_HZ / 8UL ) / timerINTERRUPT_FREQUENCY ) )\r
45 \r
46 /* The highest available interrupt priority. */\r
47 #define timerHIGHEST_PRIORITY                   ( 15 )\r
48 \r
49 /* Misc defines. */\r
50 #define timerTIMER_3_COUNT_VALUE                ( *( ( unsigned short * ) 0x8801a ) ) /*( CMT3.CMCNT )*/\r
51 \r
52 /*-----------------------------------------------------------*/\r
53 \r
54 /* Interrupt handler in which the jitter is measured. */\r
55 void vTimer2_ISR_Handler( void ) __attribute__((interrupt));\r
56 \r
57 /* Stores the value of the maximum recorded jitter between interrupts.  This is\r
58 displayed on one of the served web pages. */\r
59 volatile unsigned short usMaxJitter = 0;\r
60 \r
61 /* Counts the number of high frequency interrupts - used to generate the run\r
62 time stats. */\r
63 volatile unsigned long ulHighFrequencyTickCount = 0UL;\r
64 \r
65 /*-----------------------------------------------------------*/\r
66 \r
67 void vSetupHighFrequencyTimer( void )\r
68 {\r
69         /* Timer CMT2 is used to generate the interrupts, and CMT3 is used\r
70         to measure the jitter. */\r
71 \r
72         /* Enable compare match timer 2 and 3. */\r
73         MSTP( CMT2 ) = 0;\r
74         MSTP( CMT3 ) = 0;\r
75         \r
76         /* Interrupt on compare match. */\r
77         CMT2.CMCR.BIT.CMIE = 1;\r
78         \r
79         /* Set the compare match value. */\r
80         CMT2.CMCOR = ( unsigned short ) ( ( ( configPERIPHERAL_CLOCK_HZ / timerINTERRUPT_FREQUENCY ) -1 ) / 8 );\r
81         \r
82         /* Divide the PCLK by 8. */\r
83         CMT2.CMCR.BIT.CKS = 0;\r
84         CMT3.CMCR.BIT.CKS = 0;\r
85         \r
86         /* Enable the interrupt... */\r
87         _IEN( _CMT2_CMI2 ) = 1;\r
88         \r
89         /* ...and set its priority to the maximum possible, this is above the priority\r
90         set by configMAX_SYSCALL_INTERRUPT_PRIORITY so will nest. */\r
91         _IPR( _CMT2_CMI2 ) = timerHIGHEST_PRIORITY;\r
92         \r
93         /* Start the timers. */\r
94         CMT.CMSTR1.BIT.STR2 = 1;\r
95         CMT.CMSTR1.BIT.STR3 = 1;\r
96 }\r
97 /*-----------------------------------------------------------*/\r
98 \r
99 void vTimer2_ISR_Handler( void )\r
100 {\r
101 volatile unsigned short usCurrentCount;\r
102 static unsigned short usMaxCount = 0;\r
103 static unsigned long ulErrorCount = 0UL;\r
104 \r
105         /* This is the highest priority interrupt in the system, so there is no\r
106         advantage to re-enabling interrupts here.\r
107         \r
108         We use the timer 1 counter value to measure the clock cycles between\r
109         the timer 0 interrupts.  First stop the clock. */\r
110         CMT.CMSTR1.BIT.STR3 = 0;\r
111         portNOP();\r
112         portNOP();\r
113         usCurrentCount = timerTIMER_3_COUNT_VALUE;\r
114 \r
115         /* Is this the largest count we have measured yet? */\r
116         if( usCurrentCount > usMaxCount )\r
117         {\r
118                 if( usCurrentCount > timerEXPECTED_DIFFERENCE_VALUE )\r
119                 {\r
120                         usMaxJitter = usCurrentCount - timerEXPECTED_DIFFERENCE_VALUE;\r
121                 }\r
122                 else\r
123                 {\r
124                         /* This should not happen! */\r
125                         ulErrorCount++;\r
126                 }\r
127                 \r
128                 usMaxCount = usCurrentCount;\r
129         }\r
130 \r
131         /* Used to generate the run time stats. */\r
132         ulHighFrequencyTickCount++;\r
133         \r
134         /* Clear the timer. */\r
135         timerTIMER_3_COUNT_VALUE = 0;\r
136         \r
137         /* Then start the clock again. */\r
138         CMT.CMSTR1.BIT.STR3 = 1;\r
139 }\r
140 \r
141 \r
142 \r
143 \r
144 \r
145 \r
146 \r