📄 camif.c
字号:
/************************************************************
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 (c) 2004 SAMSUNG Electronics.
# However, Anybody can use this code without our permission.
*************************************************************/
#include <ctype.h>
#include "def.h"
#include "2440addr.h"
#include "2440lib.h"
#include "camif.h"
#include "lcdlib.h"
#include "glib.h"
#include "camproset.h" // for camera setting
//#include ".\bmp\pqvga16bsm5.h"
#include ".\bmp\422jpeg.h"
#include ".\bmp\420jpeg.h"
#include ".\bmp\foreman_cif_420.H"
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;
void * func_camera_test[][2]=
{
(void *)Test_CamPreview, "Preview Test ",
(void *)Test_CamCodec, "Codec Test ",
(void *)Camera_Iic_Test, "IIC interface test ",
(void *)Test_YCbCr_to_RGB, "YCbCr2RGB test",
0,0
};
void Camera_Test(void)
{
int i;
Uart_Printf("\n====== Camera Interface Test Start ======\n");
CamReset();
// Initializing camif
rCLKCON |= (1<<19); // enable camclk
CamPortSet();
ChangeUPllValue(60, 4, 1); // UPLL clock = 96MHz, PLL input 16.9344MHz
rCLKDIVN|=(1<<3); // UCLK 48MHz setting for UPLL 96MHz
// 0:48MHz, 1:24MHz, 2:16MHz, 3:12MHz...
// Camera clock = UPLL/[(CAMCLK_DIV+1)X2]
Uart_Printf("1...\n");
switch(USED_CAM_TYPE)
{
case CAM_AU70H :
if (AU70H_VIDEO_SIZE==1152)
SetCAMClockDivider(CAMCLK24000000); //Set Camera Clock for SXGA
if (AU70H_VIDEO_SIZE==640)
SetCAMClockDivider(CAMCLK16000000); //Set Camera Clock for VGA
break;
case CAM_S5X3A1 :
SetCAMClockDivider(CAMCLK24000000); //Set Camera Clock for SXGA
break;
default : // 24MHz
SetCAMClockDivider(CAMCLK24000000); //Set Camera Clock 24MHz s5x532, ov7620
break;
}
Uart_Printf("2...\n");
// Initializing camera module
CamModuleReset(); // s5x532 must do this..
Delay(500); // ready time of s5x433, s5x532 IIC interface. needed...
CameraModuleSetting();
Uart_Printf("Initializing end...\n");
while(1)
{
i=0;
Uart_Printf("\n\n");
while(1)
{ //display menu
Uart_Printf("%2d:%s\n",i,func_camera_test[i][1]);
i++;
if((int)(func_camera_test[i][0])==0)
{
Uart_Printf("\n");
break;
}
if((i%4)==0)
Uart_Printf("\n");
}
Uart_Printf("\nPress only Enter key to exit : ");
i = Uart_GetIntNum();
if(i==-1) break; // return.
if(i>=0 && (i<((sizeof(func_camera_test)-1)/8)) ) // select and execute...
( (void (*)(void)) (func_camera_test[i][0]) )();
}
Uart_Printf("\n====== Camera IF Test program end ======\n");
// CamModuleReset(); // s5x532 must do this..
rCLKCON &= ~(1<<19); // disable camclk
}
void CamPortSet(void)
{
save_GPJCON = rGPJCON;
save_GPJDAT = rGPJDAT;
save_GPJUP = rGPJUP;
rGPJCON = 0x2aaaaaa;
rGPJDAT = 0;
rGPJUP = 0;
}
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);
}
void CamModuleReset(void)
{
switch(USED_CAM_TYPE)
{
case CAM_OV7620 : // reset - active high
case CAM_S5X532 : // reset - active low, but H/W inverted.. so, in this case active high
case CAM_S5X433 : // reset - active low, but H/W inverted.. so, in this case active high
case CAM_S5X3A1 : // reset - active low, but H/W inverted.. so, in this case active high
rCIGCTRL |= (1<<30); //external camera reset high
Delay(30);
rCIGCTRL &= ~(1<<30); //external camera reset low
break;
case CAM_AU70H : // reset - active low
default :
rCIGCTRL &= ~(1<<30); //external camera reset low
Delay(10);
rCIGCTRL |= (1<<30); //external camera reset high
break;
}
}
// 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
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);
////////////////// 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=LCDFRAMEBUFFER;
rCIPRCLRSA2=LCDFRAMEBUFFER;
rCIPRCLRSA3=LCDFRAMEBUFFER;
rCIPRCLRSA4=LCDFRAMEBUFFER;
}
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:
tmp=(hSize/4)%8;
switch(tmp) {
case 0:
*mainBurstSize=8;
*remainedBurstSize=8;
break;
case 4:
*mainBurstSize=8;
*remainedBurstSize=4;
default:
*mainBurstSize=4;
tmp=(hSize/4)%4;
*remainedBurstSize= (tmp) ? tmp: 4;
break;
}
break;
}
}
/********************************************************
CalculatePrescalerRatioShift - none
Description:
- none
*/
void CalculatePrescalerRatioShift(U32 SrcSize, U32 DstSize, U32 *ratio,U32 *shift)
{
if(SrcSize>=64*DstSize) {
Uart_Printf("ERROR: out of the prescaler range: SrcSize/DstSize = %d(< 64)\n",SrcSize/DstSize);
while(1);
}
else if(SrcSize>=32*DstSize) {
*ratio=32;
*shift=5;
}
else if(SrcSize>=16*DstSize) {
*ratio=16;
*shift=4;
}
else if(SrcSize>=8*DstSize) {
*ratio=8;
*shift=3;
}
else if(SrcSize>=4*DstSize) {
*ratio=4;
*shift=2;
}
else if(SrcSize>=2*DstSize) {
*ratio=2;
*shift=1;
}
else {
*ratio=1;
*shift=0;
}
}
/********************************************************
CamCaptureStart - Start camera capture operation.
Description:
- mode= CAM_CODEC_CAPTURE_ENABLE_BIT or CAM_PVIEW_CAPTURE_ENABLE_BIT or both
*/
void CamCaptureStart(U32 mode)
{
if(mode&CAM_CODEC_SCALER_CAPTURE_ENABLE_BIT) {
camCodecStatus=CAM_STARTED;
rCICOSCCTRL|=CAM_CODEC_SACLER_START_BIT;
}
if(mode&CAM_PVIEW_SCALER_CAPTURE_ENABLE_BIT) {
camPviewStatus=CAM_STARTED;
rCIPRSCCTRL|=CAM_PVIEW_SACLER_START_BIT;
}
if(mode&CAM_CAMIF_GLOBAL_CAPTURE_ENABLE_BIT) {
camCodecStatus=CAM_STARTED;
rCICOSCCTRL|=CAM_CAMIF_GLOBAL_CAPTURE_ENABLE_BIT;
}
rCIIMGCPT|=CAM_CAMIF_GLOBAL_CAPTURE_ENABLE_BIT|mode;
}
void CamCaptureStop(void)
{
camCodecStatus=CAM_STOP_ISSUED;
camPviewStatus=CAM_STOP_ISSUED;
}
void _CamCodecStopHw(void)
{
rCICOSCCTRL &= ~CAM_CODEC_SACLER_START_BIT; //stop codec scaler.
rCIIMGCPT &= ~CAM_CODEC_SCALER_CAPTURE_ENABLE_BIT; //stop capturing for codec scaler.
if(!(rCIIMGCPT & CAM_PVIEW_SCALER_CAPTURE_ENABLE_BIT))
rCIIMGCPT &= ~CAM_CAMIF_GLOBAL_CAPTURE_ENABLE_BIT; //stop capturing for preview scaler if needed.
rCICOCTRL |= (1<<2); //Enable last IRQ at the end of frame capture.
//NOTE:LastIrqEn bit should be set after clearing CAPTURE_ENABLE_BIT & SCALER_START_BIT
}
void _CamPviewStopHw(void)
{
rCIPRSCCTRL &= ~CAM_PVIEW_SACLER_START_BIT; //stop preview scaler.
rCIIMGCPT &= ~CAM_PVIEW_SCALER_CAPTURE_ENABLE_BIT; //stop capturing for preview scaler.
if(!(rCIIMGCPT&CAM_CODEC_SCALER_CAPTURE_ENABLE_BIT))
rCIIMGCPT &= ~CAM_CAMIF_GLOBAL_CAPTURE_ENABLE_BIT; //stop capturing for codec scaler if needed.
rCIPRCTRL |= (1<<2); //Enable last IRQ at the end of frame capture.
//NOTE:LastIrqEn bit should be set after clearing CAPTURE_ENABLE_BIT & SCALER_START_BIT
}
void __irq CamIsr(void)
{
U32 completedFrameIndex;
if (rSUBSRCPND&BIT_SUB_CAM_C)
{
Uart_Printf("[C]");
CamCodecIntMask();
rSUBSRCPND |= BIT_SUB_CAM_C;
ClearPending(BIT_CAM);
switch(camCodecStatus) {
case CAM_STOP_ISSUED:
_CamCodecStopHw();
camCodecStatus=CAM_LAST_CAPTURING;
Uart_Printf("cr=%x\n", rCICOCTRL);
//Uart_Printf("cS1\n");
break;
case CAM_LAST_CAPTURING:
camCodecStatus=CAM_STOPPED;
CamCodecIntMask();
//Uart_Printf("cS2\n");
return;
case CAM_STARTED:
flagCaptured_C = 1;
// _CamCodecStopHw();
if(camTestMode&CAM_TEST_MODE_CODEC) {
if(camCodecCaptureCount>0)
completedFrameIndex=(((rCICOSTATUS>>26)&0x3)+4-2)%4;
//Uart_Printf("FrameIndex:%d\n",completedFrameIndex);
}
else {
//Uart_Printf("Just Capturing without display");
}
break;
case CAM_CODEC_SCALER_BYPASS_STATE:
//Uart_Printf("cBP\n");
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -