#include #include #include #include #include #include #include #include #include #include "avm_i2c.h" /*------------------------------------------------------------------------------------------*\ \*------------------------------------------------------------------------------------------*/ struct _avm_i2c_open_data { int fd; enum _avm_i2c_type device_type; }; /*------------------------------------------------------------------------------------------*\ \*------------------------------------------------------------------------------------------*/ void *avm_i2c_open(enum _avm_i2c_type device_type) { const char *filename = "/dev/i2c"; struct _avm_i2c_open_data *open_data = calloc(sizeof(struct _avm_i2c_open_data), 1);; if(open_data == NULL) return NULL; if ((open_data->fd = open(filename, O_RDWR)) < 0) { free(open_data); return NULL; } open_data->device_type = device_type; return (void *)open_data; } /*------------------------------------------------------------------------------------------*\ \*------------------------------------------------------------------------------------------*/ int avm_i2c_close(void *handle) { struct _avm_i2c_open_data *open_data = (struct _avm_i2c_open_data *)handle; if(open_data == NULL) return 1; close(open_data->fd); free(open_data); return 0; } /*------------------------------------------------------------------------------------------*\ \*------------------------------------------------------------------------------------------*/ int avm_i2c_speed(void *handle, int speed) { struct _i2c_cmd Cmd; struct _avm_i2c_open_data *open_data = (struct _avm_i2c_open_data *)handle; if(open_data == NULL) return 1; *((unsigned int *)Cmd.Buffer) = speed; if (ioctl(open_data->fd, I2C_SET_SPEED_ID, &Cmd) < 0) { return 1; } return 0; } /*------------------------------------------------------------------------------------------*\ \*------------------------------------------------------------------------------------------*/ int avm_i2c_read(void *handle, int device_addr, int register_addr, unsigned char *Buffer, int len) { struct _i2c_cmd Cmd; struct _avm_i2c_open_data *open_data = (struct _avm_i2c_open_data *)handle; if(open_data == NULL) return 1; Cmd.device_address = device_addr; Cmd.register_address = register_addr; Cmd.data_length = len; Cmd.device_type = open_data->device_type; if (ioctl(open_data->fd, I2C_READ_ID, &Cmd) < 0) { return -errno; } memcpy(Buffer, Cmd.Buffer, Cmd.data_length); return len; } /*------------------------------------------------------------------------------------------*\ \*------------------------------------------------------------------------------------------*/ int avm_i2c_write(void *handle, int device_addr, int register_addr, unsigned char *Buffer, int len) { struct _i2c_cmd Cmd; struct _avm_i2c_open_data *open_data = (struct _avm_i2c_open_data *)handle; if(open_data == NULL) return 1; Cmd.device_address = device_addr; Cmd.register_address = register_addr; Cmd.data_length = len; Cmd.device_type = open_data->device_type; memcpy(Cmd.Buffer, Buffer, Cmd.data_length); if (ioctl(open_data->fd, I2C_WRITE_ID, &Cmd) < 0) { return -errno; } return len; } /*------------------------------------------------------------------------------------------*\ \*------------------------------------------------------------------------------------------*/ int avm_i2c_raw_read(void *handle, int device_addr, unsigned char *Buffer, int len) { struct _i2c_cmd Cmd; struct _avm_i2c_open_data *open_data = (struct _avm_i2c_open_data *)handle; if(open_data == NULL) return 1; Cmd.device_address = device_addr; Cmd.data_length = len; Cmd.device_type = open_data->device_type; if (ioctl(open_data->fd, I2C_RAW_READ_ID, &Cmd) < 0) { return -errno; } memcpy(Buffer, Cmd.Buffer, Cmd.data_length); return len; } /*------------------------------------------------------------------------------------------*\ \*------------------------------------------------------------------------------------------*/ int avm_i2c_raw_write(void *handle, int device_addr, unsigned char *Buffer, int len) { struct _i2c_cmd Cmd; struct _avm_i2c_open_data *open_data = (struct _avm_i2c_open_data *)handle; if(open_data == NULL) return 1; Cmd.device_address = device_addr; Cmd.data_length = len; Cmd.device_type = open_data->device_type; memcpy(Cmd.Buffer, Buffer, Cmd.data_length); if (ioctl(open_data->fd, I2C_RAW_WRITE_ID, &Cmd) < 0) { return -errno; } return len; }