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

General

BurstSPI sends SPI data without reading it back, allowing higher speeds than the regular SPI library. This is mainly useful at high frequencies and large payloads. With a small number of bytes the setting up and finishing time will remove any advantage.

The three new functions compared to regular SPI are: fastWrite, setFormat and clearRX.

fastWrite is the function to quickly write data. setFormat is only required if the SPI format might have changed, or the first time you fastWrite something and you haven't used a regular SPI write before. clearRX is required if you also want to be able read from the SPI peripheral later on.

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

Supported targets

  • KL25Z, KL46Z
  • LPC1768, LPC11u24, LPC1114, LPC1549, LPC1347
  • STML152RE

If a target is not supported the library will issue a warning, and use regular writes. This means if you for example use this library to speed up writing to an LCD display, your LCD display library will work on all targets, and if possible BurstSPI will speed up the process.

Committer:
Sissors
Date:
Thu Jan 03 09:14:10 2013 +0000
Revision:
0:600eecd89a78
Child:
1:5ef4a019e0db
v1.0

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Sissors 0:600eecd89a78 1 #ifndef BURSTSPI_H
Sissors 0:600eecd89a78 2 #define BURSTSPI_H
Sissors 0:600eecd89a78 3
Sissors 0:600eecd89a78 4 #include "mbed.h"
Sissors 0:600eecd89a78 5
Sissors 0:600eecd89a78 6
Sissors 0:600eecd89a78 7 /** An SPI Master, used for communicating with SPI slave devices at very high speeds
Sissors 0:600eecd89a78 8 *
Sissors 0:600eecd89a78 9 * 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.
Sissors 0:600eecd89a78 10 * 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
Sissors 0:600eecd89a78 11 * very little overhead. Downsides are that currently it is TX only (all RX packets are discarded), and it requires some extra commands.
Sissors 0:600eecd89a78 12 *
Sissors 0:600eecd89a78 13 * Example:
Sissors 0:600eecd89a78 14 * @code
Sissors 0:600eecd89a78 15 * //Send 1000 SPI packets as fast as possible
Sissors 0:600eecd89a78 16 * spi.setFormat();
Sissors 0:600eecd89a78 17 * for (int i = 0; i<1000; i++)
Sissors 0:600eecd89a78 18 * spi.fastWrite(data[i]);
Sissors 0:600eecd89a78 19 * spi.clearRX();
Sissors 0:600eecd89a78 20 * @endcode
Sissors 0:600eecd89a78 21 *
Sissors 0:600eecd89a78 22 * 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
Sissors 0:600eecd89a78 23 * amount of time it should take. If you are running at 1MHz this will do alot less.
Sissors 0:600eecd89a78 24 */
Sissors 0:600eecd89a78 25 class BurstSPI : public SPI {
Sissors 0:600eecd89a78 26 public:
Sissors 0:600eecd89a78 27 /** Create a SPI master connected to the specified pins
Sissors 0:600eecd89a78 28 *
Sissors 0:600eecd89a78 29 * Pin Options:
Sissors 0:600eecd89a78 30 * (5, 6, 7) or (11, 12, 13)
Sissors 0:600eecd89a78 31 *
Sissors 0:600eecd89a78 32 * mosi or miso can be specfied as NC if not used
Sissors 0:600eecd89a78 33 *
Sissors 0:600eecd89a78 34 * @param mosi SPI Master Out, Slave In pin
Sissors 0:600eecd89a78 35 * @param miso SPI Master In, Slave Out pin
Sissors 0:600eecd89a78 36 * @param sclk SPI Clock pin
Sissors 0:600eecd89a78 37 */
Sissors 0:600eecd89a78 38 BurstSPI(PinName mosi, PinName miso, PinName sclk);
Sissors 0:600eecd89a78 39
Sissors 0:600eecd89a78 40 /** Put data packet in the SPI TX FIFO buffer
Sissors 0:600eecd89a78 41 *
Sissors 0:600eecd89a78 42 * 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.
Sissors 0:600eecd89a78 43 *
Sissors 0:600eecd89a78 44 * @param data Data to be sent to the SPI slave
Sissors 0:600eecd89a78 45 */
Sissors 0:600eecd89a78 46 void fastWrite(int data);
Sissors 0:600eecd89a78 47
Sissors 0:600eecd89a78 48 /** Use this function before fastWrite to set the correct settings
Sissors 0:600eecd89a78 49 *
Sissors 0:600eecd89a78 50 * 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
Sissors 0:600eecd89a78 51 * 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?
Sissors 0:600eecd89a78 52 * Use it, it takes very little time to execute, so can't hurt.
Sissors 0:600eecd89a78 53 */
Sissors 0:600eecd89a78 54 void setFormat( void );
Sissors 0:600eecd89a78 55
Sissors 0:600eecd89a78 56 /** After you are done with fastWrite, call this function
Sissors 0:600eecd89a78 57 *
Sissors 0:600eecd89a78 58 * 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),
Sissors 0:600eecd89a78 59 * 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
Sissors 0:600eecd89a78 60 * SPI data after using fastWrite.
Sissors 0:600eecd89a78 61 */
Sissors 0:600eecd89a78 62 void clearRX( void );
Sissors 0:600eecd89a78 63
Sissors 0:600eecd89a78 64
Sissors 0:600eecd89a78 65 //Just for documentation:
Sissors 0:600eecd89a78 66 #ifdef 0
Sissors 0:600eecd89a78 67 /** Configure the data transmission format
Sissors 0:600eecd89a78 68 *
Sissors 0:600eecd89a78 69 * @param bits Number of bits per SPI frame (4 - 16)
Sissors 0:600eecd89a78 70 * @param mode Clock polarity and phase mode (0 - 3)
Sissors 0:600eecd89a78 71 *
Sissors 0:600eecd89a78 72 * @code
Sissors 0:600eecd89a78 73 * mode | POL PHA
Sissors 0:600eecd89a78 74 * -----+--------
Sissors 0:600eecd89a78 75 * 0 | 0 0
Sissors 0:600eecd89a78 76 * 1 | 0 1
Sissors 0:600eecd89a78 77 * 2 | 1 0
Sissors 0:600eecd89a78 78 * 3 | 1 1
Sissors 0:600eecd89a78 79 * @endcode
Sissors 0:600eecd89a78 80 */
Sissors 0:600eecd89a78 81 void format(int bits, int mode = 0);
Sissors 0:600eecd89a78 82
Sissors 0:600eecd89a78 83 /** Set the spi bus clock frequency
Sissors 0:600eecd89a78 84 *
Sissors 0:600eecd89a78 85 * @param hz SCLK frequency in hz (default = 1MHz)
Sissors 0:600eecd89a78 86 */
Sissors 0:600eecd89a78 87 void frequency(int hz = 1000000);
Sissors 0:600eecd89a78 88
Sissors 0:600eecd89a78 89 /** Write to the SPI Slave and return the response
Sissors 0:600eecd89a78 90 *
Sissors 0:600eecd89a78 91 * @param value Data to be sent to the SPI slave
Sissors 0:600eecd89a78 92 *
Sissors 0:600eecd89a78 93 * @returns
Sissors 0:600eecd89a78 94 * Response from the SPI slave
Sissors 0:600eecd89a78 95 */
Sissors 0:600eecd89a78 96 virtual int write(int value);
Sissors 0:600eecd89a78 97 #endif
Sissors 0:600eecd89a78 98
Sissors 0:600eecd89a78 99 };
Sissors 0:600eecd89a78 100
Sissors 0:600eecd89a78 101 #endif