Software SPI allows non-standard SPI pins to be used for interfacing with SPI devices

Dependents:   ADNS5090 PB_Emma_Ethernet spi_test Final_Project ... more

Embed: (wiki syntax)

« Back to documentation index

SWSPI Class Reference

SWSPI Class Reference

A software implemented SPI that can use any digital pins. More...

#include <SWSPI.h>

Public Member Functions

 SWSPI (PinName mosi_pin, PinName miso_pin, PinName sclk_pin)
 Create SWSPI object.
 ~SWSPI ()
 Destructor.
void format (int bits, int mode=0)
 Specify SPI format.
void frequency (int hz=10000000)
 Specify SPI clock frequency.
int write (int value)
 Write data and read result.

Detailed Description

A software implemented SPI that can use any digital pins.

Useful when don't want to share a single SPI hardware among attached devices or when pinout doesn't match exactly to the target's SPI pins

 #include "mbed.h"
 #include "SWSPI.h"
 
 SWSPI spi(p5, p6, p7); // mosi, miso, sclk
 
 int main() 
 {
     DigitalOut cs(p8);
     spi.format(8, 0);
     spi.frequency(10000000);
     cs.write(0);
     spi.write(0x9f);
     int jedecid = (spi.write(0) << 16) | (spi.write(0) << 8) | spi.write(0);
     cs.write(1);
 }

Definition at line 49 of file SWSPI.h.


Constructor & Destructor Documentation

SWSPI ( PinName  mosi_pin,
PinName  miso_pin,
PinName  sclk_pin 
)

Create SWSPI object.

Parameters:
mosi_pin
miso_pin
sclk_pin

Definition at line 26 of file SWSPI.cpp.

~SWSPI (  )

Destructor.

Definition at line 35 of file SWSPI.cpp.


Member Function Documentation

void format ( int  bits,
int  mode = 0 
)

Specify SPI format.

Parameters:
bits8 or 16 are typical values
mode0, 1, 2, or 3 phase (bit1) and idle clock (bit0)

Definition at line 42 of file SWSPI.cpp.

void frequency ( int  hz = 10000000 )

Specify SPI clock frequency.

Parameters:
hzfrequency (optional, defaults to 10000000)

Definition at line 51 of file SWSPI.cpp.

int write ( int  value )

Write data and read result.

Parameters:
valuedata to write (see format for bit size) returns value read from device

Definition at line 56 of file SWSPI.cpp.