⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 image_sensor.c

📁 MTK6225摄像头驱动。芯片是SIV120A。
💻 C
📖 第 1 页 / 共 5 页
字号:
/*****************************************************************************
*  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
 *
 *============================================================================
 *             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 "isp_yuv_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;    // must be defined but not referenced by YUV driver
kal_uint8 g_CCT_FirstGrabColor = INPUT_ORDER_CbYCrY1;   // must be defined but not referenced by YUV driver

static kal_bool g_bVideoMode = KAL_FALSE;    // KAL_TRUE for video recorder mode
static kal_bool g_bNightMode = KAL_FALSE;
static kal_bool g_bCaptureMode = KAL_FALSE;
static kal_bool g_bPreviewMode = KAL_FALSE;
static kal_uint8 g_iBanding = CAM_BANDING_50HZ;
static kal_uint8 g_iAE_Meter = CAM_AE_METER_AUTO;

/* MAX/MIN Explosure Lines Used By AE Algorithm */
kal_uint16 MAX_EXPOSURE_LINES = 1000;   // must be defined but not referenced by YUV driver
kal_uint8  MIN_EXPOSURE_LINES = 1;  // must be defined but not referenced by YUV driver

#ifndef HW_SCCB

#define SENSOR_I2C_DELAY    (0x01)

#define I2C_START_TRANSMISSION \
{ \
    volatile kal_uint8 j; \
    SET_SCCB_CLK_OUTPUT; \
    SET_SCCB_DATA_OUTPUT; \
    SET_SCCB_CLK_HIGH; \
    SET_SCCB_DATA_HIGH; \
    for (j = 0; j < SENSOR_I2C_DELAY; j++);\
    SET_SCCB_DATA_LOW; \
    for (j = 0; j < SENSOR_I2C_DELAY; j++);\
    SET_SCCB_CLK_LOW; \
}
    
#define I2C_STOP_TRANSMISSION \
{ \
    volatile kal_uint8 j; \
    SET_SCCB_CLK_OUTPUT; \
    SET_SCCB_DATA_OUTPUT; \
    SET_SCCB_CLK_LOW; \
    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_DATA_HIGH; \
}

static void SCCB_send_byte(kal_uint8 send_byte)
{
    volatile signed char i;
    volatile kal_uint8 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() */

static kal_uint8 SCCB_get_byte(void)
{
    volatile signed char i;
    volatile kal_uint8 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_HIGH;
    SET_SCCB_DATA_OUTPUT;
    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

static void write_cmos_sensor(kal_uint32 addr, kal_uint32 para)
{
    volatile kal_uint8 j;

    #ifdef HW_SCCB
        SET_SCCB_DATA_LENGTH(3);
        ENABLE_SCCB;
        REG_SCCB_DATA = SIV120A_I2C_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(SIV120A_I2C_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() */

static kal_uint32 read_cmos_sensor(kal_uint32 addr)
{
    volatile kal_uint8 j;
    kal_uint8 get_byte = 0;

    #ifdef HW_SCCB
        SET_SCCB_DATA_LENGTH(2);
        ENABLE_SCCB;
        REG_SCCB_DATA = SIV120A_I2C_WRITE_ID | SCCB_DATA_REG_ID_ADDRESS;
        REG_SCCB_DATA = addr;
        while (SCCB_IS_WRITTING);
        ENABLE_SCCB;
        REG_SCCB_DATA = SIV120A_I2C_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(SIV120A_I2C_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(SIV120A_I2C_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() */

/*************************************************************************
* FUNCTION
*   SIV120A_Write_Sensor_Initial_Setting
*
* DESCRIPTION
*   This function initialize the registers of CMOS sensor.
*
* PARAMETERS
*   None
*
* RETURNS
*   None
*
* GLOBALS AFFECTED
*
*************************************************************************/
	void at_test_func(kal_uint32 *d)
	{
		/*----------------------------------------------------------------*/
		/* Local Variables												  */
		/*----------------------------------------------------------------*/
		kal_int32 i;
	
		/*----------------------------------------------------------------*/
		/* Code Body													  */
		/*----------------------------------------------------------------*/
		switch (d[1])
		{
			case 0:
				DRV_Reg(DRVPDN_CON3) &= (~DRVPDN_CON3_ISP);
				GPIO_ModeSetup(12, 1);
				GPIO_ModeSetup(13, 1);
				SET_CMOS_FALLING_EDGE(1);		/* set to HW default */
				SET_TG_PIXEL_CLK_DIVIDER(1);	/* set to HW default */
				ENABLE_CAMERA_TG_PHASE_COUNTER;
				ENABLE_CAMERA_CLOCK_OUTPUT_TO_CMOS;
				RESET_CMOS_SENSOR;
				POWER_ON_CMOS_SENSOR;
				ENABLE_CMOS_SESNOR;
				ENABLE_CAMERA_TG_CLK_48M;
			 UPLL_Enable(UPLL_OWNER_ISP);
	
				kal_prompt_trace(MOD_ENG, "Enable Sensor");
				break;
			case 1:
				sccb_config(SCCB_SW_8BIT, d[2], d[3], NULL);
				kal_prompt_trace(MOD_ENG, "SCCB Config %x %x", d[2], d[3]);
				break;
			case 2:
				RESET_CMOS_SENSOR_MODE1;
				kal_prompt_trace(MOD_ENG, "Reset Sensor Mode 1");
				break;
			case 3:
				RESET_CMOS_SENSOR_MODE2;
				kal_prompt_trace(MOD_ENG, "Reset Sensor Mode 2");
				break;
			case 4:
				kal_prompt_trace(MOD_ENG, "Read Sensor %x = %x", d[2], read_cmos_sensor(d[2]));
			 //   _dbg_print("Read Sensor %x = %x", d[2],read_cmos_sensor(d[2]) );//wei zeng added
			 break;
			case 5:
				write_cmos_sensor(d[2], d[3]);
				kal_prompt_trace(MOD_ENG, "Write Sensor %x = %x", d[2], read_cmos_sensor(d[2]));
			// _dbg_print("Write Sensor %x = %x", d[2],read_cmos_sensor(d[2]) );//wei zeng added
				break;
			case 6:
				break;
			case 7:
				//init_cmos_sensor();
				kal_prompt_trace(MOD_ENG, "Init Sensor");
				break;
			case 8:
				POWER_OFF_CMOS_SENSOR;
				kal_prompt_trace(MOD_ENG, "Power Off Sensor");
				break;
			case 9:
			//	  t[d[2]] = d[3];
			  //  kal_prompt_trace(MOD_ENG, "t[%d] = %d", d[2], t[d[2]]);
			 //   _dbg_print("t[%d] = %d", d[2], t[d[2]]);
				break;
			case 10:
				for (i = 0; i < 103; i++)
				{  /*
					kal_prompt_trace(
						MOD_ENG,
						"%x -> %x (%x)",
						OV7680_Init_Reg[i].addr,
						read_cmos_sensor(OV7680_Init_Reg[i].addr),
						OV7680_Init_Reg[i].value);
					*/	  
				}
				break;
			case 11:
			   /*
			for (i = 0; i < 103; i++)
				{
					if (OV7680_Init_Reg[i].addr == d[2])
						break;
				}
				*/
				if (i < 88)
				{
					//OV7680_Init_Reg[i].value = d[3];
				   // kal_prompt_trace(MOD_ENG, "Reg[%x] = %x", OV7680_Init_Reg[i].addr, OV7680_Init_Reg[i].value);
				}
				else
				{
					kal_prompt_trace(MOD_ENG, "Reg not found!");
				}
				break;
			case 12:
				kal_prompt_trace(MOD_ENG, "Read Addr %x = %x", d[2], DRV_Reg((d[2])));
				break;
			case 13:
				for (i = d[2]; i < d[3]; i++)
				{
					kal_prompt_trace(MOD_ENG, "Reg[%x] = %x", i, read_cmos_sensor(i));
				}
				break;
		}
	}

void SIV120A_Write_Sensor_Initial_Setting(void)
{
//Sensor control block
write_cmos_sensor(0x00, 0x00);
write_cmos_sensor(0x04, 0x00);  
write_cmos_sensor(0x10, 0x01);
write_cmos_sensor(0x11, 0x25);
write_cmos_sensor(0x12, 0x11);
write_cmos_sensor(0x13, 0x00);	//ABS off 
write_cmos_sensor(0x17, 0x82);	//Internal LDO On
write_cmos_sensor(0x42, 0x32);
write_cmos_sensor(0x43, 0x80);

//SIV120A 24MHz Setting for 50Hz
write_cmos_sensor(0x20, 0x00);  
write_cmos_sensor(0x21, 0xC9);
write_cmos_sensor(0x23, 0x65); 

write_cmos_sensor(0x00, 0x01);
write_cmos_sensor(0x34, 0x78);

//AE
write_cmos_sensor(0x00, 0x01);
write_cmos_sensor(0x10, 0x80);
write_cmos_sensor(0x11, 0x0A);
write_cmos_sensor(0x12, 0x80);
write_cmos_sensor(0x13, 0x78);
write_cmos_sensor(0x14, 0x78);
write_cmos_sensor(0x15, 0x78);
write_cmos_sensor(0x16, 0x00);
write_cmos_sensor(0x17, 0x04);
write_cmos_sensor(0x18, 0x00);
write_cmos_sensor(0x19, 0x8E);
write_cmos_sensor(0x1A, 0x06);
write_cmos_sensor(0x1B, 0x00);
write_cmos_sensor(0x1C, 0x18);
write_cmos_sensor(0x1D, 0x01);
write_cmos_sensor(0x1E, 0x08);

write_cmos_sensor(0x40, 0x5F);  
write_cmos_sensor(0x41, 0x08);
write_cmos_sensor(0x42, 0x3F);
write_cmos_sensor(0x43, 0x18);
write_cmos_sensor(0x44, 0x18);
write_cmos_sensor(0x45, 0x20);
write_cmos_sensor(0x46, 0x29);
write_cmos_sensor(0x47, 0x30);
write_cmos_sensor(0x48, 0x33);
write_cmos_sensor(0x49, 0x36);
write_cmos_sensor(0x4A, 0x39);
write_cmos_sensor(0x4B, 0x3B);
write_cmos_sensor(0x4C, 0x3D);
write_cmos_sensor(0x4D, 0x27);
write_cmos_sensor(0x4E, 0x1A);
write_cmos_sensor(0x4F, 0x14);

write_cmos_sensor(0x50, 0x11);  
write_cmos_sensor(0x51, 0x0F);
write_cmos_sensor(0x52, 0x0D);
write_cmos_sensor(0x53, 0x0C);
write_cmos_sensor(0x54, 0x0A);
write_cmos_sensor(0x55, 0x09);

//AWB
write_cmos_sensor(0x00, 0x02);
write_cmos_sensor(0x10, 0xD3);
write_cmos_sensor(0x11, 0xC0);
write_cmos_sensor(0x12, 0x80);
write_cmos_sensor(0x13, 0x80);
write_cmos_sensor(0x14, 0x80);
write_cmos_sensor(0x15, 0xFE);
write_cmos_sensor(0x16, 0x80);
write_cmos_sensor(0x17, 0xEA);
write_cmos_sensor(0x18, 0x80);
write_cmos_sensor(0x19, 0xA0);
write_cmos_sensor(0x1A, 0x70);
write_cmos_sensor(0x1B, 0xA0);
write_cmos_sensor(0x1C, 0x60);
write_cmos_sensor(0x1D, 0xA0);
write_cmos_sensor(0x1E, 0x70);
write_cmos_sensor(0x20, 0xE8);
write_cmos_sensor(0x21, 0x20);
write_cmos_sensor(0x22, 0xA4);
write_cmos_sensor(0x23, 0x20);
write_cmos_sensor(0x25, 0x20);
write_cmos_sensor(0x26, 0x0F);
write_cmos_sensor(0x27, 0x0D);                  
write_cmos_sensor(0x28, 0x90);
write_cmos_sensor(0x29, 0xB8);
write_cmos_sensor(0x2A, 0x90);

write_cmos_sensor(0x30, 0x00);
write_cmos_sensor(0x31, 0x10);
write_cmos_sensor(0x32, 0x00);
write_cmos_sensor(0x33, 0x20);
write_cmos_sensor(0x34, 0x02);
write_cmos_sensor(0x35, 0x76);
write_cmos_sensor(0x36, 0x01);
write_cmos_sensor(0x37, 0xD6);

write_cmos_sensor(0x40, 0x10);
write_cmos_sensor(0x41, 0x04);
write_cmos_sensor(0x42, 0x08);
write_cmos_sensor(0x43, 0x10);
write_cmos_sensor(0x44, 0x12);
write_cmos_sensor(0x45, 0x35);
write_cmos_sensor(0x46, 0x64);

write_cmos_sensor(0x50, 0x33);
write_cmos_sensor(0x51, 0x20);
write_cmos_sensor(0x52, 0xE5);
write_cmos_sensor(0x53, 0xFB);
write_cmos_sensor(0x54, 0x13);
write_cmos_sensor(0x55, 0x26);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -