Class that controls the SPI communication. ISPIController. More...
#include <SPIController.hpp>
Public Types | |
enum class | Opcode : uint8_t { Write = 0x02 , Read = 0x03 } |
Public Member Functions | |
SPIController (IoctlFunc ioctlFunc=::ioctl, OpenFunc openFunc=::open, CloseFunc closeFunc=::close) | |
Construct a new SPIController::SPIController object. | |
~SPIController () override | |
Destroy the SPIController::SPIController object. | |
bool | openDevice (const std::string &device) override |
Open the SPI device. | |
void | configure (uint8_t mode, uint8_t bits, uint32_t speed) override |
Configure the SPI device. | |
void | writeByte (uint8_t address, uint8_t data) override |
Write a byte to the SPI device. | |
uint8_t | readByte (uint8_t address) override |
Read a byte from the SPI device. | |
void | spiTransfer (const uint8_t *tx, uint8_t *rx, size_t length) override |
Transfer data over SPI. | |
void | closeDevice () override |
Close the SPI device. | |
![]() | |
virtual | ~ISPIController ()=default |
Private Attributes | |
int | spi_fd |
File descriptor of the SPI device. | |
uint8_t | mode |
Mode of the SPI communication. | |
uint8_t | bits |
Number of bits per word. | |
uint32_t | speed |
Speed of the SPI communication. | |
IoctlFunc | m_ioctlFunc |
Function pointer to the ioctl function. | |
OpenFunc | m_openFunc |
Function pointer to the open function. | |
CloseFunc | m_closeFunc |
Function pointer to the close function. | |
Static Private Attributes | |
static constexpr uint8_t | DefaultBitsPerWord = 8 |
static constexpr uint32_t | DefaultSpeedHz = 1'000'000 |
static constexpr uint8_t | DefaultMode = 0 |
Class that controls the SPI communication. ISPIController.
Definition at line 35 of file SPIController.hpp.
|
strong |
Enumerator | |
---|---|
Write | |
Read |
Definition at line 37 of file SPIController.hpp.
SPIController::SPIController | ( | IoctlFunc | ioctlFunc = ::ioctl, |
OpenFunc | openFunc = ::open, | ||
CloseFunc | closeFunc = ::close ) |
Construct a new SPIController::SPIController object.
ioctlFunc | |
openFunc | |
closeFunc |
This constructor initializes the SPIController object with the specified functions.
Definition at line 43 of file SPIController.cpp.
|
override |
Destroy the SPIController::SPIController object.
This destructor closes the SPI device.
Definition at line 54 of file SPIController.cpp.
|
overridevirtual |
Close the SPI device.
This function closes the SPI device.
Implements ISPIController.
Definition at line 166 of file SPIController.cpp.
|
overridevirtual |
Configure the SPI device.
mode | The SPI mode. |
bits | The number of bits per word. |
speed | The speed in Hz. |
std::runtime_error | if the device is not open. |
std::runtime_error | if the SPI mode cannot be set. |
std::runtime_error | if the bits per word cannot be set. |
std::runtime_error | if the speed cannot be set. |
This function configures the SPI device with the specified mode, bits per word, and speed.
Implements ISPIController.
Definition at line 85 of file SPIController.cpp.
|
overridevirtual |
Open the SPI device.
device | The device to open. |
std::runtime_error | if the device cannot be opened. |
This function opens the SPI device with the specified device name.
Implements ISPIController.
Definition at line 64 of file SPIController.cpp.
|
overridevirtual |
Read a byte from the SPI device.
address | The address to read from. |
This function reads a byte from the SPI device at the specified address.
Implements ISPIController.
Definition at line 128 of file SPIController.cpp.
|
overridevirtual |
Transfer data over SPI.
tx | The data to transmit. |
rx | The data to receive. |
length | The length of the data. |
std::runtime_error | if the SPI device is not open. |
std::runtime_error | if the SPI transfer fails. |
This function transfers data over SPI.
Implements ISPIController.
Definition at line 145 of file SPIController.cpp.
|
overridevirtual |
Write a byte to the SPI device.
address | The address to write to. |
data | The data to write. |
This function writes a byte to the SPI device at the specified address.
Implements ISPIController.
Definition at line 115 of file SPIController.cpp.
|
private |
Number of bits per word.
Definition at line 56 of file SPIController.hpp.
|
staticconstexprprivate |
Definition at line 67 of file SPIController.hpp.
|
staticconstexprprivate |
Definition at line 69 of file SPIController.hpp.
|
staticconstexprprivate |
Definition at line 68 of file SPIController.hpp.
|
private |
Function pointer to the close function.
Definition at line 65 of file SPIController.hpp.
|
private |
Function pointer to the ioctl function.
Definition at line 61 of file SPIController.hpp.
|
private |
Function pointer to the open function.
Definition at line 63 of file SPIController.hpp.
|
private |
Mode of the SPI communication.
Definition at line 54 of file SPIController.hpp.
|
private |
Speed of the SPI communication.
Definition at line 58 of file SPIController.hpp.
|
private |
File descriptor of the SPI device.
Definition at line 52 of file SPIController.hpp.