Including SPI 3-wires class

Fork of X_NUCLEO_COMMON_SPI3W by Licio Mapelli

Revision:
11:165bd6bc00ea
Parent:
10:da29b5403ca1
Child:
13:9a2c9b0bbe61
--- a/DevSPI/DevSPI.h	Fri Oct 16 14:13:27 2015 +0200
+++ b/DevSPI/DevSPI.h	Thu Feb 11 12:40:33 2016 +0000
@@ -2,8 +2,8 @@
  ******************************************************************************
  * @file    DevSPI.h
  * @author  AST / Software Platforms and Cloud
- * @version V0.0.1
- * @date    September 24th, 2015
+ * @version V1.0.1
+ * @date    February 11th, 2016
  * @brief   Header file for a special SPI class DevSPI which provides some
  *          helper function for on-board communication.
  ******************************************************************************
@@ -58,10 +58,8 @@
      */
     DevSPI(PinName mosi, PinName miso, PinName sclk) : SPI(mosi, miso, sclk)
     {
-        bits = 9;
-        mode = 0;
-        frequency_hz = 1E6;
-        setup_done = false;
+        /* Set default configuration. */
+        setup(8, 3, 1E6);
     }
 
     /*
@@ -87,9 +85,9 @@
      */
     void setup(int bits, int mode = 0, int frequency_hz = 1E6)
     {
+        /* Set given configuration. */
         format(bits, mode);
         frequency(frequency_hz);
-        setup_done = true;
     }
 
     /**
@@ -103,20 +101,16 @@
      */
     int spi_write(uint8_t* pBuffer, DigitalOut ssel, uint16_t NumBytesToWrite)
     {
-        /* Setup. */
-        if (!setup_done)
-        {
-            ssel = 1;
-            setup(8, 3, 1E6);
-            setup_done = true;
-        }
-
         /* Select the chip. */
         ssel = 0;
         
         /* Write data. */
-        for (int i = 0; i < NumBytesToWrite; i++)
-            write(pBuffer[i]);
+        if (_bits == 16)
+            for (int i = 0; i < NumBytesToWrite; i += 2)
+                write(((uint16_t *) pBuffer)[i]);
+        else if(_bits == 8)
+            for (int i = 0; i < NumBytesToWrite; i++)
+                write(pBuffer[i]);
 
         /* Unselect the chip. */
         ssel = 1;
@@ -135,20 +129,16 @@
      */
     int spi_read(uint8_t* pBuffer, DigitalOut ssel, uint16_t NumBytesToRead)
     {
-        /* Setup. */
-        if (!setup_done)
-        {
-            ssel = 1;
-            setup(8, 3, 1E6);
-            setup_done = true;
-        }
-
         /* Select the chip. */
         ssel = 0;
         
         /* Read data. */
-        for (int i = 0; i < NumBytesToRead; i++)
-            pBuffer[i] = write(0x00);
+        if (_bits == 16)
+            for (int i = 0; i < NumBytesToRead; i += 2)
+                ((uint16_t *) pBuffer)[i] = write(0x00);
+        else if(_bits == 8)
+            for (int i = 0; i < NumBytesToRead; i++)
+                pBuffer[i] = write(0x00);
 
         /* Unselect the chip. */
         ssel = 1;
@@ -168,32 +158,22 @@
      */
     int spi_read_write(uint8_t* pBufferToRead, uint8_t* pBufferToWrite, DigitalOut ssel, uint16_t NumBytes)
     {
-        /* Setup. */
-        if (!setup_done)
-        {
-            ssel = 1;
-            setup(8, 3, 1E6);
-            setup_done = true;
-        }
-
         /* Select the chip. */
         ssel = 0;
         
         /* Read and write data at the same time. */
-        for (int i = 0; i < NumBytes; i++)
-            pBufferToRead[i] = write(pBufferToWrite[i]);
+        if (_bits == 16)
+            for (int i = 0; i < NumBytes; i += 2)
+                ((uint16_t *) pBufferToRead)[i] = write(((uint16_t *) pBufferToWrite)[i]);
+        else if(_bits == 8)
+            for (int i = 0; i < NumBytes; i++)
+                pBufferToRead[i] = write(pBufferToWrite[i]);
 
         /* Unselect the chip. */
         ssel = 1;
 
         return 0;
     }
-
- protected:
-    bool setup_done;
-    int bits;
-    int mode;
-    int frequency_hz;
 };
 
 #endif /* __DEV_SPI_H */