Add volatile qualifier to loop counters used to detect stalled tasks.

This commit is contained in:
Richard Barry 2008-05-20 05:22:35 +00:00
parent bea704342a
commit b19ede773a
5 changed files with 1967 additions and 1967 deletions

View File

@ -1,434 +1,434 @@
/* /*
FreeRTOS.org V5.0.0 - Copyright (C) 2003-2008 Richard Barry. FreeRTOS.org V5.0.0 - Copyright (C) 2003-2008 Richard Barry.
This file is part of the FreeRTOS.org distribution. This file is part of the FreeRTOS.org distribution.
FreeRTOS.org is free software; you can redistribute it and/or modify FreeRTOS.org is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or the Free Software Foundation; either version 2 of the License, or
(at your option) any later version. (at your option) any later version.
FreeRTOS.org is distributed in the hope that it will be useful, FreeRTOS.org is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details. GNU General Public License for more details.
You should have received a copy of the GNU General Public License You should have received a copy of the GNU General Public License
along with FreeRTOS.org; if not, write to the Free Software along with FreeRTOS.org; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
A special exception to the GPL can be applied should you wish to distribute A special exception to the GPL can be applied should you wish to distribute
a combined work that includes FreeRTOS.org, without being obliged to provide a combined work that includes FreeRTOS.org, without being obliged to provide
the source code for any proprietary components. See the licensing section the source code for any proprietary components. See the licensing section
of http://www.FreeRTOS.org for full details of how and when the exception of http://www.FreeRTOS.org for full details of how and when the exception
can be applied. can be applied.
*************************************************************************** ***************************************************************************
*************************************************************************** ***************************************************************************
* * * *
* SAVE TIME AND MONEY! We can port FreeRTOS.org to your own hardware, * * SAVE TIME AND MONEY! We can port FreeRTOS.org to your own hardware, *
* and even write all or part of your application on your behalf. * * and even write all or part of your application on your behalf. *
* See http://www.OpenRTOS.com for details of the services we provide to * * See http://www.OpenRTOS.com for details of the services we provide to *
* expedite your project. * * expedite your project. *
* * * *
*************************************************************************** ***************************************************************************
*************************************************************************** ***************************************************************************
Please ensure to read the configuration and relevant port sections of the Please ensure to read the configuration and relevant port sections of the
online documentation. online documentation.
http://www.FreeRTOS.org - Documentation, latest information, license and http://www.FreeRTOS.org - Documentation, latest information, license and
contact details. contact details.
http://www.SafeRTOS.com - A version that is certified for use in safety http://www.SafeRTOS.com - A version that is certified for use in safety
critical systems. critical systems.
http://www.OpenRTOS.com - Commercial support, development, porting, http://www.OpenRTOS.com - Commercial support, development, porting,
licensing and training services. licensing and training services.
*/ */
/* /*
* Tests the behaviour when data is peeked from a queue when there are * Tests the behaviour when data is peeked from a queue when there are
* multiple tasks blocked on the queue. * multiple tasks blocked on the queue.
*/ */
#include <stdlib.h> #include <stdlib.h>
/* Scheduler include files. */ /* Scheduler include files. */
#include "FreeRTOS.h" #include "FreeRTOS.h"
#include "task.h" #include "task.h"
#include "queue.h" #include "queue.h"
#include "semphr.h" #include "semphr.h"
/* Demo program include files. */ /* Demo program include files. */
#include "QPeek.h" #include "QPeek.h"
#define qpeekQUEUE_LENGTH ( 5 ) #define qpeekQUEUE_LENGTH ( 5 )
#define qpeekNO_BLOCK ( 0 ) #define qpeekNO_BLOCK ( 0 )
#define qpeekSHORT_DELAY ( 10 ) #define qpeekSHORT_DELAY ( 10 )
#define qpeekLOW_PRIORITY ( tskIDLE_PRIORITY + 0 ) #define qpeekLOW_PRIORITY ( tskIDLE_PRIORITY + 0 )
#define qpeekMEDIUM_PRIORITY ( tskIDLE_PRIORITY + 1 ) #define qpeekMEDIUM_PRIORITY ( tskIDLE_PRIORITY + 1 )
#define qpeekHIGH_PRIORITY ( tskIDLE_PRIORITY + 2 ) #define qpeekHIGH_PRIORITY ( tskIDLE_PRIORITY + 2 )
#define qpeekHIGHEST_PRIORITY ( tskIDLE_PRIORITY + 3 ) #define qpeekHIGHEST_PRIORITY ( tskIDLE_PRIORITY + 3 )
/*-----------------------------------------------------------*/ /*-----------------------------------------------------------*/
/* /*
* The following three tasks are used to demonstrate the peeking behaviour. * The following three tasks are used to demonstrate the peeking behaviour.
* Each task is given a different priority to demonstrate the order in which * Each task is given a different priority to demonstrate the order in which
* tasks are woken as data is peeked from a queue. * tasks are woken as data is peeked from a queue.
*/ */
static void prvLowPriorityPeekTask( void *pvParameters ); static void prvLowPriorityPeekTask( void *pvParameters );
static void prvMediumPriorityPeekTask( void *pvParameters ); static void prvMediumPriorityPeekTask( void *pvParameters );
static void prvHighPriorityPeekTask( void *pvParameters ); static void prvHighPriorityPeekTask( void *pvParameters );
static void prvHighestPriorityPeekTask( void *pvParameters ); static void prvHighestPriorityPeekTask( void *pvParameters );
/*-----------------------------------------------------------*/ /*-----------------------------------------------------------*/
/* Flag that will be latched to pdTRUE should any unexpected behaviour be /* Flag that will be latched to pdTRUE should any unexpected behaviour be
detected in any of the tasks. */ detected in any of the tasks. */
static portBASE_TYPE xErrorDetected = pdFALSE; static volatile portBASE_TYPE xErrorDetected = pdFALSE;
/* Counter that is incremented on each cycle of a test. This is used to /* Counter that is incremented on each cycle of a test. This is used to
detect a stalled task - a test that is no longer running. */ detect a stalled task - a test that is no longer running. */
static volatile unsigned portLONG ulLoopCounter = 0; static volatile unsigned portLONG ulLoopCounter = 0;
/* Handles to the test tasks. */ /* Handles to the test tasks. */
xTaskHandle xMediumPriorityTask, xHighPriorityTask, xHighestPriorityTask; xTaskHandle xMediumPriorityTask, xHighPriorityTask, xHighestPriorityTask;
/*-----------------------------------------------------------*/ /*-----------------------------------------------------------*/
void vStartQueuePeekTasks( void ) void vStartQueuePeekTasks( void )
{ {
xQueueHandle xQueue; xQueueHandle xQueue;
/* Create the queue that we are going to use for the test/demo. */ /* Create the queue that we are going to use for the test/demo. */
xQueue = xQueueCreate( qpeekQUEUE_LENGTH, sizeof( unsigned portLONG ) ); xQueue = xQueueCreate( qpeekQUEUE_LENGTH, sizeof( unsigned portLONG ) );
/* Create the demo tasks and pass it the queue just created. We are /* Create the demo tasks and pass it the queue just created. We are
passing the queue handle by value so it does not matter that it is declared passing the queue handle by value so it does not matter that it is declared
on the stack here. */ on the stack here. */
xTaskCreate( prvLowPriorityPeekTask, ( signed portCHAR * )"PeekL", configMINIMAL_STACK_SIZE, ( void * ) xQueue, qpeekLOW_PRIORITY, NULL ); xTaskCreate( prvLowPriorityPeekTask, ( signed portCHAR * )"PeekL", configMINIMAL_STACK_SIZE, ( void * ) xQueue, qpeekLOW_PRIORITY, NULL );
xTaskCreate( prvMediumPriorityPeekTask, ( signed portCHAR * )"PeekM", configMINIMAL_STACK_SIZE, ( void * ) xQueue, qpeekMEDIUM_PRIORITY, &xMediumPriorityTask ); xTaskCreate( prvMediumPriorityPeekTask, ( signed portCHAR * )"PeekM", configMINIMAL_STACK_SIZE, ( void * ) xQueue, qpeekMEDIUM_PRIORITY, &xMediumPriorityTask );
xTaskCreate( prvHighPriorityPeekTask, ( signed portCHAR * )"PeekH1", configMINIMAL_STACK_SIZE, ( void * ) xQueue, qpeekHIGH_PRIORITY, &xHighPriorityTask ); xTaskCreate( prvHighPriorityPeekTask, ( signed portCHAR * )"PeekH1", configMINIMAL_STACK_SIZE, ( void * ) xQueue, qpeekHIGH_PRIORITY, &xHighPriorityTask );
xTaskCreate( prvHighestPriorityPeekTask, ( signed portCHAR * )"PeekH2", configMINIMAL_STACK_SIZE, ( void * ) xQueue, qpeekHIGHEST_PRIORITY, &xHighestPriorityTask ); xTaskCreate( prvHighestPriorityPeekTask, ( signed portCHAR * )"PeekH2", configMINIMAL_STACK_SIZE, ( void * ) xQueue, qpeekHIGHEST_PRIORITY, &xHighestPriorityTask );
} }
/*-----------------------------------------------------------*/ /*-----------------------------------------------------------*/
static void prvHighestPriorityPeekTask( void *pvParameters ) static void prvHighestPriorityPeekTask( void *pvParameters )
{ {
xQueueHandle xQueue = ( xQueueHandle ) pvParameters; xQueueHandle xQueue = ( xQueueHandle ) pvParameters;
unsigned portLONG ulValue; unsigned portLONG ulValue;
#ifdef USE_STDIO #ifdef USE_STDIO
{ {
void vPrintDisplayMessage( const portCHAR * const * ppcMessageToSend ); void vPrintDisplayMessage( const portCHAR * const * ppcMessageToSend );
const portCHAR * const pcTaskStartMsg = "Queue peek test started.\r\n"; const portCHAR * const pcTaskStartMsg = "Queue peek test started.\r\n";
/* Queue a message for printing to say the task has started. */ /* Queue a message for printing to say the task has started. */
vPrintDisplayMessage( &pcTaskStartMsg ); vPrintDisplayMessage( &pcTaskStartMsg );
} }
#endif #endif
for( ;; ) for( ;; )
{ {
/* Try peeking from the queue. The queue should be empty so we will /* Try peeking from the queue. The queue should be empty so we will
block, allowing the high priority task to execute. */ block, allowing the high priority task to execute. */
if( xQueuePeek( xQueue, &ulValue, portMAX_DELAY ) != pdPASS ) if( xQueuePeek( xQueue, &ulValue, portMAX_DELAY ) != pdPASS )
{ {
/* We expected to have received something by the time we unblock. */ /* We expected to have received something by the time we unblock. */
xErrorDetected = pdTRUE; xErrorDetected = pdTRUE;
} }
/* When we reach here the high and medium priority tasks should still /* When we reach here the high and medium priority tasks should still
be blocked on the queue. We unblocked because the low priority task be blocked on the queue. We unblocked because the low priority task
wrote a value to the queue, which we should have peeked. Peeking the wrote a value to the queue, which we should have peeked. Peeking the
data (rather than receiving it) will leave the data on the queue, so data (rather than receiving it) will leave the data on the queue, so
the high priority task should then have also been unblocked, but not the high priority task should then have also been unblocked, but not
yet executed. */ yet executed. */
if( ulValue != 0x11223344 ) if( ulValue != 0x11223344 )
{ {
/* We did not receive the expected value. */ /* We did not receive the expected value. */
xErrorDetected = pdTRUE; xErrorDetected = pdTRUE;
} }
if( uxQueueMessagesWaiting( xQueue ) != 1 ) if( uxQueueMessagesWaiting( xQueue ) != 1 )
{ {
/* The message should have been left on the queue. */ /* The message should have been left on the queue. */
xErrorDetected = pdTRUE; xErrorDetected = pdTRUE;
} }
/* Now we are going to actually receive the data, so when the high /* Now we are going to actually receive the data, so when the high
priority task runs it will find the queue empty and return to the priority task runs it will find the queue empty and return to the
blocked state. */ blocked state. */
ulValue = 0; ulValue = 0;
if( xQueueReceive( xQueue, &ulValue, qpeekNO_BLOCK ) != pdPASS ) if( xQueueReceive( xQueue, &ulValue, qpeekNO_BLOCK ) != pdPASS )
{ {
/* We expected to receive the value. */ /* We expected to receive the value. */
xErrorDetected = pdTRUE; xErrorDetected = pdTRUE;
} }
if( ulValue != 0x11223344 ) if( ulValue != 0x11223344 )
{ {
/* We did not receive the expected value - which should have been /* We did not receive the expected value - which should have been
the same value as was peeked. */ the same value as was peeked. */
xErrorDetected = pdTRUE; xErrorDetected = pdTRUE;
} }
/* Now we will block again as the queue is once more empty. The low /* Now we will block again as the queue is once more empty. The low
priority task can then execute again. */ priority task can then execute again. */
if( xQueuePeek( xQueue, &ulValue, portMAX_DELAY ) != pdPASS ) if( xQueuePeek( xQueue, &ulValue, portMAX_DELAY ) != pdPASS )
{ {
/* We expected to have received something by the time we unblock. */ /* We expected to have received something by the time we unblock. */
xErrorDetected = pdTRUE; xErrorDetected = pdTRUE;
} }
/* When we get here the low priority task should have again written to the /* When we get here the low priority task should have again written to the
queue. */ queue. */
if( ulValue != 0x01234567 ) if( ulValue != 0x01234567 )
{ {
/* We did not receive the expected value. */ /* We did not receive the expected value. */
xErrorDetected = pdTRUE; xErrorDetected = pdTRUE;
} }
if( uxQueueMessagesWaiting( xQueue ) != 1 ) if( uxQueueMessagesWaiting( xQueue ) != 1 )
{ {
/* The message should have been left on the queue. */ /* The message should have been left on the queue. */
xErrorDetected = pdTRUE; xErrorDetected = pdTRUE;
} }
/* We only peeked the data, so suspending ourselves now should enable /* We only peeked the data, so suspending ourselves now should enable
the high priority task to also peek the data. The high priority task the high priority task to also peek the data. The high priority task
will have been unblocked when we peeked the data as we left the data will have been unblocked when we peeked the data as we left the data
in the queue. */ in the queue. */
vTaskSuspend( NULL ); vTaskSuspend( NULL );
/* This time we are going to do the same as the above test, but the /* This time we are going to do the same as the above test, but the
high priority task is going to receive the data, rather than peek it. high priority task is going to receive the data, rather than peek it.
This means that the medium priority task should never peek the value. */ This means that the medium priority task should never peek the value. */
if( xQueuePeek( xQueue, &ulValue, portMAX_DELAY ) != pdPASS ) if( xQueuePeek( xQueue, &ulValue, portMAX_DELAY ) != pdPASS )
{ {
xErrorDetected = pdTRUE; xErrorDetected = pdTRUE;
} }
if( ulValue != 0xaabbaabb ) if( ulValue != 0xaabbaabb )
{ {
xErrorDetected = pdTRUE; xErrorDetected = pdTRUE;
} }
vTaskSuspend( NULL ); vTaskSuspend( NULL );
} }
} }
/*-----------------------------------------------------------*/ /*-----------------------------------------------------------*/
static void prvHighPriorityPeekTask( void *pvParameters ) static void prvHighPriorityPeekTask( void *pvParameters )
{ {
xQueueHandle xQueue = ( xQueueHandle ) pvParameters; xQueueHandle xQueue = ( xQueueHandle ) pvParameters;
unsigned portLONG ulValue; unsigned portLONG ulValue;
for( ;; ) for( ;; )
{ {
/* Try peeking from the queue. The queue should be empty so we will /* Try peeking from the queue. The queue should be empty so we will
block, allowing the medium priority task to execute. Both the high block, allowing the medium priority task to execute. Both the high
and highest priority tasks will then be blocked on the queue. */ and highest priority tasks will then be blocked on the queue. */
if( xQueuePeek( xQueue, &ulValue, portMAX_DELAY ) != pdPASS ) if( xQueuePeek( xQueue, &ulValue, portMAX_DELAY ) != pdPASS )
{ {
/* We expected to have received something by the time we unblock. */ /* We expected to have received something by the time we unblock. */
xErrorDetected = pdTRUE; xErrorDetected = pdTRUE;
} }
/* When we get here the highest priority task should have peeked the data /* When we get here the highest priority task should have peeked the data
(unblocking this task) then suspended (allowing this task to also peek (unblocking this task) then suspended (allowing this task to also peek
the data). */ the data). */
if( ulValue != 0x01234567 ) if( ulValue != 0x01234567 )
{ {
/* We did not receive the expected value. */ /* We did not receive the expected value. */
xErrorDetected = pdTRUE; xErrorDetected = pdTRUE;
} }
if( uxQueueMessagesWaiting( xQueue ) != 1 ) if( uxQueueMessagesWaiting( xQueue ) != 1 )
{ {
/* The message should have been left on the queue. */ /* The message should have been left on the queue. */
xErrorDetected = pdTRUE; xErrorDetected = pdTRUE;
} }
/* We only peeked the data, so suspending ourselves now should enable /* We only peeked the data, so suspending ourselves now should enable
the medium priority task to also peek the data. The medium priority task the medium priority task to also peek the data. The medium priority task
will have been unblocked when we peeked the data as we left the data will have been unblocked when we peeked the data as we left the data
in the queue. */ in the queue. */
vTaskSuspend( NULL ); vTaskSuspend( NULL );
/* This time we are going actually receive the value, so the medium /* This time we are going actually receive the value, so the medium
priority task will never peek the data - we removed it from the queue. */ priority task will never peek the data - we removed it from the queue. */
if( xQueueReceive( xQueue, &ulValue, portMAX_DELAY ) != pdPASS ) if( xQueueReceive( xQueue, &ulValue, portMAX_DELAY ) != pdPASS )
{ {
xErrorDetected = pdTRUE; xErrorDetected = pdTRUE;
} }
if( ulValue != 0xaabbaabb ) if( ulValue != 0xaabbaabb )
{ {
xErrorDetected = pdTRUE; xErrorDetected = pdTRUE;
} }
vTaskSuspend( NULL ); vTaskSuspend( NULL );
} }
} }
/*-----------------------------------------------------------*/ /*-----------------------------------------------------------*/
static void prvMediumPriorityPeekTask( void *pvParameters ) static void prvMediumPriorityPeekTask( void *pvParameters )
{ {
xQueueHandle xQueue = ( xQueueHandle ) pvParameters; xQueueHandle xQueue = ( xQueueHandle ) pvParameters;
unsigned portLONG ulValue; unsigned portLONG ulValue;
for( ;; ) for( ;; )
{ {
/* Try peeking from the queue. The queue should be empty so we will /* Try peeking from the queue. The queue should be empty so we will
block, allowing the low priority task to execute. The highest, high block, allowing the low priority task to execute. The highest, high
and medium priority tasks will then all be blocked on the queue. */ and medium priority tasks will then all be blocked on the queue. */
if( xQueuePeek( xQueue, &ulValue, portMAX_DELAY ) != pdPASS ) if( xQueuePeek( xQueue, &ulValue, portMAX_DELAY ) != pdPASS )
{ {
/* We expected to have received something by the time we unblock. */ /* We expected to have received something by the time we unblock. */
xErrorDetected = pdTRUE; xErrorDetected = pdTRUE;
} }
/* When we get here the high priority task should have peeked the data /* When we get here the high priority task should have peeked the data
(unblocking this task) then suspended (allowing this task to also peek (unblocking this task) then suspended (allowing this task to also peek
the data). */ the data). */
if( ulValue != 0x01234567 ) if( ulValue != 0x01234567 )
{ {
/* We did not receive the expected value. */ /* We did not receive the expected value. */
xErrorDetected = pdTRUE; xErrorDetected = pdTRUE;
} }
if( uxQueueMessagesWaiting( xQueue ) != 1 ) if( uxQueueMessagesWaiting( xQueue ) != 1 )
{ {
/* The message should have been left on the queue. */ /* The message should have been left on the queue. */
xErrorDetected = pdTRUE; xErrorDetected = pdTRUE;
} }
/* Just so we know the test is still running. */ /* Just so we know the test is still running. */
ulLoopCounter++; ulLoopCounter++;
/* Now we can suspend ourselves so the low priority task can execute /* Now we can suspend ourselves so the low priority task can execute
again. */ again. */
vTaskSuspend( NULL ); vTaskSuspend( NULL );
} }
} }
/*-----------------------------------------------------------*/ /*-----------------------------------------------------------*/
static void prvLowPriorityPeekTask( void *pvParameters ) static void prvLowPriorityPeekTask( void *pvParameters )
{ {
xQueueHandle xQueue = ( xQueueHandle ) pvParameters; xQueueHandle xQueue = ( xQueueHandle ) pvParameters;
unsigned portLONG ulValue; unsigned portLONG ulValue;
for( ;; ) for( ;; )
{ {
/* Write some data to the queue. This should unblock the highest /* Write some data to the queue. This should unblock the highest
priority task that is waiting to peek data from the queue. */ priority task that is waiting to peek data from the queue. */
ulValue = 0x11223344; ulValue = 0x11223344;
if( xQueueSendToBack( xQueue, &ulValue, qpeekNO_BLOCK ) != pdPASS ) if( xQueueSendToBack( xQueue, &ulValue, qpeekNO_BLOCK ) != pdPASS )
{ {
/* We were expecting the queue to be empty so we should not of /* We were expecting the queue to be empty so we should not of
had a problem writing to the queue. */ had a problem writing to the queue. */
xErrorDetected = pdTRUE; xErrorDetected = pdTRUE;
} }
/* By the time we get here the data should have been removed from /* By the time we get here the data should have been removed from
the queue. */ the queue. */
if( uxQueueMessagesWaiting( xQueue ) != 0 ) if( uxQueueMessagesWaiting( xQueue ) != 0 )
{ {
xErrorDetected = pdTRUE; xErrorDetected = pdTRUE;
} }
/* Write another value to the queue, again waking the highest priority /* Write another value to the queue, again waking the highest priority
task that is blocked on the queue. */ task that is blocked on the queue. */
ulValue = 0x01234567; ulValue = 0x01234567;
if( xQueueSendToBack( xQueue, &ulValue, qpeekNO_BLOCK ) != pdPASS ) if( xQueueSendToBack( xQueue, &ulValue, qpeekNO_BLOCK ) != pdPASS )
{ {
/* We were expecting the queue to be empty so we should not of /* We were expecting the queue to be empty so we should not of
had a problem writing to the queue. */ had a problem writing to the queue. */
xErrorDetected = pdTRUE; xErrorDetected = pdTRUE;
} }
/* All the other tasks should now have successfully peeked the data. /* All the other tasks should now have successfully peeked the data.
The data is still in the queue so we should be able to receive it. */ The data is still in the queue so we should be able to receive it. */
ulValue = 0; ulValue = 0;
if( xQueueReceive( xQueue, &ulValue, qpeekNO_BLOCK ) != pdPASS ) if( xQueueReceive( xQueue, &ulValue, qpeekNO_BLOCK ) != pdPASS )
{ {
/* We expected to receive the data. */ /* We expected to receive the data. */
xErrorDetected = pdTRUE; xErrorDetected = pdTRUE;
} }
if( ulValue != 0x01234567 ) if( ulValue != 0x01234567 )
{ {
/* We did not receive the expected value. */ /* We did not receive the expected value. */
} }
/* Lets just delay a while as this is an intensive test as we don't /* Lets just delay a while as this is an intensive test as we don't
want to starve other tests of processing time. */ want to starve other tests of processing time. */
vTaskDelay( qpeekSHORT_DELAY ); vTaskDelay( qpeekSHORT_DELAY );
/* Unsuspend the other tasks so we can repeat the test - this time /* Unsuspend the other tasks so we can repeat the test - this time
however not all the other tasks will peek the data as the high however not all the other tasks will peek the data as the high
priority task is actually going to remove it from the queue. Send priority task is actually going to remove it from the queue. Send
to front is used just to be different. As the queue is empty it to front is used just to be different. As the queue is empty it
makes no difference to the result. */ makes no difference to the result. */
vTaskResume( xMediumPriorityTask ); vTaskResume( xMediumPriorityTask );
vTaskResume( xHighPriorityTask ); vTaskResume( xHighPriorityTask );
vTaskResume( xHighestPriorityTask ); vTaskResume( xHighestPriorityTask );
ulValue = 0xaabbaabb; ulValue = 0xaabbaabb;
if( xQueueSendToFront( xQueue, &ulValue, qpeekNO_BLOCK ) != pdPASS ) if( xQueueSendToFront( xQueue, &ulValue, qpeekNO_BLOCK ) != pdPASS )
{ {
/* We were expecting the queue to be empty so we should not of /* We were expecting the queue to be empty so we should not of
had a problem writing to the queue. */ had a problem writing to the queue. */
xErrorDetected = pdTRUE; xErrorDetected = pdTRUE;
} }
/* This time we should find that the queue is empty. The high priority /* This time we should find that the queue is empty. The high priority
task actually removed the data rather than just peeking it. */ task actually removed the data rather than just peeking it. */
if( xQueuePeek( xQueue, &ulValue, qpeekNO_BLOCK ) != errQUEUE_EMPTY ) if( xQueuePeek( xQueue, &ulValue, qpeekNO_BLOCK ) != errQUEUE_EMPTY )
{ {
/* We expected to receive the data. */ /* We expected to receive the data. */
xErrorDetected = pdTRUE; xErrorDetected = pdTRUE;
} }
/* Unsuspend the highest and high priority tasks so we can go back /* Unsuspend the highest and high priority tasks so we can go back
and repeat the whole thing. The medium priority task should not be and repeat the whole thing. The medium priority task should not be
suspended as it was not able to peek the data in this last case. */ suspended as it was not able to peek the data in this last case. */
vTaskResume( xHighPriorityTask ); vTaskResume( xHighPriorityTask );
vTaskResume( xHighestPriorityTask ); vTaskResume( xHighestPriorityTask );
/* Lets just delay a while as this is an intensive test as we don't /* Lets just delay a while as this is an intensive test as we don't
want to starve other tests of processing time. */ want to starve other tests of processing time. */
vTaskDelay( qpeekSHORT_DELAY ); vTaskDelay( qpeekSHORT_DELAY );
} }
} }
/*-----------------------------------------------------------*/ /*-----------------------------------------------------------*/
/* This is called to check that all the created tasks are still running. */ /* This is called to check that all the created tasks are still running. */
portBASE_TYPE xAreQueuePeekTasksStillRunning( void ) portBASE_TYPE xAreQueuePeekTasksStillRunning( void )
{ {
static unsigned portLONG ulLastLoopCounter = 0; static unsigned portLONG ulLastLoopCounter = 0;
/* If the demo task is still running then we expect the loopcounter to /* If the demo task is still running then we expect the loopcounter to
have incremented since this function was last called. */ have incremented since this function was last called. */
if( ulLastLoopCounter == ulLoopCounter ) if( ulLastLoopCounter == ulLoopCounter )
{ {
xErrorDetected = pdTRUE; xErrorDetected = pdTRUE;
} }
ulLastLoopCounter = ulLoopCounter; ulLastLoopCounter = ulLoopCounter;
/* Errors detected in the task itself will have latched xErrorDetected /* Errors detected in the task itself will have latched xErrorDetected
to true. */ to true. */
return !xErrorDetected; return !xErrorDetected;
} }

View File

@ -1,476 +1,476 @@
/* /*
FreeRTOS.org V5.0.0 - Copyright (C) 2003-2008 Richard Barry. FreeRTOS.org V5.0.0 - Copyright (C) 2003-2008 Richard Barry.
This file is part of the FreeRTOS.org distribution. This file is part of the FreeRTOS.org distribution.
FreeRTOS.org is free software; you can redistribute it and/or modify FreeRTOS.org is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or the Free Software Foundation; either version 2 of the License, or
(at your option) any later version. (at your option) any later version.
FreeRTOS.org is distributed in the hope that it will be useful, FreeRTOS.org is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details. GNU General Public License for more details.
You should have received a copy of the GNU General Public License You should have received a copy of the GNU General Public License
along with FreeRTOS.org; if not, write to the Free Software along with FreeRTOS.org; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
A special exception to the GPL can be applied should you wish to distribute A special exception to the GPL can be applied should you wish to distribute
a combined work that includes FreeRTOS.org, without being obliged to provide a combined work that includes FreeRTOS.org, without being obliged to provide
the source code for any proprietary components. See the licensing section the source code for any proprietary components. See the licensing section
of http://www.FreeRTOS.org for full details of how and when the exception of http://www.FreeRTOS.org for full details of how and when the exception
can be applied. can be applied.
*************************************************************************** ***************************************************************************
*************************************************************************** ***************************************************************************
* * * *
* SAVE TIME AND MONEY! We can port FreeRTOS.org to your own hardware, * * SAVE TIME AND MONEY! We can port FreeRTOS.org to your own hardware, *
* and even write all or part of your application on your behalf. * * and even write all or part of your application on your behalf. *
* See http://www.OpenRTOS.com for details of the services we provide to * * See http://www.OpenRTOS.com for details of the services we provide to *
* expedite your project. * * expedite your project. *
* * * *
*************************************************************************** ***************************************************************************
*************************************************************************** ***************************************************************************
Please ensure to read the configuration and relevant port sections of the Please ensure to read the configuration and relevant port sections of the
online documentation. online documentation.
http://www.FreeRTOS.org - Documentation, latest information, license and http://www.FreeRTOS.org - Documentation, latest information, license and
contact details. contact details.
http://www.SafeRTOS.com - A version that is certified for use in safety http://www.SafeRTOS.com - A version that is certified for use in safety
critical systems. critical systems.
http://www.OpenRTOS.com - Commercial support, development, porting, http://www.OpenRTOS.com - Commercial support, development, porting,
licensing and training services. licensing and training services.
*/ */
/* /*
* This file contains some test scenarios that ensure tasks do not exit queue * This file contains some test scenarios that ensure tasks do not exit queue
* send or receive functions prematurely. A description of the tests is * send or receive functions prematurely. A description of the tests is
* included within the code. * included within the code.
*/ */
/* Kernel includes. */ /* Kernel includes. */
#include "FreeRTOS.h" #include "FreeRTOS.h"
#include "task.h" #include "task.h"
#include "queue.h" #include "queue.h"
/* Demo includes. */ /* Demo includes. */
#include "blocktim.h" #include "blocktim.h"
/* Task priorities. */ /* Task priorities. */
#define bktPRIMARY_PRIORITY ( 3 ) #define bktPRIMARY_PRIORITY ( 3 )
#define bktSECONDARY_PRIORITY ( 2 ) #define bktSECONDARY_PRIORITY ( 2 )
/* Task behaviour. */ /* Task behaviour. */
#define bktQUEUE_LENGTH ( 5 ) #define bktQUEUE_LENGTH ( 5 )
#define bktSHORT_WAIT ( ( ( portTickType ) 20 ) / portTICK_RATE_MS ) #define bktSHORT_WAIT ( ( ( portTickType ) 20 ) / portTICK_RATE_MS )
#define bktPRIMARY_BLOCK_TIME ( 10 ) #define bktPRIMARY_BLOCK_TIME ( 10 )
#define bktALLOWABLE_MARGIN ( 15 ) #define bktALLOWABLE_MARGIN ( 15 )
#define bktTIME_TO_BLOCK ( 175 ) #define bktTIME_TO_BLOCK ( 175 )
#define bktDONT_BLOCK ( ( portTickType ) 0 ) #define bktDONT_BLOCK ( ( portTickType ) 0 )
#define bktRUN_INDICATOR ( ( unsigned portBASE_TYPE ) 0x55 ) #define bktRUN_INDICATOR ( ( unsigned portBASE_TYPE ) 0x55 )
/* The queue on which the tasks block. */ /* The queue on which the tasks block. */
static xQueueHandle xTestQueue; static xQueueHandle xTestQueue;
/* Handle to the secondary task is required by the primary task for calls /* Handle to the secondary task is required by the primary task for calls
to vTaskSuspend/Resume(). */ to vTaskSuspend/Resume(). */
static xTaskHandle xSecondary; static xTaskHandle xSecondary;
/* Used to ensure that tasks are still executing without error. */ /* Used to ensure that tasks are still executing without error. */
static portBASE_TYPE xPrimaryCycles = 0, xSecondaryCycles = 0; static volatile portBASE_TYPE xPrimaryCycles = 0, xSecondaryCycles = 0;
static portBASE_TYPE xErrorOccurred = pdFALSE; static volatile portBASE_TYPE xErrorOccurred = pdFALSE;
/* Provides a simple mechanism for the primary task to know when the /* Provides a simple mechanism for the primary task to know when the
secondary task has executed. */ secondary task has executed. */
static volatile unsigned portBASE_TYPE xRunIndicator; static volatile unsigned portBASE_TYPE xRunIndicator;
/* The two test tasks. Their behaviour is commented within the files. */ /* The two test tasks. Their behaviour is commented within the files. */
static void vPrimaryBlockTimeTestTask( void *pvParameters ); static void vPrimaryBlockTimeTestTask( void *pvParameters );
static void vSecondaryBlockTimeTestTask( void *pvParameters ); static void vSecondaryBlockTimeTestTask( void *pvParameters );
/*-----------------------------------------------------------*/ /*-----------------------------------------------------------*/
void vCreateBlockTimeTasks( void ) void vCreateBlockTimeTasks( void )
{ {
/* Create the queue on which the two tasks block. */ /* Create the queue on which the two tasks block. */
xTestQueue = xQueueCreate( bktQUEUE_LENGTH, sizeof( portBASE_TYPE ) ); xTestQueue = xQueueCreate( bktQUEUE_LENGTH, sizeof( portBASE_TYPE ) );
/* Create the two test tasks. */ /* Create the two test tasks. */
xTaskCreate( vPrimaryBlockTimeTestTask, ( signed portCHAR * )"BTest1", configMINIMAL_STACK_SIZE, NULL, bktPRIMARY_PRIORITY, NULL ); xTaskCreate( vPrimaryBlockTimeTestTask, ( signed portCHAR * )"BTest1", configMINIMAL_STACK_SIZE, NULL, bktPRIMARY_PRIORITY, NULL );
xTaskCreate( vSecondaryBlockTimeTestTask, ( signed portCHAR * )"BTest2", configMINIMAL_STACK_SIZE, NULL, bktSECONDARY_PRIORITY, &xSecondary ); xTaskCreate( vSecondaryBlockTimeTestTask, ( signed portCHAR * )"BTest2", configMINIMAL_STACK_SIZE, NULL, bktSECONDARY_PRIORITY, &xSecondary );
} }
/*-----------------------------------------------------------*/ /*-----------------------------------------------------------*/
static void vPrimaryBlockTimeTestTask( void *pvParameters ) static void vPrimaryBlockTimeTestTask( void *pvParameters )
{ {
portBASE_TYPE xItem, xData; portBASE_TYPE xItem, xData;
portTickType xTimeWhenBlocking; portTickType xTimeWhenBlocking;
portTickType xTimeToBlock, xBlockedTime; portTickType xTimeToBlock, xBlockedTime;
( void ) pvParameters; ( void ) pvParameters;
for( ;; ) for( ;; )
{ {
/********************************************************************* /*********************************************************************
Test 1 Test 1
Simple block time wakeup test on queue receives. */ Simple block time wakeup test on queue receives. */
for( xItem = 0; xItem < bktQUEUE_LENGTH; xItem++ ) for( xItem = 0; xItem < bktQUEUE_LENGTH; xItem++ )
{ {
/* The queue is empty. Attempt to read from the queue using a block /* The queue is empty. Attempt to read from the queue using a block
time. When we wake, ensure the delta in time is as expected. */ time. When we wake, ensure the delta in time is as expected. */
xTimeToBlock = bktPRIMARY_BLOCK_TIME << xItem; xTimeToBlock = bktPRIMARY_BLOCK_TIME << xItem;
xTimeWhenBlocking = xTaskGetTickCount(); xTimeWhenBlocking = xTaskGetTickCount();
/* We should unblock after xTimeToBlock having not received /* We should unblock after xTimeToBlock having not received
anything on the queue. */ anything on the queue. */
if( xQueueReceive( xTestQueue, &xData, xTimeToBlock ) != errQUEUE_EMPTY ) if( xQueueReceive( xTestQueue, &xData, xTimeToBlock ) != errQUEUE_EMPTY )
{ {
xErrorOccurred = pdTRUE; xErrorOccurred = pdTRUE;
} }
/* How long were we blocked for? */ /* How long were we blocked for? */
xBlockedTime = xTaskGetTickCount() - xTimeWhenBlocking; xBlockedTime = xTaskGetTickCount() - xTimeWhenBlocking;
if( xBlockedTime < xTimeToBlock ) if( xBlockedTime < xTimeToBlock )
{ {
/* Should not have blocked for less than we requested. */ /* Should not have blocked for less than we requested. */
xErrorOccurred = pdTRUE; xErrorOccurred = pdTRUE;
} }
if( xBlockedTime > ( xTimeToBlock + bktALLOWABLE_MARGIN ) ) if( xBlockedTime > ( xTimeToBlock + bktALLOWABLE_MARGIN ) )
{ {
/* Should not have blocked for longer than we requested, /* Should not have blocked for longer than we requested,
although we would not necessarily run as soon as we were although we would not necessarily run as soon as we were
unblocked so a margin is allowed. */ unblocked so a margin is allowed. */
xErrorOccurred = pdTRUE; xErrorOccurred = pdTRUE;
} }
} }
/********************************************************************* /*********************************************************************
Test 2 Test 2
Simple block time wakeup test on queue sends. Simple block time wakeup test on queue sends.
First fill the queue. It should be empty so all sends should pass. */ First fill the queue. It should be empty so all sends should pass. */
for( xItem = 0; xItem < bktQUEUE_LENGTH; xItem++ ) for( xItem = 0; xItem < bktQUEUE_LENGTH; xItem++ )
{ {
if( xQueueSend( xTestQueue, &xItem, bktDONT_BLOCK ) != pdPASS ) if( xQueueSend( xTestQueue, &xItem, bktDONT_BLOCK ) != pdPASS )
{ {
xErrorOccurred = pdTRUE; xErrorOccurred = pdTRUE;
} }
#if configUSE_PREEMPTION == 0 #if configUSE_PREEMPTION == 0
taskYIELD(); taskYIELD();
#endif #endif
} }
for( xItem = 0; xItem < bktQUEUE_LENGTH; xItem++ ) for( xItem = 0; xItem < bktQUEUE_LENGTH; xItem++ )
{ {
/* The queue is full. Attempt to write to the queue using a block /* The queue is full. Attempt to write to the queue using a block
time. When we wake, ensure the delta in time is as expected. */ time. When we wake, ensure the delta in time is as expected. */
xTimeToBlock = bktPRIMARY_BLOCK_TIME << xItem; xTimeToBlock = bktPRIMARY_BLOCK_TIME << xItem;
xTimeWhenBlocking = xTaskGetTickCount(); xTimeWhenBlocking = xTaskGetTickCount();
/* We should unblock after xTimeToBlock having not received /* We should unblock after xTimeToBlock having not received
anything on the queue. */ anything on the queue. */
if( xQueueSend( xTestQueue, &xItem, xTimeToBlock ) != errQUEUE_FULL ) if( xQueueSend( xTestQueue, &xItem, xTimeToBlock ) != errQUEUE_FULL )
{ {
xErrorOccurred = pdTRUE; xErrorOccurred = pdTRUE;
} }
/* How long were we blocked for? */ /* How long were we blocked for? */
xBlockedTime = xTaskGetTickCount() - xTimeWhenBlocking; xBlockedTime = xTaskGetTickCount() - xTimeWhenBlocking;
if( xBlockedTime < xTimeToBlock ) if( xBlockedTime < xTimeToBlock )
{ {
/* Should not have blocked for less than we requested. */ /* Should not have blocked for less than we requested. */
xErrorOccurred = pdTRUE; xErrorOccurred = pdTRUE;
} }
if( xBlockedTime > ( xTimeToBlock + bktALLOWABLE_MARGIN ) ) if( xBlockedTime > ( xTimeToBlock + bktALLOWABLE_MARGIN ) )
{ {
/* Should not have blocked for longer than we requested, /* Should not have blocked for longer than we requested,
although we would not necessarily run as soon as we were although we would not necessarily run as soon as we were
unblocked so a margin is allowed. */ unblocked so a margin is allowed. */
xErrorOccurred = pdTRUE; xErrorOccurred = pdTRUE;
} }
} }
/********************************************************************* /*********************************************************************
Test 3 Test 3
Wake the other task, it will block attempting to post to the queue. Wake the other task, it will block attempting to post to the queue.
When we read from the queue the other task will wake, but before it When we read from the queue the other task will wake, but before it
can run we will post to the queue again. When the other task runs it can run we will post to the queue again. When the other task runs it
will find the queue still full, even though it was woken. It should will find the queue still full, even though it was woken. It should
recognise that its block time has not expired and return to block for recognise that its block time has not expired and return to block for
the remains of its block time. the remains of its block time.
Wake the other task so it blocks attempting to post to the already Wake the other task so it blocks attempting to post to the already
full queue. */ full queue. */
xRunIndicator = 0; xRunIndicator = 0;
vTaskResume( xSecondary ); vTaskResume( xSecondary );
/* We need to wait a little to ensure the other task executes. */ /* We need to wait a little to ensure the other task executes. */
while( xRunIndicator != bktRUN_INDICATOR ) while( xRunIndicator != bktRUN_INDICATOR )
{ {
/* The other task has not yet executed. */ /* The other task has not yet executed. */
vTaskDelay( bktSHORT_WAIT ); vTaskDelay( bktSHORT_WAIT );
} }
/* Make sure the other task is blocked on the queue. */ /* Make sure the other task is blocked on the queue. */
vTaskDelay( bktSHORT_WAIT ); vTaskDelay( bktSHORT_WAIT );
xRunIndicator = 0; xRunIndicator = 0;
for( xItem = 0; xItem < bktQUEUE_LENGTH; xItem++ ) for( xItem = 0; xItem < bktQUEUE_LENGTH; xItem++ )
{ {
/* Now when we make space on the queue the other task should wake /* Now when we make space on the queue the other task should wake
but not execute as this task has higher priority. */ but not execute as this task has higher priority. */
if( xQueueReceive( xTestQueue, &xData, bktDONT_BLOCK ) != pdPASS ) if( xQueueReceive( xTestQueue, &xData, bktDONT_BLOCK ) != pdPASS )
{ {
xErrorOccurred = pdTRUE; xErrorOccurred = pdTRUE;
} }
/* Now fill the queue again before the other task gets a chance to /* Now fill the queue again before the other task gets a chance to
execute. If the other task had executed we would find the queue execute. If the other task had executed we would find the queue
full ourselves, and the other task have set xRunIndicator. */ full ourselves, and the other task have set xRunIndicator. */
if( xQueueSend( xTestQueue, &xItem, bktDONT_BLOCK ) != pdPASS ) if( xQueueSend( xTestQueue, &xItem, bktDONT_BLOCK ) != pdPASS )
{ {
xErrorOccurred = pdTRUE; xErrorOccurred = pdTRUE;
} }
if( xRunIndicator == bktRUN_INDICATOR ) if( xRunIndicator == bktRUN_INDICATOR )
{ {
/* The other task should not have executed. */ /* The other task should not have executed. */
xErrorOccurred = pdTRUE; xErrorOccurred = pdTRUE;
} }
/* Raise the priority of the other task so it executes and blocks /* Raise the priority of the other task so it executes and blocks
on the queue again. */ on the queue again. */
vTaskPrioritySet( xSecondary, bktPRIMARY_PRIORITY + 2 ); vTaskPrioritySet( xSecondary, bktPRIMARY_PRIORITY + 2 );
/* The other task should now have re-blocked without exiting the /* The other task should now have re-blocked without exiting the
queue function. */ queue function. */
if( xRunIndicator == bktRUN_INDICATOR ) if( xRunIndicator == bktRUN_INDICATOR )
{ {
/* The other task should not have executed outside of the /* The other task should not have executed outside of the
queue function. */ queue function. */
xErrorOccurred = pdTRUE; xErrorOccurred = pdTRUE;
} }
/* Set the priority back down. */ /* Set the priority back down. */
vTaskPrioritySet( xSecondary, bktSECONDARY_PRIORITY ); vTaskPrioritySet( xSecondary, bktSECONDARY_PRIORITY );
} }
/* Let the other task timeout. When it unblockes it will check that it /* Let the other task timeout. When it unblockes it will check that it
unblocked at the correct time, then suspend itself. */ unblocked at the correct time, then suspend itself. */
while( xRunIndicator != bktRUN_INDICATOR ) while( xRunIndicator != bktRUN_INDICATOR )
{ {
vTaskDelay( bktSHORT_WAIT ); vTaskDelay( bktSHORT_WAIT );
} }
vTaskDelay( bktSHORT_WAIT ); vTaskDelay( bktSHORT_WAIT );
xRunIndicator = 0; xRunIndicator = 0;
/********************************************************************* /*********************************************************************
Test 4 Test 4
As per test 3 - but with the send and receive the other way around. As per test 3 - but with the send and receive the other way around.
The other task blocks attempting to read from the queue. The other task blocks attempting to read from the queue.
Empty the queue. We should find that it is full. */ Empty the queue. We should find that it is full. */
for( xItem = 0; xItem < bktQUEUE_LENGTH; xItem++ ) for( xItem = 0; xItem < bktQUEUE_LENGTH; xItem++ )
{ {
if( xQueueReceive( xTestQueue, &xData, bktDONT_BLOCK ) != pdPASS ) if( xQueueReceive( xTestQueue, &xData, bktDONT_BLOCK ) != pdPASS )
{ {
xErrorOccurred = pdTRUE; xErrorOccurred = pdTRUE;
} }
} }
/* Wake the other task so it blocks attempting to read from the /* Wake the other task so it blocks attempting to read from the
already empty queue. */ already empty queue. */
vTaskResume( xSecondary ); vTaskResume( xSecondary );
/* We need to wait a little to ensure the other task executes. */ /* We need to wait a little to ensure the other task executes. */
while( xRunIndicator != bktRUN_INDICATOR ) while( xRunIndicator != bktRUN_INDICATOR )
{ {
vTaskDelay( bktSHORT_WAIT ); vTaskDelay( bktSHORT_WAIT );
} }
vTaskDelay( bktSHORT_WAIT ); vTaskDelay( bktSHORT_WAIT );
xRunIndicator = 0; xRunIndicator = 0;
for( xItem = 0; xItem < bktQUEUE_LENGTH; xItem++ ) for( xItem = 0; xItem < bktQUEUE_LENGTH; xItem++ )
{ {
/* Now when we place an item on the queue the other task should /* Now when we place an item on the queue the other task should
wake but not execute as this task has higher priority. */ wake but not execute as this task has higher priority. */
if( xQueueSend( xTestQueue, &xItem, bktDONT_BLOCK ) != pdPASS ) if( xQueueSend( xTestQueue, &xItem, bktDONT_BLOCK ) != pdPASS )
{ {
xErrorOccurred = pdTRUE; xErrorOccurred = pdTRUE;
} }
/* Now empty the queue again before the other task gets a chance to /* Now empty the queue again before the other task gets a chance to
execute. If the other task had executed we would find the queue execute. If the other task had executed we would find the queue
empty ourselves, and the other task would be suspended. */ empty ourselves, and the other task would be suspended. */
if( xQueueReceive( xTestQueue, &xData, bktDONT_BLOCK ) != pdPASS ) if( xQueueReceive( xTestQueue, &xData, bktDONT_BLOCK ) != pdPASS )
{ {
xErrorOccurred = pdTRUE; xErrorOccurred = pdTRUE;
} }
if( xRunIndicator == bktRUN_INDICATOR ) if( xRunIndicator == bktRUN_INDICATOR )
{ {
/* The other task should not have executed. */ /* The other task should not have executed. */
xErrorOccurred = pdTRUE; xErrorOccurred = pdTRUE;
} }
/* Raise the priority of the other task so it executes and blocks /* Raise the priority of the other task so it executes and blocks
on the queue again. */ on the queue again. */
vTaskPrioritySet( xSecondary, bktPRIMARY_PRIORITY + 2 ); vTaskPrioritySet( xSecondary, bktPRIMARY_PRIORITY + 2 );
/* The other task should now have re-blocked without exiting the /* The other task should now have re-blocked without exiting the
queue function. */ queue function. */
if( xRunIndicator == bktRUN_INDICATOR ) if( xRunIndicator == bktRUN_INDICATOR )
{ {
/* The other task should not have executed outside of the /* The other task should not have executed outside of the
queue function. */ queue function. */
xErrorOccurred = pdTRUE; xErrorOccurred = pdTRUE;
} }
vTaskPrioritySet( xSecondary, bktSECONDARY_PRIORITY ); vTaskPrioritySet( xSecondary, bktSECONDARY_PRIORITY );
} }
/* Let the other task timeout. When it unblockes it will check that it /* Let the other task timeout. When it unblockes it will check that it
unblocked at the correct time, then suspend itself. */ unblocked at the correct time, then suspend itself. */
while( xRunIndicator != bktRUN_INDICATOR ) while( xRunIndicator != bktRUN_INDICATOR )
{ {
vTaskDelay( bktSHORT_WAIT ); vTaskDelay( bktSHORT_WAIT );
} }
vTaskDelay( bktSHORT_WAIT ); vTaskDelay( bktSHORT_WAIT );
xPrimaryCycles++; xPrimaryCycles++;
} }
} }
/*-----------------------------------------------------------*/ /*-----------------------------------------------------------*/
static void vSecondaryBlockTimeTestTask( void *pvParameters ) static void vSecondaryBlockTimeTestTask( void *pvParameters )
{ {
portTickType xTimeWhenBlocking, xBlockedTime; portTickType xTimeWhenBlocking, xBlockedTime;
portBASE_TYPE xData; portBASE_TYPE xData;
( void ) pvParameters; ( void ) pvParameters;
for( ;; ) for( ;; )
{ {
/********************************************************************* /*********************************************************************
Test 1 and 2 Test 1 and 2
This task does does not participate in these tests. */ This task does does not participate in these tests. */
vTaskSuspend( NULL ); vTaskSuspend( NULL );
/********************************************************************* /*********************************************************************
Test 3 Test 3
The first thing we do is attempt to read from the queue. It should be The first thing we do is attempt to read from the queue. It should be
full so we block. Note the time before we block so we can check the full so we block. Note the time before we block so we can check the
wake time is as per that expected. */ wake time is as per that expected. */
xTimeWhenBlocking = xTaskGetTickCount(); xTimeWhenBlocking = xTaskGetTickCount();
/* We should unblock after bktTIME_TO_BLOCK having not received /* We should unblock after bktTIME_TO_BLOCK having not received
anything on the queue. */ anything on the queue. */
xData = 0; xData = 0;
xRunIndicator = bktRUN_INDICATOR; xRunIndicator = bktRUN_INDICATOR;
if( xQueueSend( xTestQueue, &xData, bktTIME_TO_BLOCK ) != errQUEUE_FULL ) if( xQueueSend( xTestQueue, &xData, bktTIME_TO_BLOCK ) != errQUEUE_FULL )
{ {
xErrorOccurred = pdTRUE; xErrorOccurred = pdTRUE;
} }
/* How long were we inside the send function? */ /* How long were we inside the send function? */
xBlockedTime = xTaskGetTickCount() - xTimeWhenBlocking; xBlockedTime = xTaskGetTickCount() - xTimeWhenBlocking;
/* We should not have blocked for less time than bktTIME_TO_BLOCK. */ /* We should not have blocked for less time than bktTIME_TO_BLOCK. */
if( xBlockedTime < bktTIME_TO_BLOCK ) if( xBlockedTime < bktTIME_TO_BLOCK )
{ {
xErrorOccurred = pdTRUE; xErrorOccurred = pdTRUE;
} }
/* We should of not blocked for much longer than bktALLOWABLE_MARGIN /* We should of not blocked for much longer than bktALLOWABLE_MARGIN
either. A margin is permitted as we would not necessarily run as either. A margin is permitted as we would not necessarily run as
soon as we unblocked. */ soon as we unblocked. */
if( xBlockedTime > ( bktTIME_TO_BLOCK + bktALLOWABLE_MARGIN ) ) if( xBlockedTime > ( bktTIME_TO_BLOCK + bktALLOWABLE_MARGIN ) )
{ {
xErrorOccurred = pdTRUE; xErrorOccurred = pdTRUE;
} }
/* Suspend ready for test 3. */ /* Suspend ready for test 3. */
xRunIndicator = bktRUN_INDICATOR; xRunIndicator = bktRUN_INDICATOR;
vTaskSuspend( NULL ); vTaskSuspend( NULL );
/********************************************************************* /*********************************************************************
Test 4 Test 4
As per test three, but with the send and receive reversed. */ As per test three, but with the send and receive reversed. */
xTimeWhenBlocking = xTaskGetTickCount(); xTimeWhenBlocking = xTaskGetTickCount();
/* We should unblock after bktTIME_TO_BLOCK having not received /* We should unblock after bktTIME_TO_BLOCK having not received
anything on the queue. */ anything on the queue. */
xRunIndicator = bktRUN_INDICATOR; xRunIndicator = bktRUN_INDICATOR;
if( xQueueReceive( xTestQueue, &xData, bktTIME_TO_BLOCK ) != errQUEUE_EMPTY ) if( xQueueReceive( xTestQueue, &xData, bktTIME_TO_BLOCK ) != errQUEUE_EMPTY )
{ {
xErrorOccurred = pdTRUE; xErrorOccurred = pdTRUE;
} }
xBlockedTime = xTaskGetTickCount() - xTimeWhenBlocking; xBlockedTime = xTaskGetTickCount() - xTimeWhenBlocking;
/* We should not have blocked for less time than bktTIME_TO_BLOCK. */ /* We should not have blocked for less time than bktTIME_TO_BLOCK. */
if( xBlockedTime < bktTIME_TO_BLOCK ) if( xBlockedTime < bktTIME_TO_BLOCK )
{ {
xErrorOccurred = pdTRUE; xErrorOccurred = pdTRUE;
} }
/* We should of not blocked for much longer than bktALLOWABLE_MARGIN /* We should of not blocked for much longer than bktALLOWABLE_MARGIN
either. A margin is permitted as we would not necessarily run as soon either. A margin is permitted as we would not necessarily run as soon
as we unblocked. */ as we unblocked. */
if( xBlockedTime > ( bktTIME_TO_BLOCK + bktALLOWABLE_MARGIN ) ) if( xBlockedTime > ( bktTIME_TO_BLOCK + bktALLOWABLE_MARGIN ) )
{ {
xErrorOccurred = pdTRUE; xErrorOccurred = pdTRUE;
} }
xRunIndicator = bktRUN_INDICATOR; xRunIndicator = bktRUN_INDICATOR;
xSecondaryCycles++; xSecondaryCycles++;
} }
} }
/*-----------------------------------------------------------*/ /*-----------------------------------------------------------*/
portBASE_TYPE xAreBlockTimeTestTasksStillRunning( void ) portBASE_TYPE xAreBlockTimeTestTasksStillRunning( void )
{ {
static portBASE_TYPE xLastPrimaryCycleCount = 0, xLastSecondaryCycleCount = 0; static portBASE_TYPE xLastPrimaryCycleCount = 0, xLastSecondaryCycleCount = 0;
portBASE_TYPE xReturn = pdPASS; portBASE_TYPE xReturn = pdPASS;
/* Have both tasks performed at least one cycle since this function was /* Have both tasks performed at least one cycle since this function was
last called? */ last called? */
if( xPrimaryCycles == xLastPrimaryCycleCount ) if( xPrimaryCycles == xLastPrimaryCycleCount )
{ {
xReturn = pdFAIL; xReturn = pdFAIL;
} }
if( xSecondaryCycles == xLastSecondaryCycleCount ) if( xSecondaryCycles == xLastSecondaryCycleCount )
{ {
xReturn = pdFAIL; xReturn = pdFAIL;
} }
if( xErrorOccurred == pdTRUE ) if( xErrorOccurred == pdTRUE )
{ {
xReturn = pdFAIL; xReturn = pdFAIL;
} }
xLastSecondaryCycleCount = xSecondaryCycles; xLastSecondaryCycleCount = xSecondaryCycles;
xLastPrimaryCycleCount = xPrimaryCycles; xLastPrimaryCycleCount = xPrimaryCycles;
return xReturn; return xReturn;
} }

View File

@ -1,296 +1,296 @@
/* /*
FreeRTOS.org V5.0.0 - Copyright (C) 2003-2008 Richard Barry. FreeRTOS.org V5.0.0 - Copyright (C) 2003-2008 Richard Barry.
This file is part of the FreeRTOS.org distribution. This file is part of the FreeRTOS.org distribution.
FreeRTOS.org is free software; you can redistribute it and/or modify FreeRTOS.org is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or the Free Software Foundation; either version 2 of the License, or
(at your option) any later version. (at your option) any later version.
FreeRTOS.org is distributed in the hope that it will be useful, FreeRTOS.org is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details. GNU General Public License for more details.
You should have received a copy of the GNU General Public License You should have received a copy of the GNU General Public License
along with FreeRTOS.org; if not, write to the Free Software along with FreeRTOS.org; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
A special exception to the GPL can be applied should you wish to distribute A special exception to the GPL can be applied should you wish to distribute
a combined work that includes FreeRTOS.org, without being obliged to provide a combined work that includes FreeRTOS.org, without being obliged to provide
the source code for any proprietary components. See the licensing section the source code for any proprietary components. See the licensing section
of http://www.FreeRTOS.org for full details of how and when the exception of http://www.FreeRTOS.org for full details of how and when the exception
can be applied. can be applied.
*************************************************************************** ***************************************************************************
*************************************************************************** ***************************************************************************
* * * *
* SAVE TIME AND MONEY! We can port FreeRTOS.org to your own hardware, * * SAVE TIME AND MONEY! We can port FreeRTOS.org to your own hardware, *
* and even write all or part of your application on your behalf. * * and even write all or part of your application on your behalf. *
* See http://www.OpenRTOS.com for details of the services we provide to * * See http://www.OpenRTOS.com for details of the services we provide to *
* expedite your project. * * expedite your project. *
* * * *
*************************************************************************** ***************************************************************************
*************************************************************************** ***************************************************************************
Please ensure to read the configuration and relevant port sections of the Please ensure to read the configuration and relevant port sections of the
online documentation. online documentation.
http://www.FreeRTOS.org - Documentation, latest information, license and http://www.FreeRTOS.org - Documentation, latest information, license and
contact details. contact details.
http://www.SafeRTOS.com - A version that is certified for use in safety http://www.SafeRTOS.com - A version that is certified for use in safety
critical systems. critical systems.
http://www.OpenRTOS.com - Commercial support, development, porting, http://www.OpenRTOS.com - Commercial support, development, porting,
licensing and training services. licensing and training services.
*/ */
/* /*
* Simple demonstration of the usage of counting semaphore. * Simple demonstration of the usage of counting semaphore.
*/ */
/* Scheduler include files. */ /* Scheduler include files. */
#include "FreeRTOS.h" #include "FreeRTOS.h"
#include "task.h" #include "task.h"
#include "semphr.h" #include "semphr.h"
/* Demo program include files. */ /* Demo program include files. */
#include "countsem.h" #include "countsem.h"
/* The maximum count value that the semaphore used for the demo can hold. */ /* The maximum count value that the semaphore used for the demo can hold. */
#define countMAX_COUNT_VALUE ( 200 ) #define countMAX_COUNT_VALUE ( 200 )
/* Constants used to indicate whether or not the semaphore should have been /* Constants used to indicate whether or not the semaphore should have been
created with its maximum count value, or its minimum count value. These created with its maximum count value, or its minimum count value. These
numbers are used to ensure that the pointers passed in as the task parameters numbers are used to ensure that the pointers passed in as the task parameters
are valid. */ are valid. */
#define countSTART_AT_MAX_COUNT ( 0xaa ) #define countSTART_AT_MAX_COUNT ( 0xaa )
#define countSTART_AT_ZERO ( 0x55 ) #define countSTART_AT_ZERO ( 0x55 )
/* Two tasks are created for the test. One uses a semaphore created with its /* Two tasks are created for the test. One uses a semaphore created with its
count value set to the maximum, and one with the count value set to zero. */ count value set to the maximum, and one with the count value set to zero. */
#define countNUM_TEST_TASKS ( 2 ) #define countNUM_TEST_TASKS ( 2 )
#define countDONT_BLOCK ( 0 ) #define countDONT_BLOCK ( 0 )
/*-----------------------------------------------------------*/ /*-----------------------------------------------------------*/
/* Flag that will be latched to pdTRUE should any unexpected behaviour be /* Flag that will be latched to pdTRUE should any unexpected behaviour be
detected in any of the tasks. */ detected in any of the tasks. */
static portBASE_TYPE xErrorDetected = pdFALSE; static volatile portBASE_TYPE xErrorDetected = pdFALSE;
/*-----------------------------------------------------------*/ /*-----------------------------------------------------------*/
/* /*
* The demo task. This simply counts the semaphore up to its maximum value, * The demo task. This simply counts the semaphore up to its maximum value,
* the counts it back down again. The result of each semaphore 'give' and * the counts it back down again. The result of each semaphore 'give' and
* 'take' is inspected, with an error being flagged if it is found not to be * 'take' is inspected, with an error being flagged if it is found not to be
* the expected result. * the expected result.
*/ */
static void prvCountingSemaphoreTask( void *pvParameters ); static void prvCountingSemaphoreTask( void *pvParameters );
/* /*
* Utility function to increment the semaphore count value up from zero to * Utility function to increment the semaphore count value up from zero to
* countMAX_COUNT_VALUE. * countMAX_COUNT_VALUE.
*/ */
static void prvIncrementSemaphoreCount( xSemaphoreHandle xSemaphore, unsigned portBASE_TYPE *puxLoopCounter ); static void prvIncrementSemaphoreCount( xSemaphoreHandle xSemaphore, unsigned portBASE_TYPE *puxLoopCounter );
/* /*
* Utility function to decrement the semaphore count value up from * Utility function to decrement the semaphore count value up from
* countMAX_COUNT_VALUE to zero. * countMAX_COUNT_VALUE to zero.
*/ */
static void prvDecrementSemaphoreCount( xSemaphoreHandle xSemaphore, unsigned portBASE_TYPE *puxLoopCounter ); static void prvDecrementSemaphoreCount( xSemaphoreHandle xSemaphore, unsigned portBASE_TYPE *puxLoopCounter );
/*-----------------------------------------------------------*/ /*-----------------------------------------------------------*/
/* The structure that is passed into the task as the task parameter. */ /* The structure that is passed into the task as the task parameter. */
typedef struct COUNT_SEM_STRUCT typedef struct COUNT_SEM_STRUCT
{ {
/* The semaphore to be used for the demo. */ /* The semaphore to be used for the demo. */
xSemaphoreHandle xSemaphore; xSemaphoreHandle xSemaphore;
/* Set to countSTART_AT_MAX_COUNT if the semaphore should be created with /* Set to countSTART_AT_MAX_COUNT if the semaphore should be created with
its count value set to its max count value, or countSTART_AT_ZERO if it its count value set to its max count value, or countSTART_AT_ZERO if it
should have been created with its count value set to 0. */ should have been created with its count value set to 0. */
unsigned portBASE_TYPE uxExpectedStartCount; unsigned portBASE_TYPE uxExpectedStartCount;
/* Incremented on each cycle of the demo task. Used to detect a stalled /* Incremented on each cycle of the demo task. Used to detect a stalled
task. */ task. */
unsigned portBASE_TYPE uxLoopCounter; unsigned portBASE_TYPE uxLoopCounter;
} xCountSemStruct; } xCountSemStruct;
/* Two structures are defined, one is passed to each test task. */ /* Two structures are defined, one is passed to each test task. */
static xCountSemStruct xParameters[ countNUM_TEST_TASKS ]; static volatile xCountSemStruct xParameters[ countNUM_TEST_TASKS ];
/*-----------------------------------------------------------*/ /*-----------------------------------------------------------*/
void vStartCountingSemaphoreTasks( void ) void vStartCountingSemaphoreTasks( void )
{ {
/* Create the semaphores that we are going to use for the test/demo. The /* Create the semaphores that we are going to use for the test/demo. The
first should be created such that it starts at its maximum count value, first should be created such that it starts at its maximum count value,
the second should be created such that it starts with a count value of zero. */ the second should be created such that it starts with a count value of zero. */
xParameters[ 0 ].xSemaphore = xSemaphoreCreateCounting( countMAX_COUNT_VALUE, countMAX_COUNT_VALUE ); xParameters[ 0 ].xSemaphore = xSemaphoreCreateCounting( countMAX_COUNT_VALUE, countMAX_COUNT_VALUE );
xParameters[ 0 ].uxExpectedStartCount = countSTART_AT_MAX_COUNT; xParameters[ 0 ].uxExpectedStartCount = countSTART_AT_MAX_COUNT;
xParameters[ 0 ].uxLoopCounter = 0; xParameters[ 0 ].uxLoopCounter = 0;
xParameters[ 1 ].xSemaphore = xSemaphoreCreateCounting( countMAX_COUNT_VALUE, 0 ); xParameters[ 1 ].xSemaphore = xSemaphoreCreateCounting( countMAX_COUNT_VALUE, 0 );
xParameters[ 1 ].uxExpectedStartCount = 0; xParameters[ 1 ].uxExpectedStartCount = 0;
xParameters[ 1 ].uxLoopCounter = 0; xParameters[ 1 ].uxLoopCounter = 0;
/* Were the semaphores created? */ /* Were the semaphores created? */
if( ( xParameters[ 0 ].xSemaphore != NULL ) || ( xParameters[ 1 ].xSemaphore != NULL ) ) if( ( xParameters[ 0 ].xSemaphore != NULL ) || ( xParameters[ 1 ].xSemaphore != NULL ) )
{ {
/* Create the demo tasks, passing in the semaphore to use as the parameter. */ /* Create the demo tasks, passing in the semaphore to use as the parameter. */
xTaskCreate( prvCountingSemaphoreTask, ( signed portCHAR * ) "CNT1", configMINIMAL_STACK_SIZE, ( void * ) &( xParameters[ 0 ] ), tskIDLE_PRIORITY, NULL ); xTaskCreate( prvCountingSemaphoreTask, ( signed portCHAR * ) "CNT1", configMINIMAL_STACK_SIZE, ( void * ) &( xParameters[ 0 ] ), tskIDLE_PRIORITY, NULL );
xTaskCreate( prvCountingSemaphoreTask, ( signed portCHAR * ) "CNT2", configMINIMAL_STACK_SIZE, ( void * ) &( xParameters[ 1 ] ), tskIDLE_PRIORITY, NULL ); xTaskCreate( prvCountingSemaphoreTask, ( signed portCHAR * ) "CNT2", configMINIMAL_STACK_SIZE, ( void * ) &( xParameters[ 1 ] ), tskIDLE_PRIORITY, NULL );
} }
} }
/*-----------------------------------------------------------*/ /*-----------------------------------------------------------*/
static void prvDecrementSemaphoreCount( xSemaphoreHandle xSemaphore, unsigned portBASE_TYPE *puxLoopCounter ) static void prvDecrementSemaphoreCount( xSemaphoreHandle xSemaphore, unsigned portBASE_TYPE *puxLoopCounter )
{ {
unsigned portBASE_TYPE ux; unsigned portBASE_TYPE ux;
/* If the semaphore count is at its maximum then we should not be able to /* If the semaphore count is at its maximum then we should not be able to
'give' the semaphore. */ 'give' the semaphore. */
if( xSemaphoreGive( xSemaphore ) == pdPASS ) if( xSemaphoreGive( xSemaphore ) == pdPASS )
{ {
xErrorDetected = pdTRUE; xErrorDetected = pdTRUE;
} }
/* We should be able to 'take' the semaphore countMAX_COUNT_VALUE times. */ /* We should be able to 'take' the semaphore countMAX_COUNT_VALUE times. */
for( ux = 0; ux < countMAX_COUNT_VALUE; ux++ ) for( ux = 0; ux < countMAX_COUNT_VALUE; ux++ )
{ {
if( xSemaphoreTake( xSemaphore, countDONT_BLOCK ) != pdPASS ) if( xSemaphoreTake( xSemaphore, countDONT_BLOCK ) != pdPASS )
{ {
/* We expected to be able to take the semaphore. */ /* We expected to be able to take the semaphore. */
xErrorDetected = pdTRUE; xErrorDetected = pdTRUE;
} }
( *puxLoopCounter )++; ( *puxLoopCounter )++;
} }
#if configUSE_PREEMPTION == 0 #if configUSE_PREEMPTION == 0
taskYIELD(); taskYIELD();
#endif #endif
/* If the semaphore count is zero then we should not be able to 'take' /* If the semaphore count is zero then we should not be able to 'take'
the semaphore. */ the semaphore. */
if( xSemaphoreTake( xSemaphore, countDONT_BLOCK ) == pdPASS ) if( xSemaphoreTake( xSemaphore, countDONT_BLOCK ) == pdPASS )
{ {
xErrorDetected = pdTRUE; xErrorDetected = pdTRUE;
} }
} }
/*-----------------------------------------------------------*/ /*-----------------------------------------------------------*/
static void prvIncrementSemaphoreCount( xSemaphoreHandle xSemaphore, unsigned portBASE_TYPE *puxLoopCounter ) static void prvIncrementSemaphoreCount( xSemaphoreHandle xSemaphore, unsigned portBASE_TYPE *puxLoopCounter )
{ {
unsigned portBASE_TYPE ux; unsigned portBASE_TYPE ux;
/* If the semaphore count is zero then we should not be able to 'take' /* If the semaphore count is zero then we should not be able to 'take'
the semaphore. */ the semaphore. */
if( xSemaphoreTake( xSemaphore, countDONT_BLOCK ) == pdPASS ) if( xSemaphoreTake( xSemaphore, countDONT_BLOCK ) == pdPASS )
{ {
xErrorDetected = pdTRUE; xErrorDetected = pdTRUE;
} }
/* We should be able to 'give' the semaphore countMAX_COUNT_VALUE times. */ /* We should be able to 'give' the semaphore countMAX_COUNT_VALUE times. */
for( ux = 0; ux < countMAX_COUNT_VALUE; ux++ ) for( ux = 0; ux < countMAX_COUNT_VALUE; ux++ )
{ {
if( xSemaphoreGive( xSemaphore ) != pdPASS ) if( xSemaphoreGive( xSemaphore ) != pdPASS )
{ {
/* We expected to be able to take the semaphore. */ /* We expected to be able to take the semaphore. */
xErrorDetected = pdTRUE; xErrorDetected = pdTRUE;
} }
( *puxLoopCounter )++; ( *puxLoopCounter )++;
} }
#if configUSE_PREEMPTION == 0 #if configUSE_PREEMPTION == 0
taskYIELD(); taskYIELD();
#endif #endif
/* If the semaphore count is at its maximum then we should not be able to /* If the semaphore count is at its maximum then we should not be able to
'give' the semaphore. */ 'give' the semaphore. */
if( xSemaphoreGive( xSemaphore ) == pdPASS ) if( xSemaphoreGive( xSemaphore ) == pdPASS )
{ {
xErrorDetected = pdTRUE; xErrorDetected = pdTRUE;
} }
} }
/*-----------------------------------------------------------*/ /*-----------------------------------------------------------*/
static void prvCountingSemaphoreTask( void *pvParameters ) static void prvCountingSemaphoreTask( void *pvParameters )
{ {
xCountSemStruct *pxParameter; xCountSemStruct *pxParameter;
#ifdef USE_STDIO #ifdef USE_STDIO
void vPrintDisplayMessage( const portCHAR * const * ppcMessageToSend ); void vPrintDisplayMessage( const portCHAR * const * ppcMessageToSend );
const portCHAR * const pcTaskStartMsg = "Counting semaphore demo started.\r\n"; const portCHAR * const pcTaskStartMsg = "Counting semaphore demo started.\r\n";
/* Queue a message for printing to say the task has started. */ /* Queue a message for printing to say the task has started. */
vPrintDisplayMessage( &pcTaskStartMsg ); vPrintDisplayMessage( &pcTaskStartMsg );
#endif #endif
/* The semaphore to be used was passed as the parameter. */ /* The semaphore to be used was passed as the parameter. */
pxParameter = ( xCountSemStruct * ) pvParameters; pxParameter = ( xCountSemStruct * ) pvParameters;
/* Did we expect to find the semaphore already at its max count value, or /* Did we expect to find the semaphore already at its max count value, or
at zero? */ at zero? */
if( pxParameter->uxExpectedStartCount == countSTART_AT_MAX_COUNT ) if( pxParameter->uxExpectedStartCount == countSTART_AT_MAX_COUNT )
{ {
prvDecrementSemaphoreCount( pxParameter->xSemaphore, &( pxParameter->uxLoopCounter ) ); prvDecrementSemaphoreCount( pxParameter->xSemaphore, &( pxParameter->uxLoopCounter ) );
} }
/* Now we expect the semaphore count to be 0, so this time there is an /* Now we expect the semaphore count to be 0, so this time there is an
error if we can take the semaphore. */ error if we can take the semaphore. */
if( xSemaphoreTake( pxParameter->xSemaphore, 0 ) == pdPASS ) if( xSemaphoreTake( pxParameter->xSemaphore, 0 ) == pdPASS )
{ {
xErrorDetected = pdTRUE; xErrorDetected = pdTRUE;
} }
for( ;; ) for( ;; )
{ {
prvIncrementSemaphoreCount( pxParameter->xSemaphore, &( pxParameter->uxLoopCounter ) ); prvIncrementSemaphoreCount( pxParameter->xSemaphore, &( pxParameter->uxLoopCounter ) );
prvDecrementSemaphoreCount( pxParameter->xSemaphore, &( pxParameter->uxLoopCounter ) ); prvDecrementSemaphoreCount( pxParameter->xSemaphore, &( pxParameter->uxLoopCounter ) );
} }
} }
/*-----------------------------------------------------------*/ /*-----------------------------------------------------------*/
portBASE_TYPE xAreCountingSemaphoreTasksStillRunning( void ) portBASE_TYPE xAreCountingSemaphoreTasksStillRunning( void )
{ {
static unsigned portBASE_TYPE uxLastCount0 = 0, uxLastCount1 = 0; static unsigned portBASE_TYPE uxLastCount0 = 0, uxLastCount1 = 0;
portBASE_TYPE xReturn = pdPASS; portBASE_TYPE xReturn = pdPASS;
/* Return fail if any 'give' or 'take' did not result in the expected /* Return fail if any 'give' or 'take' did not result in the expected
behaviour. */ behaviour. */
if( xErrorDetected != pdFALSE ) if( xErrorDetected != pdFALSE )
{ {
xReturn = pdFAIL; xReturn = pdFAIL;
} }
/* Return fail if either task is not still incrementing its loop counter. */ /* Return fail if either task is not still incrementing its loop counter. */
if( uxLastCount0 == xParameters[ 0 ].uxLoopCounter ) if( uxLastCount0 == xParameters[ 0 ].uxLoopCounter )
{ {
xReturn = pdFAIL; xReturn = pdFAIL;
} }
else else
{ {
uxLastCount0 = xParameters[ 0 ].uxLoopCounter; uxLastCount0 = xParameters[ 0 ].uxLoopCounter;
} }
if( uxLastCount1 == xParameters[ 1 ].uxLoopCounter ) if( uxLastCount1 == xParameters[ 1 ].uxLoopCounter )
{ {
xReturn = pdFAIL; xReturn = pdFAIL;
} }
else else
{ {
uxLastCount1 = xParameters[ 1 ].uxLoopCounter; uxLastCount1 = xParameters[ 1 ].uxLoopCounter;
} }
return xReturn; return xReturn;
} }

View File

@ -1,414 +1,414 @@
/* /*
FreeRTOS.org V5.0.0 - Copyright (C) 2003-2008 Richard Barry. FreeRTOS.org V5.0.0 - Copyright (C) 2003-2008 Richard Barry.
This file is part of the FreeRTOS.org distribution. This file is part of the FreeRTOS.org distribution.
FreeRTOS.org is free software; you can redistribute it and/or modify FreeRTOS.org is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or the Free Software Foundation; either version 2 of the License, or
(at your option) any later version. (at your option) any later version.
FreeRTOS.org is distributed in the hope that it will be useful, FreeRTOS.org is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details. GNU General Public License for more details.
You should have received a copy of the GNU General Public License You should have received a copy of the GNU General Public License
along with FreeRTOS.org; if not, write to the Free Software along with FreeRTOS.org; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
A special exception to the GPL can be applied should you wish to distribute A special exception to the GPL can be applied should you wish to distribute
a combined work that includes FreeRTOS.org, without being obliged to provide a combined work that includes FreeRTOS.org, without being obliged to provide
the source code for any proprietary components. See the licensing section the source code for any proprietary components. See the licensing section
of http://www.FreeRTOS.org for full details of how and when the exception of http://www.FreeRTOS.org for full details of how and when the exception
can be applied. can be applied.
*************************************************************************** ***************************************************************************
*************************************************************************** ***************************************************************************
* * * *
* SAVE TIME AND MONEY! We can port FreeRTOS.org to your own hardware, * * SAVE TIME AND MONEY! We can port FreeRTOS.org to your own hardware, *
* and even write all or part of your application on your behalf. * * and even write all or part of your application on your behalf. *
* See http://www.OpenRTOS.com for details of the services we provide to * * See http://www.OpenRTOS.com for details of the services we provide to *
* expedite your project. * * expedite your project. *
* * * *
*************************************************************************** ***************************************************************************
*************************************************************************** ***************************************************************************
Please ensure to read the configuration and relevant port sections of the Please ensure to read the configuration and relevant port sections of the
online documentation. online documentation.
http://www.FreeRTOS.org - Documentation, latest information, license and http://www.FreeRTOS.org - Documentation, latest information, license and
contact details. contact details.
http://www.SafeRTOS.com - A version that is certified for use in safety http://www.SafeRTOS.com - A version that is certified for use in safety
critical systems. critical systems.
http://www.OpenRTOS.com - Commercial support, development, porting, http://www.OpenRTOS.com - Commercial support, development, porting,
licensing and training services. licensing and training services.
*/ */
/* /*
* The first test creates three tasks - two counter tasks (one continuous count * The first test creates three tasks - two counter tasks (one continuous count
* and one limited count) and one controller. A "count" variable is shared * and one limited count) and one controller. A "count" variable is shared
* between all three tasks. The two counter tasks should never be in a "ready" * between all three tasks. The two counter tasks should never be in a "ready"
* state at the same time. The controller task runs at the same priority as * state at the same time. The controller task runs at the same priority as
* the continuous count task, and at a lower priority than the limited count * the continuous count task, and at a lower priority than the limited count
* task. * task.
* *
* One counter task loops indefinitely, incrementing the shared count variable * One counter task loops indefinitely, incrementing the shared count variable
* on each iteration. To ensure it has exclusive access to the variable it * on each iteration. To ensure it has exclusive access to the variable it
* raises it's priority above that of the controller task before each * raises it's priority above that of the controller task before each
* increment, lowering it again to it's original priority before starting the * increment, lowering it again to it's original priority before starting the
* next iteration. * next iteration.
* *
* The other counter task increments the shared count variable on each * The other counter task increments the shared count variable on each
* iteration of it's loop until the count has reached a limit of 0xff - at * iteration of it's loop until the count has reached a limit of 0xff - at
* which point it suspends itself. It will not start a new loop until the * which point it suspends itself. It will not start a new loop until the
* controller task has made it "ready" again by calling vTaskResume (). * controller task has made it "ready" again by calling vTaskResume ().
* This second counter task operates at a higher priority than controller * This second counter task operates at a higher priority than controller
* task so does not need to worry about mutual exclusion of the counter * task so does not need to worry about mutual exclusion of the counter
* variable. * variable.
* *
* The controller task is in two sections. The first section controls and * The controller task is in two sections. The first section controls and
* monitors the continuous count task. When this section is operational the * monitors the continuous count task. When this section is operational the
* limited count task is suspended. Likewise, the second section controls * limited count task is suspended. Likewise, the second section controls
* and monitors the limited count task. When this section is operational the * and monitors the limited count task. When this section is operational the
* continuous count task is suspended. * continuous count task is suspended.
* *
* In the first section the controller task first takes a copy of the shared * In the first section the controller task first takes a copy of the shared
* count variable. To ensure mutual exclusion on the count variable it * count variable. To ensure mutual exclusion on the count variable it
* suspends the continuous count task, resuming it again when the copy has been * suspends the continuous count task, resuming it again when the copy has been
* taken. The controller task then sleeps for a fixed period - during which * taken. The controller task then sleeps for a fixed period - during which
* the continuous count task will execute and increment the shared variable. * the continuous count task will execute and increment the shared variable.
* When the controller task wakes it checks that the continuous count task * When the controller task wakes it checks that the continuous count task
* has executed by comparing the copy of the shared variable with its current * has executed by comparing the copy of the shared variable with its current
* value. This time, to ensure mutual exclusion, the scheduler itself is * value. This time, to ensure mutual exclusion, the scheduler itself is
* suspended with a call to vTaskSuspendAll (). This is for demonstration * suspended with a call to vTaskSuspendAll (). This is for demonstration
* purposes only and is not a recommended technique due to its inefficiency. * purposes only and is not a recommended technique due to its inefficiency.
* *
* After a fixed number of iterations the controller task suspends the * After a fixed number of iterations the controller task suspends the
* continuous count task, and moves on to its second section. * continuous count task, and moves on to its second section.
* *
* At the start of the second section the shared variable is cleared to zero. * At the start of the second section the shared variable is cleared to zero.
* The limited count task is then woken from it's suspension by a call to * The limited count task is then woken from it's suspension by a call to
* vTaskResume (). As this counter task operates at a higher priority than * vTaskResume (). As this counter task operates at a higher priority than
* the controller task the controller task should not run again until the * the controller task the controller task should not run again until the
* shared variable has been counted up to the limited value causing the counter * shared variable has been counted up to the limited value causing the counter
* task to suspend itself. The next line after vTaskResume () is therefore * task to suspend itself. The next line after vTaskResume () is therefore
* a check on the shared variable to ensure everything is as expected. * a check on the shared variable to ensure everything is as expected.
* *
* *
* The second test consists of a couple of very simple tasks that post onto a * The second test consists of a couple of very simple tasks that post onto a
* queue while the scheduler is suspended. This test was added to test parts * queue while the scheduler is suspended. This test was added to test parts
* of the scheduler not exercised by the first test. * of the scheduler not exercised by the first test.
* *
*/ */
#include <stdlib.h> #include <stdlib.h>
/* Scheduler include files. */ /* Scheduler include files. */
#include "FreeRTOS.h" #include "FreeRTOS.h"
#include "task.h" #include "task.h"
#include "semphr.h" #include "semphr.h"
/* Demo app include files. */ /* Demo app include files. */
#include "dynamic.h" #include "dynamic.h"
/* Function that implements the "limited count" task as described above. */ /* Function that implements the "limited count" task as described above. */
static portTASK_FUNCTION_PROTO( vLimitedIncrementTask, pvParameters ); static portTASK_FUNCTION_PROTO( vLimitedIncrementTask, pvParameters );
/* Function that implements the "continuous count" task as described above. */ /* Function that implements the "continuous count" task as described above. */
static portTASK_FUNCTION_PROTO( vContinuousIncrementTask, pvParameters ); static portTASK_FUNCTION_PROTO( vContinuousIncrementTask, pvParameters );
/* Function that implements the controller task as described above. */ /* Function that implements the controller task as described above. */
static portTASK_FUNCTION_PROTO( vCounterControlTask, pvParameters ); static portTASK_FUNCTION_PROTO( vCounterControlTask, pvParameters );
static portTASK_FUNCTION_PROTO( vQueueReceiveWhenSuspendedTask, pvParameters ); static portTASK_FUNCTION_PROTO( vQueueReceiveWhenSuspendedTask, pvParameters );
static portTASK_FUNCTION_PROTO( vQueueSendWhenSuspendedTask, pvParameters ); static portTASK_FUNCTION_PROTO( vQueueSendWhenSuspendedTask, pvParameters );
/* Demo task specific constants. */ /* Demo task specific constants. */
#define priSTACK_SIZE ( configMINIMAL_STACK_SIZE ) #define priSTACK_SIZE ( configMINIMAL_STACK_SIZE )
#define priSLEEP_TIME ( ( portTickType ) 128 / portTICK_RATE_MS ) #define priSLEEP_TIME ( ( portTickType ) 128 / portTICK_RATE_MS )
#define priLOOPS ( 5 ) #define priLOOPS ( 5 )
#define priMAX_COUNT ( ( unsigned portLONG ) 0xff ) #define priMAX_COUNT ( ( unsigned portLONG ) 0xff )
#define priNO_BLOCK ( ( portTickType ) 0 ) #define priNO_BLOCK ( ( portTickType ) 0 )
#define priSUSPENDED_QUEUE_LENGTH ( 1 ) #define priSUSPENDED_QUEUE_LENGTH ( 1 )
/*-----------------------------------------------------------*/ /*-----------------------------------------------------------*/
/* Handles to the two counter tasks. These could be passed in as parameters /* Handles to the two counter tasks. These could be passed in as parameters
to the controller task to prevent them having to be file scope. */ to the controller task to prevent them having to be file scope. */
static xTaskHandle xContinousIncrementHandle, xLimitedIncrementHandle; static xTaskHandle xContinousIncrementHandle, xLimitedIncrementHandle;
/* The shared counter variable. This is passed in as a parameter to the two /* The shared counter variable. This is passed in as a parameter to the two
counter variables for demonstration purposes. */ counter variables for demonstration purposes. */
static unsigned portLONG ulCounter; static unsigned portLONG ulCounter;
/* Variables used to check that the tasks are still operating without error. /* Variables used to check that the tasks are still operating without error.
Each complete iteration of the controller task increments this variable Each complete iteration of the controller task increments this variable
provided no errors have been found. The variable maintaining the same value provided no errors have been found. The variable maintaining the same value
is therefore indication of an error. */ is therefore indication of an error. */
static unsigned portSHORT usCheckVariable = ( unsigned portSHORT ) 0; static volatile unsigned portSHORT usCheckVariable = ( unsigned portSHORT ) 0;
static portBASE_TYPE xSuspendedQueueSendError = pdFALSE; static volatile portBASE_TYPE xSuspendedQueueSendError = pdFALSE;
static portBASE_TYPE xSuspendedQueueReceiveError = pdFALSE; static volatile portBASE_TYPE xSuspendedQueueReceiveError = pdFALSE;
/* Queue used by the second test. */ /* Queue used by the second test. */
xQueueHandle xSuspendedTestQueue; xQueueHandle xSuspendedTestQueue;
/*-----------------------------------------------------------*/ /*-----------------------------------------------------------*/
/* /*
* Start the three tasks as described at the top of the file. * Start the three tasks as described at the top of the file.
* Note that the limited count task is given a higher priority. * Note that the limited count task is given a higher priority.
*/ */
void vStartDynamicPriorityTasks( void ) void vStartDynamicPriorityTasks( void )
{ {
xSuspendedTestQueue = xQueueCreate( priSUSPENDED_QUEUE_LENGTH, sizeof( unsigned portLONG ) ); xSuspendedTestQueue = xQueueCreate( priSUSPENDED_QUEUE_LENGTH, sizeof( unsigned portLONG ) );
xTaskCreate( vContinuousIncrementTask, ( signed portCHAR * ) "CNT_INC", priSTACK_SIZE, ( void * ) &ulCounter, tskIDLE_PRIORITY, &xContinousIncrementHandle ); xTaskCreate( vContinuousIncrementTask, ( signed portCHAR * ) "CNT_INC", priSTACK_SIZE, ( void * ) &ulCounter, tskIDLE_PRIORITY, &xContinousIncrementHandle );
xTaskCreate( vLimitedIncrementTask, ( signed portCHAR * ) "LIM_INC", priSTACK_SIZE, ( void * ) &ulCounter, tskIDLE_PRIORITY + 1, &xLimitedIncrementHandle ); xTaskCreate( vLimitedIncrementTask, ( signed portCHAR * ) "LIM_INC", priSTACK_SIZE, ( void * ) &ulCounter, tskIDLE_PRIORITY + 1, &xLimitedIncrementHandle );
xTaskCreate( vCounterControlTask, ( signed portCHAR * ) "C_CTRL", priSTACK_SIZE, NULL, tskIDLE_PRIORITY, NULL ); xTaskCreate( vCounterControlTask, ( signed portCHAR * ) "C_CTRL", priSTACK_SIZE, NULL, tskIDLE_PRIORITY, NULL );
xTaskCreate( vQueueSendWhenSuspendedTask, ( signed portCHAR * ) "SUSP_TX", priSTACK_SIZE, NULL, tskIDLE_PRIORITY, NULL ); xTaskCreate( vQueueSendWhenSuspendedTask, ( signed portCHAR * ) "SUSP_TX", priSTACK_SIZE, NULL, tskIDLE_PRIORITY, NULL );
xTaskCreate( vQueueReceiveWhenSuspendedTask, ( signed portCHAR * ) "SUSP_RX", priSTACK_SIZE, NULL, tskIDLE_PRIORITY, NULL ); xTaskCreate( vQueueReceiveWhenSuspendedTask, ( signed portCHAR * ) "SUSP_RX", priSTACK_SIZE, NULL, tskIDLE_PRIORITY, NULL );
} }
/*-----------------------------------------------------------*/ /*-----------------------------------------------------------*/
/* /*
* Just loops around incrementing the shared variable until the limit has been * Just loops around incrementing the shared variable until the limit has been
* reached. Once the limit has been reached it suspends itself. * reached. Once the limit has been reached it suspends itself.
*/ */
static portTASK_FUNCTION( vLimitedIncrementTask, pvParameters ) static portTASK_FUNCTION( vLimitedIncrementTask, pvParameters )
{ {
unsigned portLONG *pulCounter; unsigned portLONG *pulCounter;
/* Take a pointer to the shared variable from the parameters passed into /* Take a pointer to the shared variable from the parameters passed into
the task. */ the task. */
pulCounter = ( unsigned portLONG * ) pvParameters; pulCounter = ( unsigned portLONG * ) pvParameters;
/* This will run before the control task, so the first thing it does is /* This will run before the control task, so the first thing it does is
suspend - the control task will resume it when ready. */ suspend - the control task will resume it when ready. */
vTaskSuspend( NULL ); vTaskSuspend( NULL );
for( ;; ) for( ;; )
{ {
/* Just count up to a value then suspend. */ /* Just count up to a value then suspend. */
( *pulCounter )++; ( *pulCounter )++;
if( *pulCounter >= priMAX_COUNT ) if( *pulCounter >= priMAX_COUNT )
{ {
vTaskSuspend( NULL ); vTaskSuspend( NULL );
} }
} }
} }
/*-----------------------------------------------------------*/ /*-----------------------------------------------------------*/
/* /*
* Just keep counting the shared variable up. The control task will suspend * Just keep counting the shared variable up. The control task will suspend
* this task when it wants. * this task when it wants.
*/ */
static portTASK_FUNCTION( vContinuousIncrementTask, pvParameters ) static portTASK_FUNCTION( vContinuousIncrementTask, pvParameters )
{ {
unsigned portLONG *pulCounter; unsigned portLONG *pulCounter;
unsigned portBASE_TYPE uxOurPriority; unsigned portBASE_TYPE uxOurPriority;
/* Take a pointer to the shared variable from the parameters passed into /* Take a pointer to the shared variable from the parameters passed into
the task. */ the task. */
pulCounter = ( unsigned portLONG * ) pvParameters; pulCounter = ( unsigned portLONG * ) pvParameters;
/* Query our priority so we can raise it when exclusive access to the /* Query our priority so we can raise it when exclusive access to the
shared variable is required. */ shared variable is required. */
uxOurPriority = uxTaskPriorityGet( NULL ); uxOurPriority = uxTaskPriorityGet( NULL );
for( ;; ) for( ;; )
{ {
/* Raise our priority above the controller task to ensure a context /* Raise our priority above the controller task to ensure a context
switch does not occur while we are accessing this variable. */ switch does not occur while we are accessing this variable. */
vTaskPrioritySet( NULL, uxOurPriority + 1 ); vTaskPrioritySet( NULL, uxOurPriority + 1 );
( *pulCounter )++; ( *pulCounter )++;
vTaskPrioritySet( NULL, uxOurPriority ); vTaskPrioritySet( NULL, uxOurPriority );
} }
} }
/*-----------------------------------------------------------*/ /*-----------------------------------------------------------*/
/* /*
* Controller task as described above. * Controller task as described above.
*/ */
static portTASK_FUNCTION( vCounterControlTask, pvParameters ) static portTASK_FUNCTION( vCounterControlTask, pvParameters )
{ {
unsigned portLONG ulLastCounter; unsigned portLONG ulLastCounter;
portSHORT sLoops; portSHORT sLoops;
portSHORT sError = pdFALSE; portSHORT sError = pdFALSE;
/* Just to stop warning messages. */ /* Just to stop warning messages. */
( void ) pvParameters; ( void ) pvParameters;
for( ;; ) for( ;; )
{ {
/* Start with the counter at zero. */ /* Start with the counter at zero. */
ulCounter = ( unsigned portLONG ) 0; ulCounter = ( unsigned portLONG ) 0;
/* First section : */ /* First section : */
/* Check the continuous count task is running. */ /* Check the continuous count task is running. */
for( sLoops = 0; sLoops < priLOOPS; sLoops++ ) for( sLoops = 0; sLoops < priLOOPS; sLoops++ )
{ {
/* Suspend the continuous count task so we can take a mirror of the /* Suspend the continuous count task so we can take a mirror of the
shared variable without risk of corruption. */ shared variable without risk of corruption. */
vTaskSuspend( xContinousIncrementHandle ); vTaskSuspend( xContinousIncrementHandle );
ulLastCounter = ulCounter; ulLastCounter = ulCounter;
vTaskResume( xContinousIncrementHandle ); vTaskResume( xContinousIncrementHandle );
/* Now delay to ensure the other task has processor time. */ /* Now delay to ensure the other task has processor time. */
vTaskDelay( priSLEEP_TIME ); vTaskDelay( priSLEEP_TIME );
/* Check the shared variable again. This time to ensure mutual /* Check the shared variable again. This time to ensure mutual
exclusion the whole scheduler will be locked. This is just for exclusion the whole scheduler will be locked. This is just for
demo purposes! */ demo purposes! */
vTaskSuspendAll(); vTaskSuspendAll();
{ {
if( ulLastCounter == ulCounter ) if( ulLastCounter == ulCounter )
{ {
/* The shared variable has not changed. There is a problem /* The shared variable has not changed. There is a problem
with the continuous count task so flag an error. */ with the continuous count task so flag an error. */
sError = pdTRUE; sError = pdTRUE;
} }
} }
xTaskResumeAll(); xTaskResumeAll();
} }
/* Second section: */ /* Second section: */
/* Suspend the continuous counter task so it stops accessing the shared variable. */ /* Suspend the continuous counter task so it stops accessing the shared variable. */
vTaskSuspend( xContinousIncrementHandle ); vTaskSuspend( xContinousIncrementHandle );
/* Reset the variable. */ /* Reset the variable. */
ulCounter = ( unsigned portLONG ) 0; ulCounter = ( unsigned portLONG ) 0;
/* Resume the limited count task which has a higher priority than us. /* Resume the limited count task which has a higher priority than us.
We should therefore not return from this call until the limited count We should therefore not return from this call until the limited count
task has suspended itself with a known value in the counter variable. */ task has suspended itself with a known value in the counter variable. */
vTaskResume( xLimitedIncrementHandle ); vTaskResume( xLimitedIncrementHandle );
/* Does the counter variable have the expected value? */ /* Does the counter variable have the expected value? */
if( ulCounter != priMAX_COUNT ) if( ulCounter != priMAX_COUNT )
{ {
sError = pdTRUE; sError = pdTRUE;
} }
if( sError == pdFALSE ) if( sError == pdFALSE )
{ {
/* If no errors have occurred then increment the check variable. */ /* If no errors have occurred then increment the check variable. */
portENTER_CRITICAL(); portENTER_CRITICAL();
usCheckVariable++; usCheckVariable++;
portEXIT_CRITICAL(); portEXIT_CRITICAL();
} }
/* Resume the continuous count task and do it all again. */ /* Resume the continuous count task and do it all again. */
vTaskResume( xContinousIncrementHandle ); vTaskResume( xContinousIncrementHandle );
} }
} }
/*-----------------------------------------------------------*/ /*-----------------------------------------------------------*/
static portTASK_FUNCTION( vQueueSendWhenSuspendedTask, pvParameters ) static portTASK_FUNCTION( vQueueSendWhenSuspendedTask, pvParameters )
{ {
static unsigned portLONG ulValueToSend = ( unsigned portLONG ) 0; static unsigned portLONG ulValueToSend = ( unsigned portLONG ) 0;
/* Just to stop warning messages. */ /* Just to stop warning messages. */
( void ) pvParameters; ( void ) pvParameters;
for( ;; ) for( ;; )
{ {
vTaskSuspendAll(); vTaskSuspendAll();
{ {
/* We must not block while the scheduler is suspended! */ /* We must not block while the scheduler is suspended! */
if( xQueueSend( xSuspendedTestQueue, ( void * ) &ulValueToSend, priNO_BLOCK ) != pdTRUE ) if( xQueueSend( xSuspendedTestQueue, ( void * ) &ulValueToSend, priNO_BLOCK ) != pdTRUE )
{ {
xSuspendedQueueSendError = pdTRUE; xSuspendedQueueSendError = pdTRUE;
} }
} }
xTaskResumeAll(); xTaskResumeAll();
vTaskDelay( priSLEEP_TIME ); vTaskDelay( priSLEEP_TIME );
++ulValueToSend; ++ulValueToSend;
} }
} }
/*-----------------------------------------------------------*/ /*-----------------------------------------------------------*/
static portTASK_FUNCTION( vQueueReceiveWhenSuspendedTask, pvParameters ) static portTASK_FUNCTION( vQueueReceiveWhenSuspendedTask, pvParameters )
{ {
static unsigned portLONG ulExpectedValue = ( unsigned portLONG ) 0, ulReceivedValue; static unsigned portLONG ulExpectedValue = ( unsigned portLONG ) 0, ulReceivedValue;
portBASE_TYPE xGotValue; portBASE_TYPE xGotValue;
/* Just to stop warning messages. */ /* Just to stop warning messages. */
( void ) pvParameters; ( void ) pvParameters;
for( ;; ) for( ;; )
{ {
do do
{ {
/* Suspending the scheduler here is fairly pointless and /* Suspending the scheduler here is fairly pointless and
undesirable for a normal application. It is done here purely undesirable for a normal application. It is done here purely
to test the scheduler. The inner xTaskResumeAll() should to test the scheduler. The inner xTaskResumeAll() should
never return pdTRUE as the scheduler is still locked by the never return pdTRUE as the scheduler is still locked by the
outer call. */ outer call. */
vTaskSuspendAll(); vTaskSuspendAll();
{ {
vTaskSuspendAll(); vTaskSuspendAll();
{ {
xGotValue = xQueueReceive( xSuspendedTestQueue, ( void * ) &ulReceivedValue, priNO_BLOCK ); xGotValue = xQueueReceive( xSuspendedTestQueue, ( void * ) &ulReceivedValue, priNO_BLOCK );
} }
if( xTaskResumeAll() ) if( xTaskResumeAll() )
{ {
xSuspendedQueueReceiveError = pdTRUE; xSuspendedQueueReceiveError = pdTRUE;
} }
} }
xTaskResumeAll(); xTaskResumeAll();
#if configUSE_PREEMPTION == 0 #if configUSE_PREEMPTION == 0
{ {
taskYIELD(); taskYIELD();
} }
#endif #endif
} while( xGotValue == pdFALSE ); } while( xGotValue == pdFALSE );
if( ulReceivedValue != ulExpectedValue ) if( ulReceivedValue != ulExpectedValue )
{ {
xSuspendedQueueReceiveError = pdTRUE; xSuspendedQueueReceiveError = pdTRUE;
} }
++ulExpectedValue; ++ulExpectedValue;
} }
} }
/*-----------------------------------------------------------*/ /*-----------------------------------------------------------*/
/* Called to check that all the created tasks are still running without error. */ /* Called to check that all the created tasks are still running without error. */
portBASE_TYPE xAreDynamicPriorityTasksStillRunning( void ) portBASE_TYPE xAreDynamicPriorityTasksStillRunning( void )
{ {
/* Keep a history of the check variables so we know if it has been incremented /* Keep a history of the check variables so we know if it has been incremented
since the last call. */ since the last call. */
static unsigned portSHORT usLastTaskCheck = ( unsigned portSHORT ) 0; static unsigned portSHORT usLastTaskCheck = ( unsigned portSHORT ) 0;
portBASE_TYPE xReturn = pdTRUE; portBASE_TYPE xReturn = pdTRUE;
/* Check the tasks are still running by ensuring the check variable /* Check the tasks are still running by ensuring the check variable
is still incrementing. */ is still incrementing. */
if( usCheckVariable == usLastTaskCheck ) if( usCheckVariable == usLastTaskCheck )
{ {
/* The check has not incremented so an error exists. */ /* The check has not incremented so an error exists. */
xReturn = pdFALSE; xReturn = pdFALSE;
} }
if( xSuspendedQueueSendError == pdTRUE ) if( xSuspendedQueueSendError == pdTRUE )
{ {
xReturn = pdFALSE; xReturn = pdFALSE;
} }
if( xSuspendedQueueReceiveError == pdTRUE ) if( xSuspendedQueueReceiveError == pdTRUE )
{ {
xReturn = pdFALSE; xReturn = pdFALSE;
} }
usLastTaskCheck = usCheckVariable; usLastTaskCheck = usCheckVariable;
return xReturn; return xReturn;
} }

View File

@ -1,347 +1,347 @@
/* /*
FreeRTOS.org V5.0.0 - Copyright (C) 2003-2008 Richard Barry. FreeRTOS.org V5.0.0 - Copyright (C) 2003-2008 Richard Barry.
This file is part of the FreeRTOS.org distribution. This file is part of the FreeRTOS.org distribution.
FreeRTOS.org is free software; you can redistribute it and/or modify FreeRTOS.org is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or the Free Software Foundation; either version 2 of the License, or
(at your option) any later version. (at your option) any later version.
FreeRTOS.org is distributed in the hope that it will be useful, FreeRTOS.org is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details. GNU General Public License for more details.
You should have received a copy of the GNU General Public License You should have received a copy of the GNU General Public License
along with FreeRTOS.org; if not, write to the Free Software along with FreeRTOS.org; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
A special exception to the GPL can be applied should you wish to distribute A special exception to the GPL can be applied should you wish to distribute
a combined work that includes FreeRTOS.org, without being obliged to provide a combined work that includes FreeRTOS.org, without being obliged to provide
the source code for any proprietary components. See the licensing section the source code for any proprietary components. See the licensing section
of http://www.FreeRTOS.org for full details of how and when the exception of http://www.FreeRTOS.org for full details of how and when the exception
can be applied. can be applied.
*************************************************************************** ***************************************************************************
*************************************************************************** ***************************************************************************
* * * *
* SAVE TIME AND MONEY! We can port FreeRTOS.org to your own hardware, * * SAVE TIME AND MONEY! We can port FreeRTOS.org to your own hardware, *
* and even write all or part of your application on your behalf. * * and even write all or part of your application on your behalf. *
* See http://www.OpenRTOS.com for details of the services we provide to * * See http://www.OpenRTOS.com for details of the services we provide to *
* expedite your project. * * expedite your project. *
* * * *
*************************************************************************** ***************************************************************************
*************************************************************************** ***************************************************************************
Please ensure to read the configuration and relevant port sections of the Please ensure to read the configuration and relevant port sections of the
online documentation. online documentation.
http://www.FreeRTOS.org - Documentation, latest information, license and http://www.FreeRTOS.org - Documentation, latest information, license and
contact details. contact details.
http://www.SafeRTOS.com - A version that is certified for use in safety http://www.SafeRTOS.com - A version that is certified for use in safety
critical systems. critical systems.
http://www.OpenRTOS.com - Commercial support, development, porting, http://www.OpenRTOS.com - Commercial support, development, porting,
licensing and training services. licensing and training services.
*/ */
/* /*
The tasks defined on this page demonstrate the use of recursive mutexes. The tasks defined on this page demonstrate the use of recursive mutexes.
For recursive mutex functionality the created mutex should be created using For recursive mutex functionality the created mutex should be created using
xSemaphoreCreateRecursiveMutex(), then be manipulated xSemaphoreCreateRecursiveMutex(), then be manipulated
using the xSemaphoreTakeRecursive() and xSemaphoreGiveRecursive() API using the xSemaphoreTakeRecursive() and xSemaphoreGiveRecursive() API
functions. functions.
This demo creates three tasks all of which access the same recursive mutex: This demo creates three tasks all of which access the same recursive mutex:
prvRecursiveMutexControllingTask() has the highest priority so executes prvRecursiveMutexControllingTask() has the highest priority so executes
first and grabs the mutex. It then performs some recursive accesses - first and grabs the mutex. It then performs some recursive accesses -
between each of which it sleeps for a short period to let the lower between each of which it sleeps for a short period to let the lower
priority tasks execute. When it has completed its demo functionality priority tasks execute. When it has completed its demo functionality
it gives the mutex back before suspending itself. it gives the mutex back before suspending itself.
prvRecursiveMutexBlockingTask() attempts to access the mutex by performing prvRecursiveMutexBlockingTask() attempts to access the mutex by performing
a blocking 'take'. The blocking task has a lower priority than the a blocking 'take'. The blocking task has a lower priority than the
controlling task so by the time it executes the mutex has already been controlling task so by the time it executes the mutex has already been
taken by the controlling task, causing the blocking task to block. It taken by the controlling task, causing the blocking task to block. It
does not unblock until the controlling task has given the mutex back, does not unblock until the controlling task has given the mutex back,
and it does not actually run until the controlling task has suspended and it does not actually run until the controlling task has suspended
itself (due to the relative priorities). When it eventually does obtain itself (due to the relative priorities). When it eventually does obtain
the mutex all it does is give the mutex back prior to also suspending the mutex all it does is give the mutex back prior to also suspending
itself. At this point both the controlling task and the blocking task are itself. At this point both the controlling task and the blocking task are
suspended. suspended.
prvRecursiveMutexPollingTask() runs at the idle priority. It spins round prvRecursiveMutexPollingTask() runs at the idle priority. It spins round
a tight loop attempting to obtain the mutex with a non-blocking call. As a tight loop attempting to obtain the mutex with a non-blocking call. As
the lowest priority task it will not successfully obtain the mutex until the lowest priority task it will not successfully obtain the mutex until
both the controlling and blocking tasks are suspended. Once it eventually both the controlling and blocking tasks are suspended. Once it eventually
does obtain the mutex it first unsuspends both the controlling task and does obtain the mutex it first unsuspends both the controlling task and
blocking task prior to giving the mutex back - resulting in the polling blocking task prior to giving the mutex back - resulting in the polling
task temporarily inheriting the controlling tasks priority. task temporarily inheriting the controlling tasks priority.
*/ */
/* Scheduler include files. */ /* Scheduler include files. */
#include "FreeRTOS.h" #include "FreeRTOS.h"
#include "task.h" #include "task.h"
#include "semphr.h" #include "semphr.h"
/* Demo app include files. */ /* Demo app include files. */
#include "recmutex.h" #include "recmutex.h"
/* Priorities assigned to the three tasks. */ /* Priorities assigned to the three tasks. */
#define recmuCONTROLLING_TASK_PRIORITY ( tskIDLE_PRIORITY + 2 ) #define recmuCONTROLLING_TASK_PRIORITY ( tskIDLE_PRIORITY + 2 )
#define recmuBLOCKING_TASK_PRIORITY ( tskIDLE_PRIORITY + 1 ) #define recmuBLOCKING_TASK_PRIORITY ( tskIDLE_PRIORITY + 1 )
#define recmuPOLLING_TASK_PRIORITY ( tskIDLE_PRIORITY + 0 ) #define recmuPOLLING_TASK_PRIORITY ( tskIDLE_PRIORITY + 0 )
/* The recursive call depth. */ /* The recursive call depth. */
#define recmuMAX_COUNT ( 10 ) #define recmuMAX_COUNT ( 10 )
/* Misc. */ /* Misc. */
#define recmuSHORT_DELAY ( 20 / portTICK_RATE_MS ) #define recmuSHORT_DELAY ( 20 / portTICK_RATE_MS )
#define recmuNO_DELAY ( ( portTickType ) 0 ) #define recmuNO_DELAY ( ( portTickType ) 0 )
#define recmuTWO_TICK_DELAY ( ( portTickType ) 2 ) #define recmuTWO_TICK_DELAY ( ( portTickType ) 2 )
/* The three tasks as described at the top of this file. */ /* The three tasks as described at the top of this file. */
static void prvRecursiveMutexControllingTask( void *pvParameters ); static void prvRecursiveMutexControllingTask( void *pvParameters );
static void prvRecursiveMutexBlockingTask( void *pvParameters ); static void prvRecursiveMutexBlockingTask( void *pvParameters );
static void prvRecursiveMutexPollingTask( void *pvParameters ); static void prvRecursiveMutexPollingTask( void *pvParameters );
/* The mutex used by the demo. */ /* The mutex used by the demo. */
static xSemaphoreHandle xMutex; static xSemaphoreHandle xMutex;
/* Variables used to detect and latch errors. */ /* Variables used to detect and latch errors. */
static portBASE_TYPE xErrorOccurred = pdFALSE, xControllingIsSuspended = pdFALSE, xBlockingIsSuspended = pdFALSE; static volatile portBASE_TYPE xErrorOccurred = pdFALSE, xControllingIsSuspended = pdFALSE, xBlockingIsSuspended = pdFALSE;
static unsigned portBASE_TYPE uxControllingCycles = 0, uxBlockingCycles, uxPollingCycles = 0; static volatile unsigned portBASE_TYPE uxControllingCycles = 0, uxBlockingCycles, uxPollingCycles = 0;
/* Handles of the two higher priority tasks, required so they can be resumed /* Handles of the two higher priority tasks, required so they can be resumed
(unsuspended). */ (unsuspended). */
static xTaskHandle xControllingTaskHandle, xBlockingTaskHandle; static xTaskHandle xControllingTaskHandle, xBlockingTaskHandle;
/*-----------------------------------------------------------*/ /*-----------------------------------------------------------*/
void vStartRecursiveMutexTasks( void ) void vStartRecursiveMutexTasks( void )
{ {
/* Just creates the mutex and the three tasks. */ /* Just creates the mutex and the three tasks. */
xMutex = xSemaphoreCreateRecursiveMutex(); xMutex = xSemaphoreCreateRecursiveMutex();
if( xMutex != NULL ) if( xMutex != NULL )
{ {
xTaskCreate( prvRecursiveMutexControllingTask, "Rec1", configMINIMAL_STACK_SIZE, NULL, recmuCONTROLLING_TASK_PRIORITY, &xControllingTaskHandle ); xTaskCreate( prvRecursiveMutexControllingTask, "Rec1", configMINIMAL_STACK_SIZE, NULL, recmuCONTROLLING_TASK_PRIORITY, &xControllingTaskHandle );
xTaskCreate( prvRecursiveMutexBlockingTask, "Rec2", configMINIMAL_STACK_SIZE, NULL, recmuBLOCKING_TASK_PRIORITY, &xBlockingTaskHandle ); xTaskCreate( prvRecursiveMutexBlockingTask, "Rec2", configMINIMAL_STACK_SIZE, NULL, recmuBLOCKING_TASK_PRIORITY, &xBlockingTaskHandle );
xTaskCreate( prvRecursiveMutexPollingTask, "Rec3", configMINIMAL_STACK_SIZE, NULL, recmuPOLLING_TASK_PRIORITY, NULL ); xTaskCreate( prvRecursiveMutexPollingTask, "Rec3", configMINIMAL_STACK_SIZE, NULL, recmuPOLLING_TASK_PRIORITY, NULL );
} }
} }
/*-----------------------------------------------------------*/ /*-----------------------------------------------------------*/
static void prvRecursiveMutexControllingTask( void *pvParameters ) static void prvRecursiveMutexControllingTask( void *pvParameters )
{ {
unsigned portBASE_TYPE ux; unsigned portBASE_TYPE ux;
for( ;; ) for( ;; )
{ {
/* Should not be able to 'give' the mutex, as we have not yet 'taken' /* Should not be able to 'give' the mutex, as we have not yet 'taken'
it. */ it. */
if( xSemaphoreGiveRecursive( xMutex ) == pdPASS ) if( xSemaphoreGiveRecursive( xMutex ) == pdPASS )
{ {
xErrorOccurred = pdTRUE; xErrorOccurred = pdTRUE;
} }
for( ux = 0; ux < recmuMAX_COUNT; ux++ ) for( ux = 0; ux < recmuMAX_COUNT; ux++ )
{ {
/* We should now be able to take the mutex as many times as /* We should now be able to take the mutex as many times as
we like. A one tick delay is used so the polling task will we like. A one tick delay is used so the polling task will
inherit our priority on all but the first cycle of this task. inherit our priority on all but the first cycle of this task.
If we did not block attempting to receive the mutex then no If we did not block attempting to receive the mutex then no
priority inheritance would occur. */ priority inheritance would occur. */
if( xSemaphoreTakeRecursive( xMutex, recmuTWO_TICK_DELAY ) != pdPASS ) if( xSemaphoreTakeRecursive( xMutex, recmuTWO_TICK_DELAY ) != pdPASS )
{ {
xErrorOccurred = pdTRUE; xErrorOccurred = pdTRUE;
} }
/* Ensure the other task attempting to access the mutex (and the /* Ensure the other task attempting to access the mutex (and the
other demo tasks) are able to execute. */ other demo tasks) are able to execute. */
vTaskDelay( recmuSHORT_DELAY ); vTaskDelay( recmuSHORT_DELAY );
} }
/* For each time we took the mutex, give it back. */ /* For each time we took the mutex, give it back. */
for( ux = 0; ux < recmuMAX_COUNT; ux++ ) for( ux = 0; ux < recmuMAX_COUNT; ux++ )
{ {
/* Ensure the other task attempting to access the mutex (and the /* Ensure the other task attempting to access the mutex (and the
other demo tasks) are able to execute. */ other demo tasks) are able to execute. */
vTaskDelay( recmuSHORT_DELAY ); vTaskDelay( recmuSHORT_DELAY );
/* We should now be able to give the mutex as many times as we /* We should now be able to give the mutex as many times as we
took it. */ took it. */
if( xSemaphoreGiveRecursive( xMutex ) != pdPASS ) if( xSemaphoreGiveRecursive( xMutex ) != pdPASS )
{ {
xErrorOccurred = pdTRUE; xErrorOccurred = pdTRUE;
} }
} }
/* Having given it back the same number of times as it was taken, we /* Having given it back the same number of times as it was taken, we
should no longer be the mutex owner, so the next give sh ould fail. */ should no longer be the mutex owner, so the next give sh ould fail. */
if( xSemaphoreGiveRecursive( xMutex ) == pdPASS ) if( xSemaphoreGiveRecursive( xMutex ) == pdPASS )
{ {
xErrorOccurred = pdTRUE; xErrorOccurred = pdTRUE;
} }
/* Keep count of the number of cycles this task has performed so a /* Keep count of the number of cycles this task has performed so a
stall can be detected. */ stall can be detected. */
uxControllingCycles++; uxControllingCycles++;
/* Suspend ourselves to the blocking task can execute. */ /* Suspend ourselves to the blocking task can execute. */
xControllingIsSuspended = pdTRUE; xControllingIsSuspended = pdTRUE;
vTaskSuspend( NULL ); vTaskSuspend( NULL );
xControllingIsSuspended = pdFALSE; xControllingIsSuspended = pdFALSE;
} }
} }
/*-----------------------------------------------------------*/ /*-----------------------------------------------------------*/
static void prvRecursiveMutexBlockingTask( void *pvParameters ) static void prvRecursiveMutexBlockingTask( void *pvParameters )
{ {
for( ;; ) for( ;; )
{ {
/* Attempt to obtain the mutex. We should block until the /* Attempt to obtain the mutex. We should block until the
controlling task has given up the mutex, and not actually execute controlling task has given up the mutex, and not actually execute
past this call until the controlling task is suspended. */ past this call until the controlling task is suspended. */
if( xSemaphoreTakeRecursive( xMutex, portMAX_DELAY ) == pdPASS ) if( xSemaphoreTakeRecursive( xMutex, portMAX_DELAY ) == pdPASS )
{ {
if( xControllingIsSuspended != pdTRUE ) if( xControllingIsSuspended != pdTRUE )
{ {
/* Did not expect to execute until the controlling task was /* Did not expect to execute until the controlling task was
suspended. */ suspended. */
xErrorOccurred = pdTRUE; xErrorOccurred = pdTRUE;
} }
else else
{ {
/* Give the mutex back before suspending ourselves to allow /* Give the mutex back before suspending ourselves to allow
the polling task to obtain the mutex. */ the polling task to obtain the mutex. */
if( xSemaphoreGiveRecursive( xMutex ) != pdPASS ) if( xSemaphoreGiveRecursive( xMutex ) != pdPASS )
{ {
xErrorOccurred = pdTRUE; xErrorOccurred = pdTRUE;
} }
xBlockingIsSuspended = pdTRUE; xBlockingIsSuspended = pdTRUE;
vTaskSuspend( NULL ); vTaskSuspend( NULL );
xBlockingIsSuspended = pdFALSE; xBlockingIsSuspended = pdFALSE;
} }
} }
else else
{ {
/* We should not leave the xSemaphoreTakeRecursive() function /* We should not leave the xSemaphoreTakeRecursive() function
until the mutex was obtained. */ until the mutex was obtained. */
xErrorOccurred = pdTRUE; xErrorOccurred = pdTRUE;
} }
/* The controlling and blocking tasks should be in lock step. */ /* The controlling and blocking tasks should be in lock step. */
if( uxControllingCycles != ( uxBlockingCycles + 1 ) ) if( uxControllingCycles != ( uxBlockingCycles + 1 ) )
{ {
xErrorOccurred = pdTRUE; xErrorOccurred = pdTRUE;
} }
/* Keep count of the number of cycles this task has performed so a /* Keep count of the number of cycles this task has performed so a
stall can be detected. */ stall can be detected. */
uxBlockingCycles++; uxBlockingCycles++;
} }
} }
/*-----------------------------------------------------------*/ /*-----------------------------------------------------------*/
static void prvRecursiveMutexPollingTask( void *pvParameters ) static void prvRecursiveMutexPollingTask( void *pvParameters )
{ {
for( ;; ) for( ;; )
{ {
/* Keep attempting to obtain the mutex. We should only obtain it when /* Keep attempting to obtain the mutex. We should only obtain it when
the blocking task has suspended itself. */ the blocking task has suspended itself. */
if( xSemaphoreTakeRecursive( xMutex, recmuNO_DELAY ) == pdPASS ) if( xSemaphoreTakeRecursive( xMutex, recmuNO_DELAY ) == pdPASS )
{ {
/* Is the blocking task suspended? */ /* Is the blocking task suspended? */
if( xBlockingIsSuspended != pdTRUE ) if( xBlockingIsSuspended != pdTRUE )
{ {
xErrorOccurred = pdTRUE; xErrorOccurred = pdTRUE;
} }
else else
{ {
/* Keep count of the number of cycles this task has performed so /* Keep count of the number of cycles this task has performed so
a stall can be detected. */ a stall can be detected. */
uxPollingCycles++; uxPollingCycles++;
/* We can resume the other tasks here even though they have a /* We can resume the other tasks here even though they have a
higher priority than the polling task. When they execute they higher priority than the polling task. When they execute they
will attempt to obtain the mutex but fail because the polling will attempt to obtain the mutex but fail because the polling
task is still the mutex holder. The polling task (this task) task is still the mutex holder. The polling task (this task)
will then inherit the higher priority. */ will then inherit the higher priority. */
vTaskResume( xBlockingTaskHandle ); vTaskResume( xBlockingTaskHandle );
vTaskResume( xControllingTaskHandle ); vTaskResume( xControllingTaskHandle );
/* Release the mutex, disinheriting the higher priority again. */ /* Release the mutex, disinheriting the higher priority again. */
if( xSemaphoreGiveRecursive( xMutex ) != pdPASS ) if( xSemaphoreGiveRecursive( xMutex ) != pdPASS )
{ {
xErrorOccurred = pdTRUE; xErrorOccurred = pdTRUE;
} }
} }
} }
#if configUSE_PREEMPTION == 0 #if configUSE_PREEMPTION == 0
{ {
taskYIELD(); taskYIELD();
} }
#endif #endif
} }
} }
/*-----------------------------------------------------------*/ /*-----------------------------------------------------------*/
/* This is called to check that all the created tasks are still running. */ /* This is called to check that all the created tasks are still running. */
portBASE_TYPE xAreRecursiveMutexTasksStillRunning( void ) portBASE_TYPE xAreRecursiveMutexTasksStillRunning( void )
{ {
portBASE_TYPE xReturn; portBASE_TYPE xReturn;
static unsigned portBASE_TYPE uxLastControllingCycles = 0, uxLastBlockingCycles = 0, uxLastPollingCycles = 0; static unsigned portBASE_TYPE uxLastControllingCycles = 0, uxLastBlockingCycles = 0, uxLastPollingCycles = 0;
/* Is the controlling task still cycling? */ /* Is the controlling task still cycling? */
if( uxLastControllingCycles == uxControllingCycles ) if( uxLastControllingCycles == uxControllingCycles )
{ {
xErrorOccurred = pdTRUE; xErrorOccurred = pdTRUE;
} }
else else
{ {
uxLastControllingCycles = uxControllingCycles; uxLastControllingCycles = uxControllingCycles;
} }
/* Is the blocking task still cycling? */ /* Is the blocking task still cycling? */
if( uxLastBlockingCycles == uxBlockingCycles ) if( uxLastBlockingCycles == uxBlockingCycles )
{ {
xErrorOccurred = pdTRUE; xErrorOccurred = pdTRUE;
} }
else else
{ {
uxLastBlockingCycles = uxBlockingCycles; uxLastBlockingCycles = uxBlockingCycles;
} }
/* Is the polling task still cycling? */ /* Is the polling task still cycling? */
if( uxLastPollingCycles == uxPollingCycles ) if( uxLastPollingCycles == uxPollingCycles )
{ {
xErrorOccurred = pdTRUE; xErrorOccurred = pdTRUE;
} }
else else
{ {
uxLastPollingCycles = uxPollingCycles; uxLastPollingCycles = uxPollingCycles;
} }
if( xErrorOccurred == pdTRUE ) if( xErrorOccurred == pdTRUE )
{ {
xReturn = pdFAIL; xReturn = pdFAIL;
} }
else else
{ {
xReturn = pdTRUE; xReturn = pdTRUE;
} }
return xReturn; return xReturn;
} }