📄 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
*
*============================================================================
* 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; // 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_uint8 g_iBanding = CAM_BANDING_60HZ;
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 = SIV100A_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(SIV100A_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 = SIV100A_I2C_WRITE_ID | SCCB_DATA_REG_ID_ADDRESS;
REG_SCCB_DATA = addr;
while (SCCB_IS_WRITTING);
ENABLE_SCCB;
REG_SCCB_DATA = SIV100A_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(SIV100A_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(SIV100A_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
* SIV100A_Write_Sensor_Initial_Setting
*
* DESCRIPTION
* This function initialize the registers of CMOS sensor.
*
* PARAMETERS
* None
*
* RETURNS
* None
*
* GLOBALS AFFECTED
*
*************************************************************************/
void SIV100A_Write_Sensor_Initial_Setting(void)
{
write_cmos_sensor(0x04, 0x00); // setup clock divider and HV-mirror
write_cmos_sensor(0x05, 0x07); // setup VGA output mode
// Vendor recommended value ### Don't change ###
write_cmos_sensor(0x10, 0x13);
write_cmos_sensor(0x11, 0x15);//for 2.8V AVDD, if AVDD is 2.5V, then change to 0x05
write_cmos_sensor(0x12, 0x0A);
write_cmos_sensor(0x14, 0x1F);
write_cmos_sensor(0x18, 0x00);
write_cmos_sensor(0x19, 0x7C);
// Vendor recommend value
// SIV100A 24MHz 60Hz - 24.00FPS(120/5)
write_cmos_sensor(0x20, 0x00);
// write_cmos_sensor(0x21, 0xC4); SET's setting
write_cmos_sensor(0x21, 0x03); // Kerwin's setting
write_cmos_sensor(0x22, 0x03);
write_cmos_sensor(0x34, 0x64);
// SIV100A 24MHz 50Hz - 25.00FPS(100/4)
write_cmos_sensor(0x23, 0x00);
// write_cmos_sensor(0x24, 0x9C); // SET's setting
write_cmos_sensor(0x24, 0x03); // Kerwin's setting
write_cmos_sensor(0x25, 0x03);
write_cmos_sensor(0x35, 0x7D);
write_cmos_sensor(0x33, 0x14); // Min FPS, AC60Hz = 6, AC50Hz = 5
// AE Block
write_cmos_sensor(0x40, 0x80); // AE Enable for AC 60Hz
//write_cmos_sensor(0x40, 0x9E); // AE Enable for AC 50Hz
write_cmos_sensor(0x41, 0x8A);
write_cmos_sensor(0x42, 0x7F);
write_cmos_sensor(0x43, 0xC0);
write_cmos_sensor(0x44, 0x40);
write_cmos_sensor(0x45, 0x22);
write_cmos_sensor(0x46, 0x0A);
write_cmos_sensor(0x47, 0x15);
write_cmos_sensor(0x48, 0x16);
write_cmos_sensor(0x49, 0x0B);
write_cmos_sensor(0x4A, 0x53);
write_cmos_sensor(0x4B, 0xC1);
write_cmos_sensor(0x4C, 0x0C);
write_cmos_sensor(0x4E, 0x97);
write_cmos_sensor(0x4F, 0x8A);
write_cmos_sensor(0x5A, 0x00);
// AWB Block
write_cmos_sensor(0x60, 0x90); // AWB Enable
write_cmos_sensor(0x61, 0x83);
write_cmos_sensor(0x62, 0x01);
write_cmos_sensor(0x63, 0x80);
write_cmos_sensor(0x64, 0x80);
write_cmos_sensor(0x65, 0xD0);
write_cmos_sensor(0x66, 0x8C);
write_cmos_sensor(0x67, 0xC8);
write_cmos_sensor(0x68, 0x8B);
write_cmos_sensor(0x69, 0x30);
write_cmos_sensor(0x6A, 0x2C);
write_cmos_sensor(0x6B, 0x80);
write_cmos_sensor(0x6C, 0x01);
write_cmos_sensor(0x6D, 0xE0);
write_cmos_sensor(0x6E, 0x30);
write_cmos_sensor(0x6F, 0xC0);
write_cmos_sensor(0x70, 0x80);
write_cmos_sensor(0x71, 0x32);
write_cmos_sensor(0x72, 0x32);
write_cmos_sensor(0x73, 0x80);
write_cmos_sensor(0x74, 0x80);
write_cmos_sensor(0x75, 0x40);
write_cmos_sensor(0x80, 0xAF);
write_cmos_sensor(0x81, 0x1D);// choose correct polarity, especially PCLK
write_cmos_sensor(0x83, 0x00);
write_cmos_sensor(0x85, 0xA1);
write_cmos_sensor(0x86, 0x00);
write_cmos_sensor(0x87, 0x2D);
write_cmos_sensor(0x88, 0x0F);
write_cmos_sensor(0x89, 0x10);
write_cmos_sensor(0x8D, 0x7C);
write_cmos_sensor(0x90, 0x80);
write_cmos_sensor(0x91, 0x80);
write_cmos_sensor(0x92, 0x80);
write_cmos_sensor(0x93, 0x80);
write_cmos_sensor(0x97, 0x01);
write_cmos_sensor(0xA0, 0x40);
write_cmos_sensor(0xA5, 0x14);
write_cmos_sensor(0xA6, 0x14);
write_cmos_sensor(0xA7, 0x14);
write_cmos_sensor(0xA8, 0x14);
write_cmos_sensor(0xB0, 0xFF);
write_cmos_sensor(0xB1, 0xFF);
write_cmos_sensor(0xB2, 0xFF);
write_cmos_sensor(0xB3, 0xDD);
write_cmos_sensor(0xB4, 0xDD);
write_cmos_sensor(0xB5, 0xBA);
write_cmos_sensor(0xB6, 0x28);
write_cmos_sensor(0xB7, 0x86);
write_cmos_sensor(0xB8, 0x2E);
write_cmos_sensor(0xB9, 0x54);
write_cmos_sensor(0xBA, 0x42);
write_cmos_sensor(0xBB, 0xA0);
write_cmos_sensor(0xBC, 0xA0);
// Gamma
write_cmos_sensor(0xC0, 0x00);
write_cmos_sensor(0xC1, 0x08);
write_cmos_sensor(0xC2, 0x10);
write_cmos_sensor(0xC3, 0x20);
write_cmos_sensor(0xC4, 0x40);
write_cmos_sensor(0xC5, 0x5A);
write_cmos_sensor(0xC6, 0x71);
write_cmos_sensor(0xC7, 0x84);
write_cmos_sensor(0xC8, 0x94);
write_cmos_sensor(0xC9, 0xA2);
write_cmos_sensor(0xCA, 0xAF);
write_cmos_sensor(0xCB, 0xC6);
write_cmos_sensor(0xCC, 0xDC);
write_cmos_sensor(0xCD, 0xF1);
write_cmos_sensor(0xCE, 0xFD);
write_cmos_sensor(0xCF, 0xFF);
// Color Matrix
write_cmos_sensor(0xD0, 0x3D);
write_cmos_sensor(0xD1, 0xC6);
write_cmos_sensor(0xD2, 0xFD);
write_cmos_sensor(0xD3, 0x14);
write_cmos_sensor(0xD4, 0x1A);
write_cmos_sensor(0xD5, 0x13);
write_cmos_sensor(0xD6, 0xF6);
write_cmos_sensor(0xD7, 0xBA);
write_cmos_sensor(0xD8, 0x50);
write_cmos_sensor(0xD9, 0xB4);
write_cmos_sensor(0xDA, 0x20);
write_cmos_sensor(0xDB, 0x2B);
write_cmos_sensor(0xDC, 0xF0);
// Contrast & Saturation
write_cmos_sensor(0xE0, 0x10);
write_cmos_sensor(0xE1, 0x12);
write_cmos_sensor(0xE2, 0x12);
write_cmos_sensor(0xE3, 0x00);
write_cmos_sensor(0xE4, 0xFF);
write_cmos_sensor(0xE5, 0x00);
write_cmos_sensor(0xE6, 0xFF);
write_cmos_sensor(0xE7, 0x00);
write_cmos_sensor(0xE8, 0xFF);
write_cmos_sensor(0xE9, 0x00);
write_cmos_sensor(0xEA, 0x2D);
write_cmos_sensor(0xEB, 0x38);
write_cmos_sensor(0x03, 0xC5);
write_cmos_sensor(0x7A, 0x90);
write_cmos_sensor(0x7B, 0xB0);
write_cmos_sensor(0x7C, 0x80);
} /* SIV100A_Write_Sensor_Initial_Setting */
/*************************************************************************
* FUNCTION
* SIV100A_Init
*
* DESCRIPTION
* This function initialize the registers of CMOS sensor and ISP control register.
*
* PARAMETERS
* None
*
* RETURNS
* None
*
* GLOBALS AFFECTED
*
*************************************************************************/
kal_int8 SIV100A_Init(void)
{
kal_uint8 iI;
// set sensors chip enable pin to low to activate SIV100A
REG_ISP_CMOS_SENSOR_MODE_CONFIG &= ~REG_CMOS_SENSOR_POWER_ON_BIT;
set_isp_driving_current(camera_para.SENSOR.reg[CMMCLK_CURRENT_INDEX].para);
// SIV100A's maximum MCLK is 24MHz
ENABLE_CAMERA_TG_CLK_48M;
ENABLE_CAMERA_PIXEL_CLKIN_ENABLE
UPLL_Enable(UPLL_OWNER_ISP);
cis_module_power_on(KAL_TRUE); // power on CMOS sensor
kal_sleep_task(2); // delay for stable power
/************************************************************************************
* To reset sensor, SIV100A requires to hold RESET pin low for at least 64 MCLKs *
* The following formulates the RESET waveform *
************************************************************************************/
RESET_PIN_HIGH;
RESET_PIN_LOW;
for (iI = 0; iI < 0x40; iI++);
RESET_PIN_HIGH;
kal_sleep_task(2); // delay for stable sensor
/************************************************
* Setup sensor's sync signal output polarity *
************************************************/
SET_CMOS_CLOCK_POLARITY_LOW;
SET_VSYNC_POLARITY_LOW; // valid line data in VSYNC low period
SET_HSYNC_POLARITY_LOW; // valid pixel data in HSYNC high period
ENABLE_CAMERA_INDATA_FORMAT;
SET_CAMERA_INPUT_TYPE(INPUT_YUV422);
// check sensor's device information
if (read_cmos_sensor(0x01) != SIV100A_SENSOR_ID ||
read_cmos_sensor(0x02) != SIV100A_SENSOR_VERSION) {
return -1;
}
SIV100A_Write_Sensor_Initial_Setting();
// camera_para_to_sensor();
return 1;
} /* SIV100A_Init() */
/*************************************************************************
* FUNCTION
* ConfigVBlank
*
* DESCRIPTION
* This function is to set VBlank size.
*
* PARAMETERS
* iBlank: target VBlank size
* iHz: banding frequency
* RETURNS
* None
*
* GLOBALS AFFECTED
*
*************************************************************************/
void ConfigVBlank(const kal_uint16 iBlank, const kal_uint8 iHz)
{
/********************************************
* 50Hz-relative registers can not be used *
********************************************/
const kal_uint16 iVal2Write = iBlank - 1;
ASSERT(iBlank < (1 << 10));
switch (iHz) {
case CAM_BANDING_50HZ:
case CAM_BANDING_60HZ:
write_cmos_sensor(0x20, (read_cmos_sensor(0x20) & 0xFC) | ((iVal2Write & 0x0300) >> 8));
write_cmos_sensor(0x22, iVal2Write & 0x00FF);
break;
// case CAM_BANDING_50HZ:
// SIV100A cannot use all blank size registers of 50Hz
// write_cmos_sensor(0x23, (read_cmos_sensor(0x23) & 0xFC) | ((iVal2Write & 0x0300) >> 8));
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -