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

Revision 36, 6.7 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 "semphr.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 vHandlerTask( void *pvParameters );
64 static void vPeriodicTask( 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
72 /* Declare a variable of type xSemaphoreHandle.  This is used to reference the
73 semaphore that is used to synchronize a task with an interrupt. */
74 xSemaphoreHandle xBinarySemaphore;
75
76
77 int main( void )
78 {
79     /* Before a semaphore is used it must be explicitly created.  In this example
80         a binary semaphore is created. */
81     vSemaphoreCreateBinary( xBinarySemaphore );
82
83         /* Install the interrupt handler. */
84         _dos_setvect( 0x82, vExampleInterruptHandler );
85
86         /* Check the semaphore was created successfully. */
87         if( xBinarySemaphore != NULL )
88         {
89                 /* Create the 'handler' task.  This is the task that will be synchronized
90                 with the interrupt.  The handler task is created with a high priority to
91                 ensure it runs immediately after the interrupt exits.  In this case a
92                 priority of 3 is chosen. */
93                 xTaskCreate( vHandlerTask, "Handler", 1000, NULL, 3, NULL );
94
95                 /* Create the task that will periodically generate a software interrupt.
96                 This is created with a priority below the handler task to ensure it will
97                 get preempted each time the handler task exist the Blocked state. */
98                 xTaskCreate( vPeriodicTask, "Periodic", 1000, NULL, 1, NULL );
99
100                 /* Start the scheduler so the created tasks start executing. */
101                 vTaskStartScheduler();
102         }
103                
104     /* If all is well we will never reach here as the scheduler will now be
105     running the tasks.  If we do reach here then it is likely that there was
106     insufficient heap memory available for a resource to be created. */
107         for( ;; );
108         return 0;
109 }
110 /*-----------------------------------------------------------*/
111
112 static void vHandlerTask( void *pvParameters )
113 {
114         /* As per most tasks, this task is implemented within an infinite loop. */
115         for( ;; )
116         {
117                 /* Use the semaphore to wait for the event.  The semaphore was created
118                 before the scheduler was started so before this task ran for the first
119                 time.  The task blocks indefinitely meaning this function call will only
120                 return once the semaphore has been successfully obtained - so there is no
121                 need to check the returned value. */
122                 xSemaphoreTake( xBinarySemaphore, portMAX_DELAY );
123
124                 /* To get here the event must have occurred.  Process the event (in this
125                 case we just print out a message). */
126                 vPrintString( "Handler task - Processing event.\r\n" );
127         }
128 }
129 /*-----------------------------------------------------------*/
130
131 static void vPeriodicTask( void *pvParameters )
132 {
133         /* As per most tasks, this task is implemented within an infinite loop. */
134         for( ;; )
135         {
136                 /* This task is just used to 'simulate' an interrupt.  This is done by
137                 periodically generating a software interrupt. */
138                 vTaskDelay( 500 / portTICK_RATE_MS );
139
140                 /* Generate the interrupt, printing a message both before hand and
141                 afterwards so the sequence of execution is evident from the output. */
142                 vPrintString( "Perodic task - About to generate an interrupt.\r\n" );
143                 __asm{ int 0x82 }
144                 vPrintString( "Periodic task - Interrupt generated.\r\n\r\n\r\n" );
145         }
146 }
147 /*-----------------------------------------------------------*/
148
149 static void __interrupt __far vExampleInterruptHandler( void )
150 {
151 static portBASE_TYPE xHigherPriorityTaskWoken;
152
153         xHigherPriorityTaskWoken = pdFALSE;
154
155         /* 'Give' the semaphore to unblock the task. */
156         xSemaphoreGiveFromISR( xBinarySemaphore, &xHigherPriorityTaskWoken );
157
158         if( xHigherPriorityTaskWoken == pdTRUE )
159         {
160                 /* Giving the semaphore unblocked a task, and the priority of the
161                 unblocked task is higher than the currently running task - force
162                 a context switch to ensure that the interrupt returns directly to
163                 the unblocked (higher priority) task.
164
165                 NOTE: The syntax for forcing a context switch is different depending
166                 on the port being used.  Refer to the examples for the port you are
167                 using for the correct method to use! */
168                 portSWITCH_CONTEXT();
169         }
170 }
171
172
173
174
175
176
177
178
179
Note: See TracBrowser for help on using the browser.