distortos  v0.4.0
object-oriented C++ RTOS for microcontrollers
distortos::chip::ChipSpiMasterLowLevel Class Reference

#include <ChipSpiMasterLowLevel.hpp>

Inheritance diagram for distortos::chip::ChipSpiMasterLowLevel:
[legend]
Collaboration diagram for distortos::chip::ChipSpiMasterLowLevel:
[legend]

Classes

class  Parameters
 parameters for construction of SPI master low-level drivers More...
 

Public Member Functions

constexpr ChipSpiMasterLowLevel (const Parameters &parameters)
 ChipSpiMasterLowLevel's constructor. More...
 
 ~ChipSpiMasterLowLevel () override
 SpiMasterLowLevel's destructor. More...
 
std::pair< int, uint32_t > configure (devices::SpiMode mode, uint32_t clockFrequency, uint8_t wordLength, bool lsbFirst) override
 Configures parameters of low-level SPI master driver. More...
 
void interruptHandler ()
 Interrupt handler. More...
 
int start (devices::SpiMasterBase &spiMasterBase) override
 Starts low-level SPI master driver. More...
 
int startTransfer (const void *writeBuffer, void *readBuffer, size_t size) override
 Starts asynchronous transfer. More...
 
int stop () override
 Stops low-level SPI master driver. More...
 
- Public Member Functions inherited from distortos::devices::SpiMasterLowLevel
virtual ~SpiMasterLowLevel ()=0
 SpiMasterLowLevel's destructor. More...
 

Private Member Functions

bool isStarted () const
 
bool isTransferInProgress () const
 

Private Attributes

const Parametersparameters_
 reference to configuration parameters More...
 
devices::SpiMasterBasespiMasterBase_
 pointer to SpiMasterBase object associated with this one More...
 
uint8_t *volatile readBuffer_
 buffer to which the data is being written, nullptr to ignore received data More...
 
const uint8_t *volatile writeBuffer_
 buffer with data that is being transmitted, nullptr to send dummy data More...
 
volatile size_t size_
 size of transfer (size of readBuffer_ and/or writeBuffer_), bytes More...
 
volatile size_t readPosition_
 current position in readBuffer_ More...
 
volatile size_t writePosition_
 current position in writeBuffer_ More...
 
devices::SpiMasterErrorSet errorSet_
 current set of detected errors More...
 

Detailed Description

ChipSpiMasterLowLevel class is a low-level SPI master driver for SPIv1 in STM32

Constructor & Destructor Documentation

◆ ChipSpiMasterLowLevel()

constexpr distortos::chip::ChipSpiMasterLowLevel::ChipSpiMasterLowLevel ( const Parameters parameters)
inlineexplicit

ChipSpiMasterLowLevel's constructor.

Parameters
[in]parametersis a reference to object with peripheral parameters
Here is the call graph for this function:

◆ ~ChipSpiMasterLowLevel()

distortos::chip::ChipSpiMasterLowLevel::~ChipSpiMasterLowLevel ( )
override

SpiMasterLowLevel's destructor.

Does nothing if driver is already stopped. If it's not, performs forced stop of operation.

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

Member Function Documentation

◆ configure()

std::pair< int, uint32_t > distortos::chip::ChipSpiMasterLowLevel::configure ( devices::SpiMode  mode,
uint32_t  clockFrequency,
uint8_t  wordLength,
bool  lsbFirst 
)
overridevirtual

Configures parameters of low-level SPI master driver.

Parameters
[in]modeis the desired SPI mode
[in]clockFrequencyis the desired clock frequency, Hz
[in]wordLengthselects word length, bits, {8, 16}
[in]lsbFirstselects whether MSB (false) or LSB (true) is transmitted first
Returns
pair with return code (0 on success, error code otherwise) and real clock frequency; error codes:
  • EBADF - the driver is not started;
  • EBUSY - transfer is in progress;
  • EINVAL - selected SPI mode and/or clock frequency and/or format are invalid;

Implements distortos::devices::SpiMasterLowLevel.

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

◆ interruptHandler()

void distortos::chip::ChipSpiMasterLowLevel::interruptHandler ( )

Interrupt handler.

This code handles only one SPI event. If there are more events to handle, NVIC controller will keep the interrupt pending, so it will be executed again immediately. Thanks to ARM's tail-chaining the time between exiting previous instance and entering next instance is very short - just 6 core cycles, which is less than any manual loop in the code. Speed gain is not significant (around 1-2%, depending on SPI clock frequency) and not linear (for very high frequencies of SPI's clock the code with loop is significantly faster), but this version has two major advantages:

  • the code is simpler (and shorter),
  • the code with loop doesn't work for some SPI clock frequencies (with divider == 8 almost all transfers fail due to overflow).
Note
this must not be called by user code
Here is the call graph for this function:
Here is the caller graph for this function:

◆ isStarted()

bool distortos::chip::ChipSpiMasterLowLevel::isStarted ( ) const
inlineprivate
Returns
true if driver is started, false otherwise
Here is the caller graph for this function:

◆ isTransferInProgress()

bool distortos::chip::ChipSpiMasterLowLevel::isTransferInProgress ( ) const
inlineprivate
Returns
true if transfer is in progress, false otherwise
Here is the caller graph for this function:

◆ start()

int distortos::chip::ChipSpiMasterLowLevel::start ( devices::SpiMasterBase spiMasterBase)
overridevirtual

Starts low-level SPI master driver.

Parameters
[in]spiMasterBaseis a reference to SpiMasterBase object that will be associated with this one
Returns
0 on success, error code otherwise:
  • EBADF - the driver is not stopped;

Implements distortos::devices::SpiMasterLowLevel.

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

◆ startTransfer()

int distortos::chip::ChipSpiMasterLowLevel::startTransfer ( const void *  writeBuffer,
void *  readBuffer,
size_t  size 
)
overridevirtual

Starts asynchronous transfer.

This function returns immediately. When the transfer is physically finished (either expected number of bytes were written and read or an error was detected), SpiMasterBase::transferCompleteEvent() will be executed.

Parameters
[in]writeBufferis the buffer with data that will be written, nullptr to send dummy data
[out]readBufferis the buffer with data that will be read, nullptr to ignore received data
[in]sizeis the size of transfer (size of writeBuffer and/or readBuffer), bytes, must be even if number of data bits is in range (8; 16], divisible by 3 if number of data bits is in range (16; 24] or divisible by 4 if number of data bits is in range (24; 32]
Returns
0 on success, error code otherwise:
  • EBADF - the driver is not started;
  • EBUSY - transfer is in progress;
  • EINVAL - size is invalid;

Implements distortos::devices::SpiMasterLowLevel.

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

◆ stop()

int distortos::chip::ChipSpiMasterLowLevel::stop ( )
overridevirtual

Stops low-level SPI master driver.

Returns
0 on success, error code otherwise:
  • EBADF - the driver is not started;
  • EBUSY - transfer is in progress;

Implements distortos::devices::SpiMasterLowLevel.

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

Member Data Documentation

◆ errorSet_

devices::SpiMasterErrorSet distortos::chip::ChipSpiMasterLowLevel::errorSet_
private

current set of detected errors

◆ parameters_

const Parameters& distortos::chip::ChipSpiMasterLowLevel::parameters_
private

reference to configuration parameters

◆ readBuffer_

uint8_t* volatile distortos::chip::ChipSpiMasterLowLevel::readBuffer_
private

buffer to which the data is being written, nullptr to ignore received data

◆ readPosition_

volatile size_t distortos::chip::ChipSpiMasterLowLevel::readPosition_
private

current position in readBuffer_

◆ size_

volatile size_t distortos::chip::ChipSpiMasterLowLevel::size_
private

size of transfer (size of readBuffer_ and/or writeBuffer_), bytes

◆ spiMasterBase_

devices::SpiMasterBase* distortos::chip::ChipSpiMasterLowLevel::spiMasterBase_
private

pointer to SpiMasterBase object associated with this one

◆ writeBuffer_

const uint8_t* volatile distortos::chip::ChipSpiMasterLowLevel::writeBuffer_
private

buffer with data that is being transmitted, nullptr to send dummy data

◆ writePosition_

volatile size_t distortos::chip::ChipSpiMasterLowLevel::writePosition_
private

current position in writeBuffer_


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