2 FreeRTOS V8.1.2 - Copyright (C) 2014 Real Time Engineers Ltd.
\r
5 VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.
\r
7 ***************************************************************************
\r
9 * FreeRTOS provides completely free yet professionally developed, *
\r
10 * robust, strictly quality controlled, supported, and cross *
\r
11 * platform software that has become a de facto standard. *
\r
13 * Help yourself get started quickly and support the FreeRTOS *
\r
14 * project by purchasing a FreeRTOS tutorial book, reference *
\r
15 * manual, or both from: http://www.FreeRTOS.org/Documentation *
\r
19 ***************************************************************************
\r
21 This file is part of the FreeRTOS distribution.
\r
23 FreeRTOS is free software; you can redistribute it and/or modify it under
\r
24 the terms of the GNU General Public License (version 2) as published by the
\r
25 Free Software Foundation >>!AND MODIFIED BY!<< the FreeRTOS exception.
\r
27 >>! NOTE: The modification to the GPL is included to allow you to !<<
\r
28 >>! distribute a combined work that includes FreeRTOS without being !<<
\r
29 >>! obliged to provide the source code for proprietary components !<<
\r
30 >>! outside of the FreeRTOS kernel. !<<
\r
32 FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY
\r
33 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
\r
34 FOR A PARTICULAR PURPOSE. Full license text is available from the following
\r
35 link: http://www.freertos.org/a00114.html
\r
39 ***************************************************************************
\r
41 * Having a problem? Start by reading the FAQ "My application does *
\r
42 * not run, what could be wrong?" *
\r
44 * http://www.FreeRTOS.org/FAQHelp.html *
\r
46 ***************************************************************************
\r
48 http://www.FreeRTOS.org - Documentation, books, training, latest versions,
\r
49 license and Real Time Engineers Ltd. contact details.
\r
51 http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
\r
52 including FreeRTOS+Trace - an indispensable productivity tool, a DOS
\r
53 compatible FAT file system, and our tiny thread aware UDP/IP stack.
\r
55 http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High
\r
56 Integrity Systems to sell under the OpenRTOS brand. Low cost OpenRTOS
\r
57 licenses offer ticketed support, indemnification and middleware.
\r
59 http://www.SafeRTOS.com - High Integrity Systems also provide a safety
\r
60 engineered and independently SIL3 certified version for use in safety and
\r
61 mission critical applications that require provable dependability.
\r
68 * Demonstrates and tests mutexes being used from an interrupt.
\r
74 /* Scheduler include files. */
\r
75 #include "FreeRTOS.h"
\r
79 /* Demo program include files. */
\r
80 #include "IntSemTest.h"
\r
82 /*-----------------------------------------------------------*/
\r
84 /* The priorities of the test tasks. */
\r
85 #define intsemMASTER_PRIORITY ( tskIDLE_PRIORITY )
\r
86 #define intsemSLAVE_PRIORITY ( tskIDLE_PRIORITY + 1 )
\r
88 /* The rate at which the tick hook will give the mutex. */
\r
89 #define intsemINTERRUPT_MUTEX_GIVE_PERIOD_MS ( 100 )
\r
91 /* A block time of 0 means 'don't block'. */
\r
92 #define intsemNO_BLOCK 0
\r
94 /*-----------------------------------------------------------*/
\r
97 * The master is a task that receives a mutex that is given from an interrupt -
\r
98 * although generally mutexes should not be used given in interrupts (and
\r
99 * definitely never taken in an interrupt) there are some circumstances when it
\r
100 * may be desirable.
\r
102 * The slave task is just used by the master task to force priority inheritance
\r
103 * on a mutex that is shared between the master and the slave - which is a
\r
104 * separate mutex to that given by the interrupt.
\r
106 static void vInterruptMutexSlaveTask( void *pvParameters );
\r
107 static void vInterruptMutexMasterTask( void *pvParameters );
\r
109 /*-----------------------------------------------------------*/
\r
111 /* Flag that will be latched to pdTRUE should any unexpected behaviour be
\r
112 detected in any of the tasks. */
\r
113 static volatile BaseType_t xErrorDetected = pdFALSE;
\r
115 /* Counters that are incremented on each cycle of a test. This is used to
\r
116 detect a stalled task - a test that is no longer running. */
\r
117 static volatile uint32_t ulMasterLoops = 0;
\r
119 /* Handles of the test tasks that must be accessed from other test tasks. */
\r
120 static TaskHandle_t xSlaveHandle;
\r
122 /* A mutex which is given from an interrupt - although generally mutexes should
\r
123 not be used given in interrupts (and definitely never taken in an interrupt)
\r
124 there are some circumstances when it may be desirable. */
\r
125 static SemaphoreHandle_t xISRMutex = NULL;
\r
127 /* A mutex which is shared between the master and slave tasks - the master
\r
128 does both sharing of this mutex with the slave and receiving a mutex from the
\r
130 static SemaphoreHandle_t xMasterSlaveMutex = NULL;
\r
132 /* Flag that allows the master task to control when the interrupt gives or does
\r
133 not give the mutex. There is no mutual exclusion on this variable, but this is
\r
134 only test code and it should be fine in the 32=bit test environment. */
\r
135 static BaseType_t xOkToGiveMutex = pdFALSE;
\r
137 /*-----------------------------------------------------------*/
\r
139 void vStartInterruptSemaphoreTasks( void )
\r
141 /* Create the mutex that is given from an interrupt. */
\r
142 xISRMutex = xSemaphoreCreateMutex();
\r
143 configASSERT( xISRMutex );
\r
145 /* Create the mutex that is shared between the master and slave tasks (the
\r
146 master receives a mutex from an interrupt and shares a mutex with the
\r
148 xMasterSlaveMutex = xSemaphoreCreateMutex();
\r
149 configASSERT( xMasterSlaveMutex );
\r
151 /* Create the tasks that share mutexes between then and with interrupts. */
\r
152 xTaskCreate( vInterruptMutexSlaveTask, "IntMuS", configMINIMAL_STACK_SIZE, NULL, intsemSLAVE_PRIORITY, &xSlaveHandle );
\r
153 xTaskCreate( vInterruptMutexMasterTask, "IntMuM", configMINIMAL_STACK_SIZE, NULL, intsemMASTER_PRIORITY, NULL );
\r
155 /*-----------------------------------------------------------*/
\r
157 static void vInterruptMutexMasterTask( void *pvParameters )
\r
159 const TickType_t xInterruptGivePeriod = pdMS_TO_TICKS( intsemINTERRUPT_MUTEX_GIVE_PERIOD_MS );
\r
161 /* Just to avoid compiler warnings. */
\r
162 ( void ) pvParameters;
\r
166 /* Ensure the slave is suspended, and that this task is running at the
\r
167 lower priority as expected as the start conditions. */
\r
168 #if( INCLUDE_eTaskGetState == 1 )
\r
170 configASSERT( eTaskGetState( xSlaveHandle ) == eSuspended );
\r
172 #endif /* INCLUDE_eTaskGetState */
\r
174 if( uxTaskPriorityGet( NULL ) != intsemMASTER_PRIORITY )
\r
176 xErrorDetected = pdTRUE;
\r
179 /* Take the semaphore that is shared with the slave. */
\r
180 if( xSemaphoreTake( xMasterSlaveMutex, intsemNO_BLOCK ) != pdPASS )
\r
182 xErrorDetected = pdTRUE;
\r
185 /* This task now has the mutex. Unsuspend the slave so it too
\r
186 attempts to take the mutex. */
\r
187 vTaskResume( xSlaveHandle );
\r
189 /* The slave has the higher priority so should now have executed and
\r
190 blocked on the semaphore. */
\r
191 #if( INCLUDE_eTaskGetState == 1 )
\r
193 configASSERT( eTaskGetState( xSlaveHandle ) == eBlocked );
\r
195 #endif /* INCLUDE_eTaskGetState */
\r
197 /* This task should now have inherited the priority of the slave
\r
199 if( uxTaskPriorityGet( NULL ) != intsemSLAVE_PRIORITY )
\r
201 xErrorDetected = pdTRUE;
\r
204 /* Now wait a little longer than the time between ISR gives to also
\r
205 obtain the ISR mutex. */
\r
206 xOkToGiveMutex = pdTRUE;
\r
207 if( xSemaphoreTake( xISRMutex, ( xInterruptGivePeriod * 2 ) ) != pdPASS )
\r
209 xErrorDetected = pdTRUE;
\r
211 xOkToGiveMutex = pdFALSE;
\r
213 /* Attempting to take again immediately should fail as the mutex is
\r
215 if( xSemaphoreTake( xISRMutex, intsemNO_BLOCK ) != pdFAIL )
\r
217 xErrorDetected = pdTRUE;
\r
220 /* Should still be at the priority of the slave task. */
\r
221 if( uxTaskPriorityGet( NULL ) != intsemSLAVE_PRIORITY )
\r
223 xErrorDetected = pdTRUE;
\r
226 /* Give back the ISR semaphore to ensure the priority is not
\r
227 disinherited as the shared mutex (which the higher priority task is
\r
228 attempting to obtain) is still held. */
\r
229 if( xSemaphoreGive( xISRMutex ) != pdPASS )
\r
231 xErrorDetected = pdTRUE;
\r
234 if( uxTaskPriorityGet( NULL ) != intsemSLAVE_PRIORITY )
\r
236 xErrorDetected = pdTRUE;
\r
239 /* Finally give back the shared mutex. This time the higher priority
\r
240 task should run before this task runs again - so this task should have
\r
241 disinherited the priority and the higher priority task should be in the
\r
242 suspended state again. */
\r
243 if( xSemaphoreGive( xMasterSlaveMutex ) != pdPASS )
\r
245 xErrorDetected = pdTRUE;
\r
248 if( uxTaskPriorityGet( NULL ) != intsemMASTER_PRIORITY )
\r
250 xErrorDetected = pdTRUE;
\r
253 #if( INCLUDE_eTaskGetState == 1 )
\r
255 configASSERT( eTaskGetState( xSlaveHandle ) == eSuspended );
\r
257 #endif /* INCLUDE_eTaskGetState */
\r
259 /* Ensure not to starve out other tests. */
\r
261 vTaskDelay( intsemINTERRUPT_MUTEX_GIVE_PERIOD_MS );
\r
264 /* Repeat exactly up to the point where the mutexes are given back.
\r
265 This time the shared mutex is given back first. */
\r
266 if( xSemaphoreTake( xMasterSlaveMutex, intsemNO_BLOCK ) != pdPASS )
\r
268 xErrorDetected = pdTRUE;
\r
271 vTaskResume( xSlaveHandle );
\r
273 #if( INCLUDE_eTaskGetState == 1 )
\r
275 configASSERT( eTaskGetState( xSlaveHandle ) == eBlocked );
\r
277 #endif /* INCLUDE_eTaskGetState */
\r
279 if( uxTaskPriorityGet( NULL ) != intsemSLAVE_PRIORITY )
\r
281 xErrorDetected = pdTRUE;
\r
284 xOkToGiveMutex = pdTRUE;
\r
285 if( xSemaphoreTake( xISRMutex, ( xInterruptGivePeriod * 2 ) ) != pdPASS )
\r
287 xErrorDetected = pdTRUE;
\r
289 xOkToGiveMutex = pdFALSE;
\r
291 if( uxTaskPriorityGet( NULL ) != intsemSLAVE_PRIORITY )
\r
293 xErrorDetected = pdTRUE;
\r
296 /* This is where the differences start as this time the shared mutex is
\r
297 given back first. This time to the higher priority task should run
\r
298 before this task gets to the point of releasing the interrupt mutex - so
\r
299 this task should have disinherited the priority and the higher priority
\r
300 task should be in the suspended state again. */
\r
301 if( xSemaphoreGive( xMasterSlaveMutex ) != pdPASS )
\r
303 xErrorDetected = pdTRUE;
\r
306 /* Give back the interrupt semaphore too, so the mutex held count goes
\r
307 back to 0. The mutex will then have to be reset so the ISR can give it
\r
308 in the next cycle. */
\r
309 xSemaphoreGive( xISRMutex );
\r
310 xQueueReset( ( QueueHandle_t ) xISRMutex );
\r
312 /* Ensure not to starve out other tests. */
\r
314 vTaskDelay( intsemINTERRUPT_MUTEX_GIVE_PERIOD_MS );
\r
317 /*-----------------------------------------------------------*/
\r
319 static void vInterruptMutexSlaveTask( void *pvParameters )
\r
321 /* Just to avoid compiler warnings. */
\r
322 ( void ) pvParameters;
\r
326 /* This task starts by suspending itself so when it executes can be
\r
327 controlled by the master task. */
\r
328 vTaskSuspend( NULL );
\r
330 /* This task will execute when the master task already holds the mutex.
\r
331 Attempting to take the mutex will place this task in the Blocked
\r
333 if( xSemaphoreTake( xMasterSlaveMutex, portMAX_DELAY ) != pdPASS )
\r
335 xErrorDetected = pdTRUE;
\r
338 if( xSemaphoreGive( xMasterSlaveMutex ) != pdPASS )
\r
340 xErrorDetected = pdTRUE;
\r
344 /*-----------------------------------------------------------*/
\r
346 void vInterruptSemaphorePeriodicTest( void )
\r
348 static TickType_t xLastGiveTime = 0;
\r
349 TickType_t xTimeNow;
\r
351 /* No mutual exclusion on xOkToGiveMutex, but this is only test code (and
\r
352 only executed on a 32-bit architecture) so ignore that in this case. */
\r
353 xTimeNow = xTaskGetTickCountFromISR();
\r
354 if( ( xTimeNow - xLastGiveTime ) >= pdMS_TO_TICKS( intsemINTERRUPT_MUTEX_GIVE_PERIOD_MS ) )
\r
356 configASSERT( xISRMutex );
\r
357 if( xOkToGiveMutex != pdFALSE )
\r
359 xSemaphoreGiveFromISR( xISRMutex, NULL );
\r
361 /* Second give attempt should fail. */
\r
362 configASSERT( xSemaphoreGiveFromISR( xISRMutex, NULL ) == pdFAIL );
\r
364 xLastGiveTime = xTimeNow;
\r
367 /*-----------------------------------------------------------*/
\r
369 /* This is called to check that all the created tasks are still running. */
\r
370 BaseType_t xAreInterruptSemaphoreTasksStillRunning( void )
\r
372 static uint32_t ulLastMasterLoopCounter = 0;
\r
374 /* If the demo tasks are running then it is expected that the loop counters
\r
375 will have changed since this function was last called. */
\r
376 if( ulLastMasterLoopCounter == ulMasterLoops )
\r
378 xErrorDetected = pdTRUE;
\r
381 ulLastMasterLoopCounter = ulMasterLoops;
\r
383 /* Errors detected in the task itself will have latched xErrorDetected
\r
386 return ( BaseType_t ) !xErrorDetected;
\r