Arduino Core for STM32  1.0
HardwareSPI Class Reference

This class is for handling SPI periphery. More...

#include <HardwareSPI.h>

Inheritance diagram for HardwareSPI:
Collaboration diagram for HardwareSPI:

Public Member Functions

 HardwareSPI (SPI_HandleTypeDef &hspi)
 Constructor. More...
 
 ~HardwareSPI ()
 Destructor. More...
 
bool begin (SPI_InitTypeDef &initStruct)
 Initialize SPI periphery and it's pins. More...
 
bool begin ()
 Initialize SPI periphery and it's pins. More...
 
bool end ()
 Deinitialization and disabling SPI periphery, it can be used for "sleep mode". More...
 
bool isSending ()
 Checks if data are sending or transferring right now using DMA or IT and if periphery is busy. More...
 
bool isReceiving ()
 Checks if data are receiving or transferring right now using DMA or IT and if periphery is busy. More...
 
bool isTransfering ()
 Checks if data are transferring right now using DMA or IT and if periphery is busy. More...
 
bool isTransacting ()
 Checks if data are sending, receiving, transferring right now using DMA or IT and if periphery is busy. More...
 
bool instanceExists (void)
 Checks if SPI instance exists. More...
 
bool connectDMAtoRx (DMA_HandleTypeDef &DMA_handle)
 Use this method to connect DMA channel to SPI Rx programmatically. More...
 
bool connectDMAtoTx (DMA_HandleTypeDef &DMA_handle)
 Use this method to connect DMA channel to SPI Tx programmatically. More...
 
bool disconnectDMAfromRx ()
 Disconnects DMA channel from SPI Rx. More...
 
bool disconnectDMAfromTx ()
 Disconnects DMA channel from SPI Tx. More...
 
bool isDMAconnectedToRx (void)
 Checks if DMA channel is connected to SPI Rx and can be used for data receiving/transferring. More...
 
bool isDMAconnectedToTx (void)
 Checks if DMA channel is connected to SPI Tx and can be used for data writing/transferring. More...
 
uint32_t getSPIClockFreq (void)
 Gets periphery SPI Clock Frequency in Hz. More...
 
bool setMode (uint32_t mode)
 Sets Master or Slave mode. More...
 
uint32_t getMode ()
 Gets, if periphery mode is Master or Slave. More...
 
bool setHwCS (bool HardwareCS, bool OutputCS=true)
 Sets if Chip Select pin is handled by hardware or by software (calling digitalWrite()). More...
 
bool isHwCS ()
 Gets, if Chip Select is handled by hardware. More...
 
bool isHwOutputCS ()
 Gets, if Chip Select handled by hardware is set to output mode. More...
 
bool setBitOrder (uint8_t bitOrder)
 Sets bit order to LSB or MSB. More...
 
uint8_t getBitOrder ()
 Gets, if bit order is set to LSB or MSB. More...
 
bool setDataMode (uint8_t dataMode)
 Sets data mode. More...
 
uint8_t getDataMode ()
 Gets, what data mode is set. More...
 
bool setFrequency (uint32_t freq)
 Calculates SPI clock divider from frequency and sets it. More...
 
uint32_t getFrequency ()
 Calculates SPI frequency from APB frequency and Clock Divider. More...
 
bool setClockDivider (uint32_t clockDiv)
 Sets SPI clock divider. More...
 
uint32_t getClockDivider ()
 Gets clock divider. More...
 
bool setDataBits (uint16_t bits)
 Sets SPI data width in bits. More...
 
uint16_t getDataBits ()
 Gets set data width in bits. More...
 
bool enableTImode (bool enable)
 Enables or disabled TI mode. More...
 
bool isTImodeEnabled ()
 Gets if TI mode is enabled. More...
 
bool enableCRC (bool enable)
 Enables CRC. More...
 
bool isCRCenabled ()
 Gets if CRC is enabled. More...
 
bool setCRClength (SPI_CRCLength length)
 Sets CRC length. More...
 
SPI_CRCLength getCRClength ()
 Gets CRC length. More...
 
bool setCRCPolynomial (uint32_t polynomial)
 Sets CRC polynomial. More...
 
uint32_t getCRCPolynomial ()
 Gets CRC polynomial. More...
 
void setTimeout (uint32_t Timeout)
 Sets timeout in milliseconds. More...
 
uint32_t getTimeout ()
 Gets timeout in milliseconds. More...
 
virtual SPI_Status beginTransaction (SPISettings settings)
 Sets some settings to SPI periphery before transaction. More...
 
void flushRX ()
 Clears RX FIFO buffer. More...
 
bool flush ()
 Waits until DMA or IT (nonblocking transaction methods) are timed out and then aborts them. More...
 
virtual uint16_t transfer (uint16_t data, bool lastByte=false)
 SPI transfer is based on a simultaneous send and receive data. More...
 
virtual uint16_t transfer16 (uint16_t data, bool lastByte=false)
 SPI transfer is based on a simultaneous send and receive data. More...
 
virtual uint32_t transfer32 (uint32_t data, bool lastByte=false)
 SPI transfer is based on a simultaneous send and receive data. More...
 
virtual bool transfer (void *buf, uint16_t count, bool lastByte=false)
 SPI transfer is based on a simultaneous send and receive data. More...
 
virtual bool write (uint16_t data, bool lastByte=false)
 Sends data using SPI periphery. More...
 
virtual bool write16 (uint16_t data, bool lastByte=false)
 Sends data using SPI periphery. More...
 
virtual bool write32 (uint32_t data, bool lastByte=false)
 Sends data using SPI periphery. More...
 
virtual uint16_t receive (bool lastByte=false)
 Receives data from SPI periphery. More...
 
virtual uint16_t receive16 (bool lastByte=false)
 Receives data from SPI periphery. More...
 
virtual uint32_t receive32 (bool lastByte=false)
 Receives data from SPI periphery. More...
 
virtual bool writeBytes (const uint8_t *data, uint32_t size, bool lastByte=false)
 Sends data using SPI periphery in blocking mode. More...
 
virtual bool writeBytesNonBlocking (const uint8_t *data, uint32_t size, bool lastByte=false)
 Sends data using SPI periphery using DMA or IT (interrupt) mode. More...
 
virtual bool transferBytes (const uint8_t *out, uint8_t *in, uint32_t size, bool lastByte=false)
 Transfers data using SPI periphery in blocking mode, it is sending and receiving data simultaneously. More...
 
virtual bool transferBytesNonBlocking (const uint8_t *out, uint8_t *in, uint32_t size, bool lastByte=false)
 Transfers data using SPI periphery using DMA or IT (interrupt) mode, it is sending and receiving data simultaneously. More...
 
virtual bool receiveBytes (uint8_t *data, uint32_t size, bool lastByte=false)
 Receives data from SPI periphery in blocking mode. More...
 
virtual bool receiveBytesNonBlocking (uint8_t *data, uint32_t size, bool lastByte=false)
 Receives data from SPI periphery in non-blocking mode using DMA or IT (interrupt) mode. More...
 
virtual SPI_Status endTransaction (void)
 This method don't have to be called at the end of the transaction when using HardwareSPI class, but be careful, HardwareSPI_O class requres to call this at the end of transaction. More...
 
bool isFullDuplex ()
 Check, if full duplex mode is enabled for this periphery. More...
 
bool isRxEnabled ()
 Checks, if SPI periphery can receive data. More...
 
bool isTxEnabled ()
 Checks, if SPI periphery can transmit data. More...
 
 operator bool () const
 Checks if SPI instance exists. More...
 
bool available ()
 Checks if any data are available in RX FIFO buffer. More...
 
bool hasError (void)
 Checks if instance has error. More...
 
SPI_ErrorCode getError (void)
 Gets error, that happens during begin, end or transaction. More...
 
void clearError (uint32_t clearMask=0xFFFFFFFFUL)
 Gets error, that happens during begin, end or transaction. More...
 
virtual SPI_Status lastTransactionStatus ()
 Checks last transaction status. More...
 
void setWriteDoneCallback (void(*WriteDoneCallback_)())
 Sets callback function, that will be called when all data using writeBytesNonBlocking() method were send. More...
 
void setTransferDoneCallback (void(*TransferDoneCallback_)())
 Sets callback function, that will be called when all data using transferBytesNonBlocking() method were transfered. More...
 
void setReceiveDoneCallback (void(*ReceiveDoneCallback_)())
 Sets callback function, that will be called when all data using receiveBytesNonBlocking() method were received. More...
 
void setWriteHalfCallback (void(*WriteHalfCallback_)())
 Sets callback function, that will be called when half of data using writeBytesNonBlocking() method were send. More...
 
void setTransferHalfCallback (void(*TransferHalfCallback_)())
 Sets callback function, that will be called when half of data using transferBytesNonBlocking() method were transfered. More...
 
void setReceiveHalfCallback (void(*ReceiveHalfCallback_)())
 Sets callback function, that will be called when half of data using receiveBytesNonBlocking() method were received. More...
 
void setErrorCallback (void(*ErrorCallback_)(SPI_ErrorCode))
 Sets callback function, that will be called when an error has occurred. More...
 
SPI_HandleTypeDef & getHAL_SPI_Struct ()
 Returns SPI structure, that is used for handling SPI. More...
 

Static Public Member Functions

static void handleWriteDoneCallback (SPI_TypeDef *instance)
 Handles WriteDone callback function. More...
 
static void handleTransferDoneCallback (SPI_TypeDef *instance)
 Handles TransferDone callback function. More...
 
static void handleReceiveDoneCallback (SPI_TypeDef *instance)
 Handles ReceiveDone callback function. More...
 
static void handleWriteHalfCallback (SPI_TypeDef *instance)
 Handles WriteHalf callback function. More...
 
static void handleTransferHalfCallback (SPI_TypeDef *instance)
 Handles TransferHalf callback function. More...
 
static void handleReceiveHalfCallback (SPI_TypeDef *instance)
 Handles ReceiveHalf callback function. More...
 
static void handleErrorCallback (SPI_TypeDef *instance)
 Handles Error callback function. More...
 
static uint32_t getSPIIndex (SPI_TypeDef *instance)
 Gets SPI instance index. More...
 
static HardwareSPI ** getSPIInstances ()
 Gets array of all created SPI instances (just from this class, not SPI structure). More...
 
static uint8_t getAvailableSPIsCount ()
 Gets all available SPIs instances count. More...
 

Protected Member Functions

void clearCRCErrorFlag ()
 Clears CRC error flag from interrupt register. More...
 
void clearOVRErrorFlag ()
 Clears OVR error flag from interrupt register. More...
 
void clearMODFlag ()
 Clears MOD flag from interrupt register. More...
 
void clearFREFlag ()
 Clears FRE flag from interrupt register. More...
 
void addThisToInstArr ()
 Writes new class instance to array of all timer instances. More...
 
void removeThisFromInstArr ()
 Removes this instance from instances array. More...
 

Protected Attributes

SPI_HandleTypeDef * _hspi = NULL
 Pointer to SPI handle structure. More...
 
uint32_t _startMillis = 0
 Start time of transferring/receiving/sending data non-blocking. More...
 
uint32_t timeout = 1000
 Timeout interval in ms. More...
 
SPISettings oldSettings
 Old SPI setting - to speed up begin transaction. More...
 
void(* WriteDoneCallback )() = NULL
 
void(* TransferDoneCallback )() = NULL
 
void(* ReceiveDoneCallback )() = NULL
 
void(* WriteHalfCallback )() = NULL
 
void(* TransferHalfCallback )() = NULL
 
void(* ReceiveHalfCallback )() = NULL
 
void(* ErrorCallback )(SPI_ErrorCode) = NULL
 
SPI_Status transactionError = SPI_OK
 Last transaction status. More...
 

Static Protected Attributes

static HardwareSPIinstances []
 Array of all created SPI instances (just from this class, not SPI structure). More...
 
static uint8_t availableSPIsCount = sizeof(instances)/sizeof(*instances)
 Available SPI instances count. More...
 

Detailed Description

This class is for handling SPI periphery.

Main features:

To achieve better performance, use HardwareSPI_O class instead of this one.

Note
It is not recommended to use HardwareSPI instance and HardwareSPI_O instance at same time for same periphery, but it is not strictly forbidden.

Method groups

Method group Description
Initializers Methods for initializing or de-initializing SPI periphery.
State checking methods Methods for checking current state.
Setting methods Methods for change periphery settings.
Transaction initializers Methods for initializing transaction and flushing buffers.
Transaction methods Methods for data transaction.
Callback methods Methods for event handling.
Advanced methods Advanced methods to extend this library when needed.
See also
HardwareSPI_O
HardwareSPI.h
Hardware SPI documentation
Examples
HarwareSPIExample.

Constructor & Destructor Documentation

◆ HardwareSPI()

HardwareSPI::HardwareSPI ( SPI_HandleTypeDef &  hspi)

Constructor.

◆ ~HardwareSPI()

HardwareSPI::~HardwareSPI ( )

Destructor.

Here is the call graph for this function:

Member Function Documentation

◆ clearCRCErrorFlag()

void HardwareSPI::clearCRCErrorFlag ( )
inlineprotected

Clears CRC error flag from interrupt register.

◆ clearOVRErrorFlag()

void HardwareSPI::clearOVRErrorFlag ( )
inlineprotected

Clears OVR error flag from interrupt register.

◆ clearMODFlag()

void HardwareSPI::clearMODFlag ( )
inlineprotected

Clears MOD flag from interrupt register.

◆ clearFREFlag()

void HardwareSPI::clearFREFlag ( )
inlineprotected

Clears FRE flag from interrupt register.

◆ addThisToInstArr()

void HardwareSPI::addThisToInstArr ( )
protected

Writes new class instance to array of all timer instances.

We need to know all created instances to call period elapsed callback of right timer instance.

Here is the call graph for this function:

◆ removeThisFromInstArr()

void HardwareSPI::removeThisFromInstArr ( )
protected

Removes this instance from instances array.

Here is the call graph for this function:

Member Data Documentation

◆ _hspi

SPI_HandleTypeDef* HardwareSPI::_hspi = NULL
protected

Pointer to SPI handle structure.

◆ _startMillis

uint32_t HardwareSPI::_startMillis = 0
protected

Start time of transferring/receiving/sending data non-blocking.

Used in flushTX().

◆ timeout

uint32_t HardwareSPI::timeout = 1000
protected

Timeout interval in ms.

◆ oldSettings

SPISettings HardwareSPI::oldSettings
protected

Old SPI setting - to speed up begin transaction.

◆ WriteDoneCallback

void(* HardwareSPI::WriteDoneCallback) () = NULL
protected

◆ TransferDoneCallback

void(* HardwareSPI::TransferDoneCallback) () = NULL
protected

◆ ReceiveDoneCallback

void(* HardwareSPI::ReceiveDoneCallback) () = NULL
protected

◆ WriteHalfCallback

void(* HardwareSPI::WriteHalfCallback) () = NULL
protected

◆ TransferHalfCallback

void(* HardwareSPI::TransferHalfCallback) () = NULL
protected

◆ ReceiveHalfCallback

void(* HardwareSPI::ReceiveHalfCallback) () = NULL
protected

◆ ErrorCallback

void(* HardwareSPI::ErrorCallback) (SPI_ErrorCode) = NULL
protected

◆ transactionError

SPI_Status HardwareSPI::transactionError = SPI_OK
protected

Last transaction status.

◆ instances

HardwareSPI * HardwareSPI::instances
staticprotected

Array of all created SPI instances (just from this class, not SPI structure).

◆ availableSPIsCount

uint8_t HardwareSPI::availableSPIsCount = sizeof(instances)/sizeof(*instances)
staticprotected

Available SPI instances count.


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