]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/CORTEX_A5_SAMA5D3x_Xplained_IAR/Full_Demo/IntQueueTimer.c
8a11f5f5a0b29d01ae6ea69a61e19205cc79d063
[freertos] / FreeRTOS / Demo / CORTEX_A5_SAMA5D3x_Xplained_IAR / Full_Demo / IntQueueTimer.c
1 /*\r
2     FreeRTOS V8.0.1 - 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 of\r
75  * interrupts that execute above the maximum syscall interrupt priority.\r
76  *\r
77  * All the timers can nest with the tick interrupt - creating a maximum\r
78  * interrupt nesting depth of 3 (normally 4, if the first two timers used\r
79  * separate interrupts).\r
80  *\r
81  * For convenience, the high frequency timer is also used to provide the time\r
82  * base for the run time stats.\r
83  */\r
84 \r
85 /* Scheduler includes. */\r
86 #include "FreeRTOS.h"\r
87 \r
88 /* Demo includes. */\r
89 #include "IntQueueTimer.h"\r
90 #include "IntQueue.h"\r
91 \r
92 /* Library includes. */\r
93 #include "board.h"\r
94 \r
95 /* The frequencies at which the first two timers expire are slightly offset to\r
96 ensure they don't remain synchronised.  The frequency of the highest priority\r
97 interrupt is 20 times faster so really hammers the interrupt entry and exit\r
98 code. */\r
99 #define tmrTIMERS_USED  3\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 three timer channels. */\r
119 static void prvTC0_Handler( void );\r
120 static void prvTC1_Handler( void );\r
121 \r
122 /* Used to provide a means of ensuring the intended interrupt nesting depth is\r
123 actually being reached. */\r
124 extern uint32_t ulPortInterruptNesting;\r
125 static uint32_t ulMaxRecordedNesting = 0;\r
126 \r
127 /* For convenience the high frequency timer increments a variable that is then\r
128 used as the time base for the run time stats. */\r
129 volatile uint32_t ulHighFrequencyTimerCounts = 0;\r
130 \r
131 /*-----------------------------------------------------------*/\r
132 \r
133 void vInitialiseTimerForIntQueueTest( void )\r
134 {\r
135 const uint32_t ulDivider = 128UL, ulTCCLKS = 3UL;\r
136 \r
137         /* Enable the TC clocks. */\r
138         PMC->PMC_PCER0 = 1 << ID_TC0;\r
139         PMC->PMC_PCER0 = 1 << ID_TC1;\r
140 \r
141         /* Configure TC0 channel 0 for a tmrTIMER_0_FREQUENCY frequency and trigger\r
142         on RC compare. */\r
143         TC_Configure( TC0, tmrTC0_CHANNEL_0, ulTCCLKS | TC_CMR_CPCTRG );\r
144         TC0->TC_CHANNEL[ tmrTC0_CHANNEL_0 ].TC_RC = BOARD_MCK / ( tmrTIMER_0_FREQUENCY * ulDivider );\r
145         TC0->TC_CHANNEL[ tmrTC0_CHANNEL_0 ].TC_IER = TC_IER_CPCS;\r
146 \r
147         /* Configure TC0 channel 1 for a tmrTIMER_1_FREQUENCY frequency and trigger\r
148         on RC compare. */\r
149         TC_Configure( TC0, tmrTC0_CHANNEL_1, ulTCCLKS | TC_CMR_CPCTRG );\r
150         TC0->TC_CHANNEL[ tmrTC0_CHANNEL_1 ].TC_RC = BOARD_MCK / ( tmrTIMER_1_FREQUENCY * ulDivider );\r
151         TC0->TC_CHANNEL[ tmrTC0_CHANNEL_1 ].TC_IER = TC_IER_CPCS;\r
152 \r
153         /* Configure TC1 channel 0 tmrTIMER_2_FREQUENCY frequency and trigger on\r
154         RC compare. */\r
155         TC_Configure( TC1, tmrTC1_CHANNEL_0, ulTCCLKS | TC_CMR_CPCTRG );\r
156         TC1->TC_CHANNEL[ tmrTC1_CHANNEL_0 ].TC_RC = BOARD_MCK / ( tmrTIMER_2_FREQUENCY * ulDivider );\r
157         TC1->TC_CHANNEL[ tmrTC1_CHANNEL_0 ].TC_IER = TC_IER_CPCS;\r
158 \r
159         /* Enable interrupts and start the timers. */\r
160         IRQ_ConfigureIT( ID_TC0, tmrLOWER_PRIORITY, prvTC0_Handler );\r
161         IRQ_ConfigureIT( ID_TC1, tmrHIGHER_PRIORITY, prvTC1_Handler );\r
162         IRQ_EnableIT( ID_TC0 );\r
163         IRQ_EnableIT( ID_TC1 );\r
164         TC_Start( TC0, tmrTC0_CHANNEL_0 );\r
165         TC_Start( TC0, tmrTC0_CHANNEL_1 );\r
166         TC_Start( TC1, tmrTC1_CHANNEL_0 );\r
167 }\r
168 /*-----------------------------------------------------------*/\r
169 \r
170 static void prvTC0_Handler( void )\r
171 {\r
172 #warning Why can interrupts only be enabled inside the C function?\r
173         __enable_interrupt();\r
174 \r
175     /* Read will clear the status bit. */\r
176         if( ( TC0->TC_CHANNEL[ tmrTC0_CHANNEL_0 ].TC_SR & tmrRC_COMPARE ) != 0 )\r
177         {\r
178                 portYIELD_FROM_ISR( xFirstTimerHandler() );\r
179         }\r
180 \r
181         if( ( TC0->TC_CHANNEL[ tmrTC0_CHANNEL_1 ].TC_SR & tmrRC_COMPARE ) != 0 )\r
182         {\r
183                 portYIELD_FROM_ISR( xSecondTimerHandler() );\r
184         }\r
185 }\r
186 /*-----------------------------------------------------------*/\r
187 \r
188 static void prvTC1_Handler( void )\r
189 {\r
190 volatile uint32_t ulDummy;\r
191 \r
192         __enable_interrupt();\r
193 \r
194     /* Dummy read to clear status bit. */\r
195     ulDummy = TC1->TC_CHANNEL[ tmrTC1_CHANNEL_0 ].TC_SR;\r
196 \r
197         /* Latch the maximum nesting count. */\r
198         if( ulPortInterruptNesting > ulMaxRecordedNesting )\r
199         {\r
200                 ulMaxRecordedNesting = ulPortInterruptNesting;\r
201         }\r
202 \r
203         /* Keep a count of the number of interrupts to use as a time base for the\r
204         run-time stats. */\r
205         ulHighFrequencyTimerCounts++;\r
206 }\r
207 \r