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 to be created. Two instances of this task are created. */ |
---|
60 |
static void prvPrintTask( void *pvParameters ); |
---|
61 |
|
---|
62 |
/* The function that uses a mutex to control access to standard out. */ |
---|
63 |
static void prvNewPrintString( const portCHAR *pcString ); |
---|
64 |
|
---|
65 |
/*-----------------------------------------------------------*/ |
---|
66 |
|
---|
67 |
/* Declare a variable of type xSemaphoreHandle. This is used to reference the |
---|
68 |
mutex type semaphore that is used to ensure mutual exclusive access to stdout. */ |
---|
69 |
xSemaphoreHandle xMutex; |
---|
70 |
|
---|
71 |
|
---|
72 |
int main( void ) |
---|
73 |
{ |
---|
74 |
/* Before a semaphore is used it must be explicitly created. In this example |
---|
75 |
a mutex type semaphore is created. */ |
---|
76 |
xMutex = xSemaphoreCreateMutex(); |
---|
77 |
|
---|
78 |
/* The tasks are going to use a pseudo random delay, seed the random number |
---|
79 |
generator. */ |
---|
80 |
srand( 567 ); |
---|
81 |
|
---|
82 |
/* Check the semaphore was created successfully. */ |
---|
83 |
if( xMutex != NULL ) |
---|
84 |
{ |
---|
85 |
/* Create two instances of the tasks that attempt to write stdout. The |
---|
86 |
string they attempt to write is passed in as the task parameter. The tasks |
---|
87 |
are created at different priorities so some pre-emption will occur. */ |
---|
88 |
xTaskCreate( prvPrintTask, "Print1", 1000, "Task 1 ******************************************\r\n", 1, NULL ); |
---|
89 |
xTaskCreate( prvPrintTask, "Print2", 1000, "Task 2 ------------------------------------------\r\n", 2, NULL ); |
---|
90 |
|
---|
91 |
/* Start the scheduler so the created tasks start executing. */ |
---|
92 |
vTaskStartScheduler(); |
---|
93 |
} |
---|
94 |
|
---|
95 |
/* If all is well we will never reach here as the scheduler will now be |
---|
96 |
running the tasks. If we do reach here then it is likely that there was |
---|
97 |
insufficient heap memory available for a resource to be created. */ |
---|
98 |
for( ;; ); |
---|
99 |
return 0; |
---|
100 |
} |
---|
101 |
/*-----------------------------------------------------------*/ |
---|
102 |
|
---|
103 |
static void prvNewPrintString( const portCHAR *pcString ) |
---|
104 |
{ |
---|
105 |
/* The semaphore is created before the scheduler is started so already |
---|
106 |
exists by the time this task executes. |
---|
107 |
|
---|
108 |
Attempt to take the semaphore, blocking indefinitely if the mutex is not |
---|
109 |
available immediately. The call to xSemaphoreTake() will only return when |
---|
110 |
the semaphore has been successfully obtained so there is no need to check the |
---|
111 |
return value. If any other delay period was used then the code must check |
---|
112 |
that xSemaphoreTake() returns pdTRUE before accessing the resource (in this |
---|
113 |
case standard out. */ |
---|
114 |
xSemaphoreTake( xMutex, portMAX_DELAY ); |
---|
115 |
{ |
---|
116 |
/* The following line will only execute once the semaphore has been |
---|
117 |
successfully obtained - so standard out can be accessed freely. */ |
---|
118 |
printf( "%s", pcString ); |
---|
119 |
fflush( stdout ); |
---|
120 |
} |
---|
121 |
xSemaphoreGive( xMutex ); |
---|
122 |
|
---|
123 |
/* Allow any key to stop the application running. A real application that |
---|
124 |
actually used the key value should protect access to the keyboard too. A |
---|
125 |
real application is very unlikely to have more than one task processing |
---|
126 |
key presses though! */ |
---|
127 |
if( kbhit() ) |
---|
128 |
{ |
---|
129 |
vTaskEndScheduler(); |
---|
130 |
} |
---|
131 |
} |
---|
132 |
/*-----------------------------------------------------------*/ |
---|
133 |
|
---|
134 |
static void prvPrintTask( void *pvParameters ) |
---|
135 |
{ |
---|
136 |
char *pcStringToPrint; |
---|
137 |
|
---|
138 |
/* Two instances of this task are created so the string the task will send |
---|
139 |
to prvNewPrintString() is passed in the task parameter. Cast this to the |
---|
140 |
required type. */ |
---|
141 |
pcStringToPrint = ( char * ) pvParameters; |
---|
142 |
|
---|
143 |
for( ;; ) |
---|
144 |
{ |
---|
145 |
/* Print out the string using the newly defined function. */ |
---|
146 |
prvNewPrintString( pcStringToPrint ); |
---|
147 |
|
---|
148 |
/* Wait a pseudo random time. Note that rand() is not necessarily |
---|
149 |
re-entrant, but in this case it does not really matter as the code does |
---|
150 |
not care what value is returned. In a more secure application a version |
---|
151 |
of rand() that is known to be re-entrant should be used - or calls to |
---|
152 |
rand() should be protected using a critical section. */ |
---|
153 |
vTaskDelay( ( rand() & 0x1FF ) ); |
---|
154 |
} |
---|
155 |
} |
---|
156 |
|
---|
157 |
|
---|
158 |
|
---|