]> begriffs open source - cmsis-freertos/blob - Demo/Common/Minimal/countsem.c
Update cmsis_os2.c
[cmsis-freertos] / Demo / Common / Minimal / countsem.c
1 /*
2     FreeRTOS V9.0.0 - Copyright (C) 2016 Real Time Engineers Ltd.
3     All rights reserved
4
5     VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.
6
7     This file is part of the FreeRTOS distribution.
8
9     FreeRTOS is free software; you can redistribute it and/or modify it under
10     the terms of the GNU General Public License (version 2) as published by the
11     Free Software Foundation >>>> AND MODIFIED BY <<<< the FreeRTOS exception.
12
13     ***************************************************************************
14     >>!   NOTE: The modification to the GPL is included to allow you to     !<<
15     >>!   distribute a combined work that includes FreeRTOS without being   !<<
16     >>!   obliged to provide the source code for proprietary components     !<<
17     >>!   outside of the FreeRTOS kernel.                                   !<<
18     ***************************************************************************
19
20     FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY
21     WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
22     FOR A PARTICULAR PURPOSE.  Full license text is available on the following
23     link: http://www.freertos.org/a00114.html
24
25     ***************************************************************************
26      *                                                                       *
27      *    FreeRTOS provides completely free yet professionally developed,    *
28      *    robust, strictly quality controlled, supported, and cross          *
29      *    platform software that is more than just the market leader, it     *
30      *    is the industry's de facto standard.                               *
31      *                                                                       *
32      *    Help yourself get started quickly while simultaneously helping     *
33      *    to support the FreeRTOS project by purchasing a FreeRTOS           *
34      *    tutorial book, reference manual, or both:                          *
35      *    http://www.FreeRTOS.org/Documentation                              *
36      *                                                                       *
37     ***************************************************************************
38
39     http://www.FreeRTOS.org/FAQHelp.html - Having a problem?  Start by reading
40     the FAQ page "My application does not run, what could be wrong?".  Have you
41     defined configASSERT()?
42
43     http://www.FreeRTOS.org/support - In return for receiving this top quality
44     embedded software for free we request you assist our global community by
45     participating in the support forum.
46
47     http://www.FreeRTOS.org/training - Investing in training allows your team to
48     be as productive as possible as early as possible.  Now you can receive
49     FreeRTOS training directly from Richard Barry, CEO of Real Time Engineers
50     Ltd, and the world's leading authority on the world's leading RTOS.
51
52     http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
53     including FreeRTOS+Trace - an indispensable productivity tool, a DOS
54     compatible FAT file system, and our tiny thread aware UDP/IP stack.
55
56     http://www.FreeRTOS.org/labs - Where new FreeRTOS products go to incubate.
57     Come and try FreeRTOS+TCP, our new open source TCP/IP stack for FreeRTOS.
58
59     http://www.OpenRTOS.com - Real Time Engineers ltd. license FreeRTOS to High
60     Integrity Systems ltd. to sell under the OpenRTOS brand.  Low cost OpenRTOS
61     licenses offer ticketed support, indemnification and commercial middleware.
62
63     http://www.SafeRTOS.com - High Integrity Systems also provide a safety
64     engineered and independently SIL3 certified version for use in safety and
65     mission critical applications that require provable dependability.
66
67     1 tab == 4 spaces!
68 */
69
70
71 /*
72  * Simple demonstration of the usage of counting semaphore.
73  */
74
75 /* Scheduler include files. */
76 #include "FreeRTOS.h"
77 #include "task.h"
78 #include "semphr.h"
79
80 /* Demo program include files. */
81 #include "countsem.h"
82
83 /* The maximum count value that the semaphore used for the demo can hold. */
84 #define countMAX_COUNT_VALUE    ( 200 )
85
86 /* Constants used to indicate whether or not the semaphore should have been
87 created with its maximum count value, or its minimum count value.  These
88 numbers are used to ensure that the pointers passed in as the task parameters
89 are valid. */
90 #define countSTART_AT_MAX_COUNT ( 0xaa )
91 #define countSTART_AT_ZERO              ( 0x55 )
92
93 /* Two tasks are created for the test.  One uses a semaphore created with its
94 count value set to the maximum, and one with the count value set to zero. */
95 #define countNUM_TEST_TASKS             ( 2 )
96 #define countDONT_BLOCK                 ( 0 )
97
98 /*-----------------------------------------------------------*/
99
100 /* Flag that will be latched to pdTRUE should any unexpected behaviour be
101 detected in any of the tasks. */
102 static volatile BaseType_t xErrorDetected = pdFALSE;
103
104 /*-----------------------------------------------------------*/
105
106 /*
107  * The demo task.  This simply counts the semaphore up to its maximum value,
108  * the counts it back down again.  The result of each semaphore 'give' and
109  * 'take' is inspected, with an error being flagged if it is found not to be
110  * the expected result.
111  */
112 static void prvCountingSemaphoreTask( void *pvParameters );
113
114 /*
115  * Utility function to increment the semaphore count value up from zero to
116  * countMAX_COUNT_VALUE.
117  */
118 static void prvIncrementSemaphoreCount( SemaphoreHandle_t xSemaphore, UBaseType_t *puxLoopCounter );
119
120 /*
121  * Utility function to decrement the semaphore count value up from
122  * countMAX_COUNT_VALUE to zero.
123  */
124 static void prvDecrementSemaphoreCount( SemaphoreHandle_t xSemaphore, UBaseType_t *puxLoopCounter );
125
126 /*-----------------------------------------------------------*/
127
128 /* The structure that is passed into the task as the task parameter. */
129 typedef struct COUNT_SEM_STRUCT
130 {
131         /* The semaphore to be used for the demo. */
132         SemaphoreHandle_t xSemaphore;
133
134         /* Set to countSTART_AT_MAX_COUNT if the semaphore should be created with
135         its count value set to its max count value, or countSTART_AT_ZERO if it
136         should have been created with its count value set to 0. */
137         UBaseType_t uxExpectedStartCount;
138
139         /* Incremented on each cycle of the demo task.  Used to detect a stalled
140         task. */
141         UBaseType_t uxLoopCounter;
142 } xCountSemStruct;
143
144 /* Two structures are defined, one is passed to each test task. */
145 static volatile xCountSemStruct xParameters[ countNUM_TEST_TASKS ];
146
147 /*-----------------------------------------------------------*/
148
149 void vStartCountingSemaphoreTasks( void )
150 {
151         /* Create the semaphores that we are going to use for the test/demo.  The
152         first should be created such that it starts at its maximum count value,
153         the second should be created such that it starts with a count value of zero. */
154         xParameters[ 0 ].xSemaphore = xSemaphoreCreateCounting( countMAX_COUNT_VALUE, countMAX_COUNT_VALUE );
155         xParameters[ 0 ].uxExpectedStartCount = countSTART_AT_MAX_COUNT;
156         xParameters[ 0 ].uxLoopCounter = 0;
157
158         xParameters[ 1 ].xSemaphore = xSemaphoreCreateCounting( countMAX_COUNT_VALUE, 0 );
159         xParameters[ 1 ].uxExpectedStartCount = 0;
160         xParameters[ 1 ].uxLoopCounter = 0;
161
162         /* Were the semaphores created? */
163         if( ( xParameters[ 0 ].xSemaphore != NULL ) || ( xParameters[ 1 ].xSemaphore != NULL ) )
164         {
165                 /* vQueueAddToRegistry() adds the semaphore to the registry, if one is
166                 in use.  The registry is provided as a means for kernel aware
167                 debuggers to locate semaphores and has no purpose if a kernel aware
168                 debugger is not being used.  The call to vQueueAddToRegistry() will be
169                 removed by the pre-processor if configQUEUE_REGISTRY_SIZE is not
170                 defined or is defined to be less than 1. */
171                 vQueueAddToRegistry( ( QueueHandle_t ) xParameters[ 0 ].xSemaphore, "Counting_Sem_1" );
172                 vQueueAddToRegistry( ( QueueHandle_t ) xParameters[ 1 ].xSemaphore, "Counting_Sem_2" );
173
174                 /* Create the demo tasks, passing in the semaphore to use as the parameter. */
175                 xTaskCreate( prvCountingSemaphoreTask, "CNT1", configMINIMAL_STACK_SIZE, ( void * ) &( xParameters[ 0 ] ), tskIDLE_PRIORITY, NULL );
176                 xTaskCreate( prvCountingSemaphoreTask, "CNT2", configMINIMAL_STACK_SIZE, ( void * ) &( xParameters[ 1 ] ), tskIDLE_PRIORITY, NULL );
177         }
178 }
179 /*-----------------------------------------------------------*/
180
181 static void prvDecrementSemaphoreCount( SemaphoreHandle_t xSemaphore, UBaseType_t *puxLoopCounter )
182 {
183 UBaseType_t ux;
184
185         /* If the semaphore count is at its maximum then we should not be able to
186         'give' the semaphore. */
187         if( xSemaphoreGive( xSemaphore ) == pdPASS )
188         {
189                 xErrorDetected = pdTRUE;
190         }
191
192         /* We should be able to 'take' the semaphore countMAX_COUNT_VALUE times. */
193         for( ux = 0; ux < countMAX_COUNT_VALUE; ux++ )
194         {
195                 configASSERT( uxSemaphoreGetCount( xSemaphore ) == ( countMAX_COUNT_VALUE - ux ) );
196
197                 if( xSemaphoreTake( xSemaphore, countDONT_BLOCK ) != pdPASS )
198                 {
199                         /* We expected to be able to take the semaphore. */
200                         xErrorDetected = pdTRUE;
201                 }
202
203                 ( *puxLoopCounter )++;
204         }
205
206         #if configUSE_PREEMPTION == 0
207                 taskYIELD();
208         #endif
209
210         /* If the semaphore count is zero then we should not be able to 'take'
211         the semaphore. */
212         configASSERT( uxSemaphoreGetCount( xSemaphore ) == 0 );
213         if( xSemaphoreTake( xSemaphore, countDONT_BLOCK ) == pdPASS )
214         {
215                 xErrorDetected = pdTRUE;
216         }
217 }
218 /*-----------------------------------------------------------*/
219
220 static void prvIncrementSemaphoreCount( SemaphoreHandle_t xSemaphore, UBaseType_t *puxLoopCounter )
221 {
222 UBaseType_t ux;
223
224         /* If the semaphore count is zero then we should not be able to 'take'
225         the semaphore. */
226         if( xSemaphoreTake( xSemaphore, countDONT_BLOCK ) == pdPASS )
227         {
228                 xErrorDetected = pdTRUE;
229         }
230
231         /* We should be able to 'give' the semaphore countMAX_COUNT_VALUE times. */
232         for( ux = 0; ux < countMAX_COUNT_VALUE; ux++ )
233         {
234                 configASSERT( uxSemaphoreGetCount( xSemaphore ) == ux );
235
236                 if( xSemaphoreGive( xSemaphore ) != pdPASS )
237                 {
238                         /* We expected to be able to take the semaphore. */
239                         xErrorDetected = pdTRUE;
240                 }
241
242                 ( *puxLoopCounter )++;
243         }
244
245         #if configUSE_PREEMPTION == 0
246                 taskYIELD();
247         #endif
248
249         /* If the semaphore count is at its maximum then we should not be able to
250         'give' the semaphore. */
251         if( xSemaphoreGive( xSemaphore ) == pdPASS )
252         {
253                 xErrorDetected = pdTRUE;
254         }
255 }
256 /*-----------------------------------------------------------*/
257
258 static void prvCountingSemaphoreTask( void *pvParameters )
259 {
260 xCountSemStruct *pxParameter;
261
262         #ifdef USE_STDIO
263         void vPrintDisplayMessage( const char * const * ppcMessageToSend );
264
265                 const char * const pcTaskStartMsg = "Counting semaphore demo started.\r\n";
266
267                 /* Queue a message for printing to say the task has started. */
268                 vPrintDisplayMessage( &pcTaskStartMsg );
269         #endif
270
271         /* The semaphore to be used was passed as the parameter. */
272         pxParameter = ( xCountSemStruct * ) pvParameters;
273
274         /* Did we expect to find the semaphore already at its max count value, or
275         at zero? */
276         if( pxParameter->uxExpectedStartCount == countSTART_AT_MAX_COUNT )
277         {
278                 prvDecrementSemaphoreCount( pxParameter->xSemaphore, &( pxParameter->uxLoopCounter ) );
279         }
280
281         /* Now we expect the semaphore count to be 0, so this time there is an
282         error if we can take the semaphore. */
283         if( xSemaphoreTake( pxParameter->xSemaphore, 0 ) == pdPASS )
284         {
285                 xErrorDetected = pdTRUE;
286         }
287
288         for( ;; )
289         {
290                 prvIncrementSemaphoreCount( pxParameter->xSemaphore, &( pxParameter->uxLoopCounter ) );
291                 prvDecrementSemaphoreCount( pxParameter->xSemaphore, &( pxParameter->uxLoopCounter ) );
292         }
293 }
294 /*-----------------------------------------------------------*/
295
296 BaseType_t xAreCountingSemaphoreTasksStillRunning( void )
297 {
298 static UBaseType_t uxLastCount0 = 0, uxLastCount1 = 0;
299 BaseType_t xReturn = pdPASS;
300
301         /* Return fail if any 'give' or 'take' did not result in the expected
302         behaviour. */
303         if( xErrorDetected != pdFALSE )
304         {
305                 xReturn = pdFAIL;
306         }
307
308         /* Return fail if either task is not still incrementing its loop counter. */
309         if( uxLastCount0 == xParameters[ 0 ].uxLoopCounter )
310         {
311                 xReturn = pdFAIL;
312         }
313         else
314         {
315                 uxLastCount0 = xParameters[ 0 ].uxLoopCounter;
316         }
317
318         if( uxLastCount1 == xParameters[ 1 ].uxLoopCounter )
319         {
320                 xReturn = pdFAIL;
321         }
322         else
323         {
324                 uxLastCount1 = xParameters[ 1 ].uxLoopCounter;
325         }
326
327         return xReturn;
328 }
329
330