EasyCAT shield library - It allows you to make an EtherCAT slave

Dependents:   TestEasyCAT_SM_sync TestEasyCAT_LoopBack TestEasyCAT_DC_sync TestEasyCAT

The EasyCAT Shield and /static/img/mbed.gif boards

/media/uploads/EasyCAT/easycat_onnucleo.jpg

  • The EasyCAT Shield is an Arduino shield, designed and manufactured in Italy by AB&T Tecnologie Informatiche, that allow us to build a custom EtherCAT® slave in an easy way.
  • The EasyCAT Shield uses the 3x2 SPI connector to communicate with the microcontroller. This connector is standard on all the Arduino boards but some Arduino compatible boards don’t provide it. In this case, the SPI signal are always present on pins 13,12,and 11. Some examples of this boards are the STM32 Nucleo and the NXP LPCXpresso, part of the Mbed ecosystem.
  • To address this issue in the EasyCAT Shield revision “C” there are three solder jumpers, on the bottom side of the board, that allow us to connect the SPI signals, SCK,MISO and MOSI, also on pins 13, 12 and 11.

/media/uploads/EasyCAT/spi_selection_jumpered.jpg

  • For your convenience the EasyCAT Shield can be ordered with the three solder jumpers already bridged and with the 3x2 connector not installed on the board. To request this option select EasyCAT spi_on_13_12_11 in the webshop.

Import libraryEasyCAT_lib

EasyCAT shield library - It allows you to make an EtherCAT slave

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