小弟使用IIC读写摄像头OV5642寄存器,读写都失败,IIC驱动提示“ACK not received”。
从设备寄存器地址是datasheet上写的,应该不会错。
是何原因那?
附上一段代码:
int ModuleWriteBlock()
{
int i;
UCHAR BUF = 0;
RETAILMSG(OV9650_DEBUG,(TEXT("[CAMERA] ModuleWriteBlock++ \r\n")));
// Read OV9650 PID
HW_ReadRegisters(&BUF, OV9650_PID_REG, 1);
if (OV9650_PID == BUF)
{
RETAILMSG(OV9650_DEBUG,(TEXT("[CAMERA] Read OV9650_PID: 0x%x successfully! \r\n"), BUF));
}
else
{
RETAILMSG(OV9650_ERROR,(TEXT("[CAMERA] Read OV9650_PID: 0x%x failed! \r\n"), BUF));
}
// Read OV9650 VID
HW_ReadRegisters(&BUF, OV9650_VER_REG, 1);
if (OV9650_VER == BUF)
{
RETAILMSG(OV9650_DEBUG,(TEXT("[CAMERA] Read OV9650_VER_REG: 0x%x successfully! \r\n"), BUF));
}
else
{
RETAILMSG(OV9650_ERROR,(TEXT("[CAMERA] Read OV9650_VER_REG: 0x%x failed! \r\n"), BUF));
}
RETAILMSG(OV9650_DEBUG, (TEXT("sizeof(ov9650_reg)= %d, sizeof(ov9650_reg[0]) = %d, OV9650_REGS = %d.\r\n"), sizeof(ov9650_reg), sizeof(ov9650_reg[0]), OV9650_REGS));
#if OV9650_DEBUG
for(i = 0; i < 6; i++)
{
HW_ReadRegisters(&BUF, 0x3400|i, 1);
RETAILMSG(OV9650_DEBUG,(TEXT("HW_ReadRegisters: Reg[0x%x] = 0x%x, i = %d.\r\n"), 0x3400|i, BUF ,i));
}
#endif
for(i = 0; i < OV9650_REGS; i++)
{
HW_WriteRegisters(&ov9650_reg[i][0], 2);
RETAILMSG(OV9650_DEBUG,(TEXT("HW_WriteRegisters: Reg[0x%x] = 0x%x, i = %d.\r\n"), ov9650_reg[i][0], ov9650_reg[i][1], i));
}
ModuleSetImageSize(VGA);
#if OV9650_DEBUG
for(i = 0; i < OV9650_REGS; i++)
{
HW_ReadRegisters(&BUF, ov9650_reg[i][0], 1);
RETAILMSG(OV9650_DEBUG,(TEXT("HW_ReadRegisters: Reg[0x%x] = 0x%x, i = %d.\r\n"), ov9650_reg[i][0], BUF ,i));
}
#endif
RETAILMSG(OV9650_DEBUG,(TEXT("ModuleWriteBloc-- \r\n")));
return TRUE;
}
// ------------------------------------------------
// 写模组寄存器
// ------------------------------------------------
DWORD
HW_WriteRegisters(
PUCHAR pBuff, // Optional buffer
DWORD nRegs // number of registers
)
{
DWORD dwErr = 0;
DWORD bytes;
IIC_IO_DESC IIC_Data;
RETAILMSG(OV9650_DEBUG,(TEXT("HW_WriteRegisters++ \r\n")));
IIC_Data.SlaveAddress = CAMERA_WRITE;
IIC_Data.Count = nRegs;
IIC_Data.Data = pBuff;
// use iocontrol to write
if ( !DeviceIoControl( hI2C,
IOCTL_IIC_WRITE,
&IIC_Data,
sizeof(IIC_IO_DESC),
NULL,
0,
&bytes,
NULL) )
{
dwErr = GetLastError();
RETAILMSG(OV9650_ERROR,(TEXT("IOCTL_IIC_WRITE ERROR: %u \r\n"), dwErr));
}
if ( dwErr )
{
RETAILMSG(OV9650_ERROR, (TEXT("I2CWrite ERROR: %u \r\n"), dwErr));
}
RETAILMSG(OV9650_DEBUG,(TEXT("HW_WriteRegisters-- \r\n")));
return dwErr;
}
// ------------------------------------------------
// 读模组寄存器
// ------------------------------------------------
DWORD
HW_ReadRegisters(
PUCHAR pBuff, // Optional buffer
UCHAR StartReg, // Start Register
DWORD nRegs // Number of Registers
)
{
DWORD dwErr = 0;
DWORD bytes;
IIC_IO_DESC IIC_AddressData;
IIC_IO_DESC IIC_Data;
RETAILMSG(OV9650_DEBUG,(TEXT("HW_ReadRegisters++ \r\n")));
IIC_AddressData.SlaveAddress = CAMERA_WRITE;
IIC_AddressData.Data = &StartReg;
IIC_AddressData.Count = 1;