47 #include <linux/i2c.h> 48 #include <linux/i2c-dev.h> 53 I2C::I2C(
const char *
name,
const char *devname,
const int bus,
const uint16_t address,
const uint32_t frequency) :
56 DEVICE_DEBUG(
"I2C::I2C name = %s devname = %s", name, devname);
58 _device_id.devid_s.bus_type = DeviceBusType_I2C;
59 _device_id.devid_s.bus = bus;
60 _device_id.devid_s.address = address;
62 _device_id.devid_s.devtype = 0;
89 snprintf(dev_path,
sizeof(dev_path),
"/dev/i2c-%i", get_device_bus());
90 _fd = ::open(dev_path, O_RDWR);
93 PX4_ERR(
"could not open %s", dev_path);
102 I2C::transfer(
const uint8_t *
send,
const unsigned send_len, uint8_t *recv,
const unsigned recv_len)
104 struct i2c_msg msgv[2];
107 unsigned retry_count = 0;
110 PX4_ERR(
"I2C device not opened");
115 DEVICE_DEBUG(
"transfer out %p/%u in %p/%u", send, send_len, recv, recv_len);
119 msgv[msgs].addr = get_device_address();
120 msgv[msgs].flags = 0;
121 msgv[msgs].buf =
const_cast<uint8_t *
>(
send);
122 msgv[msgs].len = send_len;
127 msgv[msgs].addr = get_device_address();
128 msgv[msgs].flags = I2C_M_READ;
129 msgv[msgs].buf = recv;
130 msgv[msgs].len = recv_len;
138 struct i2c_rdwr_ioctl_data packets;
142 packets.nmsgs = msgs;
144 ret = ::ioctl(
_fd, I2C_RDWR, (
unsigned long)&packets);
166 #endif // __PX4_LINUX
int transfer(const uint8_t *send, const unsigned send_len, uint8_t *recv, const unsigned recv_len)
Perform an I2C transaction to the device.
Namespace encapsulating all device framework classes, functions and data.
void init()
Activates/configures the hardware registers.
virtual int init() override
uint8_t _retries
The number of times a read or write operation will be retried on error.
#define DEVICE_DEBUG(FMT,...)