]> git.sur5r.net Git - freertos/blob - Demo/TriCore_TC1782_TriBoard_GCC/RTOSDemo/InterruptNestTest.c
Wind up the baud rate on the TriCore UART to obtain an extra level of interrupt nesting.
[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 /* Standard includes. */\r
55 #include <stdlib.h>\r
56 #include <string.h>\r
57 \r
58 /* Scheduler includes. */\r
59 #include "FreeRTOS.h"\r
60 #include "task.h"\r
61 #include "semphr.h"\r
62 \r
63 /* Demo application includes. */\r
64 #include "InterruptNestTest.h"\r
65 \r
66 /* TriCore specific includes. */\r
67 #include <tc1782.h>\r
68 #include <machine/intrinsics.h>\r
69 #include <machine/cint.h>\r
70 #include <machine/wdtcon.h>\r
71 \r
72 #warning DOCUMENT THIS\r
73 /* This constant is specific to this test application.  It allows the high\r
74 frequency (interrupt nesting test) timer to know how often to trigger, and the\r
75 check task to know how many iterations to expect at any given time. */\r
76 #define tmrtestHIGH_FREQUENCY_TIMER_TEST_HZ             ( 8931UL )\r
77 \r
78 static void prvPortHighFrequencyTimerHandler( int iArg ) __attribute__((longcall));\r
79 static void prvHighFrequencyTimerTask( void *pvParameters );\r
80 \r
81 static const unsigned long ulCompareMatchValue = configPERIPHERAL_CLOCK_HZ / tmrtestHIGH_FREQUENCY_TIMER_TEST_HZ;\r
82 static const unsigned long ulInterruptsPer10ms = tmrtestHIGH_FREQUENCY_TIMER_TEST_HZ / 100UL;\r
83 static const unsigned long ulSemaphoreGiveRate_ms = 10UL;\r
84 \r
85 static xSemaphoreHandle xHighFrequencyTimerSemaphore = NULL;\r
86 static unsigned long ulHighFrequencyCounterIterations = 0UL;\r
87 \r
88 /*-----------------------------------------------------------*/\r
89 \r
90 void vSetupInterruptNestingTest( void )\r
91 {\r
92 unsigned long ulCompareMatchBits;\r
93 \r
94         /* Create the semaphore used to communicate between the high frequency\r
95         interrupt and the task. */\r
96         vSemaphoreCreateBinary( xHighFrequencyTimerSemaphore );\r
97         configASSERT( xHighFrequencyTimerSemaphore );\r
98         \r
99         /* Create the task that pends on the semaphore that is given by the\r
100         high frequency interrupt. */\r
101         xTaskCreate( prvHighFrequencyTimerTask, ( signed char * ) "HFTmr", configMINIMAL_STACK_SIZE, NULL, configMAX_PRIORITIES - 1, NULL );\r
102         \r
103         /* Setup the interrupt itself.  The STM module clock divider is setup when \r
104         the tick interrupt is configured - which is when the scheduler is started - \r
105         so there is no need     to do it here.\r
106 \r
107         The tick interrupt uses compare match 0, so this test uses compare match\r
108         1, which means shifting up the values by 16 before writing them to the\r
109         register. */\r
110         ulCompareMatchBits = ( 0x1fUL - __CLZ( ulCompareMatchValue ) );\r
111         ulCompareMatchBits <<= 16UL;\r
112         \r
113         /* Write the values to the relevant SMT registers, without changing other\r
114         bits. */\r
115         taskENTER_CRITICAL();\r
116         {\r
117                 STM_CMCON.reg &= ~( 0x1fUL << 16UL );\r
118                 STM_CMCON.reg |= ulCompareMatchBits;\r
119                 STM_CMP1.reg = ulCompareMatchValue;\r
120 \r
121                 if( 0 != _install_int_handler( configHIGH_FREQUENCY_TIMER_PRIORITY, prvPortHighFrequencyTimerHandler, 0 ) )\r
122                 {\r
123                         /* Set-up the interrupt. */\r
124                         STM_SRC1.reg = ( configHIGH_FREQUENCY_TIMER_PRIORITY | 0x00005000UL );\r
125         \r
126                         /* Enable the Interrupt. */\r
127                         STM_ISRR.reg &= ~( 0x03UL << 2UL );\r
128                         STM_ISRR.reg |= ( 0x1UL << 2UL );\r
129                         STM_ICR.reg &= ~( 0x07UL << 4UL );\r
130                         STM_ICR.reg |= ( 0x5UL << 4UL );\r
131                 }\r
132                 else\r
133                 {\r
134                         /* Failed to install the interrupt. */\r
135                         configASSERT( ( ( volatile void * ) NULL ) );\r
136                 }\r
137         }\r
138         taskEXIT_CRITICAL();\r
139 }\r
140 /*-----------------------------------------------------------*/\r
141 \r
142 unsigned long ulInterruptNestingTestGetIterationCount( unsigned long *pulExpectedIncFrequency_ms )\r
143 {\r
144         *pulExpectedIncFrequency_ms = ulSemaphoreGiveRate_ms;\r
145         return ulHighFrequencyCounterIterations;\r
146 }\r
147 /*-----------------------------------------------------------*/\r
148 \r
149 static void prvHighFrequencyTimerTask( void *pvParameters )\r
150 {\r
151         /* Just to remove compiler warnings about the unused parameter. */\r
152         ( void ) pvParameters;\r
153 \r
154         for( ;; )\r
155         {\r
156                 /* Wait for the next trigger from the high frequency timer interrupt. */\r
157                 xSemaphoreTake( xHighFrequencyTimerSemaphore, portMAX_DELAY );\r
158                 \r
159                 /* Just count how many times the task has been unblocked before\r
160                 returning to wait for the semaphore again. */\r
161                 ulHighFrequencyCounterIterations++;\r
162         }\r
163 }\r
164 /*-----------------------------------------------------------*/\r
165 \r
166 static void prvPortHighFrequencyTimerHandler( int iArg )\r
167 {\r
168 static volatile unsigned long ulExecutionCounter = 0UL;\r
169 unsigned long ulHigherPriorityTaskWoken = pdFALSE;\r
170 \r
171         /* Just to avoid compiler warnings about unused parameters. */\r
172         ( void ) iArg;\r
173 \r
174         /* Clear the interrupt source. */\r
175         STM_ISRR.reg = 1UL << 2UL;\r
176 \r
177         /* Reload the Compare Match register for X ticks into the future.*/\r
178         STM_CMP1.reg += ulCompareMatchValue;\r
179 \r
180         ulExecutionCounter++;\r
181         \r
182         if( ulExecutionCounter >= ulInterruptsPer10ms )\r
183         {\r
184                 ulExecutionCounter = xSemaphoreGiveFromISR( xHighFrequencyTimerSemaphore, &ulHigherPriorityTaskWoken );\r
185                 \r
186                 /* If the semaphore was given ulExeuctionCounter will now be pdTRUE. */\r
187                 configASSERT( ulExecutionCounter == pdTRUE );\r
188                 \r
189                 /* Start counting again. */\r
190                 ulExecutionCounter = 0UL;\r
191         }\r
192         \r
193         portYIELD_FROM_ISR( ulHigherPriorityTaskWoken );\r
194 }\r