Arduino Core for STM32  1.0
SPI setting methods

Methods for change periphery settings. For every setting method, there is also method, that reads the current setting right from register. More...

Functions

bool HardwareSPI::connectDMAtoRx (DMA_HandleTypeDef &DMA_handle)
 Use this method to connect DMA channel to SPI Rx programmatically. More...
 
bool HardwareSPI::connectDMAtoTx (DMA_HandleTypeDef &DMA_handle)
 Use this method to connect DMA channel to SPI Tx programmatically. More...
 
bool HardwareSPI::disconnectDMAfromRx ()
 Disconnects DMA channel from SPI Rx. More...
 
bool HardwareSPI::disconnectDMAfromTx ()
 Disconnects DMA channel from SPI Tx. More...
 
bool HardwareSPI::isDMAconnectedToRx (void)
 Checks if DMA channel is connected to SPI Rx and can be used for data receiving/transferring. More...
 
bool HardwareSPI::isDMAconnectedToTx (void)
 Checks if DMA channel is connected to SPI Tx and can be used for data writing/transferring. More...
 
uint32_t HardwareSPI::getSPIClockFreq (void)
 Gets periphery SPI Clock Frequency in Hz. More...
 
bool HardwareSPI::setMode (uint32_t mode)
 Sets Master or Slave mode. More...
 
uint32_t HardwareSPI::getMode ()
 Gets, if periphery mode is Master or Slave. More...
 
bool HardwareSPI::setHwCS (bool HardwareCS, bool OutputCS=true)
 Sets if Chip Select pin is handled by hardware or by software (calling digitalWrite()). More...
 
bool HardwareSPI::isHwCS ()
 Gets, if Chip Select is handled by hardware. More...
 
bool HardwareSPI::isHwOutputCS ()
 Gets, if Chip Select handled by hardware is set to output mode. More...
 
bool HardwareSPI::setBitOrder (uint8_t bitOrder)
 Sets bit order to LSB or MSB. More...
 
uint8_t HardwareSPI::getBitOrder ()
 Gets, if bit order is set to LSB or MSB. More...
 
bool HardwareSPI::setDataMode (uint8_t dataMode)
 Sets data mode. More...
 
uint8_t HardwareSPI::getDataMode ()
 Gets, what data mode is set. More...
 
bool HardwareSPI::setFrequency (uint32_t freq)
 Calculates SPI clock divider from frequency and sets it. More...
 
uint32_t HardwareSPI::getFrequency ()
 Calculates SPI frequency from APB frequency and Clock Divider. More...
 
bool HardwareSPI::setClockDivider (uint32_t clockDiv)
 Sets SPI clock divider. More...
 
uint32_t HardwareSPI::getClockDivider ()
 Gets clock divider. More...
 
bool HardwareSPI::setDataBits (uint16_t bits)
 Sets SPI data width in bits. More...
 
uint16_t HardwareSPI::getDataBits ()
 Gets set data width in bits. More...
 
bool HardwareSPI::enableTImode (bool enable)
 Enables or disabled TI mode. More...
 
bool HardwareSPI::isTImodeEnabled ()
 Gets if TI mode is enabled. More...
 
bool HardwareSPI::enableCRC (bool enable)
 Enables CRC. More...
 
bool HardwareSPI::isCRCenabled ()
 Gets if CRC is enabled. More...
 
bool HardwareSPI::setCRClength (SPI_CRCLength length)
 Sets CRC length. More...
 
SPI_CRCLength HardwareSPI::getCRClength ()
 Gets CRC length. More...
 
bool HardwareSPI::setCRCPolynomial (uint32_t polynomial)
 Sets CRC polynomial. More...
 
uint32_t HardwareSPI::getCRCPolynomial ()
 Gets CRC polynomial. More...
 
void HardwareSPI::setTimeout (uint32_t Timeout)
 Sets timeout in milliseconds. More...
 
uint32_t HardwareSPI::getTimeout ()
 Gets timeout in milliseconds. More...
 

Detailed Description

Methods for change periphery settings. For every setting method, there is also method, that reads the current setting right from register.

Function Documentation

◆ connectDMAtoRx()

bool HardwareSPI::connectDMAtoRx ( DMA_HandleTypeDef &  DMA_handle)

Use this method to connect DMA channel to SPI Rx programmatically.

Note
This method also switch from IT mode to DMA mode when using writeBytesNonBlocking() or transferBytesNonBlocking() (Transfer use DMA only when both Rx and Tx channels use DMA).
You don't have to connect DMA channel to SPI Rx when you have already done it in .ioc file.
Parameters
DMA_handleDMA channel handle structure, you want to connect.
Returns
Returns true when connection was done successfully.
Here is the call graph for this function:

◆ connectDMAtoTx()

bool HardwareSPI::connectDMAtoTx ( DMA_HandleTypeDef &  DMA_handle)

Use this method to connect DMA channel to SPI Tx programmatically.

Note
This method also switch from IT mode to DMA mode when using receiveBytesNonBlocking() or transferBytesNonBlocking() (Transfer use DMA only when both Rx and Tx channels use DMA).
You don't have to connect DMA channel to SPI Tx when you have already done it in .ioc file.
Parameters
DMA_handleDMA channel handle structure, you want to connect.
Returns
Returns true when connection was done successfully.
Here is the call graph for this function:

◆ disconnectDMAfromRx()

bool HardwareSPI::disconnectDMAfromRx ( )

Disconnects DMA channel from SPI Rx.

Returns
Returns true when DMA was disconnected successfully.
Note
This method also switch from DMA mode to IT mode, see also connectDMAtoRx().
Here is the call graph for this function:

◆ disconnectDMAfromTx()

bool HardwareSPI::disconnectDMAfromTx ( )

Disconnects DMA channel from SPI Tx.

Returns
Returns true when DMA was disconnected successfully.
Note
This method also switch from DMA mode to IT mode, see also connectDMAtoTx().
Here is the call graph for this function:

◆ isDMAconnectedToRx()

bool HardwareSPI::isDMAconnectedToRx ( void  )

Checks if DMA channel is connected to SPI Rx and can be used for data receiving/transferring.

Returns
Return true if DMA channel is connected to SPI Rx.

◆ isDMAconnectedToTx()

bool HardwareSPI::isDMAconnectedToTx ( void  )

Checks if DMA channel is connected to SPI Tx and can be used for data writing/transferring.

Returns
Return true if DMA channel is connected to SPI Tx.

◆ getSPIClockFreq()

uint32_t HardwareSPI::getSPIClockFreq ( void  )

Gets periphery SPI Clock Frequency in Hz.

Note
This function works properly only when IS_SPI_INSTANCE_ON_APB1 and IS_SPI_INSTANCE_ON_APB2 macro is defined properly.
Returns
Returns periphery SPI Clock Frequency in Hz.
Here is the call graph for this function:

◆ setMode()

bool HardwareSPI::setMode ( uint32_t  mode)

Sets Master or Slave mode.

Note
This setting can be set only when periphery is not transfering data or is not locked. HardwareSPI_O locks periphery when calling beginTransaction() method.
Parameters
modeMaster or Slave mode. (Use macros: SPI_MASTER and SPI_SLAVE)
See also
SPI modes definitions (MASTER/SLAVE)
Returns
Returns true when setting was set correctly.
Here is the call graph for this function:

◆ getMode()

uint32_t HardwareSPI::getMode ( )

Gets, if periphery mode is Master or Slave.

See also
SPI modes definitions (MASTER/SLAVE)
Returns
Returns periphery mode.
Here is the call graph for this function:

◆ setHwCS()

bool HardwareSPI::setHwCS ( bool  HardwareCS,
bool  OutputCS = true 
)

Sets if Chip Select pin is handled by hardware or by software (calling digitalWrite()).

Note
Hardware Chip Select works different than software, it pulls down CS pin on every transfered byte and several SPI slave devices requires CS pin set low during whole transaction.
This setting can be set only when periphery is not transferring data or is not locked. HardwareSPI_O locks periphery when calling beginTransaction() method.
Warning
When you enable HardwareCS, you have to set pin mode of designated pin to alternate function manually or call begin() method, else it will not works. There is also option to set "Hardware NSS Signal" setting in .ioc file not to "Disabled" option then you can disable HardwareCS and re-enable it, but you cannot change pinMode of CS pin.
Parameters
HardwareCSTrue to use hardware Chip Select.
OutputCSTrue to use hardware Chip Select in output mode (Default value: true). This parameter is ignored when HardwareCS is set to false.
Returns
Returns true when setting was set correctly.
Here is the call graph for this function:

◆ isHwCS()

bool HardwareSPI::isHwCS ( )

Gets, if Chip Select is handled by hardware.

Returns
Returns true when Chip Select is handled by hardware.
Here is the call graph for this function:

◆ isHwOutputCS()

bool HardwareSPI::isHwOutputCS ( )

Gets, if Chip Select handled by hardware is set to output mode.

Returns
Returns true when Chip Select handled by hardware is set to output mode.
Here is the call graph for this function:

◆ setBitOrder()

bool HardwareSPI::setBitOrder ( uint8_t  bitOrder)

Sets bit order to LSB or MSB.

Note
This setting can be set only when periphery is not transferring data or is not locked. HardwareSPI_O locks periphery when calling beginTransaction() method.
Parameters
bitOrderUse macros: LSBFIRST or MSBFIRST.
Returns
Returns true when setting was set correctly.
Here is the call graph for this function:

◆ getBitOrder()

uint8_t HardwareSPI::getBitOrder ( )

Gets, if bit order is set to LSB or MSB.

Returns
Returns bit order macros LSBFIRST or MSBFIRST.
Here is the call graph for this function:

◆ setDataMode()

bool HardwareSPI::setDataMode ( uint8_t  dataMode)

Sets data mode.

Note
This setting can be set only when periphery is not transferring data or is not locked. HardwareSPI_O locks periphery when calling beginTransaction() method.
Parameters
dataModeSelected data mode. Use values from the table below:
SPI mode CPOL CPHA
SPI_MODE0 0 0
SPI_MODE1 0 1
SPI_MODE2 1 0
SPI_MODE3 1 1
See: SPI_Data_Mode
Note
When TI mode is enabled, data mode is forcibly set to SPI_MODE0.
Returns
Returns true when setting was set correctly.

SPI_MODE0 0x00 - CPOL: 0 CPHA: 0 SPI_MODE1 0x01 - CPOL: 0 CPHA: 1 SPI_MODE2 0x10 - CPOL: 1 CPHA: 0 SPI_MODE3 0x11 - CPOL: 1 CPHA: 1

< CPOL (Clock Polarity)

< CPHA (Clock Phase)

Here is the call graph for this function:

◆ getDataMode()

uint8_t HardwareSPI::getDataMode ( )

Gets, what data mode is set.

Returns
Returns one of the possible values from the table below:
SPI mode CPOL CPHA
SPI_MODE0 0 0
SPI_MODE1 0 1
SPI_MODE2 1 0
SPI_MODE3 1 1

SPI_MODE0 0x00 - CPOL: 0 CPHA: 0 SPI_MODE1 0x01 - CPOL: 0 CPHA: 1 SPI_MODE2 0x10 - CPOL: 1 CPHA: 0 SPI_MODE3 0x11 - CPOL: 1 CPHA: 1

< CPOL (Clock Polarity)

< CPHA (Clock Phase)

Here is the call graph for this function:

◆ setFrequency()

bool HardwareSPI::setFrequency ( uint32_t  freq)

Calculates SPI clock divider from frequency and sets it.

Note
This setting can be set only when periphery is not transferring data or is not locked. HardwareSPI_O locks periphery when calling beginTransaction() method.
The real frequency will never by higher than the requested frequency.
Parameters
freqFrequency in Hz.
Returns
Returns true when setting was set correctly.
See also
setClockDivider()
Here is the call graph for this function:

◆ getFrequency()

uint32_t HardwareSPI::getFrequency ( )

Calculates SPI frequency from APB frequency and Clock Divider.

Returns
Returns SPI frequency in Hz.
Here is the call graph for this function:

◆ setClockDivider()

bool HardwareSPI::setClockDivider ( uint32_t  clockDiv)

Sets SPI clock divider.

The real frequency depends on the APB clock frequency and clock divider.

Note
This setting can be set only when periphery is not transferring data or is not locked. HardwareSPI_O locks periphery when calling beginTransaction() method.
Parameters
clockDivClock divider value. Use one of the defined value from SPI_Clock_Div.
Returns
Returns true when setting was set correctly.
See also
setFrequency()
Here is the call graph for this function:

◆ getClockDivider()

uint32_t HardwareSPI::getClockDivider ( )

Gets clock divider.

Returns
Returns clock divider value. Returned value is one of these defined values: SPI_Clock_Div.
Here is the call graph for this function:

◆ setDataBits()

bool HardwareSPI::setDataBits ( uint16_t  bits)

Sets SPI data width in bits.

Note
This setting can be set only when periphery is not transferring data or is not locked. HardwareSPI_O locks periphery when calling beginTransaction() method.
Parameters
bitsWidth of data in bits. Use values: 4 <= bits <= 16
Note
If you set bits > 8, and use transfer()/write()/receive() method to send one byte, it will send two bytes.
Warning
When you have enabled CRC, it can work only when data width is set to 8 or 16.
Returns
Returns true when setting was set correctly.
Here is the call graph for this function:

◆ getDataBits()

uint16_t HardwareSPI::getDataBits ( )

Gets set data width in bits.

Returns
Returns data width in bits.
Here is the call graph for this function:

◆ enableTImode()

bool HardwareSPI::enableTImode ( bool  enable)

Enables or disabled TI mode.

Note
This setting can be set only when periphery is not transferring data or is not locked. HardwareSPI_O locks periphery when calling beginTransaction() method.
Parameters
enableTrue to enable TI mode, false to enable Motorola mode. Motorola mode is used more often.
Note
TI mode forcibly sets data mode to SPI_MODE0.
Returns
Returns true when setting was set correctly.
Here is the call graph for this function:

◆ isTImodeEnabled()

bool HardwareSPI::isTImodeEnabled ( )

Gets if TI mode is enabled.

Returns
Returns true when TI mode is enabled.
Here is the call graph for this function:

◆ enableCRC()

bool HardwareSPI::enableCRC ( bool  enable)

Enables CRC.

Note
This setting can be set only when periphery is not transferring data or is not locked. HardwareSPI_O locks periphery when calling beginTransaction() method.
Parameters
enableTrue to enable CRC.
Note
CRC will not be enabled when data width is set to different value than 8 or 16. See setDataBits().
Returns
Returns true when setting was set correctly.
Here is the call graph for this function:

◆ isCRCenabled()

bool HardwareSPI::isCRCenabled ( )

Gets if CRC is enabled.

Returns
Returns true when CRC is enabled.
Here is the call graph for this function:

◆ setCRClength()

bool HardwareSPI::setCRClength ( SPI_CRCLength  length)

Sets CRC length.

Note
This setting can be set only when periphery is not transferring data or is not locked. HardwareSPI_O locks periphery when calling beginTransaction() method.
Parameters
lengthCRC length to be set. See SPI_CRCLength enum.
Returns
Returns true when setting was set correctly.
Here is the call graph for this function:

◆ getCRClength()

SPI_CRCLength HardwareSPI::getCRClength ( )

Gets CRC length.

Returns
Returns CRC length. Possible returned values are: SPI_CRC_1BYTE or SPI_CRC_2BYTES
Here is the call graph for this function:

◆ setCRCPolynomial()

bool HardwareSPI::setCRCPolynomial ( uint32_t  polynomial)

Sets CRC polynomial.

Note
This setting can be set only when periphery is not transferring data or is not locked. HardwareSPI_O locks periphery when calling beginTransaction() method.
Parameters
polynomialCRC polynomial. It is recommended to use macros: CRC_X0, CRC_X1, ..., CRC_X7; see: CRC_Poly_Terms. Polynomial have to contain CRC_X0. Polynomial doesn't have to contain highest coefficient, because it is added automatically from CRC length. For example CRC-8 polynomial is X^8 + X^5 + X^4 + 1, so you have to enter (CRC_X5 | CRC_X4 | CRC_X0), CRC_X8 is added automatically by CRC length. It's value have to be less than 256 and always has to be odd.
Returns
Returns true when setting was set correctly.
Here is the call graph for this function:

◆ getCRCPolynomial()

uint32_t HardwareSPI::getCRCPolynomial ( )

Gets CRC polynomial.

Returns
Returns CRC polynomial. Polynomial is represented by bits, so 00001101 represents X^3+X^2+X^0.
Here is the call graph for this function:

◆ setTimeout()

void HardwareSPI::setTimeout ( uint32_t  Timeout)

Sets timeout in milliseconds.

Timeout is used in all methods, that transact any data and in method HardwareSPI_O::endTransaction()

Note
Timeout is set to whole method, it does not depends on number of transacted bytes.
Parameters
TimeoutTimeout in milliseconds.
Returns
Returns true when setting was set correctly.

◆ getTimeout()

uint32_t HardwareSPI::getTimeout ( )

Gets timeout in milliseconds.

Returns
Returns timeout in milliseconds.