je ne sais plus
Revision 0:3787bbf77ca8, committed 2015-06-17
- Comitter:
- FreeControl
- Date:
- Wed Jun 17 13:49:54 2015 +0000
- Commit message:
- je ne sais plus
Changed in this revision
diff -r 000000000000 -r 3787bbf77ca8 BurstSPI_KL25Z.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/BurstSPI_KL25Z.cpp Wed Jun 17 13:49:54 2015 +0000 @@ -0,0 +1,24 @@ +#ifdef TARGET_KL25Z +#include "FastSPI.h" + +void FastSPI::fastWrite(int data) { + //Wait until FIFO has space + while(((_spi.spi->S) & SPI_S_SPTEF_MASK) == 0); + //transmit data + _spi.spi->D = data; + } + +void FastSPI::clearRX( void ) { + //We put in a delay here, this function shouldn't be called very often, so not a huge problem + //Without delay you will rise the CS line before it is finished (been there, done that) + //We use time required to transmit 20 bits (8 bits being transmitted, 8 bits in FIFO, 4 bits safety margin + + float bytetime = 20.0/_hz; + wait(bytetime); + + //Wait until status is flagged that we can read, read: + while (_spi.spi->S & SPI_S_SPRF_MASK == 0); + int dummy = _spi.spi->D; + +} +#endif \ No newline at end of file
diff -r 000000000000 -r 3787bbf77ca8 BurstSPI_KL46Z.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/BurstSPI_KL46Z.cpp Wed Jun 17 13:49:54 2015 +0000 @@ -0,0 +1,24 @@ +#ifdef TARGET_KL46Z +#include "FastSPI.h" + +void FastSPI::fastWrite(int data) { + //Wait until FIFO has space + while(((_spi.spi->S) & SPI_S_SPTEF_MASK) == 0); + //transmit data + _spi.spi->DL = data; + } + +void FastSPI::clearRX( void ) { + //We put in a delay here, this function shouldn't be called very often, so not a huge problem + //Without delay you will rise the CS line before it is finished (been there, done that) + //We use time required to transmit 20 bits (8 bits being transmitted, 8 bits in FIFO, 4 bits safety margin + + float bytetime = 20.0/_hz; + wait(bytetime); + + //Wait until status is flagged that we can read, read: + while (_spi.spi->S & SPI_S_SPRF_MASK == 0); + int dummy = _spi.spi->DL; + +} +#endif \ No newline at end of file
diff -r 000000000000 -r 3787bbf77ca8 BurstSPI_LPC_X.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/BurstSPI_LPC_X.cpp Wed Jun 17 13:49:54 2015 +0000 @@ -0,0 +1,20 @@ +#if defined(TARGET_LPC1768) || defined(TARGET_LPC1114) || defined(TARGET_LPC11U24) || defined(TARGET_LPC13XX) +#include "FastSPI.h" + +void FastSPI::fastWrite(int data) { + //Wait until FIFO has space + while(((_spi.spi->SR) & 0x02) == 0); + + //transmit data + _spi.spi->DR = data; + } + +void FastSPI::clearRX( void ) { + //Do it while either data in RX buffer, or while it is busy + while(((_spi.spi->SR) & ((1<<4) + (1<<2))) != 0) { + //Wait until data in RX buffer + while(((_spi.spi->SR) & (1<<2)) == 0); + int dummy = _spi.spi->DR; + } +} +#endif \ No newline at end of file
diff -r 000000000000 -r 3787bbf77ca8 BurstSPI_NUCLEO_L152RE.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/BurstSPI_NUCLEO_L152RE.cpp Wed Jun 17 13:49:54 2015 +0000 @@ -0,0 +1,27 @@ + + +/* FastSPI_NUCLEO_L152RE.cpp */ +#ifdef TARGET_NUCLEO_L152RE +#include "FastSPI.h" + +void FastSPI::fastWrite(int data) { + + SPI_TypeDef *spi = (SPI_TypeDef *)(_spi.spi); + // Check if data is transmitted + while (!((SPI_I2S_GetFlagStatus(spi, SPI_I2S_FLAG_TXE) != RESET) ? 1 : 0)); + SPI_I2S_SendData(spi, (uint16_t)data); + } + +void FastSPI::clearRX( void ) { + int status; + //Check if the RX buffer is busy + SPI_TypeDef *spi = (SPI_TypeDef *)(_spi.spi); + status = ((SPI_I2S_GetFlagStatus(spi, SPI_I2S_FLAG_BSY) != RESET) ? 1 : 0); + if (status){ + // Check RX buffer readable + while (!((SPI_I2S_GetFlagStatus(spi, SPI_I2S_FLAG_RXNE) != RESET) ? 1 : 0)); + int dummy = (int)SPI_I2S_ReceiveData(spi); + } +} +#endif +
diff -r 000000000000 -r 3787bbf77ca8 BurstSPI_Unsupported.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/BurstSPI_Unsupported.cpp Wed Jun 17 13:49:54 2015 +0000 @@ -0,0 +1,18 @@ +#if !(defined(TARGET_KL25Z) || defined(TARGET_KL46Z)) +#if !(defined(TARGET_LPC1768) || defined(TARGET_LPC1114) || defined(TARGET_LPC11U24) || defined(TARGET_LPC13XX)) +#if !(defined(TARGET_NUCLEO_L152RE)) + +#warning FastSPI target not supported, reverting to regular SPI + +#include "FastSPI.h" + +void FastSPI::fastWrite(int data) { + write(data); +} + +void FastSPI::clearRX( void ) { + +} +#endif //Freescale +#endif //NXP +#endif //NUCLEO \ No newline at end of file
diff -r 000000000000 -r 3787bbf77ca8 FastSPI.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/FastSPI.h Wed Jun 17 13:49:54 2015 +0000 @@ -0,0 +1,116 @@ +#ifndef FastSPI_H +#define FastSPI_H + +#include "mbed.h" + +// Directement dérivé de "BurstSPI" +// Changement de nom pour ne pas avoir de conflit + +/** 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: + * @code + * //Send 1000 SPI packets as fast as possible + * spi.setFormat(); + * for (int i = 0; i<1000; i++) + * spi.fastWrite(data[i]); + * spi.clearRX(); + * @endcode + * + * 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. + */ +class FastSPI : public SPI +{ +public: + /** 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 + * + * @param mosi SPI Master Out, Slave In pin + * @param miso SPI Master In, Slave Out pin + * @param sclk SPI Clock pin + */ + FastSPI(PinName mosi, PinName miso, PinName sclk) : SPI(mosi, miso, sclk) {}; + + /** 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. + * + * @param data Data to be sent to the SPI slave + */ + void fastWrite(int data); + + /** 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. + */ + void setFormat( void ) { + format(_bits, _mode); + frequency(_hz); + } + + /** 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. + */ + void clearRX( void ); + + + //Just for documentation: +#if 0 + /** Configure the data transmission format + * + * @param bits Number of bits per SPI frame (4 - 16) + * @param mode Clock polarity and phase mode (0 - 3) + * + * @code + * mode | POL PHA + * -----+-------- + * 0 | 0 0 + * 1 | 0 1 + * 2 | 1 0 + * 3 | 1 1 + * @endcode + */ + void format(int bits, int mode = 0); + + /** Set the spi bus clock frequency + * + * @param hz SCLK frequency in hz (default = 1MHz) + */ + void frequency(int hz = 1000000); + + /** Write to the SPI Slave and return the response + * + * @param value Data to be sent to the SPI slave + * + * @returns + * Response from the SPI slave + */ + virtual int write(int value); +#endif + +}; + +#endif \ No newline at end of file