]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/CORTEX_A5_SAMA5D4x_EK_IAR/Full_Demo/IntQueueTimer.c
Update version numbers in preparation for V8.2.0 release candidate 1.
[freertos] / FreeRTOS / Demo / CORTEX_A5_SAMA5D4x_EK_IAR / Full_Demo / IntQueueTimer.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  * This file initialises three timers as follows:\r
98  *\r
99  * TC0 channels 0 and 1 provide the interrupts that are used with the IntQ\r
100  * standard demo tasks, which test interrupt nesting and using queues from\r
101  * interrupts.  As the interrupt is shared the nesting achieved is not as deep\r
102  * as normal when this test is executed, but still worth while.\r
103  *\r
104  * TC2 channel 0 provides a much higher frequency timer that tests the nesting\r
105  * of interrupts that don't use the FreeRTOS API.  For convenience, the high\r
106  * frequency timer also keeps a count of the number of time it executes, and the\r
107  * count is used as the time base for the run time stats (which can be viewed\r
108  * through the CLI).\r
109  *\r
110  * All the timers can nest with the tick interrupt - creating a maximum\r
111  * interrupt nesting depth of 3 (normally 4, if the first two timers used\r
112  * separate interrupts).\r
113  *\r
114  */\r
115 \r
116 /* Scheduler includes. */\r
117 #include "FreeRTOS.h"\r
118 \r
119 /* Demo includes. */\r
120 #include "IntQueueTimer.h"\r
121 #include "IntQueue.h"\r
122 \r
123 /* Library includes. */\r
124 #include "board.h"\r
125 \r
126 /* The frequencies at which the first two timers expire are slightly offset to\r
127 ensure they don't remain synchronised.  The frequency of the highest priority\r
128 interrupt is 20 times faster so really hammers the interrupt entry and exit\r
129 code. */\r
130 #define tmrTIMER_0_FREQUENCY    ( 2000UL )\r
131 #define tmrTIMER_1_FREQUENCY    ( 2003UL )\r
132 #define tmrTIMER_2_FREQUENCY    ( 20000UL )\r
133 \r
134 /* The channels used in TC0 for generating the three interrupts. */\r
135 #define tmrTC0_CHANNEL_0                0 /* At tmrTIMER_0_FREQUENCY */\r
136 #define tmrTC0_CHANNEL_1                1 /* At tmrTIMER_1_FREQUENCY */\r
137 #define tmrTC1_CHANNEL_0                0 /* At tmrTIMER_2_FREQUENCY */\r
138 \r
139 /* The bit within the RC_SR register that indicates an RC compare. */\r
140 #define tmrRC_COMPARE                   ( 1UL << 4UL )\r
141 \r
142 /* The high frequency interrupt given the highest priority or all.  The priority\r
143 of the lower frequency timers must still be above the tick interrupt priority. */\r
144 #define tmrLOWER_PRIORITY               3\r
145 #define tmrHIGHER_PRIORITY              5\r
146 /*-----------------------------------------------------------*/\r
147 \r
148 /* Handlers for the two timer peripherals - two channels are used in the TC0\r
149 timer. */\r
150 static void prvTC0_Handler( void );\r
151 static void prvTC1_Handler( void );\r
152 \r
153 /* Used to provide a means of ensuring the intended interrupt nesting depth is\r
154 actually being reached. */\r
155 extern uint32_t ulPortInterruptNesting;\r
156 static uint32_t ulMaxRecordedNesting = 0;\r
157 \r
158 /* For convenience the high frequency timer increments a variable that is then\r
159 used as the time base for the run time stats. */\r
160 volatile uint32_t ulHighFrequencyTimerCounts = 0;\r
161 \r
162 /*-----------------------------------------------------------*/\r
163 \r
164 void vInitialiseTimerForIntQueueTest( void )\r
165 {\r
166 const uint32_t ulDivider = 128UL, ulTCCLKS = 3UL;\r
167 \r
168         /* Enable the TC clocks. */\r
169         PMC_EnablePeripheral( ID_TC0 );\r
170         PMC_EnablePeripheral( ID_TC1 );\r
171 \r
172         /* Configure TC0 channel 0 for a tmrTIMER_0_FREQUENCY frequency and trigger\r
173         on RC compare.  This is part of the IntQTimer test. */\r
174         TC_Configure( TC0, tmrTC0_CHANNEL_0, ulTCCLKS | TC_CMR_CPCTRG );\r
175         TC0->TC_CHANNEL[ tmrTC0_CHANNEL_0 ].TC_RC = ( BOARD_MCK / 2 ) / ( tmrTIMER_0_FREQUENCY * ulDivider );\r
176         TC0->TC_CHANNEL[ tmrTC0_CHANNEL_0 ].TC_IER = TC_IER_CPCS;\r
177 \r
178         /* Configure TC0 channel 1 for a tmrTIMER_1_FREQUENCY frequency and trigger\r
179         on RC compare.  This is part of the IntQTimer test. */\r
180         TC_Configure( TC0, tmrTC0_CHANNEL_1, ulTCCLKS | TC_CMR_CPCTRG );\r
181         TC0->TC_CHANNEL[ tmrTC0_CHANNEL_1 ].TC_RC = ( BOARD_MCK / 2 ) / ( tmrTIMER_1_FREQUENCY * ulDivider );\r
182         TC0->TC_CHANNEL[ tmrTC0_CHANNEL_1 ].TC_IER = TC_IER_CPCS;\r
183 \r
184         /* Configure TC1 channel 0 tmrTIMER_2_FREQUENCY frequency and trigger on\r
185         RC compare.  This is the very high frequency timer. */\r
186         TC_Configure( TC1, tmrTC1_CHANNEL_0, ulTCCLKS | TC_CMR_CPCTRG );\r
187         TC1->TC_CHANNEL[ tmrTC1_CHANNEL_0 ].TC_RC = BOARD_MCK / ( tmrTIMER_2_FREQUENCY * ulDivider );\r
188         TC1->TC_CHANNEL[ tmrTC1_CHANNEL_0 ].TC_IER = TC_IER_CPCS;\r
189 \r
190         /* First setup TC0 interrupt, in which two channels are used. */\r
191     AIC->AIC_SSR = ID_TC0;\r
192 \r
193         /* Ensure the interrupt is disabled before setting mode and handler. */\r
194     AIC->AIC_IDCR = AIC_IDCR_INTD;\r
195     AIC->AIC_SMR  = AIC_SMR_SRCTYPE_EXT_POSITIVE_EDGE |  tmrLOWER_PRIORITY;\r
196     AIC->AIC_SVR = ( uint32_t ) prvTC0_Handler;\r
197 \r
198         /* Start with the interrupt clear. */\r
199     AIC->AIC_ICCR = AIC_ICCR_INTCLR;\r
200 \r
201         /* Do the same for TC1 - which is the high frequency timer. */\r
202     AIC->AIC_SSR = ID_TC1;\r
203     AIC->AIC_IDCR = AIC_IDCR_INTD;\r
204     AIC->AIC_SMR  = AIC_SMR_SRCTYPE_EXT_POSITIVE_EDGE | tmrHIGHER_PRIORITY;\r
205     AIC->AIC_SVR = ( uint32_t ) prvTC1_Handler;\r
206     AIC->AIC_ICCR = AIC_ICCR_INTCLR;\r
207 \r
208         /* Finally enable the interrupts and start the timers. */\r
209         AIC_EnableIT( ID_TC0 );\r
210         AIC_EnableIT( ID_TC1 );\r
211         TC_Start( TC0, tmrTC0_CHANNEL_0 );\r
212         TC_Start( TC0, tmrTC0_CHANNEL_1 );\r
213         TC_Start( TC1, tmrTC1_CHANNEL_0 );\r
214 }\r
215 /*-----------------------------------------------------------*/\r
216 \r
217 static void prvTC0_Handler( void )\r
218 {\r
219 uint32_t ulDidSomething;\r
220 \r
221         do\r
222         {\r
223                 ulDidSomething = pdFALSE;\r
224 \r
225                 /* Read will clear the status bit. */\r
226                 if( ( TC0->TC_CHANNEL[ tmrTC0_CHANNEL_0 ].TC_SR & tmrRC_COMPARE ) != 0 )\r
227                 {\r
228                         /* Call the IntQ test function for this channel. */\r
229                         portYIELD_FROM_ISR( xFirstTimerHandler() );\r
230                         ulDidSomething = pdTRUE;\r
231                 }\r
232 \r
233                 if( ( TC0->TC_CHANNEL[ tmrTC0_CHANNEL_1 ].TC_SR & tmrRC_COMPARE ) != 0 )\r
234                 {\r
235                         /* Call the IntQ test function for this channel. */\r
236                         portYIELD_FROM_ISR( xSecondTimerHandler() );\r
237                         ulDidSomething = pdTRUE;\r
238                 }\r
239 \r
240         } while( ulDidSomething == pdTRUE );\r
241 }\r
242 /*-----------------------------------------------------------*/\r
243 \r
244 static void prvTC1_Handler( void )\r
245 {\r
246 volatile uint32_t ulDummy;\r
247 \r
248     /* Dummy read to clear status bit. */\r
249     ulDummy = TC1->TC_CHANNEL[ tmrTC1_CHANNEL_0 ].TC_SR;\r
250 \r
251         /* Latch the maximum nesting count. */\r
252         if( ulPortInterruptNesting > ulMaxRecordedNesting )\r
253         {\r
254                 ulMaxRecordedNesting = ulPortInterruptNesting;\r
255         }\r
256 \r
257         /* Keep a count of the number of interrupts to use as a time base for the\r
258         run-time stats. */\r
259         ulHighFrequencyTimerCounts++;\r
260 }\r
261 \r