24bit addr的读写

static unsigned char isx019_checksum(unsigned char *buf, unsigned int len)
{
	unsigned char ret = 0;
	unsigned int i;

	for (i = 0; i < len; i++) {
		ret += buf[i];
	}

	return ret;
}

int __isx_write(struct i2c_client_t *client, unsigned short addr,
	unsigned int len, unsigned char *value)
{
	struct i2c_msg_t msg[1];

	if (!client || !client->adapter) {
		loge("adapter error");
		return -ISP_EINVAL;
	}

	msg[0].addr = addr;
	msg[0].len = len;
	msg[0].buf = value;
	msg[0].flags = 0;
	loge_if_ret(i2c_xfer(client->adapter, msg, 1, 0, client->speed_cfg));

	return 0;
}

int __isx_read(struct i2c_client_t *client, unsigned short addr,
	unsigned int len, void *value)
{
	struct i2c_msg_t msg[1];

	if (!client || !client->adapter || !value) {
		loge("client adapter doesn't exist");
		return -ISP_EINVAL;
	}

	msg[0].addr = addr;
	msg[0].len = len;
	msg[0].buf = value;
	msg[0].flags = 0x01; /* read flag */
	loge_if_ret(i2c_xfer(client->adapter, msg, 1, 0, client->speed_cfg));

	return 0;
}

static int isx019_access_unlock_send(struct i2c_client_t *client, unsigned short addr)
{
	unsigned char buf[255];
	unsigned int i = 0;
	unsigned char checksum;

	buf[i++] = 0x09;
	buf[i++] = 0x01;
	buf[i++] = 0x06;
	buf[i++] = 0x00;
	buf[i++] = 0x57;
	buf[i++] = 0x52;
	buf[i++] = 0x45;
	buf[i++] = 0x4e;
	checksum = isx019_checksum(buf, i);
	buf[i++] = checksum;

	return __isx_write(client, addr, i, buf);
}

static int isx019_access_unlock_recv(struct i2c_client_t *client, unsigned short addr)
{
	int ret;
	int read_len;
	unsigned char status_code;
	unsigned char value[255];
	ret = __isx_read(client, addr, 5, value);
	if (ret)
		return ret;

	read_len = value[0];
	unsigned char checksum = value[read_len-1];
	if (checksum != isx019_checksum(value, read_len-1)) {
		loge("checksum error");
	}

	status_code = value[3];

	return (status_code == 0x01) ? 0 : status_code;
}

static int isx019_flash_read_send(struct i2c_client_t *client, unsigned short addr, unsigned int flash_addr)
{
	unsigned char buf[255];
	unsigned int i = 0;
	unsigned char checksum;

	buf[i++] = 0x0a;
	buf[i++] = 0x01;
	buf[i++] = 0x07;
	buf[i++] = 0x03;
	buf[i++] = (flash_addr >> 24) & 0xff;
	buf[i++] = (flash_addr >> 16) & 0xff;
	buf[i++] = (flash_addr >> 8) & 0xff;
	buf[i++] = (flash_addr >> 0) & 0xff;
	buf[i++] = 0x01;
	checksum = isx019_checksum(buf, i);
	buf[i++] = checksum;

	return __isx_write(client, addr, i, buf);

}

static int isx019_flash_read_recv(struct i2c_client_t *client, unsigned short addr, unsigned char *read_value)
{
	int ret;
	int read_len;
	unsigned char status_code;
	unsigned char value[255];

	ret = __isx_read(client, addr, 7, value);
	if (ret)
		return ret;

	read_len = value[0];
	unsigned char checksum = value[read_len-1];
	if (checksum != isx019_checksum(value, read_len-1)) {
		loge("checksum error");
	}

	status_code = value[3];
	*read_value = value[4];

	return (status_code == 0x01) ? 0 : status_code;
}

static int isx019_access_lock_send(struct i2c_client_t *client, unsigned short addr)
{
	unsigned char buf[255];
	unsigned int i = 0;
	unsigned char checksum;

	buf[i++] = 0x09;
	buf[i++] = 0x01;
	buf[i++] = 0x06;
	buf[i++] = 0x00;
	buf[i++] = 0x57;
	buf[i++] = 0x52;
	buf[i++] = 0x44;
	buf[i++] = 0x53;
	checksum = isx019_checksum(buf, i);
	buf[i++] = checksum;

	return __isx_write(client, addr, i, buf);
}

static int isx019_access_lock_recv(struct i2c_client_t *client, unsigned short addr)
{
	int ret;
	int read_len;
	unsigned char status_code;
	unsigned char value[255];
	ret = __isx_read(client, addr, 5, value);
	if (ret)
		return ret;

	read_len = value[0];
	unsigned char checksum = value[read_len-1];
	if (checksum != isx019_checksum(value, read_len-1)) {
		loge("checksum error");
	}

	status_code = value[3];

	return (status_code == 0x01) ? 0 : status_code;;
}

static int isx019_flash_read(struct i2c_client_t *client, unsigned int flash_addr,
			unsigned char *read_value, int read_len, unsigned char camera_index)
{
	int ret, i;
	unsigned short addr;

	if (camera_index == 0)
		addr = 0x31;
	else if (camera_index == 1)
		addr = 0x32;
	else if (camera_index == 2)
		addr = 0x33;
	else if (camera_index == 3)
		addr = 0x34;
	else {
		cdc_loge("invalid camera_index:%d", camera_index);
		return -ISP_EINVAL;
	}

	ret = isx019_access_unlock_send(client, addr);
	if (ret) {
		cdc_loge("fail to isx019_access_unlock_send(%d)", ret);
		return ret;
	}

	os_mdelay(1);

	ret = isx019_access_unlock_recv(client, addr);
	if (ret) {
		cdc_loge("fail to isx019_access_unlock_recv(%d)", ret);
		return ret;
	}

	os_mdelay(1);

	for (i = 0; i < read_len; i++) {
		ret = isx019_flash_read_send(client, addr, flash_addr+i);
		if (ret) {
			cdc_loge("fail to isx019_flash_read_send(%d)", ret);
			return ret;
		}

		os_mdelay(2);

		ret = isx019_flash_read_recv(client, addr, read_value+i);
		if (ret) {
			cdc_loge("fail to isx019_flash_read_recv(%d)", ret);
			return ret;
		}

		os_mdelay(1);
	}
	ret = isx019_access_lock_send(client, addr);
	if (ret) {
		cdc_loge("fail to isx019_access_lock_send(%d)", ret);
		return ret;
	}

	os_mdelay(1);

	ret = isx019_access_lock_recv(client, addr);
	if (ret) {
		cdc_loge("fail to isx019_access_lock_recv(%d)", ret);
		return ret;
	}

	return 0;
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值