LoRaWANCH341 Library
Loading...
Searching...
No Matches
Public Member Functions | Private Member Functions | Private Attributes | List of all members
LinuxSPI Class Reference

Implementation of SPIInterface using Linux spidev. More...

#include <LinuxSPI.hpp>

Inheritance diagram for LinuxSPI:
Inheritance graph
Collaboration diagram for LinuxSPI:
Collaboration graph

Public Member Functions

 LinuxSPI (const std::string &device="/dev/spidev0.0", uint32_t speed=1000000, uint8_t mode=0)
 Constructs a new LinuxSPI object.
 
 ~LinuxSPI ()
 Destroys the LinuxSPI object.
 
bool open ()
 Opens the SPI device.
 
void close ()
 Closes the SPI device.
 
std::vector< uint8_t > transfer (const std::vector< uint8_t > &write_data, size_t read_length=0)
 Transfers data over the SPI interface.
 
bool digitalWrite (uint8_t pin, bool value)
 Sets the value of a GPIO pin.
 
bool digitalRead (uint8_t pin)
 Reads the value of a GPIO pin.
 
bool pinMode (uint8_t pin, uint8_t mode)
 Sets the mode of a GPIO pin.
 
bool configureInterrupt (uint8_t pin, bool enable) override
 Configure interrupt settings for a GPIO pin.
 
bool setInterruptCallback (InterruptCallback callback) override
 Set interrupt callback.
 
bool enableInterrupt (bool enable) override
 Enable or disable interrupts.
 
bool isActive () const override
 Check if the device is active/connected.
 
- Public Member Functions inherited from SPIInterface
virtual ~SPIInterface ()=default
 

Private Member Functions

bool exportGPIO (uint8_t pin)
 Exports a GPIO pin for use.
 
bool unexportGPIO (uint8_t pin)
 Unexports a GPIO pin.
 
bool setGPIODirection (uint8_t pin, const std::string &direction)
 Sets the direction of a GPIO pin.
 
bool writeGPIOValue (uint8_t pin, bool value)
 Writes a value to a GPIO pin.
 
bool readGPIOValue (uint8_t pin)
 Reads the value of a GPIO pin.
 
void interruptThread ()
 

Private Attributes

std::string device_path
 
uint32_t speed_hz
 
uint8_t spi_mode
 
int fd
 
std::string gpio_export_path
 
std::string gpio_unexport_path
 
std::map< uint8_t, std::string > gpio_pin_paths
 
InterruptCallback interruptCallback
 
std::atomic< bool > interrupt_running
 
std::thread interrupt_thread
 
int interrupt_pin
 
InterruptCallback interrupt_callback
 
bool interrupt_enabled = false
 

Additional Inherited Members

- Public Types inherited from SPIInterface
using InterruptCallback = std::function< void(void)>
 
- Static Public Attributes inherited from SPIInterface
static constexpr uint8_t INPUT = 0
 
static constexpr uint8_t OUTPUT = 1
 
static constexpr uint8_t INPUT_PULLUP = 2
 

Detailed Description

Implementation of SPIInterface using Linux spidev.

This class provides an implementation of the SPIInterface using the spidev interface available in Linux. It allows communication with SPI devices and control of GPIO pins for interrupt handling.

Note
This class is designed to work on Linux systems with spidev support.
Parameters
deviceThe SPI device path (default: "/dev/spidev0.0").
speedThe SPI communication speed in Hz (default: 1,000,000 Hz).
modeThe SPI mode (default: 0).
Author
Sergio PĂ©rez
Date
2025

A class to interface with SPI devices on Linux systems.

This class provides methods to open, close, and transfer data over an SPI interface, as well as control GPIO pins and handle interrupts.

Constructor & Destructor Documentation

◆ LinuxSPI()

LinuxSPI::LinuxSPI ( const std::string &  device = "/dev/spidev0.0",
uint32_t  speed = 1000000,
uint8_t  mode = 0 
)

Constructs a new LinuxSPI object.

Parameters
deviceThe SPI device path (default is "/dev/spidev0.0").
speedThe SPI communication speed in Hz (default is 1,000,000 Hz).
modeThe SPI mode (default is 0).

◆ ~LinuxSPI()

LinuxSPI::~LinuxSPI ( )

Destroys the LinuxSPI object.

Here is the call graph for this function:

Member Function Documentation

◆ close()

void LinuxSPI::close ( )
virtual

Closes the SPI device.

Implements SPIInterface.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ configureInterrupt()

bool LinuxSPI::configureInterrupt ( uint8_t  pin,
bool  enable 
)
inlineoverridevirtual

Configure interrupt settings for a GPIO pin.

Parameters
pinThe pin number
enableTrue to enable the interrupt, false to disable it
Returns
True if the operation was successful, false otherwise

Implements SPIInterface.

◆ digitalRead()

bool LinuxSPI::digitalRead ( uint8_t  pin)
virtual

Reads the value of a GPIO pin.

Parameters
pinThe GPIO pin number.
Returns
The value of the GPIO pin (true for high, false for low).

Implements SPIInterface.

Here is the call graph for this function:

◆ digitalWrite()

bool LinuxSPI::digitalWrite ( uint8_t  pin,
bool  value 
)
virtual

Sets the value of a GPIO pin.

Parameters
pinThe GPIO pin number.
valueThe value to set (true for high, false for low).
Returns
true if the value was successfully set, false otherwise.

Implements SPIInterface.

Here is the call graph for this function:

◆ enableInterrupt()

bool LinuxSPI::enableInterrupt ( bool  enable)
overridevirtual

Enable or disable interrupts.

Parameters
enableTrue to enable interrupts, false to disable them
Returns
True if successful, false otherwise

Implements SPIInterface.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ exportGPIO()

bool LinuxSPI::exportGPIO ( uint8_t  pin)
private

Exports a GPIO pin for use.

Parameters
pinThe GPIO pin number.
Returns
true if the pin was successfully exported, false otherwise.
Here is the caller graph for this function:

◆ interruptThread()

void LinuxSPI::interruptThread ( )
private
Here is the call graph for this function:
Here is the caller graph for this function:

◆ isActive()

bool LinuxSPI::isActive ( ) const
inlineoverridevirtual

Check if the device is active/connected.

Returns
True if the device is active, false otherwise

Implements SPIInterface.

◆ open()

bool LinuxSPI::open ( )
virtual

Opens the SPI device.

Returns
true if the device was successfully opened, false otherwise.

Implements SPIInterface.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ pinMode()

bool LinuxSPI::pinMode ( uint8_t  pin,
uint8_t  mode 
)
virtual

Sets the mode of a GPIO pin.

Parameters
pinThe GPIO pin number.
modeThe mode to set (e.g., input or output).
Returns
true if the mode was successfully set, false otherwise.

Implements SPIInterface.

Here is the call graph for this function:

◆ readGPIOValue()

bool LinuxSPI::readGPIOValue ( uint8_t  pin)
private

Reads the value of a GPIO pin.

Parameters
pinThe GPIO pin number.
Returns
The value of the GPIO pin (true for high, false for low).
Here is the caller graph for this function:

◆ setGPIODirection()

bool LinuxSPI::setGPIODirection ( uint8_t  pin,
const std::string &  direction 
)
private

Sets the direction of a GPIO pin.

Parameters
pinThe GPIO pin number.
directionThe direction to set ("in" for input, "out" for output).
Returns
true if the direction was successfully set, false otherwise.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ setInterruptCallback()

bool LinuxSPI::setInterruptCallback ( InterruptCallback  callback)
overridevirtual

Set interrupt callback.

Parameters
callbackFunction to call when interrupt occurs
Returns
True if successful, false otherwise

Implements SPIInterface.

◆ transfer()

std::vector< uint8_t > LinuxSPI::transfer ( const std::vector< uint8_t > &  write_data,
size_t  read_length = 0 
)
virtual

Transfers data over the SPI interface.

Parameters
write_dataThe data to be written to the SPI device.
read_lengthThe number of bytes to read from the SPI device (default is 0).
Returns
A vector containing the data read from the SPI device.

Implements SPIInterface.

◆ unexportGPIO()

bool LinuxSPI::unexportGPIO ( uint8_t  pin)
private

Unexports a GPIO pin.

Parameters
pinThe GPIO pin number.
Returns
true if the pin was successfully unexported, false otherwise.
Here is the caller graph for this function:

◆ writeGPIOValue()

bool LinuxSPI::writeGPIOValue ( uint8_t  pin,
bool  value 
)
private

Writes a value to a GPIO pin.

Parameters
pinThe GPIO pin number.
valueThe value to write (true for high, false for low).
Returns
true if the value was successfully written, false otherwise.
Here is the caller graph for this function:

Member Data Documentation

◆ device_path

std::string LinuxSPI::device_path
private

◆ fd

int LinuxSPI::fd
private

◆ gpio_export_path

std::string LinuxSPI::gpio_export_path
private

◆ gpio_pin_paths

std::map<uint8_t, std::string> LinuxSPI::gpio_pin_paths
private

◆ gpio_unexport_path

std::string LinuxSPI::gpio_unexport_path
private

◆ interrupt_callback

InterruptCallback LinuxSPI::interrupt_callback
private

◆ interrupt_enabled

bool LinuxSPI::interrupt_enabled = false
private

◆ interrupt_pin

int LinuxSPI::interrupt_pin
private

◆ interrupt_running

std::atomic<bool> LinuxSPI::interrupt_running
private

◆ interrupt_thread

std::thread LinuxSPI::interrupt_thread
private

◆ interruptCallback

InterruptCallback LinuxSPI::interruptCallback
private

◆ speed_hz

uint32_t LinuxSPI::speed_hz
private

◆ spi_mode

uint8_t LinuxSPI::spi_mode
private

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