aboutsummaryrefslogtreecommitdiffstats
path: root/mpm/lib/i2c/i2cdev_iface.cpp
diff options
context:
space:
mode:
authorAlex Williams <alex.williams@ni.com>2018-10-12 17:17:13 -0700
committerBrent Stapleton <bstapleton@g.hmc.edu>2018-10-19 10:17:08 -0700
commita830fab2a00acd158f14d716e3493ef50afd8aeb (patch)
tree96425e10026d94dfcef46ebbfa8cc5bccb5cf90f /mpm/lib/i2c/i2cdev_iface.cpp
parent0e30a5ca0872762a36be15f030a763c7f67dd003 (diff)
downloaduhd-a830fab2a00acd158f14d716e3493ef50afd8aeb.tar.gz
uhd-a830fab2a00acd158f14d716e3493ef50afd8aeb.tar.bz2
uhd-a830fab2a00acd158f14d716e3493ef50afd8aeb.zip
mpm: Add i2c APIs for simple transfers
Diffstat (limited to 'mpm/lib/i2c/i2cdev_iface.cpp')
-rw-r--r--mpm/lib/i2c/i2cdev_iface.cpp95
1 files changed, 95 insertions, 0 deletions
diff --git a/mpm/lib/i2c/i2cdev_iface.cpp b/mpm/lib/i2c/i2cdev_iface.cpp
new file mode 100644
index 000000000..43aeea5e2
--- /dev/null
+++ b/mpm/lib/i2c/i2cdev_iface.cpp
@@ -0,0 +1,95 @@
+//
+// Copyright 2018 Ettus Research, a National Instruments Company
+//
+// SPDX-License-Identifier: GPL-3.0-or-later
+//
+
+
+#include <mpm/i2c/i2c_iface.hpp>
+#include <mpm/exception.hpp>
+
+#include "i2cdev.h"
+
+#include <fcntl.h>
+#include <linux/i2c.h>
+#include <linux/i2c-dev.h>
+
+#include <boost/format.hpp>
+#include <iostream>
+
+using namespace mpm::i2c;
+
+/******************************************************************************
+ * Implementation
+ *****************************************************************************/
+class i2cdev_iface_impl : public i2c_iface
+{
+public:
+
+ i2cdev_iface_impl(
+ const std::string &device,
+ const uint16_t addr,
+ const bool ten_bit_addr,
+ const unsigned int timeout_ms
+ ) : _addr(addr),
+ _ten_bit_addr(ten_bit_addr),
+ _timeout_ms(timeout_ms)
+ {
+ if (i2cdev_open(
+ &_fd,
+ device.c_str(),
+ timeout_ms) < 0)
+ {
+ throw mpm::runtime_error(str(
+ boost::format("Could not initialize i2cdev device %s")
+ % device));
+ }
+
+ if (_fd < 0)
+ {
+ throw mpm::runtime_error(str(
+ boost::format("Could not open i2cdev device %s")
+ % device));
+ }
+ }
+
+ ~i2cdev_iface_impl()
+ {
+ close(_fd);
+ }
+
+ int transfer(uint8_t *tx, size_t tx_len, uint8_t *rx, size_t rx_len)
+ {
+ int ret = i2cdev_transfer(_fd, _addr, _ten_bit_addr,
+ tx, tx_len, rx, rx_len);
+
+ if (ret) {
+ throw mpm::runtime_error(str(
+ boost::format("I2C Transaction failed!")
+ ));
+ }
+
+ return ret;
+ }
+
+private:
+ int _fd;
+ const uint16_t _addr;
+ const bool _ten_bit_addr;
+ const unsigned int _timeout_ms;
+};
+
+/******************************************************************************
+ * Factory
+ *****************************************************************************/
+i2c_iface::sptr i2c_iface::make_i2cdev(
+ const std::string &bus,
+ const uint16_t addr,
+ const bool ten_bit_addr,
+ const int timeout_ms
+) {
+ return std::make_shared<i2cdev_iface_impl>(
+ bus, addr, ten_bit_addr, timeout_ms
+ );
+}
+