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: CamInterface.c
28
	Module Date: 04/12/2004
29
	Module Auth: John Orlando
30
 
31
	Description: This file is responsible for providing an
32
	interface to the OV6620 camera hardware.  This includes
33
	an interface to CamInterface.S for certain low-level,
34
	optimized camera access routines.
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
    11/15/2004     1.2     ifdef'd out the resetCam routine, since
42
                           resetting the cam now causes the OV6620's
43
                           clock to not be output (we have to start
44
                           it up after each reset with the external
45
                           tiny12 processor).
46
    1/16/2005      1.4     Ensure that the TCCR1B register is set so
47
                           nothing is clocking timer1 at startup.                             
48
***********************************************************/
49
 
50
/*	Includes */
51
#include <avr/interrupt.h>
52
#include <avr/sleep.h>
53
#include <avr/eeprom.h>
54
#include <stdlib.h>
55
#include <string.h>
56
#include "CommonDefs.h"
57
#include "CamInterface.h"
58
#include "Utility.h"
59
#include "UIMgr.h"
60
#include "Executive.h"
61
#include "UartInterface.h"
62
 
63
/*  Local Variables */
64
 
65
/* 	Local Structures and Typedefs */
66
 
67
/*  Definitions */
68
//#define OUTPUT_INITIAL_COLOR_MAP 1
69
 
70
#define FAST_ACQUIRE 1
71
#define CAM_G_BUS          	PINB
72
#define CAM_G_BUS_DIR      	DDRB
73
#define CAM_RB_BUS          PINC
74
#define CAM_RB_BUS_DIR 		DDRC
75
 
76
#define CAM_CONTROL_PORT     PORTD
77
#define CAM_CONTROL_PORT_DIR DDRD
78
#define CAM_RESET_LINE       BIT7
79
#define CAM_PIXEL_CLK_COUNT  BIT5
80
#define CAM_HREF             BIT4
81
#define CAM_PIXEL_CLK_INT    BIT3
82
#define CAM_VSYNC            BIT2
83
 
84
/*  Global Variables */
85
/* NOTE: This file MUST appear first in the Makefile for these variables to
86
be placed properly in RAM */
87
 
88
/* The colorMap[] table provides the membership lookup table to convert 
89
RGB or YUV pixel values into actual colors.  The membership table contains
90
16 elements for each color channel, concatenated together.  The Red (or Y)
91
value is located in the first 16 bytes, the G (or U) value is located in
92
the second 16 bytes, and the B (or V) value is located in the last 16 bytes:
93
 
94
    ----------------------------------------------------------------------------------
95
    |red0|red1|red2|...|red15|green0|green1|green2|...|green15|blue0|blue1|...|blue15|
96
mem:|0x00 0x01 0x02     0x15   0x16   0x17   0x18       0x31   0x32  0x33       0x47 |
97
    ---------------------------------------------------------------------------------
98
Thus, the red lookup is accessed at colorMap+0, the green lookup is accessed 
99
at colorMap+16, and the blue lookup is accessed at colorMap+32.  */	
100
unsigned char colorMap[NUM_ELEMENTS_IN_COLOR_MAP] __attribute__ ((section (".noinit")));
101
 
102
/*  Extern Variables */
103
/* These two buffers hold the current and previous lines
104
of pixel data.  They are sized to the worst case scenario,
105
where the color changes between every pixel (unrealistic).
106
The format of each buffer is for all the even bytes to hold
107
the run-length, and the odd bytes to hold the color data. */
108
 
109
/* In addition, if we are in frameDump mode, we use these buffers
110
to store the acquired line data...we are actually grabbing ALL of the
111
pixels in a line (176) instead of the 88 we get normally during tracking.
112
But since we have enough to hold 88-RLE blocks, we already have the 176
113
allocated for this... */
114
unsigned char currentLineBuffer[LENGTH_OF_LINE_BUFFER];
115
unsigned char previousLineBuffer[LENGTH_OF_LINE_BUFFER];
116
 
117
/*  Extern Functions */
118
/* These functions are located in assembly files, and thus
119
must be externed here so they can be referenced in the source below. */
120
extern void CamIntAsm_waitForNewTrackingFrame(unsigned char *pBuffer, unsigned char *pMemLookup);
121
extern void CamIntAsm_waitForNewDumpFrame(unsigned char *pCurrBuffer, unsigned char *pPrevBuffer);
122
extern void CamIntAsm_acquireTrackingLine(unsigned char *pBuffer, unsigned char *pMemLookup);
123
extern void CamIntAsm_acquireDumpLine(unsigned char *pCurrBuffer, unsigned char *pPrevBuffer);
124
 
125
/***********************************************************
126
	Function Name: CamInt_init
127
	Function Description: This function is responsible
128
	for initializing the camera interface.  This includes
129
	setting up the i/o ports that are used to read the
130
	camera busses, as well as resetting the camera.
131
	Inputs:  none
132
	Outputs: none
133
***********************************************************/	
134
void CamInt_init(void)
135
{
136
#if OUTPUT_INITIAL_COLOR_MAP
137
	unsigned char asciiBuffer[5];
138
    unsigned char i;
139
#endif    
140
 
141
	/* set up the mega8 ports that will be interfacing
142
	with the camera */	
143
	CAM_CONTROL_PORT_DIR |= (1<<CAM_RESET_LINE); /* cam reset is output */
144
	CAM_CONTROL_PORT_DIR |= 0x80;   /* set just the MSB as an output */
145
	CAM_CONTROL_PORT_DIR &= 0xFB;   /* make sure bit2 is clear (input) */
146
	CAM_CONTROL_PORT &= 0x7F;   /* set reset line low */
147
	CAM_G_BUS_DIR &= 0xF0;  /* 4-bit G bus all inputs */
148
    CAM_G_BUS_DIR |= 0xF0;  /* disable the pull-up on PB4 and PB5 */
149
	CAM_RB_BUS_DIR &= 0xF0;  /* 4-bit RB bus all inputs */
150
 
151
    /* ensure that timer1 is disabled to start...eventually, when PCLK needs
152
    to feed timer1 through the external counter, it will be enabled on an
153
    "as needed" basis...*/
154
	TCCR1B &= ~( (1<<CS12)|(1<<CS11)|(1<<CS10) );
155
 
156
	/* we'll turn on the interrupt after we assign the initial TCNT value */
157
 
158
	/* set up External Interrupt1 to interrupt us on rising edges (HREF)...
159
	this is needed to indicate when the first pixel of each line is about to start, so
160
	we can synch up with it...this interrupt will be disabled once HREF goes high */
161
 
162
	MCUCR |= (1<<ISC11) | (1<<ISC10);  /* rising edge interrupt */
163
	/* the interrupt will be enabled when we are ready to detect the rising edge of
164
	HREF...its now primed and ready to go */
165
 
166
	/* set up External Interrupt0 to interrupt us on rising edges (VSYNC) */
167
	MCUCR |= (1<<ISC01) | (1<<ISC00);	/* rising edge interrupt */ 
168
	GICR  |= (1<<INT0);    /* interrupt request enabled */
169
 
170
	/* set up TimerO to count and be clocked from an external pulse source
171
	(HREF) on falling edges...eventually, we need to enable the interrupt
172
	for this!  FIX THIS */
173
	TCCR0 = (1<<CS02)|(1<<CS01)|(0<<CS00);
174
 
175
	/* setting up the PCLK counter with Timer1 will be done right after
176
	we start receiving pixels in each line...we sacrifice the first pixel
177
	in each line, but we'll account for it...*/
178
 
179
	/* set up the mega8 so that its sleep mode puts it in an IDLE sleep
180
	mode, where it can wake up as fast as possible */
181
	set_sleep_mode(SLEEP_MODE_IDLE);
182
	/* umm....we need to actually enable the sleep mode...*/
183
	MCUCR |= 0x80;
184
 
185
	/* initialize the memLookup table */
186
	memset(colorMap,0x00,NUM_ELEMENTS_IN_COLOR_MAP);   
187
 
188
	/* read the color map out of EEPROM */
189
	eeprom_read_block(colorMap, (unsigned char*)0x01,NUM_ELEMENTS_IN_COLOR_MAP);
190
 
191
#if OUTPUT_INITIAL_COLOR_MAP    
192
    UIMgr_txBuffer("\r\n",2);
193
    for (i=0; i<NUM_ELEMENTS_IN_COLOR_MAP; i++)
194
	{
195
		memset(asciiBuffer,0x00,5);
196
		itoa(colorMap[i],asciiBuffer,10);
197
		UIMgr_txBuffer(asciiBuffer,3);
198
		UIMgr_txBuffer(" ",1);
199
		if (i==15 || i == 31)
200
		{
201
			/* break up the output */
202
			UIMgr_txBuffer("\r\n",2);
203
		}
204
	}
205
#endif    
206
 
207
#ifndef NO_CRYSTAL
208
	CamInt_resetCam();	
209
#endif    
210
}
211
 
212
/***********************************************************
213
	Function Name: CamInt_resetCam
214
	Function Description: This function is responsible
215
	for resetting the camera.  This is accomplished by
216
	toggling the reset line on the OV6620 for ~100 mS.
217
	Inputs:  none
218
	Outputs: none
219
    IMPORTANT NOTE: This function has effectively been removed
220
    since resetting the camera now causes the camera to not
221
    output the clock signal.  Thus, if we reset the cam, the
222
    AVR has no clock, and thus doesn't run...
223
***********************************************************/	
224
void CamInt_resetCam(void)
225
{
226
 
227
#if 0
228
	CAM_CONTROL_PORT |= (1<<CAM_RESET_LINE); /* cam reset line high */
229
	Utility_delay(500);
230
	CAM_CONTROL_PORT &= (0<<CAM_RESET_LINE); /* cam reset line low */
231
	Utility_delay(100);
232
#endif    
233
}
234
 
235