mpm: i2c: Add vector-based transfer function

This could lead to a less-restricted implementation for use in Python.
This commit is contained in:
Alex Williams 2018-10-16 08:23:35 -07:00 committed by Brent Stapleton
parent a830fab2a0
commit 5e548c1a08
3 changed files with 41 additions and 0 deletions

View File

@ -9,6 +9,7 @@
#include <boost/noncopyable.hpp>
#include <memory>
#include <string>
#include <vector>
namespace mpm { namespace i2c {
@ -39,6 +40,17 @@ namespace mpm { namespace i2c {
* \param rx_len Number of bytes to read
*/
virtual int transfer(uint8_t *tx, size_t tx_len, uint8_t *rx, size_t rx_len) = 0;
/*!
* \param tx Buffer of data to send
* \param rx Buffer to hold read data
*
* All data in tx will be transmitted.
* The amount of data read will be determined by the number of elements
* in the rx vector. Those elements will be overwritten with the data.
* Use the resize() function for a new rx vector.
*/
virtual int transfer(std::vector<uint8_t> *tx, std::vector<uint8_t> *rx) = 0;
};
}}; /* namespace mpm::i2c */

View File

@ -13,6 +13,17 @@ void export_i2c() {
LIBMPM_BOOST_PREAMBLE("i2c")
bp::def("make_i2cdev_regs_iface", &mpm::i2c::make_i2cdev_regs_iface);
/*
bp::def("make_i2cdev", &mpm::i2c::i2c_iface::make_i2cdev);
int (mpm::i2c::i2c_iface::*transfer_vec)(std::vector<uint8_t>*,
std::vector<uint8_t>*) =
&mpm::i2c::i2c_iface::transfer;
bp::class_<mpm::i2c::i2c_iface, boost::noncopyable,
std::shared_ptr<mpm::i2c::i2c_iface> >("i2c_iface", bp::no_init)
.def("transfer", transfer_vec)
;
*/
}

View File

@ -72,6 +72,24 @@ public:
return ret;
}
int transfer(std::vector<uint8_t> *tx, std::vector<uint8_t> *rx)
{
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();
}
if (rx) {
rx_data = rx->data();
rx_len = rx->size();
}
int ret = transfer(tx_data, tx_len, rx_data, rx_len);
return ret;
}
private:
int _fd;
const uint16_t _addr;