Wrapper around the nRF51 SPI peripheral.
More...
#include <nrfcxx/periph.hpp>
|
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.
|
|
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.
◆ ssize_type
The type used for transfer sizes (non-negative) or errors (negative).
- See also
- error_type
◆ 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_scl | the GPIO pin number to be used for the SCL (clock) signal. |
psel_mosi | the GPIO pin number to be used for the MOSI signal. |
psel_miso | the GPIO pin number to be used for the MISO signal. Pass -1 to disable MISO, as required by some write-only devices. |
frequency | the 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(). |
config | the 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
-
mode | the 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_first | true if the device should shift out the least significant bit first. Defaults to MSB first. |
- Returns
- the corresponding CONFIG value.
◆ enable()
◆ 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_Hz | requested 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
-
idx | the 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()
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_data | the data to be transmitted (generally, a command). The pointer may be null only if tx_len is zero. |
tx_len | the number of bytes to transmit as the command. The value may be zero if this call reads data resulting from a previous command. |
rx_len | the 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_data | where 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: