2022-05-19 05:11:57 +08:00
|
|
|
#include "common/i2c.h"
|
2020-08-26 17:30:30 +08:00
|
|
|
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <sys/ioctl.h>
|
2021-05-09 13:15:17 +08:00
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#include <cassert>
|
2021-06-09 04:46:22 +08:00
|
|
|
#include <cstdio>
|
2021-05-09 13:15:17 +08:00
|
|
|
#include <stdexcept>
|
|
|
|
|
2022-05-19 05:11:57 +08:00
|
|
|
#include "common/util.h"
|
|
|
|
#include "common/swaglog.h"
|
|
|
|
#include "common/util.h"
|
2020-08-26 17:30:30 +08:00
|
|
|
|
|
|
|
#define UNUSED(x) (void)(x)
|
|
|
|
|
|
|
|
#ifdef QCOM2
|
|
|
|
// TODO: decide if we want to isntall libi2c-dev everywhere
|
2020-11-13 09:09:36 +08:00
|
|
|
extern "C" {
|
|
|
|
#include <linux/i2c-dev.h>
|
|
|
|
#include <i2c/smbus.h>
|
|
|
|
}
|
2020-08-26 17:30:30 +08:00
|
|
|
|
2021-06-11 16:17:52 +08:00
|
|
|
I2CBus::I2CBus(uint8_t bus_id) {
|
2020-08-26 17:30:30 +08:00
|
|
|
char bus_name[20];
|
|
|
|
snprintf(bus_name, 20, "/dev/i2c-%d", bus_id);
|
|
|
|
|
2021-09-01 07:22:56 +08:00
|
|
|
i2c_fd = HANDLE_EINTR(open(bus_name, O_RDWR));
|
2021-06-11 16:17:52 +08:00
|
|
|
if(i2c_fd < 0) {
|
2020-08-26 17:30:30 +08:00
|
|
|
throw std::runtime_error("Failed to open I2C bus");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-11 16:17:52 +08:00
|
|
|
I2CBus::~I2CBus() {
|
|
|
|
if(i2c_fd >= 0) { close(i2c_fd); }
|
2020-08-26 17:30:30 +08:00
|
|
|
}
|
|
|
|
|
2021-06-11 16:17:52 +08:00
|
|
|
int I2CBus::read_register(uint8_t device_address, uint register_address, uint8_t *buffer, uint8_t len) {
|
2020-08-26 17:30:30 +08:00
|
|
|
int ret = 0;
|
|
|
|
|
2021-08-31 07:07:34 +08:00
|
|
|
ret = HANDLE_EINTR(ioctl(i2c_fd, I2C_SLAVE, device_address));
|
2020-08-26 17:30:30 +08:00
|
|
|
if(ret < 0) { goto fail; }
|
|
|
|
|
|
|
|
ret = i2c_smbus_read_i2c_block_data(i2c_fd, register_address, len, buffer);
|
|
|
|
if((ret < 0) || (ret != len)) { goto fail; }
|
|
|
|
|
|
|
|
fail:
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2021-06-11 16:17:52 +08:00
|
|
|
int I2CBus::set_register(uint8_t device_address, uint register_address, uint8_t data) {
|
2020-08-26 17:30:30 +08:00
|
|
|
int ret = 0;
|
|
|
|
|
2021-08-31 07:07:34 +08:00
|
|
|
ret = HANDLE_EINTR(ioctl(i2c_fd, I2C_SLAVE, device_address));
|
2020-08-26 17:30:30 +08:00
|
|
|
if(ret < 0) { goto fail; }
|
|
|
|
|
|
|
|
ret = i2c_smbus_write_byte_data(i2c_fd, register_address, data);
|
|
|
|
if(ret < 0) { goto fail; }
|
|
|
|
|
|
|
|
fail:
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
2021-06-11 16:17:52 +08:00
|
|
|
I2CBus::I2CBus(uint8_t bus_id) {
|
2020-08-26 17:30:30 +08:00
|
|
|
UNUSED(bus_id);
|
|
|
|
i2c_fd = -1;
|
|
|
|
}
|
|
|
|
|
2021-06-11 16:17:52 +08:00
|
|
|
I2CBus::~I2CBus() {}
|
2020-08-26 17:30:30 +08:00
|
|
|
|
2021-06-11 16:17:52 +08:00
|
|
|
int I2CBus::read_register(uint8_t device_address, uint register_address, uint8_t *buffer, uint8_t len) {
|
2020-08-26 17:30:30 +08:00
|
|
|
UNUSED(device_address);
|
|
|
|
UNUSED(register_address);
|
|
|
|
UNUSED(buffer);
|
|
|
|
UNUSED(len);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2021-06-11 16:17:52 +08:00
|
|
|
int I2CBus::set_register(uint8_t device_address, uint register_address, uint8_t data) {
|
2020-08-26 17:30:30 +08:00
|
|
|
UNUSED(device_address);
|
|
|
|
UNUSED(register_address);
|
|
|
|
UNUSED(data);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
#endif
|