Subversion Repositories svnkaklik

Rev

Rev 409 | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log

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