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