je ne sais plus

Committer:
FreeControl
Date:
Wed Jun 17 13:49:54 2015 +0000
Revision:
0:3787bbf77ca8
je ne sais plus

Who changed what in which revision?

UserRevisionLine numberNew contents of line
FreeControl 0:3787bbf77ca8 1 #ifndef FastSPI_H
FreeControl 0:3787bbf77ca8 2 #define FastSPI_H
FreeControl 0:3787bbf77ca8 3
FreeControl 0:3787bbf77ca8 4 #include "mbed.h"
FreeControl 0:3787bbf77ca8 5
FreeControl 0:3787bbf77ca8 6 // Directement dérivé de "BurstSPI"
FreeControl 0:3787bbf77ca8 7 // Changement de nom pour ne pas avoir de conflit
FreeControl 0:3787bbf77ca8 8
FreeControl 0:3787bbf77ca8 9 /** An SPI Master, used for communicating with SPI slave devices at very high speeds
FreeControl 0:3787bbf77ca8 10 *
FreeControl 0:3787bbf77ca8 11 * The default mbed SPI class allows for communication via the SPI bus at high clock frequencies,
FreeControl 0:3787bbf77ca8 12 * however at these frequencies there is alot of overhead from the mbed code.
FreeControl 0:3787bbf77ca8 13 * While this code makes sure your code is alot more robust, it is also relative slow.
FreeControl 0:3787bbf77ca8 14 * This library adds to your default SPI commands some extra commands to transmit data rapidly with
FreeControl 0:3787bbf77ca8 15 * very little overhead. Downsides are that currently it is TX only (all RX packets are discarded),
FreeControl 0:3787bbf77ca8 16 * and it requires some extra commands.
FreeControl 0:3787bbf77ca8 17 *
FreeControl 0:3787bbf77ca8 18 * Example:
FreeControl 0:3787bbf77ca8 19 * @code
FreeControl 0:3787bbf77ca8 20 * //Send 1000 SPI packets as fast as possible
FreeControl 0:3787bbf77ca8 21 * spi.setFormat();
FreeControl 0:3787bbf77ca8 22 * for (int i = 0; i<1000; i++)
FreeControl 0:3787bbf77ca8 23 * spi.fastWrite(data[i]);
FreeControl 0:3787bbf77ca8 24 * spi.clearRX();
FreeControl 0:3787bbf77ca8 25 * @endcode
FreeControl 0:3787bbf77ca8 26 *
FreeControl 0:3787bbf77ca8 27 * As an example, writing 76,800 16-bit data packets to an LCD screen at 48MHz requires 111ms with
FreeControl 0:3787bbf77ca8 28 * the normal mbed library. With this library it takes 25ms, which is also the theoretical
FreeControl 0:3787bbf77ca8 29 * amount of time it should take. If you are running at 1MHz this will do alot less.
FreeControl 0:3787bbf77ca8 30 */
FreeControl 0:3787bbf77ca8 31 class FastSPI : public SPI
FreeControl 0:3787bbf77ca8 32 {
FreeControl 0:3787bbf77ca8 33 public:
FreeControl 0:3787bbf77ca8 34 /** Create a SPI master connected to the specified pins
FreeControl 0:3787bbf77ca8 35 *
FreeControl 0:3787bbf77ca8 36 * Pin Options:
FreeControl 0:3787bbf77ca8 37 * (5, 6, 7) or (11, 12, 13)
FreeControl 0:3787bbf77ca8 38 *
FreeControl 0:3787bbf77ca8 39 * mosi or miso can be specfied as NC if not used
FreeControl 0:3787bbf77ca8 40 *
FreeControl 0:3787bbf77ca8 41 * @param mosi SPI Master Out, Slave In pin
FreeControl 0:3787bbf77ca8 42 * @param miso SPI Master In, Slave Out pin
FreeControl 0:3787bbf77ca8 43 * @param sclk SPI Clock pin
FreeControl 0:3787bbf77ca8 44 */
FreeControl 0:3787bbf77ca8 45 FastSPI(PinName mosi, PinName miso, PinName sclk) : SPI(mosi, miso, sclk) {};
FreeControl 0:3787bbf77ca8 46
FreeControl 0:3787bbf77ca8 47 /** Put data packet in the SPI TX FIFO buffer
FreeControl 0:3787bbf77ca8 48 *
FreeControl 0:3787bbf77ca8 49 * If there is no space in the FIFO buffer it will block until there is space.
FreeControl 0:3787bbf77ca8 50 * The FIFO buffer will automatically send the packets. There is no receiving here, only transmitting.
FreeControl 0:3787bbf77ca8 51 *
FreeControl 0:3787bbf77ca8 52 * @param data Data to be sent to the SPI slave
FreeControl 0:3787bbf77ca8 53 */
FreeControl 0:3787bbf77ca8 54 void fastWrite(int data);
FreeControl 0:3787bbf77ca8 55
FreeControl 0:3787bbf77ca8 56 /** Use this function before fastWrite to set the correct settings
FreeControl 0:3787bbf77ca8 57 *
FreeControl 0:3787bbf77ca8 58 * It is not needed to use this if the last SPI commands were either normal SPI transmissions,
FreeControl 0:3787bbf77ca8 59 * or setting different format/frequency for this object. It is required to call this
FreeControl 0:3787bbf77ca8 60 * function when several SPI objects use the same peripheral, and your last transmission was
FreeControl 0:3787bbf77ca8 61 * from a different object with different settings. Not sure if you should use it?
FreeControl 0:3787bbf77ca8 62 * Use it, it takes very little time to execute, so can't hurt.
FreeControl 0:3787bbf77ca8 63 */
FreeControl 0:3787bbf77ca8 64 void setFormat( void ) {
FreeControl 0:3787bbf77ca8 65 format(_bits, _mode);
FreeControl 0:3787bbf77ca8 66 frequency(_hz);
FreeControl 0:3787bbf77ca8 67 }
FreeControl 0:3787bbf77ca8 68
FreeControl 0:3787bbf77ca8 69 /** After you are done with fastWrite, call this function
FreeControl 0:3787bbf77ca8 70 *
FreeControl 0:3787bbf77ca8 71 * FastWrite simply fills the SPI's (SSP's actually) TX FIFO buffer as fast as it can,
FreeControl 0:3787bbf77ca8 72 * and that is the only thing it does. It doesn't do anything with received packages (currently, may change),
FreeControl 0:3787bbf77ca8 73 * so the the RX buffer is full with unneeded packets. This function waits until transmission is finished,
FreeControl 0:3787bbf77ca8 74 * and clears the RX buffer. You always have to call this before you want to receive
FreeControl 0:3787bbf77ca8 75 * SPI data after using fastWrite.
FreeControl 0:3787bbf77ca8 76 */
FreeControl 0:3787bbf77ca8 77 void clearRX( void );
FreeControl 0:3787bbf77ca8 78
FreeControl 0:3787bbf77ca8 79
FreeControl 0:3787bbf77ca8 80 //Just for documentation:
FreeControl 0:3787bbf77ca8 81 #if 0
FreeControl 0:3787bbf77ca8 82 /** Configure the data transmission format
FreeControl 0:3787bbf77ca8 83 *
FreeControl 0:3787bbf77ca8 84 * @param bits Number of bits per SPI frame (4 - 16)
FreeControl 0:3787bbf77ca8 85 * @param mode Clock polarity and phase mode (0 - 3)
FreeControl 0:3787bbf77ca8 86 *
FreeControl 0:3787bbf77ca8 87 * @code
FreeControl 0:3787bbf77ca8 88 * mode | POL PHA
FreeControl 0:3787bbf77ca8 89 * -----+--------
FreeControl 0:3787bbf77ca8 90 * 0 | 0 0
FreeControl 0:3787bbf77ca8 91 * 1 | 0 1
FreeControl 0:3787bbf77ca8 92 * 2 | 1 0
FreeControl 0:3787bbf77ca8 93 * 3 | 1 1
FreeControl 0:3787bbf77ca8 94 * @endcode
FreeControl 0:3787bbf77ca8 95 */
FreeControl 0:3787bbf77ca8 96 void format(int bits, int mode = 0);
FreeControl 0:3787bbf77ca8 97
FreeControl 0:3787bbf77ca8 98 /** Set the spi bus clock frequency
FreeControl 0:3787bbf77ca8 99 *
FreeControl 0:3787bbf77ca8 100 * @param hz SCLK frequency in hz (default = 1MHz)
FreeControl 0:3787bbf77ca8 101 */
FreeControl 0:3787bbf77ca8 102 void frequency(int hz = 1000000);
FreeControl 0:3787bbf77ca8 103
FreeControl 0:3787bbf77ca8 104 /** Write to the SPI Slave and return the response
FreeControl 0:3787bbf77ca8 105 *
FreeControl 0:3787bbf77ca8 106 * @param value Data to be sent to the SPI slave
FreeControl 0:3787bbf77ca8 107 *
FreeControl 0:3787bbf77ca8 108 * @returns
FreeControl 0:3787bbf77ca8 109 * Response from the SPI slave
FreeControl 0:3787bbf77ca8 110 */
FreeControl 0:3787bbf77ca8 111 virtual int write(int value);
FreeControl 0:3787bbf77ca8 112 #endif
FreeControl 0:3787bbf77ca8 113
FreeControl 0:3787bbf77ca8 114 };
FreeControl 0:3787bbf77ca8 115
FreeControl 0:3787bbf77ca8 116 #endif