Pratima Gokhale / EsmacatShield

Dependents:   EASE_Example HelloWorld_EASE_Proximity_Sensor HelloWorld_EASE_Motor_Example Example_EtherCAT_System_Using_EASE ... more

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers EsmacatShield.cpp Source File

EsmacatShield.cpp

Go to the documentation of this file.
00001 /**
00002   Copyright (c) 2020 https://www.esmacat.com/
00003 
00004   Licensed under the Apache License, Version 2.0 (the "License");
00005   you may not use this file except in compliance with the License.
00006   You may obtain a copy of the License at
00007 
00008     http://www.apache.org/licenses/LICENSE-2.0
00009 
00010   Unless required by applicable law or agreed to in writing, software
00011   distributed under the License is distributed on an "AS IS" BASIS,
00012   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013   See the License for the specific language governing permissions and
00014   limitations under the License.
00015 
00016   EsmacatShield.cpp - Library for using EtherCAT Arduino Shield by Esmacat(EASE).
00017   Created by Esmacat, 01/22/2020
00018 
00019 *******************************************************************************
00020 * @file EsmacatShield.cpp
00021 *******************************************************************************
00022 */
00023 
00024 #include "EsmacatShield.h"
00025 
00026 EsmacatShield::EsmacatShield(SPI &spi, DigitalOut &pin):ecat_spi(spi),ecat_cs(pin) 
00027 {
00028      
00029 }
00030 
00031 void EsmacatShield::setup_spi()
00032 {
00033     /*Chip must be deselected*/
00034     ecat_cs = 1;
00035 
00036   /*Setup the spi for 8 bit data, Mode 1,
00037     with a 3MHz clock rate*/
00038     ecat_spi.format(8,1);
00039     ecat_spi.frequency(3000000);
00040     
00041     /* Chip must be selected*/
00042     ecat_cs = 0;
00043 
00044 }
00045 
00046 
00047 void EsmacatShield::write_reg_value(int write_addr, int16_t value, bool led_on)
00048 {
00049   uint8_t v1,v2;
00050   // Chip must be selected
00051   ecat_cs = 0;
00052   wait_us(2000);                       //sleep for 2 ms;
00053   write_addr = write_addr <<3;
00054   if (led_on)
00055   {
00056     ecat_spi.write(((EASE_WRITE_REG|write_addr)| EASE_LED_ON)& EASE_SINGLE_SHOT);
00057   }
00058   else
00059   {
00060     ecat_spi.write((EASE_WRITE_REG|write_addr)& EASE_LED_OFF & EASE_SINGLE_SHOT); 
00061   }
00062   v1 = (value&0xFF00) >> 8;
00063   v2 = (value&0x00FF);
00064   ecat_spi.write(v1);
00065   ecat_spi.write(v2);
00066   // Chip must be deselected
00067   ecat_cs = 1;
00068   wait_us(2000);                       //sleep for 2 ms;
00069 }
00070 
00071 int16_t* EsmacatShield::get_ecat_registers(int16_t regs[8]) {
00072   regs[0] = read_reg_value(1);
00073   regs[1] = read_reg_value(2);
00074   regs[2] = read_reg_value(3);
00075   regs[3] = read_reg_value(4);
00076   regs[4] = read_reg_value(5);
00077   regs[5] = read_reg_value(6);
00078   regs[6] = read_reg_value(7);
00079   regs[7] = read_reg_value(0);
00080   return(regs);
00081 }
00082 
00083 int16_t EsmacatShield::read_reg_value(int16_t read_addr)
00084 {
00085   uint16_t v2,v3;
00086   // Chip must be selected
00087   ecat_cs = 0;
00088   wait_us(2000);                       //sleep for 2 ms;
00089   read_addr = read_addr <<3;
00090   ecat_spi.write( EASE_READ_REG|read_addr);
00091   v2 = ecat_spi.write(0x00);
00092   v3 = ecat_spi.write(0x00);
00093   // Chip must be deselected
00094   ecat_cs = 1;
00095   wait_us(2000);                      //sleep for 2 ms;
00096 
00097   return (v2<<8)+v3;
00098 }
00099 
00100 EsmacatShield::~EsmacatShield(void) 
00101 {
00102   //empty block
00103 }
00104