]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/CORTEX_A5_SAMA5D4x_EK_IAR/Full_Demo/IntQueueTimer.c
Update version number to 9.0.0rc2.
[freertos] / FreeRTOS / Demo / CORTEX_A5_SAMA5D4x_EK_IAR / Full_Demo / IntQueueTimer.c
1 /*\r
2     FreeRTOS V9.0.0rc2 - Copyright (C) 2016 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     ***************************************************************************\r
14     >>!   NOTE: The modification to the GPL is included to allow you to     !<<\r
15     >>!   distribute a combined work that includes FreeRTOS without being   !<<\r
16     >>!   obliged to provide the source code for proprietary components     !<<\r
17     >>!   outside of the FreeRTOS kernel.                                   !<<\r
18     ***************************************************************************\r
19 \r
20     FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY\r
21     WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS\r
22     FOR A PARTICULAR PURPOSE.  Full license text is available on the following\r
23     link: http://www.freertos.org/a00114.html\r
24 \r
25     ***************************************************************************\r
26      *                                                                       *\r
27      *    FreeRTOS provides completely free yet professionally developed,    *\r
28      *    robust, strictly quality controlled, supported, and cross          *\r
29      *    platform software that is more than just the market leader, it     *\r
30      *    is the industry's de facto standard.                               *\r
31      *                                                                       *\r
32      *    Help yourself get started quickly while simultaneously helping     *\r
33      *    to support the FreeRTOS project by purchasing a FreeRTOS           *\r
34      *    tutorial book, reference manual, or both:                          *\r
35      *    http://www.FreeRTOS.org/Documentation                              *\r
36      *                                                                       *\r
37     ***************************************************************************\r
38 \r
39     http://www.FreeRTOS.org/FAQHelp.html - Having a problem?  Start by reading\r
40     the FAQ page "My application does not run, what could be wrong?".  Have you\r
41     defined configASSERT()?\r
42 \r
43     http://www.FreeRTOS.org/support - In return for receiving this top quality\r
44     embedded software for free we request you assist our global community by\r
45     participating in the support forum.\r
46 \r
47     http://www.FreeRTOS.org/training - Investing in training allows your team to\r
48     be as productive as possible as early as possible.  Now you can receive\r
49     FreeRTOS training directly from Richard Barry, CEO of Real Time Engineers\r
50     Ltd, and the world's leading authority on the world's leading RTOS.\r
51 \r
52     http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,\r
53     including FreeRTOS+Trace - an indispensable productivity tool, a DOS\r
54     compatible FAT file system, and our tiny thread aware UDP/IP stack.\r
55 \r
56     http://www.FreeRTOS.org/labs - Where new FreeRTOS products go to incubate.\r
57     Come and try FreeRTOS+TCP, our new open source TCP/IP stack for FreeRTOS.\r
58 \r
59     http://www.OpenRTOS.com - Real Time Engineers ltd. license FreeRTOS to High\r
60     Integrity Systems ltd. to sell under the OpenRTOS brand.  Low cost OpenRTOS\r
61     licenses offer ticketed support, indemnification and commercial middleware.\r
62 \r
63     http://www.SafeRTOS.com - High Integrity Systems also provide a safety\r
64     engineered and independently SIL3 certified version for use in safety and\r
65     mission critical applications that require provable dependability.\r
66 \r
67     1 tab == 4 spaces!\r
68 */\r
69 \r
70 /*\r
71  * This file initialises three timers as follows:\r
72  *\r
73  * TC0 channels 0 and 1 provide the interrupts that are used with the IntQ\r
74  * standard demo tasks, which test interrupt nesting and using queues from\r
75  * interrupts.  As the interrupt is shared the nesting achieved is not as deep\r
76  * as normal when this test is executed, but still worth while.\r
77  *\r
78  * TC2 channel 0 provides a much higher frequency timer that tests the nesting\r
79  * of interrupts that don't use the FreeRTOS API.  For convenience, the high\r
80  * frequency timer also keeps a count of the number of time it executes, and the\r
81  * count is used as the time base for the run time stats (which can be viewed\r
82  * through the CLI).\r
83  *\r
84  * All the timers can nest with the tick interrupt - creating a maximum\r
85  * interrupt nesting depth of 3 (normally 4, if the first two timers used\r
86  * separate interrupts).\r
87  *\r
88  */\r
89 \r
90 /* Scheduler includes. */\r
91 #include "FreeRTOS.h"\r
92 \r
93 /* Demo includes. */\r
94 #include "IntQueueTimer.h"\r
95 #include "IntQueue.h"\r
96 \r
97 /* Library includes. */\r
98 #include "board.h"\r
99 \r
100 /* The frequencies at which the first two timers expire are slightly offset to\r
101 ensure they don't remain synchronised.  The frequency of the highest priority\r
102 interrupt is 20 times faster so really hammers the interrupt entry and exit\r
103 code. */\r
104 #define tmrTIMER_0_FREQUENCY    ( 2000UL )\r
105 #define tmrTIMER_1_FREQUENCY    ( 2003UL )\r
106 #define tmrTIMER_2_FREQUENCY    ( 20000UL )\r
107 \r
108 /* The channels used in TC0 for generating the three interrupts. */\r
109 #define tmrTC0_CHANNEL_0                0 /* At tmrTIMER_0_FREQUENCY */\r
110 #define tmrTC0_CHANNEL_1                1 /* At tmrTIMER_1_FREQUENCY */\r
111 #define tmrTC1_CHANNEL_0                0 /* At tmrTIMER_2_FREQUENCY */\r
112 \r
113 /* The bit within the RC_SR register that indicates an RC compare. */\r
114 #define tmrRC_COMPARE                   ( 1UL << 4UL )\r
115 \r
116 /* The high frequency interrupt given the highest priority or all.  The priority\r
117 of the lower frequency timers must still be above the tick interrupt priority. */\r
118 #define tmrLOWER_PRIORITY               3\r
119 #define tmrHIGHER_PRIORITY              5\r
120 /*-----------------------------------------------------------*/\r
121 \r
122 /* Handlers for the two timer peripherals - two channels are used in the TC0\r
123 timer. */\r
124 static void prvTC0_Handler( void );\r
125 static void prvTC1_Handler( void );\r
126 \r
127 /* Used to provide a means of ensuring the intended interrupt nesting depth is\r
128 actually being reached. */\r
129 extern uint32_t ulPortInterruptNesting;\r
130 static uint32_t ulMaxRecordedNesting = 0;\r
131 \r
132 /* For convenience the high frequency timer increments a variable that is then\r
133 used as the time base for the run time stats. */\r
134 volatile uint32_t ulHighFrequencyTimerCounts = 0;\r
135 \r
136 /*-----------------------------------------------------------*/\r
137 \r
138 void vInitialiseTimerForIntQueueTest( void )\r
139 {\r
140 const uint32_t ulDivider = 128UL, ulTCCLKS = 3UL;\r
141 \r
142         /* Enable the TC clocks. */\r
143         PMC_EnablePeripheral( ID_TC0 );\r
144         PMC_EnablePeripheral( ID_TC1 );\r
145 \r
146         /* Configure TC0 channel 0 for a tmrTIMER_0_FREQUENCY frequency and trigger\r
147         on RC compare.  This is part of the IntQTimer test. */\r
148         TC_Configure( TC0, tmrTC0_CHANNEL_0, ulTCCLKS | TC_CMR_CPCTRG );\r
149         TC0->TC_CHANNEL[ tmrTC0_CHANNEL_0 ].TC_RC = ( BOARD_MCK / 2 ) / ( tmrTIMER_0_FREQUENCY * ulDivider );\r
150         TC0->TC_CHANNEL[ tmrTC0_CHANNEL_0 ].TC_IER = TC_IER_CPCS;\r
151 \r
152         /* Configure TC0 channel 1 for a tmrTIMER_1_FREQUENCY frequency and trigger\r
153         on RC compare.  This is part of the IntQTimer test. */\r
154         TC_Configure( TC0, tmrTC0_CHANNEL_1, ulTCCLKS | TC_CMR_CPCTRG );\r
155         TC0->TC_CHANNEL[ tmrTC0_CHANNEL_1 ].TC_RC = ( BOARD_MCK / 2 ) / ( tmrTIMER_1_FREQUENCY * ulDivider );\r
156         TC0->TC_CHANNEL[ tmrTC0_CHANNEL_1 ].TC_IER = TC_IER_CPCS;\r
157 \r
158         /* Configure TC1 channel 0 tmrTIMER_2_FREQUENCY frequency and trigger on\r
159         RC compare.  This is the very high frequency timer. */\r
160         TC_Configure( TC1, tmrTC1_CHANNEL_0, ulTCCLKS | TC_CMR_CPCTRG );\r
161         TC1->TC_CHANNEL[ tmrTC1_CHANNEL_0 ].TC_RC = BOARD_MCK / ( tmrTIMER_2_FREQUENCY * ulDivider );\r
162         TC1->TC_CHANNEL[ tmrTC1_CHANNEL_0 ].TC_IER = TC_IER_CPCS;\r
163 \r
164         /* First setup TC0 interrupt, in which two channels are used. */\r
165     AIC->AIC_SSR = ID_TC0;\r
166 \r
167         /* Ensure the interrupt is disabled before setting mode and handler. */\r
168     AIC->AIC_IDCR = AIC_IDCR_INTD;\r
169     AIC->AIC_SMR  = AIC_SMR_SRCTYPE_EXT_POSITIVE_EDGE |  tmrLOWER_PRIORITY;\r
170     AIC->AIC_SVR = ( uint32_t ) prvTC0_Handler;\r
171 \r
172         /* Start with the interrupt clear. */\r
173     AIC->AIC_ICCR = AIC_ICCR_INTCLR;\r
174 \r
175         /* Do the same for TC1 - which is the high frequency timer. */\r
176     AIC->AIC_SSR = ID_TC1;\r
177     AIC->AIC_IDCR = AIC_IDCR_INTD;\r
178     AIC->AIC_SMR  = AIC_SMR_SRCTYPE_EXT_POSITIVE_EDGE | tmrHIGHER_PRIORITY;\r
179     AIC->AIC_SVR = ( uint32_t ) prvTC1_Handler;\r
180     AIC->AIC_ICCR = AIC_ICCR_INTCLR;\r
181 \r
182         /* Finally enable the interrupts and start the timers. */\r
183         AIC_EnableIT( ID_TC0 );\r
184         AIC_EnableIT( ID_TC1 );\r
185         TC_Start( TC0, tmrTC0_CHANNEL_0 );\r
186         TC_Start( TC0, tmrTC0_CHANNEL_1 );\r
187         TC_Start( TC1, tmrTC1_CHANNEL_0 );\r
188 }\r
189 /*-----------------------------------------------------------*/\r
190 \r
191 static void prvTC0_Handler( void )\r
192 {\r
193 uint32_t ulDidSomething;\r
194 \r
195         do\r
196         {\r
197                 ulDidSomething = pdFALSE;\r
198 \r
199                 /* Read will clear the status bit. */\r
200                 if( ( TC0->TC_CHANNEL[ tmrTC0_CHANNEL_0 ].TC_SR & tmrRC_COMPARE ) != 0 )\r
201                 {\r
202                         /* Call the IntQ test function for this channel. */\r
203                         portYIELD_FROM_ISR( xFirstTimerHandler() );\r
204                         ulDidSomething = pdTRUE;\r
205                 }\r
206 \r
207                 if( ( TC0->TC_CHANNEL[ tmrTC0_CHANNEL_1 ].TC_SR & tmrRC_COMPARE ) != 0 )\r
208                 {\r
209                         /* Call the IntQ test function for this channel. */\r
210                         portYIELD_FROM_ISR( xSecondTimerHandler() );\r
211                         ulDidSomething = pdTRUE;\r
212                 }\r
213 \r
214         } while( ulDidSomething == pdTRUE );\r
215 }\r
216 /*-----------------------------------------------------------*/\r
217 \r
218 static void prvTC1_Handler( void )\r
219 {\r
220 volatile uint32_t ulDummy;\r
221 \r
222     /* Dummy read to clear status bit. */\r
223     ulDummy = TC1->TC_CHANNEL[ tmrTC1_CHANNEL_0 ].TC_SR;\r
224 \r
225         /* Latch the maximum nesting count. */\r
226         if( ulPortInterruptNesting > ulMaxRecordedNesting )\r
227         {\r
228                 ulMaxRecordedNesting = ulPortInterruptNesting;\r
229         }\r
230 \r
231         /* Keep a count of the number of interrupts to use as a time base for the\r
232         run-time stats. */\r
233         ulHighFrequencyTimerCounts++;\r
234 }\r
235 \r