Linux: I2C virtual device

Create and open I2C virtual device and support I2C_RDWR ioctl

Signed-off-by: Mark Charlebois <charlebm@gmail.com>
This commit is contained in:
Mark Charlebois
2015-03-25 00:00:57 -07:00
parent 192d70e570
commit 340beac672
2 changed files with 30 additions and 2 deletions
+28 -2
View File
@@ -100,6 +100,12 @@ I2C::init()
return ret;
}
_fd = px4_open(_dname.c_str(), PX4_F_RDONLY | PX4_F_WRONLY);
if (_fd < 0) {
debug("px4_open failed of device %s", _dname.c_str());
return PX4_ERROR;
}
#if 0
// Open the actual I2C device and map to the virtual dev name
char str[22];
@@ -111,6 +117,7 @@ I2C::init()
warnx("could not open %s for virtual device %s", str, _dname.c_str());
return -errno;
}
#endif
return ret;
}
@@ -124,6 +131,11 @@ I2C::transfer(const uint8_t *send, unsigned send_len, uint8_t *recv, unsigned re
int ret;
unsigned retry_count = 0;
if (_fd < 0) {
warnx("I2C device not opened");
return 1;
}
do {
// debug("transfer out %p/%u in %p/%u", send, send_len, recv, recv_len);
msgs = 0;
@@ -150,7 +162,7 @@ I2C::transfer(const uint8_t *send, unsigned send_len, uint8_t *recv, unsigned re
packets.msgs = msgv;
packets.nmsgs = msgs;
ret = ::ioctl(_fd, I2C_RDWR, &packets);
ret = px4_ioctl(_fd, I2C_RDWR, (unsigned long)&packets);
if (ret < 0) {
warnx("I2C transfer failed");
return 1;
@@ -187,7 +199,7 @@ I2C::transfer(struct i2c_msg *msgv, unsigned msgs)
packets.msgs = msgv;
packets.nmsgs = msgs;
ret = ::ioctl(_fd, I2C_RDWR, &packets);
ret = px4_ioctl(_fd, I2C_RDWR, (unsigned long)&packets);
if (ret < 0) {
warnx("I2C transfer failed");
return 1;
@@ -209,4 +221,18 @@ I2C::transfer(struct i2c_msg *msgv, unsigned msgs)
return ret;
}
int I2C::ioctl(device::px4_dev_handle_t *handlep, int cmd, unsigned long arg)
{
//struct i2c_rdwr_ioctl_data *packets = (i2c_rdwr_ioctl_data *)(void *)arg;
switch (cmd) {
case I2C_RDWR:
warnx("I2C transfer request");
return 0;
default:
/* give it to the superclass */
return CDev::ioctl(handlep, cmd, arg);
}
}
} // namespace device
+2
View File
@@ -99,6 +99,8 @@ protected:
virtual int probe();
#endif
virtual int ioctl(device::px4_dev_handle_t *handlep, int cmd, unsigned long arg);
/**
* Perform an I2C transaction to the device.
*