A SPI Master, used for communicating with SPI slave devices. More...
#include <SPI.h>
Public Member Functions | |
SPI (PinName mosi, PinName miso, PinName sclk, PinName ssel=NC) | |
Create a SPI master connected to the specified pins. More... | |
SPI (PinName mosi, PinName miso, PinName sclk, PinName ssel, use_gpio_ssel_t) | |
Create a SPI master connected to the specified pins. More... | |
SPI (const spi_pinmap_t &static_pinmap) | |
Create a SPI master connected to the specified pins. More... | |
SPI (const spi_pinmap_t &static_pinmap, PinName ssel) | |
Create a SPI master connected to the specified pins. More... | |
void | format (int bits, int mode=0) |
Configure the data transmission format. More... | |
void | frequency (int hz=1000000) |
Set the SPI bus clock frequency. More... | |
virtual int | write (int value) |
Write to the SPI Slave and return the response. More... | |
virtual int | write (const char *tx_buffer, int tx_length, char *rx_buffer, int rx_length) |
Write to the SPI Slave and obtain the response. More... | |
virtual void | lock (void) |
Acquire exclusive access to this SPI bus. More... | |
virtual void | unlock (void) |
Release exclusive access to this SPI bus. More... | |
void | select (void) |
Assert the Slave Select line, acquiring exclusive access to this SPI bus. More... | |
void | deselect (void) |
Deassert the Slave Select line, releasing exclusive access to this SPI bus. More... | |
void | set_default_write_value (char data) |
Set default write data. More... | |
template<typename Type > | |
int | transfer (const Type *tx_buffer, int tx_length, Type *rx_buffer, int rx_length, const event_callback_t &callback, int event=SPI_EVENT_COMPLETE) |
Start non-blocking SPI transfer using 8bit buffers. More... | |
void | abort_transfer () |
Abort the on-going SPI transfer, and continue with transfers in the queue, if any. More... | |
void | clear_transfer_buffer () |
Clear the queue of transfers. More... | |
void | abort_all_transfers () |
Clear the queue of transfers and abort the on-going transfer. More... | |
int | set_dma_usage (DMAUsage usage) |
Configure DMA usage suggestion for non-blocking transfers. More... | |
A SPI Master, used for communicating with SPI slave devices.
The default format is set to 8-bits, mode 0, and a clock frequency of 1MHz.
Most SPI devices will also require Chip Select and Reset signals. These can be controlled using DigitalOut pins.
Example of how to send a byte to a SPI slave and record the response:
Example using hardware Chip Select line:
SPI | ( | PinName | mosi, |
PinName | miso, | ||
PinName | sclk, | ||
PinName | ssel = NC |
||
) |
Create a SPI master connected to the specified pins.
SPI | ( | PinName | mosi, |
PinName | miso, | ||
PinName | sclk, | ||
PinName | ssel, | ||
use_gpio_ssel_t | |||
) |
Create a SPI master connected to the specified pins.
SPI | ( | const spi_pinmap_t & | static_pinmap | ) |
Create a SPI master connected to the specified pins.
static_pinmap | reference to structure which holds static pinmap. |
SPI | ( | const spi_pinmap_t & | static_pinmap, |
PinName | ssel | ||
) |
Create a SPI master connected to the specified pins.
static_pinmap | reference to structure which holds static pinmap. |
ssel | SPI Chip Select pin. |
void abort_all_transfers | ( | ) |
Clear the queue of transfers and abort the on-going transfer.
void abort_transfer | ( | ) |
Abort the on-going SPI transfer, and continue with transfers in the queue, if any.
void clear_transfer_buffer | ( | ) |
Clear the queue of transfers.
void deselect | ( | void | ) |
Deassert the Slave Select line, releasing exclusive access to this SPI bus.
void format | ( | int | bits, |
int | mode = 0 |
||
) |
Configure the data transmission format.
bits | Number of bits per SPI frame (4 - 32, target dependent). |
mode | Clock polarity and phase mode (0 - 3). |
void frequency | ( | int | hz = 1000000 | ) |
Set the SPI bus clock frequency.
hz | Clock frequency in Hz (default = 1MHz). |
|
virtual |
Acquire exclusive access to this SPI bus.
void select | ( | void | ) |
Assert the Slave Select line, acquiring exclusive access to this SPI bus.
If use_gpio_ssel was not passed to the constructor, this only acquires exclusive access; it cannot assert the Slave Select line.
void set_default_write_value | ( | char | data | ) |
Set default write data.
SPI requires the master to send some data during a read operation. Different devices may require different default byte values. For example: A SD Card requires default bytes to be 0xFF.
data | Default character to be transmitted during a read operation. |
int set_dma_usage | ( | DMAUsage | usage | ) |
Configure DMA usage suggestion for non-blocking transfers.
usage | The usage DMA hint for peripheral. |
0 | The usage was set. |
-1 | Usage cannot be set as there is an ongoing transaction. |
int transfer | ( | const Type * | tx_buffer, |
int | tx_length, | ||
Type * | rx_buffer, | ||
int | rx_length, | ||
const event_callback_t & | callback, | ||
int | event = SPI_EVENT_COMPLETE |
||
) |
Start non-blocking SPI transfer using 8bit buffers.
This function locks the deep sleep until any event has occurred.
tx_buffer | The TX buffer with data to be transferred. If NULL is passed, the default SPI value is sent. |
tx_length | The length of TX buffer in bytes. |
rx_buffer | The RX buffer which is used for received data. If NULL is passed, received data are ignored. |
rx_length | The length of RX buffer in bytes. |
callback | The event callback function. |
event | The event mask of events to modify. |
0 | If the transfer has started. |
-1 | If SPI peripheral is busy. |
|
virtual |
Release exclusive access to this SPI bus.
|
virtual |
|
virtual |
Write to the SPI Slave and obtain the response.
The total number of bytes sent and received will be the maximum of tx_length and rx_length. The bytes written will be padded with the value 0xff.
tx_buffer | Pointer to the byte-array of data to write to the device. |
tx_length | Number of bytes to write, may be zero. |
rx_buffer | Pointer to the byte-array of data to read from the device. |
rx_length | Number of bytes to read, may be zero. |