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 |
#include "portasm.h" |
---|
55 |
|
---|
56 |
/* Demo includes. */ |
---|
57 |
#include "basic_io.h" |
---|
58 |
|
---|
59 |
/* Compiler includes. */ |
---|
60 |
#include <dos.h> |
---|
61 |
|
---|
62 |
/* The tasks to be created. */ |
---|
63 |
static void vIntegerGenerator( void *pvParameters ); |
---|
64 |
static void vStringPrinter( void *pvParameters ); |
---|
65 |
|
---|
66 |
/* The service routine for the interrupt. This is the interrupt that the task |
---|
67 |
will be synchronized with. */ |
---|
68 |
static void __interrupt __far vExampleInterruptHandler( void ); |
---|
69 |
|
---|
70 |
|
---|
71 |
unsigned long ulNext = 0; |
---|
72 |
unsigned long ulCount; |
---|
73 |
unsigned long ul[ 100 ]; |
---|
74 |
|
---|
75 |
/*-----------------------------------------------------------*/ |
---|
76 |
|
---|
77 |
/* Declare two variables of type xQueueHandle. One queue will be read from |
---|
78 |
within an ISR, the other will be written to from within an ISR. */ |
---|
79 |
xQueueHandle xIntegerQueue, xStringQueue; |
---|
80 |
|
---|
81 |
int main( void ) |
---|
82 |
{ |
---|
83 |
/* Before a queue can be used it must first be created. Create both queues |
---|
84 |
used by this example. One queue can hold variables of type unsigned long, |
---|
85 |
the other queue can hold variables of type char*. Both queues can hold a |
---|
86 |
maximum of 10 items. A real application should check the return values to |
---|
87 |
ensure the queues have been successfully created. */ |
---|
88 |
xIntegerQueue = xQueueCreate( 10, sizeof( unsigned long ) ); |
---|
89 |
xStringQueue = xQueueCreate( 10, sizeof( char * ) ); |
---|
90 |
|
---|
91 |
/* Install the interrupt handler. */ |
---|
92 |
_dos_setvect( 0x82, vExampleInterruptHandler ); |
---|
93 |
|
---|
94 |
/* Create the task that uses a queue to pass integers to the interrupt service |
---|
95 |
routine. The task is created at priority 1. */ |
---|
96 |
xTaskCreate( vIntegerGenerator, "IntGen", 1000, NULL, 1, NULL ); |
---|
97 |
|
---|
98 |
/* Create the task that prints out the strings sent to it from the interrupt |
---|
99 |
service routine. This task is created at the higher priority of 2. */ |
---|
100 |
xTaskCreate( vStringPrinter, "String", 1000, NULL, 2, NULL ); |
---|
101 |
|
---|
102 |
/* Start the scheduler so the created tasks start executing. */ |
---|
103 |
vTaskStartScheduler(); |
---|
104 |
|
---|
105 |
/* If all is well we will never reach here as the scheduler will now be |
---|
106 |
running the tasks. If we do reach here then it is likely that there was |
---|
107 |
insufficient heap memory available for a resource to be created. */ |
---|
108 |
for( ;; ); |
---|
109 |
return 0; |
---|
110 |
} |
---|
111 |
/*-----------------------------------------------------------*/ |
---|
112 |
|
---|
113 |
static void vIntegerGenerator( void *pvParameters ) |
---|
114 |
{ |
---|
115 |
portTickType xLastExecutionTime; |
---|
116 |
unsigned portLONG ulValueToSend = 0; |
---|
117 |
int i; |
---|
118 |
|
---|
119 |
/* Initialize the variable used by the call to vTaskDelayUntil(). */ |
---|
120 |
xLastExecutionTime = xTaskGetTickCount(); |
---|
121 |
|
---|
122 |
for( ;; ) |
---|
123 |
{ |
---|
124 |
/* This is a periodic task. Block until it is time to run again. |
---|
125 |
The task will execute every 200ms. */ |
---|
126 |
vTaskDelayUntil( &xLastExecutionTime, 200 / portTICK_RATE_MS ); |
---|
127 |
|
---|
128 |
/* Send an incrementing number to the queue five times. These will be |
---|
129 |
read from the queue by the interrupt service routine. A block time is |
---|
130 |
not specified. */ |
---|
131 |
for( i = 0; i < 5; i++ ) |
---|
132 |
{ |
---|
133 |
xQueueSendToBack( xIntegerQueue, &ulValueToSend, 0 ); |
---|
134 |
ulValueToSend++; |
---|
135 |
} |
---|
136 |
|
---|
137 |
/* Force an interrupt so the interrupt service routine can read the |
---|
138 |
values from the queue. */ |
---|
139 |
vPrintString( "Generator task - About to generate an interrupt.\r\n" ); |
---|
140 |
__asm{ int 0x82 } |
---|
141 |
vPrintString( "Generator task - Interrupt generated.\r\n\r\n\r\n" ); |
---|
142 |
} |
---|
143 |
} |
---|
144 |
/*-----------------------------------------------------------*/ |
---|
145 |
|
---|
146 |
static void vStringPrinter( void *pvParameters ) |
---|
147 |
{ |
---|
148 |
char *pcString; |
---|
149 |
|
---|
150 |
for( ;; ) |
---|
151 |
{ |
---|
152 |
/* Block on the queue to wait for data to arrive. */ |
---|
153 |
xQueueReceive( xStringQueue, &pcString, portMAX_DELAY ); |
---|
154 |
|
---|
155 |
/* Print out the string received. */ |
---|
156 |
vPrintString( pcString ); |
---|
157 |
} |
---|
158 |
} |
---|
159 |
/*-----------------------------------------------------------*/ |
---|
160 |
|
---|
161 |
static void __interrupt __far vExampleInterruptHandler( void ) |
---|
162 |
{ |
---|
163 |
static portBASE_TYPE xHigherPriorityTaskWoken; |
---|
164 |
static unsigned long ulReceivedNumber; |
---|
165 |
|
---|
166 |
/* The strings are declared static const to ensure they are not allocated to the |
---|
167 |
interrupt service routine stack, and exist even when the interrupt service routine |
---|
168 |
is not executing. */ |
---|
169 |
static const char *pcStrings[] = |
---|
170 |
{ |
---|
171 |
"String 0\r\n", |
---|
172 |
"String 1\r\n", |
---|
173 |
"String 2\r\n", |
---|
174 |
"String 3\r\n" |
---|
175 |
}; |
---|
176 |
|
---|
177 |
xHigherPriorityTaskWoken = pdFALSE; |
---|
178 |
|
---|
179 |
/* Loop until the queeu is empty. */ |
---|
180 |
while( xQueueReceiveFromISR( xIntegerQueue, &ulReceivedNumber, &xHigherPriorityTaskWoken ) != errQUEUE_EMPTY ) |
---|
181 |
{ |
---|
182 |
/* Truncate the received value to the last two bits (values 0 to 3 inc.), then |
---|
183 |
send the string that corresponds to the truncated value to the other |
---|
184 |
queue. */ |
---|
185 |
ulReceivedNumber &= 0x03; |
---|
186 |
xQueueSendToBackFromISR( xStringQueue, &pcStrings[ ulReceivedNumber ], &xHigherPriorityTaskWoken ); |
---|
187 |
} |
---|
188 |
|
---|
189 |
/* Did receiving on a queue or sending on a queue unblock a task that has a |
---|
190 |
priority higher than the currently executing task? If so, force a context |
---|
191 |
switch here. */ |
---|
192 |
if( xHigherPriorityTaskWoken == pdTRUE ) |
---|
193 |
{ |
---|
194 |
/* NOTE: The syntax for forcing a context switch is different depending |
---|
195 |
on the port being used. Refer to the examples for the port you are |
---|
196 |
using for the correct method to use! */ |
---|
197 |
portSWITCH_CONTEXT(); |
---|
198 |
} |
---|
199 |
} |
---|
200 |
|
---|
201 |
|
---|
202 |
|
---|
203 |
|
---|
204 |
|
---|
205 |
|
---|
206 |
|
---|
207 |
|
---|
208 |
|
---|