项目:基于 ZYNQ 的 IMX2221 摄像头实时视频流采集传输
章节:传感器配置(二)
本章目的:使用 ZYNQ 芯片的 PS 端的 SPI 接口对 CMOS 图像传感器进行设置操作,保证 CMOS配置的需求,使其进入用户需要的工作模式。(代码编写)
首先几点说明:
- IMX222摄像头默认传输的图像为bayer格式
- IMX222摄像头通过SPI总线进行配置,由于摄像头的管脚在PL端,所以我们通过emio的spi总线进行连接,cmos_rst通过gpio的emio进行连接
首先给出spi初始化代码
#include "xspips.h"
#define SPI_DEV_ID XPAR_PS7_SPI_0_DEVICE_ID
//spi ptr
static XSpiPs SpiPs;
static XSpiPs_Config *SpiPsCfgPtr;
//SPI Initial interface
int initSPI(){
int status ;
SpiPsCfgPtr = XSpiPs_LookupConfig(SPI_DEV_ID);
status = XSpiPs_CfgInitialize(&SpiPs,SpiPsCfgPtr,SpiPsCfgPtr->BaseAddress);
if(status != XST_SUCCESS){
printf("XSpi cfginitialize failed \n\r");
return status;
}
status = XSpiPs_SetOptions(&SpiPs,XSPIPS_FORCE_SSELECT_OPTION|XSPIPS_MASTER_OPTION);
if(status != XST_SUCCESS){
printf("XSpi setOptions failed \n\r");
return status;
}
status = XSpiPs_SetClkPrescaler(&SpiPs,XSPIPS_CLK_PRESCALE_32);
if(status != XST_SUCCESS){
printf("XSpi setClk failed \n\r");
return status;
}
status = XSpiPs_SetSlaveSelect(&SpiPs,0);
if(status != XST_SUCCESS){
printf("XSpi set cs failed \n\r");
return status;
}
return XST_SUCCESS;
}
8bit数据高低位转换代码
u8 HighTransLow8bit(u8 data){
u8 tmp;
tmp = (data<<4)|(data>>4);
tmp = ((tmp<<2)&0xcc)|((tmp>>2)&0x33);
tmp=((tmp<<1)&0xaa)|((tmp>>1)&0x55);
return tmp;
}
IMX222初始化代码
#define REG_NUM 29
#define REG_BYTE 3
u8 imx222_reg[REG_NUM][REG_BYTE]={
{ 0x02, 0x00, 0x31 }, //standby
{ 0x02, 0x01,0x01}, // vs reverse
{ 0X02, 0x02, 0xF }, //1080p
{ 0X02, 0x03, 0x4c }, //2
{ 0X02, 0x04, 0x04 }, //3
{ 0X02, 0x05, 0x65 }, //4
{ 0X02, 0x06, 0x04 }, //5
{ 0X02, 0x11, 0x00 },//6
{ 0X02, 0x12, 0x82 }, //7
{ 0X02, 0x14, 0x00 }, //8
{ 0X02, 0x15, 0x00 }, //9
{ 0X02, 0x16, 0x3C }, //10
{ 0X02, 0x17, 0x00 }, //11
{ 0X02, 0x18, 0xC0 }, //12
{ 0X02, 0x19, 0x07 }, //13
{ 0X02, 0x1A, 0x51 }, //14
{ 0X02, 0x1B, 0x04 }, //15
{ 0X02, 0x1e, 0x20 }, //gain
{ 0X02, 0x20, 0xf0 }, //16
{ 0X02, 0x21, 0x00 }, //16
{ 0X02, 0x2c, 0x00 }, //16
{ 0X02, 0x2d, 0x42 }, //clk
{ 0X02, 0x3b, 0xe1 }, //18 sync code
{ 0X02, 0x9A, 0x26 }, //22
{ 0X02, 0x9B, 0x02 }, //23
{ 0X02, 0xCE, 0x16 }, //24
{ 0X02, 0xCF, 0x82 }, //25
{ 0X02, 0xD0, 0x00 }, //26
{ 0X02, 0x00, 0x00 }, //27
};
int initIMX222(XSpiPs *spipsptr){
int i;
int j;
int status;
for(i=0;i<REG_NUM;i++){
for(j=0;j<REG_BYTE;j++){
imx222_reg[i][j] = HighTransLow8bit(imx222_reg[i][j]);
}
}
for(i=0;i<REG_NUM;i++){
//XSpiPs_PolledTransfer spi continue trans
status = XSpiPs_PolledTransfer(spipsptr,imx222_reg[i],NULL,REG_BYTE);
if(status != XST_SUCCESS){
printf("XSpiPs_PolledTransfer failed \n\r");
return XST_FAILURE;
}
}
return XST_SUCCESS;
}