api / mbed / trunk / SPIHalfDuplex

SPIHalfDuplex

class SPIHalfDuplex : public SPI

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);
}
SPIHalfDuplexA SPI half-duplex master, used for communicating with SPI slave devices over a shared data line.
Functions
SPIHalfDuplexCreate a SPI half-duplex master connected to the specified pins
formatConfigure the data transmission format
frequencySet the spi bus clock frequency
writeWrite to the SPI Slave and return the response
slave_formatSet the number of databits expected from the slave, from 4-16

Functions

SPIHalfDuplex

SPIHalfDuplex(PinName mosi,  
PinName miso,  
PinName sclk,  
const char *name =  NULL)

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

Variables

mosiSPI Master Out, Slave In pin
misoSPI Master In, Slave Out pin
sclkSPI Clock pin
name(optional) A string to identify the object

Pin Options

(5, 6, 7) or (11, 12, 13)

mosi or miso can be specfied as NC if not used

format

void format(int bits,  
int mode =  0)

Configure the data transmission format

Variables

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

frequency

void frequency(int hz =  1000000)

Set the spi bus clock frequency

Variables

hzSCLK frequency in hz (default = 1MHz)

write

virtual int write(int value)

Write to the SPI Slave and return the response

Variables

valueData to be sent to the SPI slave
returnsResponse from the SPI slave

slave_format

void slave_format(int sbits)

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

Variables

sbitsNumber of expected bits in the slave response
class SPIHalfDuplex : public SPI
A SPI half-duplex master, used for communicating with SPI slave devices over a shared data line.
SPIHalfDuplex(PinName mosi,  
PinName miso,  
PinName sclk,  
const char *name =  NULL)
Create a SPI half-duplex 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
void slave_format(int sbits)
Set the number of databits expected from the slave, from 4-16
class DigitalOut : public Base
A digital output, used for setting the state of a pin