nrfcxx  0.1.0
C++-17 Framework for Nordic nRF5 Devices
Data Structures | Public Types | Public Member Functions | Static Public Member Functions | Protected Member Functions | Protected Attributes
nrfcxx::periph::SPI Class Reference

Wrapper around the nRF51 SPI peripheral. More...

#include <nrfcxx/periph.hpp>

Inheritance diagram for nrfcxx::periph::SPI:
nrfcxx::periph::details::comm_error_support

Data Structures

struct  configuration_type
 

Public Types

using size_type = unsigned int
 The type used for transfer sizes (unsigned)
 
using ssize_type = int
 The type used for transfer sizes (non-negative) or errors (negative). More...
 
using scoped_enabler = details::scoped_enabler< SPI >
 The scoped enabler uses a SPI instance.
 
- Public Types inherited from nrfcxx::periph::details::comm_error_support
using size_type = unsigned int
 The type used for transfer sizes (unsigned)
 
using ssize_type = int
 The type used for transfer sizes (non-negative) or errors (negative). More...
 
using error_type = unsigned int
 The type used to encode TWI peripheral errors. More...
 

Public Member Functions

const nrf5::SPI_Typeperipheral () const
 Reference the underlying peripheral.
 
bool enabled () const
 Return true iff the device is enabled.
 
scoped_enabler scoped_enable () noexcept
 Construct an RAII object that controls whether the instance is enabled. More...
 
ssize_type bus_configure (int psel_sck, int psel_mosi, int psel_miso, uint32_t frequency, uint32_t config)
 Configure the SPI bus. More...
 
ssize_type enable ()
 Enable the SPI peripheral. More...
 
void disable ()
 Disable the SPI peripheral.
 
ssize_type tx_rx (const uint8_t *tx_data, size_type tx_len, size_type rx_len, uint8_t *rx_data, uint8_t tx_dummy=0)
 Transmit and/or receive data over the SPI bus. More...
 

Static Public Member Functions

static constexpr uint32_t frequency_from_Hz (unsigned int freq_Hz)
 Calculate the appropriate frequency assignment for a requested rate. More...
 
static constexpr uint32_t config_from_mode (uint8_t mode, bool lsb_first=false)
 Determine the value for the CONFIG register. More...
 
static SPIinstance (int idx)
 Reference the abstraction instance for a specific peripheral instance. More...
 
- Static Public Member Functions inherited from nrfcxx::periph::details::comm_error_support
constexpr static error_type error_decoded (ssize_type rc)
 Extract an encoded error value from an API return value. More...
 
constexpr static ssize_type error_encoded (error_type ec)
 Pack an error value into a negative return value. More...
 

Protected Member Functions

constexpr SPI (const nrf5::SPI_Type &spi)
 
error_type set_enabled_ (bool enabled)
 

Protected Attributes

const nrf5::SPI_Typespi_
 
configuration_type configuration_
 

Additional Inherited Members

- Static Public Attributes inherited from nrfcxx::periph::details::comm_error_support
constexpr static unsigned int ERR_OVERRUN = 0x001
 NRF_TWI_Type::ERRORSRC bit indicating incomplete reception at start.
 
constexpr static unsigned int ERR_ANACK = 0x002
 NRF_TWI_Type::ERRORSRC bit indicating NACK received during address transmission.
 
constexpr static unsigned int ERR_DNACK = 0x004
 NRF_TWI_Type::ERRORSRC bit indicating NACK received during data transmission.
 
constexpr static unsigned int ERR_TIMEOUT = 0x100
 Bit set in an error code when the TWI bus transaction timed out.
 
constexpr static unsigned int ERR_CLEAR = 0x200
 Bit set in an error code when the TWI bus could not be cleared.
 
constexpr static unsigned int ERR_INVALID = 0x400
 Bit set in an error code to indicate that the bus was not properly configured or a parameter was invalid.
 
constexpr static unsigned int ERR_CHECKSUM = 0x800
 Bit set in an error code to indicate a checksum error. More...
 
constexpr static unsigned int ERR_UNKNOWN = 0x1000
 Bit set in an error code to indicate an undescribable error.
 

Detailed Description

Wrapper around the nRF51 SPI peripheral.

Note
This abstraction handles only configuration and input/output. Control of the chip-select is not incorporated.
Warning
TWI0 and SPI0 share an interrupt and peripheral address space and cannot be simultaneously enabled. Similarly for TWI1, SPI1, and SPIS1.

Member Typedef Documentation

◆ ssize_type

The type used for transfer sizes (non-negative) or errors (negative).

See also
error_type

Member Function Documentation

◆ bus_configure()

ssize_type nrfcxx::periph::SPI::bus_configure ( int  psel_sck,
int  psel_mosi,
int  psel_miso,
uint32_t  frequency,
uint32_t  config 
)

Configure the SPI bus.

Parameters
psel_sclthe GPIO pin number to be used for the SCL (clock) signal.
psel_mosithe GPIO pin number to be used for the MOSI signal.
psel_misothe GPIO pin number to be used for the MISO signal. Pass -1 to disable MISO, as required by some write-only devices.
frequencythe bus frequency, being one of #SPI_FREQUENCY_FREQUENCY_K125, #SPI_FREQUENCY_FREQUENCY_M8, another value defined value allowed for the FREQUENCY register of the SPI peripheral. See frequency_from_Hz().
configthe value desired for the CONFIG register of the SPI peripheral. See config_from_mode().
Returns
Zero if successfully configured, otherwise a negative encoded error.

◆ config_from_mode()

static constexpr uint32_t nrfcxx::periph::SPI::config_from_mode ( uint8_t  mode,
bool  lsb_first = false 
)
inlinestaticconstexpr

Determine the value for the CONFIG register.

Parameters
modethe standard 2-bit SPI mode where CPOL is the upper bit and CPHA is the lower bit. Only the low two bits are used.
lsb_firsttrue if the device should shift out the least significant bit first. Defaults to MSB first.
Returns
the corresponding CONFIG value.

◆ enable()

ssize_type nrfcxx::periph::SPI::enable ( void  )
inline

Enable the SPI peripheral.

Returns
0 on success or negative encoded error.
See also
bus_configure

◆ frequency_from_Hz()

static constexpr uint32_t nrfcxx::periph::SPI::frequency_from_Hz ( unsigned int  freq_Hz)
inlinestaticconstexpr

Calculate the appropriate frequency assignment for a requested rate.

Supported frequencies for nRF51 are 125kHz * 2^n for n between 0 (125 kHz) through 6 (8 MHz) inclusive. Take largest value that does not exceed the requested frequency, or the smallest allowed value, whichever is greater.

Parameters
freq_Hzrequested clock speed in Hz
Returns
value to be stored in the FREQUENCY register of the SPI peripheral.

◆ instance()

static SPI& nrfcxx::periph::SPI::instance ( int  idx)
static

Reference the abstraction instance for a specific peripheral instance.

Parameters
idxthe peripheral instance desired.
Warning
If idx specifies a peripheral instance that does not exist on the device the system will reset into failsafe mode.

◆ scoped_enable()

scoped_enabler nrfcxx::periph::SPI::scoped_enable ( )
inlinenoexcept

Construct an RAII object that controls whether the instance is enabled.

Example:

// spi may or may not be enabled
if (auto enabler = spi.scoped_enable()) {
  // spi is enabled, use it
} else {
  // enable() failed, result in enabler.result()
}
// spi is disabled if it was enabled by previous statement
// spi remains enabled if it was enabled before previous statement

◆ tx_rx()

ssize_type nrfcxx::periph::SPI::tx_rx ( const uint8_t *  tx_data,
size_type  tx_len,
size_type  rx_len,
uint8_t *  rx_data,
uint8_t  tx_dummy = 0 
)

Transmit and/or receive data over the SPI bus.

This routine transmits tx_len octets from tx_data, storing the octets received in response into rx_data. It then transmits rx_len dummy bytes, appending the resulting response into rx_data.

Parameters
tx_datathe data to be transmitted (generally, a command). The pointer may be null only if tx_len is zero.
tx_lenthe number of bytes to transmit as the command. The value may be zero if this call reads data resulting from a previous command.
rx_lenthe number of additional bytes expected in response, exclusive of the synchronous responses to bytes transmitted from tx_data. This data is elicited by transmitting the requested number of tx_dummy values.
rx_datawhere to store the responses received during the transmit and receive phases. A null pointer may be passed if the incoming data is not of interest. If the pointer is not null, the space available must be at least tx_len + rx_len.
Returns
the total number of bytes stored in rx_data (or that would have been stored if rx_data were not null), or -1 if an error occcured.

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