?? image_sensor.c.bak
字號:
/*****************************************************************************
* Copyright Statement:
* --------------------
* This software is protected by Copyright and the information contained
* herein is confidential. The software may not be copied and the information
* contained herein may not be used or disclosed except with the written
* permission of MediaTek Inc. (C) 2005
*
* BY OPENING THIS FILE, BUYER HEREBY UNEQUIVOCALLY ACKNOWLEDGES AND AGREES
* THAT THE SOFTWARE/FIRMWARE AND ITS DOCUMENTATIONS ("MEDIATEK SOFTWARE")
* RECEIVED FROM MEDIATEK AND/OR ITS REPRESENTATIVES ARE PROVIDED TO BUYER ON
* AN "AS-IS" BASIS ONLY. MEDIATEK EXPRESSLY DISCLAIMS ANY AND ALL WARRANTIES,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR NONINFRINGEMENT.
* NEITHER DOES MEDIATEK PROVIDE ANY WARRANTY WHATSOEVER WITH RESPECT TO THE
* SOFTWARE OF ANY THIRD PARTY WHICH MAY BE USED BY, INCORPORATED IN, OR
* SUPPLIED WITH THE MEDIATEK SOFTWARE, AND BUYER AGREES TO LOOK ONLY TO SUCH
* THIRD PARTY FOR ANY WARRANTY CLAIM RELATING THERETO. MEDIATEK SHALL ALSO
* NOT BE RESPONSIBLE FOR ANY MEDIATEK SOFTWARE RELEASES MADE TO BUYER'S
* SPECIFICATION OR TO CONFORM TO A PARTICULAR STANDARD OR OPEN FORUM.
*
* BUYER'S SOLE AND EXCLUSIVE REMEDY AND MEDIATEK'S ENTIRE AND CUMULATIVE
* LIABILITY WITH RESPECT TO THE MEDIATEK SOFTWARE RELEASED HEREUNDER WILL BE,
* AT MEDIATEK'S OPTION, TO REVISE OR REPLACE THE MEDIATEK SOFTWARE AT ISSUE,
* OR REFUND ANY SOFTWARE LICENSE FEES OR SERVICE CHARGE PAID BY BUYER TO
* MEDIATEK FOR SUCH MEDIATEK SOFTWARE AT ISSUE.
*
* THE TRANSACTION CONTEMPLATED HEREUNDER SHALL BE CONSTRUED IN ACCORDANCE
* WITH THE LAWS OF THE STATE OF CALIFORNIA, USA, EXCLUDING ITS CONFLICT OF
* LAWS PRINCIPLES. ANY DISPUTES, CONTROVERSIES OR CLAIMS ARISING THEREOF AND
* RELATED THERETO SHALL BE SETTLED BY ARBITRATION IN SAN FRANCISCO, CA, UNDER
* THE RULES OF THE INTERNATIONAL CHAMBER OF COMMERCE (ICC).
*
*****************************************************************************/
/*****************************************************************************
*
* Filename:
* ---------
* image_sensor.c
*
* Project:
* --------
* Maui_sw
*
* Description:
* ------------
* Image sensor driver function
*
* Author:
* -------
* -------
*
*============================================================================
* HISTORY
* Below this line, this part is controlled by PVCS VM. DO NOT MODIFY!!
*------------------------------------------------------------------------------
*
*------------------------------------------------------------------------------
* Upper this line, this part is controlled by PVCS VM. DO NOT MODIFY!!
*============================================================================
****************************************************************************/
#include "drv_comm.h"
#include "IntrCtrl.h"
#include "reg_base.h"
#include "gpio_sw.h"
#include "sccb.h"
#include "isp_if.h"
#include "image_sensor.h"
#include "camera_para.h"
#include "upll_ctrl.h"
#include "med_api.h"
/* Global Valuable */
SensorInfo g_CCT_MainSensor=OV7660_OMNIVISION;
#if defined(SENSOR_ROTATE_0)
kal_uint8 g_CCT_FirstGrabColor=BAYER_R;
#else
kal_uint8 g_CCT_FirstGrabColor=BAYER_Gb;
#endif
kal_uint8 start_grab_x_offset=0, start_grab_y_offset=0;
kal_bool gVGAmode=KAL_TRUE, sensor_night_mode=KAL_FALSE, MPEG4_encode_mode=KAL_FALSE, g_bMJPEG_mode = KAL_FALSE;
kal_uint8 normal_gain=0, night_gain=SENSOR_NIGHT_MODE_GAIN;
kal_uint8 preview_pclk_division=0, capture_pclk_division=0;
kal_uint16 dummy_pixels=0, dummy_lines=0, extra_exposure_lines=0;
kal_uint16 exposure_lines=0;
kal_uint16 sensor_global_gain=BASEGAIN, sensor_gain_base=0x0;
/* Debug Message, Don't Care */
kal_uint16 sensor_frame_rate;
/* MAX/MIN Explosure Lines Used By AE Algorithm */
kal_uint16 MAX_EXPOSURE_LINES=(PIXEL_CLK/MIN_FRAME_RATE)/VGA_PERIOD_PIXEL_NUMS;
kal_uint8 MIN_EXPOSURE_LINES=2;
/* Parameter For Engineer mode function */
kal_uint32 FAC_SENSOR_REG;
/* Image Sensor ID */
kal_uint16 sensor_id=0;
kal_bool sensor_cap_state=KAL_FALSE;
#ifndef HW_SCCB
void SCCB_send_byte(kal_uint8 send_byte)
{
volatile signed char i;
volatile kal_uint32 j;
for (i=7;i>=0;i--)
{ /* data bit 7~0 */
if (send_byte & (1<<i))
{
SET_SCCB_DATA_HIGH;
}
else
{
SET_SCCB_DATA_LOW;
}
for(j=0;j<SENSOR_I2C_DELAY;j++);
SET_SCCB_CLK_HIGH;
for(j=0;j<SENSOR_I2C_DELAY;j++);
SET_SCCB_CLK_LOW;
for(j=0;j<SENSOR_I2C_DELAY;j++);
}
/* don't care bit, 9th bit */
SET_SCCB_DATA_LOW;
SET_SCCB_DATA_INPUT;
SET_SCCB_CLK_HIGH;
for(j=0;j<SENSOR_I2C_DELAY;j++);
SET_SCCB_CLK_LOW;
SET_SCCB_DATA_OUTPUT;
} /* SCCB_send_byte() */
kal_uint8 SCCB_get_byte(void)
{
volatile signed char i;
volatile kal_uint32 j;
kal_uint8 get_byte=0;
SET_SCCB_DATA_INPUT;
for (i=7;i>=0;i--)
{ /* data bit 7~0 */
SET_SCCB_CLK_HIGH;
for(j=0;j<SENSOR_I2C_DELAY;j++);
if (GET_SCCB_DATA_BIT)
get_byte |= (1<<i);
for(j=0;j<SENSOR_I2C_DELAY;j++);
SET_SCCB_CLK_LOW;
for(j=0;j<SENSOR_I2C_DELAY;j++);
}
/* don't care bit, 9th bit */
SET_SCCB_DATA_OUTPUT;
SET_SCCB_DATA_HIGH;
for(j=0;j<SENSOR_I2C_DELAY;j++);
SET_SCCB_CLK_HIGH;
for(j=0;j<SENSOR_I2C_DELAY;j++);
SET_SCCB_CLK_LOW;
return get_byte;
} /* SCCB_get_byte() */
#endif
void write_cmos_sensor(kal_uint32 addr, kal_uint32 para)
{
volatile kal_uint32 j;
#ifdef HW_SCCB
SET_SCCB_DATA_LENGTH(3);
ENABLE_SCCB;
REG_SCCB_DATA = OV76X0_WRITE_ID | SCCB_DATA_REG_ID_ADDRESS;
REG_SCCB_DATA = addr;
REG_SCCB_DATA = para;
while (SCCB_IS_WRITTING) {};
#else
I2C_START_TRANSMISSION;
for(j=0;j<SENSOR_I2C_DELAY;j++);
SCCB_send_byte(OV76X0_WRITE_ID);
for(j=0;j<SENSOR_I2C_DELAY;j++);
SCCB_send_byte(addr);
for(j=0;j<SENSOR_I2C_DELAY;j++);
SCCB_send_byte(para);
for(j=0;j<SENSOR_I2C_DELAY;j++);
I2C_STOP_TRANSMISSION;
#endif /* HW_SCCB */
} /* write_cmos_sensor() */
kal_uint32 read_cmos_sensor(kal_uint32 addr)
{
volatile kal_uint32 j;
kal_uint8 get_byte=0;
#ifdef HW_SCCB
SET_SCCB_DATA_LENGTH(2);
ENABLE_SCCB;
REG_SCCB_DATA = OV76X0_WRITE_ID | SCCB_DATA_REG_ID_ADDRESS;
REG_SCCB_DATA = addr;
while (SCCB_IS_WRITTING) {};
ENABLE_SCCB;
REG_SCCB_DATA = OV76X0_READ_ID | SCCB_DATA_REG_ID_ADDRESS;
REG_SCCB_DATA=0;
while (SCCB_IS_READING) {};
get_byte = REG_SCCB_READ_DATA & 0xFF;
#else
I2C_START_TRANSMISSION;
for(j=0;j<SENSOR_I2C_DELAY;j++);
SCCB_send_byte(OV76X0_WRITE_ID);
for(j=0;j<SENSOR_I2C_DELAY;j++);
SCCB_send_byte(addr);
for(j=0;j<SENSOR_I2C_DELAY;j++);
I2C_STOP_TRANSMISSION;
for(j=0;j<SENSOR_I2C_DELAY;j++);
I2C_START_TRANSMISSION;
for(j=0;j<SENSOR_I2C_DELAY;j++);
SCCB_send_byte(OV76X0_READ_ID);
for(j=0;j<SENSOR_I2C_DELAY;j++);
get_byte=SCCB_get_byte();
for(j=0;j<SENSOR_I2C_DELAY;j++);
I2C_STOP_TRANSMISSION;
#endif
return get_byte;
} /* read_cmos_sensor() */
void write_OV76X0_shutter(kal_uint16 shutter)
{
kal_uint8 temp_reg;
if(shutter<=VGA_EXPOSURE_LIMITATION)
{
sensor_frame_rate=(10*PIXEL_CLK/VGA_PERIOD_PIXEL_NUMS)/(VGA_PERIOD_LINE_NUMS+dummy_lines);
extra_exposure_lines=0;
}
else
{
sensor_frame_rate=(10*PIXEL_CLK/VGA_PERIOD_PIXEL_NUMS)/(shutter+dummy_lines);
extra_exposure_lines=shutter-VGA_EXPOSURE_LIMITATION;
}
if(shutter>VGA_EXPOSURE_LIMITATION)
shutter=VGA_EXPOSURE_LIMITATION;
write_cmos_sensor(0x2D,extra_exposure_lines&0xFF); // ADVFL(LSB of extra exposure lines)
write_cmos_sensor(0x2E,(extra_exposure_lines&0xFF00)>>8); // ADVFH(MSB of extra exposure lines)
temp_reg=read_cmos_sensor(0x04);
write_cmos_sensor(0x04,( (temp_reg&0xFC) | (shutter&0x3) )); // AEC[b1~b0]
write_cmos_sensor(0x10,((shutter&0x3FC)>>2)); // AEC[b9~b2]
write_cmos_sensor(0x07,((shutter&0x7C00)>>10)); // AEC[b10]/AEC[b15~b10]
} /* write_OV76X0_shutter */
kal_uint16 read_OV76X0_shutter(void)
{
kal_uint8 temp_reg1, temp_reg2, temp_reg3, temp_reg4, temp_reg5;
kal_uint16 shutter;
temp_reg1=read_cmos_sensor(0x04); // AEC[b1~b0]
temp_reg2=read_cmos_sensor(0x10); // AEC[b9~b2]
temp_reg3=read_cmos_sensor(0x07); // AEC[b15~b10]
temp_reg4=read_cmos_sensor(0x2D);
temp_reg5=read_cmos_sensor(0x2E);
shutter=((temp_reg3&0x1F)<<10)|(temp_reg2<<2)|(temp_reg1&0x3); // AEC[b10]/AEC[b15~b10]
extra_exposure_lines=(temp_reg5<<8)|(temp_reg4);
exposure_lines=shutter+extra_exposure_lines;
return exposure_lines;
} /* read_OV76X0_shutter */
void write_OV76X0_gain(kal_uint16 gain)
{
kal_uint16 temp_reg;
if(gain>=1*BASEGAIN && gain<2*BASEGAIN)
{
sensor_global_gain=gain&(~0x3);
temp_reg=(sensor_global_gain-1*BASEGAIN)/4;
}
else if(gain>=2*BASEGAIN && gain<4*BASEGAIN)
{
sensor_global_gain=gain&(~0x7);
temp_reg=0x10;
temp_reg|=(sensor_global_gain-2*BASEGAIN)/8;
}
else if(gain>=4*BASEGAIN && gain<8*BASEGAIN)
{
sensor_global_gain=gain&(~0xF);
temp_reg=0x30;
temp_reg|=(sensor_global_gain-4*BASEGAIN)/16;
}
else if(gain>=8*BASEGAIN && gain<16*BASEGAIN)
{
sensor_global_gain=gain&(~0x1F);
temp_reg=0x70;
temp_reg|=(sensor_global_gain-8*BASEGAIN)/32;
}
else if(gain>=16*BASEGAIN)
{
sensor_global_gain=gain&(~0x3F);
temp_reg=0xF0;
temp_reg|=(sensor_global_gain-16*BASEGAIN)/64;
}
else
ASSERT(0);
write_cmos_sensor(0x00,temp_reg);
} /* write_OV76X0_gain */
kal_uint16 read_OV76X0_gain(void)
{
kal_uint16 sensor_gain;
kal_uint16 temp_reg;
temp_reg=read_cmos_sensor(0x00);
sensor_gain=(BASEGAIN+((temp_reg&0x1F)*BASEGAIN)/16);
if(temp_reg&0x20)
sensor_gain<<=1;
if(temp_reg&0x40)
sensor_gain<<=1;
if(temp_reg&0x80)
sensor_gain<<=1;
return sensor_gain;
} /* read_OV76X0_gain */
void set_OV76X0_dummy(kal_uint16 pixels, kal_uint16 lines)
{
write_cmos_sensor(0x2A,((pixels&0x700)>>4));
write_cmos_sensor(0x2B,(pixels&0xFF));
write_cmos_sensor(0x92,(lines&0xFF));
write_cmos_sensor(0x93,((lines&0xFF00)>>8));
} /* set_OV76X0_dummy */
/*************************************************************************
* FUNCTION
* config_OV76X0_window
*
* DESCRIPTION
* This function config the hardware window of OV76X0 for getting specified
* data of that window.
*
* PARAMETERS
* start_x : start column of the interested window
* start_y : start row of the interested window
* width : column widht of the itnerested window
* height : row depth of the itnerested window
*
* RETURNS
* the data that read from OV76X0
*
* GLOBALS AFFECTED
*
*************************************************************************/
void config_OV76X0_window(kal_uint16 startx,kal_uint16 starty,kal_uint16 width, kal_uint16 height)
{
kal_uint16 endx=(startx+width-1);
kal_uint16 endy=(starty+height-1);
kal_uint8 temp_reg1, temp_reg2;
temp_reg1=(read_cmos_sensor(0x03)&0xF0);
temp_reg2=(read_cmos_sensor(0x32)&0xC0);
// Horizontal
write_cmos_sensor(0x32,0x80|((endx&0x7)<<3)|(startx&0x7)); // b[5:3]:HREF end low 3bits. b[2:0]:HREF start low 3bits.
write_cmos_sensor(0x17,(startx&0x7F8)>>3); // HREF start high 8bits
write_cmos_sensor(0x18,(endx&0x7F8)>>3); // HREF end high 8bits
// Vertical
write_cmos_sensor(0x03,temp_reg1|((endy&0x3)<<2)|(starty&0x3)); // b[3:2]:VREF end low 2bits. b[1:0]:VREF start low 2bits.
write_cmos_sensor(0x19,(starty&0x3FC)>>2); // VREF start high 8bits
write_cmos_sensor(0x1A,(endy&0x3FC)>>2); // VREF end high 8bits
} /* config_OV76X0_window */
/*************************************************************************
* FUNCTION
* init_OV76X0
*
* DESCRIPTION
* This function initialize the registers of CMOS sensor and ISP control register.
*
* PARAMETERS
* None
*
* RETURNS
* None
*
* GLOBALS AFFECTED
*
*************************************************************************/
kal_int8 init_OV76X0(void)
{
cis_module_power_on(KAL_TRUE); // Power On CIS Power
kal_sleep_task(2); // To wait for Stable Power
#if defined(OV7660)
RESET_CMOS_SENSOR_MODE1; // High - reset, Low - normal.
#elif defined(OV7670)
RESET_CMOS_SENSOR_MODE2; // High - normal, Low - reset.
#else
RESET_CMOS_SENSOR_MODE1; // High - reset, Low - normal.
#endif
SET_CMOS_CLOCK_POLARITY_LOW;
SET_VSYNC_POLARITY_LOW;
SET_HSYNC_POLARITY_LOW;
ENABLE_CAMERA_INDATA_FORMAT;
SET_CAMERA_INPUT_TYPE(INPUT_YUV422);
ENABLE_CAMERA_TG_CLK_48M;
UPLL_Enable(UPLL_OWNER_ISP);
set_isp_driving_current(camera_para.SENSOR.reg[CMMCLK_CURRENT_INDEX].para);
// Reset Sensor
write_cmos_sensor(0x12,0x80);
kal_sleep_task(2);
sensor_id=(read_cmos_sensor(0x0A)<<8)|read_cmos_sensor(0x0B);
if((sensor_id != OV7660_SENSOR_ID) && (sensor_id != OV7670_SENSOR_ID))
return -1;
// Initail Sequence Write In.
//write_cmos_sensor(0x11,0x01);
write_cmos_sensor(0x11,0x80);
write_cmos_sensor(0x3a,0x0C);
write_cmos_sensor(0x3D,0xC0);
write_cmos_sensor(0x12,0x00);
write_cmos_sensor(0x15,0x40);
write_cmos_sensor(0x17,0x13);
write_cmos_sensor(0x18,0x01);
write_cmos_sensor(0x32,0xbF);
write_cmos_sensor(0x19,0x02);
write_cmos_sensor(0x1a,0x7a);
write_cmos_sensor(0x03,0x0a);
write_cmos_sensor(0x0c,0x00);
write_cmos_sensor(0x3e,0x00);
write_cmos_sensor(0x70,0x3a);
write_cmos_sensor(0x71,0x35);
write_cmos_sensor(0x72,0x11);
write_cmos_sensor(0x73,0xf0);
write_cmos_sensor(0xa2,0x02);
// add by joe ovt 12/14/2006
write_cmos_sensor(0x7a,0x20);
write_cmos_sensor(0x7b,0x03);
write_cmos_sensor(0x7c,0x0a);
write_cmos_sensor(0x7d,0x1a);
write_cmos_sensor(0x7e,0x3f);
write_cmos_sensor(0x7f,0x4e);
write_cmos_sensor(0x80,0x5b);
write_cmos_sensor(0x81,0x68);
write_cmos_sensor(0x82,0x75);
write_cmos_sensor(0x83,0x7f);
write_cmos_sensor(0x84,0x89);
write_cmos_sensor(0x85,0x9a);
write_cmos_sensor(0x86,0xa6);
write_cmos_sensor(0x87,0xbd);
write_cmos_sensor(0x88,0xd3);
write_cmos_sensor(0x89,0xe8);
write_cmos_sensor(0x13,0xe0);
write_cmos_sensor(0x00,0x00);
write_cmos_sensor(0x10,0x00);
write_cmos_sensor(0x0d,0x40);
write_cmos_sensor(0x14,0x28);
write_cmos_sensor(0xa5,0x02);
write_cmos_sensor(0xab,0x02);
write_cmos_sensor(0x24,0x68);
write_cmos_sensor(0x25,0x58);
write_cmos_sensor(0x26,0xc2); //e3
write_cmos_sensor(0x9f,0x78);
write_cmos_sensor(0xa0,0x68);
write_cmos_sensor(0xa1,0x03);
write_cmos_sensor(0xa6,0xD8);
write_cmos_sensor(0xa7,0xD8);
write_cmos_sensor(0xa8,0xf0);
write_cmos_sensor(0xa9,0x90);
write_cmos_sensor(0xaa,0x14);
write_cmos_sensor(0x13,0xe5);
write_cmos_sensor(0x0e,0x61);
write_cmos_sensor(0x0f,0x4b);
write_cmos_sensor(0x16,0x02);
write_cmos_sensor(0x1e,0x07);
write_cmos_sensor(0x21,0x02);
write_cmos_sensor(0x22,0x91);
write_cmos_sensor(0x29,0x07);
write_cmos_sensor(0x33,0x0b);
write_cmos_sensor(0x35,0x0b);
write_cmos_sensor(0x37,0x1d);
write_cmos_sensor(0x38,0x71);
write_cmos_sensor(0x39,0x2a);
write_cmos_sensor(0x3c,0x78);
write_cmos_sensor(0x4d,0x40);
write_cmos_sensor(0x4e,0x20);
write_cmos_sensor(0x69,0x00);
write_cmos_sensor(0x6b,0x0a);
write_cmos_sensor(0x74,0x10);
write_cmos_sensor(0x8d,0x4f);
write_cmos_sensor(0x8e,0x00);
write_cmos_sensor(0x8f,0x00);
write_cmos_sensor(0x90,0x00);
write_cmos_sensor(0x91,0x00);
write_cmos_sensor(0x96,0x00);
write_cmos_sensor(0x9a,0x80);
write_cmos_sensor(0xb0,0x84);
write_cmos_sensor(0xb1,0x0c);
write_cmos_sensor(0xb2,0x0e);
write_cmos_sensor(0xb3,0x82);
write_cmos_sensor(0xb8,0x0a);
write_cmos_sensor (0x43,0x0a);
write_cmos_sensor(0x44,0xf2);
write_cmos_sensor(0x45,0x39);
write_cmos_sensor(0x46,0x62);
write_cmos_sensor(0x47,0x3d);
write_cmos_sensor(0x48,0x55);
write_cmos_sensor(0x59,0x83);
write_cmos_sensor(0x5a,0x0d);
write_cmos_sensor(0x5b,0xcd);
write_cmos_sensor(0x5c,0x8c);
write_cmos_sensor(0x5d,0x77);
write_cmos_sensor(0x5e,0x16);
write_cmos_sensor(0x6c,0x0a);
write_cmos_sensor(0x6d,0x65);
write_cmos_sensor(0x6e,0x11);
write_cmos_sensor(0x6f,0x9e);
//
write_cmos_sensor(0x6a,0x40);
write_cmos_sensor(0x01,0x56);
write_cmos_sensor(0x02,0x44);
write_cmos_sensor(0x13,0xe7);
/* Color Matrix */
write_cmos_sensor(0x4f,0x88);
write_cmos_sensor(0x50,0x8B);
write_cmos_sensor(0x51,0x04);
write_cmos_sensor(0x52,0x11);
write_cmos_sensor(0x53,0x8C);
write_cmos_sensor(0x54,0x9D);
write_cmos_sensor(0x55,0x00);
write_cmos_sensor(0x56,0x40);
write_cmos_sensor(0x57,0x80);
write_cmos_sensor(0x58,0x9A);
write_cmos_sensor(0x41,0x08);
write_cmos_sensor(0x3f,0x00);
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -