ZYNQ linux环境下PS I2C配置OV5640

平台:ubuntu虚拟机 ZYNQ7035

1.vivado编辑BD文件,设置两个IIC接口

ZYNQ linux环境下PS I2C配置OV5640_第1张图片

2.设备树搭建,应用petalinux调用hdf直接生成

	在Ubuntu虚拟机内搭建工程
source /opt/pkg/petalinux/2018.3/settings.sh

petalinux-create -t project --template zynq -n cam2hdmi   //创建文件夹
//将生成的hdf文件放到文件夹下
cd cam2hdmi //进入到 petalinux 工程目录下
petalinux-config --get-hw-description 
//--进入系统配置
petalinux-config -c kernel	//配置内核
petalinux-config -c rootfs   //配置根文件  配置密码
petalinux-build   //编译工程
//没有外部设备以上基本不用更改
petalinux-package --boot --fsbl --fpga --u-boot --force    //生成boot.bin文件

将boot.bin image.ub文件拷贝到SD卡,启动文件制作完成。
系统启动可以通过串口看到:
ZYNQ linux环境下PS I2C配置OV5640_第2张图片

3.在线调试

因为配置时调用PS端以太网,选用linux TCF agent在线调试方法。
直接在SDK内编译函数实现功能。
主函数:I2C0_FILE_NAME 具体位置,例如我的:/dev/i2c-0

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 

#include "VDMA.h"
#include "i2c.h"
#include"camera_reg.h"


int main(){
	iic_init(I2C0_FILE_NAME);
	iic_init(I2C1_FILE_NAME);
}

linux下操作IIC写入函数:

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include "types.h"
#include "i2c.h"
#include "camera_reg.h"

int iic_init(char* iic_path){
	Xuint8 fd;
///reset cam1 config
	fd = iic_open(iic_path);
	//fd = iic_open(I2C0_FILE_NAME);
	if (fd<0 ){

		printf("open faild");
	}
    IicWrite(fd,0x3017, 0x00);
	ov5640_init_rgb(fd);
	iic_close(fd);
	return 0;
}

int iic_open(char* iic_path)
{
	int file = 0;
	// Open a connection to the I2C userspace control file.
	if ((file = open(iic_path, O_RDWR)) < 0) {
		perror("Unable to open i2c control file");
		exit(-1);
	}
	return file;
}

void iic_close(int file)
{
	close(file);
}

int IicWrite(Xuint8 file, Xuint16 addr,Xuint8 value)
{
    unsigned char outbuf[3];
    struct i2c_rdwr_ioctl_data packets;
    struct i2c_msg messages[1];

    messages[0].addr	= CAM_OV5640; //器件地址
    messages[0].flags	= 0; //0 write 1 read
    messages[0].len		= sizeof(outbuf);
    messages[0].buf		= outbuf;

    outbuf[0] = addr >>8;   //因为该器件寄存器地址为16位
    outbuf[1] = addr;
    outbuf[2] = value;

    /* Transfer the i2c packets to the kernel and verify it worked */
    packets.msgs  = messages;
    packets.nmsgs = 1;
    if(ioctl(file, I2C_RDWR, &packets) < 0) {
        perror("Unable to send data");
        return -1;
    }
    usleep(2000);
    return 0;
}

摄像头配置参数:参考米联客代码


void ov5640_init_rgb(Xuint8 file)
{
	IicWrite(file,0x3103, 0x11); // system clock from pad, bit[1]
	IicWrite(file,0x3008, 0x82); // software reset, bit[7]
	// delay 5ms
	IicWrite(file,0x3008, 0x42); // software power down, bit[6]
	IicWrite(file,0x3103, 0x03); // system clock from PLL, bit[1]
	IicWrite(file,0x3017, 0xff); // FREX, Vsync, HREF, PCLK, D[9:6] output enable
	IicWrite(file,0x3018, 0xff); // D[5:0], GPIO[1:0] output enable
	IicWrite(file,0x3034, 0x1a); // MIPI 10-bit
	IicWrite(file,0x3037, 0x13); // PLL root divider, bit[4], PLL pre-divider, bit[3:0]
	IicWrite(file,0x3108, 0x01); // PCLK root divider, bit[5:4], SCLK2x root divider, bit[3:2]
	// SCLK root divider, bit[1:0]
	IicWrite(file,0x3630, 0x36);
	IicWrite(file,0x3631, 0x0e);
	IicWrite(file,0x3632, 0xe2);
	IicWrite(file,0x3633, 0x12);
	IicWrite(file,0x3621, 0xe0);
	IicWrite(file,0x3704, 0xa0);
	IicWrite(file,0x3703, 0x5a);
	IicWrite(file,0x3715, 0x78);
	IicWrite(file,0x3717, 0x01);
	IicWrite(file,0x370b, 0x60);
	IicWrite(file,0x3705, 0x1a);
	IicWrite(file,0x3905, 0x02);
	IicWrite(file,0x3906, 0x10);
	IicWrite(file,0x3901, 0x0a);
	IicWrite(file,0x3731, 0x12);
	IicWrite(file,0x3600, 0x08); // VCM control
	IicWrite(file,0x3601, 0x33); // VCM control
	IicWrite(file,0x302d, 0x60); // system control
	IicWrite(file,0x3620, 0x52);
	IicWrite(file,0x371b, 0x20);
	IicWrite(file,0x471c, 0x50);
	IicWrite(file,0x3a13, 0x43); // pre-gain = 1.047x
	IicWrite(file,0x3a18, 0x00); // gain ceiling
	IicWrite(file,0x3a19, 0xf8); // gain ceiling = 15.5x
	IicWrite(file,0x3635, 0x13);
	IicWrite(file,0x3636, 0x03);
	IicWrite(file,0x3634, 0x40);
	IicWrite(file,0x3622, 0x01);
	// 50/60Hz detection 50/60Hz 灯光条纹过滤
	IicWrite(file,0x3c01, 0x34); // Band auto, bit[7]
	IicWrite(file,0x3c04, 0x28); // threshold low sum
	IicWrite(file,0x3c05, 0x98); // threshold high sum
	IicWrite(file,0x3c06, 0x00); // light meter 1 threshold[15:8]
	IicWrite(file,0x3c07, 0x08); // light meter 1 threshold[7:0]
	IicWrite(file,0x3c08, 0x00); // light meter 2 threshold[15:8]
	IicWrite(file,0x3c09, 0x1c); // light meter 2 threshold[7:0]
	IicWrite(file,0x3c0a, 0x9c); // sample number[15:8]
	IicWrite(file,0x3c0b, 0x40); // sample number[7:0]
	IicWrite(file,0x3810, 0x00); // Timing Hoffset[11:8]
	IicWrite(file,0x3811, 0x10); // Timing Hoffset[7:0]
	IicWrite(file,0x3812, 0x00); // Timing Voffset[10:8]
	IicWrite(file,0x3708, 0x64);
	IicWrite(file,0x4001, 0x02); // BLC start from line 2
	IicWrite(file,0x4005, 0x1a); // BLC always update
	IicWrite(file,0x3000, 0x00); // enable blocks
	IicWrite(file,0x3004, 0xff); // enable clocks
	IicWrite(file,0x300e, 0x58); // MIPI power down, DVP enable
	IicWrite(file,0x302e, 0x00);
	IicWrite(file,0x4300, 0x61); // YUV 422, YUYV
	IicWrite(file,0x501f, 0x01); // YUV 422
	IicWrite(file,0x440e, 0x00);
	IicWrite(file,0x5000, 0xa7); // Lenc on, raw gamma on, BPC on, WPC on, CIP on
	// AEC target 自动曝光控制
	IicWrite(file,0x3a0f, 0x30); // stable range in high
	IicWrite(file,0x3a10, 0x28); // stable range in low
	IicWrite(file,0x3a1b, 0x30); // stable range out high
	IicWrite(file,0x3a1e, 0x26); // stable range out low
	IicWrite(file,0x3a11, 0x60); // fast zone high
	IicWrite(file,0x3a1f, 0x14); // fast zone low
	// Lens correction for ? 镜头补偿
	IicWrite(file,0x5800, 0x23);
	IicWrite(file,0x5801, 0x14);
	IicWrite(file,0x5802, 0x0f);
	IicWrite(file,0x5803, 0x0f);
	IicWrite(file,0x5804, 0x12);
	IicWrite(file,0x5805, 0x26);
	IicWrite(file,0x5806, 0x0c);
	IicWrite(file,0x5807, 0x08);
	IicWrite(file,0x5808, 0x05);
	IicWrite(file,0x5809, 0x05);
	IicWrite(file,0x580a, 0x08);
	IicWrite(file,0x580b, 0x0d);
	IicWrite(file,0x580c, 0x08);
	IicWrite(file,0x580d, 0x03);
	IicWrite(file,0x580e, 0x00);
	IicWrite(file,0x580f, 0x00);
	IicWrite(file,0x5810, 0x03);
	IicWrite(file,0x5811, 0x09);
	IicWrite(file,0x5812, 0x07);
	IicWrite(file,0x5813, 0x03);
	IicWrite(file,0x5814, 0x00);
	IicWrite(file,0x5815, 0x01);
	IicWrite(file,0x5816, 0x03);
	IicWrite(file,0x5817, 0x08);
	IicWrite(file,0x5818, 0x0d);
	IicWrite(file,0x5819, 0x08);
	IicWrite(file,0x581a, 0x05);
	IicWrite(file,0x581b, 0x06);
	IicWrite(file,0x581c, 0x08);
	IicWrite(file,0x581d, 0x0e);
	IicWrite(file,0x581e, 0x29);
	IicWrite(file,0x581f, 0x17);
	IicWrite(file,0x5820, 0x11);
	IicWrite(file,0x5821, 0x11);
	IicWrite(file,0x5822, 0x15);
	IicWrite(file,0x5823, 0x28);
	IicWrite(file,0x5824, 0x46);
	IicWrite(file,0x5825, 0x26);
	IicWrite(file,0x5826, 0x08);
	IicWrite(file,0x5827, 0x26);
	IicWrite(file,0x5828, 0x64);
	IicWrite(file,0x5829, 0x26);
	IicWrite(file,0x582a, 0x24);
	IicWrite(file,0x582b, 0x22);
	IicWrite(file,0x582c, 0x24);
	IicWrite(file,0x582d, 0x24);
	IicWrite(file,0x582e, 0x06);
	IicWrite(file,0x582f, 0x22);
	IicWrite(file,0x5830, 0x40);
	IicWrite(file,0x5831, 0x42);
	IicWrite(file,0x5832, 0x24);
	IicWrite(file,0x5833, 0x26);
	IicWrite(file,0x5834, 0x24);
	IicWrite(file,0x5835, 0x22);
	IicWrite(file,0x5836, 0x22);
	IicWrite(file,0x5837, 0x26);
	IicWrite(file,0x5838, 0x44);
	IicWrite(file,0x5839, 0x24);
	IicWrite(file,0x583a, 0x26);
	IicWrite(file,0x583b, 0x28);
	IicWrite(file,0x583c, 0x42);
	IicWrite(file,0x583d, 0xce); // lenc BR offset
	// AWB 自动白平衡
	/*IicWrite(file,0x5180, 0xff); // AWB B block
	IicWrite(file,0x5181, 0xf2); // AWB control
	IicWrite(file,0x5182, 0x00); // [7:4] max local counter, [3:0] max fast counter
	IicWrite(file,0x5183, 0x14); // AWB advanced
	IicWrite(file,0x5184, 0x25);
	IicWrite(file,0x5185, 0x24);
	IicWrite(file,0x5186, 0x09);
	IicWrite(file,0x5187, 0x09);
	IicWrite(file,0x5188, 0x09);
	IicWrite(file,0x5189, 0x75);
	IicWrite(file,0x518a, 0x54);
	IicWrite(file,0x518b, 0xe0);
	IicWrite(file,0x518c, 0xb2);
	IicWrite(file,0x518d, 0x42);
	IicWrite(file,0x518e, 0x3d);
	IicWrite(file,0x518f, 0x56);
	IicWrite(file,0x5190, 0x46);
	IicWrite(file,0x5191, 0xf8); // AWB top limit
	IicWrite(file,0x5192, 0x04); // AWB bottom limit
	IicWrite(file,0x5193, 0x70); // red limit
	IicWrite(file,0x5194, 0xf0); // green limit
	IicWrite(file,0x5195, 0xf0); // blue limit
	IicWrite(file,0x5196, 0x03); // AWB control
	IicWrite(file,0x5197, 0x01); // local limit
	IicWrite(file,0x5198, 0x04);
	IicWrite(file,0x5199, 0x12);
	IicWrite(file,0x519a, 0x04);
	IicWrite(file,0x519b, 0x00);
	IicWrite(file,0x519c, 0x06);
	IicWrite(file,0x519d, 0x82);
	IicWrite(file,0x519e, 0x38); // AWB control*/
	IicWrite(file,0x5180, 0xff); // AWB B block
	IicWrite(file,0x5181, 0x58); // AWB control
	IicWrite(file,0x5182, 0x11); // [7:4] max local counter, [3:0] max fast counter
	IicWrite(file,0x5183, 0x90); // AWB advanced
	IicWrite(file,0x5184, 0x25);
	IicWrite(file,0x5185, 0x24);
	IicWrite(file,0x5186, 0x09);
	IicWrite(file,0x5187, 0x09);
	IicWrite(file,0x5188, 0x09);
	IicWrite(file,0x5189, 0x75);
	IicWrite(file,0x518a, 0x54);
	IicWrite(file,0x518b, 0xe0);
	IicWrite(file,0x518c, 0xb2);
	IicWrite(file,0x518d, 0x42);
	IicWrite(file,0x518e, 0x3d);
	IicWrite(file,0x518f, 0x56);
	IicWrite(file,0x5190, 0x46);
	IicWrite(file,0x5191, 0xff); // AWB top limit
	IicWrite(file,0x5192, 0x00); // AWB bottom limit
	IicWrite(file,0x5193, 0xf0); // red limit
	IicWrite(file,0x5194, 0xf0); // green limit
	IicWrite(file,0x5195, 0xf0); // blue limit
	IicWrite(file,0x5196, 0x03); // AWB control
	IicWrite(file,0x5197, 0x02); // local limit
	IicWrite(file,0x5198, 0x04);
	IicWrite(file,0x5199, 0x12);
	IicWrite(file,0x519a, 0x04);
	IicWrite(file,0x519b, 0x00);
	IicWrite(file,0x519c, 0x06);
	IicWrite(file,0x519d, 0x82);
	IicWrite(file,0x519e, 0x00); // AWB control
	// Gamma 伽玛曲线
	IicWrite(file,0x5480, 0x01); // Gamma bias plus on, bit[0]
	IicWrite(file,0x5481, 0x08);
	IicWrite(file,0x5482, 0x14);
	IicWrite(file,0x5483, 0x28);
	IicWrite(file,0x5484, 0x51);
	IicWrite(file,0x5485, 0x65);
	IicWrite(file,0x5486, 0x71);
	IicWrite(file,0x5487, 0x7d);
	IicWrite(file,0x5488, 0x87);
	IicWrite(file,0x5489, 0x91);
	IicWrite(file,0x548a, 0x9a);
	IicWrite(file,0x548b, 0xaa);
	IicWrite(file,0x548c, 0xb8);
	IicWrite(file,0x548d, 0xcd);
	IicWrite(file,0x548e, 0xdd);
	IicWrite(file,0x548f, 0xea);
	IicWrite(file,0x5490, 0x1d);
	// color matrix 色彩矩阵
	IicWrite(file,0x5381, 0x1e); // CMX1 for Y
	IicWrite(file,0x5382, 0x5b); // CMX2 for Y
	IicWrite(file,0x5383, 0x08); // CMX3 for Y
	IicWrite(file,0x5384, 0x0a); // CMX4 for U
	IicWrite(file,0x5385, 0x7e); // CMX5 for U
	IicWrite(file,0x5386, 0x88); // CMX6 for U
	IicWrite(file,0x5387, 0x7c); // CMX7 for V
	IicWrite(file,0x5388, 0x6c); // CMX8 for V
	IicWrite(file,0x5389, 0x10); // CMX9 for V
	IicWrite(file,0x538a, 0x01); // sign[9]
	IicWrite(file,0x538b, 0x98); // sign[8:1]
	// UV adjust UV色彩饱和度调整
	IicWrite(file,0x5580, 0x06); // saturation on, bit[1]
	IicWrite(file,0x5583, 0x40);
	IicWrite(file,0x5584, 0x10);
	IicWrite(file,0x5589, 0x10);
	IicWrite(file,0x558a, 0x00);
	IicWrite(file,0x558b, 0xf8);
	IicWrite(file,0x501d, 0x40); // enable manual offset of contrast
	// CIP 锐化和降噪
	IicWrite(file,0x5300, 0x08); // CIP sharpen MT threshold 1
	IicWrite(file,0x5301, 0x30); // CIP sharpen MT threshold 2
	IicWrite(file,0x5302, 0x10); // CIP sharpen MT offset 1
	IicWrite(file,0x5303, 0x00); // CIP sharpen MT offset 2
	IicWrite(file,0x5304, 0x08); // CIP DNS threshold 1
	IicWrite(file,0x5305, 0x30); // CIP DNS threshold 2
	IicWrite(file,0x5306, 0x08); // CIP DNS offset 1
	IicWrite(file,0x5307, 0x16); // CIP DNS offset 2
	IicWrite(file,0x5309, 0x08); // CIP sharpen TH threshold 1
	IicWrite(file,0x530a, 0x30); // CIP sharpen TH threshold 2
	IicWrite(file,0x530b, 0x04); // CIP sharpen TH offset 1
	IicWrite(file,0x530c, 0x06); // CIP sharpen TH offset 2
	IicWrite(file,0x5025, 0x00);
	IicWrite(file,0x3008, 0x02); // wake up from standby, bit[6]
	// YUV VGA 30fps, night mode 5fps
	// Input Clock = 24Mhz, PCLK = 56MHz
	IicWrite(file,0x3035, 0x11); // PLL
	IicWrite(file,0x3036, 0x46); // PLL
	IicWrite(file,0x3c07, 0x08); // light meter 1 threshold [7:0]
	IicWrite(file,0x3820, 0x40); // Sensor flip off, ISP flip on
	IicWrite(file,0x3821, 0x07); // Sensor mirror on, ISP mirror on, H binning on
	IicWrite(file,0x3814, 0x31); // X INC
	IicWrite(file,0x3815, 0x31); // Y INC
	IicWrite(file,0x3800, 0x00); // HS
	IicWrite(file,0x3801, 0x00); // HS
	IicWrite(file,0x3802, 0x00); // VS
	IicWrite(file,0x3803, 0x04); // VS
	IicWrite(file,0x3804, 0x0a); // HW (HE)
	IicWrite(file,0x3805, 0x3f); // HW (HE)
	IicWrite(file,0x3806, 0x07); // VH (VE)
	IicWrite(file,0x3807, 0x9b); // VH (VE)
	IicWrite(file,0x3808, 0x02); // DVPHO
	IicWrite(file,0x3809, 0x80); // DVPHO
	IicWrite(file,0x380a, 0x01); // DVPVO
	IicWrite(file,0x380b, 0xe0); // DVPVO
	IicWrite(file,0x380c, 0x07); // HTS
	IicWrite(file,0x380d, 0x68); // HTS
	IicWrite(file,0x380e, 0x03); // VTS
	IicWrite(file,0x380f, 0xd8); // VTS
	IicWrite(file,0x3813, 0x06); // Timing Voffset
	IicWrite(file,0x3618, 0x00);
	IicWrite(file,0x3612, 0x29);
	IicWrite(file,0x3709, 0x52);
	IicWrite(file,0x370c, 0x03);
	IicWrite(file,0x3a02, 0x17); // 60Hz max exposure, night mode 5fps
	IicWrite(file,0x3a03, 0x10); // 60Hz max exposure
	IicWrite(file,0x3a14, 0x17); // 50Hz max exposure, night mode 5fps
	IicWrite(file,0x3a15, 0x10); // 50Hz max exposure
	IicWrite(file,0x4004, 0x02); // BLC 2 lines
	IicWrite(file,0x3002, 0x1c); // reset JFIFO, SFIFO, JPEG
	IicWrite(file,0x3006, 0xc3); // disable clock of JPEG2x, JPEG
	IicWrite(file,0x4713, 0x03); // JPEG mode 3
	IicWrite(file,0x4407, 0x04); // Quantization scale
	IicWrite(file,0x460b, 0x35);
	IicWrite(file,0x460c, 0x22);
	IicWrite(file,0x4837, 0x22); // DVP CLK divider
	IicWrite(file,0x3824, 0x02); // DVP CLK divider
	IicWrite(file,0x5001, 0xa3); // SDE on, scale on, UV average off, color matrix on, AWB on
	IicWrite(file,0x3503, 0x00); // AEC/AGC on
}

你可能感兴趣的:(zynq,liunx,linux,运维,服务器)