]> begriffs open source - cmsis-freertos/blob - Demo/Common/Full/PollQ.c
Update cmsis_os2.c
[cmsis-freertos] / Demo / Common / Full / PollQ.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  * This is a very simple queue test.  See the BlockQ. c documentation for a more 
73  * comprehensive version.
74  *
75  * Creates two tasks that communicate over a single queue.  One task acts as a 
76  * producer, the other a consumer.  
77  *
78  * The producer loops for three iteration, posting an incrementing number onto the 
79  * queue each cycle.  It then delays for a fixed period before doing exactly the 
80  * same again.
81  *
82  * The consumer loops emptying the queue.  Each item removed from the queue is 
83  * checked to ensure it contains the expected value.  When the queue is empty it 
84  * blocks for a fixed period, then does the same again.
85  *
86  * All queue access is performed without blocking.  The consumer completely empties 
87  * the queue each time it runs so the producer should never find the queue full.  
88  *
89  * An error is flagged if the consumer obtains an unexpected value or the producer 
90  * find the queue is full.
91  *
92  * \page PollQC pollQ.c
93  * \ingroup DemoFiles
94  * <HR>
95  */
96
97 /*
98 Changes from V2.0.0
99
100         + Delay periods are now specified using variables and constants of
101           TickType_t rather than unsigned long.
102 */
103
104 #include <stdlib.h>
105
106 /* Scheduler include files. */
107 #include "FreeRTOS.h"
108 #include "task.h"
109 #include "queue.h"
110 #include "print.h"
111
112 /* Demo program include files. */
113 #include "PollQ.h"
114
115 #define pollqSTACK_SIZE         ( ( unsigned short ) configMINIMAL_STACK_SIZE )
116
117 /* The task that posts the incrementing number onto the queue. */
118 static void vPolledQueueProducer( void *pvParameters );
119
120 /* The task that empties the queue. */
121 static void vPolledQueueConsumer( void *pvParameters );
122
123 /* Variables that are used to check that the tasks are still running with no errors. */
124 static volatile short sPollingConsumerCount = 0, sPollingProducerCount = 0;
125 /*-----------------------------------------------------------*/
126
127 void vStartPolledQueueTasks( unsigned portBASE_TYPE uxPriority )
128 {
129 static QueueHandle_t xPolledQueue;
130 const unsigned portBASE_TYPE uxQueueSize = 10;
131
132         /* Create the queue used by the producer and consumer. */
133         xPolledQueue = xQueueCreate( uxQueueSize, ( unsigned portBASE_TYPE ) sizeof( unsigned short ) );
134
135         /* Spawn the producer and consumer. */
136         xTaskCreate( vPolledQueueConsumer, "QConsNB", pollqSTACK_SIZE, ( void * ) &xPolledQueue, uxPriority, NULL );
137         xTaskCreate( vPolledQueueProducer, "QProdNB", pollqSTACK_SIZE, ( void * ) &xPolledQueue, uxPriority, NULL );
138 }
139 /*-----------------------------------------------------------*/
140
141 static void vPolledQueueProducer( void *pvParameters )
142 {
143 unsigned short usValue = 0, usLoop;
144 QueueHandle_t *pxQueue;
145 const TickType_t xDelay = ( TickType_t ) 200 / portTICK_PERIOD_MS;
146 const unsigned short usNumToProduce = 3;
147 const char * const pcTaskStartMsg = "Polled queue producer started.\r\n";
148 const char * const pcTaskErrorMsg = "Could not post on polled queue.\r\n";
149 short sError = pdFALSE;
150
151         /* Queue a message for printing to say the task has started. */
152         vPrintDisplayMessage( &pcTaskStartMsg );
153
154         /* The queue being used is passed in as the parameter. */
155         pxQueue = ( QueueHandle_t * ) pvParameters;
156
157         for( ;; )
158         {               
159                 for( usLoop = 0; usLoop < usNumToProduce; ++usLoop )
160                 {
161                         /* Send an incrementing number on the queue without blocking. */
162                         if( xQueueSendToBack( *pxQueue, ( void * ) &usValue, ( TickType_t ) 0 ) != pdPASS )
163                         {
164                                 /* We should never find the queue full - this is an error. */
165                                 vPrintDisplayMessage( &pcTaskErrorMsg );
166                                 sError = pdTRUE;
167                         }
168                         else
169                         {
170                                 if( sError == pdFALSE )
171                                 {
172                                         /* If an error has ever been recorded we stop incrementing the 
173                                         check variable. */
174                                         ++sPollingProducerCount;
175                                 }
176
177                                 /* Update the value we are going to post next time around. */
178                                 ++usValue;
179                         }
180                 }
181
182                 /* Wait before we start posting again to ensure the consumer runs and 
183                 empties the queue. */
184                 vTaskDelay( xDelay );
185         }
186 }
187 /*-----------------------------------------------------------*/
188
189 static void vPolledQueueConsumer( void *pvParameters )
190 {
191 unsigned short usData, usExpectedValue = 0;
192 QueueHandle_t *pxQueue;
193 const TickType_t xDelay = ( TickType_t ) 200 / portTICK_PERIOD_MS;
194 const char * const pcTaskStartMsg = "Polled queue consumer started.\r\n";
195 const char * const pcTaskErrorMsg = "Incorrect value received on polled queue.\r\n";
196 short sError = pdFALSE;
197
198         /* Queue a message for printing to say the task has started. */
199         vPrintDisplayMessage( &pcTaskStartMsg );
200
201         /* The queue being used is passed in as the parameter. */
202         pxQueue = ( QueueHandle_t * ) pvParameters;
203
204         for( ;; )
205         {               
206                 /* Loop until the queue is empty. */
207                 while( uxQueueMessagesWaiting( *pxQueue ) )
208                 {
209                         if( xQueueReceive( *pxQueue, &usData, ( TickType_t ) 0 ) == pdPASS )
210                         {
211                                 if( usData != usExpectedValue )
212                                 {
213                                         /* This is not what we expected to receive so an error has 
214                                         occurred. */
215                                         vPrintDisplayMessage( &pcTaskErrorMsg );
216                                         sError = pdTRUE;
217                                         /* Catch-up to the value we received so our next expected value 
218                                         should again be correct. */
219                                         usExpectedValue = usData;
220                                 }
221                                 else
222                                 {
223                                         if( sError == pdFALSE )
224                                         {
225                                                 /* Only increment the check variable if no errors have 
226                                                 occurred. */
227                                                 ++sPollingConsumerCount;
228                                         }
229                                 }
230                                 ++usExpectedValue;
231                         }
232                 }
233
234                 /* Now the queue is empty we block, allowing the producer to place more 
235                 items in the queue. */
236                 vTaskDelay( xDelay );
237         }
238 }
239 /*-----------------------------------------------------------*/
240
241 /* This is called to check that all the created tasks are still running with no errors. */
242 portBASE_TYPE xArePollingQueuesStillRunning( void )
243 {
244 static short sLastPollingConsumerCount = 0, sLastPollingProducerCount = 0;
245 portBASE_TYPE xReturn;
246
247         if( ( sLastPollingConsumerCount == sPollingConsumerCount ) ||
248                 ( sLastPollingProducerCount == sPollingProducerCount ) 
249           )
250         {
251                 xReturn = pdFALSE;
252         }
253         else
254         {
255                 xReturn = pdTRUE;
256         }
257
258         sLastPollingConsumerCount = sPollingConsumerCount;
259         sLastPollingProducerCount = sPollingProducerCount;
260
261         return xReturn;
262 }