Lib BurstSPI give access to fastWrite function and clearRX function, this lib is now compatible with Nucleo L152RE board

Fork of BurstSPI by Erik -

Files at this revision

API Documentation at this revision

Comitter:
Sissors
Date:
Thu Jul 04 19:03:58 2013 +0000
Parent:
2:a8e55f7cbfee
Child:
4:8585ddebd28b
Commit message:
Refactored code, added version 1 of KL25Z support

Changed in this revision

BurstSPI.cpp Show diff for this revision Revisions of this file
BurstSPI.h Show annotated file Show diff for this revision Revisions of this file
BurstSPI_KL25Z.cpp Show annotated file Show diff for this revision Revisions of this file
BurstSPI_LPC1768.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/BurstSPI.cpp	Fri Jan 04 09:51:42 2013 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,28 +0,0 @@
-#include "BurstSPI.h"
-
-BurstSPI::BurstSPI(PinName mosi, PinName miso, PinName sclk) : SPI(mosi, miso, sclk){
-
-}
-
-
-void BurstSPI::fastWrite(int data) {
-    //Wait until FIFO has space
-    while(((_spi.spi->SR) & 0x02) == 0);
-    
-    //transmit data
-    _spi.spi->DR = data;
-    }
-
-void BurstSPI::setFormat( void ) {
-    format(_bits, _mode);
-    frequency(_hz);
-    }
-
-void BurstSPI::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;
-        }
-}
\ No newline at end of file
--- a/BurstSPI.h	Fri Jan 04 09:51:42 2013 +0000
+++ b/BurstSPI.h	Thu Jul 04 19:03:58 2013 +0000
@@ -26,7 +26,8 @@
  * 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 BurstSPI : public SPI {
+class BurstSPI : public SPI
+{
 public:
     /** Create a SPI master connected to the specified pins
     *
@@ -39,7 +40,7 @@
     *  @param miso SPI Master In, Slave Out pin
     *  @param sclk SPI Clock pin
     */
-    BurstSPI(PinName mosi, PinName miso, PinName sclk);
+    BurstSPI(PinName mosi, PinName miso, PinName sclk) : SPI(mosi, miso, sclk) {};
 
     /** Put data packet in the SPI TX FIFO buffer
     *
@@ -49,17 +50,20 @@
     *  @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 );
-    
+    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,
@@ -67,34 +71,34 @@
     * 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
+#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 
+     * mode | POL PHA
+     * -----+--------
+     *   0  |  0   0
      *   1  |  0   1
-     *   2  |  1   0 
+     *   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
@@ -103,7 +107,7 @@
      *    Response from the SPI slave
     */
     virtual int write(int value);
-    #endif
+#endif
 
 };
 
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/BurstSPI_KL25Z.cpp	Thu Jul 04 19:03:58 2013 +0000
@@ -0,0 +1,24 @@
+#ifdef TARGET_KL25Z
+#include "BurstSPI.h"
+
+void BurstSPI::fastWrite(int data) {
+    //Wait until FIFO has space
+    while(((_spi.spi->S) & SPI_S_SPTEF_MASK) == 0);
+    //transmit data
+    _spi.spi->D = data;
+    }
+
+void BurstSPI::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
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/BurstSPI_LPC1768.cpp	Thu Jul 04 19:03:58 2013 +0000
@@ -0,0 +1,20 @@
+#ifdef TARGET_LPC1768
+#include "BurstSPI.h"
+
+void BurstSPI::fastWrite(int data) {
+    //Wait until FIFO has space
+    while(((_spi.spi->SR) & 0x02) == 0);
+    
+    //transmit data
+    _spi.spi->DR = data;
+    }
+
+void BurstSPI::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