Class to be able to send SPI data with almost no overhead, useful at very high speeds.

Dependents:   MakerBotServer epaper_mbed_130411_KL25Z epaper_mbed_test epaper_mbed_test_copy1 ... more

Embed: (wiki syntax)

« Back to documentation index

BurstSPI Class Reference

BurstSPI Class Reference

An SPI Master, used for communicating with SPI slave devices at very high speeds. More...

#include <BurstSPI.h>

Public Member Functions

 BurstSPI (PinName mosi, PinName miso, PinName sclk)
 Create a SPI master connected to the specified pins.
void fastWrite (int data)
 Put data packet in the SPI TX FIFO buffer.
void setFormat (void)
 Use this function before fastWrite to set the correct settings.
void clearRX (void)
 After you are done with fastWrite, call this function.
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.

Detailed Description

An SPI Master, used for communicating with SPI slave devices at very high speeds.

The default mbed SPI class allows for communication via the SPI bus at high clock frequencies, however at these frequencies there is alot of overhead from the mbed code. While this code makes sure your code is alot more robust, it is also relative slow. This library adds to your default SPI commands some extra commands to transmit data rapidly with very little overhead. Downsides are that currently it is TX only (all RX packets are discarded), and it requires some extra commands.

Example:

  //Send 1000 SPI packets as fast as possible
  spi.setFormat();
  for (int i = 0; i<1000; i++)
    spi.fastWrite(data[i]);
  spi.clearRX();

As an example, writing 76,800 16-bit data packets to an LCD screen at 48MHz requires 111ms with the normal mbed library. With this library it takes 25ms, which is also the theoretical amount of time it should take. If you are running at 1MHz this will do alot less.

Definition at line 29 of file BurstSPI.h.


Constructor & Destructor Documentation

BurstSPI ( PinName  mosi,
PinName  miso,
PinName  sclk 
)

Create a SPI 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

Definition at line 43 of file BurstSPI.h.


Member Function Documentation

void clearRX ( void   )

After you are done with fastWrite, call this function.

FastWrite simply fills the SPI's (SSP's actually) TX FIFO buffer as fast as it can, and that is the only thing it does. It doesn't do anything with received packages (currently, may change), so the the RX buffer is full with unneeded packets. This function waits until transmission is finished, and clears the RX buffer. You always have to call this before you want to receive SPI data after using fastWrite.

Definition at line 11 of file BurstSPI_KL25Z.cpp.

void fastWrite ( int  data )

Put data packet in the SPI TX FIFO buffer.

If there is no space in the FIFO buffer it will block until there is space. The FIFO buffer will automatically send the packets. There is no receiving here, only transmitting.

Parameters:
dataData to be sent to the SPI slave

Definition at line 4 of file BurstSPI_KL25Z.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
void frequency ( int  hz = 1000000 )

Set the spi bus clock frequency.

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

Use this function before fastWrite to set the correct settings.

It is not needed to use this if the last SPI commands were either normal SPI transmissions, or setting different format/frequency for this object. It is required to call this function when several SPI objects use the same peripheral, and your last transmission was from a different object with different settings. Not sure if you should use it? Use it, it takes very little time to execute, so can't hurt.

Definition at line 62 of file BurstSPI.h.

virtual 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