bme680/i2c.c

57 lines
958 B
C
Raw Normal View History

2023-09-17 20:31:02 +00:00
#include <stdio.h>
#include <fcntl.h>
#include <linux/i2c.h>
#include <linux/i2c-dev.h>
#include <stdint.h>
#include <sys/ioctl.h>
#include <unistd.h>
#include <stdlib.h>
2023-11-04 20:37:39 +00:00
#include "i2c.h"
2023-09-17 20:31:02 +00:00
int i2c_init(const char *dev, uint8_t addr) {
int fd = -1;
fd = open(dev, O_RDWR);
if (fd < 0) {
fprintf(stderr, "could not open device: %s\n", dev);
return -1;
}
if (ioctl(fd, I2C_SLAVE, addr) < 0) {
fprintf(stderr, "failed to acquire bus/talk to slave\n");
close(fd);
return -1;
}
return fd;
}
2023-11-04 20:37:39 +00:00
int i2c_read_reg(int fd, uint8_t reg, uint8_t *dst, uint32_t size) {
2023-09-17 20:31:02 +00:00
uint8_t cmd[2] = {reg, 0x00};
write(fd, cmd, 2);
2023-11-04 20:37:39 +00:00
if (read(fd, dst, size) != (ssize_t)size) {
2023-09-17 20:31:02 +00:00
fprintf(stderr, "error read()\n");
2023-11-04 20:37:39 +00:00
return I2C_ERR;
2023-09-17 20:31:02 +00:00
}
2023-11-04 20:37:39 +00:00
return I2C_OK;
2023-09-17 20:31:02 +00:00
}
int i2c_write_reg(int fd, uint8_t reg, uint8_t value) {
uint8_t cmd[2] = {reg, value};
2023-11-04 20:37:39 +00:00
2023-09-17 20:31:02 +00:00
if (write(fd, cmd, 2) != 2) {
fprintf(stderr, "error write()\n");
2023-11-04 20:37:39 +00:00
return I2C_ERR;
2023-09-17 20:31:02 +00:00
}
2023-11-04 20:37:39 +00:00
return I2C_OK;
2023-09-17 20:31:02 +00:00
}