Hotwheels-Cluster 1.2
Creation of Cluster APP for SEA:ME project.
 
Loading...
Searching...
No Matches
SPIController Class Reference

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.
 
- Public Member Functions inherited from ISPIController
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
 

Detailed Description

Class that controls the SPI communication. ISPIController.

Definition at line 35 of file SPIController.hpp.

Member Enumeration Documentation

◆ Opcode

enum class SPIController::Opcode : uint8_t
strong
Enumerator
Write 
Read 

Definition at line 37 of file SPIController.hpp.

Constructor & Destructor Documentation

◆ SPIController()

SPIController::SPIController ( IoctlFunc ioctlFunc = ::ioctl,
OpenFunc openFunc = ::open,
CloseFunc closeFunc = ::close )

Construct a new SPIController::SPIController object.

Parameters
ioctlFunc
openFunc
closeFunc

This constructor initializes the SPIController object with the specified functions.

Definition at line 43 of file SPIController.cpp.

◆ ~SPIController()

SPIController::~SPIController ( )
override

Destroy the SPIController::SPIController object.

This destructor closes the SPI device.

Definition at line 54 of file SPIController.cpp.

Here is the call graph for this function:

Member Function Documentation

◆ closeDevice()

void SPIController::closeDevice ( )
overridevirtual

Close the SPI device.

This function closes the SPI device.

Implements ISPIController.

Definition at line 166 of file SPIController.cpp.

Here is the caller graph for this function:

◆ configure()

void SPIController::configure ( uint8_t mode,
uint8_t bits,
uint32_t speed )
overridevirtual

Configure the SPI device.

Parameters
modeThe SPI mode.
bitsThe number of bits per word.
speedThe speed in Hz.
Exceptions
std::runtime_errorif the device is not open.
std::runtime_errorif the SPI mode cannot be set.
std::runtime_errorif the bits per word cannot be set.
std::runtime_errorif 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.

◆ openDevice()

bool SPIController::openDevice ( const std::string & device)
overridevirtual

Open the SPI device.

Parameters
deviceThe device to open.
Returns
True if the device was opened successfully.
Exceptions
std::runtime_errorif 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.

◆ readByte()

uint8_t SPIController::readByte ( uint8_t address)
overridevirtual

Read a byte from the SPI device.

Parameters
addressThe address to read from.
Returns
The byte read from the SPI device.

This function reads a byte from the SPI device at the specified address.

Implements ISPIController.

Definition at line 128 of file SPIController.cpp.

Here is the call graph for this function:

◆ spiTransfer()

void SPIController::spiTransfer ( const uint8_t * tx,
uint8_t * rx,
size_t length )
overridevirtual

Transfer data over SPI.

Parameters
txThe data to transmit.
rxThe data to receive.
lengthThe length of the data.
Exceptions
std::runtime_errorif the SPI device is not open.
std::runtime_errorif the SPI transfer fails.

This function transfers data over SPI.

Implements ISPIController.

Definition at line 145 of file SPIController.cpp.

Here is the caller graph for this function:

◆ writeByte()

void SPIController::writeByte ( uint8_t address,
uint8_t data )
overridevirtual

Write a byte to the SPI device.

Parameters
addressThe address to write to.
dataThe 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.

Here is the call graph for this function:

Field Documentation

◆ bits

uint8_t SPIController::bits
private

Number of bits per word.

Definition at line 56 of file SPIController.hpp.

◆ DefaultBitsPerWord

uint8_t SPIController::DefaultBitsPerWord = 8
staticconstexprprivate

Definition at line 67 of file SPIController.hpp.

◆ DefaultMode

uint8_t SPIController::DefaultMode = 0
staticconstexprprivate

Definition at line 69 of file SPIController.hpp.

◆ DefaultSpeedHz

uint32_t SPIController::DefaultSpeedHz = 1'000'000
staticconstexprprivate

Definition at line 68 of file SPIController.hpp.

◆ m_closeFunc

CloseFunc SPIController::m_closeFunc
private

Function pointer to the close function.

Definition at line 65 of file SPIController.hpp.

◆ m_ioctlFunc

IoctlFunc SPIController::m_ioctlFunc
private

Function pointer to the ioctl function.

Definition at line 61 of file SPIController.hpp.

◆ m_openFunc

OpenFunc SPIController::m_openFunc
private

Function pointer to the open function.

Definition at line 63 of file SPIController.hpp.

◆ mode

uint8_t SPIController::mode
private

Mode of the SPI communication.

Definition at line 54 of file SPIController.hpp.

◆ speed

uint32_t SPIController::speed
private

Speed of the SPI communication.

Definition at line 58 of file SPIController.hpp.

◆ spi_fd

int SPIController::spi_fd
private

File descriptor of the SPI device.

Definition at line 52 of file SPIController.hpp.


The documentation for this class was generated from the following files: