2 FreeRTOS V9.0.0rc1 - Copyright (C) 2016 Real Time Engineers Ltd.
\r
5 VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.
\r
7 This file is part of the FreeRTOS distribution.
\r
9 FreeRTOS is free software; you can redistribute it and/or modify it under
\r
10 the terms of the GNU General Public License (version 2) as published by the
\r
11 Free Software Foundation >>>> AND MODIFIED BY <<<< the FreeRTOS exception.
\r
13 ***************************************************************************
\r
14 >>! NOTE: The modification to the GPL is included to allow you to !<<
\r
15 >>! distribute a combined work that includes FreeRTOS without being !<<
\r
16 >>! obliged to provide the source code for proprietary components !<<
\r
17 >>! outside of the FreeRTOS kernel. !<<
\r
18 ***************************************************************************
\r
20 FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY
\r
21 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
\r
22 FOR A PARTICULAR PURPOSE. Full license text is available on the following
\r
23 link: http://www.freertos.org/a00114.html
\r
25 ***************************************************************************
\r
27 * FreeRTOS provides completely free yet professionally developed, *
\r
28 * robust, strictly quality controlled, supported, and cross *
\r
29 * platform software that is more than just the market leader, it *
\r
30 * is the industry's de facto standard. *
\r
32 * Help yourself get started quickly while simultaneously helping *
\r
33 * to support the FreeRTOS project by purchasing a FreeRTOS *
\r
34 * tutorial book, reference manual, or both: *
\r
35 * http://www.FreeRTOS.org/Documentation *
\r
37 ***************************************************************************
\r
39 http://www.FreeRTOS.org/FAQHelp.html - Having a problem? Start by reading
\r
40 the FAQ page "My application does not run, what could be wrong?". Have you
\r
41 defined configASSERT()?
\r
43 http://www.FreeRTOS.org/support - In return for receiving this top quality
\r
44 embedded software for free we request you assist our global community by
\r
45 participating in the support forum.
\r
47 http://www.FreeRTOS.org/training - Investing in training allows your team to
\r
48 be as productive as possible as early as possible. Now you can receive
\r
49 FreeRTOS training directly from Richard Barry, CEO of Real Time Engineers
\r
50 Ltd, and the world's leading authority on the world's leading RTOS.
\r
52 http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
\r
53 including FreeRTOS+Trace - an indispensable productivity tool, a DOS
\r
54 compatible FAT file system, and our tiny thread aware UDP/IP stack.
\r
56 http://www.FreeRTOS.org/labs - Where new FreeRTOS products go to incubate.
\r
57 Come and try FreeRTOS+TCP, our new open source TCP/IP stack for FreeRTOS.
\r
59 http://www.OpenRTOS.com - Real Time Engineers ltd. license FreeRTOS to High
\r
60 Integrity Systems ltd. to sell under the OpenRTOS brand. Low cost OpenRTOS
\r
61 licenses offer ticketed support, indemnification and commercial middleware.
\r
63 http://www.SafeRTOS.com - High Integrity Systems also provide a safety
\r
64 engineered and independently SIL3 certified version for use in safety and
\r
65 mission critical applications that require provable dependability.
\r
72 * Demonstrates and tests mutexes being used from an interrupt.
\r
78 /* Scheduler include files. */
\r
79 #include "FreeRTOS.h"
\r
83 /* Demo program include files. */
\r
84 #include "IntSemTest.h"
\r
86 /*-----------------------------------------------------------*/
\r
88 /* The priorities of the test tasks. */
\r
89 #define intsemMASTER_PRIORITY ( tskIDLE_PRIORITY )
\r
90 #define intsemSLAVE_PRIORITY ( tskIDLE_PRIORITY + 1 )
\r
92 /* The rate at which the tick hook will give the mutex. */
\r
93 #define intsemINTERRUPT_MUTEX_GIVE_PERIOD_MS ( 100 )
\r
95 /* A block time of 0 means 'don't block'. */
\r
96 #define intsemNO_BLOCK 0
\r
98 /* The maximum count value for the counting semaphore given from an
\r
100 #define intsemMAX_COUNT 3
\r
102 /*-----------------------------------------------------------*/
\r
105 * The master is a task that receives a mutex that is given from an interrupt -
\r
106 * although generally mutexes should not be used given in interrupts (and
\r
107 * definitely never taken in an interrupt) there are some circumstances when it
\r
108 * may be desirable.
\r
110 * The slave task is just used by the master task to force priority inheritance
\r
111 * on a mutex that is shared between the master and the slave - which is a
\r
112 * separate mutex to that given by the interrupt.
\r
114 static void vInterruptMutexSlaveTask( void *pvParameters );
\r
115 static void vInterruptMutexMasterTask( void *pvParameters );
\r
118 * A test whereby the master takes the shared and interrupt mutexes in that
\r
119 * order, then gives them back in the same order, ensuring the priority
\r
120 * inheritance is behaving as expected at each step.
\r
122 static void prvTakeAndGiveInTheSameOrder( void );
\r
125 * A test whereby the master takes the shared and interrupt mutexes in that
\r
126 * order, then gives them back in the opposite order to which they were taken,
\r
127 * ensuring the priority inheritance is behaving as expected at each step.
\r
129 static void prvTakeAndGiveInTheOppositeOrder( void );
\r
132 * A simple task that interacts with an interrupt using a counting semaphore,
\r
133 * primarily for code coverage purposes.
\r
135 static void vInterruptCountingSemaphoreTask( void *pvParameters );
\r
137 /*-----------------------------------------------------------*/
\r
139 /* Flag that will be latched to pdTRUE should any unexpected behaviour be
\r
140 detected in any of the tasks. */
\r
141 static volatile BaseType_t xErrorDetected = pdFALSE;
\r
143 /* Counters that are incremented on each cycle of a test. This is used to
\r
144 detect a stalled task - a test that is no longer running. */
\r
145 static volatile uint32_t ulMasterLoops = 0, ulCountingSemaphoreLoops = 0;
\r
147 /* Handles of the test tasks that must be accessed from other test tasks. */
\r
148 static TaskHandle_t xSlaveHandle;
\r
150 /* A mutex which is given from an interrupt - although generally mutexes should
\r
151 not be used given in interrupts (and definitely never taken in an interrupt)
\r
152 there are some circumstances when it may be desirable. */
\r
153 static SemaphoreHandle_t xISRMutex = NULL;
\r
155 /* A counting semaphore which is given from an interrupt. */
\r
156 static SemaphoreHandle_t xISRCountingSemaphore = NULL;
\r
158 /* A mutex which is shared between the master and slave tasks - the master
\r
159 does both sharing of this mutex with the slave and receiving a mutex from the
\r
161 static SemaphoreHandle_t xMasterSlaveMutex = NULL;
\r
163 /* Flag that allows the master task to control when the interrupt gives or does
\r
164 not give the mutex. There is no mutual exclusion on this variable, but this is
\r
165 only test code and it should be fine in the 32=bit test environment. */
\r
166 static BaseType_t xOkToGiveMutex = pdFALSE, xOkToGiveCountingSemaphore = pdFALSE;
\r
168 /* Used to coordinate timing between tasks and the interrupt. */
\r
169 const TickType_t xInterruptGivePeriod = pdMS_TO_TICKS( intsemINTERRUPT_MUTEX_GIVE_PERIOD_MS );
\r
171 /*-----------------------------------------------------------*/
\r
173 void vStartInterruptSemaphoreTasks( void )
\r
175 /* Create the semaphores that are given from an interrupt. */
\r
176 xISRMutex = xSemaphoreCreateMutex();
\r
177 configASSERT( xISRMutex );
\r
178 xISRCountingSemaphore = xSemaphoreCreateCounting( intsemMAX_COUNT, 0 );
\r
179 configASSERT( xISRCountingSemaphore );
\r
181 /* Create the mutex that is shared between the master and slave tasks (the
\r
182 master receives a mutex from an interrupt and shares a mutex with the
\r
184 xMasterSlaveMutex = xSemaphoreCreateMutex();
\r
185 configASSERT( xMasterSlaveMutex );
\r
187 /* Create the tasks that share mutexes between then and with interrupts. */
\r
188 xTaskCreate( vInterruptMutexSlaveTask, "IntMuS", configMINIMAL_STACK_SIZE, NULL, intsemSLAVE_PRIORITY, &xSlaveHandle );
\r
189 xTaskCreate( vInterruptMutexMasterTask, "IntMuM", configMINIMAL_STACK_SIZE, NULL, intsemMASTER_PRIORITY, NULL );
\r
191 /* Create the task that blocks on the counting semaphore. */
\r
192 xTaskCreate( vInterruptCountingSemaphoreTask, "IntCnt", configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY, NULL );
\r
194 /*-----------------------------------------------------------*/
\r
196 static void vInterruptMutexMasterTask( void *pvParameters )
\r
198 /* Just to avoid compiler warnings. */
\r
199 ( void ) pvParameters;
\r
203 prvTakeAndGiveInTheSameOrder();
\r
205 /* Ensure not to starve out other tests. */
\r
207 vTaskDelay( intsemINTERRUPT_MUTEX_GIVE_PERIOD_MS );
\r
209 prvTakeAndGiveInTheOppositeOrder();
\r
211 /* Ensure not to starve out other tests. */
\r
213 vTaskDelay( intsemINTERRUPT_MUTEX_GIVE_PERIOD_MS );
\r
216 /*-----------------------------------------------------------*/
\r
218 static void prvTakeAndGiveInTheSameOrder( void )
\r
220 /* Ensure the slave is suspended, and that this task is running at the
\r
221 lower priority as expected as the start conditions. */
\r
222 #if( INCLUDE_eTaskGetState == 1 )
\r
224 configASSERT( eTaskGetState( xSlaveHandle ) == eSuspended );
\r
226 #endif /* INCLUDE_eTaskGetState */
\r
228 if( uxTaskPriorityGet( NULL ) != intsemMASTER_PRIORITY )
\r
230 xErrorDetected = pdTRUE;
\r
233 /* Take the semaphore that is shared with the slave. */
\r
234 if( xSemaphoreTake( xMasterSlaveMutex, intsemNO_BLOCK ) != pdPASS )
\r
236 xErrorDetected = pdTRUE;
\r
239 /* This task now has the mutex. Unsuspend the slave so it too
\r
240 attempts to take the mutex. */
\r
241 vTaskResume( xSlaveHandle );
\r
243 /* The slave has the higher priority so should now have executed and
\r
244 blocked on the semaphore. */
\r
245 #if( INCLUDE_eTaskGetState == 1 )
\r
247 configASSERT( eTaskGetState( xSlaveHandle ) == eBlocked );
\r
249 #endif /* INCLUDE_eTaskGetState */
\r
251 /* This task should now have inherited the priority of the slave
\r
253 if( uxTaskPriorityGet( NULL ) != intsemSLAVE_PRIORITY )
\r
255 xErrorDetected = pdTRUE;
\r
258 /* Now wait a little longer than the time between ISR gives to also
\r
259 obtain the ISR mutex. */
\r
260 xOkToGiveMutex = pdTRUE;
\r
261 if( xSemaphoreTake( xISRMutex, ( xInterruptGivePeriod * 2 ) ) != pdPASS )
\r
263 xErrorDetected = pdTRUE;
\r
265 xOkToGiveMutex = pdFALSE;
\r
267 /* Attempting to take again immediately should fail as the mutex is
\r
269 if( xSemaphoreTake( xISRMutex, intsemNO_BLOCK ) != pdFAIL )
\r
271 xErrorDetected = pdTRUE;
\r
274 /* Should still be at the priority of the slave task. */
\r
275 if( uxTaskPriorityGet( NULL ) != intsemSLAVE_PRIORITY )
\r
277 xErrorDetected = pdTRUE;
\r
280 /* Give back the ISR semaphore to ensure the priority is not
\r
281 disinherited as the shared mutex (which the higher priority task is
\r
282 attempting to obtain) is still held. */
\r
283 if( xSemaphoreGive( xISRMutex ) != pdPASS )
\r
285 xErrorDetected = pdTRUE;
\r
288 if( uxTaskPriorityGet( NULL ) != intsemSLAVE_PRIORITY )
\r
290 xErrorDetected = pdTRUE;
\r
293 /* Finally give back the shared mutex. This time the higher priority
\r
294 task should run before this task runs again - so this task should have
\r
295 disinherited the priority and the higher priority task should be in the
\r
296 suspended state again. */
\r
297 if( xSemaphoreGive( xMasterSlaveMutex ) != pdPASS )
\r
299 xErrorDetected = pdTRUE;
\r
302 if( uxTaskPriorityGet( NULL ) != intsemMASTER_PRIORITY )
\r
304 xErrorDetected = pdTRUE;
\r
307 #if( INCLUDE_eTaskGetState == 1 )
\r
309 configASSERT( eTaskGetState( xSlaveHandle ) == eSuspended );
\r
311 #endif /* INCLUDE_eTaskGetState */
\r
313 /* Reset the mutex ready for the next round. */
\r
314 xQueueReset( xISRMutex );
\r
316 /*-----------------------------------------------------------*/
\r
318 static void prvTakeAndGiveInTheOppositeOrder( void )
\r
320 /* Ensure the slave is suspended, and that this task is running at the
\r
321 lower priority as expected as the start conditions. */
\r
322 #if( INCLUDE_eTaskGetState == 1 )
\r
324 configASSERT( eTaskGetState( xSlaveHandle ) == eSuspended );
\r
326 #endif /* INCLUDE_eTaskGetState */
\r
328 if( uxTaskPriorityGet( NULL ) != intsemMASTER_PRIORITY )
\r
330 xErrorDetected = pdTRUE;
\r
333 /* Take the semaphore that is shared with the slave. */
\r
334 if( xSemaphoreTake( xMasterSlaveMutex, intsemNO_BLOCK ) != pdPASS )
\r
336 xErrorDetected = pdTRUE;
\r
339 /* This task now has the mutex. Unsuspend the slave so it too
\r
340 attempts to take the mutex. */
\r
341 vTaskResume( xSlaveHandle );
\r
343 /* The slave has the higher priority so should now have executed and
\r
344 blocked on the semaphore. */
\r
345 #if( INCLUDE_eTaskGetState == 1 )
\r
347 configASSERT( eTaskGetState( xSlaveHandle ) == eBlocked );
\r
349 #endif /* INCLUDE_eTaskGetState */
\r
351 /* This task should now have inherited the priority of the slave
\r
353 if( uxTaskPriorityGet( NULL ) != intsemSLAVE_PRIORITY )
\r
355 xErrorDetected = pdTRUE;
\r
358 /* Now wait a little longer than the time between ISR gives to also
\r
359 obtain the ISR mutex. */
\r
360 xOkToGiveMutex = pdTRUE;
\r
361 if( xSemaphoreTake( xISRMutex, ( xInterruptGivePeriod * 2 ) ) != pdPASS )
\r
363 xErrorDetected = pdTRUE;
\r
365 xOkToGiveMutex = pdFALSE;
\r
367 /* Attempting to take again immediately should fail as the mutex is
\r
369 if( xSemaphoreTake( xISRMutex, intsemNO_BLOCK ) != pdFAIL )
\r
371 xErrorDetected = pdTRUE;
\r
374 /* Should still be at the priority of the slave task. */
\r
375 if( uxTaskPriorityGet( NULL ) != intsemSLAVE_PRIORITY )
\r
377 xErrorDetected = pdTRUE;
\r
380 /* Give back the shared semaphore to ensure the priority is not disinherited
\r
381 as the ISR mutex is still held. The higher priority slave task should run
\r
382 before this task runs again. */
\r
383 if( xSemaphoreGive( xMasterSlaveMutex ) != pdPASS )
\r
385 xErrorDetected = pdTRUE;
\r
388 /* Should still be at the priority of the slave task as this task still
\r
389 holds one semaphore (this is a simplification in the priority inheritance
\r
391 if( uxTaskPriorityGet( NULL ) != intsemSLAVE_PRIORITY )
\r
393 xErrorDetected = pdTRUE;
\r
396 /* Give back the ISR semaphore, which should result in the priority being
\r
397 disinherited as it was the last mutex held. */
\r
398 if( xSemaphoreGive( xISRMutex ) != pdPASS )
\r
400 xErrorDetected = pdTRUE;
\r
403 if( uxTaskPriorityGet( NULL ) != intsemMASTER_PRIORITY )
\r
405 xErrorDetected = pdTRUE;
\r
408 /* Reset the mutex ready for the next round. */
\r
409 xQueueReset( xISRMutex );
\r
411 /*-----------------------------------------------------------*/
\r
413 static void vInterruptMutexSlaveTask( void *pvParameters )
\r
415 /* Just to avoid compiler warnings. */
\r
416 ( void ) pvParameters;
\r
420 /* This task starts by suspending itself so when it executes can be
\r
421 controlled by the master task. */
\r
422 vTaskSuspend( NULL );
\r
424 /* This task will execute when the master task already holds the mutex.
\r
425 Attempting to take the mutex will place this task in the Blocked
\r
427 if( xSemaphoreTake( xMasterSlaveMutex, portMAX_DELAY ) != pdPASS )
\r
429 xErrorDetected = pdTRUE;
\r
432 if( xSemaphoreGive( xMasterSlaveMutex ) != pdPASS )
\r
434 xErrorDetected = pdTRUE;
\r
438 /*-----------------------------------------------------------*/
\r
440 static void vInterruptCountingSemaphoreTask( void *pvParameters )
\r
443 const TickType_t xDelay = pdMS_TO_TICKS( intsemINTERRUPT_MUTEX_GIVE_PERIOD_MS ) * ( intsemMAX_COUNT + 1 );
\r
445 ( void ) pvParameters;
\r
449 /* Expect to start with the counting semaphore empty. */
\r
450 if( uxQueueMessagesWaiting( ( QueueHandle_t ) xISRCountingSemaphore ) != 0 )
\r
452 xErrorDetected = pdTRUE;
\r
455 /* Wait until it is expected that the interrupt will have filled the
\r
456 counting semaphore. */
\r
457 xOkToGiveCountingSemaphore = pdTRUE;
\r
458 vTaskDelay( xDelay );
\r
459 xOkToGiveCountingSemaphore = pdFALSE;
\r
461 /* Now it is expected that the counting semaphore is full. */
\r
462 if( uxQueueMessagesWaiting( ( QueueHandle_t ) xISRCountingSemaphore ) != intsemMAX_COUNT )
\r
464 xErrorDetected = pdTRUE;
\r
467 if( uxQueueSpacesAvailable( ( QueueHandle_t ) xISRCountingSemaphore ) != 0 )
\r
469 xErrorDetected = pdTRUE;
\r
472 ulCountingSemaphoreLoops++;
\r
474 /* Expect to be able to take the counting semaphore intsemMAX_COUNT
\r
475 times. A block time of 0 is used as the semaphore should already be
\r
478 while( xSemaphoreTake( xISRCountingSemaphore, 0 ) == pdPASS )
\r
483 if( xCount != intsemMAX_COUNT )
\r
485 xErrorDetected = pdTRUE;
\r
488 /* Now raise the priority of this task so it runs immediately that the
\r
489 semaphore is given from the interrupt. */
\r
490 vTaskPrioritySet( NULL, configMAX_PRIORITIES - 1 );
\r
492 /* Block to wait for the semaphore to be given from the interrupt. */
\r
493 xOkToGiveCountingSemaphore = pdTRUE;
\r
494 xSemaphoreTake( xISRCountingSemaphore, portMAX_DELAY );
\r
495 xSemaphoreTake( xISRCountingSemaphore, portMAX_DELAY );
\r
496 xOkToGiveCountingSemaphore = pdFALSE;
\r
498 /* Reset the priority so as not to disturbe other tests too much. */
\r
499 vTaskPrioritySet( NULL, tskIDLE_PRIORITY );
\r
501 ulCountingSemaphoreLoops++;
\r
504 /*-----------------------------------------------------------*/
\r
506 void vInterruptSemaphorePeriodicTest( void )
\r
508 static TickType_t xLastGiveTime = 0;
\r
509 BaseType_t xHigherPriorityTaskWoken = pdFALSE;
\r
510 TickType_t xTimeNow;
\r
512 /* No mutual exclusion on xOkToGiveMutex, but this is only test code (and
\r
513 only executed on a 32-bit architecture) so ignore that in this case. */
\r
514 xTimeNow = xTaskGetTickCountFromISR();
\r
515 if( ( ( TickType_t ) ( xTimeNow - xLastGiveTime ) ) >= pdMS_TO_TICKS( intsemINTERRUPT_MUTEX_GIVE_PERIOD_MS ) )
\r
517 configASSERT( xISRMutex );
\r
518 if( xOkToGiveMutex != pdFALSE )
\r
520 /* Null is used as the second parameter in this give, and non-NULL
\r
521 in the other gives for code coverage reasons. */
\r
522 xSemaphoreGiveFromISR( xISRMutex, NULL );
\r
524 /* Second give attempt should fail. */
\r
525 configASSERT( xSemaphoreGiveFromISR( xISRMutex, &xHigherPriorityTaskWoken ) == pdFAIL );
\r
528 if( xOkToGiveCountingSemaphore != pdFALSE )
\r
530 xSemaphoreGiveFromISR( xISRCountingSemaphore, &xHigherPriorityTaskWoken );
\r
532 xLastGiveTime = xTimeNow;
\r
535 /* Remove compiler warnings about the value being set but not used. */
\r
536 ( void ) xHigherPriorityTaskWoken;
\r
538 /*-----------------------------------------------------------*/
\r
540 /* This is called to check that all the created tasks are still running. */
\r
541 BaseType_t xAreInterruptSemaphoreTasksStillRunning( void )
\r
543 static uint32_t ulLastMasterLoopCounter = 0, ulLastCountingSemaphoreLoops = 0;
\r
545 /* If the demo tasks are running then it is expected that the loop counters
\r
546 will have changed since this function was last called. */
\r
547 if( ulLastMasterLoopCounter == ulMasterLoops )
\r
549 xErrorDetected = pdTRUE;
\r
552 ulLastMasterLoopCounter = ulMasterLoops;
\r
554 if( ulLastCountingSemaphoreLoops == ulCountingSemaphoreLoops )
\r
556 xErrorDetected = pdTRUE;
\r
559 ulLastCountingSemaphoreLoops = ulCountingSemaphoreLoops++;
\r
561 /* Errors detected in the task itself will have latched xErrorDetected
\r
564 return ( BaseType_t ) !xErrorDetected;
\r