]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/RX200_RX210-RSK_Renesas/RTOSDemo/HighFrequencyTimerTest.c
e7dba36f2541da1347a116dbe9e059d67aa3425b
[freertos] / FreeRTOS / Demo / RX200_RX210-RSK_Renesas / RTOSDemo / HighFrequencyTimerTest.c
1 /*\r
2     FreeRTOS V8.2.0rc1 - Copyright (C) 2014 Real Time Engineers Ltd.\r
3     All rights reserved\r
4 \r
5     VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.\r
6 \r
7     This file is part of the FreeRTOS distribution.\r
8 \r
9     FreeRTOS is free software; you can redistribute it and/or modify it under\r
10     the terms of the GNU General Public License (version 2) as published by the\r
11     Free Software Foundation >>!AND MODIFIED BY!<< the FreeRTOS exception.\r
12 \r
13     >>!   NOTE: The modification to the GPL is included to allow you to     !<<\r
14     >>!   distribute a combined work that includes FreeRTOS without being   !<<\r
15     >>!   obliged to provide the source code for proprietary components     !<<\r
16     >>!   outside of the FreeRTOS kernel.                                   !<<\r
17 \r
18     FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY\r
19     WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS\r
20     FOR A PARTICULAR PURPOSE.  Full license text is available on the following\r
21     link: http://www.freertos.org/a00114.html\r
22 \r
23     1 tab == 4 spaces!\r
24 \r
25     ***************************************************************************\r
26      *                                                                       *\r
27      *    Having a problem?  Start by reading the FAQ "My application does   *\r
28      *    not run, what could be wrong?".  Have you defined configASSERT()?  *\r
29      *                                                                       *\r
30      *    http://www.FreeRTOS.org/FAQHelp.html                               *\r
31      *                                                                       *\r
32     ***************************************************************************\r
33 \r
34     ***************************************************************************\r
35      *                                                                       *\r
36      *    FreeRTOS provides completely free yet professionally developed,    *\r
37      *    robust, strictly quality controlled, supported, and cross          *\r
38      *    platform software that is more than just the market leader, it     *\r
39      *    is the industry's de facto standard.                               *\r
40      *                                                                       *\r
41      *    Help yourself get started quickly while simultaneously helping     *\r
42      *    to support the FreeRTOS project by purchasing a FreeRTOS           *\r
43      *    tutorial book, reference manual, or both:                          *\r
44      *    http://www.FreeRTOS.org/Documentation                              *\r
45      *                                                                       *\r
46     ***************************************************************************\r
47 \r
48     ***************************************************************************\r
49      *                                                                       *\r
50      *   Investing in training allows your team to be as productive as       *\r
51      *   possible as early as possible, lowering your overall development    *\r
52      *   cost, and enabling you to bring a more robust product to market     *\r
53      *   earlier than would otherwise be possible.  Richard Barry is both    *\r
54      *   the architect and key author of FreeRTOS, and so also the world's   *\r
55      *   leading authority on what is the world's most popular real time     *\r
56      *   kernel for deeply embedded MCU designs.  Obtaining your training    *\r
57      *   from Richard ensures your team will gain directly from his in-depth *\r
58      *   product knowledge and years of usage experience.  Contact Real Time *\r
59      *   Engineers Ltd to enquire about the FreeRTOS Masterclass, presented  *\r
60      *   by Richard Barry:  http://www.FreeRTOS.org/contact\r
61      *                                                                       *\r
62     ***************************************************************************\r
63 \r
64     ***************************************************************************\r
65      *                                                                       *\r
66      *    You are receiving this top quality software for free.  Please play *\r
67      *    fair and reciprocate by reporting any suspected issues and         *\r
68      *    participating in the community forum:                              *\r
69      *    http://www.FreeRTOS.org/support                                    *\r
70      *                                                                       *\r
71      *    Thank you!                                                         *\r
72      *                                                                       *\r
73     ***************************************************************************\r
74 \r
75     http://www.FreeRTOS.org - Documentation, books, training, latest versions,\r
76     license and Real Time Engineers Ltd. contact details.\r
77 \r
78     http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,\r
79     including FreeRTOS+Trace - an indispensable productivity tool, a DOS\r
80     compatible FAT file system, and our tiny thread aware UDP/IP stack.\r
81 \r
82     http://www.FreeRTOS.org/labs - Where new FreeRTOS products go to incubate.\r
83     Come and try FreeRTOS+TCP, our new open source TCP/IP stack for FreeRTOS.\r
84 \r
85     http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High\r
86     Integrity Systems ltd. to sell under the OpenRTOS brand.  Low cost OpenRTOS\r
87     licenses offer ticketed support, indemnification and commercial middleware.\r
88 \r
89     http://www.SafeRTOS.com - High Integrity Systems also provide a safety\r
90     engineered and independently SIL3 certified version for use in safety and\r
91     mission critical applications that require provable dependability.\r
92 \r
93     1 tab == 4 spaces!\r
94 */\r
95 \r
96 /* \r
97  * High frequency timer test as described in main.c. \r
98  */\r
99 \r
100 /* Scheduler includes. */\r
101 #include "FreeRTOS.h"\r
102 \r
103 /* Hardware specifics. */\r
104 #include "iodefine.h"\r
105 \r
106 /* The set frequency of the interrupt.  Deviations from this are measured as\r
107 the jitter. */\r
108 #define timerINTERRUPT_FREQUENCY                ( 20000UL )\r
109 \r
110 /* The expected time between each of the timer interrupts - if the jitter was\r
111 zero. */\r
112 #define timerEXPECTED_DIFFERENCE_VALUE  ( ( unsigned short ) ( ( configPERIPHERAL_CLOCK_HZ / 8UL ) / timerINTERRUPT_FREQUENCY ) )\r
113 \r
114 /* The highest available interrupt priority. */\r
115 #define timerHIGHEST_PRIORITY                   ( 15 )\r
116 \r
117 /* Misc defines. */\r
118 #define timerTIMER_3_COUNT_VALUE                ( *( ( unsigned short * ) 0x8801a ) ) /*( CMT3.CMCNT )*/\r
119 \r
120 /*-----------------------------------------------------------*/\r
121 \r
122 /* Interrupt handler in which the jitter is measured. */\r
123 static void prvTimer2IntHandler( void );\r
124 \r
125 /* Stores the value of the maximum recorded jitter between interrupts.  This is\r
126 displayed on one of the served web pages. */\r
127 volatile unsigned short usMaxJitter = 0;\r
128 \r
129 /* Counts the number of high frequency interrupts - used to generate the run\r
130 time stats. */\r
131 volatile unsigned long ulHighFrequencyTickCount = 0UL;\r
132 \r
133 /*-----------------------------------------------------------*/\r
134 \r
135 void vSetupHighFrequencyTimer( void )\r
136 {\r
137         /* Timer CMT2 is used to generate the interrupts, and CMT3 is used\r
138         to measure the jitter. */\r
139 \r
140         /* Enable compare match timer 2 and 3. */\r
141         MSTP( CMT2 ) = 0;\r
142         MSTP( CMT3 ) = 0;\r
143         \r
144         /* Interrupt on compare match. */\r
145         CMT2.CMCR.BIT.CMIE = 1;\r
146         \r
147         /* Set the compare match value. */\r
148         CMT2.CMCOR = ( unsigned short ) ( ( ( configPERIPHERAL_CLOCK_HZ / timerINTERRUPT_FREQUENCY ) -1 ) / 8 );\r
149         \r
150         /* Divide the PCLK by 8. */\r
151         CMT2.CMCR.BIT.CKS = 0;\r
152         CMT3.CMCR.BIT.CKS = 0;\r
153         \r
154         /* Enable the interrupt... */\r
155         _IEN( _CMT2_CMI2 ) = 1;\r
156         \r
157         /* ...and set its priority to the maximum possible, this is above the priority\r
158         set by configMAX_SYSCALL_INTERRUPT_PRIORITY so will nest. */\r
159         _IPR( _CMT2_CMI2 ) = timerHIGHEST_PRIORITY;\r
160         \r
161         /* Start the timers. */\r
162         CMT.CMSTR1.BIT.STR2 = 1;\r
163         CMT.CMSTR1.BIT.STR3 = 1;\r
164 }\r
165 /*-----------------------------------------------------------*/\r
166 \r
167 #pragma interrupt ( prvTimer2IntHandler( vect = _VECT( _CMT2_CMI2 ), enable ) )\r
168 static void prvTimer2IntHandler( void )\r
169 {\r
170 volatile unsigned short usCurrentCount;\r
171 static unsigned short usMaxCount = 0;\r
172 static unsigned long ulErrorCount = 0UL;\r
173 \r
174         /* We use the timer 1 counter value to measure the clock cycles between\r
175         the timer 0 interrupts.  First stop the clock. */\r
176         CMT.CMSTR1.BIT.STR3 = 0;\r
177         nop();\r
178         nop();\r
179         usCurrentCount = timerTIMER_3_COUNT_VALUE;\r
180 \r
181         /* Is this the largest count we have measured yet? */\r
182         if( usCurrentCount > usMaxCount )\r
183         {\r
184                 if( usCurrentCount > timerEXPECTED_DIFFERENCE_VALUE )\r
185                 {\r
186                         usMaxJitter = usCurrentCount - timerEXPECTED_DIFFERENCE_VALUE;\r
187                 }\r
188                 else\r
189                 {\r
190                         /* This should not happen! */\r
191                         ulErrorCount++;\r
192                 }\r
193                 \r
194                 usMaxCount = usCurrentCount;\r
195         }\r
196 \r
197         /* Used to generate the run time stats. */\r
198         ulHighFrequencyTickCount++;\r
199         \r
200         /* Clear the timer. */\r
201         timerTIMER_3_COUNT_VALUE = 0;\r
202         \r
203         /* Then start the clock again. */\r
204         CMT.CMSTR1.BIT.STR3 = 1;\r
205 }\r
206 \r
207 \r
208 \r
209 \r
210 \r
211 \r
212 \r