distortos  v0.4.0
object-oriented C++ RTOS for microcontrollers
distortos::devices::SpiDevice Class Reference

#include <SpiDevice.hpp>

Collaboration diagram for distortos::devices::SpiDevice:
[legend]

Public Member Functions

constexpr SpiDevice (SpiMaster &spiMaster, OutputPin &slaveSelectPin, const SpiMode mode, const uint32_t maxClockFrequency, const uint8_t wordLength, const bool lsbFirst)
 SpiDevice's constructor. More...
 
 ~SpiDevice ()
 SpiDevice's destructor. More...
 
int close ()
 Closes SPI device. More...
 
std::pair< int, size_t > executeTransaction (SpiMasterOperationRange operationRange)
 Executes series of operations as a single atomic transaction. More...
 
bool getLsbFirst () const
 
uint32_t getMaxClockFrequency () const
 
SpiMode getMode () const
 
OutputPingetSlaveSelectPin () const
 
uint8_t getWordLength () const
 
bool lock ()
 Locks the object for exclusive use by current thread using condition variable and mutex. More...
 
int open ()
 Opens SPI device. More...
 
void unlock (bool previousLockState)
 Unlocks the object that was previously locked by current thread. More...
 

Private Member Functions

bool lockInternal ()
 Internal version of lock() - without locking the mutex. More...
 
void unlockInternal (bool previousLockState)
 Internal version of unlock() - without locking the mutex. More...
 

Private Attributes

ConditionVariable conditionVariable_
 condition variable used for locking access to this object More...
 
Mutex mutex_
 mutex used to serialize access to this object More...
 
uint32_t maxClockFrequency_
 max clock frequency supported by SPI slave device, Hz More...
 
const Threadowner_
 pointer to thread that locked this object More...
 
OutputPinslaveSelectPin_
 reference to slave select pin of this SPI slave device More...
 
SpiMasterspiMaster_
 reference to SPI master to which this SPI slave device is connected More...
 
bool lsbFirst_
 
SpiMode mode_
 SPI mode used by SPI slave device. More...
 
uint8_t openCount_
 number of times this device was opened but not yet closed More...
 
uint8_t wordLength_
 word length used by SPI slave device, bits More...
 

Detailed Description

SpiDevice class represents a single SPI slave device connected to SPI master

Constructor & Destructor Documentation

◆ SpiDevice()

constexpr distortos::devices::SpiDevice::SpiDevice ( SpiMaster spiMaster,
OutputPin slaveSelectPin,
const SpiMode  mode,
const uint32_t  maxClockFrequency,
const uint8_t  wordLength,
const bool  lsbFirst 
)
inline

SpiDevice's constructor.

Parameters
[in]spiMasteris a reference to SPI master to which this SPI slave device is connected
[in]slaveSelectPinis a reference to slave select pin of this SPI slave device
[in]modeis the SPI mode used by SPI slave device
[in]maxClockFrequencyis the max clock frequency supported by SPI slave device, Hz
[in]wordLengthis the word length used by SPI slave device, bits
[in]lsbFirstselects whether data should be transmitted/received to/from the SPI slave device with MSB (false) or LSB (true) first
Here is the call graph for this function:

◆ ~SpiDevice()

distortos::devices::SpiDevice::~SpiDevice ( )

SpiDevice's destructor.

Does nothing if all users already closed this device. If they did not, performs forced close of device.

Warning
This function must not be called from interrupt context!
Here is the call graph for this function:
Here is the caller graph for this function:

Member Function Documentation

◆ close()

int distortos::devices::SpiDevice::close ( )

Closes SPI device.

Does nothing if any user still has this device opened. Otherwise low-level driver is stopped.

Warning
This function must not be called from interrupt context!
Returns
0 on success, error code otherwise:
Here is the call graph for this function:
Here is the caller graph for this function:

◆ executeTransaction()

std::pair< int, size_t > distortos::devices::SpiDevice::executeTransaction ( SpiMasterOperationRange  operationRange)

Executes series of operations as a single atomic transaction.

Wrapper for SpiMaster::executeTransaction().

Warning
This function must not be called from interrupt context!
Parameters
[in]operationRangeis the range of operations that will be executed
Returns
pair with return code (0 on success, error code otherwise) and number of successfully completed operations from operationRange; error codes:
Here is the call graph for this function:
Here is the caller graph for this function:

◆ getLsbFirst()

bool distortos::devices::SpiDevice::getLsbFirst ( ) const
inline
Returns
false if data should be transmitted/received to/from the SPI slave device with MSB first, true if data should be transmitted/received to/from the SPI slave device with LSB first
Here is the caller graph for this function:

◆ getMaxClockFrequency()

uint32_t distortos::devices::SpiDevice::getMaxClockFrequency ( ) const
inline
Returns
max clock frequency supported by SPI slave device, Hz
Here is the caller graph for this function:

◆ getMode()

SpiMode distortos::devices::SpiDevice::getMode ( ) const
inline
Returns
SPI mode used by SPI slave device
Here is the caller graph for this function:

◆ getSlaveSelectPin()

OutputPin& distortos::devices::SpiDevice::getSlaveSelectPin ( ) const
inline
Returns
reference to slave select pin of this SPI slave device
Here is the caller graph for this function:

◆ getWordLength()

uint8_t distortos::devices::SpiDevice::getWordLength ( ) const
inline
Returns
word length used by SPI slave device, bits
Here is the call graph for this function:
Here is the caller graph for this function:

◆ lock()

bool distortos::devices::SpiDevice::lock ( )

Locks the object for exclusive use by current thread using condition variable and mutex.

When the object is locked, any call to any member function from other thread will be blocked until the object is unlocked. Locking is optional, but may be useful when more than one transaction must be done atomically.

Note
Locks may be nested.
Warning
This function must not be called from interrupt context!
Returns
previous state of lock: false if this SPI device was unlocked before this call, true if it was already locked by current thread
Here is the call graph for this function:
Here is the caller graph for this function:

◆ lockInternal()

bool distortos::devices::SpiDevice::lockInternal ( )
private

Internal version of lock() - without locking the mutex.

Returns
previous state of lock: false if this SPI device was unlocked before this call, true if it was already locked by current thread
Here is the call graph for this function:
Here is the caller graph for this function:

◆ open()

int distortos::devices::SpiDevice::open ( )

Opens SPI device.

Does nothing if any user already has this device opened. Otherwise associated SPI master is opened.

Warning
This function must not be called from interrupt context!
Returns
0 on success, error code otherwise:
  • EMFILE - this device is already opened too many times;
  • error codes returned by SpiMaster::open();
Here is the call graph for this function:
Here is the caller graph for this function:

◆ unlock()

void distortos::devices::SpiDevice::unlock ( bool  previousLockState)

Unlocks the object that was previously locked by current thread.

Does nothing if SPI device is not locked by current thread.

Note
Locks may be nested.
Warning
This function must not be called from interrupt context!
Parameters
previousLockStateis the value returned by matching call to lock()
Here is the call graph for this function:
Here is the caller graph for this function:

◆ unlockInternal()

void distortos::devices::SpiDevice::unlockInternal ( bool  previousLockState)
private

Internal version of unlock() - without locking the mutex.

Does nothing if SPI device is not locked by current thread.

Parameters
previousLockStateis the value returned by matching call to lockInternal()
Here is the call graph for this function:
Here is the caller graph for this function:

Member Data Documentation

◆ conditionVariable_

ConditionVariable distortos::devices::SpiDevice::conditionVariable_
private

condition variable used for locking access to this object

◆ lsbFirst_

bool distortos::devices::SpiDevice::lsbFirst_
private

selects whether data should be transmitted/received to/from the SPI slave device with MSB (false) or LSB (true) first

◆ maxClockFrequency_

uint32_t distortos::devices::SpiDevice::maxClockFrequency_
private

max clock frequency supported by SPI slave device, Hz

◆ mode_

SpiMode distortos::devices::SpiDevice::mode_
private

SPI mode used by SPI slave device.

◆ mutex_

Mutex distortos::devices::SpiDevice::mutex_
private

mutex used to serialize access to this object

◆ openCount_

uint8_t distortos::devices::SpiDevice::openCount_
private

number of times this device was opened but not yet closed

◆ owner_

const Thread* distortos::devices::SpiDevice::owner_
private

pointer to thread that locked this object

◆ slaveSelectPin_

OutputPin& distortos::devices::SpiDevice::slaveSelectPin_
private

reference to slave select pin of this SPI slave device

◆ spiMaster_

SpiMaster& distortos::devices::SpiDevice::spiMaster_
private

reference to SPI master to which this SPI slave device is connected

◆ wordLength_

uint8_t distortos::devices::SpiDevice::wordLength_
private

word length used by SPI slave device, bits


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