]> git.sur5r.net Git - freertos/blob - Demo/TriCore_TC1782_TriBoard_GCC/RTOSDemo/InterruptNestTest.c
All for the TriCore demo:
[freertos] / Demo / TriCore_TC1782_TriBoard_GCC / RTOSDemo / InterruptNestTest.c
1 /*\r
2     FreeRTOS V7.0.2 - Copyright (C) 2011 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     http://www.FreeRTOS.org - Documentation, latest information, license and\r
45     contact details.\r
46 \r
47     http://www.SafeRTOS.com - A version that is certified for use in safety\r
48     critical systems.\r
49 \r
50     http://www.OpenRTOS.com - Commercial support, development, porting,\r
51     licensing and training services.\r
52 */\r
53 \r
54 /*\r
55  * It is intended that the tasks and timers in this file cause interrupts to\r
56  * nest at least one level deeper than they would otherwise.\r
57  *\r
58  * A timer is configured to create an interrupt at moderately high frequency,\r
59  * as defined by the tmrtestHIGH_FREQUENCY_TIMER_TEST_HZ constant.  The\r
60  * interrupt priority is by configHIGH_FREQUENCY_TIMER_PRIORITY, which is set\r
61  * to ( configMAX_SYSCALL_INTERRUPT_PRIORITY - 1UL ), the second highest\r
62  * priority from which interrupt safe FreeRTOS API calls can be made.\r
63  *\r
64  * The timer interrupt handler counts the number of times it is called.  When\r
65  * it has determined that the number of calls means that 10ms has passed, it\r
66  * 'gives' a semaphore, and resets it call count.\r
67  *\r
68  * A high priority task is used to receive the data posted to the queue by the\r
69  * interrupt service routine.  The task should, then, leave the blocked state\r
70  * and 'take' the available semaphore every 10ms.  The frequency at which it\r
71  * actually leaves the blocked state is verified by the demo's check task (see\r
72  * the the documentation for the entire demo on the FreeRTOS.org web site),\r
73  * which then flags an error if the frequency lower than expected.\r
74  */\r
75 \r
76 /* Standard includes. */\r
77 #include <stdlib.h>\r
78 #include <string.h>\r
79 \r
80 /* Scheduler includes. */\r
81 #include "FreeRTOS.h"\r
82 #include "task.h"\r
83 #include "semphr.h"\r
84 \r
85 /* Demo application includes. */\r
86 #include "InterruptNestTest.h"\r
87 \r
88 /* TriCore specific includes. */\r
89 #include <tc1782.h>\r
90 #include <machine/intrinsics.h>\r
91 #include <machine/cint.h>\r
92 #include <machine/wdtcon.h>\r
93 \r
94 /* This constant is specific to this test application.  It allows the high\r
95 frequency (interrupt nesting test) timer to know how often to trigger, and the\r
96 check task to know how many iterations to expect at any given time. */\r
97 #define tmrtestHIGH_FREQUENCY_TIMER_TEST_HZ             ( 8931UL )\r
98 \r
99 /*\r
100  * The handler for the high priority timer interrupt.\r
101  */\r
102 static void prvPortHighFrequencyTimerHandler( int iArg ) __attribute__((longcall));\r
103 \r
104 /*\r
105  * The task that receives messages posted to a queue by the higher priority\r
106  * timer interrupt.\r
107  */\r
108 static void prvHighFrequencyTimerTask( void *pvParameters );\r
109 \r
110 /* Constants used to configure the timer and determine how often the task\r
111 should receive data. */\r
112 static const unsigned long ulCompareMatchValue = configPERIPHERAL_CLOCK_HZ / tmrtestHIGH_FREQUENCY_TIMER_TEST_HZ;\r
113 static const unsigned long ulInterruptsPer10ms = tmrtestHIGH_FREQUENCY_TIMER_TEST_HZ / 100UL;\r
114 static const unsigned long ulSemaphoreGiveRate_ms = 10UL;\r
115 \r
116 /* The semaphore used to synchronise the interrupt with the task. */\r
117 static xSemaphoreHandle xHighFrequencyTimerSemaphore = NULL;\r
118 \r
119 /* Holds the count of the number of times the task is unblocked by the timer. */\r
120 static volatile unsigned long ulHighFrequencyTaskIterations = 0UL;\r
121 \r
122 /*-----------------------------------------------------------*/\r
123 \r
124 void vSetupInterruptNestingTest( void )\r
125 {\r
126 unsigned long ulCompareMatchBits;\r
127 \r
128         /* Create the semaphore used to communicate between the high frequency\r
129         interrupt and the task. */\r
130         vSemaphoreCreateBinary( xHighFrequencyTimerSemaphore );\r
131         configASSERT( xHighFrequencyTimerSemaphore );\r
132         \r
133         /* Create the task that pends on the semaphore that is given by the\r
134         high frequency interrupt. */\r
135         xTaskCreate( prvHighFrequencyTimerTask, ( signed char * ) "HFTmr", configMINIMAL_STACK_SIZE, NULL, configMAX_PRIORITIES - 1, NULL );\r
136         \r
137         /* Setup the interrupt itself.  The STM module clock divider is setup when \r
138         the tick interrupt is configured - which is when the scheduler is started - \r
139         so there is no need     to do it here.\r
140 \r
141         The tick interrupt uses compare match 0, so this test uses compare match\r
142         1, which means shifting up the values by 16 before writing them to the\r
143         register. */\r
144         ulCompareMatchBits = ( 0x1fUL - __CLZ( ulCompareMatchValue ) );\r
145         ulCompareMatchBits <<= 16UL;\r
146         \r
147         /* Write the values to the relevant SMT registers, without changing other\r
148         bits. */\r
149         taskENTER_CRITICAL();\r
150         {\r
151                 STM_CMCON.reg &= ~( 0x1fUL << 16UL );\r
152                 STM_CMCON.reg |= ulCompareMatchBits;\r
153                 STM_CMP1.reg = ulCompareMatchValue;\r
154 \r
155                 if( 0 != _install_int_handler( configHIGH_FREQUENCY_TIMER_PRIORITY, prvPortHighFrequencyTimerHandler, 0 ) )\r
156                 {\r
157                         /* Set-up the interrupt. */\r
158                         STM_SRC1.reg = ( configHIGH_FREQUENCY_TIMER_PRIORITY | 0x00005000UL );\r
159         \r
160                         /* Enable the Interrupt. */\r
161                         STM_ISRR.reg &= ~( 0x03UL << 2UL );\r
162                         STM_ISRR.reg |= ( 0x1UL << 2UL );\r
163                         STM_ICR.reg &= ~( 0x07UL << 4UL );\r
164                         STM_ICR.reg |= ( 0x5UL << 4UL );\r
165                 }\r
166                 else\r
167                 {\r
168                         /* Failed to install the interrupt. */\r
169                         configASSERT( ( ( volatile void * ) NULL ) );\r
170                 }\r
171         }\r
172         taskEXIT_CRITICAL();\r
173 }\r
174 /*-----------------------------------------------------------*/\r
175 \r
176 unsigned long ulInterruptNestingTestGetIterationCount( unsigned long *pulExpectedIncFrequency_ms )\r
177 {\r
178 unsigned long ulReturn;\r
179 \r
180         *pulExpectedIncFrequency_ms = ulSemaphoreGiveRate_ms;\r
181         portENTER_CRITICAL();\r
182         {\r
183                 ulReturn = ulHighFrequencyTaskIterations;\r
184                 ulHighFrequencyTaskIterations = 0UL;\r
185         }\r
186 \r
187         return ulReturn;\r
188 }\r
189 /*-----------------------------------------------------------*/\r
190 \r
191 static void prvHighFrequencyTimerTask( void *pvParameters )\r
192 {\r
193         /* Just to remove compiler warnings about the unused parameter. */\r
194         ( void ) pvParameters;\r
195 \r
196         for( ;; )\r
197         {\r
198                 /* Wait for the next trigger from the high frequency timer interrupt. */\r
199                 xSemaphoreTake( xHighFrequencyTimerSemaphore, portMAX_DELAY );\r
200                 \r
201                 /* Just count how many times the task has been unblocked before\r
202                 returning to wait for the semaphore again. */\r
203                 ulHighFrequencyTaskIterations++;\r
204         }\r
205 }\r
206 /*-----------------------------------------------------------*/\r
207 \r
208 static void prvPortHighFrequencyTimerHandler( int iArg )\r
209 {\r
210 static volatile unsigned long ulExecutionCounter = 0UL;\r
211 unsigned long ulHigherPriorityTaskWoken = pdFALSE;\r
212 \r
213         /* Just to avoid compiler warnings about unused parameters. */\r
214         ( void ) iArg;\r
215 \r
216         /* Clear the interrupt source. */\r
217         STM_ISRR.reg = 1UL << 2UL;\r
218 \r
219         /* Reload the Compare Match register for X ticks into the future.*/\r
220         STM_CMP1.reg += ulCompareMatchValue;\r
221 \r
222         ulExecutionCounter++;\r
223         \r
224         if( ulExecutionCounter >= ulInterruptsPer10ms )\r
225         {\r
226                 ulExecutionCounter = xSemaphoreGiveFromISR( xHighFrequencyTimerSemaphore, &ulHigherPriorityTaskWoken );\r
227                 \r
228                 /* If the semaphore was given ulExeuctionCounter will now be pdTRUE. */\r
229                 configASSERT( ulExecutionCounter == pdTRUE );\r
230                 \r
231                 /* Start counting again. */\r
232                 ulExecutionCounter = 0UL;\r
233         }\r
234         \r
235         /* Context switch on exit if necessary. */\r
236         portYIELD_FROM_ISR( ulHigherPriorityTaskWoken );\r
237 }\r