Yang Zhang / EasyCAT_lib
Revision:
2:e0fc1b098ce8
Parent:
0:7816b38c99cc
Child:
3:7d8b74ba7225
--- a/EasyCAT.cpp	Tue Sep 12 17:36:25 2017 +0000
+++ b/EasyCAT.cpp	Thu Mar 28 08:32:57 2019 +0000
@@ -23,30 +23,21 @@
 #include "EasyCAT.h"                         
 
 
-DigitalOut * SpiCs;								 	
-SPI Spi(D11, D12, D13);                          // declare MOSI MISO SCK 
-
+static DigitalOut * SpiCs;								 	
+static SPI Spi(D11, D12, D13);                          // declare MOSI MISO SCK 
 
 #define SCS_Low_macro  (*SpiCs = 0);			// macro for set/reset SPI chip select
 #define SCS_High_macro (*SpiCs = 1);			//
 
-												// macro for the SPI transfer		
-inline static void SPI_TransferTx (unsigned char Data) 
-{                                               //
-	Spi.write(Data);                            //
-};                                              //
-                                                //
-inline static void SPI_TransferTxLast (unsigned char Data) 
-{                                               //
-	Spi.write(Data);                            // 
-};                                              //
-                                                //
-inline static unsigned char SPI_TransferRx (unsigned char Data) 
-{                                               //
-	return Spi.write(Data);                     //
-};	                                            //
+inline static void SPI_TransferTxBuffer(const void* buffer, int buffer_size)
+{
+    Spi.write(static_cast<const char*>(buffer), buffer_size, NULL, 0);
+}
 
-
+inline static void SPI_TransferRxBuffer(void* buffer, int buffer_size)
+{
+    Spi.write(NULL, 0, static_cast<char*>(buffer), buffer_size);
+}
   
 //---- constructors --------------------------------------------------------------------------------
 
@@ -68,6 +59,9 @@
                                                 // for EasyCAT board REV_B we have three additional options:
                                                 // A5, D6, D7 
 {                                       
+  Sync_ = ASYNC;                                // if no synchronization mode is declared
+                                                // ASYNC is the default
+
   SCS_ = SCS;                                   //  initialize chip select  
   SpiCs = new DigitalOut(SCS_, 1);				//
 }                                               // 
@@ -315,18 +309,12 @@
   ULONG Result; 
   UWORD Addr;
   Addr.Word = Address; 
-  unsigned char i; 
 
   SCS_Low_macro                                             // SPI chip select enable
 
-  SPI_TransferTx(COMM_SPI_READ);                            // SPI read command
-  SPI_TransferTx(Addr.Byte[1]);                             // address of the register
-  SPI_TransferTxLast(Addr.Byte[0]);                         // to read, MsByte first
- 
-  for (i=0; i<Len; i++)                                     // read the requested number of bytes
-  {                                                         // LsByte first 
-    Result.Byte[i] = SPI_TransferRx(DUMMY_BYTE);            //
-  }                                                         //    
+  char buffer[3] = {COMM_SPI_READ, Addr.Byte[1], Addr.Byte[0]};
+  SPI_TransferTxBuffer(buffer, 3);
+  SPI_TransferRxBuffer(Result.Byte, Len);
   
   SCS_High_macro                                            // SPI chip select disable 
  
@@ -350,14 +338,9 @@
 
   SCS_Low_macro                                             // SPI chip select enable  
   
-  SPI_TransferTx(COMM_SPI_WRITE);                           // SPI write command
-  SPI_TransferTx(Addr.Byte[1]);                             // address of the register
-  SPI_TransferTx(Addr.Byte[0]);                             // to write MsByte first
-
-  SPI_TransferTx(Data.Byte[0]);                             // data to write 
-  SPI_TransferTx(Data.Byte[1]);                             // LsByte first
-  SPI_TransferTx(Data.Byte[2]);                             //
-  SPI_TransferTxLast(Data.Byte[3]);                         //
+  char buffer[7] = {COMM_SPI_WRITE, Addr.Byte[1], Addr.Byte[0],
+                    Data.Byte[0], Data.Byte[1], Data.Byte[2], Data.Byte[3]};
+  SPI_TransferTxBuffer(buffer, 7);
  
   SCS_High_macro                                            // SPI chip select enable   
 }
@@ -437,7 +420,6 @@
                                       // that will be use by our application to write the outputs
 {
   ULONG TempLong;
-  unsigned char i;
    
   #if TOT_BYTE_NUM_OUT > 0
 
@@ -464,14 +446,9 @@
   
     SCS_Low_macro                                                 // enable SPI chip select 
   
-    SPI_TransferTx(COMM_SPI_READ);                                // SPI read command
-    SPI_TransferTx(0x00);                                         // address of the read  
-    SPI_TransferTxLast(0x00);                                     // fifo MsByte first
-  
-    for (i=0; i< FST_BYTE_NUM_ROUND_OUT; i++)                     // transfer the data
-    {                                                             //
-      BufferOut.Byte[i] = SPI_TransferRx(DUMMY_BYTE);             //
-    }                                                             //
+    char buffer[3] = {COMM_SPI_READ, 0, 0x00};
+    SPI_TransferTxBuffer(buffer, 3);
+    SPI_TransferRxBuffer(BufferOut.Byte, FST_BYTE_NUM_ROUND_OUT);
     
     SCS_High_macro                                                // disable SPI chip select    
   #endif  
@@ -490,15 +467,10 @@
 
     SCS_Low_macro                                               // enable SPI chip select   
     
-    SPI_TransferTx(COMM_SPI_READ);                              // SPI read command
-    SPI_TransferTx(0x00);                                       // address of the read  
-    SPI_TransferTxLast(0x00);                                   // fifo MsByte first
+    char buffer[3] = {COMM_SPI_READ, 0, 0x00};
+    SPI_TransferTxBuffer(buffer, 3);
+    SPI_TransferRxBuffer(&BufferOut.Byte[64], SEC_BYTE_NUM_ROUND_OUT);
     
-    for (i=0; i< (SEC_BYTE_NUM_ROUND_OUT); i++)                 // transfer loop for the remaining 
-    {                                                           // bytes
-      BufferOut.Byte[i+64] = SPI_TransferRx(DUMMY_BYTE);        // we transfer the second part of
-    }                                                           // the buffer, so offset by 64
-      
     SCS_High_macro                                              // SPI chip select disable  
   #endif    
 }
@@ -512,7 +484,6 @@
                                        // application and that will be sent to the EtherCAT master
 {
   ULONG TempLong;
-  unsigned char i;  
   
   
   
@@ -541,16 +512,9 @@
   
     SCS_Low_macro                                                 // enable SPI chip select
   
-    SPI_TransferTx(COMM_SPI_WRITE);                               // SPI write command
-    SPI_TransferTx(0x00);                                         // address of the write fifo 
-    SPI_TransferTx(0x20);                                         // MsByte first 
-
-    for (i=0; i< (FST_BYTE_NUM_ROUND_IN - 1 ); i++)               // transfer the data
-    {                                                             //
-      SPI_TransferTx (BufferIn.Byte[i]);                          // 
-    }                                                             //
-                                                                  //  
-    SPI_TransferTxLast (BufferIn.Byte[i]);                        // one last byte
+    char buffer[3] = {COMM_SPI_WRITE, 0x00, 0x20};
+    SPI_TransferTxBuffer(buffer, 3);
+    SPI_TransferTxBuffer(BufferIn.Byte, FST_BYTE_NUM_ROUND_IN);
   
     SCS_High_macro                                                // disable SPI chip select           
   #endif        
@@ -568,16 +532,9 @@
                              
     SCS_Low_macro                                               // enable SPI chip select
     
-    SPI_TransferTx(COMM_SPI_WRITE);                             // SPI write command
-    SPI_TransferTx(0x00);                                       // address of the write fifo 
-    SPI_TransferTx(0x20);                                       // MsByte first 
-
-    for (i=0; i< (SEC_BYTE_NUM_ROUND_IN - 1); i++)              // transfer loop for the remaining 
-    {                                                           // bytes
-      SPI_TransferTx (BufferIn.Byte[i+64]);                     // we transfer the second part of
-    }                                                           // the buffer, so offset by 64
-                                                                //  
-    SPI_TransferTxLast (BufferIn.Byte[i+64]);                   // one last byte  
+    char buffer[3] = {COMM_SPI_WRITE, 0x00, 0x20};
+    SPI_TransferTxBuffer(buffer, 3);
+    SPI_TransferTxBuffer(&BufferIn.Byte[64], SEC_BYTE_NUM_ROUND_IN);
 
     SCS_High_macro                                              // disable SPI chip select    
   #endif