]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/TriCore_TC1782_TriBoard_GCC/RTOSDemo/InterruptNestTest.c
Update version numbers in preparation for V8.2.0 release candidate 1.
[freertos] / FreeRTOS / Demo / TriCore_TC1782_TriBoard_GCC / RTOSDemo / InterruptNestTest.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  * It is intended that the tasks and timers in this file cause interrupts to\r
98  * nest at least one level deeper than they would otherwise.\r
99  *\r
100  * A timer is configured to create an interrupt at moderately high frequency,\r
101  * as defined by the tmrtestHIGH_FREQUENCY_TIMER_TEST_HZ constant.  The\r
102  * interrupt priority is by configHIGH_FREQUENCY_TIMER_PRIORITY, which is set\r
103  * to ( configMAX_SYSCALL_INTERRUPT_PRIORITY - 1UL ), the second highest\r
104  * priority from which interrupt safe FreeRTOS API calls can be made.\r
105  *\r
106  * The timer interrupt handler counts the number of times it is called.  When\r
107  * it has determined that the number of calls means that 10ms has passed, it\r
108  * 'gives' a semaphore, and resets it call count.\r
109  *\r
110  * A high priority task is used to receive the data posted to the queue by the\r
111  * interrupt service routine.  The task should, then, leave the blocked state\r
112  * and 'take' the available semaphore every 10ms.  The frequency at which it\r
113  * actually leaves the blocked state is verified by the demo's check task (see\r
114  * the the documentation for the entire demo on the FreeRTOS.org web site),\r
115  * which then flags an error if the frequency lower than expected.\r
116  */\r
117 \r
118 /* Standard includes. */\r
119 #include <stdlib.h>\r
120 #include <string.h>\r
121 \r
122 /* Scheduler includes. */\r
123 #include "FreeRTOS.h"\r
124 #include "task.h"\r
125 #include "semphr.h"\r
126 \r
127 /* Demo application includes. */\r
128 #include "InterruptNestTest.h"\r
129 \r
130 /* TriCore specific includes. */\r
131 #include <tc1782.h>\r
132 #include <machine/intrinsics.h>\r
133 #include <machine/cint.h>\r
134 #include <machine/wdtcon.h>\r
135 \r
136 /* This constant is specific to this test application.  It allows the high\r
137 frequency (interrupt nesting test) timer to know how often to trigger, and the\r
138 check task to know how many iterations to expect at any given time. */\r
139 #define tmrtestHIGH_FREQUENCY_TIMER_TEST_HZ             ( 8931UL )\r
140 \r
141 /*\r
142  * The handler for the high priority timer interrupt.\r
143  */\r
144 static void prvPortHighFrequencyTimerHandler( int iArg ) __attribute__((longcall));\r
145 \r
146 /*\r
147  * The task that receives messages posted to a queue by the higher priority\r
148  * timer interrupt.\r
149  */\r
150 static void prvHighFrequencyTimerTask( void *pvParameters );\r
151 \r
152 /* Constants used to configure the timer and determine how often the task\r
153 should receive data. */\r
154 static const unsigned long ulCompareMatchValue = configPERIPHERAL_CLOCK_HZ / tmrtestHIGH_FREQUENCY_TIMER_TEST_HZ;\r
155 static const unsigned long ulInterruptsPer10ms = tmrtestHIGH_FREQUENCY_TIMER_TEST_HZ / 100UL;\r
156 static const unsigned long ulSemaphoreGiveRate_ms = 10UL;\r
157 \r
158 /* The semaphore used to synchronise the interrupt with the task. */\r
159 static SemaphoreHandle_t xHighFrequencyTimerSemaphore = NULL;\r
160 \r
161 /* Holds the count of the number of times the task is unblocked by the timer. */\r
162 static volatile unsigned long ulHighFrequencyTaskIterations = 0UL;\r
163 \r
164 /*-----------------------------------------------------------*/\r
165 \r
166 void vSetupInterruptNestingTest( void )\r
167 {\r
168 unsigned long ulCompareMatchBits;\r
169 \r
170         /* Create the semaphore used to communicate between the high frequency\r
171         interrupt and the task. */\r
172         vSemaphoreCreateBinary( xHighFrequencyTimerSemaphore );\r
173         configASSERT( xHighFrequencyTimerSemaphore );\r
174         \r
175         /* Create the task that pends on the semaphore that is given by the\r
176         high frequency interrupt. */\r
177         xTaskCreate( prvHighFrequencyTimerTask, "HFTmr", configMINIMAL_STACK_SIZE, NULL, configMAX_PRIORITIES - 1, NULL );\r
178         \r
179         /* Setup the interrupt itself.  The STM module clock divider is setup when \r
180         the tick interrupt is configured - which is when the scheduler is started - \r
181         so there is no need     to do it here.\r
182 \r
183         The tick interrupt uses compare match 0, so this test uses compare match\r
184         1, which means shifting up the values by 16 before writing them to the\r
185         register. */\r
186         ulCompareMatchBits = ( 0x1fUL - __CLZ( ulCompareMatchValue ) );\r
187         ulCompareMatchBits <<= 16UL;\r
188         \r
189         /* Write the values to the relevant SMT registers, without changing other\r
190         bits. */\r
191         taskENTER_CRITICAL();\r
192         {\r
193                 STM_CMCON.reg &= ~( 0x1fUL << 16UL );\r
194                 STM_CMCON.reg |= ulCompareMatchBits;\r
195                 STM_CMP1.reg = ulCompareMatchValue;\r
196 \r
197                 if( 0 != _install_int_handler( configHIGH_FREQUENCY_TIMER_PRIORITY, prvPortHighFrequencyTimerHandler, 0 ) )\r
198                 {\r
199                         /* Set-up the interrupt. */\r
200                         STM_SRC1.reg = ( configHIGH_FREQUENCY_TIMER_PRIORITY | 0x00005000UL );\r
201         \r
202                         /* Enable the Interrupt. */\r
203                         STM_ISRR.reg &= ~( 0x03UL << 2UL );\r
204                         STM_ISRR.reg |= ( 0x1UL << 2UL );\r
205                         STM_ICR.reg &= ~( 0x07UL << 4UL );\r
206                         STM_ICR.reg |= ( 0x5UL << 4UL );\r
207                 }\r
208                 else\r
209                 {\r
210                         /* Failed to install the interrupt. */\r
211                         configASSERT( ( ( volatile void * ) NULL ) );\r
212                 }\r
213         }\r
214         taskEXIT_CRITICAL();\r
215 }\r
216 /*-----------------------------------------------------------*/\r
217 \r
218 unsigned long ulInterruptNestingTestGetIterationCount( unsigned long *pulExpectedIncFrequency_ms )\r
219 {\r
220 unsigned long ulReturn;\r
221 \r
222         *pulExpectedIncFrequency_ms = ulSemaphoreGiveRate_ms;\r
223         portENTER_CRITICAL();\r
224         {\r
225                 ulReturn = ulHighFrequencyTaskIterations;\r
226                 ulHighFrequencyTaskIterations = 0UL;\r
227         }\r
228 \r
229         return ulReturn;\r
230 }\r
231 /*-----------------------------------------------------------*/\r
232 \r
233 static void prvHighFrequencyTimerTask( void *pvParameters )\r
234 {\r
235         /* Just to remove compiler warnings about the unused parameter. */\r
236         ( void ) pvParameters;\r
237 \r
238         for( ;; )\r
239         {\r
240                 /* Wait for the next trigger from the high frequency timer interrupt. */\r
241                 xSemaphoreTake( xHighFrequencyTimerSemaphore, portMAX_DELAY );\r
242                 \r
243                 /* Just count how many times the task has been unblocked before\r
244                 returning to wait for the semaphore again. */\r
245                 ulHighFrequencyTaskIterations++;\r
246         }\r
247 }\r
248 /*-----------------------------------------------------------*/\r
249 \r
250 static void prvPortHighFrequencyTimerHandler( int iArg )\r
251 {\r
252 static volatile unsigned long ulExecutionCounter = 0UL;\r
253 unsigned long ulHigherPriorityTaskWoken = pdFALSE;\r
254 \r
255         /* Just to avoid compiler warnings about unused parameters. */\r
256         ( void ) iArg;\r
257 \r
258         /* Clear the interrupt source. */\r
259         STM_ISRR.reg = 1UL << 2UL;\r
260 \r
261         /* Reload the Compare Match register for X ticks into the future.*/\r
262         STM_CMP1.reg += ulCompareMatchValue;\r
263 \r
264         ulExecutionCounter++;\r
265         \r
266         if( ulExecutionCounter >= ulInterruptsPer10ms )\r
267         {\r
268                 ulExecutionCounter = xSemaphoreGiveFromISR( xHighFrequencyTimerSemaphore, &ulHigherPriorityTaskWoken );\r
269                 \r
270                 /* If the semaphore was given ulExeuctionCounter will now be pdTRUE. */\r
271                 configASSERT( ulExecutionCounter == pdTRUE );\r
272                 \r
273                 /* Start counting again. */\r
274                 ulExecutionCounter = 0UL;\r
275         }\r
276         \r
277         /* Context switch on exit if necessary. */\r
278         portYIELD_FROM_ISR( ulHigherPriorityTaskWoken );\r
279 }\r