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 "semphr.h" |
---|
54 |
|
---|
55 |
/* Compiler includes. */ |
---|
56 |
#include <stdlib.h> |
---|
57 |
#include <stdio.h> |
---|
58 |
|
---|
59 |
/* The task that sends messages to the stdio gatekeeper. Two instances of this |
---|
60 |
task are created. */ |
---|
61 |
static void prvPrintTask( void *pvParameters ); |
---|
62 |
|
---|
63 |
/* The gatekeeper task itself. */ |
---|
64 |
static void prvStdioGatekeeperTask( void *pvParameters ); |
---|
65 |
|
---|
66 |
/* Define the strings that the tasks and interrupt will print out via the gatekeeper. */ |
---|
67 |
static char *pcStringsToPrint[] = |
---|
68 |
{ |
---|
69 |
"Task 1 ****************************************************\r\n", |
---|
70 |
"Task 2 ----------------------------------------------------\r\n", |
---|
71 |
"Message printed from the tick hook interrupt ##############\r\n" |
---|
72 |
}; |
---|
73 |
|
---|
74 |
/*-----------------------------------------------------------*/ |
---|
75 |
|
---|
76 |
/* Declare a variable of type xQueueHandle. This is used to send messages from |
---|
77 |
the print tasks to the gatekeeper task. */ |
---|
78 |
xQueueHandle xPrintQueue; |
---|
79 |
|
---|
80 |
|
---|
81 |
int main( void ) |
---|
82 |
{ |
---|
83 |
/* Before a queue is used it must be explicitly created. The queue is created |
---|
84 |
to hold a maximum of 5 character pointers. */ |
---|
85 |
xPrintQueue = xQueueCreate( 5, sizeof( char * ) ); |
---|
86 |
|
---|
87 |
/* The tasks are going to use a pseudo random delay, seed the random number |
---|
88 |
generator. */ |
---|
89 |
srand( 567 ); |
---|
90 |
|
---|
91 |
/* Check the queue was created successfully. */ |
---|
92 |
if( xPrintQueue != NULL ) |
---|
93 |
{ |
---|
94 |
/* Create two instances of the tasks that send messages to the gatekeeper. |
---|
95 |
The index to the string they attempt to write is passed in as the task |
---|
96 |
parameter (4th parameter to xTaskCreate()). The tasks are created at |
---|
97 |
different priorities so some pre-emption will occur. */ |
---|
98 |
xTaskCreate( prvPrintTask, "Print1", 1000, ( void * ) 0, 1, NULL ); |
---|
99 |
xTaskCreate( prvPrintTask, "Print2", 1000, ( void * ) 1, 2, NULL ); |
---|
100 |
|
---|
101 |
/* Create the gatekeeper task. This is the only task that is permitted |
---|
102 |
to access standard out. */ |
---|
103 |
xTaskCreate( prvStdioGatekeeperTask, "Gatekeeper", 1000, NULL, 0, NULL ); |
---|
104 |
|
---|
105 |
/* Start the scheduler so the created tasks start executing. */ |
---|
106 |
vTaskStartScheduler(); |
---|
107 |
} |
---|
108 |
|
---|
109 |
/* If all is well we will never reach here as the scheduler will now be |
---|
110 |
running the tasks. If we do reach here then it is likely that there was |
---|
111 |
insufficient heap memory available for a resource to be created. */ |
---|
112 |
for( ;; ); |
---|
113 |
return 0; |
---|
114 |
} |
---|
115 |
/*-----------------------------------------------------------*/ |
---|
116 |
|
---|
117 |
static void prvStdioGatekeeperTask( void *pvParameters ) |
---|
118 |
{ |
---|
119 |
char *pcMessageToPrint; |
---|
120 |
|
---|
121 |
/* This is the only task that is allowed to write to the terminal output. |
---|
122 |
Any other task wanting to write to the output does not access the terminal |
---|
123 |
directly, but instead sends the output to this task. As only one task |
---|
124 |
writes to standard out there are no mutual exclusion or serialization issues |
---|
125 |
to consider within this task itself. */ |
---|
126 |
for( ;; ) |
---|
127 |
{ |
---|
128 |
/* Wait for a message to arrive. */ |
---|
129 |
xQueueReceive( xPrintQueue, &pcMessageToPrint, portMAX_DELAY ); |
---|
130 |
|
---|
131 |
/* There is no need to check the return value as the task will block |
---|
132 |
indefinitely and only run again when a message has arrived. When the |
---|
133 |
next line is executed there will be a message to be output. */ |
---|
134 |
printf( "%s", pcMessageToPrint ); |
---|
135 |
fflush( stdout ); |
---|
136 |
|
---|
137 |
/* Now simply go back to wait for the next message. */ |
---|
138 |
} |
---|
139 |
} |
---|
140 |
/*-----------------------------------------------------------*/ |
---|
141 |
|
---|
142 |
void vApplicationTickHook( void ) |
---|
143 |
{ |
---|
144 |
static int iCount = 0; |
---|
145 |
portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE; |
---|
146 |
|
---|
147 |
/* Print out a message every 200 ticks. The message is not written out |
---|
148 |
directly, but sent to the gatekeeper task. */ |
---|
149 |
iCount++; |
---|
150 |
if( iCount >= 200 ) |
---|
151 |
{ |
---|
152 |
/* In this case the last parameter (xHigherPriorityTaskWoken) is not |
---|
153 |
actually used but must still be supplied. */ |
---|
154 |
xQueueSendToFrontFromISR( xPrintQueue, &( pcStringsToPrint[ 2 ] ), &xHigherPriorityTaskWoken ); |
---|
155 |
|
---|
156 |
/* Reset the count ready to print out the string again in 200 ticks |
---|
157 |
time. */ |
---|
158 |
iCount = 0; |
---|
159 |
} |
---|
160 |
} |
---|
161 |
/*-----------------------------------------------------------*/ |
---|
162 |
|
---|
163 |
static void prvPrintTask( void *pvParameters ) |
---|
164 |
{ |
---|
165 |
int iIndexToString; |
---|
166 |
|
---|
167 |
/* Two instances of this task are created so the index to the string the task |
---|
168 |
will send to the gatekeeper task is passed in the task parameter. Cast this |
---|
169 |
to the required type. */ |
---|
170 |
iIndexToString = ( int ) pvParameters; |
---|
171 |
|
---|
172 |
for( ;; ) |
---|
173 |
{ |
---|
174 |
/* Print out the string, not directly but by passing the string to the |
---|
175 |
gatekeeper task on the queue. The queue is created before the scheduler is |
---|
176 |
started so will already exist by the time this task executes. A block time |
---|
177 |
is not specified as there should always be space in the queue. */ |
---|
178 |
xQueueSendToBack( xPrintQueue, &( pcStringsToPrint[ iIndexToString ] ), 0 ); |
---|
179 |
|
---|
180 |
/* Wait a pseudo random time. Note that rand() is not necessarily |
---|
181 |
re-entrant, but in this case it does not really matter as the code does |
---|
182 |
not care what value is returned. In a more secure application a version |
---|
183 |
of rand() that is known to be re-entrant should be used - or calls to |
---|
184 |
rand() should be protected using a critical section. */ |
---|
185 |
vTaskDelay( ( rand() & 0x1FF ) ); |
---|
186 |
} |
---|
187 |
} |
---|
188 |
|
---|
189 |
|
---|
190 |
|
---|