EsmacatShield - Library for EtherCAT Arduino Shield by Esmacat (EASE)

Dependents:   EASE_Example HelloWorld_EASE_Proximity_Sensor HelloWorld_EASE_Motor_Example Example_EtherCAT_System_Using_EASE ... more

Information about Esmacat and EASE is provided in the link below. https://os.mbed.com/users/pratima_hb/code/EASE_Example/wiki/Homepage

EsmacatShield.cpp

Committer:
pratima_hb
Date:
2020-01-29
Revision:
0:4a9e3331b131
Child:
1:b66c3e4ce9f5

File content as of revision 0:4a9e3331b131:

#include "EsmacatShield.h"

EsmacatShield::EsmacatShield(SPI &spi, DigitalOut &pin):ecat_spi(spi),ecat_cs(pin) 
{
     
}

void EsmacatShield::setup_spi()
{
    /*Chip must be deselected*/
    ecat_cs = 1;

  /*Setup the spi for 8 bit data, Mode 1,
    with a 3MHz clock rate*/
    ecat_spi.format(8,1);
    ecat_spi.frequency(3000000);
    
    /* Chip must be selected*/
    ecat_cs = 0;

}


void EsmacatShield::write_reg_value(int write_addr,int value, bool led_on)
{
  uint8_t v1,v2;
  // Chip must be selected
  ecat_cs = 0;
  wait_us(2000);                       //sleep for 2 ms;
  write_addr = write_addr <<3;
  if (led_on)
  {
    ecat_spi.write(((EASE_WRITE_REG|write_addr)| EASE_LED_ON)& EASE_SINGLE_SHOT);
  }
  else
  {
    ecat_spi.write((EASE_WRITE_REG|write_addr)& EASE_LED_OFF & EASE_SINGLE_SHOT); 
  }
  v1 = (value&0xFF00) >> 8;
  v2 = (value&0x00FF);
  ecat_spi.write(v1);
  ecat_spi.write(v2);
  // Chip must be deselected
  ecat_cs = 1;
  wait_us(2000);                       //sleep for 2 ms;
}

int* EsmacatShield::get_ecat_registers(int regs[8]) {
  regs[0] = read_reg_value(1);
  regs[1] = read_reg_value(2);
  regs[2] = read_reg_value(3);
  regs[3] = read_reg_value(4);
  regs[4] = read_reg_value(5);
  regs[5] = read_reg_value(6);
  regs[6] = read_reg_value(7);
  regs[7] = read_reg_value(0);
  return(regs);
}

int EsmacatShield::read_reg_value(int read_addr)
{
  uint16_t v2,v3;
  // Chip must be selected
  ecat_cs = 0;
  wait_us(2000);                       //sleep for 2 ms;
  read_addr = read_addr <<3;
  ecat_spi.write( EASE_READ_REG|read_addr);
  v2 = ecat_spi.write(0x00);
  v3 = ecat_spi.write(0x00);
  // Chip must be deselected
  ecat_cs = 1;
  wait_us(2000);                      //sleep for 2 ms;

  return (v2<<8)+v3;
}

EsmacatShield::~EsmacatShield(void) 
{
  //empty block
}