Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

make reads non blocking and set device address once #12

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion include/i2c/i2c.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ void i2c_close(int bus);
int i2c_open(const char *bus_name);

/* Initialize I2CDevice with default value */
void i2c_init_device(I2CDevice *device);
int i2c_init_device(I2CDevice *device);

/* Get i2c device description */
char *i2c_get_device_desc(const I2CDevice *device, char *buf, size_t size);
Expand Down
27 changes: 12 additions & 15 deletions src/i2c.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ int i2c_open(const char *bus_name)
return -1;
}

// make reading non blocking
fcntl(fd, F_SETFL, O_NONBLOCK);

return fd;
}

Expand All @@ -51,7 +54,7 @@ void i2c_close(int bus)
** @brief : Initialize I2CDevice with defualt value
** #device : I2CDevice struct
*/
void i2c_init_device(I2CDevice *device)
int i2c_init_device(I2CDevice *device)
{
/* 7 bit device address */
device->tenbit = 0;
Expand All @@ -64,6 +67,12 @@ void i2c_init_device(I2CDevice *device)

/* 1 byte internal(word) address */
device->iaddr_bytes = 1;

/* Set i2c slave address once */
if (i2c_select(device->bus, device->addr, device->tenbit) == -1) {
return -1;
}
return 0;
}


Expand Down Expand Up @@ -218,12 +227,6 @@ ssize_t i2c_read(const I2CDevice *device, unsigned int iaddr, void *buf, size_t
unsigned char addr[INT_ADDR_MAX_BYTES];
unsigned char delay = GET_I2C_DELAY(device->delay);

/* Set i2c slave address */
if (i2c_select(device->bus, device->addr, device->tenbit) == -1) {

return -1;
}

/* Convert i2c internal address */
memset(addr, 0, sizeof(addr));
i2c_iaddr_convert(iaddr, device->iaddr_bytes, addr);
Expand Down Expand Up @@ -266,12 +269,6 @@ ssize_t i2c_write(const I2CDevice *device, unsigned int iaddr, const void *buf,
unsigned char delay = GET_I2C_DELAY(device->delay);
unsigned char tmp_buf[PAGE_MAX_BYTES + INT_ADDR_MAX_BYTES];

/* Set i2c slave address */
if (i2c_select(device->bus, device->addr, device->tenbit) == -1) {

return -1;
}

/* Once only can write less than 4 byte */
while (remain > 0) {

Expand Down Expand Up @@ -350,8 +347,8 @@ int i2c_select(int bus, unsigned long dev_addr, unsigned long tenbit)
return -1;
}

/* Set i2c device as slave ans set it address */
if (ioctl(bus, I2C_SLAVE, dev_addr)) {
/* Set i2c device as slave and set it address */
if (ioctl(bus, I2C_SLAVE_FORCE, dev_addr)) {

perror("Set i2c device address failed");
return -1;
Expand Down