From 01ddd50e55fae3382c1300616747fc9aebb39523 Mon Sep 17 00:00:00 2001 From: Mark Meserve Date: Fri, 1 Sep 2017 15:11:50 -0700 Subject: mpm: spi: Added 16-bit SPI transactions - Adds convenience method to read 16-bit SPI transactions - Fixes peek16 in spi_regs_iface to actually use 16-bit transactions --- mpm/lib/spi/spidev_iface.cpp | 28 ++++++++++++++++++++++++++-- 1 file changed, 26 insertions(+), 2 deletions(-) (limited to 'mpm/lib/spi/spidev_iface.cpp') diff --git a/mpm/lib/spi/spidev_iface.cpp b/mpm/lib/spi/spidev_iface.cpp index 74cb6bc88..522238e19 100644 --- a/mpm/lib/spi/spidev_iface.cpp +++ b/mpm/lib/spi/spidev_iface.cpp @@ -89,11 +89,35 @@ public: )); } - // Assumes that only a single byte is being read. - // TODO the function does not advertise this. Should probably fix. return uint32_t(rx[2]); } + uint32_t transfer24_16( + const uint32_t data_ + ) { + int ret(0); + + uint32_t data = data_; + uint8_t *tx_data = reinterpret_cast(&data); + + // Create tx and rx buffers: + uint8_t tx[] = {tx_data[2], tx_data[1], tx_data[0]}; // FIXME guarantee endianness + uint8_t rx[3]; // Buffer length must match tx buffer + + if (transfer( + _fd, + &tx[0], &rx[0], + 3, + _speed, _bits, _delay + ) != 0) { + throw mpm::runtime_error(str( + boost::format("SPI Transaction failed!") + )); + } + + return uint32_t(rx[1] << 8 | rx[2]); + } + private: int _fd; const uint32_t _mode; -- cgit v1.2.3