Subversion Repositories svnkaklik

Rev

Rev 409 | Only display areas with differences | Regard 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: CamConfig.c
27
	Module Name: CamConfig.c
28
	Module Date: 04/10/2004
28
	Module Date: 04/10/2004
29
    Module Auth: John Orlando 
29
    Module Auth: John Orlando 
30
	
30
	
31
	Description: This module is responsible for the 
31
	Description: This module is responsible for the 
32
	high-level configuration activities of the OV6620
32
	high-level configuration activities of the OV6620
33
	camera module.  This module interfaces with the
33
	camera module.  This module interfaces with the
34
	I2CInterface module to perform this configuration.
34
	I2CInterface module to perform this configuration.
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
    11/15/2004     1.2     Added code to un-tri-state the
41
    11/15/2004     1.2     Added code to un-tri-state the
42
                           OV6620's pixel data busses at
42
                           OV6620's pixel data busses at
43
                           startup after four seconds.  
43
                           startup after four seconds.  
44
                           This was added in to 
44
                           This was added in to 
45
                           allow the user to re-program the
45
                           allow the user to re-program the
46
                           mega8 at startup if needed.
46
                           mega8 at startup if needed.
47
***********************************************************/
47
***********************************************************/
48
 
48
 
49
/*  Includes */
49
/*  Includes */
50
#include <avr/io.h>
50
#include <avr/io.h>
51
#include "CamConfig.h"
51
#include "CamConfig.h"
52
#include "I2CInterface.h"
52
#include "I2CInterface.h"
53
#include "CommonDefs.h"
53
#include "CommonDefs.h"
54
#include "Utility.h"
54
#include "Utility.h"
55
 
55
 
56
/**********************************************************/
56
/**********************************************************/
57
/*  Definitions */
57
/*  Definitions */
58
/* The length of an I2C command is made up of a register address
58
/* The length of an I2C command is made up of a register address
59
plus the actual value of the register */
59
plus the actual value of the register */
60
#define SIZE_OF_I2C_CMD 2
60
#define SIZE_OF_I2C_CMD 2
61
#define MAX_NUM_CONFIG_CMDS 8
61
#define MAX_NUM_CONFIG_CMDS 8
62
#define CAM_CONFIG_TX_FIFO_SIZE MAX_NUM_CONFIG_CMDS 
62
#define CAM_CONFIG_TX_FIFO_SIZE MAX_NUM_CONFIG_CMDS 
63
#define CAM_CONFIG_TX_FIFO_MASK CAM_CONFIG_TX_FIFO_SIZE-1
63
#define CAM_CONFIG_TX_FIFO_MASK CAM_CONFIG_TX_FIFO_SIZE-1
64
 
64
 
65
/*  Local Variables */
65
/*  Local Variables */
66
 
66
 
67
/*  Local Structures and Typedefs */
67
/*  Local Structures and Typedefs */
68
 
68
 
69
/*  Local Function Prototypes */
69
/*  Local Function Prototypes */
70
static i2cCmd_t CamConfig_readTxFifo(void);
70
static i2cCmd_t CamConfig_readTxFifo(void);
71
 
71
 
72
/*  Extern Variables */
72
/*  Extern Variables */
73
i2cCmd_t 		CamConfig_txFifo[CAM_CONFIG_TX_FIFO_SIZE];
73
i2cCmd_t 		CamConfig_txFifo[CAM_CONFIG_TX_FIFO_SIZE];
74
unsigned char CamConfig_txFifoHead=0;
74
unsigned char CamConfig_txFifoHead=0;
75
unsigned char CamConfig_txFifoTail=0;
75
unsigned char CamConfig_txFifoTail=0;
76
 
76
 
77
/***********************************************************
77
/***********************************************************
78
	Function Name: CamConfig_init
78
	Function Name: CamConfig_init
79
	Function Description: This function is responsible for
79
	Function Description: This function is responsible for
80
	performing the initial configuration of the camera.
80
	performing the initial configuration of the camera.
81
	Inputs:  none
81
	Inputs:  none
82
	Outputs: none
82
	Outputs: none
83
***********************************************************/	
83
***********************************************************/	
84
void CamConfig_init(void)
84
void CamConfig_init(void)
85
{
85
{
86
	CamConfig_setCamReg(0x14,0x20);  /* reduce frame size */
86
	CamConfig_setCamReg(0x14,0x20);  /* reduce frame size */
87
	CamConfig_setCamReg(0x39,0x40);  /* gate PCLK with HREF */
87
	CamConfig_setCamReg(0x39,0x40);  /* gate PCLK with HREF */
88
	CamConfig_setCamReg(0x12,0x28);  /* set RGB mode, with no AWB */
88
	CamConfig_setCamReg(0x12,0x28);  /* set RGB mode, with no AWB */
89
	CamConfig_setCamReg(0x28,0x05);  /* set color sequencer */
89
	CamConfig_setCamReg(0x28,0x05);  /* set color sequencer */
90
    CamConfig_setCamReg(0x13,0x01);  /* un-tri-state the Y/UV lines */
90
    CamConfig_setCamReg(0x13,0x01);  /* un-tri-state the Y/UV lines */
91
	
91
	
92
	/* send the first four cmds in the I2C fifo */
92
	/* send the first four cmds in the I2C fifo */
93
	CamConfig_sendFifoCmds();	
93
	CamConfig_sendFifoCmds();	
94
}
94
}
95
 
95
 
96
 
96
 
97
/***********************************************************
97
/***********************************************************
98
	Function Name: CamConfig_setCamReg
98
	Function Name: CamConfig_setCamReg
99
	Function Description: This function is responsible for
99
	Function Description: This function is responsible for
100
	creating an I2C cmd structure and placing it into the
100
	creating an I2C cmd structure and placing it into the
101
	cmd fifo.
101
	cmd fifo.
102
	Inputs:  reg - the register to modify
102
	Inputs:  reg - the register to modify
103
	         val - the new value of the register
103
	         val - the new value of the register
104
	Outputs: none
104
	Outputs: none
105
***********************************************************/	
105
***********************************************************/	
106
void CamConfig_setCamReg(unsigned char reg, unsigned char val)
106
void CamConfig_setCamReg(unsigned char reg, unsigned char val)
107
{
107
{
108
	i2cCmd_t cmd;
108
	i2cCmd_t cmd;
109
	
109
	
110
	cmd.configReg = reg;
110
	cmd.configReg = reg;
111
	cmd.data = val;
111
	cmd.data = val;
112
#ifndef SIMULATION	
112
#ifndef SIMULATION	
113
	CamConfig_writeTxFifo(cmd);
113
	CamConfig_writeTxFifo(cmd);
114
#endif	
114
#endif	
115
}
115
}
116
/***********************************************************
116
/***********************************************************
117
	Function Name: CamConfig_sendFifoCmds
117
	Function Name: CamConfig_sendFifoCmds
118
	Function Description: This function is responsible for
118
	Function Description: This function is responsible for
119
	sending the entire contents of the config fifo.  This
119
	sending the entire contents of the config fifo.  This
120
	function won't return until the configuration process
120
	function won't return until the configuration process
121
	is complete (or an error is encountered).
121
	is complete (or an error is encountered).
122
	Inputs:  none
122
	Inputs:  none
123
	Outputs: none
123
	Outputs: none
124
	Note: Since this function is written to use the TWI
124
	Note: Since this function is written to use the TWI
125
	interrupt in the I2CInterface module, there will be 
125
	interrupt in the I2CInterface module, there will be 
126
	some busy-waiting here...no big deal, since we end up
126
	some busy-waiting here...no big deal, since we end up
127
	having to trash the frame that we are executing this
127
	having to trash the frame that we are executing this
128
	slave write in anyway (since we can't meet the strict
128
	slave write in anyway (since we can't meet the strict
129
	timing requirements and write i2c at the same time).
129
	timing requirements and write i2c at the same time).
130
***********************************************************/	
130
***********************************************************/	
131
void CamConfig_sendFifoCmds(void)
131
void CamConfig_sendFifoCmds(void)
132
{
132
{
133
	i2cCmd_t cmd;
133
	i2cCmd_t cmd;
134
	
134
	
135
	while (CamConfig_txFifoHead != CamConfig_txFifoTail)
135
	while (CamConfig_txFifoHead != CamConfig_txFifoTail)
136
	{
136
	{
137
		cmd = CamConfig_readTxFifo();
137
		cmd = CamConfig_readTxFifo();
138
		I2CInt_writeData(CAM_ADDRESS,&cmd.configReg,SIZE_OF_I2C_CMD);
138
		I2CInt_writeData(CAM_ADDRESS,&cmd.configReg,SIZE_OF_I2C_CMD);
139
		Utility_delay(100);		
139
		Utility_delay(100);		
140
		/* wait for the I2C transaction to complete */
140
		/* wait for the I2C transaction to complete */
141
		while(I2CInt_isI2cBusy() == TRUE);
141
		while(I2CInt_isI2cBusy() == TRUE);
142
	} 
142
	} 
143
}
143
}
144
 
144
 
145
/***********************************************************
145
/***********************************************************
146
	Function Name: CamConfig_writeTxFifo
146
	Function Name: CamConfig_writeTxFifo
147
	Function Description: This function is responsible for
147
	Function Description: This function is responsible for
148
	adding a new command to the tx fifo.  It adjusts all
148
	adding a new command to the tx fifo.  It adjusts all
149
	needed pointers.
149
	needed pointers.
150
	Inputs:  cmd - the i2cCmd_t to add to the fifo
150
	Inputs:  cmd - the i2cCmd_t to add to the fifo
151
	Outputs: bool_t - indicating if writing to the fifo
151
	Outputs: bool_t - indicating if writing to the fifo
152
	         causes it to wrap
152
	         causes it to wrap
153
***********************************************************/	
153
***********************************************************/	
154
bool_t CamConfig_writeTxFifo(i2cCmd_t cmd)
154
bool_t CamConfig_writeTxFifo(i2cCmd_t cmd)
155
{
155
{
156
	unsigned char tmpHead;
156
	unsigned char tmpHead;
157
	bool_t retVal = TRUE;
157
	bool_t retVal = TRUE;
158
 	
158
 	
159
	CamConfig_txFifo[CamConfig_txFifoHead] = cmd;
159
	CamConfig_txFifo[CamConfig_txFifoHead] = cmd;
160
		
160
		
161
	/* see if we need to wrap */
161
	/* see if we need to wrap */
162
	tmpHead = (CamConfig_txFifoHead+1) & (CAM_CONFIG_TX_FIFO_MASK);
162
	tmpHead = (CamConfig_txFifoHead+1) & (CAM_CONFIG_TX_FIFO_MASK);
163
	CamConfig_txFifoHead = tmpHead;
163
	CamConfig_txFifoHead = tmpHead;
164
	
164
	
165
	/* check to see if we have filled up the queue */
165
	/* check to see if we have filled up the queue */
166
	if (CamConfig_txFifoHead == CamConfig_txFifoTail)
166
	if (CamConfig_txFifoHead == CamConfig_txFifoTail)
167
	{
167
	{
168
		/* we wrapped the fifo...return false */
168
		/* we wrapped the fifo...return false */
169
		retVal = FALSE;
169
		retVal = FALSE;
170
	}
170
	}
171
	return(retVal);
171
	return(retVal);
172
}
172
}
173
 
173
 
174
/***********************************************************
174
/***********************************************************
175
	Function Name: CamConfig_readTxFifo
175
	Function Name: CamConfig_readTxFifo
176
	Function Description: This function is responsible for
176
	Function Description: This function is responsible for
177
	reading a cmd out of the tx fifo.
177
	reading a cmd out of the tx fifo.
178
	Inputs:  none
178
	Inputs:  none
179
	Outputs: i2cCmd_t - the cmd read from the fifo
179
	Outputs: i2cCmd_t - the cmd read from the fifo
180
***********************************************************/	
180
***********************************************************/	
181
static i2cCmd_t CamConfig_readTxFifo(void)
181
static i2cCmd_t CamConfig_readTxFifo(void)
182
{
182
{
183
	i2cCmd_t cmd;
183
	i2cCmd_t cmd;
184
	unsigned char tmpTail;
184
	unsigned char tmpTail;
185
	
185
	
186
	/* just return the current tail from the rx fifo */
186
	/* just return the current tail from the rx fifo */
187
	cmd = CamConfig_txFifo[CamConfig_txFifoTail];	
187
	cmd = CamConfig_txFifo[CamConfig_txFifoTail];	
188
	tmpTail = (CamConfig_txFifoTail+1) & (CAM_CONFIG_TX_FIFO_MASK);
188
	tmpTail = (CamConfig_txFifoTail+1) & (CAM_CONFIG_TX_FIFO_MASK);
189
	CamConfig_txFifoTail = tmpTail;
189
	CamConfig_txFifoTail = tmpTail;
190
	
190
	
191
	return(cmd);
191
	return(cmd);
192
}
192
}