]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/TriCore_TC1782_TriBoard_GCC/RTOSDemo/InterruptNestTest.c
Minor updates and change version number for V7.5.0 release.
[freertos] / FreeRTOS / Demo / TriCore_TC1782_TriBoard_GCC / RTOSDemo / InterruptNestTest.c
1 /*\r
2     FreeRTOS V7.5.0 - Copyright (C) 2013 Real Time Engineers Ltd.\r
3 \r
4     VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.\r
5 \r
6     ***************************************************************************\r
7      *                                                                       *\r
8      *    FreeRTOS provides completely free yet professionally developed,    *\r
9      *    robust, strictly quality controlled, supported, and cross          *\r
10      *    platform software that has become a de facto standard.             *\r
11      *                                                                       *\r
12      *    Help yourself get started quickly and support the FreeRTOS         *\r
13      *    project by purchasing a FreeRTOS tutorial book, reference          *\r
14      *    manual, or both from: http://www.FreeRTOS.org/Documentation        *\r
15      *                                                                       *\r
16      *    Thank you!                                                         *\r
17      *                                                                       *\r
18     ***************************************************************************\r
19 \r
20     This file is part of the FreeRTOS distribution.\r
21 \r
22     FreeRTOS is free software; you can redistribute it and/or modify it under\r
23     the terms of the GNU General Public License (version 2) as published by the\r
24     Free Software Foundation >>!AND MODIFIED BY!<< the FreeRTOS exception.\r
25 \r
26     >>! NOTE: The modification to the GPL is included to allow you to distribute\r
27     >>! a combined work that includes FreeRTOS without being obliged to provide\r
28     >>! the source code for proprietary components outside of the FreeRTOS\r
29     >>! kernel.\r
30 \r
31     FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY\r
32     WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS\r
33     FOR A PARTICULAR PURPOSE.  Full license text is available from the following\r
34     link: http://www.freertos.org/a00114.html\r
35 \r
36     1 tab == 4 spaces!\r
37 \r
38     ***************************************************************************\r
39      *                                                                       *\r
40      *    Having a problem?  Start by reading the FAQ "My application does   *\r
41      *    not run, what could be wrong?"                                     *\r
42      *                                                                       *\r
43      *    http://www.FreeRTOS.org/FAQHelp.html                               *\r
44      *                                                                       *\r
45     ***************************************************************************\r
46 \r
47     http://www.FreeRTOS.org - Documentation, books, training, latest versions,\r
48     license and Real Time Engineers Ltd. contact details.\r
49 \r
50     http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,\r
51     including FreeRTOS+Trace - an indispensable productivity tool, a DOS\r
52     compatible FAT file system, and our tiny thread aware UDP/IP stack.\r
53 \r
54     http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High\r
55     Integrity Systems to sell under the OpenRTOS brand.  Low cost OpenRTOS\r
56     licenses offer ticketed support, indemnification and middleware.\r
57 \r
58     http://www.SafeRTOS.com - High Integrity Systems also provide a safety\r
59     engineered and independently SIL3 certified version for use in safety and\r
60     mission critical applications that require provable dependability.\r
61 \r
62     1 tab == 4 spaces!\r
63 */\r
64 \r
65 /*\r
66  * It is intended that the tasks and timers in this file cause interrupts to\r
67  * nest at least one level deeper than they would otherwise.\r
68  *\r
69  * A timer is configured to create an interrupt at moderately high frequency,\r
70  * as defined by the tmrtestHIGH_FREQUENCY_TIMER_TEST_HZ constant.  The\r
71  * interrupt priority is by configHIGH_FREQUENCY_TIMER_PRIORITY, which is set\r
72  * to ( configMAX_SYSCALL_INTERRUPT_PRIORITY - 1UL ), the second highest\r
73  * priority from which interrupt safe FreeRTOS API calls can be made.\r
74  *\r
75  * The timer interrupt handler counts the number of times it is called.  When\r
76  * it has determined that the number of calls means that 10ms has passed, it\r
77  * 'gives' a semaphore, and resets it call count.\r
78  *\r
79  * A high priority task is used to receive the data posted to the queue by the\r
80  * interrupt service routine.  The task should, then, leave the blocked state\r
81  * and 'take' the available semaphore every 10ms.  The frequency at which it\r
82  * actually leaves the blocked state is verified by the demo's check task (see\r
83  * the the documentation for the entire demo on the FreeRTOS.org web site),\r
84  * which then flags an error if the frequency lower than expected.\r
85  */\r
86 \r
87 /* Standard includes. */\r
88 #include <stdlib.h>\r
89 #include <string.h>\r
90 \r
91 /* Scheduler includes. */\r
92 #include "FreeRTOS.h"\r
93 #include "task.h"\r
94 #include "semphr.h"\r
95 \r
96 /* Demo application includes. */\r
97 #include "InterruptNestTest.h"\r
98 \r
99 /* TriCore specific includes. */\r
100 #include <tc1782.h>\r
101 #include <machine/intrinsics.h>\r
102 #include <machine/cint.h>\r
103 #include <machine/wdtcon.h>\r
104 \r
105 /* This constant is specific to this test application.  It allows the high\r
106 frequency (interrupt nesting test) timer to know how often to trigger, and the\r
107 check task to know how many iterations to expect at any given time. */\r
108 #define tmrtestHIGH_FREQUENCY_TIMER_TEST_HZ             ( 8931UL )\r
109 \r
110 /*\r
111  * The handler for the high priority timer interrupt.\r
112  */\r
113 static void prvPortHighFrequencyTimerHandler( int iArg ) __attribute__((longcall));\r
114 \r
115 /*\r
116  * The task that receives messages posted to a queue by the higher priority\r
117  * timer interrupt.\r
118  */\r
119 static void prvHighFrequencyTimerTask( void *pvParameters );\r
120 \r
121 /* Constants used to configure the timer and determine how often the task\r
122 should receive data. */\r
123 static const unsigned long ulCompareMatchValue = configPERIPHERAL_CLOCK_HZ / tmrtestHIGH_FREQUENCY_TIMER_TEST_HZ;\r
124 static const unsigned long ulInterruptsPer10ms = tmrtestHIGH_FREQUENCY_TIMER_TEST_HZ / 100UL;\r
125 static const unsigned long ulSemaphoreGiveRate_ms = 10UL;\r
126 \r
127 /* The semaphore used to synchronise the interrupt with the task. */\r
128 static xSemaphoreHandle xHighFrequencyTimerSemaphore = NULL;\r
129 \r
130 /* Holds the count of the number of times the task is unblocked by the timer. */\r
131 static volatile unsigned long ulHighFrequencyTaskIterations = 0UL;\r
132 \r
133 /*-----------------------------------------------------------*/\r
134 \r
135 void vSetupInterruptNestingTest( void )\r
136 {\r
137 unsigned long ulCompareMatchBits;\r
138 \r
139         /* Create the semaphore used to communicate between the high frequency\r
140         interrupt and the task. */\r
141         vSemaphoreCreateBinary( xHighFrequencyTimerSemaphore );\r
142         configASSERT( xHighFrequencyTimerSemaphore );\r
143         \r
144         /* Create the task that pends on the semaphore that is given by the\r
145         high frequency interrupt. */\r
146         xTaskCreate( prvHighFrequencyTimerTask, ( signed char * ) "HFTmr", configMINIMAL_STACK_SIZE, NULL, configMAX_PRIORITIES - 1, NULL );\r
147         \r
148         /* Setup the interrupt itself.  The STM module clock divider is setup when \r
149         the tick interrupt is configured - which is when the scheduler is started - \r
150         so there is no need     to do it here.\r
151 \r
152         The tick interrupt uses compare match 0, so this test uses compare match\r
153         1, which means shifting up the values by 16 before writing them to the\r
154         register. */\r
155         ulCompareMatchBits = ( 0x1fUL - __CLZ( ulCompareMatchValue ) );\r
156         ulCompareMatchBits <<= 16UL;\r
157         \r
158         /* Write the values to the relevant SMT registers, without changing other\r
159         bits. */\r
160         taskENTER_CRITICAL();\r
161         {\r
162                 STM_CMCON.reg &= ~( 0x1fUL << 16UL );\r
163                 STM_CMCON.reg |= ulCompareMatchBits;\r
164                 STM_CMP1.reg = ulCompareMatchValue;\r
165 \r
166                 if( 0 != _install_int_handler( configHIGH_FREQUENCY_TIMER_PRIORITY, prvPortHighFrequencyTimerHandler, 0 ) )\r
167                 {\r
168                         /* Set-up the interrupt. */\r
169                         STM_SRC1.reg = ( configHIGH_FREQUENCY_TIMER_PRIORITY | 0x00005000UL );\r
170         \r
171                         /* Enable the Interrupt. */\r
172                         STM_ISRR.reg &= ~( 0x03UL << 2UL );\r
173                         STM_ISRR.reg |= ( 0x1UL << 2UL );\r
174                         STM_ICR.reg &= ~( 0x07UL << 4UL );\r
175                         STM_ICR.reg |= ( 0x5UL << 4UL );\r
176                 }\r
177                 else\r
178                 {\r
179                         /* Failed to install the interrupt. */\r
180                         configASSERT( ( ( volatile void * ) NULL ) );\r
181                 }\r
182         }\r
183         taskEXIT_CRITICAL();\r
184 }\r
185 /*-----------------------------------------------------------*/\r
186 \r
187 unsigned long ulInterruptNestingTestGetIterationCount( unsigned long *pulExpectedIncFrequency_ms )\r
188 {\r
189 unsigned long ulReturn;\r
190 \r
191         *pulExpectedIncFrequency_ms = ulSemaphoreGiveRate_ms;\r
192         portENTER_CRITICAL();\r
193         {\r
194                 ulReturn = ulHighFrequencyTaskIterations;\r
195                 ulHighFrequencyTaskIterations = 0UL;\r
196         }\r
197 \r
198         return ulReturn;\r
199 }\r
200 /*-----------------------------------------------------------*/\r
201 \r
202 static void prvHighFrequencyTimerTask( void *pvParameters )\r
203 {\r
204         /* Just to remove compiler warnings about the unused parameter. */\r
205         ( void ) pvParameters;\r
206 \r
207         for( ;; )\r
208         {\r
209                 /* Wait for the next trigger from the high frequency timer interrupt. */\r
210                 xSemaphoreTake( xHighFrequencyTimerSemaphore, portMAX_DELAY );\r
211                 \r
212                 /* Just count how many times the task has been unblocked before\r
213                 returning to wait for the semaphore again. */\r
214                 ulHighFrequencyTaskIterations++;\r
215         }\r
216 }\r
217 /*-----------------------------------------------------------*/\r
218 \r
219 static void prvPortHighFrequencyTimerHandler( int iArg )\r
220 {\r
221 static volatile unsigned long ulExecutionCounter = 0UL;\r
222 unsigned long ulHigherPriorityTaskWoken = pdFALSE;\r
223 \r
224         /* Just to avoid compiler warnings about unused parameters. */\r
225         ( void ) iArg;\r
226 \r
227         /* Clear the interrupt source. */\r
228         STM_ISRR.reg = 1UL << 2UL;\r
229 \r
230         /* Reload the Compare Match register for X ticks into the future.*/\r
231         STM_CMP1.reg += ulCompareMatchValue;\r
232 \r
233         ulExecutionCounter++;\r
234         \r
235         if( ulExecutionCounter >= ulInterruptsPer10ms )\r
236         {\r
237                 ulExecutionCounter = xSemaphoreGiveFromISR( xHighFrequencyTimerSemaphore, &ulHigherPriorityTaskWoken );\r
238                 \r
239                 /* If the semaphore was given ulExeuctionCounter will now be pdTRUE. */\r
240                 configASSERT( ulExecutionCounter == pdTRUE );\r
241                 \r
242                 /* Start counting again. */\r
243                 ulExecutionCounter = 0UL;\r
244         }\r
245         \r
246         /* Context switch on exit if necessary. */\r
247         portYIELD_FROM_ISR( ulHigherPriorityTaskWoken );\r
248 }\r