]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/CORTEX_A5_SAMA5D2x_Xplained_IAR/Full_Demo/IntQueueTimer.c
Update version number in preparation for maintenance release.
[freertos] / FreeRTOS / Demo / CORTEX_A5_SAMA5D2x_Xplained_IAR / Full_Demo / IntQueueTimer.c
1 /*\r
2     FreeRTOS V9.0.1 - Copyright (C) 2017 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 "peripherals/pio.h"\r
99 #include "peripherals/pmc.h"\r
100 #include "peripherals/aic.h"\r
101 #include "peripherals/tc.h"\r
102 #include "board.h"\r
103 \r
104 /* The frequencies at which the first two timers expire are slightly offset to\r
105 ensure they don't remain synchronised.  The frequency of the highest priority\r
106 interrupt is 20 times faster so really hammers the interrupt entry and exit\r
107 code. */\r
108 #define tmrTIMER_0_FREQUENCY    ( 2000UL )\r
109 #define tmrTIMER_1_FREQUENCY    ( 1690UL )\r
110 #define tmrTIMER_2_FREQUENCY    ( 20000UL )\r
111 \r
112 /* The channels used in TC0 for generating the three interrupts. */\r
113 #define tmrTC0_CHANNEL_0                0 /* At tmrTIMER_0_FREQUENCY */\r
114 #define tmrTC0_CHANNEL_1                1 /* At tmrTIMER_1_FREQUENCY */\r
115 #define tmrTC1_CHANNEL_0                0 /* At tmrTIMER_2_FREQUENCY */\r
116 \r
117 /* The bit within the RC_SR register that indicates an RC compare. */\r
118 #define tmrRC_COMPARE                   ( 1UL << 4UL )\r
119 \r
120 /* The high frequency interrupt given the highest priority or all.  The priority\r
121 of the lower frequency timers must still be above the tick interrupt priority. */\r
122 #define tmrLOWER_PRIORITY               1\r
123 #define tmrHIGHER_PRIORITY              5\r
124 /*-----------------------------------------------------------*/\r
125 \r
126 /* Handlers for the two timer peripherals - two channels are used in the TC0\r
127 timer. */\r
128 static void prvTC0_Handler( void );\r
129 static void prvTC1_Handler( void );\r
130 \r
131 /* Used to provide a means of ensuring the intended interrupt nesting depth is\r
132 actually being reached. */\r
133 extern uint32_t ulPortInterruptNesting;\r
134 static uint32_t ulMaxRecordedNesting = 0;\r
135 \r
136 /* For convenience the high frequency timer increments a variable that is then\r
137 used as the time base for the run time stats. */\r
138 volatile uint32_t ulHighFrequencyTimerCounts = 0;\r
139 \r
140 /*-----------------------------------------------------------*/\r
141 \r
142 void vInitialiseTimerForIntQueueTest( void )\r
143 {\r
144         /* Enable the TC clocks. */\r
145         pmc_enable_peripheral( ID_TC0 );\r
146         pmc_enable_peripheral( ID_TC1 );\r
147 \r
148         /* Configure TC0 channel 0 for a tmrTIMER_0_FREQUENCY frequency and trigger\r
149         on RC compare. */\r
150         tc_trigger_on_freq( TC0, tmrTC0_CHANNEL_0, tmrTIMER_0_FREQUENCY );\r
151         TC0->TC_CHANNEL[ tmrTC0_CHANNEL_0 ].TC_IER = TC_IER_CPCS;\r
152 \r
153         /* Configure TC0 channel 1 for a tmrTIMER_1_FREQUENCY frequency and trigger\r
154         on RC compare. */\r
155         tc_trigger_on_freq( TC0, tmrTC0_CHANNEL_1, tmrTIMER_1_FREQUENCY );\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. */\r
160         tc_trigger_on_freq( TC1, tmrTC1_CHANNEL_0, tmrTIMER_2_FREQUENCY );\r
161         TC1->TC_CHANNEL[ tmrTC1_CHANNEL_0 ].TC_IER = TC_IER_CPCS;\r
162 \r
163         /* Enable interrupts and start the timers. */\r
164         aic_configure( ID_TC0, tmrLOWER_PRIORITY );\r
165         aic_set_source_vector( ID_TC0, prvTC0_Handler );\r
166         aic_configure( ID_TC1, tmrHIGHER_PRIORITY );\r
167         aic_set_source_vector( ID_TC1, prvTC1_Handler );\r
168         aic_enable( ID_TC0 );\r
169         aic_enable( ID_TC1 );\r
170         tc_start( TC0, tmrTC0_CHANNEL_0 );\r
171         tc_start( TC0, tmrTC0_CHANNEL_1 );\r
172         tc_start( TC1, tmrTC1_CHANNEL_0 );\r
173 }\r
174 /*-----------------------------------------------------------*/\r
175 \r
176 static void prvTC0_Handler( void )\r
177 {\r
178     /* Read will clear the status bit. */\r
179         if( ( TC0->TC_CHANNEL[ tmrTC0_CHANNEL_0 ].TC_SR & tmrRC_COMPARE ) != 0 )\r
180         {\r
181                 portYIELD_FROM_ISR( xFirstTimerHandler() );\r
182         }\r
183 \r
184         if( ( TC0->TC_CHANNEL[ tmrTC0_CHANNEL_1 ].TC_SR & tmrRC_COMPARE ) != 0 )\r
185         {\r
186                 portYIELD_FROM_ISR( xSecondTimerHandler() );\r
187         }\r
188 }\r
189 /*-----------------------------------------------------------*/\r
190 \r
191 static void prvTC1_Handler( void )\r
192 {\r
193 volatile uint32_t ulDummy;\r
194 \r
195     /* Dummy read to clear status bit. */\r
196     ulDummy = TC1->TC_CHANNEL[ tmrTC1_CHANNEL_0 ].TC_SR;\r
197 \r
198         /* Latch the maximum nesting count. */\r
199         if( ulPortInterruptNesting > ulMaxRecordedNesting )\r
200         {\r
201                 ulMaxRecordedNesting = ulPortInterruptNesting;\r
202         }\r
203 \r
204         /* Keep a count of the number of interrupts to use as a time base for the\r
205         run-time stats. */\r
206         ulHighFrequencyTimerCounts++;\r
207 }\r
208 \r