mbed library sources

Fork of mbed-src by mbed official

Embed: (wiki syntax)

« Back to documentation index

SPI Class Reference

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.
void format (int bits, int mode=0)
 Configure the data transmission format.
void frequency (int hz=1000000)
 Set the spi bus clock frequency.
virtual int write (int value)
 Write to the SPI Slave and return the response.
virtual int transfer (uint8_t *tx_buffer, int tx_length, uint8_t *rx_buffer, int rx_length, const event_callback_t &callback, int event=SPI_EVENT_COMPLETE)
 Start non-blocking SPI transfer using 8bit buffers.
virtual int transfer (uint16_t *tx_buffer, int tx_length, uint16_t *rx_buffer, int rx_length, const event_callback_t &callback, int event=SPI_EVENT_COMPLETE)
 Start non-blocking SPI transfer using 16bit buffers.
virtual int transfer (uint32_t *tx_buffer, int tx_length, uint32_t *rx_buffer, int rx_length, const event_callback_t &callback, int event=SPI_EVENT_COMPLETE)
 Start non-blocking SPI transfer using 32bit buffers.
void abort_transfer ()
 Abort the on-going SPI transfer, and continue with transfer's in the queue if any.
void clear_transfer_buffer ()
 Clear the transaction buffer.
void abort_all_transfers ()
 Clear the transaction buffer and abort on-going transfer.
int set_dma_usage (DMAUsage usage)
 Configure DMA usage suggestion for non-blocking transfers.

Protected Member Functions

void irq_handler_asynch (void)
 SPI IRQ handler.
int transfer (void *tx_buffer, int tx_length, void *rx_buffer, int rx_length, unsigned char bit_width, const event_callback_t &callback, int event)
 Common transfer method.
int queue_transfer (void *tx_buffer, int tx_length, void *rx_buffer, int rx_length, unsigned char bit_width, const event_callback_t &callback, int event)
void start_transfer (void *tx_buffer, int tx_length, void *rx_buffer, int rx_length, unsigned char bit_width, const event_callback_t &callback, int event)
 Configures a callback, spi peripheral and initiate a new transfer.
void start_transaction (transaction_t *data)
 Start a new transaction.
void dequeue_transaction ()
 Dequeue a transaction.

Detailed Description

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:

 // Send a byte to a SPI slave, and record the response

 #include "mbed.h"

 // hardware ssel (where applicable)
 //SPI device(p5, p6, p7, p8); // mosi, miso, sclk, ssel

 // software ssel
 SPI device(p5, p6, p7); // mosi, miso, sclk
 DigitalOut cs(p8); // ssel

 int main() {
     // hardware ssel (where applicable)
     //int response = device.write(0xFF);

     // software ssel
     cs = 0;
     int response = device.write(0xFF);
     cs = 1;
 }

Definition at line 66 of file SPI.h.


Constructor & Destructor Documentation

SPI ( PinName  mosi,
PinName  miso,
PinName  sclk,
PinName  ssel = NC 
)

Create a SPI master connected to the specified pins.

mosi or miso can be specfied as NC if not used

Parameters:
mosiSPI Master Out, Slave In pin
misoSPI Master In, Slave Out pin
sclkSPI Clock pin
sselSPI chip select pin

Definition at line 26 of file SPI.cpp.


Member Function Documentation

void abort_all_transfers (  )

Clear the transaction buffer and abort on-going transfer.

Definition at line 96 of file SPI.cpp.

void abort_transfer ( void   )

Abort the on-going SPI transfer, and continue with transfer's in the queue if any.

Definition at line 80 of file SPI.cpp.

void clear_transfer_buffer (  )

Clear the transaction buffer.

Definition at line 89 of file SPI.cpp.

void dequeue_transaction (  ) [protected]

Dequeue a transaction.

Definition at line 150 of file SPI.cpp.

void format ( int  bits,
int  mode = 0 
)

Configure the data transmission format.

Parameters:
bitsNumber of bits per SPI frame (4 - 16)
modeClock polarity and phase mode (0 - 3)
 mode | POL PHA
 -----+--------
   0  |  0   0
   1  |  0   1
   2  |  1   0
   3  |  1   1

Definition at line 40 of file SPI.cpp.

void frequency ( int  hz = 1000000 )

Set the spi bus clock frequency.

Parameters:
hzSCLK frequency in hz (default = 1MHz)

Definition at line 47 of file SPI.cpp.

void irq_handler_asynch ( void   ) [protected]

SPI IRQ handler.

Definition at line 162 of file SPI.cpp.

int queue_transfer ( void *  tx_buffer,
int  tx_length,
void *  rx_buffer,
int  rx_length,
unsigned char  bit_width,
const event_callback_t callback,
int  event 
) [protected]
Parameters:
tx_bufferThe TX buffer with data to be transfered. If NULL is passed, the default SPI value is sent
tx_lengthThe length of TX buffer
rx_bufferThe RX buffer which is used for received data. If NULL is passed, received data are ignored
rx_lengthThe length of RX buffer
bit_widthThe buffers element width
callbackThe event callback function
eventThe logical OR of events to modify
Returns:
Zero if a transfer was added to the queue, or -1 if the queue is full

Definition at line 111 of file SPI.cpp.

int set_dma_usage ( DMAUsage  usage )

Configure DMA usage suggestion for non-blocking transfers.

Parameters:
usageThe usage DMA hint for peripheral
Returns:
Zero if the usage was set, -1 if a transaction is on-going

Definition at line 102 of file SPI.cpp.

void start_transaction ( transaction_t data ) [protected]

Start a new transaction.

Parameters:
dataTransaction data

Definition at line 145 of file SPI.cpp.

void start_transfer ( void *  tx_buffer,
int  tx_length,
void *  rx_buffer,
int  rx_length,
unsigned char  bit_width,
const event_callback_t callback,
int  event 
) [protected]

Configures a callback, spi peripheral and initiate a new transfer.

Parameters:
tx_bufferThe TX buffer with data to be transfered. If NULL is passed, the default SPI value is sent
tx_lengthThe length of TX buffer
rx_bufferThe RX buffer which is used for received data. If NULL is passed, received data are ignored
rx_lengthThe length of RX buffer
bit_widthThe buffers element width
callbackThe event callback function
eventThe logical OR of events to modify

Definition at line 135 of file SPI.cpp.

int transfer ( void *  tx_buffer,
int  tx_length,
void *  rx_buffer,
int  rx_length,
unsigned char  bit_width,
const event_callback_t callback,
int  event 
) [protected]

Common transfer method.

Parameters:
tx_bufferThe TX buffer with data to be transfered. If NULL is passed, the default SPI value is sent
tx_lengthThe length of TX buffer
rx_bufferThe RX buffer which is used for received data. If NULL is passed, received data are ignored
rx_lengthThe length of RX buffer
bit_widthThe buffers element width
callbackThe event callback function
eventThe logical OR of events to modify
Returns:
Zero if the transfer has started or was added to the queue, or -1 if SPI peripheral is busy/buffer is full

Definition at line 71 of file SPI.cpp.

virtual int transfer ( uint16_t *  tx_buffer,
int  tx_length,
uint16_t *  rx_buffer,
int  rx_length,
const event_callback_t callback,
int  event = SPI_EVENT_COMPLETE 
) [virtual]

Start non-blocking SPI transfer using 16bit buffers.

Parameters:
tx_bufferThe TX buffer with data to be transfered. If NULL is passed, the default SPI value is sent
tx_lengthThe length of TX buffer
rx_bufferThe RX buffer which is used for received data. If NULL is passed, received data are ignored
rx_lengthThe length of RX buffer
callbackThe event callback function
eventThe logical OR of events to modify
Returns:
Zero if the transfer has started, or -1 if SPI peripheral is busy

Definition at line 142 of file SPI.h.

virtual int transfer ( uint32_t *  tx_buffer,
int  tx_length,
uint32_t *  rx_buffer,
int  rx_length,
const event_callback_t callback,
int  event = SPI_EVENT_COMPLETE 
) [virtual]

Start non-blocking SPI transfer using 32bit buffers.

Parameters:
tx_bufferThe TX buffer with data to be transfered. If NULL is passed, the default SPI value is sent
tx_lengthThe length of TX buffer
rx_bufferThe RX buffer which is used for received data. If NULL is passed, received data are ignored
rx_lengthThe length of RX buffer
callbackThe event callback function
eventThe logical OR of events to modify
Returns:
Zero if the transfer has started, or -1 if SPI peripheral is busy

Definition at line 158 of file SPI.h.

virtual int transfer ( uint8_t *  tx_buffer,
int  tx_length,
uint8_t *  rx_buffer,
int  rx_length,
const event_callback_t callback,
int  event = SPI_EVENT_COMPLETE 
) [virtual]

Start non-blocking SPI transfer using 8bit buffers.

Parameters:
tx_bufferThe TX buffer with data to be transfered. If NULL is passed, the default SPI value is sent
tx_lengthThe length of TX buffer
rx_bufferThe RX buffer which is used for received data. If NULL is passed, received data are ignored
rx_lengthThe length of RX buffer
callbackThe event callback function
eventThe logical OR of events to modify
Returns:
Zero if the transfer has started, or -1 if SPI peripheral is busy

Definition at line 126 of file SPI.h.

int write ( int  value ) [virtual]

Write to the SPI Slave and return the response.

Parameters:
valueData to be sent to the SPI slave
Returns:
Response from the SPI slave

Definition at line 64 of file SPI.cpp.