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