2 * FreeRTOS Kernel V10.0.0
\r
3 * Copyright (C) 2017 Amazon.com, Inc. or its affiliates. All Rights Reserved.
\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
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
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
23 * http://www.FreeRTOS.org
\r
24 * http://aws.amazon.com/freertos
\r
26 * 1 tab == 4 spaces!
\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
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
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
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
51 /* Standard includes. */
\r
55 /* Scheduler includes. */
\r
56 #include "FreeRTOS.h"
\r
60 /* Demo application includes. */
\r
61 #include "InterruptNestTest.h"
\r
63 /* TriCore specific includes. */
\r
65 #include <machine/intrinsics.h>
\r
66 #include <machine/cint.h>
\r
67 #include <machine/wdtcon.h>
\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
75 * The handler for the high priority timer interrupt.
\r
77 static void prvPortHighFrequencyTimerHandler( int iArg ) __attribute__((longcall));
\r
80 * The task that receives messages posted to a queue by the higher priority
\r
83 static void prvHighFrequencyTimerTask( void *pvParameters );
\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
91 /* The semaphore used to synchronise the interrupt with the task. */
\r
92 static SemaphoreHandle_t xHighFrequencyTimerSemaphore = NULL;
\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
97 /*-----------------------------------------------------------*/
\r
99 void vSetupInterruptNestingTest( void )
\r
101 unsigned long ulCompareMatchBits;
\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
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
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
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
119 ulCompareMatchBits = ( 0x1fUL - __CLZ( ulCompareMatchValue ) );
\r
120 ulCompareMatchBits <<= 16UL;
\r
122 /* Write the values to the relevant SMT registers, without changing other
\r
124 taskENTER_CRITICAL();
\r
126 STM_CMCON.reg &= ~( 0x1fUL << 16UL );
\r
127 STM_CMCON.reg |= ulCompareMatchBits;
\r
128 STM_CMP1.reg = ulCompareMatchValue;
\r
130 if( 0 != _install_int_handler( configHIGH_FREQUENCY_TIMER_PRIORITY, prvPortHighFrequencyTimerHandler, 0 ) )
\r
132 /* Set-up the interrupt. */
\r
133 STM_SRC1.reg = ( configHIGH_FREQUENCY_TIMER_PRIORITY | 0x00005000UL );
\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
143 /* Failed to install the interrupt. */
\r
144 configASSERT( ( ( volatile void * ) NULL ) );
\r
147 taskEXIT_CRITICAL();
\r
149 /*-----------------------------------------------------------*/
\r
151 unsigned long ulInterruptNestingTestGetIterationCount( unsigned long *pulExpectedIncFrequency_ms )
\r
153 unsigned long ulReturn;
\r
155 *pulExpectedIncFrequency_ms = ulSemaphoreGiveRate_ms;
\r
156 portENTER_CRITICAL();
\r
158 ulReturn = ulHighFrequencyTaskIterations;
\r
159 ulHighFrequencyTaskIterations = 0UL;
\r
164 /*-----------------------------------------------------------*/
\r
166 static void prvHighFrequencyTimerTask( void *pvParameters )
\r
168 /* Just to remove compiler warnings about the unused parameter. */
\r
169 ( void ) pvParameters;
\r
173 /* Wait for the next trigger from the high frequency timer interrupt. */
\r
174 xSemaphoreTake( xHighFrequencyTimerSemaphore, portMAX_DELAY );
\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
181 /*-----------------------------------------------------------*/
\r
183 static void prvPortHighFrequencyTimerHandler( int iArg )
\r
185 static volatile unsigned long ulExecutionCounter = 0UL;
\r
186 unsigned long ulHigherPriorityTaskWoken = pdFALSE;
\r
188 /* Just to avoid compiler warnings about unused parameters. */
\r
191 /* Clear the interrupt source. */
\r
192 STM_ISRR.reg = 1UL << 2UL;
\r
194 /* Reload the Compare Match register for X ticks into the future.*/
\r
195 STM_CMP1.reg += ulCompareMatchValue;
\r
197 ulExecutionCounter++;
\r
199 if( ulExecutionCounter >= ulInterruptsPer10ms )
\r
201 ulExecutionCounter = xSemaphoreGiveFromISR( xHighFrequencyTimerSemaphore, &ulHigherPriorityTaskWoken );
\r
203 /* If the semaphore was given ulExeuctionCounter will now be pdTRUE. */
\r
204 configASSERT( ulExecutionCounter == pdTRUE );
\r
206 /* Start counting again. */
\r
207 ulExecutionCounter = 0UL;
\r
210 /* Context switch on exit if necessary. */
\r
211 portYIELD_FROM_ISR( ulHigherPriorityTaskWoken );
\r