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;
}
24bit addr的读写
于 2022-03-17 11:12:09 首次发布