EasyCAT test with SM synchronization - EtherCAT slave example

Dependencies:   mbed EasyCAT_lib

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 TestEasyCAT_SM_sync program is a basic example that shows how to excange some I/O between the EtherCAT master and the EasyCAT slave, using the Synchronization Manager synchronization.

Import programTestEasyCAT_SM_sync

EasyCAT test with SM synchronization - EtherCAT slave example

Import libraryEasyCAT_lib

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

main.cpp

Committer:
EasyCAT
Date:
2019-01-09
Revision:
1:a1411df7f601
Parent:
0:ec011e648d2e

File content as of revision 1:a1411df7f601:

//********************************************************************************************
//                                                                                           *
// AB&T Tecnologie Informatiche - Ivrea Italy                                                *
// http://www.bausano.net                                                                    *
// https://www.ethercat.org/en/products/791FFAA126AD43859920EA64384AD4FD.htm                 *
//                                                                                           *  
//********************************************************************************************    
//                                                                                           *
// This software is distributed as an example, in the hope that it could be useful,          *
// WITHOUT ANY WARRANTY, even the implied warranty of FITNESS FOR A PARTICULAR PURPOSE       *
//                                                                                           *
//******************************************************************************************** 


// revision 2 - moved  "DigitalOut Led(LED1)"


//- EasyCAT shield application basic example 170912
//- using SM syncronization for mbed boards 
//
//- Derived from the example project TestEasyCAT_SM_sync.ino for the AB&T EasyCAT Arduino shield
//
//- In this example we poll the interrupt pin coming from the LAN9252, so not a real interrupt,
//- to synchronize the EasyCAT and the Master activities


//- Tested with the STM32 NUCLEO-F767ZI board 



#include "mbed.h"   
#include "EasyCAT.h"                // EasyCAT library to interface the LAN9252
    

void Application (void); 
void SafeOut(void);


EasyCAT EASYCAT(SM_SYNC);           // EasyCAT instantiation
                                    // In order to use the SM Synchronization we have to set 
                                    // the "SM_SYNC" parameter in the constructor 

                                    // The constructor allow us to choose the pin used for the EasyCAT SPI chip select 
                                    // Without any parameter pin 9 will be used 
                                                                      
                                    // for EasyCAT board REV_A we can choose between:
                                    // D8, D9, D10 
                                    //  
                                    // for EasyCAT board REV_B we can choose between:
                                    // D8, D9, D10, A5, D6, D7                                    

                                    // example:                                  
//EasyCAT EASYCAT(D8, SM_SYNC);     // pin D8 will be used as SPI chip select


                                    // The chip select chosen by the firmware must match the setting on the board

                                    // On board REV_A the chip select is set soldering
                                    // a 0 ohm resistor in the appropriate position

                                    // On board REV_B the chip select is set
                                    // througt a bank of jumpers                                    



//---- pins declaration ------------------------------------------------------------------------------



AnalogIn Ana0(A0);               // analog input 0
AnalogIn Ana1(A1);               // analog input 1


DigitalOut Out_0(A2);            // four bits output
DigitalOut Out_1(A3);            // 
DigitalOut Out_2(A4);            // 
DigitalOut Out_3(A5);            // 

DigitalIn In_0(D3);              // four bits input
DigitalIn In_1(D5);              // 
DigitalIn In_2(D6);              // 
DigitalIn In_3(D7);              //     

  


DigitalIn InterruptPin(D2);      // This pin receive the SM interrupt 
                                 // generated by the LAN9252
                                 // We poll it, so it is not used as an interrupt



//---- global variables ---------------------------------------------------------------------------


UWORD ContaUp;                      // used for sawthoot test generation
UWORD ContaDown;                    //

unsigned long Millis = 0;
unsigned long PreviousSaw = 0;
unsigned long PreviousCycle = 0;
unsigned long PreviousMillis = 0;


//---- declarations for Arduino "millis()" emulation ----------------------- 

static Ticker uS_Tick;
static volatile uint32_t MillisVal = 0;

void InitMillis(void);    
void mS_Tick(void);
  
inline static uint32_t millis (void) 
{
    return MillisVal; 
};
 
      

//---------------------------------------------------------------------------------------------
 
int main(void)
{
          
  printf ("\nEasyCAT - Generic EtherCAT slave\n");      // print the banner

  InitMillis();                                         // init Arduino "millis()" emulation                

  ContaDown.Word = 0x0000;
  ContaUp.Word = 0x0000; 
                                                        //---- initialize the EasyCAT board -----
                                                                  
  if (EASYCAT.Init() == true)                           // initialization
  {                                                     // succesfully completed
    printf ("initialized\n");                           //
  }                                                            
  
  else                                                  // initialization failed   
  {                                                     // the EasyCAT board was not recognized
    printf ("initialization failed\n");                 //     
                                                        // The most common reason is that the SPI 
                                                        // chip select choosen on the board doesn't 
                                                        // match the one choosen by the firmware
                                                           
    DigitalOut Led(LED1);                               //                                                           
                                                                  
    while (1)                                           // stay in loop for ever
    {                                                   // with the led blinking
        Led = 1;                                        //    
        wait_ms(125);                                   //   
        Led = 0;                                        //
        wait_ms(125);                                   //
    }                                                   // 
  }   
   
 
  while (1)                                             
  {
      
    #define WatchDogTime 100

                                                       
    Millis = millis();                                        // if there is no INT signal from the LAN9252 for more
    if (Millis - PreviousMillis >= WatchDogTime)              // than 100mS we put the output in a safe state
    {                                                         // 
      PreviousMillis = Millis;                                //
                                                            //
      SafeOut();                                              //
    }                                                         //


    if (InterruptPin == 0)                         // If the INT signal is detected LOW
    {                                                         // 

      EASYCAT.MainTask();                                     // We execute the EtherCAT task
                                                            //      
      Application();                                          // and the user application   

     PreviousMillis = millis();                              // refresh the watchdog timer
    }
  }  
}
      
  
  
  
  
  


//---- user application ------------------------------------------------------------------------------

void Application (void)                                        

{ 
  float Analog; 
                                                      // --- analog inputs management ---
                                                      //
  Analog = Ana0.read();                               // read analog input 0
  Analog = Analog * 255;                              // normalize it on 8 bits   
  EASYCAT.BufferIn.Byte[0] = (uint8_t)Analog;         // and put the result into
                                                      // input Byte 0 
                                                      
  Analog = Ana1.read();                               // read analog input 1
  Analog = Analog * 255;                              // normalize it on 8 bits  
  EASYCAT.BufferIn.Byte[1] = (uint8_t)Analog;         // and put the result into
                                                      // input Byte 1    


                                                      // --- four output bits management ----
                                                      //       
  if (EASYCAT.BufferOut.Byte[0] & 0b00000001)         // the four output bits are mapped to the
    Out_0 = 1;                                        // lower nibble of output Byte 0
  else                                                //
    Out_0 = 0;                                        // 
                                                      //
  if (EASYCAT.BufferOut.Byte[0] & (1<<0))             // 
    Out_0 = 1;                                        //
  else                                                //
    Out_0 = 0;                                        // 
                                                      //
  if (EASYCAT.BufferOut.Byte[0] & (1<<1))             // 
    Out_1 = 1;                                        //
  else                                                //
    Out_1 = 0;                                        // 
                                                      //
  if (EASYCAT.BufferOut.Byte[0] & (1<<2))             // 
    Out_2 = 1;                                        //
  else                                                //
    Out_2 = 0;                                        // 
                                                      //
  if (EASYCAT.BufferOut.Byte[0] & (1<<3))             // 
    Out_3 = 1;                                        //
  else                                                //
    Out_3 = 0;                                        //         

                                                      //--- four input bits management ---
                                                      //  
  EASYCAT.BufferIn.Byte[6] = 0x00;                    // the four input pins are mapped to the     
  if (!In_0)                                          // lower nibble of input Byte 6
    EASYCAT.BufferIn.Byte[6] |= 0b00000001;           //    
  if (!In_1)                                          //        
    EASYCAT.BufferIn.Byte[6] |= 0b00000010;           //    
  if (!In_2)                                          //
    EASYCAT.BufferIn.Byte[6] |= 0b00000100;           //
  if (!In_3)                                          //
    EASYCAT.BufferIn.Byte[6] |= 0b00001000;           //
    
  
                                                      // --- test sawtooth generation --- 
                                                      //
  Millis = millis();                                  // each 100 mS
  
  if (Millis - PreviousSaw >= 100)                    // 
  {                                                   // 
    PreviousSaw = Millis;                             //
                                                      //
    ContaUp.Word++;                                   // we increment the variable ContaUp  
    ContaDown.Word--;                                 // and decrement ContaDown   
  }                                                   //

                                                      // we use these variables to create sawtooth,
                                                      // with different slopes and periods, for
                                                      // test pourpose, in input Bytes 2,3,4,5,30,31
                                                      
  EASYCAT.BufferIn.Byte[2] = ContaUp.Byte[0];         // slow rising slope
  EASYCAT.BufferIn.Byte[3] = ContaUp.Byte[1];         // extremly slow rising slope
  
  EASYCAT.BufferIn.Byte[4] = ContaDown.Byte[0];       // slow falling slope
  EASYCAT.BufferIn.Byte[5] = ContaDown.Byte[1];       // extremly slow falling slope
 
 
  EASYCAT.BufferIn.Byte[30] = ContaUp.Byte[0] << 2;   // medium speed rising slope
  EASYCAT.BufferIn.Byte[31] = ContaDown.Byte[0] << 2; // medium speed falling slope    
}   


//------------------------------------------------------------------------------------------------

void SafeOut(void)

{
  Out_0 = 0; 
  Out_1 = 0; 
  Out_2 = 0;    
  Out_3 = 0; 
}


//--- functions for Arduino "millis()" emulation -------------------------------------


void InitMillis(void) 
{
  uS_Tick.attach (&mS_Tick, 0.001);        
}

void mS_Tick(void)
{
  MillisVal++;
}