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

#include <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...
 
int close ()
 Closes SPI master. More...
 
std::pair< int, size_t > executeTransaction (const SpiDevice &device, SpiMasterOperationRange operationRange)
 Executes series of operations as a single atomic transaction. More...
 
int open ()
 Opens SPI master. More...
 

Private Member Functions

void notifyWaiter (int ret={})
 Notifies waiting thread about completion of transaction. More...
 
void transferCompleteEvent (SpiMasterErrorSet errorSet, size_t bytesTransfered) override
 "Transfer complete" event More...
 
- Private Member Functions inherited from distortos::devices::SpiMasterBase
virtual ~SpiMasterBase ()=0
 SpiMasterBase's destructor. More...
 

Private Attributes

Mutex mutex_
 mutex used to serialize access to this object More...
 
SpiMasterOperationRange operationRange_
 range of operations that are part of currently handled transaction More...
 
volatile int ret_
 error codes detected in transferCompleteEvent() 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...
 

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
Here is the call graph for this function:

◆ ~SpiMaster()

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

SpiMaster'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::SpiMaster::close ( )

Closes SPI master.

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::SpiMaster::executeTransaction ( const SpiDevice device,
SpiMasterOperationRange  operationRange 
)

Executes series of operations as a single atomic transaction.

First SPI is configured to match parameters of SPI device (clock frequency, mode, format, ...). Then the device is selected and the operations are executed. The transaction is finished when all operations are complete or when any error is detected - in either case the device is unselected and this function returns.

Warning
This function must not be called from interrupt context!
Parameters
[in]deviceis a reference to SPI device which is the target of the transaction
[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:

◆ notifyWaiter()

void distortos::devices::SpiMaster::notifyWaiter ( int  ret = {})
private

Notifies waiting thread about completion of transaction.

Parameters
[in]retis the last error code returned by transaction handling code, default - 0
Here is the caller graph for this function:

◆ open()

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

Opens SPI master.

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

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:

◆ transferCompleteEvent()

void distortos::devices::SpiMaster::transferCompleteEvent ( SpiMasterErrorSet  errorSet,
size_t  bytesTransfered 
)
overrideprivatevirtual

"Transfer complete" event

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

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

Parameters
[in]errorSetis the set of error bits
[in]bytesTransferedis the number of bytes transfered by low-level SPI master driver (read from write buffer and/or written to read buffer), may be unreliable if errorSet is not empty (i.e. transfer error was detected)

Implements distortos::devices::SpiMasterBase.

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

◆ operationRange_

SpiMasterOperationRange distortos::devices::SpiMaster::operationRange_
private

range of operations that are part of currently handled transaction

◆ ret_

volatile int distortos::devices::SpiMaster::ret_
private

error codes detected in transferCompleteEvent()

◆ 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


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