api / mbed / trunk / SPI

SPI

class SPI : public Base

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"

SPI device(p5, p6, p7); // mosi, miso, sclk

int main() {
    int response = device.write(0xFF);
}
SPIA SPI Master, used for communicating with SPI slave devices
Functions
SPICreate a SPI 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

Functions

SPI

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

Create a SPI 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
class SPI : public Base
A SPI Master, used for communicating with SPI slave devices
SPI(PinName mosi,  
PinName miso,  
PinName sclk,  
const char *name =  NULL)
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
class DigitalOut : public Base
A digital output, used for setting the state of a pin