root/Examples_CP-JR_ARM7_LPC2368/FreeRTOS_Book/Source-Code-For-Examples/Examples/Example011/main.c

Revision 36, 8.0 kB (checked in by phil, 15 years ago)

added purchased FreeRTOS book

Line 
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 #define mainSENDER_1            1
59 #define mainSENDER_2            2
60
61 /* The tasks to be created.  Two instances are created of the sender task while
62 only a single instance is created of the receiver task. */
63 static void vSenderTask( void *pvParameters );
64 static void vReceiverTask( void *pvParameters );
65
66 /*-----------------------------------------------------------*/
67
68 /* Declare a variable of type xQueueHandle.  This is used to store the queue
69 that is accessed by all three tasks. */
70 xQueueHandle xQueue;
71
72 /* Define the structure type that will be passed on the queue. */
73 typedef struct
74 {
75         unsigned char ucValue;
76         unsigned char ucSource;
77 } xData;
78
79 /* Declare two variables of type xData that will be passed on the queue. */
80 static const xData xStructsToSend[ 2 ] =
81 {
82         { 100, mainSENDER_1 }, /* Used by Sender1. */
83         { 200, mainSENDER_2 }  /* Used by Sender2. */
84 };
85
86 int main( void )
87 {
88     /* The queue is created to hold a maximum of 3 structures of type xData. */
89     xQueue = xQueueCreate( 3, sizeof( xData ) );
90
91         if( xQueue != NULL )
92         {
93                 /* Create two instances of the task that will write to the queue.  The
94                 parameter is used to pass the structure that the task should write to the
95                 queue, so one task will continuously send xStructsToSend[ 0 ] to the queue
96                 while the other task will continuously send xStructsToSend[ 1 ].  Both
97                 tasks are created at priority 2 which is above the priority of the receiver. */
98                 xTaskCreate( vSenderTask, "Sender1", 1000, ( void * ) &( xStructsToSend[ 0 ] ), 2, NULL );
99                 xTaskCreate( vSenderTask, "Sender2", 1000, ( void * ) &( xStructsToSend[ 1 ] ), 2, NULL );
100
101                 /* Create the task that will read from the queue.  The task is created with
102                 priority 1, so below the priority of the sender tasks. */
103                 xTaskCreate( vReceiverTask, "Receiver", 1000, NULL, 1, NULL );
104
105                 /* Start the scheduler so the created tasks start executing. */
106                 vTaskStartScheduler();
107         }
108         else
109         {
110                 /* The queue could not be created. */
111         }
112                
113     /* If all is well we will never reach here as the scheduler will now be
114     running the tasks.  If we do reach here then it is likely that there was
115     insufficient heap memory available for a resource to be created. */
116         for( ;; );
117         return 0;
118 }
119 /*-----------------------------------------------------------*/
120
121 static void vSenderTask( void *pvParameters )
122 {
123 portBASE_TYPE xStatus;
124 const portTickType xTicksToWait = 100 / portTICK_RATE_MS;
125
126         /* As per most tasks, this task is implemented within an infinite loop. */
127         for( ;; )
128         {
129                 /* The first parameter is the queue to which data is being sent.  The
130                 queue was created before the scheduler was started, so before this task
131                 started to execute.
132
133                 The second parameter is the address of the structure being sent.  The
134                 address is passed in as the task parameter.
135
136                 The third parameter is the Block time - the time the task should be kept
137                 in the Blocked state to wait for space to become available on the queue
138                 should the queue already be full.  A block time is specified as the queue
139                 will become full.  Items will only be removed from the queue when both
140                 sending tasks are in the Blocked state.. */
141                 xStatus = xQueueSendToBack( xQueue, pvParameters, xTicksToWait );
142
143                 if( xStatus != pdPASS )
144                 {
145                         /* We could not write to the queue because it was full - this must
146                         be an error as the receiving task should make space in the queue
147                         as soon as both sending tasks are in the Blocked state. */
148                         vPrintString( "Could not send to the queue.\r\n" );
149                 }
150
151                 /* Allow the other sender task to execute. */
152                 taskYIELD();
153         }
154 }
155 /*-----------------------------------------------------------*/
156
157 static void vReceiverTask( void *pvParameters )
158 {
159 /* Declare the structure that will hold the values received from the queue. */
160 xData xReceivedStructure;
161 portBASE_TYPE xStatus;
162
163         /* This task is also defined within an infinite loop. */
164         for( ;; )
165         {
166                 /* As this task only runs when the sending tasks are in the Blocked state,
167                 and the sending tasks only block when the queue is full, this task should
168                 always find the queue to be full.  3 is the queue length. */
169                 if( uxQueueMessagesWaiting( xQueue ) != 3 )
170                 {
171                         vPrintString( "Queue should have been full!\r\n" );
172                 }
173
174                 /* The first parameter is the queue from which data is to be received.  The
175                 queue is created before the scheduler is started, and therefore before this
176                 task runs for the first time.
177
178                 The second parameter is the buffer into which the received data will be
179                 placed.  In this case the buffer is simply the address of a variable that
180                 has the required size to hold the received structure.
181
182                 The last parameter is the block time - the maximum amount of time that the
183                 task should remain in the Blocked state to wait for data to be available
184                 should the queue already be empty.  A block time is not necessary as this
185                 task will only run when the queue is full so data will always be available. */
186                 xStatus = xQueueReceive( xQueue, &xReceivedStructure, 0 );
187
188                 if( xStatus == pdPASS )
189                 {
190                         /* Data was successfully received from the queue, print out the received
191                         value and the source of the value. */
192                         if( xReceivedStructure.ucSource == mainSENDER_1 )
193                         {
194                                 vPrintStringAndNumber( "From Sender 1 = ", xReceivedStructure.ucValue );
195                         }
196                         else
197                         {
198                                 vPrintStringAndNumber( "From Sender 2 = ", xReceivedStructure.ucValue );
199                         }
200                 }
201                 else
202                 {
203                         /* We did not receive anything from the queue.  This must be an error
204                         as this task should only run when the queue is full. */
205                         vPrintString( "Could not receive from the queue.\r\n" );
206                 }
207         }
208 }
209
210
211
212
213
214
Note: See TracBrowser for help on using the browser.