29#include <linux/spi/spidev.h>
67 throw std::runtime_error(
"Failed to open SPI device");
87 throw std::runtime_error(
"SPI device not open");
95 throw std::runtime_error(
"Failed to set SPI mode");
99 throw std::runtime_error(
"Failed to set SPI bits per word");
103 throw std::runtime_error(
"Failed to set SPI speed");
116 uint8_t tx[] = {
static_cast<uint8_t
>(
Opcode::Write), address, data};
129 uint8_t tx[] = {
static_cast<uint8_t
>(
Opcode::Read), address, 0x00};
130 uint8_t rx[
sizeof(tx)] = {0};
147 throw std::runtime_error(
"SPI device not open");
150 struct spi_ioc_transfer transfer = {};
151 transfer.tx_buf =
reinterpret_cast<unsigned long>(tx);
152 transfer.rx_buf =
reinterpret_cast<unsigned long>(rx);
153 transfer.len = length;
154 transfer.speed_hz =
speed;
155 transfer.bits_per_word =
bits;
158 throw std::runtime_error(
"SPI transfer error");
Definition of the SPIController class.
int(*)(const char *, int,...) OpenFunc
int(*)(int, unsigned long,...) IoctlFunc
static constexpr uint32_t DefaultSpeedHz
void configure(uint8_t mode, uint8_t bits, uint32_t speed) override
Configure the SPI device.
IoctlFunc m_ioctlFunc
Function pointer to the ioctl function.
SPIController(IoctlFunc ioctlFunc=::ioctl, OpenFunc openFunc=::open, CloseFunc closeFunc=::close)
Construct a new SPIController::SPIController object.
uint8_t mode
Mode of the SPI communication.
CloseFunc m_closeFunc
Function pointer to the close function.
static constexpr uint8_t DefaultMode
uint8_t readByte(uint8_t address) override
Read a byte from the SPI device.
void closeDevice() override
Close the SPI device.
int spi_fd
File descriptor of the SPI device.
void spiTransfer(const uint8_t *tx, uint8_t *rx, size_t length) override
Transfer data over SPI.
void writeByte(uint8_t address, uint8_t data) override
Write a byte to the SPI device.
bool openDevice(const std::string &device) override
Open the SPI device.
uint8_t bits
Number of bits per word.
static constexpr uint8_t DefaultBitsPerWord
uint32_t speed
Speed of the SPI communication.
OpenFunc m_openFunc
Function pointer to the open function.
~SPIController() override
Destroy the SPIController::SPIController object.