📄 camera_drive.c
字号:
#include"camera_drive.h"
//#include "system_common.h"
#define SDRAM_START 0
int i2c_write(unsigned char address,unsigned short data)
{
unsigned char device_add_w=0xb8;
unsigned char device_add_r=0xb9;
int i;
unsigned char ack;
unsigned char rdatah;
unsigned char rdatal;
rdatah=(data>>8)&0x00ff;
rdatal=data&0x00ff;
//address=0x09;
//while(1)
{
//start bit
SCLOCK_DOWN();
SDATA_UP();
SCLOCK_UP();
SCLOCK_UP();
SDATA_DOWN();
//device add write
for(i=7;i>=0;i--)
{
SCLOCK_DOWN();
if(((device_add_w>>i)<<1)&0x0002)
SDATA_UP();
else
SDATA_DOWN();
SCLOCK_DOWN();
SCLOCK_UP();
SCLOCK_UP();
}
SCLOCK_DOWN();
SCLOCK_DOWN();
//ack1
*pFIO_DIR &=0xfffD;//1=output
*pFIO_INEN |=0x0002;
SCLOCK_UP();
SCLOCK_UP();
ack=SDATA_R;
SCLOCK_DOWN();
*pFIO_INEN &=0xfffD;
*pFIO_DIR |=0x0002;//1=output
// SDATA_UP();
// SCLOCK_DOWN();
//reg address
for(i=7;i>=0;i--)
{
SCLOCK_DOWN();
if(((address>>i)<<1)&0x0002)
SDATA_UP();
else
SDATA_DOWN();
SCLOCK_DOWN();
SCLOCK_UP();
SCLOCK_UP();
}
SCLOCK_DOWN();
SCLOCK_DOWN();
//ack2
*pFIO_DIR &=0xfffD;//1=output
*pFIO_INEN |=0x0002;
SCLOCK_UP();
SCLOCK_UP();
ack=SDATA_R;
SCLOCK_DOWN();
*pFIO_INEN &=0xfffD;
*pFIO_DIR |=0x0002;//1=output
SCLOCK_DOWN();
//data H
for(i=7;i>=0;i--)
{
SCLOCK_DOWN();
if(((rdatah>>i)<<1)&0x0002)
SDATA_UP();
else
SDATA_DOWN();
SCLOCK_DOWN();
SCLOCK_UP();
SCLOCK_UP();
}
SCLOCK_DOWN();
SCLOCK_DOWN();
//ack2
*pFIO_DIR &=0xfffD;//1=output
*pFIO_INEN |=0x0002;
SCLOCK_UP();
SCLOCK_UP();
ack=SDATA_R;
SCLOCK_DOWN();
*pFIO_INEN &=0xfffD;
*pFIO_DIR |=0x0002;//1=output
SCLOCK_DOWN();
//data L
for(i=7;i>=0;i--)
{
SCLOCK_DOWN();
if(((rdatal>>i)<<1)&0x0002)
SDATA_UP();
else
SDATA_DOWN();
SCLOCK_DOWN();
SCLOCK_UP();
SCLOCK_UP();
}
SCLOCK_DOWN();
SCLOCK_DOWN();
//ack2
*pFIO_DIR &=0xfffD;//1=output
*pFIO_INEN |=0x0002;
SCLOCK_UP();
SCLOCK_UP();
ack=SDATA_R;
SCLOCK_DOWN();
*pFIO_INEN &=0xfffD;
*pFIO_DIR |=0x0002;//1=output
SCLOCK_DOWN();
//stop bit
SCLOCK_DOWN();
SDATA_DOWN();
SCLOCK_UP();
SDATA_UP();
SDATA_UP();
SCLOCK_DOWN();
///////////////////////////////////////////////////////
//SDATA_UP();
//SCLOCK_UP();
for(i=0;i<0x80;i++);
*pFIO_INEN &=0xfffD;
*pFIO_DIR |=0x0002;//1=output
}
//i2c_read();
}
unsigned short i2c_read(unsigned char address)
{
unsigned char device_add_w=0xb8;
unsigned char device_add_r=0xb9;
int i;
unsigned char ack;
unsigned char rdatah;
unsigned char rdatal;
unsigned char temp;
unsigned short data;
//address=0x09;
//while(1)
{
//start bit
SCLOCK_DOWN();
SDATA_UP();
SCLOCK_UP();
SCLOCK_UP();
SDATA_DOWN();
//device add write
for(i=7;i>=0;i--)
{
SCLOCK_DOWN();
if(((device_add_w>>i)<<1)&0x0002)
SDATA_UP();
else
SDATA_DOWN();
SCLOCK_DOWN();
SCLOCK_UP();
SCLOCK_UP();
}
SCLOCK_DOWN();
SCLOCK_DOWN();
//ack1
*pFIO_DIR &=0xfffD;//1=output
*pFIO_INEN |=0x0002;
SCLOCK_UP();
SCLOCK_UP();
ack=SDATA_R;
SCLOCK_DOWN();
*pFIO_INEN &=0xfffD;
*pFIO_DIR |=0x0002;//1=output
// SDATA_UP();
// SCLOCK_DOWN();
//reg address
for(i=7;i>=0;i--)
{
SCLOCK_DOWN();
if(((address>>i)<<1)&0x0002)
SDATA_UP();
else
SDATA_DOWN();
SCLOCK_DOWN();
SCLOCK_UP();
SCLOCK_UP();
}
SCLOCK_DOWN();
SCLOCK_DOWN();
//ack2
*pFIO_DIR &=0xfffD;//1=output
*pFIO_INEN |=0x0002;
SCLOCK_UP();
SCLOCK_UP();
ack=SDATA_R;
SCLOCK_DOWN();
*pFIO_INEN &=0xfffD;
*pFIO_DIR |=0x0002;//1=output
SCLOCK_DOWN();
//start bit
SCLOCK_DOWN();
SDATA_UP();
SCLOCK_UP();
SCLOCK_UP();
SDATA_DOWN();
SCLOCK_DOWN();
//device add read
for(i=7;i>=0;i--)
{
SCLOCK_DOWN();
if(((device_add_r>>i)<<1)&0x0002)
SDATA_UP();
else
SDATA_DOWN();
SCLOCK_DOWN();
SCLOCK_UP();
SCLOCK_UP();
}
SCLOCK_DOWN();
SCLOCK_DOWN();
//ack3
*pFIO_DIR &=0xfffD;//1=output
*pFIO_INEN |=0x0002;
SCLOCK_UP();
ack=SDATA_R;
//*pFIO_INEN &=0xfffD;
//*pFIO_DIR |=0x0002;//1=output
SCLOCK_DOWN();
SCLOCK_DOWN();
SCLOCK_DOWN();
SCLOCK_DOWN();
SCLOCK_DOWN();
SCLOCK_DOWN();
//read H
//*pFIO_DIR &=0xfffD;//1=output
//*pFIO_INEN |=0x0002;
rdatah=0;
for(i=7;i>=0;i--)
{
SCLOCK_DOWN();
SCLOCK_DOWN();
SCLOCK_DOWN();
SCLOCK_DOWN();
SCLOCK_UP();
temp=SDATA_R;
rdatah|=((temp>>1)&0x01)<<i;
SCLOCK_UP();
}
SCLOCK_DOWN();
SCLOCK_DOWN();
*pFIO_INEN &=0xfffD;
*pFIO_DIR |=0x0002;//1=output
//ack
SDATA_DOWN();
SCLOCK_UP();
SDATA_DOWN();
SCLOCK_UP();
SCLOCK_DOWN();
//read L
*pFIO_DIR &=0xfffD;//1=output
*pFIO_INEN |=0x0002;
rdatal=0;
for(i=7;i>=0;i--)
{
SCLOCK_DOWN();
SCLOCK_DOWN();
SCLOCK_DOWN();
SCLOCK_DOWN();
SCLOCK_UP();
temp=SDATA_R;
rdatal|=((temp>>1)&0x01)<<i;
SCLOCK_UP();
}
SCLOCK_DOWN();
SCLOCK_DOWN();
*pFIO_INEN &=0xfffD;
*pFIO_DIR |=0x0002;//1=output
//nack
SDATA_UP();
SCLOCK_UP();
SDATA_UP();
SCLOCK_UP();
SCLOCK_DOWN();
//stop bit
SCLOCK_DOWN();
SDATA_DOWN();
SCLOCK_UP();
SDATA_UP();
SDATA_UP();
SCLOCK_DOWN();
///////////////////////////////////////////////////////
//SDATA_UP();
//SCLOCK_UP();
for(i=0;i<0x200;i++);
*pFIO_INEN &=0xfffD;
*pFIO_DIR |=0x0002;//1=output
}
data=rdatah;
data=(data<<8)&0xff00;
data|=rdatal;
return data;
}
/*
unsigned char *INPUTFRAMEBUFA_C=(unsigned char *)(SDRAM_START);
unsigned char *TEST_BUFFER_C=(unsigned char *)(SDRAM_START+0x1000000);
EX_INTERRUPT_HANDLER(DMA0_PPI_ISR) // <--|declaration in exception.h -->
{ // |declaration with _pragma(interrupt) the ISR Startaddress
int i=0;
//disable the interrupt request
*pDMA0_IRQ_STATUS = 0x1;//只采一阵
rgb_yuv420(INPUTFRAMEBUFA_C,TEST_BUFFER_C);
i=1;
//while(1);
}//end DMA0_PPI_ISRu
void rgb_yuv420(unsigned char *in_buffer_start,unsigned char *out_buffer_start)
{
int height=480;
int width=512;
int i,j,k;
unsigned char *y_start;
unsigned char *u_start;
unsigned char *v_start;
unsigned char b,g1,g2,r,y,u,v;
y_start=out_buffer_start;
u_start=out_buffer_start+height*width;
v_start=u_start+height*width/4;
for(j=0;j<height/2;j++)
{
for(i=0;i<width/2;i++)
{
b=*(in_buffer_start+j*2*width*2+i*2);
g1=*(in_buffer_start+j*2*width*2+i*2+1);
g2=*(in_buffer_start+(j*2+1)*width*2+i*2);
r=*(in_buffer_start+(j*2+1)*width*2+i*2+1);
// y=0.299*r + 0.2935*(g1+g2) + 0.114*b;
y=(unsigned char)((306*(int)r + 301*(g1+g2) + 117*b)>>10);
//u=0.564*(b-y);
u=(unsigned char)((578*(b-y))>>10);
v=(unsigned char)((578*(r-y))>>10);
*(y_start+j*2*width*2+i*2)=y;
*(y_start+j*2*width*2+i*2+1)=y;
*(y_start+(j*2+1)*width*2+i*2)=y;
*(y_start+(j*2+1)*width*2+i*2+1)=y;
*(u_start+j*width*2+i)=u;
*(v_start+j*width*2+i)=v;
}
}
}
void rgb_yuv422(unsigned char *in_buffer_start,unsigned char *out_buffer_start)
{
int height=480;
int width=512;
int i,j,k;
unsigned char *y_start;
unsigned char *u_start;
unsigned char *v_start;
unsigned char b,g1,g2,r,y,u,v;
y_start=out_buffer_start;
for(j=0;j<height/2;j++)
{
for(i=0;i<width/2;i++)
{
b=*(in_buffer_start+j*2*width*2+i*2);
g1=*(in_buffer_start+j*2*width*2+i*2+1);
g2=*(in_buffer_start+(j*2+1)*width*2+i*2);
r=*(in_buffer_start+(j*2+1)*width*2+i*2+1);
// y=0.299*r + 0.2935*(g1+g2) + 0.114*b;
y=(unsigned char)((306*(int)r + 301*(g1+g2) + 117*b)>>10);
//u=0.564*(b-y);
u=(unsigned char)((578*(b-y))>>10);
v=(unsigned char)((578*(r-y))>>10);
*(y_start+j*2*width*4+i*4+1)=y;
*(y_start+j*2*width*4+i*4+3)=y;
*(y_start+(j*2+1)*width*4+i*4+1)=y;
*(y_start+(j*2+1)*width*4+i*4+3)=y;
*(y_start+j*2*width*4+i*4)=u;
*(y_start+(j*2+1)*width*4+i*4)=u;
*(y_start+j*2*width*4+i*4+2)=v;
*(y_start+(j*2+1)*width*4+i*4+2)=v;
}
}
}
void camera_test()
{
unsigned short test1;
unsigned short test2;
int i;
unsigned short status;
test1=0x0249;
//isr init
*pSIC_IAR0 = *pSIC_IAR0 & 0xffffffff | 0x00000000;
*pSIC_IAR1 = *pSIC_IAR1 & 0xffffffff | 0x00000001; // map DMA0 PPI Interrupt -> IVG8
*pSIC_IAR2 = *pSIC_IAR2 & 0xffffffff | 0x00000000;
register_handler(ik_ivg8, DMA0_PPI_ISR); // assign DMA0 PPI ISR to interrupt vector 8
*pSIC_IMASK=0x00000100; // all interrupts 0=disabled but DMA0 PPI interrupt enabled
//dma_init;
//Target address of the DMA
*pDMA0_START_ADDR = 0x0;
//Line_Length 16bit transfers will be executed
*pDMA0_X_COUNT = IMG_WIDTH;
//The modifier is set to 2 because of the 16bit transfers
*pDMA0_X_MODIFY = 0x1;
//Frame_Length 16bit transfers will be executed
*pDMA0_Y_COUNT = IMG_HEIGHT;
//The modifier is set to 2 because of the 16bit transfers
*pDMA0_Y_MODIFY = 0x1;
//PPI Peripheral is used
*pDMA0_PERIPHERAL_MAP = 0x0;
//DMA Config: Enable DMA | Memory write DMA | 2-D DMA | Discard DMA FIFO before start | enable assertation of interrupt | NDSIZE for stop mode | Enable STOP DMA
*pDMA0_CONFIG = DMAEN | DI_EN | WNR | WDSIZE_8| DMA2D | RESTART | DI_EN;
//i2c_write(0x09,test1);
i2c_write(0x0c,0x0001);
//i2c_write(0x7f,0x26aa);
//i2c_write(0x7f,0x3000);
//i2c_write(0x7f,0x2800);
i2c_write(0x7f,0x0000);
//i2c_write(0x7f,0x3800);
i2c_write(0x72,0x0000);
i2c_write(0x05,94);
i2c_write(0x06,45);
i2c_write(0x03,480);
i2c_write(0x04,720);
test2=i2c_read(0x00);
//ppi init;
*pPPI_DELAY=0;
*pPPI_COUNT=IMG_WIDTH-1;
*pPPI_FRAME=IMG_HEIGHT;
*pPPI_CONTROL=0x402d;//0x402d;//0x404d;//0x00ad;
while(1)
{
status=*pPPI_STATUS;
if(status)
while(1);
}
}
*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -