Rtos API example

Embed: (wiki syntax)

« Back to documentation index

SPIHalfDuplex Class Reference

SPIHalfDuplex Class Reference

A SPI half-duplex master, used for communicating with SPI slave devices over a shared data line. More...

#include <SPIHalfDuplex.h>

Inherits mbed::SPI.

Public Member Functions

 SPIHalfDuplex (PinName mosi, PinName miso, PinName sclk)
 Create a SPI half-duplex master connected to the specified pins.
virtual int write (int value)
 Write to the SPI Slave and return the response.
void slave_format (int sbits)
 Set the number of databits expected from the slave, from 4-16.
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 (const char *tx_buffer, int tx_length, char *rx_buffer, int rx_length)
 Write to the SPI Slave and obtain the response.
virtual void lock (void)
 Acquire exclusive access to this SPI bus.
virtual void unlock (void)
 Release exclusive access to this SPI bus.
void set_default_write_value (char data)
 Set default write data SPI requires the master to send some data during a read operation.
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.
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

int transfer (const 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.
void irq_handler_asynch (void)
 SPI IRQ handler.
int queue_transfer (const 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 (const 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 half-duplex master, used for communicating with SPI slave devices over a shared data line.

The default format is set to 8-bits for both master and slave, and a clock frequency of 1MHz

Most SPI devies will also require Chip Select and Reset signals. These can be controlled using <DigitalOut> pins.

Although this is for a shared data line, both MISO and MOSI are defined, and should be tied together externally to the mbed. This class handles the tri-stating of the MOSI pin.

Example:

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

 #include "mbed.h"

 SPIHalfDuplex device(p5, p6, p7) // mosi, miso, sclk

 int main() {
     int respone = device.write(0xAA);
 }

Definition at line 42 of file SPIHalfDuplex.h.


Constructor & Destructor Documentation

SPIHalfDuplex ( PinName  mosi,
PinName  miso,
PinName  sclk 
)

Create a SPI half-duplex master connected to the specified pins.

Pin Options: (5, 6, 7) or (11, 12, 13)

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
name(optional) A string to identify the object

Definition at line 16 of file SPIHalfDuplex.cpp.


Member Function Documentation

void abort_all_transfers (  ) [inherited]

Clear the transaction buffer and abort on-going transfer.

Definition at line 157 of file SPI.cpp.

void abort_transfer ( void   ) [inherited]

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

Definition at line 140 of file SPI.cpp.

void clear_transfer_buffer (  ) [inherited]

Clear the transaction buffer.

Definition at line 150 of file SPI.cpp.

void dequeue_transaction (  ) [protected, inherited]

Dequeue a transaction.

Definition at line 217 of file SPI.cpp.

void format ( int  bits,
int  mode = 0 
) [inherited]

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 47 of file SPI.cpp.

void frequency ( int  hz = 1000000 ) [inherited]

Set the spi bus clock frequency.

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

Definition at line 62 of file SPI.cpp.

void irq_handler_asynch ( void   ) [protected, inherited]

SPI IRQ handler.

Definition at line 229 of file SPI.cpp.

void lock ( void   ) [virtual, inherited]

Acquire exclusive access to this SPI bus.

Definition at line 115 of file SPI.cpp.

int queue_transfer ( const void *  tx_buffer,
int  tx_length,
void *  rx_buffer,
int  rx_length,
unsigned char  bit_width,
const event_callback_t &  callback,
int  event 
) [protected, inherited]
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 in bytes
rx_bufferThe RX buffer which is used for received data. If NULL is passed, received data are ignored
rx_lengthThe length of RX buffer in bytes
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 172 of file SPI.cpp.

void set_default_write_value ( char  data ) [inherited]

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.

Parameters:
dataDefault character to be transmitted while read operation

Definition at line 123 of file SPI.cpp.

int set_dma_usage ( DMAUsage  usage ) [inherited]

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 163 of file SPI.cpp.

void slave_format ( int  sbits )

Set the number of databits expected from the slave, from 4-16.

Parameters:
sbitsNumber of expected bits in the slave response

Definition at line 22 of file SPIHalfDuplex.cpp.

void start_transaction ( transaction_t data ) [protected, inherited]

Start a new transaction.

Parameters:
dataTransaction data

Definition at line 212 of file SPI.cpp.

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

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 in bytes
rx_bufferThe RX buffer which is used for received data. If NULL is passed, received data are ignored
rx_lengthThe length of RX buffer in bytes
bit_widthThe buffers element width
callbackThe event callback function
eventThe logical OR of events to modify

Definition at line 201 of file SPI.cpp.

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 
) [inherited]

Start non-blocking SPI transfer using 8bit buffers.

This function locks the deep sleep until any event has occured

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 in bytes
rx_bufferThe RX buffer which is used for received data. If NULL is passed, received data are ignored
rx_lengthThe length of RX buffer in bytes
callbackThe event callback function
eventThe logical OR of events to modify. Look at spi hal header file for SPI events.
Returns:
Zero if the transfer has started, or -1 if SPI peripheral is busy

Definition at line 172 of file SPI.h.

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

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 in bytes
rx_bufferThe RX buffer which is used for received data. If NULL is passed, received data are ignored
rx_lengthThe length of RX buffer in bytes
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 131 of file SPI.cpp.

void unlock ( void   ) [virtual, inherited]

Release exclusive access to this SPI bus.

Definition at line 119 of file SPI.cpp.

int write ( const char *  tx_buffer,
int  tx_length,
char *  rx_buffer,
int  rx_length 
) [virtual, inherited]

Write to the SPI Slave and obtain the response.

The total number of bytes sent and recieved will be the maximum of tx_length and rx_length. The bytes written will be padded with the value 0xff.

Parameters:
tx_bufferPointer to the byte-array of data to write to the device
tx_lengthNumber of bytes to write, may be zero
rx_bufferPointer to the byte-array of data to read from the device
rx_lengthNumber of bytes to read, may be zero
Returns:
The number of bytes written and read from the device. This is maximum of tx_length and rx_length.

Definition at line 107 of file SPI.cpp.

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

Reimplemented from SPI.

Definition at line 26 of file SPIHalfDuplex.cpp.