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