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

📄 image_sensor.c

📁 原相摄像头驱动代码
💻 C
📖 第 1 页 / 共 3 页
字号:
/*************************************************************************
* FUNCTION
*	get_PAS6311_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_PAS6311_period(kal_uint16 *pixel_number, kal_uint16 *line_number)
{
	*pixel_number=PV_PERIOD_PIXEL_NUMS;			/* pixel numbers in one period of HSYNC */
	*line_number=PV_PERIOD_LINE_NUMS;				/* line numbers in one period of VSYNC */
}	/* get_PAS6311_period */

void PAS6311_preview(image_sensor_exposure_window_struct *image_window, image_sensor_config_struct *sensor_config_data)
{
	kal_uint16 test;
	kal_uint16 temp_reg, PV_W=0, PV_H=0;

	kal_prompt_trace(MOD_ENG,"PAS6311_preview");

#ifdef USE_48MHZ
	ENABLE_CAMERA_PIXEL_CLKIN_ENABLE;//[12]
	ENABLE_CAMERA_TG_CLK_48M;//[8]
	UPLL_Enable(UPLL_OWNER_ISP);
//	PIXEL_CLK = 48000000 / (((DRV_Reg32(ISP_TG_PHASE_COUNTER_REG)&0xF0)>>4)+1);
//#else
//	PIXEL_CLK = 52000000 / (((DRV_Reg32(ISP_TG_PHASE_COUNTER_REG)&0xF0)>>4)+1);
#endif
//	MAX_EXPOSURE_LINES = (PIXEL_CLK/MIN_FRAME_RATE)/PV_PERIOD_PIXEL_NUMS;


	#ifdef OUTPUT_DEBUG_INFO
   	sprintf(temp_buffer, "Begin of PAS6311_preview");
	rmmi_write_to_uart((kal_uint8*) temp_buffer, strlen(temp_buffer), KAL_TRUE);
	sprintf(temp_buffer, "pre_shut:%d", exposure_lines);
	rmmi_write_to_uart((kal_uint8*) temp_buffer, strlen(temp_buffer), KAL_TRUE);
	#endif

	gVGAmode=KAL_TRUE;


   	config_PAS6311_window(0,0,PV_ACTIVE_PIXEL_NUMS-1,PV_ACTIVE_LINE_NUMS+6-1);


	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(5);    //12MHz/12MHz
		SET_CMOS_RISING_EDGE(0);
		SET_CMOS_FALLING_EDGE(3);
		ENABLE_CAMERA_PIXEL_CLKIN_ENABLE;
		SET_TG_PIXEL_CLK_DIVIDER(11);
		SET_CMOS_DATA_LATCH(6);

		dummy_pixels=0;
		dummy_lines=0;//(MIN_LINES_PER_FRAME*3/4-PV_PERIOD_LINE_NUMS+50);
		dummy_lines&=(~0x1);

		start_grab_x_offset=0;
		start_grab_y_offset=0;
		PV_W=40;
		PV_H=30;
	}
	else
	{
		MPEG4_encode_mode=KAL_FALSE;

		SET_TG_OUTPUT_CLK_DIVIDER(3);    //24MHz/24MHz
		SET_CMOS_RISING_EDGE(0);
		SET_CMOS_FALLING_EDGE(2);
		ENABLE_CAMERA_PIXEL_CLKIN_ENABLE;
		SET_TG_PIXEL_CLK_DIVIDER(7);
		SET_CMOS_DATA_LATCH(3);

		start_grab_x_offset=0;
		start_grab_y_offset=0;
		PV_W=0;
		PV_H=0;

      	dummy_pixels=100;
		dummy_lines=_DUMMY_;
	}

	preview_pclk_division=((DRV_Reg32(ISP_TG_PHASE_COUNTER_REG)&0xF0)>>4)+1;


#ifdef USE_48MHZ
	ENABLE_CAMERA_TG_CLK_48M;
	UPLL_Enable(UPLL_OWNER_ISP);
//	PIXEL_CLK = 48000000 / (((DRV_Reg32(ISP_TG_PHASE_COUNTER_REG)&0xF0)>>4)+1);
//#else
//	PIXEL_CLK = 52000000 / (((DRV_Reg32(ISP_TG_PHASE_COUNTER_REG)&0xF0)>>4)+1);
#endif
//	MAX_EXPOSURE_LINES = (PIXEL_CLK/MIN_FRAME_RATE)/PV_PERIOD_PIXEL_NUMS;



	temp_reg = read_cmos_sensor(0x21);
	temp_reg &= 0xF3;
	switch (sensor_config_data->image_mirror)
	{
		case IMAGE_NORMAL:
			temp_reg |= 0x0C;						//modify by wankel
			write_cmos_sensor(0x21, temp_reg);		//modify by wankel
			//write_cmos_sensor(0x21, temp_reg);
			kal_prompt_trace(MOD_ENG,"PAS6311_preview",IMAGE_NORMAL);
		break;
		case IMAGE_H_MIRROR:
			temp_reg |= 0x04;
			write_cmos_sensor(0x21, temp_reg);
			kal_prompt_trace(MOD_ENG,"PAS6311_preview",IMAGE_H_MIRROR);
		break;
		case IMAGE_V_MIRROR:
			temp_reg |= 0x08;
			write_cmos_sensor(0x21, temp_reg);
			kal_prompt_trace(MOD_ENG,"PAS6311_preview",IMAGE_V_MIRROR);
		break;
		case IMAGE_HV_MIRROR:
			temp_reg |= 0x0C;
			write_cmos_sensor(0x21, temp_reg);
			kal_prompt_trace(MOD_ENG,"PAS6311_preview",IMAGE_HV_MIRROR);
		break;
	}


	image_window->grab_start_x=IMAGE_SENSOR_PV_INSERTED_PIXELS+start_grab_x_offset;
	image_window->grab_start_y=IMAGE_SENSOR_PV_INSERTED_LINES+start_grab_y_offset;
	image_window->exposure_window_width=IMAGE_SENSOR_PV_WIDTH-PV_W;
	image_window->exposure_window_height=IMAGE_SENSOR_PV_HEIGHT-PV_H;

	write_cmos_sensor(0x03, Nov_Size+dummy_pixels);	//Nov_size
	write_cmos_sensor(0x04, (Line_per_Frame+dummy_lines)&0xFF);
	write_cmos_sensor(0x05, ((Line_per_Frame+dummy_lines)&0x3F00)>>8);
	write_cmos_sensor(0x11, 0x01);

	write_PAS6311_shutter(exposure_lines);

	#ifdef OUTPUT_DEBUG_INFO
   	sprintf(temp_buffer, "pre_shut:%d, dummy_p=%d, dummy_l=%d", exposure_lines, dummy_pixels, dummy_lines);
	rmmi_write_to_uart((kal_uint8*) temp_buffer, strlen(temp_buffer), KAL_TRUE);
	sprintf(temp_buffer, "End of PAS6311_preview");
	rmmi_write_to_uart((kal_uint8*) temp_buffer, strlen(temp_buffer), KAL_TRUE);
	#endif

}	/* PAS6311_preview */

void PAS6311_capture(image_sensor_exposure_window_struct *image_window, image_sensor_config_struct *sensor_config_data)
{
	volatile kal_uint32 shutter=exposure_lines;
	kal_bool	full_cap = 0;

	if(MPEG4_encode_mode)
	    ASSERT(0);

	if( sensor_config_data->meta_mode == KAL_TRUE )
		full_cap = KAL_FALSE;
	else
		full_cap = KAL_TRUE;	// ALWAYS FULL MODE

	if(sensor_config_data->enable_shutter_tansfer==KAL_TRUE)
		shutter=sensor_config_data->capture_shutter;

	#ifdef OUTPUT_DEBUG_INFO
   	sprintf(temp_buffer, "Begin of PAS6311_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

	/* for get_Grabsize function   */
	resolution_info.width=image_window->image_target_width;
	resolution_info.height=image_window->image_target_height;

	// Check Webcam Mode
	if(sensor_config_data->frame_rate==0xF0)
	{
		gVGAmode=KAL_TRUE;

		SET_TG_OUTPUT_CLK_DIVIDER(3);    // 12Mhz/12MHz
		SET_CMOS_RISING_EDGE(0);
		SET_CMOS_FALLING_EDGE(2);
		ENABLE_CAMERA_PIXEL_CLKIN_ENABLE;
		SET_TG_PIXEL_CLK_DIVIDER(7);
		SET_CMOS_DATA_LATCH(3);

		dummy_pixels=0;
		dummy_lines=_DUMMY_;

		MAX_EXPOSURE_LINES = (kal_uint16)((PIXEL_CLK/MIN_FRAME_RATE)/(PV_PERIOD_PIXEL_NUMS));//+dummy_pixels));
		write_cmos_sensor(0x03, Nov_Size);//+dummy_pixels);	//Nov_size
		write_cmos_sensor(0x04, (Line_per_Frame+dummy_lines)&0xFF);
		write_cmos_sensor(0x05, ((Line_per_Frame+dummy_lines)&0x3F00)>>8);
		write_cmos_sensor(0x11, 0x01);

		image_window->grab_start_x=IMAGE_SENSOR_FULL_INSERTED_PIXELS+start_grab_x_offset;
		image_window->grab_start_y=IMAGE_SENSOR_FULL_INSERTED_LINES+dummy_lines+start_grab_y_offset;
		image_window->exposure_window_width=IMAGE_SENSOR_FULL_WIDTH-12;
		image_window->exposure_window_height=IMAGE_SENSOR_FULL_HEIGHT-16;
	}
	else
	{
		gVGAmode=KAL_TRUE;


         if (image_window->digital_zoom_factor>=(ISP_DIGITAL_ZOOM_INTERVAL<<1)||
          ((image_window->image_target_width>640)&&(image_window->image_target_height>480)))
         {
        
          write_cmos_sensor(0x11,0x81);
          
          SET_TG_OUTPUT_CLK_DIVIDER(7);
          SET_CMOS_RISING_EDGE(0);
          SET_CMOS_FALLING_EDGE(3);
          SET_TG_PIXEL_CLK_DIVIDER(12);
          SET_CMOS_DATA_LATCH(6);
          
           dummy_pixels=100;
          dummy_lines=0;
          shutter >>= 1;//2;
         }
         else
         {    
			SET_TG_OUTPUT_CLK_DIVIDER(3);    // 12Mhz/12MHz
			SET_CMOS_RISING_EDGE(0);
			SET_CMOS_FALLING_EDGE(2);
			ENABLE_CAMERA_PIXEL_CLKIN_ENABLE;
			SET_TG_PIXEL_CLK_DIVIDER(7);
		    SET_CMOS_DATA_LATCH(3);

			dummy_pixels=20;
			dummy_lines=_DUMMY_;
           }
			/* reduce exposure time according to the ratio of line width with subsample and without subsample */
//			shutter >>= 1;//2;
//		}

		capture_pclk_division=((DRV_Reg32(ISP_TG_PHASE_COUNTER_REG)&0xF0)>>4)+1;

//  shutter=(shutter*preview_pclk_division)/capture_pclk_division;
//  shutter=(shutter*PV_PERIOD_PIXEL_NUMS)/(FULL_PERIOD_PIXEL_NUMS);//+dummy_pixels);

		image_window->grab_start_x=IMAGE_SENSOR_FULL_INSERTED_PIXELS+start_grab_x_offset;
		image_window->grab_start_y=IMAGE_SENSOR_FULL_INSERTED_LINES+dummy_lines+start_grab_y_offset;
		image_window->exposure_window_width=IMAGE_SENSOR_FULL_WIDTH;
		image_window->exposure_window_height=IMAGE_SENSOR_FULL_HEIGHT;

		write_cmos_sensor(0x03, Nov_Size+dummy_pixels);	//Nov_size
		write_cmos_sensor(0x04, (Line_per_Frame+dummy_lines)&0xFF);
		write_cmos_sensor(0x05, ((Line_per_Frame+dummy_lines)&0x3F00)>>8);
		write_cmos_sensor(0x11, 0x01);
}

	if(shutter<1)
		shutter=1;

	write_PAS6311_shutter(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 PAS6311_capture");
	rmmi_write_to_uart((kal_uint8*) temp_buffer, strlen(temp_buffer), KAL_TRUE);
	#endif

}	/* PAS6311_capture() */

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

	write_cmos_sensor(addr,para);

	//write_cmos_sensor(0x11, 0x01);
}	/* write_PAS6311_reg() */

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

/*************************************************************************
* FUNCTION
*	set_PAS6311_shutter
*
* DESCRIPTION
*	This function set e-shutter of PAS6311 to change exposure time.
*
* PARAMETERS
*	shutter : exposured lines
*
* RETURNS
*	None
*
* GLOBALS AFFECTED
*
*************************************************************************/
void set_PAS6311_shutter(kal_uint16 shutter)
{
   exposure_lines=shutter;
	write_PAS6311_shutter(shutter);
}	/* set_PAS6311_shutter */

/*************************************************************************
* FUNCTION
*	set_PAS6311_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_PAS6311_gain(kal_uint16 gain)
{
	kal_uint16 reg_gain;
//   sensor_global_gain=(gain*sensor_gain_base)/BASEGAIN;
   write_PAS6311_gain(gain);
   reg_gain=gain;//(sensor_global_gain*BASEGAIN);

   #ifdef OUTPUT_DEBUG_INFO
   sprintf(temp_buffer, "fps:%d, expo_lines:%d, gain-AE:%d, return:%d",sensor_frame_rate, exposure_lines, gain, sensor_global_gain);
	rmmi_write_to_uart((kal_uint8*) temp_buffer, strlen(temp_buffer), KAL_TRUE);
	#endif
   return reg_gain;
}

/*************************************************************************
* FUNCTION
*	PAS6311_night_mode
*
* DESCRIPTION
*	This function night mode of PAS6311.
*
* PARAMETERS
*	none
*
* RETURNS
*	None
*
* GLOBALS AFFECTED
*
*************************************************************************/
void PAS6311_night_mode(kal_bool enable)
{
	if(enable)
	{
		MAX_EXPOSURE_LINES = (kal_uint16)(((PIXEL_CLK/2)/VIDEO_NIGHTMODE_FRAME_RATE)/PV_PERIOD_PIXEL_NUMS);
		if(KAL_TRUE == MPEG4_encode_mode)
	        {
	               dummy_lines=422;//(MAX_EXPOSURE_LINES-PV_PERIOD_LINE_NUMS);
	               dummy_lines&=(~0x1);
	               write_cmos_sensor(0x04, (PV_PERIOD_LINE_NUMS+dummy_lines)&0xFF);
	               write_cmos_sensor(0x05, ((PV_PERIOD_LINE_NUMS+dummy_lines)&0x3F00)>>8);
	        }
		if(camera_oper_data.pregain_mode==ISP_ONLY)
		    write_cmos_sensor(0x42,night_gain);
		    sensor_night_mode=KAL_TRUE;
	}
	else
	{
		MAX_EXPOSURE_LINES = (kal_uint16)(((PIXEL_CLK/2)/VIDEO_NORMALMODE_FRAME_RATE)/PV_PERIOD_PIXEL_NUMS);
		if(KAL_TRUE == MPEG4_encode_mode)
		   {
//		      if(MAX_EXPOSURE_LINES <= PV_PERIOD_LINE_NUMS)
		        MAX_EXPOSURE_LINES = PV_PERIOD_LINE_NUMS;
		      dummy_lines=(MAX_EXPOSURE_LINES-PV_PERIOD_LINE_NUMS);
		      dummy_lines&=(~0x1);
		      write_cmos_sensor(0x04, (PV_PERIOD_LINE_NUMS+dummy_lines)&0xFF);
	          write_cmos_sensor(0x05, ((PV_PERIOD_LINE_NUMS+dummy_lines)&0x3F00)>>8);
	      }
		if(camera_oper_data.pregain_mode==ISP_ONLY)
		    write_cmos_sensor(0x10,normal_gain);
		    sensor_night_mode=KAL_FALSE;
	}

}	/* PAS6311_night_mode */

/*************************************************************************
* FUNCTION
*	image_sensor_func_PAS6311
*
* DESCRIPTION
*	PAS6311 Image Sensor functions struct.
*
* PARAMETERS
*	none
*
* RETURNS
*	None
*
* GLOBALS AFFECTED
*
*************************************************************************/
image_sensor_func_struct image_sensor_func_PAS6311=
{
	init_PAS6311,
	get_PAS6311_id,
	get_PAS6311_size,
	get_PAS6311_period,
	PAS6311_preview,
	PAS6311_capture,
	write_PAS6311_reg,
	read_PAS6311_reg,
	set_PAS6311_shutter,
	PAS6311_night_mode,
	power_off_PAS6311,
	set_PAS6311_gain
};	/* image_sensor_func_PAS6311 */

/*************************************************************************
* FUNCTION
*	cam_module_func_config
*
* DESCRIPTION
*	This function maps the external camera module function API structure.
*
* PARAMETERS
*
* RETURNS
*	None
*
* GLOBALS AFFECTED
*
*************************************************************************/
void image_sensor_func_config(void)
{
	image_sensor_func=&image_sensor_func_PAS6311;
}	/* cam_module_func_config() */

// write camera_para to sensor register
void camera_para_to_sensor(void)
{
	kal_uint32	i;
	kal_uint16 count=0;
	kal_uint32  tmp;

	write_cmos_sensor(0x11,0x80);

	write_cmos_sensor(0x02, 0x02);
	write_cmos_sensor(0x03, 0x88);
	write_cmos_sensor(0x04, 0xfc);
	write_cmos_sensor(0x05, 0x01);
	write_cmos_sensor(0x06, 0x0f);
	write_cmos_sensor(0x07, 0x11);
	write_cmos_sensor(0x08, 0x0f);
	write_cmos_sensor(0x09, 0x18);
	write_cmos_sensor(0x0a, 0x3c);
	write_cmos_sensor(0x0b, 0x1e);
	write_cmos_sensor(0x0c, 0x21);
	write_cmos_sensor(0x0d, 0x32);
	write_cmos_sensor(0x0e, 0x00);
	write_cmos_sensor(0x0f, 0x00);
	write_cmos_sensor(0x10, 0x0f);
	write_cmos_sensor(0x12, 0x05);
	write_cmos_sensor(0x17, 0x00);
	write_cmos_sensor(0x18, 0x00);
	write_cmos_sensor(0x19, 0x00);
	write_cmos_sensor(0x1a, 0x00);
	write_cmos_sensor(0x1b, 0x87);
	write_cmos_sensor(0x1c, 0x02);
	write_cmos_sensor(0x1d, 0xe7);

⌨️ 快捷键说明

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