]> begriffs open source - cmsis-freertos/blob - Demo/Common/Minimal/countsem.c
Update README.md - branch main is now the base branch
[cmsis-freertos] / Demo / Common / Minimal / countsem.c
1 /*
2  * FreeRTOS V202111.00
3  * Copyright (C) 2020 Amazon.com, Inc. or its affiliates.  All Rights Reserved.
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a copy of
6  * this software and associated documentation files (the "Software"), to deal in
7  * the Software without restriction, including without limitation the rights to
8  * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
9  * the Software, and to permit persons to whom the Software is furnished to do so,
10  * subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice shall be included in all
13  * copies or substantial portions of the Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
17  * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
18  * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
19  * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21  *
22  * http://www.FreeRTOS.org
23  * http://aws.amazon.com/freertos
24  *
25  * 1 tab == 4 spaces!
26  */
27
28
29 /*
30  * Simple demonstration of the usage of counting semaphore.
31  */
32
33 /* Scheduler include files. */
34 #include "FreeRTOS.h"
35 #include "task.h"
36 #include "semphr.h"
37
38 /* Demo program include files. */
39 #include "countsem.h"
40
41 /* The maximum count value that the semaphore used for the demo can hold. */
42 #define countMAX_COUNT_VALUE       ( 200 )
43
44 /* Constants used to indicate whether or not the semaphore should have been
45  * created with its maximum count value, or its minimum count value.  These
46  * numbers are used to ensure that the pointers passed in as the task parameters
47  * are valid. */
48 #define countSTART_AT_MAX_COUNT    ( 0xaa )
49 #define countSTART_AT_ZERO         ( 0x55 )
50
51 /* Two tasks are created for the test.  One uses a semaphore created with its
52  * count value set to the maximum, and one with the count value set to zero. */
53 #define countNUM_TEST_TASKS        ( 2 )
54 #define countDONT_BLOCK            ( 0 )
55
56 /*-----------------------------------------------------------*/
57
58 /* Flag that will be latched to pdTRUE should any unexpected behaviour be
59  * detected in any of the tasks. */
60 static volatile BaseType_t xErrorDetected = pdFALSE;
61
62 /*-----------------------------------------------------------*/
63
64 /*
65  * The demo task.  This simply counts the semaphore up to its maximum value,
66  * the counts it back down again.  The result of each semaphore 'give' and
67  * 'take' is inspected, with an error being flagged if it is found not to be
68  * the expected result.
69  */
70 static void prvCountingSemaphoreTask( void * pvParameters );
71
72 /*
73  * Utility function to increment the semaphore count value up from zero to
74  * countMAX_COUNT_VALUE.
75  */
76 static void prvIncrementSemaphoreCount( SemaphoreHandle_t xSemaphore,
77                                         volatile UBaseType_t * puxLoopCounter );
78
79 /*
80  * Utility function to decrement the semaphore count value up from
81  * countMAX_COUNT_VALUE to zero.
82  */
83 static void prvDecrementSemaphoreCount( SemaphoreHandle_t xSemaphore,
84                                         volatile UBaseType_t * puxLoopCounter );
85
86 /*-----------------------------------------------------------*/
87
88 /* The structure that is passed into the task as the task parameter. */
89 typedef struct COUNT_SEM_STRUCT
90 {
91     /* The semaphore to be used for the demo. */
92     SemaphoreHandle_t xSemaphore;
93
94     /* Set to countSTART_AT_MAX_COUNT if the semaphore should be created with
95      * its count value set to its max count value, or countSTART_AT_ZERO if it
96      * should have been created with its count value set to 0. */
97     UBaseType_t uxExpectedStartCount;
98
99     /* Incremented on each cycle of the demo task.  Used to detect a stalled
100      * task. */
101     volatile UBaseType_t uxLoopCounter;
102 } xCountSemStruct;
103
104 /* Two structures are defined, one is passed to each test task. */
105 static xCountSemStruct xParameters[ countNUM_TEST_TASKS ];
106
107 /*-----------------------------------------------------------*/
108
109 void vStartCountingSemaphoreTasks( void )
110 {
111     /* Create the semaphores that we are going to use for the test/demo.  The
112      * first should be created such that it starts at its maximum count value,
113      * the second should be created such that it starts with a count value of zero. */
114     xParameters[ 0 ].xSemaphore = xSemaphoreCreateCounting( countMAX_COUNT_VALUE, countMAX_COUNT_VALUE );
115     xParameters[ 0 ].uxExpectedStartCount = countSTART_AT_MAX_COUNT;
116     xParameters[ 0 ].uxLoopCounter = 0;
117
118     xParameters[ 1 ].xSemaphore = xSemaphoreCreateCounting( countMAX_COUNT_VALUE, 0 );
119     xParameters[ 1 ].uxExpectedStartCount = 0;
120     xParameters[ 1 ].uxLoopCounter = 0;
121
122     /* Were the semaphores created? */
123     if( ( xParameters[ 0 ].xSemaphore != NULL ) || ( xParameters[ 1 ].xSemaphore != NULL ) )
124     {
125         /* vQueueAddToRegistry() adds the semaphore to the registry, if one is
126          * in use.  The registry is provided as a means for kernel aware
127          * debuggers to locate semaphores and has no purpose if a kernel aware
128          * debugger is not being used.  The call to vQueueAddToRegistry() will be
129          * removed by the pre-processor if configQUEUE_REGISTRY_SIZE is not
130          * defined or is defined to be less than 1. */
131         vQueueAddToRegistry( ( QueueHandle_t ) xParameters[ 0 ].xSemaphore, "Counting_Sem_1" );
132         vQueueAddToRegistry( ( QueueHandle_t ) xParameters[ 1 ].xSemaphore, "Counting_Sem_2" );
133
134         /* Create the demo tasks, passing in the semaphore to use as the parameter. */
135         xTaskCreate( prvCountingSemaphoreTask, "CNT1", configMINIMAL_STACK_SIZE, ( void * ) &( xParameters[ 0 ] ), tskIDLE_PRIORITY, NULL );
136         xTaskCreate( prvCountingSemaphoreTask, "CNT2", configMINIMAL_STACK_SIZE, ( void * ) &( xParameters[ 1 ] ), tskIDLE_PRIORITY, NULL );
137     }
138 }
139 /*-----------------------------------------------------------*/
140
141 static void prvDecrementSemaphoreCount( SemaphoreHandle_t xSemaphore,
142                                         volatile UBaseType_t * puxLoopCounter )
143 {
144     UBaseType_t ux;
145
146     /* If the semaphore count is at its maximum then we should not be able to
147      * 'give' the semaphore. */
148     if( xSemaphoreGive( xSemaphore ) == pdPASS )
149     {
150         xErrorDetected = pdTRUE;
151     }
152
153     /* We should be able to 'take' the semaphore countMAX_COUNT_VALUE times. */
154     for( ux = 0; ux < countMAX_COUNT_VALUE; ux++ )
155     {
156         configASSERT( uxSemaphoreGetCount( xSemaphore ) == ( countMAX_COUNT_VALUE - ux ) );
157
158         if( xSemaphoreTake( xSemaphore, countDONT_BLOCK ) != pdPASS )
159         {
160             /* We expected to be able to take the semaphore. */
161             xErrorDetected = pdTRUE;
162         }
163
164         ( *puxLoopCounter )++;
165     }
166
167     #if configUSE_PREEMPTION == 0
168         taskYIELD();
169     #endif
170
171     /* If the semaphore count is zero then we should not be able to     'take'
172      * the semaphore. */
173     configASSERT( uxSemaphoreGetCount( xSemaphore ) == 0 );
174
175     if( xSemaphoreTake( xSemaphore, countDONT_BLOCK ) == pdPASS )
176     {
177         xErrorDetected = pdTRUE;
178     }
179 }
180 /*-----------------------------------------------------------*/
181
182 static void prvIncrementSemaphoreCount( SemaphoreHandle_t xSemaphore,
183                                         volatile UBaseType_t * puxLoopCounter )
184 {
185     UBaseType_t ux;
186
187     /* If the semaphore count is zero then we should not be able to     'take'
188      * the semaphore. */
189     if( xSemaphoreTake( xSemaphore, countDONT_BLOCK ) == pdPASS )
190     {
191         xErrorDetected = pdTRUE;
192     }
193
194     /* We should be able to 'give' the semaphore countMAX_COUNT_VALUE times. */
195     for( ux = 0; ux < countMAX_COUNT_VALUE; ux++ )
196     {
197         configASSERT( uxSemaphoreGetCount( xSemaphore ) == ux );
198
199         if( xSemaphoreGive( xSemaphore ) != pdPASS )
200         {
201             /* We expected to be able to take the semaphore. */
202             xErrorDetected = pdTRUE;
203         }
204
205         ( *puxLoopCounter )++;
206     }
207
208     #if configUSE_PREEMPTION == 0
209         taskYIELD();
210     #endif
211
212     /* If the semaphore count is at its maximum then we should not be able to
213      * 'give' the semaphore. */
214     if( xSemaphoreGive( xSemaphore ) == pdPASS )
215     {
216         xErrorDetected = pdTRUE;
217     }
218 }
219 /*-----------------------------------------------------------*/
220
221 static void prvCountingSemaphoreTask( void * pvParameters )
222 {
223     xCountSemStruct * pxParameter;
224
225     #ifdef USE_STDIO
226         void vPrintDisplayMessage( const char * const * ppcMessageToSend );
227
228         const char * const pcTaskStartMsg = "Counting semaphore demo started.\r\n";
229
230         /* Queue a message for printing to say the task has started. */
231         vPrintDisplayMessage( &pcTaskStartMsg );
232     #endif
233
234     /* The semaphore to be used was passed as the parameter. */
235     pxParameter = ( xCountSemStruct * ) pvParameters;
236
237     /* Did we expect to find the semaphore already at its max count value, or
238      * at zero? */
239     if( pxParameter->uxExpectedStartCount == countSTART_AT_MAX_COUNT )
240     {
241         prvDecrementSemaphoreCount( pxParameter->xSemaphore, &( pxParameter->uxLoopCounter ) );
242     }
243
244     /* Now we expect the semaphore count to be 0, so this time there is an
245      * error if we can take the semaphore. */
246     if( xSemaphoreTake( pxParameter->xSemaphore, 0 ) == pdPASS )
247     {
248         xErrorDetected = pdTRUE;
249     }
250
251     for( ; ; )
252     {
253         prvIncrementSemaphoreCount( pxParameter->xSemaphore, &( pxParameter->uxLoopCounter ) );
254         prvDecrementSemaphoreCount( pxParameter->xSemaphore, &( pxParameter->uxLoopCounter ) );
255     }
256 }
257 /*-----------------------------------------------------------*/
258
259 BaseType_t xAreCountingSemaphoreTasksStillRunning( void )
260 {
261     static UBaseType_t uxLastCount0 = 0, uxLastCount1 = 0;
262     BaseType_t xReturn = pdPASS;
263
264     /* Return fail if any 'give' or 'take' did not result in the expected
265      * behaviour. */
266     if( xErrorDetected != pdFALSE )
267     {
268         xReturn = pdFAIL;
269     }
270
271     /* Return fail if either task is not still incrementing its loop counter. */
272     if( uxLastCount0 == xParameters[ 0 ].uxLoopCounter )
273     {
274         xReturn = pdFAIL;
275     }
276     else
277     {
278         uxLastCount0 = xParameters[ 0 ].uxLoopCounter;
279     }
280
281     if( uxLastCount1 == xParameters[ 1 ].uxLoopCounter )
282     {
283         xReturn = pdFAIL;
284     }
285     else
286     {
287         uxLastCount1 = xParameters[ 1 ].uxLoopCounter;
288     }
289
290     return xReturn;
291 }