Subversion Repositories svnkaklik

Rev

Details | Last modification | View Log

Rev Author Line No. Line
151 kaklik 1
/*
2
    Copyright (C) 2004    John Orlando
3
 
4
   AVRcam: a small real-time image processing engine.
5
 
6
    This program is free software; you can redistribute it and/or
7
    modify it under the terms of the GNU General Public
8
    License as published by the Free Software Foundation; either
9
    version 2 of the License, or (at your option) any later version.
10
 
11
    This program 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 GNU
14
    General Public License for more details.
15
 
16
    You should have received a copy of the GNU General Public
17
    License along with this program; if not, write to the Free Software
18
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19
 
20
   For more information on the AVRcam, please contact:
21
 
22
   john@jrobot.net
23
 
24
   or go to www.jrobot.net for more details regarding the system.
25
*/
26
/***********************************************************
27
	Module Name: Executive.c
28
	Module Date: 04/12/2004
29
	Module Auth: John Orlando
30
 
31
	Description: This file is responsible for implementing a
32
	minimalist event dispatcher.  It keeps track of an event
33
	fifo that waits for new events to come in, and dispatches
34
	them to any entities that care about them.
35
 
36
    Revision History:
37
    Date        Rel Ver.    Notes
38
    4/10/2004      0.1     Module created
39
    6/30/2004      1.0     Initial release for Circuit Cellar
40
                           contest.
41
    1/16/2005      1.4     Fixed issue where the interrupts weren't
42
                           being turned off when the fastEventBitmask
43
                           was being accessed.  Also removed redundant
44
                           interrupt masking when accessing the
45
                           main event fifo.  Also fixed issue where
46
                           the main event fifo wasn't being checked
47
                           for events if an event was pending in
48
                           the fast event fifo.
49
***********************************************************/
50
 
51
/*	Includes */
52
#include <stdlib.h>
53
#include "CommonDefs.h"
54
#include "Executive.h"
55
#include "FrameMgr.h"
56
#include "CamInterface.h"
57
#include "UIMgr.h"
58
#include "UartInterface.h"
59
#include "CamConfig.h"
60
#include "Utility.h"
61
 
62
/*  Local Variables */
63
unsigned char Exec_eventFifo[EXEC_EVENT_FIFO_SIZE];
64
unsigned char Exec_eventFifoHead=0;
65
unsigned char Exec_eventFifoTail=0;
66
 
67
/*  Local Function Definitions */
68
static unsigned char Exec_readEventFifo(void);
69
 
70
/* 	Local Structures and Typedefs */
71
 
72
/*  Extern Variables */
73
/* This bitmask holds events that need to be processed as fast as possible */
74
unsigned char fastEventBitmask = 0x00;
75
 
76
/*  Definitions */
77
#define IS_DATA_IN_EVENT_FIFO() (!(Exec_eventFifoHead == Exec_eventFifoTail))
78
/***********************************************************
79
	Function Name: Exec_run
80
	Function Description: This function is responsible for
81
	running the main control loop.  The control loop is 
82
	based on checking both the fast-event bitmask (for high
83
    priority events) and the event FIFO to determine if an
84
    event needs to be handled.  The event is then dispatched
85
    to the appropriate handler.
86
	Inputs:  none
87
	Outputs: none
88
***********************************************************/	
89
void Exec_run(void)
90
{
91
	unsigned char eventGenerated;
92
 
93
	while(1)
94
	{
95
		if (fastEventBitmask)
96
		{
97
			/* an event needing fast processing has been received */
98
			/* a received line needs to be processed...this
99
			needs to be processed as quickly as possible */
100
			if (fastEventBitmask & FEV_ACQUIRE_LINE_COMPLETE)
101
			{
102
                DISABLE_INTS();
103
				fastEventBitmask &= ~FEV_ACQUIRE_LINE_COMPLETE;	
104
                ENABLE_INTS();
105
				FrameMgr_processLine();				
106
 
107
				/* also check if serial data needs to be sent
108
				out through UIMgr */
109
				UIMgr_transmitPendingData();	
110
 
111
				/* we can't just call acquire line again here,
112
				since we don't know if we need to acquire another
113
				line or not (it depends on the FrameMgr to figure
114
				this out) */
115
			}
116
			if (fastEventBitmask & FEV_PROCESS_LINE_COMPLETE)
117
			{
118
                DISABLE_INTS();
119
				fastEventBitmask &= ~FEV_PROCESS_LINE_COMPLETE;
120
                ENABLE_INTS();
121
				FrameMgr_acquireLine();
122
			}
123
		}		
124
 
125
        if (IS_DATA_IN_EVENT_FIFO() == TRUE)		
126
		{			
127
            eventGenerated = Exec_readEventFifo();
128
			switch(eventGenerated)
129
			{
130
				case (EV_DUMP_FRAME):
131
					FrameMgr_dispatchEvent(eventGenerated);
132
					break;
133
 
134
				case (EV_ENABLE_TRACKING):
135
					FrameMgr_dispatchEvent(eventGenerated);
136
					break;
137
 
138
				case (EV_DISABLE_TRACKING):
139
					FrameMgr_dispatchEvent(eventGenerated);
140
					break;
141
 
142
				case (EV_ACQUIRE_LINE_COMPLETE):
143
					FrameMgr_dispatchEvent(eventGenerated);
144
					UIMgr_dispatchEvent(eventGenerated);
145
					break;
146
 
147
				case (EV_ACQUIRE_FRAME_COMPLETE):				
148
					FrameMgr_dispatchEvent(eventGenerated);
149
					break;
150
 
151
				case (EV_PROCESS_LINE_COMPLETE):
152
					FrameMgr_dispatchEvent(eventGenerated);
153
					break;
154
 
155
				case (EV_PROCESS_FRAME_COMPLETE):
156
					FrameMgr_dispatchEvent(eventGenerated);
157
					break;
158
 
159
				case (EV_SERIAL_DATA_RECEIVED):
160
					UIMgr_dispatchEvent(eventGenerated);
161
					FrameMgr_dispatchEvent(eventGenerated);
162
					break;																
163
 
164
				case (EV_SERIAL_DATA_PENDING_TX):
165
					UIMgr_dispatchEvent(eventGenerated);
166
					break;
167
 
168
				default:		
169
					break;
170
			}			
171
		}
172
 
173
        /* toggle the debug line */
174
 
175
	}
176
}
177
 
178
/***********************************************************
179
	Function Name: Exec_readEventFifo
180
	Function Description: This function is responsible for
181
	reading a single event out of the event fifo.
182
	Inputs:  none 
183
	Outputs: unsigned char-the data read
184
***********************************************************/	
185
static unsigned char Exec_readEventFifo(void)
186
{
187
	unsigned char dataByte, tmpTail;
188
 
189
	DISABLE_INTS();
190
	/* just return the current tail from the tx fifo */
191
	dataByte = Exec_eventFifo[Exec_eventFifoTail];	
192
	tmpTail = (Exec_eventFifoTail+1) & (EXEC_EVENT_FIFO_MASK);
193
	Exec_eventFifoTail = tmpTail;
194
	ENABLE_INTS();
195
 
196
	return(dataByte);
197
}
198
 
199
/***********************************************************
200
	Function Name: Exec_writeEventFifo
201
	Function Description: This function is responsible for
202
	writing a single event to the event fifo and
203
	updating the appropriate pointers.
204
	Inputs:  data - the byte to write to the Fifo 
205
	Outputs: none
206
***********************************************************/	
207
void Exec_writeEventFifo(unsigned char event)
208
{
209
	unsigned char tmpHead;
210
 
211
	DISABLE_INTS();
212
	Exec_eventFifo[Exec_eventFifoHead] = event;
213
 
214
    /* now move the head up */
215
    tmpHead = (Exec_eventFifoHead + 1) & (EXEC_EVENT_FIFO_MASK);
216
    Exec_eventFifoHead = tmpHead;
217
	ENABLE_INTS();
218
}
219