📄 image_sensor.c
字号:
// GAMMA setting
write_cmos_sensor(0x60, 0x01); // GMACTL, enable GAMMA
write_cmos_sensor(0x61, 0x03); // GMA0
write_cmos_sensor(0x62, 0x13); // GMA1
write_cmos_sensor(0x63, 0x1E); // GMA2
write_cmos_sensor(0x64, 0x30); // GMA3
write_cmos_sensor(0x65, 0x4F); // GMA4
write_cmos_sensor(0x66, 0x69); // GMA5
write_cmos_sensor(0x67, 0x80); // GMA6
write_cmos_sensor(0x68, 0x93); // GMA7
write_cmos_sensor(0x69, 0xA5); // GMA8
write_cmos_sensor(0x6A, 0xB5); // GMA9
write_cmos_sensor(0x6B, 0xC3); // GMA10
write_cmos_sensor(0x6C, 0xD9); // GMA11
write_cmos_sensor(0x6D, 0xEA); // GMA12
write_cmos_sensor(0x6E, 0xF6); // GMA13
write_cmos_sensor(0x6F, 0xFF); // GMA14
write_cmos_sensor(0x90, 0x80); // EDGECTL, enable edge enchancement
write_cmos_sensor(0x91, 0x9D); // ???
write_cmos_sensor(0x92, 0x02); // ???
write_cmos_sensor(0x93, 0x90); // EDGEGAIN
write_cmos_sensor(0x95, 0x00); // ???
write_cmos_sensor(0x96, 0x08); // ???
write_cmos_sensor(0xA0, 0x1A); // LPFCTL1, enable LPF
write_cmos_sensor(0xA3, 0x30); // ???
write_cmos_sensor(0xA4, 0x08); // ???
write_cmos_sensor(0xA5, 0x28); // ???
write_cmos_sensor(0xB0, 0x22); // ???
write_cmos_sensor(0xB1, 0x22); // ???
write_cmos_sensor(0xC0, 0xC3); // DPCCTL, dead pixel cancellation
write_cmos_sensor(0xC2, 0x44); // ???
write_cmos_sensor(0xC4, 0x30); // ???
write_cmos_sensor(0xC5, 0x10); // ???
write_cmos_sensor(0xC6, 0x08); // ???
write_cmos_sensor(0xC7, 0x7F); // ???
write_cmos_sensor(0xD0, 0x07); // LENSCTL, lens shading correction
write_cmos_sensor(0xD3, 0x88); // LENSR
write_cmos_sensor(0xD4, 0x48); // LENSG
write_cmos_sensor(0xD5, 0x40); // LENSB
write_cmos_sensor(0xD6, 0x50); // ???
write_cmos_sensor(0xD7, 0x40); // ???
CHANGE_TO_PAGE(2);
write_cmos_sensor(0x10, 0x00); // ZOOMCTL, disable image scaling
write_cmos_sensor(0x11, 0x01); // ZOUTWIDH
write_cmos_sensor(0x12, 0x40); // ZOUTWIDL
write_cmos_sensor(0x13, 0x00); // ???
write_cmos_sensor(0x14, 0xF0); // ZOUTHGTL
write_cmos_sensor(0x15, 0x00); // ZWINSTXH
write_cmos_sensor(0x16, 0x00); // ZWINSTXL
write_cmos_sensor(0x17, 0x00); // ???
write_cmos_sensor(0x18, 0x00); // ZWINSTYL
write_cmos_sensor(0x19, 0x01); // ZWINENXH
write_cmos_sensor(0x1A, 0x40); // ZWINENXL
write_cmos_sensor(0x1B, 0x00); // ???
write_cmos_sensor(0x1C, 0xF0); // ZWINENYL
write_cmos_sensor(0x1D, 0x10); // ZVERSTEPH
write_cmos_sensor(0x1E, 0x00); // ZVERSTEPL
write_cmos_sensor(0x1F, 0x10); // ZHORSTEPH
write_cmos_sensor(0x20, 0x00); // ZHORSTEPL
write_cmos_sensor(0x21, 0x55); // ZFIFODLY
write_cmos_sensor(0x62, 0x5A); // ???
CHANGE_TO_PAGE(3);
write_cmos_sensor(0x10, 0x8C); // AECTL1
write_cmos_sensor(0x11, 0x80); // AECTL2
write_cmos_sensor(0x12, 0x30); // AECTL3
write_cmos_sensor(0x13, 0xA5); // ???
write_cmos_sensor(0x14, 0x30); // ???
write_cmos_sensor(0x15, 0x51); // ???
write_cmos_sensor(0x16, 0x47); // ???
write_cmos_sensor(0x19, 0x30); // ISCTL
write_cmos_sensor(0x1C, 0x40); // YLVL
write_cmos_sensor(0x1D, 0x45); // YTH1
write_cmos_sensor(0x1E, 0x28); // YTH2
write_cmos_sensor(0x26, 0x95); // AEWGT
write_cmos_sensor(0x33, 0x00); // EXPTIMEH
write_cmos_sensor(0x34, 0x49); // EXPTIMEM
write_cmos_sensor(0x35, 0x3E); // EXPTIMEL
write_cmos_sensor(0x36, 0x01); // EXPMINH
write_cmos_sensor(0x37, 0x00); // EXPMINL
write_cmos_sensor(0x38, 0x04); // EXPMAXH
write_cmos_sensor(0x39, 0x93); // EXPMAXM
write_cmos_sensor(0x3A, 0xE0); // EXPMAXL
write_cmos_sensor(0x3B, 0x3A); // EXP100H
write_cmos_sensor(0x3C, 0x98); // EXP100L
write_cmos_sensor(0x3D, 0x30); // EXP120H
write_cmos_sensor(0x3E, 0xD4); // EXP120L
write_cmos_sensor(0x3F, 0x49); // EXPDPCH
write_cmos_sensor(0x40, 0x3E); // EXPDPCL
write_cmos_sensor(0x50, 0x16); // AG
write_cmos_sensor(0x51, 0x11); // AGMIN
write_cmos_sensor(0x52, 0x88); // AGMAX
write_cmos_sensor(0x53, 0x11); // AGLVL
write_cmos_sensor(0x54, 0x11); // AGTH1
write_cmos_sensor(0x55, 0x20); // AGTH2
write_cmos_sensor(0x56, 0x30); // AGDPC
write_cmos_sensor(0x57, 0x30); // AGSAT
write_cmos_sensor(0x58, 0x60); // AGBRT
write_cmos_sensor(0x59, 0x1A); // ???
write_cmos_sensor(0x5A, 0x18); // ???
write_cmos_sensor(0x5B, 0x15); // ???
write_cmos_sensor(0x5C, 0x12); // ???
write_cmos_sensor(0x5D, 0x10); // ???
write_cmos_sensor(0x5E, 0x20); // ???
CHANGE_TO_PAGE(4);
write_cmos_sensor(0x10, 0xE9); // AWBCTL1
write_cmos_sensor(0x19, 0x55); // ???
write_cmos_sensor(0x20, 0x13); // UVTH1
write_cmos_sensor(0x21, 0x69); // UVTH2
write_cmos_sensor(0x23, 0xE3); // YRANGE
write_cmos_sensor(0x24, 0xFB); // CDIFF
write_cmos_sensor(0x25, 0x88); // CSUM
write_cmos_sensor(0x2C, 0x08); // WHTPXLTH
write_cmos_sensor(0x40, 0x3E); // RGAIN
write_cmos_sensor(0x41, 0x20); // GGAIN
write_cmos_sensor(0x42, 0x35); // BGAIN
write_cmos_sensor(0x43, 0x7F); // RMAX
write_cmos_sensor(0x44, 0x19); // RMIN
write_cmos_sensor(0x45, 0x5A); // BMAX
write_cmos_sensor(0x46, 0x19); // BMIN
write_cmos_sensor(0x60, 0x80); // ULVL
write_cmos_sensor(0x61, 0x80); // VLVL
CHANGE_TO_PAGE(0);
write_cmos_sensor(0x01, 0x00); // power sleep mode off
} /* MC501CB_Write_Sensor_Initial_Setting */
/*************************************************************************
* FUNCTION
* MC501CB_Init
*
* DESCRIPTION
* This function initialize the registers of CMOS sensor and ISP control register.
*
* PARAMETERS
* None
*
* RETURNS
* None
*
* GLOBALS AFFECTED
*
*************************************************************************/
kal_int8 MC501CB_Init(void)
{
/********************************
* MC501CB's Max MCLK is 27MHz *
********************************/
volatile kal_uint8 iI;
kal_uint8 cTempReg;
set_isp_driving_current(camera_para.SENSOR.reg[CMMCLK_CURRENT_INDEX].para);
cis_module_power_on(KAL_TRUE); // power on CMOS sensor
kal_sleep_task(2); // delay for stable power
// set sensors chip enable pin to high to activate MC501CB
SET_CHIP_ENABLE_HIGH;
/************************************************************************************
* To reset sensor, MC501CB requires to hold RESET pin low for at least 17 MCLKs *
* The following formulates the RESET waveform *
************************************************************************************/
RESET_PIN_HIGH;
kal_sleep_task(1); // high for at least 1ms
RESET_PIN_LOW; // keep low at least 17 MCLK
for (iI = 0; iI < 0x80; iI++);
RESET_PIN_HIGH;
kal_sleep_task(1); // 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_PIXEL_CLKIN_ENABLE; // enable PCLK reference for data latch
ENABLE_CAMERA_INDATA_FORMAT;
SET_CAMERA_INPUT_TYPE(INPUT_YUV422);
// check sensor's device information
cTempReg = MC501CB_ReadReg(0x0004);
if ((cTempReg != MC501CB_SENSOR_ID)&&(cTempReg != MC501CC_SENSOR_ID)) {
return -1;
}
MC501CB_Write_Sensor_Initial_Setting();
// camera_para_to_sensor();
CHANGE_TO_PAGE(3);
g_iPreviewExp = (read_cmos_sensor(0x30) << 16) |
(read_cmos_sensor(0x31) << 8) |
read_cmos_sensor(0x32);
return 1;
} /* MC501CB_Init() */
/*************************************************************************
* FUNCTION
* ConfigVBlank
*
* DESCRIPTION
* This function is to set VBlank size.
*
* PARAMETERS
* iBlank: target VBlank size
*
* RETURNS
* None
*
* GLOBALS AFFECTED
*
*************************************************************************/
void ConfigVBlank(const kal_uint16 iBlank)
{
/********************************************************************
* VSYNCH, VSYNCL must be changed together, otherwise no effect *
********************************************************************/
// VBLANK time = the period of VSYNC signal high period
// if 0x12, page 0, SYNCCTL[3] = 0, VSYNC[0x1D, 0x1E] = VBLANK time
// if 0x12, page 0, SYNCCTL[3] = 1, VSYNC[0x1D, 0x1E] = VBLANK time + VSCLIP[0x1F]
CHANGE_TO_PAGE(0);
write_cmos_sensor(0x1D, iBlank >> 8);
write_cmos_sensor(0x1E, iBlank & 0x00FF);
} /* ConfigVBlank */
/*************************************************************************
* FUNCTION
* ConfigBanding
*
* DESCRIPTION
* This function is to set MC501CB's anti-flicker.
*
* PARAMETERS
* iBanding: banding frequency
*
* RETURNS
* None
*
* GLOBALS AFFECTED
*
*************************************************************************/
void ConfigBanding(const kal_uint8 iBanding)
{
const kal_uint8 iDivider =
g_bVideoMode == KAL_TRUE ? CLOCK_DIVIDER_FOR_VIDEO : CLOCK_DIVIDER_FOR_CAMERA;
const kal_uint32 iOPCLK = MCLK_FOR_MC501CB / iDivider / 2;
kal_uint32 iFlicker = 0;
kal_uint8 iTemp;
switch (iBanding) {
case CAM_BANDING_60HZ:
/****************************************************************************
* High byte of exposure time for 1/120sec *
* EXP120[15:0] = {EXP120H[7:0], EXP120L[7:0]} *
* EXP120 is anti-flickering exposure time step for 120Hz. *
* The unit of exposure time is 8 times of OPCLKˇs period *
* (1 pixel clock period), To. *
* To : period of OPCLK that is internal clock to process one pixel. *
* It is a unit of time in this device. *
* OPCLK = [Scale of Clock Divider * 1/2] * MCLK *
* Example) EXP120 = anti-flickering exposure time step for 120Hz / (8 x *
* period of OPCLK), = 8.3ms / (8 X 83.333ns) = 12500(decimal)= 0x30D4 *
* where MCLK = 24MHz and OPCLK = 12MHz *
****************************************************************************/
iFlicker = iOPCLK / (8 * 120);
ASSERT(iFlicker < (1 << 16))
CHANGE_TO_PAGE(3);
write_cmos_sensor(0x3D, iFlicker >> 8);
write_cmos_sensor(0x3E, iFlicker && 0x000000FF);
iTemp = read_cmos_sensor(0x10) & 0xEF;
write_cmos_sensor(0x10, iTemp); // switch to 60Hz mode
break;
case CAM_BANDING_50HZ:
/****************************************************************************
* High byte of exposure time for 1/100sec *
* EXP100[15:0] = {EXP100H[7:0], EXP100L[7:0]} *
* EXP100 is anti-flickering exposure time step for 100Hz. *
* The unit of exposure time is 8 times of OPCLKˇs period *
* (1 pixel clock period), To. *
* To : period of OPCLK that is internal clock to process one pixel. *
* It is a unit of time in this device. *
* OPCLK = [Scale of Clock Divider * 1/2] * MCLK *
* Example) EXP100 = anti-flickering exposure time step for 100Hz / (8 x *
* period of OPCLK), = 10ms / (8 X 83.333ns) = 15000(decimal)= 0x3A98 *
* where MCLK = 24MHz and OPCLK = 12MHz *
****************************************************************************/
iFlicker = iOPCLK / (8 * 100);
ASSERT(iFlicker < (1 << 16))
CHANGE_TO_PAGE(3);
write_cmos_sensor(0x3B, iFlicker >> 8);
write_cmos_sensor(0x3C, iFlicker && 0x000000FF);
iTemp = read_cmos_sensor(0x10) & 0xEF;
write_cmos_sensor(0x10, iTemp | 0x10); // switch to 50Hz mode
break;
default:
ASSERT(0);
}
g_iFlicker = iFlicker;
} /* ConfigBanding */
/*************************************************************************
* FUNCTION
* ConfigFixFrameRate
*
* DESCRIPTION
* This function set sensor to fixed frame rate output.
*
* PARAMETERS
* fFPS: fixed rate rate
*
* RETURNS
* None
*
* GLOBALS AFFECTED
*
*************************************************************************/
void ConfigFixFrameRate(const float fFPS)
{
/* ******************************************************************************************
* Sensor has fixed frame rate time regardless of the change *
* of exposure time by setting bit[2] of VDOCTL2[0x11:P0]. User *
* should know that frame rate is slow by fixed frame time. *
* Following is the condition for fixed frame rate : *
* 1. Bit[2] of VDOCTL2[0x11:P0] is turned on *
* 2. EXPFIX[0x41, 0x42, 0x43:P3] should be over 1.25 times EXPMAX[0x38,0x39,0x3A:P3]. *
* Fixed Frame time = EXPFIX[0x41, 0x42, 0x42:P3] * 8 * To + *
* VSYNC[0x1D,0x1E:P0] * (640 + HBLANK) * To *
* *
* To = Period of OPCLK *
* OPCLK = MCLK * 1/2 * [Clock Divider] *
********************************************************************************************/
kal_uint8 iTemp;
const float fFrameTime = 1000 * 1.0f / fFPS; // in ms unit
const float fTo = 1000 * 1.0f / (MCLK_FOR_MC501CB / CLOCK_DIVIDER_FOR_VIDEO / 2); // in ms unit
const kal_uint32 iExpFix = (kal_uint32)
((fFrameTime - (VBLANK_VIDEO_PREVIEW * PIXELS_PER_LINE_VIDEO * fTo)) / (8 * fTo));
const kal_uint32 iFactor = (kal_uint32) (iExpFix / 1.25f / g_iFlicker);
const kal_uint32 iMaxExp = iFactor * g_iFlicker;
ASSERT(iExpFix < (1 << 24));
// setup EXPFIX
CHANGE_TO_PAGE(3);
write_cmos_sensor(0x41, iExpFix >> 16);
write_cmos_sensor(0x42, (iExpFix >> 8) & 0x000000FF);
write_cmos_sensor(0x43, iExpFix & 0x000000FF);
// setup EXPMAX
write_cmos_sensor(0x38, iMaxExp >> 16);
write_cmos_sensor(0x39, (iMaxExp >> 8) & 0x000000FF);
write_cmos_sensor(0x3A, iMaxExp & 0x000000FF);
// turn on fixed frame rate
CHANGE_TO_PAGE(0);
iTemp = read_cmos_sensor(0x11) & 0xFB;
write_cmos_sensor(0x11, iTemp | 0x04);
} /* ConfigFixFrameRate */
/*************************************************************************
* FUNCTION
* ConfigMinFrameRate
*
* DESCRIPTION
* This function setup the MAX exposure time to control the MIN frame rate
*
* PARAMETERS
* fFPS: fixed rate rate
*
* RETURNS
* None
*
* GLOBALS AFFECTED
*
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -