]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/CORTEX_A5_SAMA5D3x_Xplained_IAR/Full_Demo/IntQueueTimer.c
Update version number to 8.1.2 after moving the defaulting of configUSE_PORT_OPTIMISE...
[freertos] / FreeRTOS / Demo / CORTEX_A5_SAMA5D3x_Xplained_IAR / Full_Demo / IntQueueTimer.c
1 /*\r
2     FreeRTOS V8.1.2 - 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     ***************************************************************************\r
8      *                                                                       *\r
9      *    FreeRTOS provides completely free yet professionally developed,    *\r
10      *    robust, strictly quality controlled, supported, and cross          *\r
11      *    platform software that has become a de facto standard.             *\r
12      *                                                                       *\r
13      *    Help yourself get started quickly and support the FreeRTOS         *\r
14      *    project by purchasing a FreeRTOS tutorial book, reference          *\r
15      *    manual, or both from: http://www.FreeRTOS.org/Documentation        *\r
16      *                                                                       *\r
17      *    Thank you!                                                         *\r
18      *                                                                       *\r
19     ***************************************************************************\r
20 \r
21     This file is part of the FreeRTOS distribution.\r
22 \r
23     FreeRTOS is free software; you can redistribute it and/or modify it under\r
24     the terms of the GNU General Public License (version 2) as published by the\r
25     Free Software Foundation >>!AND MODIFIED BY!<< the FreeRTOS exception.\r
26 \r
27     >>!   NOTE: The modification to the GPL is included to allow you to     !<<\r
28     >>!   distribute a combined work that includes FreeRTOS without being   !<<\r
29     >>!   obliged to provide the source code for proprietary components     !<<\r
30     >>!   outside of the FreeRTOS kernel.                                   !<<\r
31 \r
32     FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY\r
33     WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS\r
34     FOR A PARTICULAR PURPOSE.  Full license text is available from the following\r
35     link: http://www.freertos.org/a00114.html\r
36 \r
37     1 tab == 4 spaces!\r
38 \r
39     ***************************************************************************\r
40      *                                                                       *\r
41      *    Having a problem?  Start by reading the FAQ "My application does   *\r
42      *    not run, what could be wrong?"                                     *\r
43      *                                                                       *\r
44      *    http://www.FreeRTOS.org/FAQHelp.html                               *\r
45      *                                                                       *\r
46     ***************************************************************************\r
47 \r
48     http://www.FreeRTOS.org - Documentation, books, training, latest versions,\r
49     license and Real Time Engineers Ltd. contact details.\r
50 \r
51     http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,\r
52     including FreeRTOS+Trace - an indispensable productivity tool, a DOS\r
53     compatible FAT file system, and our tiny thread aware UDP/IP stack.\r
54 \r
55     http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High\r
56     Integrity Systems to sell under the OpenRTOS brand.  Low cost OpenRTOS\r
57     licenses offer ticketed support, indemnification and middleware.\r
58 \r
59     http://www.SafeRTOS.com - High Integrity Systems also provide a safety\r
60     engineered and independently SIL3 certified version for use in safety and\r
61     mission critical applications that require provable dependability.\r
62 \r
63     1 tab == 4 spaces!\r
64 */\r
65 \r
66 /*\r
67  * This file initialises three timers as follows:\r
68  *\r
69  * TC0 channels 0 and 1 provide the interrupts that are used with the IntQ\r
70  * standard demo tasks, which test interrupt nesting and using queues from\r
71  * interrupts.  As the interrupt is shared the nesting achieved is not as deep\r
72  * as normal when this test is executed, but still worth while.\r
73  *\r
74  * TC2 channel 0 provides a much higher frequency timer that tests the nesting\r
75  * of interrupts that don't use the FreeRTOS API.  For convenience, the high\r
76  * frequency timer also keeps a count of the number of time it executes, and the\r
77  * count is used as the time base for the run time stats (which can be viewed\r
78  * through the CLI).\r
79  *\r
80  * All the timers can nest with the tick interrupt - creating a maximum\r
81  * interrupt nesting depth of 3 (normally 4, if the first two timers used\r
82  * separate interrupts).\r
83  *\r
84  */\r
85 \r
86 /* Scheduler includes. */\r
87 #include "FreeRTOS.h"\r
88 \r
89 /* Demo includes. */\r
90 #include "IntQueueTimer.h"\r
91 #include "IntQueue.h"\r
92 \r
93 /* Library includes. */\r
94 #include "board.h"\r
95 \r
96 /* The frequencies at which the first two timers expire are slightly offset to\r
97 ensure they don't remain synchronised.  The frequency of the highest priority\r
98 interrupt is 20 times faster so really hammers the interrupt entry and exit\r
99 code. */\r
100 #define tmrTIMER_0_FREQUENCY    ( 2000UL )\r
101 #define tmrTIMER_1_FREQUENCY    ( 2003UL )\r
102 #define tmrTIMER_2_FREQUENCY    ( 20000UL )\r
103 \r
104 /* The channels used in TC0 for generating the three interrupts. */\r
105 #define tmrTC0_CHANNEL_0                0 /* At tmrTIMER_0_FREQUENCY */\r
106 #define tmrTC0_CHANNEL_1                1 /* At tmrTIMER_1_FREQUENCY */\r
107 #define tmrTC1_CHANNEL_0                0 /* At tmrTIMER_2_FREQUENCY */\r
108 \r
109 /* The bit within the RC_SR register that indicates an RC compare. */\r
110 #define tmrRC_COMPARE                   ( 1UL << 4UL )\r
111 \r
112 /* The high frequency interrupt given the highest priority or all.  The priority\r
113 of the lower frequency timers must still be above the tick interrupt priority. */\r
114 #define tmrLOWER_PRIORITY               1\r
115 #define tmrHIGHER_PRIORITY              5\r
116 /*-----------------------------------------------------------*/\r
117 \r
118 /* Handlers for the two timer peripherals - two channels are used in the TC0\r
119 timer. */\r
120 static void prvTC0_Handler( void );\r
121 static void prvTC1_Handler( void );\r
122 \r
123 /* Used to provide a means of ensuring the intended interrupt nesting depth is\r
124 actually being reached. */\r
125 extern uint32_t ulPortInterruptNesting;\r
126 static uint32_t ulMaxRecordedNesting = 0;\r
127 \r
128 /* For convenience the high frequency timer increments a variable that is then\r
129 used as the time base for the run time stats. */\r
130 volatile uint32_t ulHighFrequencyTimerCounts = 0;\r
131 \r
132 /*-----------------------------------------------------------*/\r
133 \r
134 void vInitialiseTimerForIntQueueTest( void )\r
135 {\r
136 const uint32_t ulDivider = 128UL, ulTCCLKS = 3UL;\r
137 \r
138         /* Enable the TC clocks. */\r
139         PMC->PMC_PCER0 = 1 << ID_TC0;\r
140         PMC->PMC_PCER0 = 1 << ID_TC1;\r
141 \r
142         /* Configure TC0 channel 0 for a tmrTIMER_0_FREQUENCY frequency and trigger\r
143         on RC compare. */\r
144         TC_Configure( TC0, tmrTC0_CHANNEL_0, ulTCCLKS | TC_CMR_CPCTRG );\r
145         TC0->TC_CHANNEL[ tmrTC0_CHANNEL_0 ].TC_RC = BOARD_MCK / ( tmrTIMER_0_FREQUENCY * ulDivider );\r
146         TC0->TC_CHANNEL[ tmrTC0_CHANNEL_0 ].TC_IER = TC_IER_CPCS;\r
147 \r
148         /* Configure TC0 channel 1 for a tmrTIMER_1_FREQUENCY frequency and trigger\r
149         on RC compare. */\r
150         TC_Configure( TC0, tmrTC0_CHANNEL_1, ulTCCLKS | TC_CMR_CPCTRG );\r
151         TC0->TC_CHANNEL[ tmrTC0_CHANNEL_1 ].TC_RC = BOARD_MCK / ( tmrTIMER_1_FREQUENCY * ulDivider );\r
152         TC0->TC_CHANNEL[ tmrTC0_CHANNEL_1 ].TC_IER = TC_IER_CPCS;\r
153 \r
154         /* Configure TC1 channel 0 tmrTIMER_2_FREQUENCY frequency and trigger on\r
155         RC compare. */\r
156         TC_Configure( TC1, tmrTC1_CHANNEL_0, ulTCCLKS | TC_CMR_CPCTRG );\r
157         TC1->TC_CHANNEL[ tmrTC1_CHANNEL_0 ].TC_RC = BOARD_MCK / ( tmrTIMER_2_FREQUENCY * ulDivider );\r
158         TC1->TC_CHANNEL[ tmrTC1_CHANNEL_0 ].TC_IER = TC_IER_CPCS;\r
159 \r
160         /* Enable interrupts and start the timers. */\r
161         IRQ_ConfigureIT( ID_TC0, tmrLOWER_PRIORITY, prvTC0_Handler );\r
162         IRQ_ConfigureIT( ID_TC1, tmrHIGHER_PRIORITY, prvTC1_Handler );\r
163         IRQ_EnableIT( ID_TC0 );\r
164         IRQ_EnableIT( ID_TC1 );\r
165         TC_Start( TC0, tmrTC0_CHANNEL_0 );\r
166         TC_Start( TC0, tmrTC0_CHANNEL_1 );\r
167         TC_Start( TC1, tmrTC1_CHANNEL_0 );\r
168 }\r
169 /*-----------------------------------------------------------*/\r
170 \r
171 static void prvTC0_Handler( void )\r
172 {\r
173     /* Read will clear the status bit. */\r
174         if( ( TC0->TC_CHANNEL[ tmrTC0_CHANNEL_0 ].TC_SR & tmrRC_COMPARE ) != 0 )\r
175         {\r
176                 portYIELD_FROM_ISR( xFirstTimerHandler() );\r
177         }\r
178 \r
179         if( ( TC0->TC_CHANNEL[ tmrTC0_CHANNEL_1 ].TC_SR & tmrRC_COMPARE ) != 0 )\r
180         {\r
181                 portYIELD_FROM_ISR( xSecondTimerHandler() );\r
182         }\r
183 }\r
184 /*-----------------------------------------------------------*/\r
185 \r
186 static void prvTC1_Handler( void )\r
187 {\r
188 volatile uint32_t ulDummy;\r
189 \r
190     /* Dummy read to clear status bit. */\r
191     ulDummy = TC1->TC_CHANNEL[ tmrTC1_CHANNEL_0 ].TC_SR;\r
192 \r
193         /* Latch the maximum nesting count. */\r
194         if( ulPortInterruptNesting > ulMaxRecordedNesting )\r
195         {\r
196                 ulMaxRecordedNesting = ulPortInterruptNesting;\r
197         }\r
198 \r
199         /* Keep a count of the number of interrupts to use as a time base for the\r
200         run-time stats. */\r
201         ulHighFrequencyTimerCounts++;\r
202 }\r
203 \r