]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/TriCore_TC1782_TriBoard_GCC/RTOSDemo/InterruptNestTest.c
Update to MIT licensed FreeRTOS V10.0.0 - see https://www.freertos.org/History.txt
[freertos] / FreeRTOS / Demo / TriCore_TC1782_TriBoard_GCC / RTOSDemo / InterruptNestTest.c
1 /*\r
2  * FreeRTOS Kernel V10.0.0\r
3  * Copyright (C) 2017 Amazon.com, Inc. or its affiliates.  All Rights Reserved.\r
4  *\r
5  * Permission is hereby granted, free of charge, to any person obtaining a copy of\r
6  * this software and associated documentation files (the "Software"), to deal in\r
7  * the Software without restriction, including without limitation the rights to\r
8  * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of\r
9  * the Software, and to permit persons to whom the Software is furnished to do so,\r
10  * subject to the following conditions:\r
11  *\r
12  * The above copyright notice and this permission notice shall be included in all\r
13  * copies or substantial portions of the Software. If you wish to use our Amazon\r
14  * FreeRTOS name, please do so in a fair use way that does not cause confusion.\r
15  *\r
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\r
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS\r
18  * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR\r
19  * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER\r
20  * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN\r
21  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\r
22  *\r
23  * http://www.FreeRTOS.org\r
24  * http://aws.amazon.com/freertos\r
25  *\r
26  * 1 tab == 4 spaces!\r
27  */\r
28 \r
29 /*\r
30  * It is intended that the tasks and timers in this file cause interrupts to\r
31  * nest at least one level deeper than they would otherwise.\r
32  *\r
33  * A timer is configured to create an interrupt at moderately high frequency,\r
34  * as defined by the tmrtestHIGH_FREQUENCY_TIMER_TEST_HZ constant.  The\r
35  * interrupt priority is by configHIGH_FREQUENCY_TIMER_PRIORITY, which is set\r
36  * to ( configMAX_SYSCALL_INTERRUPT_PRIORITY - 1UL ), the second highest\r
37  * priority from which interrupt safe FreeRTOS API calls can be made.\r
38  *\r
39  * The timer interrupt handler counts the number of times it is called.  When\r
40  * it has determined that the number of calls means that 10ms has passed, it\r
41  * 'gives' a semaphore, and resets it call count.\r
42  *\r
43  * A high priority task is used to receive the data posted to the queue by the\r
44  * interrupt service routine.  The task should, then, leave the blocked state\r
45  * and 'take' the available semaphore every 10ms.  The frequency at which it\r
46  * actually leaves the blocked state is verified by the demo's check task (see\r
47  * the the documentation for the entire demo on the FreeRTOS.org web site),\r
48  * which then flags an error if the frequency lower than expected.\r
49  */\r
50 \r
51 /* Standard includes. */\r
52 #include <stdlib.h>\r
53 #include <string.h>\r
54 \r
55 /* Scheduler includes. */\r
56 #include "FreeRTOS.h"\r
57 #include "task.h"\r
58 #include "semphr.h"\r
59 \r
60 /* Demo application includes. */\r
61 #include "InterruptNestTest.h"\r
62 \r
63 /* TriCore specific includes. */\r
64 #include <tc1782.h>\r
65 #include <machine/intrinsics.h>\r
66 #include <machine/cint.h>\r
67 #include <machine/wdtcon.h>\r
68 \r
69 /* This constant is specific to this test application.  It allows the high\r
70 frequency (interrupt nesting test) timer to know how often to trigger, and the\r
71 check task to know how many iterations to expect at any given time. */\r
72 #define tmrtestHIGH_FREQUENCY_TIMER_TEST_HZ             ( 8931UL )\r
73 \r
74 /*\r
75  * The handler for the high priority timer interrupt.\r
76  */\r
77 static void prvPortHighFrequencyTimerHandler( int iArg ) __attribute__((longcall));\r
78 \r
79 /*\r
80  * The task that receives messages posted to a queue by the higher priority\r
81  * timer interrupt.\r
82  */\r
83 static void prvHighFrequencyTimerTask( void *pvParameters );\r
84 \r
85 /* Constants used to configure the timer and determine how often the task\r
86 should receive data. */\r
87 static const unsigned long ulCompareMatchValue = configPERIPHERAL_CLOCK_HZ / tmrtestHIGH_FREQUENCY_TIMER_TEST_HZ;\r
88 static const unsigned long ulInterruptsPer10ms = tmrtestHIGH_FREQUENCY_TIMER_TEST_HZ / 100UL;\r
89 static const unsigned long ulSemaphoreGiveRate_ms = 10UL;\r
90 \r
91 /* The semaphore used to synchronise the interrupt with the task. */\r
92 static SemaphoreHandle_t xHighFrequencyTimerSemaphore = NULL;\r
93 \r
94 /* Holds the count of the number of times the task is unblocked by the timer. */\r
95 static volatile unsigned long ulHighFrequencyTaskIterations = 0UL;\r
96 \r
97 /*-----------------------------------------------------------*/\r
98 \r
99 void vSetupInterruptNestingTest( void )\r
100 {\r
101 unsigned long ulCompareMatchBits;\r
102 \r
103         /* Create the semaphore used to communicate between the high frequency\r
104         interrupt and the task. */\r
105         vSemaphoreCreateBinary( xHighFrequencyTimerSemaphore );\r
106         configASSERT( xHighFrequencyTimerSemaphore );\r
107         \r
108         /* Create the task that pends on the semaphore that is given by the\r
109         high frequency interrupt. */\r
110         xTaskCreate( prvHighFrequencyTimerTask, "HFTmr", configMINIMAL_STACK_SIZE, NULL, configMAX_PRIORITIES - 1, NULL );\r
111         \r
112         /* Setup the interrupt itself.  The STM module clock divider is setup when \r
113         the tick interrupt is configured - which is when the scheduler is started - \r
114         so there is no need     to do it here.\r
115 \r
116         The tick interrupt uses compare match 0, so this test uses compare match\r
117         1, which means shifting up the values by 16 before writing them to the\r
118         register. */\r
119         ulCompareMatchBits = ( 0x1fUL - __CLZ( ulCompareMatchValue ) );\r
120         ulCompareMatchBits <<= 16UL;\r
121         \r
122         /* Write the values to the relevant SMT registers, without changing other\r
123         bits. */\r
124         taskENTER_CRITICAL();\r
125         {\r
126                 STM_CMCON.reg &= ~( 0x1fUL << 16UL );\r
127                 STM_CMCON.reg |= ulCompareMatchBits;\r
128                 STM_CMP1.reg = ulCompareMatchValue;\r
129 \r
130                 if( 0 != _install_int_handler( configHIGH_FREQUENCY_TIMER_PRIORITY, prvPortHighFrequencyTimerHandler, 0 ) )\r
131                 {\r
132                         /* Set-up the interrupt. */\r
133                         STM_SRC1.reg = ( configHIGH_FREQUENCY_TIMER_PRIORITY | 0x00005000UL );\r
134         \r
135                         /* Enable the Interrupt. */\r
136                         STM_ISRR.reg &= ~( 0x03UL << 2UL );\r
137                         STM_ISRR.reg |= ( 0x1UL << 2UL );\r
138                         STM_ICR.reg &= ~( 0x07UL << 4UL );\r
139                         STM_ICR.reg |= ( 0x5UL << 4UL );\r
140                 }\r
141                 else\r
142                 {\r
143                         /* Failed to install the interrupt. */\r
144                         configASSERT( ( ( volatile void * ) NULL ) );\r
145                 }\r
146         }\r
147         taskEXIT_CRITICAL();\r
148 }\r
149 /*-----------------------------------------------------------*/\r
150 \r
151 unsigned long ulInterruptNestingTestGetIterationCount( unsigned long *pulExpectedIncFrequency_ms )\r
152 {\r
153 unsigned long ulReturn;\r
154 \r
155         *pulExpectedIncFrequency_ms = ulSemaphoreGiveRate_ms;\r
156         portENTER_CRITICAL();\r
157         {\r
158                 ulReturn = ulHighFrequencyTaskIterations;\r
159                 ulHighFrequencyTaskIterations = 0UL;\r
160         }\r
161 \r
162         return ulReturn;\r
163 }\r
164 /*-----------------------------------------------------------*/\r
165 \r
166 static void prvHighFrequencyTimerTask( void *pvParameters )\r
167 {\r
168         /* Just to remove compiler warnings about the unused parameter. */\r
169         ( void ) pvParameters;\r
170 \r
171         for( ;; )\r
172         {\r
173                 /* Wait for the next trigger from the high frequency timer interrupt. */\r
174                 xSemaphoreTake( xHighFrequencyTimerSemaphore, portMAX_DELAY );\r
175                 \r
176                 /* Just count how many times the task has been unblocked before\r
177                 returning to wait for the semaphore again. */\r
178                 ulHighFrequencyTaskIterations++;\r
179         }\r
180 }\r
181 /*-----------------------------------------------------------*/\r
182 \r
183 static void prvPortHighFrequencyTimerHandler( int iArg )\r
184 {\r
185 static volatile unsigned long ulExecutionCounter = 0UL;\r
186 unsigned long ulHigherPriorityTaskWoken = pdFALSE;\r
187 \r
188         /* Just to avoid compiler warnings about unused parameters. */\r
189         ( void ) iArg;\r
190 \r
191         /* Clear the interrupt source. */\r
192         STM_ISRR.reg = 1UL << 2UL;\r
193 \r
194         /* Reload the Compare Match register for X ticks into the future.*/\r
195         STM_CMP1.reg += ulCompareMatchValue;\r
196 \r
197         ulExecutionCounter++;\r
198         \r
199         if( ulExecutionCounter >= ulInterruptsPer10ms )\r
200         {\r
201                 ulExecutionCounter = xSemaphoreGiveFromISR( xHighFrequencyTimerSemaphore, &ulHigherPriorityTaskWoken );\r
202                 \r
203                 /* If the semaphore was given ulExeuctionCounter will now be pdTRUE. */\r
204                 configASSERT( ulExecutionCounter == pdTRUE );\r
205                 \r
206                 /* Start counting again. */\r
207                 ulExecutionCounter = 0UL;\r
208         }\r
209         \r
210         /* Context switch on exit if necessary. */\r
211         portYIELD_FROM_ISR( ulHigherPriorityTaskWoken );\r
212 }\r