2 FreeRTOS V7.0.2 - Copyright (C) 2011 Real Time Engineers Ltd.
\r
5 ***************************************************************************
\r
7 * FreeRTOS tutorial books are available in pdf and paperback. *
\r
8 * Complete, revised, and edited pdf reference manuals are also *
\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
18 * >>> See http://www.FreeRTOS.org/Documentation for details. <<< *
\r
20 * Thank you for using FreeRTOS, and thank you for your support! *
\r
22 ***************************************************************************
\r
25 This file is part of the FreeRTOS distribution.
\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
44 http://www.FreeRTOS.org - Documentation, latest information, license and
\r
47 http://www.SafeRTOS.com - A version that is certified for use in safety
\r
50 http://www.OpenRTOS.com - Commercial support, development, porting,
\r
51 licensing and training services.
\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
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
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
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
76 /* Standard includes. */
\r
80 /* Scheduler includes. */
\r
81 #include "FreeRTOS.h"
\r
85 /* Demo application includes. */
\r
86 #include "InterruptNestTest.h"
\r
88 /* TriCore specific includes. */
\r
90 #include <machine/intrinsics.h>
\r
91 #include <machine/cint.h>
\r
92 #include <machine/wdtcon.h>
\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
100 * The handler for the high priority timer interrupt.
\r
102 static void prvPortHighFrequencyTimerHandler( int iArg ) __attribute__((longcall));
\r
105 * The task that receives messages posted to a queue by the higher priority
\r
108 static void prvHighFrequencyTimerTask( void *pvParameters );
\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
116 /* The semaphore used to synchronise the interrupt with the task. */
\r
117 static xSemaphoreHandle xHighFrequencyTimerSemaphore = NULL;
\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
122 /*-----------------------------------------------------------*/
\r
124 void vSetupInterruptNestingTest( void )
\r
126 unsigned long ulCompareMatchBits;
\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
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
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
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
144 ulCompareMatchBits = ( 0x1fUL - __CLZ( ulCompareMatchValue ) );
\r
145 ulCompareMatchBits <<= 16UL;
\r
147 /* Write the values to the relevant SMT registers, without changing other
\r
149 taskENTER_CRITICAL();
\r
151 STM_CMCON.reg &= ~( 0x1fUL << 16UL );
\r
152 STM_CMCON.reg |= ulCompareMatchBits;
\r
153 STM_CMP1.reg = ulCompareMatchValue;
\r
155 if( 0 != _install_int_handler( configHIGH_FREQUENCY_TIMER_PRIORITY, prvPortHighFrequencyTimerHandler, 0 ) )
\r
157 /* Set-up the interrupt. */
\r
158 STM_SRC1.reg = ( configHIGH_FREQUENCY_TIMER_PRIORITY | 0x00005000UL );
\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
168 /* Failed to install the interrupt. */
\r
169 configASSERT( ( ( volatile void * ) NULL ) );
\r
172 taskEXIT_CRITICAL();
\r
174 /*-----------------------------------------------------------*/
\r
176 unsigned long ulInterruptNestingTestGetIterationCount( unsigned long *pulExpectedIncFrequency_ms )
\r
178 unsigned long ulReturn;
\r
180 *pulExpectedIncFrequency_ms = ulSemaphoreGiveRate_ms;
\r
181 portENTER_CRITICAL();
\r
183 ulReturn = ulHighFrequencyTaskIterations;
\r
184 ulHighFrequencyTaskIterations = 0UL;
\r
189 /*-----------------------------------------------------------*/
\r
191 static void prvHighFrequencyTimerTask( void *pvParameters )
\r
193 /* Just to remove compiler warnings about the unused parameter. */
\r
194 ( void ) pvParameters;
\r
198 /* Wait for the next trigger from the high frequency timer interrupt. */
\r
199 xSemaphoreTake( xHighFrequencyTimerSemaphore, portMAX_DELAY );
\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
206 /*-----------------------------------------------------------*/
\r
208 static void prvPortHighFrequencyTimerHandler( int iArg )
\r
210 static volatile unsigned long ulExecutionCounter = 0UL;
\r
211 unsigned long ulHigherPriorityTaskWoken = pdFALSE;
\r
213 /* Just to avoid compiler warnings about unused parameters. */
\r
216 /* Clear the interrupt source. */
\r
217 STM_ISRR.reg = 1UL << 2UL;
\r
219 /* Reload the Compare Match register for X ticks into the future.*/
\r
220 STM_CMP1.reg += ulCompareMatchValue;
\r
222 ulExecutionCounter++;
\r
224 if( ulExecutionCounter >= ulInterruptsPer10ms )
\r
226 ulExecutionCounter = xSemaphoreGiveFromISR( xHighFrequencyTimerSemaphore, &ulHigherPriorityTaskWoken );
\r
228 /* If the semaphore was given ulExeuctionCounter will now be pdTRUE. */
\r
229 configASSERT( ulExecutionCounter == pdTRUE );
\r
231 /* Start counting again. */
\r
232 ulExecutionCounter = 0UL;
\r
235 /* Context switch on exit if necessary. */
\r
236 portYIELD_FROM_ISR( ulHigherPriorityTaskWoken );
\r