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

📄 image_sensor.c

📁 MTK MT6225平台用的美光摄像头MT9V112驱动
💻 C
📖 第 1 页 / 共 4 页
字号:
/*************************************************************************
* FUNCTION
*	get_MT9V11X_period
*
* DESCRIPTION
*	This function return the image width and height of image sensor.
*
* PARAMETERS
*	*pixel_number : address pointer of pixel numbers in one period of HSYNC
*  *line_number : address pointer of line numbers in one period of VSYNC
*
* RETURNS
*	None
*
* GLOBALS AFFECTED
*
*************************************************************************/
void get_MT9V11X_period(kal_uint16 *pixel_number, kal_uint16 *line_number)
{
	*pixel_number=VGA_PERIOD_PIXEL_NUMS;			/* pixel numbers in one period of HSYNC */
	*line_number=VGA_PERIOD_LINE_NUMS;			/* line numbers in one period of VSYNC */
}	/* get_MT9V11X_period */

void MT9V11X_preview(image_sensor_exposure_window_struct *image_window, image_sensor_config_struct *sensor_config_data)
{	
	sensor_cap_state=KAL_FALSE;
	g_bMJPEG_mode = KAL_FALSE;
	
	write_cmos_sensor(0xF0, 0);					// Switch to Page0
	
	if((sensor_config_data->isp_op_mode==ISP_MJPEG_PREVIEW_MODE)||
		(sensor_config_data->isp_op_mode==ISP_MJPEG_ENCODE_MODE))
	{
	        MPEG4_encode_mode=KAL_FALSE;
			g_bMJPEG_mode = KAL_TRUE;
	    	
	    	/* config TG of ISP to match the setting of image sensor*/
	    	SET_TG_OUTPUT_CLK_DIVIDER(7);
	    	SET_CMOS_RISING_EDGE(0);
	    	SET_CMOS_FALLING_EDGE(3);
	    	ENABLE_CAMERA_PIXEL_CLKIN_ENABLE;
	    	SET_TG_PIXEL_CLK_DIVIDER(7);
	    	SET_CMOS_DATA_LATCH(3);
        
	    	dummy_pixels=0;
	    	dummy_lines=0;
	}
	
	else
	{
	    if(sensor_config_data->frame_rate==0x0F)		// MPEG4 Encode Mode
	    {
	    	MPEG4_encode_mode=KAL_TRUE;
	    	
	    	/* config TG of ISP to match the setting of image sensor*/
	    	SET_TG_OUTPUT_CLK_DIVIDER(3);
	    	SET_CMOS_RISING_EDGE(0);
	    	SET_CMOS_FALLING_EDGE(2);
	    	ENABLE_CAMERA_PIXEL_CLKIN_ENABLE;
	    	SET_TG_PIXEL_CLK_DIVIDER(3);
	    	SET_CMOS_DATA_LATCH(2);
        
	    	dummy_pixels=0;
	    	dummy_lines=0;
	    }
	    else
	    {
	    	MPEG4_encode_mode=KAL_FALSE;
	    	
	    	/* config TG of ISP to match the setting of image sensor*/
	    	SET_TG_OUTPUT_CLK_DIVIDER(1);
	    	SET_CMOS_RISING_EDGE(0);
	    	SET_CMOS_FALLING_EDGE(1);
	    	ENABLE_CAMERA_PIXEL_CLKIN_ENABLE;
	    	SET_TG_PIXEL_CLK_DIVIDER(1);
	    	SET_CMOS_DATA_LATCH(1);
	    	
            	dummy_pixels=0;
            	dummy_lines=0;
	    }
	}
	
	preview_pclk_division=((DRV_Reg32(ISP_TG_PHASE_COUNTER_REG)&0xF0)>>4)+1;

	switch (sensor_config_data->image_mirror)
	{
		case IMAGE_NORMAL:
			SET_CAMERA_INPUT_ORDER(INPUT_ORDER_CrYCbY1);
			write_cmos_sensor(0x20,0x703);
		break;
		case IMAGE_H_MIRROR:
			SET_CAMERA_INPUT_ORDER(INPUT_ORDER_CrYCbY1);
			write_cmos_sensor(0x20,0x701);
		break;
		case IMAGE_V_MIRROR:
			SET_CAMERA_INPUT_ORDER(INPUT_ORDER_CrYCbY1);
			write_cmos_sensor(0x20,0x702);
		break;
		case IMAGE_HV_MIRROR:
			SET_CAMERA_INPUT_ORDER(INPUT_ORDER_CbYCrY1);
			write_cmos_sensor(0x20,0x700);
		break;
	}

	image_window->grab_start_x=IMAGE_SENSOR_VGA_INSERTED_PIXELS+start_grab_x_offset;
	image_window->grab_start_y=IMAGE_SENSOR_VGA_INSERTED_LINES+dummy_lines+start_grab_y_offset;
	image_window->exposure_window_width=IMAGE_SENSOR_VGA_WIDTH;
	image_window->exposure_window_height=IMAGE_SENSOR_VGA_HEIGHT-IMAGE_HEIGHT_OFFSET;
	
	write_cmos_sensor(0x05, (IMAGE_SENSOR_VGA_HBLANKING+dummy_pixels));
	write_cmos_sensor(0x06, (IMAGE_SENSOR_VGA_VBLANKING+dummy_lines));
//	write_cmos_sensor(0x09, exposure_lines);
//	write_cmos_sensor(0xC8, 0x800B);
}	/* MT9V11X_preview */

void MT9V11X_capture(image_sensor_exposure_window_struct *image_window, image_sensor_config_struct *sensor_config_data)
{
    volatile kal_uint32 shutter=exposure_lines;
 
    
    sensor_cap_state=KAL_TRUE;
	
	if(MPEG4_encode_mode)
	    ASSERT(0);
	
	if(sensor_config_data->enable_shutter_tansfer==KAL_TRUE)
		shutter=sensor_config_data->capture_shutter;
		
	#ifdef OUTPUT_DEBUG_INFO
	sprintf(temp_buffer, "Begin of MT9V11X_capture");
	rmmi_write_to_uart((kal_uint8*) temp_buffer, strlen(temp_buffer), KAL_TRUE);
	sprintf(temp_buffer, "cap_shutter:%d, pre_shut:%d", shutter, exposure_lines);
	rmmi_write_to_uart((kal_uint8*) temp_buffer, strlen(temp_buffer), KAL_TRUE);
	#endif
	
	write_cmos_sensor(0xF0, 0x00);		//swithc to page 0
	shutter = read_cmos_sensor(0x09);
	
    if(sensor_config_data->frame_rate==0xF0)	// If WEBCAM mode.
    {
	SET_TG_OUTPUT_CLK_DIVIDER(1);		// 24Mhz/24MHz, Sync
	SET_CMOS_RISING_EDGE(0);
	SET_CMOS_FALLING_EDGE(1);
	ENABLE_CAMERA_PIXEL_CLKIN_ENABLE;		
	SET_TG_PIXEL_CLK_DIVIDER(1);
	SET_CMOS_DATA_LATCH(1);		
						
//	MAX_EXPOSURE_LINES = (kal_uint16)((PIXEL_CLK/MIN_FRAME_RATE)/(PV_PERIOD_PIXEL_NUMS+PV_dummy_pixels));
	write_cmos_sensor(0x05, IMAGE_SENSOR_VGA_HBLANKING+dummy_pixels);
	write_cmos_sensor(0x06, IMAGE_SENSOR_VGA_VBLANKING+dummy_lines);
			
	image_window->grab_start_x=IMAGE_SENSOR_VGA_INSERTED_PIXELS+start_grab_x_offset;
	image_window->grab_start_y=IMAGE_SENSOR_VGA_INSERTED_LINES+start_grab_y_offset;
	image_window->exposure_window_width=IMAGE_SENSOR_VGA_WIDTH-IMAGE_WIDTH_OFFSET;
	image_window->exposure_window_height=IMAGE_SENSOR_VGA_HEIGHT-IMAGE_HEIGHT_OFFSET;		
        
    }
    else if ((image_window->image_target_width<=IMAGE_SENSOR_VGA_WIDTH)&&
	     (image_window->image_target_height<=IMAGE_SENSOR_VGA_HEIGHT))
    {		/* Less than VGA Mode */
		if (image_window->digital_zoom_factor>=(ISP_DIGITAL_ZOOM_INTERVAL<<1))
		{
			SET_TG_OUTPUT_CLK_DIVIDER(3);		// 6/6MHz, Sync
			SET_CMOS_RISING_EDGE(0);
			SET_CMOS_FALLING_EDGE(2);
			ENABLE_CAMERA_PIXEL_CLKIN_ENABLE;
			SET_TG_PIXEL_CLK_DIVIDER(3);
			SET_CMOS_DATA_LATCH(2);
			shutter >>= 1;			
			
		}
		else
		{
			SET_TG_OUTPUT_CLK_DIVIDER(3);		// 12Mhz/12MHz, Sync
			SET_CMOS_RISING_EDGE(0);
			SET_CMOS_FALLING_EDGE(2);
			ENABLE_CAMERA_PIXEL_CLKIN_ENABLE;
			SET_TG_PIXEL_CLK_DIVIDER(3);
			SET_CMOS_DATA_LATCH(2);			
			shutter>>=1;			
		}
		
//		capture_pclk_division=((DRV_Reg32(ISP_TG_PHASE_COUNTER_REG)&0xF0)>>4)+1;
		
//		shutter=(shutter*preview_pclk_division)/capture_pclk_division;
//		shutter=(shutter*VGA_PERIOD_PIXEL_NUMS)/(VGA_PERIOD_PIXEL_NUMS+dummy_pixels);

		write_cmos_sensor(0x05, IMAGE_SENSOR_VGA_HBLANKING+dummy_pixels);
		write_cmos_sensor(0x06, IMAGE_SENSOR_VGA_VBLANKING+dummy_lines);
			
		image_window->grab_start_x=IMAGE_SENSOR_VGA_INSERTED_PIXELS+start_grab_x_offset;
		image_window->grab_start_y=IMAGE_SENSOR_VGA_INSERTED_LINES+start_grab_y_offset;
		image_window->exposure_window_width=IMAGE_SENSOR_VGA_WIDTH;
		image_window->exposure_window_height=IMAGE_SENSOR_VGA_HEIGHT-IMAGE_HEIGHT_OFFSET;	
     }
	
	if(shutter<1)
		shutter=1;

	write_cmos_sensor(0x09, shutter);

	
	#ifdef OUTPUT_DEBUG_INFO
	sprintf(temp_buffer, "cap_shut:%d, pre_shut:%d, pre_pclk_div=%d, cap_pclk_div=%d, dummy_p=%d, dummy_l=%d", shutter, exposure_lines, preview_pclk_division, capture_pclk_division, dummy_pixels, dummy_lines);
	rmmi_write_to_uart((kal_uint8*) temp_buffer, strlen(temp_buffer), KAL_TRUE);
	sprintf(temp_buffer, "End of MT9V11X_capture");
	rmmi_write_to_uart((kal_uint8*) temp_buffer, strlen(temp_buffer), KAL_TRUE);
	#endif
	
}	/* MT9V11X_capture() */

/*************************************************************************
* FUNCTION
*	write_MT9V11X_reg
*
* DESCRIPTION
*	This function set the register of MT9V11X.
*
* PARAMETERS
*	addr : the register index of MT9V11X
*  para : setting parameter of the specified register of MT9V11X
*
* RETURNS
*	None
*
* GLOBALS AFFECTED
*
*************************************************************************/
void write_MT9V11X_reg(kal_uint32 addr, kal_uint32 para)
{
	write_cmos_sensor(addr,para);
}	/* write_MT9V11X_reg() */

/*************************************************************************
* FUNCTION
*	read_cmos_sensor
*
* DESCRIPTION
*	This function read parameter of specified register from MT9V11X.
*
* PARAMETERS
*	addr : the register index of MT9V11X
*
* RETURNS
*	the data that read from MT9V11X
*
* GLOBALS AFFECTED
*
*************************************************************************/
kal_uint32 read_MT9V11X_reg(kal_uint32 addr)
{
	return (read_cmos_sensor(addr));
}	/* read_MT9V11X_reg() */

/*************************************************************************
* FUNCTION
*	set_MT9V11X_shutter
*
* DESCRIPTION
*	This function set e-shutter of MT9V11X to change exposure time.
*
* PARAMETERS
*	shutter : exposured lines
*
* RETURNS
*	None
*
* GLOBALS AFFECTED
*
*************************************************************************/
void set_MT9V11X_shutter(kal_uint16 shutter)
{
	exposure_lines=shutter;
	
	write_cmos_sensor(0xF0, 0);					// Switch to Page0
	write_cmos_sensor(0x09,shutter);
}	/* set_MT9V11X_shutter */

/*************************************************************************
* FUNCTION
*	set_MT9V11X_gain
*
* DESCRIPTION
*	This function is to set global gain to sensor.
*
* PARAMETERS
*	gain : sensor global gain(base: 0x40)
*
* RETURNS
*	the actually gain set to sensor.
*
* GLOBALS AFFECTED
*
*************************************************************************/
kal_uint16 set_MT9V11X_gain(kal_uint16 gain)
{
   sensor_global_gain=(gain*sensor_gain_base)/BASEGAIN;
   write_MT9V11X_gain(sensor_global_gain);
   sensor_global_gain=(sensor_global_gain*BASEGAIN)/sensor_gain_base;
   return sensor_global_gain;
}

/*************************************************************************
* FUNCTION
*	MT9V11X_night_mode
*
* DESCRIPTION
*	This function night mode of MT9V11X.
*
* PARAMETERS
*	none
*
* RETURNS
*	None
*
* GLOBALS AFFECTED
*
*************************************************************************/
void MT9V11X_night_mode(kal_bool enable)
{   
	kal_uint32 dummy = IMAGE_SENSOR_VGA_VBLANKING+dummy_lines;
    if(!sensor_cap_state)
    {   
        if(enable)
        {
        	write_cmos_sensor(0xF0, 0x00);		//swithc to page 0
        	if(KAL_TRUE == MPEG4_encode_mode)
        	{
        		MAX_EXPOSURE_LINES = (kal_uint16)((PIXEL_CLK/2/7.5)/((VGA_PERIOD_PIXEL_NUMS+dummy_pixels)*2));//YUV H-sync data dobule
	          if((MAX_EXPOSURE_LINES>IMAGE_SENSOR_VGA_HEIGHT)&&(dummy<(MAX_EXPOSURE_LINES-IMAGE_SENSOR_VGA_HEIGHT)))     	
	               	dummy=(MAX_EXPOSURE_LINES-IMAGE_SENSOR_VGA_HEIGHT);
	               	dummy&=(~0x1);
	               	write_cmos_sensor(0x06, dummy);
        	}
        	else if(KAL_TRUE == g_bMJPEG_mode)
        	{
        		MAX_EXPOSURE_LINES = (kal_uint16)((PIXEL_CLK/4/7.5)/((VGA_PERIOD_PIXEL_NUMS+dummy_pixels)*2));
	          if((MAX_EXPOSURE_LINES>IMAGE_SENSOR_VGA_HEIGHT)&&(dummy<(MAX_EXPOSURE_LINES-IMAGE_SENSOR_VGA_HEIGHT)))     	
	               	dummy=(MAX_EXPOSURE_LINES-IMAGE_SENSOR_VGA_HEIGHT);
	               	dummy&=(~0x1);
	               	write_cmos_sensor(0x06, dummy);
        	}
        	if(camera_oper_data.pregain_mode==ISP_ONLY)
		    write_cmos_sensor(0x2F,night_gain);
		sensor_night_mode=KAL_TRUE;

        }
        else
        {
        	write_cmos_sensor(0xF0, 0x00);		//swithc to page 0
        	if(KAL_TRUE == MPEG4_encode_mode)
        	{
        		MAX_EXPOSURE_LINES = (kal_uint16)((PIXEL_CLK/2/15)/((VGA_PERIOD_PIXEL_NUMS+dummy_pixels)*2));
	          if((MAX_EXPOSURE_LINES>IMAGE_SENSOR_VGA_HEIGHT)&&(dummy<(MAX_EXPOSURE_LINES-IMAGE_SENSOR_VGA_HEIGHT)))     	
	               	dummy=(MAX_EXPOSURE_LINES-IMAGE_SENSOR_VGA_HEIGHT);
	               	dummy&=(~0x1);
	               	write_cmos_sensor(0x06, dummy);
        	}
        	else if(KAL_TRUE == g_bMJPEG_mode)
        	{
        		MAX_EXPOSURE_LINES = (kal_uint16)((PIXEL_CLK/4/7.5)/((VGA_PERIOD_PIXEL_NUMS+dummy_pixels)*2));
	          if((MAX_EXPOSURE_LINES>IMAGE_SENSOR_VGA_HEIGHT)&&(dummy<(MAX_EXPOSURE_LINES-IMAGE_SENSOR_VGA_HEIGHT)))     	
	               	dummy=(MAX_EXPOSURE_LINES-IMAGE_SENSOR_VGA_HEIGHT);
	               	dummy&=(~0x1);
	               	write_cmos_sensor(0x06, dummy);
        	}
        	if(camera_oper_data.pregain_mode==ISP_ONLY)
		    write_cmos_sensor(0x2F,normal_gain);
		sensor_night_mode=KAL_FALSE;

        }
    }

}	/* MT9V11X_night_mode */

/*************************************************************************
* FUNCTION
*	set_MT9V11X_flashlight
*
* DESCRIPTION
*	turn on/off MT9V11X flashlight .
*
* PARAMETERS
*	none
*
* RETURNS
*	None
*
* GLOBALS AFFECTED
*
*************************************************************************/
void set_MT9V11X_flashlight(kal_bool enable)
{
	// Todo
}

/*************************************************************************
* FUNCTION
*	set_MT9V11X_param_zoom
*
* DESCRIPTION
*	MT9V11X zoom setting.
*
* PARAMETERS
*	none
*
* RETURNS
*	None
*
* GLOBALS AFFECTED
*
*************************************************************************/
kal_uint32 set_MT9V11X_param_zoom(kal_uint32 para)
{
	return KAL_FALSE;
}

/*************************************************************************
* FUNCTION
*	set_MT9V11X_param_contrast
*
* DESCRIPTION
*	MT9V11X contrast setting.
*
* PARAMETERS
*	none
*
* RETURNS
*	None
*
* GLOBALS AFFECTED
*
*************************************************************************/
kal_uint32 set_MT9V11X_param_contrast(kal_uint32 para)
{
//	write_cmos_sensor(0xF0,0x01);		//switch to page 1

	/*Level 0 ~ Level 7 (No contrast/25%/50%/75%/100%/125%/150%/200%) */
//	write_cmos_sensor(0x05,para&0x07);
	return KAL_TRUE;	
}

/*************************************************************************
* FUNCTION
*	set_MT9V11X_param_brightness
*
* DESCRIPTION
*	MT9V11X brightness setting.
*
* PARAMETERS
*	none
*
* RETURNS
*	None
*
* GLOBALS AFFECTED
*
*************************************************************************/
kal_uint32 set_MT9V11X_param_brightness(kal_uint32 para)
{
#if (0)
	kal_uint16 Type;
	
	write_cmos_sensor(0xF0,0x01);		//switch to page 1
	Type = (kal_uint16)(read_cmos_sensor(0x3A)&0x100);
	
	/* Set brightness, the default value is 0x10, the max is 0xFF */
	if (Type == 1)
		write_cmos_sensor(0x34,(para&0xFF)<<8);	//RGB output
	else
		write_cmos_sensor(0x34,para&0xFF);	//YUV output
#endif		
	return KAL_TRUE;	
}

/*************************************************************************
* FUNCTION
*	set_MT9V11X_param_hue
*
* DESCRIPTION
*	MT9V11X hue setting.
*
* PARAMETERS
*	none
*
* RETURNS
*	None
*
* GLOBALS AFFECTED
*
*************************************************************************/
kal_uint32 set_MT9V11X_param_hue(kal_uint32 para)
{
	// Not Support
	return KAL_FALSE;	
}

/*************************************************************************
* FUNCTION
*	set_MT9V11X_param_gamma
*
* DESCRIPTION
*	MT9V11X gamma setting.
*

⌨️ 快捷键说明

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