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

📄 camif.c

📁 2440开发板测试程序(含LCD、摄像头演示等)
💻 C
📖 第 1 页 / 共 2 页
字号:
/************************************************************
File Name	: camif.c
Descriptions
-S3C2440 camera test routines & basic libraries
History
- July 23, 2003. Draft Version 0.0 by purnnamu
- Janualy 15, 2004. Modifed by Boaz

	Copyright 2004 SAMSUNG Electronics.
	However, Anybody can use this code without our permission.  
*************************************************************/

#include "def.h"
#include "option.h"
#include "2440addr.h"
#include "2440lib.h"

#include "camif.h"
#include "camproset.h" // for camera setting

extern unsigned char ucdragon_240_100[];
extern unsigned char xyx_240_320[];
//*****************************************************************************
#define MVAL		(13)
#define MVAL_USED 	(0)		//0=each frame   1=rate by MVAL
#define INVVDEN		(1)		//0=normal       1=inverted
#define BSWP		(0)		//Byte swap control
#define HWSWP		(1)		//Half word swap control

#define M5D(n) ((n) & 0x1fffff)	// To get lower 21bits

//TFT 240320
#define LCD_XSIZE_TFT_240320 	(320)	
#define LCD_YSIZE_TFT_240320 	(240)

//TFT 240320
#define SCR_XSIZE_TFT_240320 	(320)
#define SCR_YSIZE_TFT_240320 	(240)
//#define SCR_XSIZE_TFT_240320 	(320)
//#define SCR_YSIZE_TFT_240320 	(240)

//TFT240320
#define HOZVAL_TFT_240320	(LCD_XSIZE_TFT_240320-1)
#define LINEVAL_TFT_240320	(LCD_YSIZE_TFT_240320-1)
/*********************************************************
//Timing parameter for LCD LQ035Q7DB02
#define VBPD_240320		(4)		//垂直同步信号的后肩
#define VFPD_240320		(4)		//垂直同步信号的前肩
#define VSPW_240320		(4)		//垂直同步信号的脉宽

	#define HBPD_240320		(13)		//水平同步信号的后肩
	#define HFPD_240320		(4)		//水平同步信号的前肩
	#define HSPW_240320		(18)		//水平同步信号的脉宽
	
		#define CLKVAL_TFT_240320	(7) 	
*********************************************************/
/*********************************************************/
//WX3500B_M06
#define VBPD_240320		(14)		//垂直同步信号的后肩
#define VFPD_240320		(11)		//垂直同步信号的前肩
#define VSPW_240320		(2)		//垂直同步信号的脉宽

#define HBPD_240320		(37)		//水平同步信号的后肩
#define HFPD_240320		(19)		//水平同步信号的前肩
#define HSPW_240320		(29)		//水平同步信号的脉宽
#define CLKVAL_TFT_240320	(7) 	
/*********************************************************/
//FCLK=180MHz,HCLK=90MHz,VCLK=6.5MHz

// GPB1/TOUT1 for Backlight control(PWM)
#define GPB1_TO_OUT()   (rGPBUP &= 0xfffd, rGPBCON &= 0xfffffff3, rGPBCON |= 0x00000004)
#define GPB1_TO_1()     (rGPBDAT |= 0x0002)
#define GPB1_TO_0()     (rGPBDAT &= 0xfffd)

volatile static unsigned short LCD_BUFFER_CAM[SCR_YSIZE_TFT_240320][SCR_XSIZE_TFT_240320];
//*****************************************************************************

volatile U32 camTestMode;
volatile U32 camCodecCaptureCount;
volatile U32 camPviewCaptureCount;
volatile U32 camCodecStatus;
volatile U32 camPviewStatus;
volatile U32 amount;

U32 save_GPJCON, save_GPJDAT, save_GPJUP;

U8 flagCaptured_P = 0;
U8 flagCaptured_C = 0;

int Test_OV9650(void);	//hzh

												/**************************************************************
												LCD视频和控制信号输出或者停止,1开启视频输出
**************************************************************/
static void Lcd_EnvidOnOff(int onoff)
{
	if(onoff==1)
		rLCDCON1|=1; // ENVID=ON
	else
		rLCDCON1 =rLCDCON1 & 0x3fffe; // ENVID Off
}

/**************************************************************
320×240 8Bpp TFT LCD 电源控制引脚使能
**************************************************************/
static void Lcd_PowerEnable(int invpwren,int pwren)
{
	//GPG4 is setted as LCD_PWREN
	rGPGUP=rGPGUP&(~(1<<4))|(1<<4); // Pull-up disable
	rGPGCON=rGPGCON&(~(3<<8))|(3<<8); //GPG4=LCD_PWREN
	rGPGDAT = rGPGDAT | (1<<4) ;
	//	invpwren=pwren;
	//Enable LCD POWER ENABLE Function
	rLCDCON5=rLCDCON5&(~(1<<3))|(pwren<<3);   // PWREN
	rLCDCON5=rLCDCON5&(~(1<<5))|(invpwren<<5);   // INVPWREN
}
//*****************************************************************************
static void LCD_LTS350Q1_PE1_Init(void)
{
	//qjy: turn on the blacklight!
	GPB1_TO_OUT();
	GPB1_TO_1();
	
	rGPCUP  = 0x00000000;
	 rGPCCON = 0xaaaa02a9; 
	 
	 rGPDUP  = 0x00000000;
   rGPDCON=0xaaaaaaaa; //Initialize VD[15:8]
	 
	 rLCDCON1=(CLKVAL_TFT_240320<<8)|(MVAL_USED<<7)|(3<<5)|(12<<1)|0;
	 // TFT LCD panel,12bpp TFT,ENVID=off
	 rLCDCON2=(VBPD_240320<<24)|(LINEVAL_TFT_240320<<14)|(VFPD_240320<<6)|(VSPW_240320);
	 rLCDCON3=(HBPD_240320<<19)|(HOZVAL_TFT_240320<<8)|(HFPD_240320);
	 rLCDCON4=(MVAL<<8)|(HSPW_240320);
	 rLCDCON5 = (1<<11) | (1<<10) | (1<<9) | (1<<8) | (0<<7) | (0<<6)
		 | (1<<3)  |(BSWP<<1) | (HWSWP);
	 
	 rLCDSADDR1=(((U32)LCD_BUFFER_CAM>>22)<<21)|M5D((U32)LCD_BUFFER_CAM>>1);
	 rLCDSADDR2=M5D( ((U32)LCD_BUFFER_CAM+(SCR_XSIZE_TFT_240320*LCD_YSIZE_TFT_240320*2))>>1 );
	 rLCDSADDR3=(((SCR_XSIZE_TFT_240320-LCD_XSIZE_TFT_240320)/1)<<11)|(LCD_XSIZE_TFT_240320/1);
	 rLCDINTMSK|=(3); // MASK LCD Sub Interrupt
	 rTCONSEL &= (~7) ;     // Disable LPC3480
	 rTPAL=0; // Disable Temp Palette
	 LcdBkLtSet( 70 ) ;
	 Lcd_PowerEnable(0, 1);
	 Lcd_EnvidOnOff(1);		//turn on vedio
	 
}
//*****************************************************************************
static void Paint_Bmp(int x0,int y0,int h,int l,unsigned char bmp[])
{
	int x,y;
	U32 c;
	int p = 0;
	
	for( y = y0 ; y < l ; y++ )
	{
		for( x = x0 ; x < h ; x++ )
		{
    		c = bmp[p+1] | (bmp[p]<<8) ;
				
				if ( ( (x0+x) < SCR_XSIZE_TFT_240320) && ( (y0+y) < SCR_YSIZE_TFT_240320) )
					LCD_BUFFER_CAM[y0+y][x0+x] = c ;
				
				p = p + 2 ;
		}
	}
}

void Camera_Test(void)
{
	Uart_Printf("\nCamera Preview Test\n");
//	CamReset();
	SetCAMClockDivider(CAMCLK24000000);
	if( Test_OV9650() )
	{
		Uart_Printf("\nTest is failed!!!\n");
		return ;
	}
	Uart_Printf("Initializing end...\n");
	Test_CamPreview() ;
	Uart_Printf("\nCamera Preview Test End\n");
	rCLKCON &= ~(1<<19); // disable camclk
	Lcd_Tft_LTS350Q1_PE1_Init() ;
}


void CamPortSet(void)
{
	save_GPJCON = rGPJCON;
	save_GPJDAT = rGPJDAT;
	save_GPJUP = rGPJUP;
	
	rGPJCON = 0x2aaaaaa;
	rGPJDAT = 0;
	rGPJUP = 0;
	
	rGPGCON &= ~(3<<24);
	rGPGCON |= 1<<24;
	rGPGUP  |= 1<<12;
	rGPGDAT &= ~(1<<12);
}

void CamPortReturn(void)
{
	rGPJCON = save_GPJCON;
	rGPJDAT = save_GPJDAT;
	rGPJUP = save_GPJUP;
}

void CamPreviewIntUnmask(void)
{
	rINTSUBMSK &= ~(BIT_SUB_CAM_P);//INT CAMERA Port A ENABLE 
	rINTMSK &= ~(BIT_CAM);
}

void CamCodecIntUnmask(void)
{
	rINTSUBMSK &= ~(BIT_SUB_CAM_C);//INT CAMERA Port B ENABLE 
	rINTMSK &= ~(BIT_CAM);
}

void CamPreviewIntMask(void)
{
	rINTSUBMSK |= BIT_SUB_CAM_P;//INT CAMERA Port A ENABLE 
	rINTMSK |= (BIT_CAM);
}

void CamCodecIntMask(void)
{
	rINTSUBMSK |= BIT_SUB_CAM_C;//INT CAMERA Port B ENABLE 
	rINTMSK |= (BIT_CAM);
}


/******************************************************
*                                                                      							*    
*                       camera interface initialization                    		*
*                                                                            					*     
*******************************************************/

void CamReset(void)
{
	rCIGCTRL |= (1<<31); //camera I/F soft reset
	Delay(10);
	rCIGCTRL &= ~(1<<31);
}

// 0:48MHz, 1:24MHz, 2:16MHz, 3:12MHz...
// Camera clock = UPLL/[(CAMCLK_DIV+1)X2]
void SetCAMClockDivider(int divn) 
{
	rCAMDIVN = (rCAMDIVN & ~(0xf))|(1<<4)|(divn); // CAMCLK is divided..
}

/* Description of Parameters
CoDstWidth: Destination Width of Codec Path
CoDstHeight: Destination Height of Codec Path
PrDstWidth: Destination Width of Preview Path
PrDstHeight: Destination Height of Preview Path
WinHorOffset: Size of Window Offset for Horizontal Direction
WinVerOffset: Size of Window Offset for Vertical Direction
CoFrameBuffer: Start Address for Codec DMA
PrFrameBuffer: Start Address for Previe DMA
*/
void CamInit(U32 CoDstWidth, U32 CoDstHeight, U32 PrDstWidth, U32 PrDstHeight, 
						 U32 WinHorOffset, U32 WinVerOffset,  U32 CoFrameBuffer, U32 PrFrameBuffer)
{
	U32 WinOfsEn;
	U32 divisor, multiplier;
	U32 MainBurstSizeY, RemainedBurstSizeY, MainBurstSizeC, RemainedBurstSizeC, MainBurstSizeRGB, RemainedBurstSizeRGB;
	U32 H_Shift, V_Shift, PreHorRatio, PreVerRatio, MainHorRatio, MainVerRatio;
	U32 SrcWidth, SrcHeight;
	U32 ScaleUp_H_Co, ScaleUp_V_Co, ScaleUp_H_Pr, ScaleUp_V_Pr;
	
	//constant for calculating codec dma address
	if(CAM_CODEC_OUTPUT)
		divisor=2; //CCIR-422
	else
		divisor=4; //CCIR-420
		
	//constant for calculating preview dma address
	if(CAM_PVIEW_OUTPUT)
		multiplier=4;
	else
		multiplier=2;
	
	if(WinHorOffset==0 && WinVerOffset==0)
		WinOfsEn=0;
	else
		WinOfsEn=1;
	
	SrcWidth=CAM_SRC_HSIZE-WinHorOffset*2;
	SrcHeight=CAM_SRC_VSIZE-WinVerOffset*2;
	
	if(SrcWidth>=CoDstWidth) ScaleUp_H_Co=0; //down
	else ScaleUp_H_Co=1;		//up
	
	if(SrcHeight>=CoDstHeight) ScaleUp_V_Co=0;
	else ScaleUp_V_Co=1;		
	
	if(SrcWidth>=PrDstWidth) ScaleUp_H_Pr=0; //down
	else ScaleUp_H_Pr=1;		//up
	
	if(SrcHeight>=PrDstHeight) ScaleUp_V_Pr=0;   // edited 040225
	else ScaleUp_V_Pr=1;		
	
	////////////////// common control setting
	rCIGCTRL |= (1<<26)|(0<<27); // inverse PCLK, test pattern
	//--- hzh
	//rCIGCTRL |= (0<<26)|(0<<27); // don't inverse PCLK, test pattern
	//---
	rCIWDOFST = (1<<30)|(0xf<<12); // clear overflow 
	rCIWDOFST = 0;	
	rCIWDOFST=(WinOfsEn<<31)|(WinHorOffset<<16)|(WinVerOffset);
	rCISRCFMT=(CAM_ITU601<<31)|(0<<30)|(0<<29)|(CAM_SRC_HSIZE<<16)|(CAM_ORDER_YCBYCR<<14)|(CAM_SRC_VSIZE);
	//--- hzh
	//rCISRCFMT=(CAM_ITU601<<31)|(1<<30)|(0<<29)|(CAM_SRC_HSIZE<<16)|(CAM_ORDER_YCBYCR<<14)|(CAM_SRC_VSIZE);
	//---
	
	////////////////// codec port setting
	if (CAM_CODEC_4PP)
	{
		rCICOYSA1=CoFrameBuffer;
		rCICOYSA2=rCICOYSA1+CoDstWidth*CoDstHeight+2*CoDstWidth*CoDstHeight/divisor;
		rCICOYSA3=rCICOYSA2+CoDstWidth*CoDstHeight+2*CoDstWidth*CoDstHeight/divisor;
		rCICOYSA4=rCICOYSA3+CoDstWidth*CoDstHeight+2*CoDstWidth*CoDstHeight/divisor;
		
		rCICOCBSA1=rCICOYSA1+CoDstWidth*CoDstHeight;
		rCICOCBSA2=rCICOYSA2+CoDstWidth*CoDstHeight;
		rCICOCBSA3=rCICOYSA3+CoDstWidth*CoDstHeight;
		rCICOCBSA4=rCICOYSA4+CoDstWidth*CoDstHeight;
		
		rCICOCRSA1=rCICOCBSA1+CoDstWidth*CoDstHeight/divisor;
		rCICOCRSA2=rCICOCBSA2+CoDstWidth*CoDstHeight/divisor;
		rCICOCRSA3=rCICOCBSA3+CoDstWidth*CoDstHeight/divisor;
		rCICOCRSA4=rCICOCBSA4+CoDstWidth*CoDstHeight/divisor;
	}
	else
	{
		rCICOYSA1=CoFrameBuffer;
		rCICOYSA2=rCICOYSA1;
		rCICOYSA3=rCICOYSA1;
		rCICOYSA4=rCICOYSA1;
		
		rCICOCBSA1=rCICOYSA1+CoDstWidth*CoDstHeight;
		rCICOCBSA2=rCICOCBSA1;
		rCICOCBSA3=rCICOCBSA1;
		rCICOCBSA4=rCICOCBSA1;
		
		rCICOCRSA1=rCICOCBSA1+CoDstWidth*CoDstHeight/divisor;
		rCICOCRSA2=rCICOCRSA1;
		rCICOCRSA3=rCICOCRSA1;
		rCICOCRSA4=rCICOCRSA1;
	}
	rCICOTRGFMT=(CAM_CODEC_IN_422<<31)|(CAM_CODEC_OUTPUT<<30)|(CoDstWidth<<16)|(CAM_FLIP_NORMAL<<14)|(CoDstHeight);
	
	CalculateBurstSize(CoDstWidth, &MainBurstSizeY, &RemainedBurstSizeY);
	CalculateBurstSize(CoDstWidth/2, &MainBurstSizeC, &RemainedBurstSizeC);
	rCICOCTRL=(MainBurstSizeY<<19)|(RemainedBurstSizeY<<14)|(MainBurstSizeC<<9)|(RemainedBurstSizeC<<4);
	
	CalculatePrescalerRatioShift(SrcWidth, CoDstWidth, &PreHorRatio, &H_Shift);
	CalculatePrescalerRatioShift(SrcHeight, CoDstHeight, &PreVerRatio, &V_Shift);
	MainHorRatio=(SrcWidth<<8)/(CoDstWidth<<H_Shift);
	MainVerRatio=(SrcHeight<<8)/(CoDstHeight<<V_Shift);
	
	rCICOSCPRERATIO=((10-H_Shift-V_Shift)<<28)|(PreHorRatio<<16)|(PreVerRatio);
	rCICOSCPREDST=((SrcWidth/PreHorRatio)<<16)|(SrcHeight/PreVerRatio); 
	rCICOSCCTRL=(CAM_SCALER_BYPASS_OFF<<31)|(ScaleUp_H_Co<<30)|(ScaleUp_V_Co<<29)|(MainHorRatio<<16)|(MainVerRatio);
	
	rCICOTAREA=CoDstWidth*CoDstHeight;
	
	///////////////// preview port setting
	if (CAM_PVIEW_4PP) // codec view mode
	{
		rCIPRCLRSA1=PrFrameBuffer;
		rCIPRCLRSA2=rCIPRCLRSA1+PrDstWidth*PrDstHeight*multiplier;
		rCIPRCLRSA3=rCIPRCLRSA2+PrDstWidth*PrDstHeight*multiplier;
		rCIPRCLRSA4=rCIPRCLRSA3+PrDstWidth*PrDstHeight*multiplier;
	}	
	else // direct preview mode
	{
		rCIPRCLRSA1 = (U32)LCD_BUFFER_CAM;
		rCIPRCLRSA2 = (U32)LCD_BUFFER_CAM;
		rCIPRCLRSA3 = (U32)LCD_BUFFER_CAM;
		rCIPRCLRSA4 = (U32)LCD_BUFFER_CAM;
	}	
	
	rCIPRTRGFMT=(PrDstWidth<<16)|(CAM_FLIP_NORMAL<<14)|(PrDstHeight);
	
	if (CAM_PVIEW_OUTPUT==CAM_RGB24B)
		CalculateBurstSize(PrDstWidth*2, &MainBurstSizeRGB, &RemainedBurstSizeRGB);
	else // RGB16B
		CalculateBurstSize(PrDstWidth*2, &MainBurstSizeRGB, &RemainedBurstSizeRGB);
	rCIPRCTRL=(MainBurstSizeRGB<<19)|(RemainedBurstSizeRGB<<14);
	
	CalculatePrescalerRatioShift(SrcWidth, PrDstWidth, &PreHorRatio, &H_Shift);
	CalculatePrescalerRatioShift(SrcHeight, PrDstHeight, &PreVerRatio, &V_Shift);
	MainHorRatio=(SrcWidth<<8)/(PrDstWidth<<H_Shift);
	MainVerRatio=(SrcHeight<<8)/(PrDstHeight<<V_Shift);
	rCIPRSCPRERATIO=((10-H_Shift-V_Shift)<<28)|(PreHorRatio<<16)|(PreVerRatio);		 
	rCIPRSCPREDST=((SrcWidth/PreHorRatio)<<16)|(SrcHeight/PreVerRatio);
	rCIPRSCCTRL=(1<<31)|(CAM_PVIEW_OUTPUT<<30)|(ScaleUp_H_Pr<<29)|(ScaleUp_V_Pr<<28)|(MainHorRatio<<16)|(MainVerRatio);
	
	rCIPRTAREA= PrDstWidth*PrDstHeight;
}



/********************************************************
CalculateBurstSize - Calculate the busrt lengths

	Description:	
	- dstHSize: the number of the byte of H Size.
	
		*/
		void CalculateBurstSize(U32 hSize,U32 *mainBurstSize,U32 *remainedBurstSize)
		{
			U32 tmp;	
			tmp=(hSize/4)%16;
			switch(tmp) {
			case 0:
				*mainBurstSize=16;
				*remainedBurstSize=16;
				break;
			case 4:
				*mainBurstSize=16;
				*remainedBurstSize=4;
				break;
			case 8:
				*mainBurstSize=16;
				*remainedBurstSize=8;
				break;
			default: 

⌨️ 快捷键说明

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