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

📄 image_sensor.c

📁 OV2640.rar
💻 C
📖 第 1 页 / 共 4 页
字号:
/*****************************************************************************
*  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 "af.h"

#define	USE_48MHZ
/* Global Variables */
SensorInfo g_CCT_MainSensor = OV2640_OMNIVISION;
kal_uint8 g_CCT_FirstGrabColor = BAYER_Gr;

kal_bool gVGAmode = KAL_TRUE, MPEG4_encode_mode = KAL_FALSE;
static kal_uint8  g_iPV_PCLK_Divider = 1;
kal_uint16 extra_exposure_lines = 0;
static kal_uint16 g_iExpLines = 0;
kal_uint16 sensor_global_gain=BASEGAIN, sensor_gain_base=0x0;
static kal_uint16 g_iPreview_Column_Pixel = 0;
OV2640_OP_TYPE g_iOV2640_Mode = OV2640_MODE_NONE;

/* MAX/MIN Explosure Lines Used By AE Algorithm */
kal_uint16 MAX_EXPOSURE_LINES = 1000;
kal_uint8  MIN_EXPOSURE_LINES = 2;

/* Parameter For Engineer mode function */
kal_uint32 FAC_SENSOR_REG;

extern kal_uint32 SCCB_DELAY;//huafeizhou061016 add

#ifndef HW_SCCB

static void SCCB_send_byte(const kal_uint8 iSendByte)
{
    volatile signed char iI;
    volatile kal_uint8 iJ;

    for (iI = 7; iI >= 0; iI--) {   // data bit 7~0
        if (iSendByte & (1 << iI)) {
            SET_SCCB_DATA_HIGH;
        }else {
            SET_SCCB_DATA_LOW;
        }
        for (iJ = 0; iJ < SENSOR_I2C_DELAY; iJ++);
        SET_SCCB_CLK_HIGH;
        for (iJ = 0; iJ < SENSOR_I2C_DELAY; iJ++);
        SET_SCCB_CLK_LOW;
        for (iJ = 0; iJ < SENSOR_I2C_DELAY; iJ++);
    }

    // don't care bit, 9th bit
    SET_SCCB_DATA_LOW;
    SET_SCCB_DATA_INPUT;
    SET_SCCB_CLK_HIGH;
    for (iJ = 0; iJ < SENSOR_I2C_DELAY; iJ++);
    SET_SCCB_CLK_LOW;
    SET_SCCB_DATA_OUTPUT;
}   /* SCCB_send_byte() */

static kal_uint8 SCCB_get_byte(void)
{
    volatile signed char iI;
    volatile kal_uint8 iJ;
    kal_uint8 iGetByte = 0;

    SET_SCCB_DATA_INPUT;

    for (iJ = 0; iJ < SENSOR_I2C_DELAY; iJ++);

	for (iI = 7; iI >= 0; iI--) {    // data bit 7~0
        SET_SCCB_CLK_HIGH;
        for (iJ = 0; iJ < SENSOR_I2C_DELAY; iJ++);
        if (GET_SCCB_DATA_BIT) {
            iGetByte |= (1 << iI);
        }
        for (iJ = 0; iJ < SENSOR_I2C_DELAY; iJ++);
        SET_SCCB_CLK_LOW;
        for (iJ = 0; iJ < SENSOR_I2C_DELAY; iJ++);
    }

    // don't care bit, 9th bit
    SET_SCCB_DATA_OUTPUT;
    SET_SCCB_DATA_HIGH;
    for (iJ = 0; iJ < SENSOR_I2C_DELAY; iJ++);
    SET_SCCB_CLK_HIGH;
    for (iJ = 0; iJ < SENSOR_I2C_DELAY; iJ++);
    SET_SCCB_CLK_LOW;

	return iGetByte;
}   /* SCCB_get_byte()  */

#endif

static void write_cmos_sensor(const kal_uint32 iAddr, const kal_uint32 iPara)
{
    volatile kal_uint8 iI;

    #ifdef HW_SCCB
        SET_SCCB_DATA_LENGTH(3);
        ENABLE_SCCB;
        REG_SCCB_DATA = OV2640_WRITE_ID | SCCB_DATA_REG_ID_ADDRESS;
        REG_SCCB_DATA = iAddr;
        REG_SCCB_DATA = iPara;
        while (SCCB_IS_WRITTING);
    #else
        I2C_START_TRANSMISSION;
        for (iI = 0; iI < SENSOR_I2C_DELAY; iI++);
        SCCB_send_byte(OV2640_WRITE_ID);
        for (iI = 0; iI < SENSOR_I2C_DELAY; iI++);
        SCCB_send_byte(iAddr);
        for (iI = 0; iI < SENSOR_I2C_DELAY; iI++);
        SCCB_send_byte(iPara);
        for (iI = 0; iI < SENSOR_I2C_DELAY; iI++);
        I2C_STOP_TRANSMISSION;
    #endif  /*  HW_SCCB */
}   /*  write_cmos_sensor()  */

static kal_uint32 read_cmos_sensor(const kal_uint32 iAddr)
{
    volatile kal_uint8 iI;
    kal_uint8 iGetByte = 0;

    #ifdef HW_SCCB
        SET_SCCB_DATA_LENGTH(2);
        ENABLE_SCCB;
        REG_SCCB_DATA = OV2640_WRITE_ID | SCCB_DATA_REG_ID_ADDRESS;
        REG_SCCB_DATA = iAddr;
        while (SCCB_IS_WRITTING);
        ENABLE_SCCB;
        REG_SCCB_DATA = OV2640_READ_ID | SCCB_DATA_REG_ID_ADDRESS;
        REG_SCCB_DATA = 0;
        while (SCCB_IS_READING);
        iGetByte = REG_SCCB_READ_DATA & 0xFF;
    #else
        I2C_START_TRANSMISSION;
        for (iI = 0; iI < SENSOR_I2C_DELAY; iI++);
        SCCB_send_byte(OV2640_WRITE_ID);
        for (iI = 0; iI < SENSOR_I2C_DELAY; iI++);
        SCCB_send_byte(iAddr);
        for (iI = 0; iI < SENSOR_I2C_DELAY; iI++);
        I2C_STOP_TRANSMISSION;
        for (iI = 0; iI < SENSOR_I2C_DELAY; iI++);
        I2C_START_TRANSMISSION;
        for (iI = 0; iI < SENSOR_I2C_DELAY; iI++);
        SCCB_send_byte(OV2640_READ_ID);
        for (iI = 0; iI < SENSOR_I2C_DELAY; iI++);
        iGetByte = SCCB_get_byte();
        for (iI = 0; iI < SENSOR_I2C_DELAY; iI++);
        I2C_STOP_TRANSMISSION;
    #endif

    return iGetByte;
}   /*  read_cmos_sensor()  */

void page_write_cmos_sensor(kal_uint32 iAddr, kal_uint32 iPara)
{
    kal_uint16 iRegPage, iRegAddr;

    iRegPage = iAddr >> 8;
    iRegAddr = iAddr & 0x000000FF;

    write_cmos_sensor(PAGE_SETTING_REG, iRegPage);
    write_cmos_sensor(iRegAddr, iPara);
}

static kal_uint32 page_read_cmos_sensor(const kal_uint32 iAddr)
{
    kal_uint16 iRegPage, iRegAddr;
    kal_uint8 iValue = 0x00;

    iRegPage = iAddr >> 8;
    iRegAddr = iAddr & 0x000000FF;

    write_cmos_sensor(PAGE_SETTING_REG, iRegPage);
    iValue = read_cmos_sensor(iRegAddr);

    return iValue;
}

void write_OV2640_shutter(kal_uint16 shutter)
{
    kal_uint8 iTemp;

    if (gVGAmode) {
        if (shutter <= PV_EXPOSURE_LIMITATION) {
            extra_exposure_lines = 0;
        }else {
            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(PAGE_SETTING_REG, 0x01);
    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)

    iTemp = read_cmos_sensor(0x04);
    write_cmos_sensor(0x04, ((iTemp & 0xFC) | (shutter & 0x3)));	// AEC[b1~b0]
    write_cmos_sensor(0x10, ((shutter & 0x3FC) >> 2));						// AEC[b9~b2]
    write_cmos_sensor(0x45, ((shutter & 0xFC00) >> 10));						// AEC[b10]/AEC[b15~b10]
}   /* write_OV2640_shutter */

static kal_uint16 Reg2Gain(const kal_uint8 iReg)
{
    kal_uint8 iI;
    kal_uint16 iGain = BASEGAIN;    // 1x-gain base

    // Range: 1x to 32x
    // Gain = (GAIN[7] + 1) * (GAIN[6] + 1) * (GAIN[5] + 1) * (GAIN[4] + 1) * (1 + GAIN[3:0] / 16)
    for (iI = 7; iI >= 4; iI--) {
        iGain *= (((iReg >> iI) & 0x01) + 1);
    }

    return iGain +  iGain * (iReg & 0x0F) / 16;
}

static kal_uint8 Gain2Reg(const kal_uint16 iGain)
{
    kal_uint8 iReg = 0x00;

    if (iGain < 2 * BASEGAIN) {
        // Gain = 1 + GAIN[3:0](0x00) / 16
        iReg = 16 * (iGain - BASEGAIN) / BASEGAIN;
    }else if (iGain < 4 * BASEGAIN) {
        // Gain = 2 * (1 + GAIN[3:0](0x00) / 16)
        iReg |= 0x10;
        iReg |= 8 * (iGain - 2 * BASEGAIN) / BASEGAIN;
    }else if (iGain < 8 * BASEGAIN) {
        // Gain = 4 * (1 + GAIN[3:0](0x00) / 16)
        iReg |= 0x30;
        iReg |= 4 * (iGain - 4 * BASEGAIN) / BASEGAIN;
    }else if (iGain < 16 * BASEGAIN) {
        // Gain = 8 * (1 + GAIN[3:0](0x00) / 16)
        iReg |= 0x70;
        iReg |= 2 * (iGain - 8 * BASEGAIN) / BASEGAIN;
    }else if (iGain < 32 * BASEGAIN) {
        // Gain = 16 * (1 + GAIN[3:0](0x00) / 16)
        iReg |= 0xF0;
        iReg |= (iGain - 16 * BASEGAIN) / BASEGAIN;
    }else {
        ASSERT(0);
    }

    return iReg;
}

static void OV2640_SetDummy(const kal_uint16 iPixels, const kal_uint16 iLines)
{
    ASSERT(iPixels < 4096);
    write_cmos_sensor(PAGE_SETTING_REG, 0x01);
    write_cmos_sensor(0x2A, (iPixels & 0x0F00) >> 4);
    write_cmos_sensor(0x2B, iPixels & 0x00FF);
    write_cmos_sensor(0x46, iLines & 0x00FF);
    write_cmos_sensor(0x47, iLines >> 8);
}   /*  OV2640_SetDummy */

static void OV2640_InitialSetting(void)
{
#ifdef OV_PROCESSING_RAW
    write_cmos_sensor(PAGE_SETTING_REG, 0x00);
    write_cmos_sensor(0x2C, 0xFF);
    write_cmos_sensor(0x2E, 0xDF);
    write_cmos_sensor(PAGE_SETTING_REG, 0x01);
    write_cmos_sensor(0x3C, 0x32);
    //
    write_cmos_sensor(0x11, 0x00); // clk divider
    write_cmos_sensor(0x09, 0x02);
    write_cmos_sensor(0x04, 0x28);
    write_cmos_sensor(0x13, 0xE0); // AEC/AGC off
    write_cmos_sensor(0x14, 0x48);
    write_cmos_sensor(0x2C, 0x0C); // reserved
    write_cmos_sensor(0x33, 0x78); // reserved
    write_cmos_sensor(0x3A, 0x33); // reserved
    write_cmos_sensor(0x3B, 0xFB); // reserved
    write_cmos_sensor(0x3E, 0x00); // reserved
    write_cmos_sensor(0x43, 0x11); // reserved
    write_cmos_sensor(0x16, 0x10); // reserved
    //
    write_cmos_sensor(0x39, 0x02); // reserved
    //
    write_cmos_sensor(0x35, 0xDA); // reserved
    write_cmos_sensor(0x22, 0x1A); // reserved
    write_cmos_sensor(0x37, 0xC3); // reserved
    write_cmos_sensor(0x23, 0x00); // reserved
    write_cmos_sensor(0x34, 0xC0); // reserved
    write_cmos_sensor(0x36, 0x1A); // reserved
    write_cmos_sensor(0x06, 0x88); // reserved
    write_cmos_sensor(0x07, 0xC0); // reserved
    write_cmos_sensor(0x0D, 0x87);
    write_cmos_sensor(0x0E, 0x41); // reserved
    write_cmos_sensor(0x4C, 0x00); // reserved
    //
    write_cmos_sensor(0x4A, 0x81); // reserved
    write_cmos_sensor(0x21, 0x99); // reserved
    //
    write_cmos_sensor(0x24, 0x40);
    write_cmos_sensor(0x25, 0x38);
    write_cmos_sensor(0x26, 0x82);
    write_cmos_sensor(0x5C, 0x00);
    write_cmos_sensor(0x63, 0x00);
    //
    write_cmos_sensor(0x61, 0x70);
    write_cmos_sensor(0x62, 0x80);
    write_cmos_sensor(0x7C, 0x05); // reserved
    //
    write_cmos_sensor(0x20, 0x80); // reserved
    write_cmos_sensor(0x28, 0x30); // reserved
    write_cmos_sensor(0x6C, 0x00); // reserved
    write_cmos_sensor(0x6E, 0x00); // reserved
    write_cmos_sensor(0x70, 0x02); // reserved
    write_cmos_sensor(0x71, 0x94); // reserved
    write_cmos_sensor(0x73, 0xc1); // reserved
    //
    //write_cmos_sensor(0x3D, 0x34);
    write_cmos_sensor(0x5A, 0x57);
    write_cmos_sensor(0x4F, 0xBB);
    write_cmos_sensor(0x50, 0x9C);
    //
    //
    write_cmos_sensor(PAGE_SETTING_REG, 0x00);
    write_cmos_sensor(0xE5, 0x7F); // Bypass DSP
    write_cmos_sensor(0xF9, 0xC0); // Bypass DSP
    write_cmos_sensor(0x41, 0x24);
    write_cmos_sensor(0xE0, 0x14);
    write_cmos_sensor(0x76, 0xFF); // Bypass DSP
    write_cmos_sensor(0x33, 0xA0);
    write_cmos_sensor(0x42, 0x20);
    write_cmos_sensor(0x43, 0x18);
    write_cmos_sensor(0x4C, 0x00);
    //write_cmos_sensor(0x87, 0xD0);
    write_cmos_sensor(0x88, 0x3F);
    write_cmos_sensor(0xD7, 0x03);
    write_cmos_sensor(0xD9, 0x10);
    write_cmos_sensor(0xD3, 0x82);
    //
    write_cmos_sensor(0xC8, 0x08);
    write_cmos_sensor(0xC9, 0x80);
    //
    write_cmos_sensor(0x7C, 0x00);// SDE command
    write_cmos_sensor(0x7D, 0x00);
    write_cmos_sensor(0x7C, 0x03);
    write_cmos_sensor(0x7D, 0x48);
    write_cmos_sensor(0x7D, 0x48);
    write_cmos_sensor(0x7C, 0x08);
    write_cmos_sensor(0x7D, 0x20);
    write_cmos_sensor(0x7D, 0x10);
    write_cmos_sensor(0x7D, 0x0E);
    //
    write_cmos_sensor(0x92, 0x00);
    write_cmos_sensor(0x93, 0x06);
    write_cmos_sensor(0x93, 0xE4);
    write_cmos_sensor(0x93, 0x05);
    write_cmos_sensor(0x93, 0x05);
    write_cmos_sensor(0x93, 0x00);
    write_cmos_sensor(0x93, 0x04);
    write_cmos_sensor(0x93, 0x00);
    write_cmos_sensor(0x93, 0x00);
    write_cmos_sensor(0x93, 0x00);
    write_cmos_sensor(0x93, 0x00);
    write_cmos_sensor(0x93, 0x00);
    write_cmos_sensor(0x93, 0x00);
    write_cmos_sensor(0x93, 0x00);
    //
    write_cmos_sensor(0xC3, 0xED);
    write_cmos_sensor(0xA4, 0x00);
    write_cmos_sensor(0xA8, 0x00);
    write_cmos_sensor(0xC5, 0x11);
    write_cmos_sensor(0xC6, 0x51);

⌨️ 快捷键说明

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