distortos  v0.7.0
object-oriented C++ RTOS for microcontrollers
distortos::devices::SpiMaster Class Reference

#include "distortos/devices/communication/SpiMaster.hpp"

Inheritance diagram for distortos::devices::SpiMaster:
[legend]
Collaboration diagram for distortos::devices::SpiMaster:
[legend]

Public Member Functions

constexpr SpiMaster (SpiMasterLowLevel &spiMaster)
 SpiMaster's constructor. More...
 
 ~SpiMaster () override
 SpiMaster's destructor. More...
 

Private Member Functions

void close ()
 Closes SPI master. More...
 
void configure (SpiMode mode, uint32_t clockFrequency, uint8_t wordLength, bool lsbFirst, uint32_t dummyData) const
 Configures parameters of SPI master. More...
 
int executeTransaction (SpiMasterTransfersRange transfersRange)
 Executes series of transfers as a single atomic transaction. More...
 
void lock ()
 Locks SPI master for exclusive use by current thread. More...
 
void notifyWaiter (bool success)
 Notifies waiting thread about completion of transaction. More...
 
int open ()
 Opens SPI master. More...
 
void transferCompleteEvent (bool success) override
 "Transfer complete" event More...
 
void unlock ()
 Unlocks SPI master which was previously locked by current thread. More...
 
- Private Member Functions inherited from distortos::devices::SpiMasterBase
virtual ~SpiMasterBase ()=default
 SpiMasterBase's destructor. More...
 

Private Attributes

Mutex mutex_
 mutex used to serialize access to this object More...
 
SpiMasterTransfersRange transfersRange_
 range of transfers that are part of currently handled transaction More...
 
Semaphore *volatile semaphore_
 pointer to semaphore used to notify waiting thread about completion of transaction More...
 
SpiMasterLowLevelspiMaster_
 reference to low-level implementation of SpiMasterLowLevel interface More...
 
uint8_t openCount_
 number of times this device was opened but not yet closed More...
 
volatile bool success_
 tells whether the transaction was successful (true) or not (false) More...
 

Friends

class SpiMasterHandle
 

Detailed Description

SpiMaster class is a driver for SPI master

Constructor & Destructor Documentation

◆ SpiMaster()

constexpr distortos::devices::SpiMaster::SpiMaster ( SpiMasterLowLevel spiMaster)
inlineexplicit

SpiMaster's constructor.

Parameters
[in]spiMasteris a reference to low-level implementation of SpiMasterLowLevel interface

◆ ~SpiMaster()

distortos::devices::SpiMaster::~SpiMaster ( )
override

SpiMaster's destructor.

Precondition
Device is closed.

Member Function Documentation

◆ close()

void distortos::devices::SpiMaster::close ( )
private

Closes SPI master.

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

Precondition
Device is opened.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ configure()

void distortos::devices::SpiMaster::configure ( SpiMode  mode,
uint32_t  clockFrequency,
uint8_t  wordLength,
bool  lsbFirst,
uint32_t  dummyData 
) const
private

Configures parameters of SPI master.

Precondition
Device is opened.
clockFrequency and wordLength are valid for associated low-level implementation of SpiMasterLowLevel interface.
Parameters
[in]modeis the desired SPI mode
[in]clockFrequencyis the desired clock frequency, Hz
[in]wordLengthselects word length, bits
[in]lsbFirstselects whether MSB (false) or LSB (true) is transmitted first
[in]dummyDatais the dummy data that will be sent if write buffer of transfer is nullptr
Here is the call graph for this function:
Here is the caller graph for this function:

◆ executeTransaction()

int distortos::devices::SpiMaster::executeTransaction ( SpiMasterTransfersRange  transfersRange)
private

Executes series of transfers as a single atomic transaction.

The transaction is finished when all transfers are complete or when any error is detected.

Warning
This function must not be called from interrupt context!
Precondition
Device is opened.
transfersRange has at least one transfer.
Parameters
[in]transfersRangeis the range of transfers that will be executed
Returns
0 on success, error code otherwise:
  • EIO - failure detected by low-level SPI master driver;
Here is the call graph for this function:
Here is the caller graph for this function:

◆ lock()

void distortos::devices::SpiMaster::lock ( )
private

Locks SPI master for exclusive use by current thread.

Note
Locks are recursive.
Warning
This function must not be called from interrupt context!
Precondition
The number of recursive locks of device is less than 65535.
Postcondition
Device is locked.
Here is the call graph for this function:

◆ notifyWaiter()

void distortos::devices::SpiMaster::notifyWaiter ( bool  success)
private

Notifies waiting thread about completion of transaction.

Parameters
[in]successtells whether the transaction was successful (true) or not (false)
Here is the caller graph for this function:

◆ open()

int distortos::devices::SpiMaster::open ( )
private

Opens SPI master.

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

Precondition
The number of times the device is opened is less than 255.
Returns
0 on success, error code otherwise:
Here is the call graph for this function:
Here is the caller graph for this function:

◆ transferCompleteEvent()

void distortos::devices::SpiMaster::transferCompleteEvent ( bool  success)
overrideprivatevirtual

"Transfer complete" event

Called by low-level SPI master driver when the transfer is physically finished.

Handles the next transfer from the currently handled transaction. If there are no more transfers, waiting thread is notified about completion of transaction.

Parameters
[in]successtells whether the transfer was successful (true) or not (false)

Implements distortos::devices::SpiMasterBase.

Here is the call graph for this function:

◆ unlock()

void distortos::devices::SpiMaster::unlock ( )
private

Unlocks SPI master which was previously locked by current thread.

Note
Locks are recursive.
Warning
This function must not be called from interrupt context!
Precondition
This function is called by the thread that locked the device.
Here is the call graph for this function:
Here is the caller graph for this function:

Member Data Documentation

◆ mutex_

Mutex distortos::devices::SpiMaster::mutex_
private

mutex used to serialize access to this object

◆ openCount_

uint8_t distortos::devices::SpiMaster::openCount_
private

number of times this device was opened but not yet closed

◆ semaphore_

Semaphore* volatile distortos::devices::SpiMaster::semaphore_
private

pointer to semaphore used to notify waiting thread about completion of transaction

◆ spiMaster_

SpiMasterLowLevel& distortos::devices::SpiMaster::spiMaster_
private

reference to low-level implementation of SpiMasterLowLevel interface

◆ success_

volatile bool distortos::devices::SpiMaster::success_
private

tells whether the transaction was successful (true) or not (false)

◆ transfersRange_

SpiMasterTransfersRange distortos::devices::SpiMaster::transfersRange_
private

range of transfers that are part of currently handled transaction


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