aboutsummaryrefslogtreecommitdiffstats
path: root/mpm/lib/i2c
diff options
context:
space:
mode:
Diffstat (limited to 'mpm/lib/i2c')
-rw-r--r--mpm/lib/i2c/i2c_regs_iface.cpp75
-rw-r--r--mpm/lib/i2c/i2cdev_iface.cpp78
2 files changed, 58 insertions, 95 deletions
diff --git a/mpm/lib/i2c/i2c_regs_iface.cpp b/mpm/lib/i2c/i2c_regs_iface.cpp
index c476cf636..575e19d26 100644
--- a/mpm/lib/i2c/i2c_regs_iface.cpp
+++ b/mpm/lib/i2c/i2c_regs_iface.cpp
@@ -4,10 +4,10 @@
// SPDX-License-Identifier: GPL-3.0-or-later
//
-#include <mpm/types/regs_iface.hpp>
+#include <mpm/exception.hpp>
#include <mpm/i2c/i2c_iface.hpp>
#include <mpm/i2c/i2c_regs_iface.hpp>
-#include <mpm/exception.hpp>
+#include <mpm/types/regs_iface.hpp>
using mpm::types::regs_iface;
@@ -18,26 +18,21 @@ using mpm::types::regs_iface;
class i2c_regs_iface_impl : public regs_iface
{
public:
-
- i2c_regs_iface_impl(
- mpm::i2c::i2c_iface::sptr i2c_iface,
- const size_t reg_addr_size
- ) : _i2c_iface(i2c_iface),
- _reg_addr_size(reg_addr_size)
+ i2c_regs_iface_impl(mpm::i2c::i2c_iface::sptr i2c_iface, const size_t reg_addr_size)
+ : _i2c_iface(i2c_iface), _reg_addr_size(reg_addr_size)
{
if (reg_addr_size > 4) {
throw mpm::runtime_error("reg_addr_size too largs for i2c_regs_iface");
}
}
- uint8_t peek8(
- const uint32_t addr
- ) {
+ uint8_t peek8(const uint32_t addr)
+ {
uint8_t rx[1];
uint8_t tx[5];
int i = 0;
for (; i < _reg_addr_size; i++) {
- tx[i] = 0xff & (addr >> 8*(_reg_addr_size-i-1));
+ tx[i] = 0xff & (addr >> 8 * (_reg_addr_size - i - 1));
}
int err = _i2c_iface->transfer(tx, _reg_addr_size, rx, 1);
@@ -48,14 +43,12 @@ public:
return rx[0];
}
- void poke8(
- const uint32_t addr,
- const uint8_t data
- ) {
+ void poke8(const uint32_t addr, const uint8_t data)
+ {
uint8_t tx[5];
int i = 0;
for (; i < _reg_addr_size; i++) {
- tx[i] = 0xff & (addr >> 8*(_reg_addr_size-i-1));
+ tx[i] = 0xff & (addr >> 8 * (_reg_addr_size - i - 1));
}
tx[i] = data;
@@ -65,14 +58,13 @@ public:
}
}
- uint16_t peek16(
- const uint32_t addr
- ) {
+ uint16_t peek16(const uint32_t addr)
+ {
uint8_t rx[2];
uint8_t tx[5];
int i = 0;
for (; i < _reg_addr_size; i++) {
- tx[i] = 0xff & (addr >> 8*(_reg_addr_size-i-1));
+ tx[i] = 0xff & (addr >> 8 * (_reg_addr_size - i - 1));
}
int err = _i2c_iface->transfer(tx, _reg_addr_size, rx, 2);
@@ -81,21 +73,19 @@ public:
}
uint16_t data = rx[0];
- data = (data << 8) | rx[1];
+ data = (data << 8) | rx[1];
return data;
}
- void poke16(
- const uint32_t addr,
- const uint16_t data
- ) {
+ void poke16(const uint32_t addr, const uint16_t data)
+ {
uint8_t tx[6];
int i = 0;
for (; i < _reg_addr_size; i++) {
- tx[i] = 0xff & (addr >> 8*(_reg_addr_size-i-1));
+ tx[i] = 0xff & (addr >> 8 * (_reg_addr_size - i - 1));
}
- tx[i] = (data >> 8) & 0xff;
- tx[i+1] = data & 0xff;
+ tx[i] = (data >> 8) & 0xff;
+ tx[i + 1] = data & 0xff;
int err = _i2c_iface->transfer(tx, _reg_addr_size + 2, NULL, 0);
if (err) {
@@ -110,27 +100,18 @@ private:
};
regs_iface::sptr mpm::i2c::make_i2c_regs_iface(
- mpm::i2c::i2c_iface::sptr i2c_iface,
- const size_t reg_addr_size
-) {
- return std::make_shared<i2c_regs_iface_impl>(
- i2c_iface,
- reg_addr_size
- );
+ mpm::i2c::i2c_iface::sptr i2c_iface, const size_t reg_addr_size)
+{
+ return std::make_shared<i2c_regs_iface_impl>(i2c_iface, reg_addr_size);
}
-mpm::types::regs_iface::sptr mpm::i2c::make_i2cdev_regs_iface(
- const std::string &bus,
+mpm::types::regs_iface::sptr mpm::i2c::make_i2cdev_regs_iface(const std::string& bus,
const uint16_t addr,
const bool ten_bit_addr,
const int timeout_ms,
- const size_t reg_addr_size
-) {
- auto i2c_iface_sptr = mpm::i2c::i2c_iface::make_i2cdev(
- bus, addr, ten_bit_addr, timeout_ms
- );
- return std::make_shared<i2c_regs_iface_impl>(
- i2c_iface_sptr,
- reg_addr_size
- );
+ const size_t reg_addr_size)
+{
+ auto i2c_iface_sptr =
+ mpm::i2c::i2c_iface::make_i2cdev(bus, addr, ten_bit_addr, timeout_ms);
+ return std::make_shared<i2c_regs_iface_impl>(i2c_iface_sptr, reg_addr_size);
}
diff --git a/mpm/lib/i2c/i2cdev_iface.cpp b/mpm/lib/i2c/i2cdev_iface.cpp
index 5b59e06f8..b346597a8 100644
--- a/mpm/lib/i2c/i2cdev_iface.cpp
+++ b/mpm/lib/i2c/i2cdev_iface.cpp
@@ -5,15 +5,12 @@
//
-#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 <linux/i2c.h>
+#include <mpm/exception.hpp>
+#include <mpm/i2c/i2c_iface.hpp>
#include <boost/format.hpp>
#include <iostream>
@@ -25,17 +22,15 @@ using namespace mpm::i2c;
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,
- const bool do_open = false
- ) : _device(device),
- _addr(addr),
- _ten_bit_addr(ten_bit_addr),
- _timeout_ms(timeout_ms)
+ i2cdev_iface_impl(const std::string& device,
+ const uint16_t addr,
+ const bool ten_bit_addr,
+ const unsigned int timeout_ms,
+ const bool do_open = false)
+ : _device(device)
+ , _addr(addr)
+ , _ten_bit_addr(ten_bit_addr)
+ , _timeout_ms(timeout_ms)
{
if (do_open)
_open();
@@ -49,13 +44,12 @@ public:
close(_fd);
}
- int transfer(uint8_t *tx, size_t tx_len, uint8_t *rx, size_t rx_len, bool do_close)
+ int transfer(uint8_t* tx, size_t tx_len, uint8_t* rx, size_t rx_len, bool do_close)
{
if (_fd < 0)
_open();
- int ret = i2cdev_transfer(_fd, _addr, _ten_bit_addr,
- tx, tx_len, rx, rx_len);
+ int ret = i2cdev_transfer(_fd, _addr, _ten_bit_addr, tx, tx_len, rx, rx_len);
if (do_close) {
close(_fd);
@@ -63,27 +57,25 @@ public:
}
if (ret) {
- throw mpm::runtime_error(str(
- boost::format("I2C Transaction failed!")
- ));
+ throw mpm::runtime_error(str(boost::format("I2C Transaction failed!")));
}
return ret;
}
- int transfer(std::vector<uint8_t> *tx, std::vector<uint8_t> *rx, bool do_close)
+ int transfer(std::vector<uint8_t>* tx, std::vector<uint8_t>* rx, bool do_close)
{
uint8_t *tx_data = NULL, *rx_data = NULL;
size_t tx_len = 0, rx_len = 0;
if (tx) {
tx_data = tx->data();
- tx_len = tx->size();
+ tx_len = tx->size();
}
if (rx) {
rx_data = rx->data();
- rx_len = rx->size();
+ rx_len = rx->size();
}
int ret = transfer(tx_data, tx_len, rx_data, rx_len, do_close);
@@ -97,22 +89,16 @@ private:
const bool _ten_bit_addr;
const unsigned int _timeout_ms;
- int _open(void) {
- if (i2cdev_open(
- &_fd,
- _device.c_str(),
- _timeout_ms) < 0)
- {
- throw mpm::runtime_error(str(
- boost::format("Could not initialize i2cdev device %s")
- % _device));
+ int _open(void)
+ {
+ 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));
+ if (_fd < 0) {
+ throw mpm::runtime_error(
+ str(boost::format("Could not open i2cdev device %s") % _device));
}
}
};
@@ -120,14 +106,10 @@ private:
/******************************************************************************
* Factory
*****************************************************************************/
-i2c_iface::sptr i2c_iface::make_i2cdev(
- const std::string &bus,
+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
- );
+ const int timeout_ms)
+{
+ return std::make_shared<i2cdev_iface_impl>(bus, addr, ten_bit_addr, timeout_ms);
}
-