1 |
/* |
---|
2 |
FreeRTOS.org V5.0.4 - Copyright (C) 2003-2008 Richard Barry. |
---|
3 |
|
---|
4 |
This file is part of the FreeRTOS.org distribution. |
---|
5 |
|
---|
6 |
FreeRTOS.org is free software; you can redistribute it and/or modify |
---|
7 |
it under the terms of the GNU General Public License as published by |
---|
8 |
the Free Software Foundation; either version 2 of the License, or |
---|
9 |
(at your option) any later version. |
---|
10 |
|
---|
11 |
FreeRTOS.org is distributed in the hope that it will be useful, |
---|
12 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
13 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
14 |
GNU General Public License for more details. |
---|
15 |
|
---|
16 |
You should have received a copy of the GNU General Public License |
---|
17 |
along with FreeRTOS.org; if not, write to the Free Software |
---|
18 |
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
---|
19 |
|
---|
20 |
A special exception to the GPL can be applied should you wish to distribute |
---|
21 |
a combined work that includes FreeRTOS.org, without being obliged to provide |
---|
22 |
the source code for any proprietary components. See the licensing section |
---|
23 |
of http://www.FreeRTOS.org for full details of how and when the exception |
---|
24 |
can be applied. |
---|
25 |
|
---|
26 |
*************************************************************************** |
---|
27 |
*************************************************************************** |
---|
28 |
* * |
---|
29 |
* SAVE TIME AND MONEY! We can port FreeRTOS.org to your own hardware, * |
---|
30 |
* and even write all or part of your application on your behalf. * |
---|
31 |
* See http://www.OpenRTOS.com for details of the services we provide to * |
---|
32 |
* expedite your project. * |
---|
33 |
* * |
---|
34 |
*************************************************************************** |
---|
35 |
*************************************************************************** |
---|
36 |
|
---|
37 |
Please ensure to read the configuration and relevant port sections of the |
---|
38 |
online documentation. |
---|
39 |
|
---|
40 |
http://www.FreeRTOS.org - Documentation, latest information, license and |
---|
41 |
contact details. |
---|
42 |
|
---|
43 |
http://www.SafeRTOS.com - A version that is certified for use in safety |
---|
44 |
critical systems. |
---|
45 |
|
---|
46 |
http://www.OpenRTOS.com - Commercial support, development, porting, |
---|
47 |
licensing and training services. |
---|
48 |
*/ |
---|
49 |
|
---|
50 |
/* FreeRTOS.org includes. */ |
---|
51 |
#include "FreeRTOS.h" |
---|
52 |
#include "task.h" |
---|
53 |
#include "queue.h" |
---|
54 |
|
---|
55 |
/* Demo includes. */ |
---|
56 |
#include "basic_io.h" |
---|
57 |
|
---|
58 |
|
---|
59 |
/* The tasks to be created. Two instances are created of the sender task while |
---|
60 |
only a single instance is created of the receiver task. */ |
---|
61 |
static void vSenderTask( void *pvParameters ); |
---|
62 |
static void vReceiverTask( void *pvParameters ); |
---|
63 |
|
---|
64 |
/*-----------------------------------------------------------*/ |
---|
65 |
|
---|
66 |
/* Declare a variable of type xQueueHandle. This is used to store the queue |
---|
67 |
that is accessed by all three tasks. */ |
---|
68 |
xQueueHandle xQueue; |
---|
69 |
|
---|
70 |
|
---|
71 |
int main( void ) |
---|
72 |
{ |
---|
73 |
/* The queue is created to hold a maximum of 5 long values. */ |
---|
74 |
xQueue = xQueueCreate( 5, sizeof( long ) ); |
---|
75 |
|
---|
76 |
if( xQueue != NULL ) |
---|
77 |
{ |
---|
78 |
/* Create two instances of the task that will write to the queue. The |
---|
79 |
parameter is used to pass the value that the task should write to the queue, |
---|
80 |
so one task will continuously write 100 to the queue while the other task |
---|
81 |
will continuously write 200 to the queue. Both tasks are created at |
---|
82 |
priority 1. */ |
---|
83 |
xTaskCreate( vSenderTask, "Sender1", 1000, ( void * ) 100, 1, NULL ); |
---|
84 |
xTaskCreate( vSenderTask, "Sender2", 1000, ( void * ) 200, 1, NULL ); |
---|
85 |
|
---|
86 |
/* Create the task that will read from the queue. The task is created with |
---|
87 |
priority 2, so above the priority of the sender tasks. */ |
---|
88 |
xTaskCreate( vReceiverTask, "Receiver", 1000, NULL, 2, NULL ); |
---|
89 |
|
---|
90 |
/* Start the scheduler so the created tasks start executing. */ |
---|
91 |
vTaskStartScheduler(); |
---|
92 |
} |
---|
93 |
else |
---|
94 |
{ |
---|
95 |
/* The queue could not be created. */ |
---|
96 |
} |
---|
97 |
|
---|
98 |
/* If all is well we will never reach here as the scheduler will now be |
---|
99 |
running the tasks. If we do reach here then it is likely that there was |
---|
100 |
insufficient heap memory available for a resource to be created. */ |
---|
101 |
for( ;; ); |
---|
102 |
return 0; |
---|
103 |
} |
---|
104 |
/*-----------------------------------------------------------*/ |
---|
105 |
|
---|
106 |
static void vSenderTask( void *pvParameters ) |
---|
107 |
{ |
---|
108 |
long lValueToSend; |
---|
109 |
portBASE_TYPE xStatus; |
---|
110 |
|
---|
111 |
/* Two instances are created of this task so the value that is sent to the |
---|
112 |
queue is passed in via the task parameter rather than be hard coded. This way |
---|
113 |
each instance can use a different value. Cast the parameter to the required |
---|
114 |
type. */ |
---|
115 |
lValueToSend = ( long ) pvParameters; |
---|
116 |
|
---|
117 |
/* As per most tasks, this task is implemented within an infinite loop. */ |
---|
118 |
for( ;; ) |
---|
119 |
{ |
---|
120 |
/* The first parameter is the queue to which data is being sent. The |
---|
121 |
queue was created before the scheduler was started, so before this task |
---|
122 |
started to execute. |
---|
123 |
|
---|
124 |
The second parameter is the address of the data to be sent. |
---|
125 |
|
---|
126 |
The third parameter is the Block time the time the task should be kept |
---|
127 |
in the Blocked state to wait for space to become available on the queue |
---|
128 |
should the queue already be full. In this case we dont specify a block |
---|
129 |
time because there should always be space in the queue. */ |
---|
130 |
xStatus = xQueueSendToBack( xQueue, &lValueToSend, 0 ); |
---|
131 |
|
---|
132 |
if( xStatus != pdPASS ) |
---|
133 |
{ |
---|
134 |
/* We could not write to the queue because it was full this must |
---|
135 |
be an error as the queue should never contain more than one item! */ |
---|
136 |
vPrintString( "Could not send to the queue.\r\n" ); |
---|
137 |
} |
---|
138 |
|
---|
139 |
/* Allow the other sender task to execute. */ |
---|
140 |
taskYIELD(); |
---|
141 |
} |
---|
142 |
} |
---|
143 |
/*-----------------------------------------------------------*/ |
---|
144 |
|
---|
145 |
static void vReceiverTask( void *pvParameters ) |
---|
146 |
{ |
---|
147 |
/* Declare the variable that will hold the values received from the queue. */ |
---|
148 |
long lReceivedValue; |
---|
149 |
portBASE_TYPE xStatus; |
---|
150 |
const portTickType xTicksToWait = 100 / portTICK_RATE_MS; |
---|
151 |
|
---|
152 |
/* This task is also defined within an infinite loop. */ |
---|
153 |
for( ;; ) |
---|
154 |
{ |
---|
155 |
/* As this task unblocks immediately that data is written to the queue this |
---|
156 |
call should always find the queue empty. */ |
---|
157 |
if( uxQueueMessagesWaiting( xQueue ) != 0 ) |
---|
158 |
{ |
---|
159 |
vPrintString( "Queue should have been empty!\r\n" ); |
---|
160 |
} |
---|
161 |
|
---|
162 |
/* The first parameter is the queue from which data is to be received. The |
---|
163 |
queue is created before the scheduler is started, and therefore before this |
---|
164 |
task runs for the first time. |
---|
165 |
|
---|
166 |
The second parameter is the buffer into which the received data will be |
---|
167 |
placed. In this case the buffer is simply the address of a variable that |
---|
168 |
has the required size to hold the received data. |
---|
169 |
|
---|
170 |
the last parameter is the block time the maximum amount of time that the |
---|
171 |
task should remain in the Blocked state to wait for data to be available should |
---|
172 |
the queue already be empty. */ |
---|
173 |
xStatus = xQueueReceive( xQueue, &lReceivedValue, xTicksToWait ); |
---|
174 |
|
---|
175 |
if( xStatus == pdPASS ) |
---|
176 |
{ |
---|
177 |
/* Data was successfully received from the queue, print out the received |
---|
178 |
value. */ |
---|
179 |
vPrintStringAndNumber( "Received = ", lReceivedValue ); |
---|
180 |
} |
---|
181 |
else |
---|
182 |
{ |
---|
183 |
/* We did not receive anything from the queue even after waiting for 100ms. |
---|
184 |
This must be an error as the sending tasks are free running and will be |
---|
185 |
continuously writing to the queue. */ |
---|
186 |
vPrintString( "Could not receive from the queue.\r\n" ); |
---|
187 |
} |
---|
188 |
} |
---|
189 |
} |
---|
190 |
|
---|
191 |
|
---|
192 |
|
---|
193 |
|
---|
194 |
|
---|
195 |
|
---|