📄 image_sensor.c
字号:
/*****************************************************************************
* 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
*
****************************************************************************/
#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=OV9660_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 gPVmode=KAL_TRUE, sensor_night_mode=KAL_FALSE, MPEG4_encode_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;
#ifdef OUTPUT_DEBUG_INFO
char temp_buffer[256];
#endif
/* MAX/MIN Explosure Lines Used By AE Algorithm */
kal_uint16 MAX_EXPOSURE_LINES=(PIXEL_CLK/MIN_FRAME_RATE)/PV_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;
sensor_resolution_struct resolution_info;
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 = OV9660_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(OV9660_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 = OV9660_WRITE_ID | SCCB_DATA_REG_ID_ADDRESS;
REG_SCCB_DATA = addr;
while (SCCB_IS_WRITTING) {};
ENABLE_SCCB;
REG_SCCB_DATA = OV9660_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(OV9660_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(OV9660_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_OV9660_shutter(kal_uint16 shutter)
{
kal_uint8 temp_reg,temp_reg1;
if(gPVmode)
{
if(shutter<=PV_EXPOSURE_LIMITATION)
{
sensor_frame_rate=(10*PIXEL_CLK/PV_PERIOD_PIXEL_NUMS)/(PV_PERIOD_LINE_NUMS+dummy_lines);
extra_exposure_lines=0;
}
else
{
sensor_frame_rate=(10*PIXEL_CLK/PV_PERIOD_PIXEL_NUMS)/(shutter+dummy_lines);
extra_exposure_lines=shutter-PV_EXPOSURE_LIMITATION;
}
if(shutter>PV_EXPOSURE_LIMITATION)
shutter=PV_EXPOSURE_LIMITATION;
}
else
{
if(shutter<=FULL_EXPOSURE_LIMITATION)
{
extra_exposure_lines=0;
}
else
{
extra_exposure_lines=shutter-FULL_EXPOSURE_LIMITATION;
}
if(shutter>FULL_EXPOSURE_LIMITATION)
shutter=FULL_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);
temp_reg1=read_cmos_sensor(0x45);
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(0x45,((temp_reg1&0xC0)| (shutter&0xFC00)>>10)); // AEC[b10]/AEC[b15~b10]
} /* write_OV9660_shutter */
kal_uint16 read_OV9660_shutter(void)
{
kal_uint8 temp_reg1, temp_reg2, temp_reg3, temp_reg4, temp_reg5;
kal_uint16 temp_reg;
temp_reg1=read_cmos_sensor(0x04); // AEC[b1~b0]
temp_reg2=read_cmos_sensor(0x10); // AEC[b9~b2]
temp_reg3=read_cmos_sensor(0x45); // AEC[b15~b10]
temp_reg4=read_cmos_sensor(0x2d); // Dummy Line [b7:0]
temp_reg5=read_cmos_sensor(0x2e); // Dummy Line [b15~b10]
temp_reg=((temp_reg3&0x3f)<<10)|(temp_reg2<<2)|(temp_reg1&0x3); // AEC[b10]/AEC[b15~b10]
temp_reg= temp_reg + temp_reg4 + (temp_reg5 <<8); // include dummy lines
return temp_reg;
} /* read_OV9660_shutter */
void write_OV9660_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
ASSERT(0);
write_cmos_sensor(0x00,temp_reg);
} /* write_OV9660_gain */
kal_uint16 read_OV9660_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_OV9660_gain */
void set_OV9660_dummy(kal_uint16 pixels, kal_uint16 lines)
{
write_cmos_sensor(0x2A,((pixels&0x700)>>4));
write_cmos_sensor(0x2B,(pixels&0xFF));
write_cmos_sensor(0x46,(lines&0xFF));
write_cmos_sensor(0x47,((lines&0xFF00)>>8));
} /* set_OV9660_dummy */
/*************************************************************************
* FUNCTION
* config_OV9660_window
*
* DESCRIPTION
* This function config the hardware window of OV9660 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 OV9660
*
* GLOBALS AFFECTED
*
*************************************************************************/
void config_OV9660_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;
startx=startx/2;
starty=starty/2;
endx=endx/2;
endy=endy/2;
temp_reg1=(read_cmos_sensor(0x03)&0xC0);
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&0x7)<<3)|(starty&0x7));// b[5:3]:VREF end low 3bits. b[2:0]:VREF start low 3bits.
write_cmos_sensor(0x19,(starty&0x7F8)>>3); // VREF start high 8bits
write_cmos_sensor(0x1A,(endy&0x7F8)>>3); // VREF end high 8bits
} /* config_OV9660_window */
/*************************************************************************
* FUNCTION
* init_OV9660
*
* DESCRIPTION
* This function initialize the registers of CMOS sensor and ISP control register.
*
* PARAMETERS
* None
*
* RETURNS
* None
*
* GLOBALS AFFECTED
*
*************************************************************************/
kal_int8 init_OV9660(void)
{
cis_module_power_on(KAL_TRUE); // Power On CIS Power
kal_sleep_task(2); // To wait for Stable Power
RESET_CMOS_SENSOR_MODE2; // Low Active
SET_CMOS_CLOCK_POLARITY_LOW;
SET_VSYNC_POLARITY_LOW;
SET_HSYNC_POLARITY_LOW;
//#if defined(SENSOR_ROTATE_0) //倒立问题
// SET_FIRST_GRAB_COLOR(BAYER_R);;
//#else
// SET_FIRST_GRAB_COLOR(BAYER_Gb);;
//#endif
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);
// set_isp_interrupt_trigger_delay_lines(1);
// Reset Sensor
kal_sleep_task(100);
write_cmos_sensor(0x3e,0xd0);
kal_sleep_task(10);
write_cmos_sensor(0x3e,0xd0);
kal_sleep_task(10);
write_cmos_sensor(0x12,0x80);
kal_sleep_task(15);
sensor_id=(read_cmos_sensor(0x0A)<<8)|read_cmos_sensor(0x0B);
if(sensor_id != OV9660_SENSOR_ID)
kal_sleep_task(5);
//init code from frank
//vga mode init code
//write_cmos_sensor(0x12, 0x80);
write_cmos_sensor(0xd5, 0xff);
write_cmos_sensor(0xd6, 0x3f);
write_cmos_sensor(0x3d, 0x3c);
write_cmos_sensor(0x11, 0x81);
write_cmos_sensor(0x2a, 0x00);
write_cmos_sensor(0x2b, 0x00);
//write_cmos_sensor(0x33, 0xc8);
//write_cmos_sensor(0x04, 0xe8);
write_cmos_sensor(0x3a, 0xf1);
write_cmos_sensor(0x3b, 0x00);
//write_cmos_sensor(0x3b, 0x08); // bypass internal regulator
write_cmos_sensor(0x3c, 0x58);
write_cmos_sensor(0x3e, 0x50);
write_cmos_sensor(0x71, 0x00);
write_cmos_sensor(0x15, 0x00);
write_cmos_sensor(0xD7, 0x10);
write_cmos_sensor(0x6a, 0x24);
write_cmos_sensor(0x85, 0xe7);
write_cmos_sensor(0x63, 0x00);
write_cmos_sensor(0x12, 0x40); //vga mode
//write_cmos_sensor(0x14, 0x50);
//write_cmos_sensor(0x0c, 0xb8);
write_cmos_sensor(0x4d, 0x09);
write_cmos_sensor(0x5c, 0x80);
write_cmos_sensor(0x17, 0x0c);
write_cmos_sensor(0x18, 0x5c);
write_cmos_sensor(0x19, 0x02);
write_cmos_sensor(0x1a, 0x3f);
write_cmos_sensor(0x03, 0x03);
write_cmos_sensor(0x32, 0xad);
write_cmos_sensor(0x5a, 0x23);
write_cmos_sensor(0x2b, 0x00);
write_cmos_sensor(0x36, 0xb4);
write_cmos_sensor(0x65, 0x10);
write_cmos_sensor(0x70, 0x02);
write_cmos_sensor(0x71, 0x9f);
write_cmos_sensor(0x64, 0xa4);
//write_cmos_sensor(0x64, 0x24); //for data pixtel color issue , related with voltage
write_cmos_sensor(0x43, 0x00);
write_cmos_sensor(0x5D, 0x55);
write_cmos_sensor(0x5E, 0x57);
write_cmos_sensor(0x5F, 0x21);
write_cmos_sensor(0x24, 0x3e);
write_cmos_sensor(0x25, 0x38);
write_cmos_sensor(0x26, 0x72);
write_cmos_sensor(0x14, 0x28);//38
write_cmos_sensor(0x0C, 0x38);
write_cmos_sensor(0x4F, 0x4f);
write_cmos_sensor(0x50, 0x42);
write_cmos_sensor(0x5A, 0x67);
write_cmos_sensor(0x7d, 0x00);
write_cmos_sensor(0x7e, 0x00);
write_cmos_sensor(0x82, 0x03);
write_cmos_sensor(0x7f, 0x00);
write_cmos_sensor(0x83, 0x07);
write_cmos_sensor(0x80, 0x03);
write_cmos_sensor(0x81, 0x04);
write_cmos_sensor(0x96, 0xf0);
write_cmos_sensor(0x97, 0x0a);
write_cmos_sensor(0x92, 0x33);
write_cmos_sensor(0x94, 0x5a);
write_cmos_sensor(0x93, 0x3a);
write_cmos_sensor(0x95, 0x48);
write_cmos_sensor(0x91, 0xfc);
write_cmos_sensor(0x90, 0xff);
write_cmos_sensor(0x8e, 0x4e);
write_cmos_sensor(0x8f, 0x4e);
write_cmos_sensor(0x8d, 0x13);
write_cmos_sensor(0x8c, 0x0c);
write_cmos_sensor(0x8b, 0x0c);
write_cmos_sensor(0x86, 0x9e);
write_cmos_sensor(0x87, 0x11);
write_cmos_sensor(0x88, 0x22);
write_cmos_sensor(0x89, 0x05);
write_cmos_sensor(0x8a, 0x03);
#if 1
write_cmos_sensor(0xaa, 0x24);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -