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

Committer:
pratima_hb
Date:
Thu Feb 06 17:35:14 2020 +0000
Revision:
2:77c2a6061e77
Parent:
1:b66c3e4ce9f5
Updated the data types(to int16_t) of the read and write function to be able to send and receive negative numbers

Who changed what in which revision?

UserRevisionLine numberNew contents of line
pratima_hb 1:b66c3e4ce9f5 1 /**
pratima_hb 1:b66c3e4ce9f5 2 Copyright (c) 2020 https://www.esmacat.com/
pratima_hb 1:b66c3e4ce9f5 3
pratima_hb 1:b66c3e4ce9f5 4 Licensed under the Apache License, Version 2.0 (the "License");
pratima_hb 1:b66c3e4ce9f5 5 you may not use this file except in compliance with the License.
pratima_hb 1:b66c3e4ce9f5 6 You may obtain a copy of the License at
pratima_hb 1:b66c3e4ce9f5 7
pratima_hb 1:b66c3e4ce9f5 8 http://www.apache.org/licenses/LICENSE-2.0
pratima_hb 1:b66c3e4ce9f5 9
pratima_hb 1:b66c3e4ce9f5 10 Unless required by applicable law or agreed to in writing, software
pratima_hb 1:b66c3e4ce9f5 11 distributed under the License is distributed on an "AS IS" BASIS,
pratima_hb 1:b66c3e4ce9f5 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
pratima_hb 1:b66c3e4ce9f5 13 See the License for the specific language governing permissions and
pratima_hb 1:b66c3e4ce9f5 14 limitations under the License.
pratima_hb 1:b66c3e4ce9f5 15
pratima_hb 1:b66c3e4ce9f5 16 EsmacatShield.cpp - Library for using EtherCAT Arduino Shield by Esmacat(EASE).
pratima_hb 1:b66c3e4ce9f5 17 Created by Esmacat, 01/22/2020
pratima_hb 1:b66c3e4ce9f5 18
pratima_hb 1:b66c3e4ce9f5 19 *******************************************************************************
pratima_hb 1:b66c3e4ce9f5 20 * @file EsmacatShield.cpp
pratima_hb 1:b66c3e4ce9f5 21 *******************************************************************************
pratima_hb 1:b66c3e4ce9f5 22 */
pratima_hb 1:b66c3e4ce9f5 23
pratima_hb 0:4a9e3331b131 24 #include "EsmacatShield.h"
pratima_hb 0:4a9e3331b131 25
pratima_hb 0:4a9e3331b131 26 EsmacatShield::EsmacatShield(SPI &spi, DigitalOut &pin):ecat_spi(spi),ecat_cs(pin)
pratima_hb 0:4a9e3331b131 27 {
pratima_hb 0:4a9e3331b131 28
pratima_hb 0:4a9e3331b131 29 }
pratima_hb 0:4a9e3331b131 30
pratima_hb 0:4a9e3331b131 31 void EsmacatShield::setup_spi()
pratima_hb 0:4a9e3331b131 32 {
pratima_hb 0:4a9e3331b131 33 /*Chip must be deselected*/
pratima_hb 0:4a9e3331b131 34 ecat_cs = 1;
pratima_hb 0:4a9e3331b131 35
pratima_hb 0:4a9e3331b131 36 /*Setup the spi for 8 bit data, Mode 1,
pratima_hb 0:4a9e3331b131 37 with a 3MHz clock rate*/
pratima_hb 0:4a9e3331b131 38 ecat_spi.format(8,1);
pratima_hb 0:4a9e3331b131 39 ecat_spi.frequency(3000000);
pratima_hb 0:4a9e3331b131 40
pratima_hb 0:4a9e3331b131 41 /* Chip must be selected*/
pratima_hb 0:4a9e3331b131 42 ecat_cs = 0;
pratima_hb 0:4a9e3331b131 43
pratima_hb 0:4a9e3331b131 44 }
pratima_hb 0:4a9e3331b131 45
pratima_hb 0:4a9e3331b131 46
pratima_hb 2:77c2a6061e77 47 void EsmacatShield::write_reg_value(int write_addr, int16_t value, bool led_on)
pratima_hb 0:4a9e3331b131 48 {
pratima_hb 0:4a9e3331b131 49 uint8_t v1,v2;
pratima_hb 0:4a9e3331b131 50 // Chip must be selected
pratima_hb 0:4a9e3331b131 51 ecat_cs = 0;
pratima_hb 0:4a9e3331b131 52 wait_us(2000); //sleep for 2 ms;
pratima_hb 0:4a9e3331b131 53 write_addr = write_addr <<3;
pratima_hb 0:4a9e3331b131 54 if (led_on)
pratima_hb 0:4a9e3331b131 55 {
pratima_hb 0:4a9e3331b131 56 ecat_spi.write(((EASE_WRITE_REG|write_addr)| EASE_LED_ON)& EASE_SINGLE_SHOT);
pratima_hb 0:4a9e3331b131 57 }
pratima_hb 0:4a9e3331b131 58 else
pratima_hb 0:4a9e3331b131 59 {
pratima_hb 0:4a9e3331b131 60 ecat_spi.write((EASE_WRITE_REG|write_addr)& EASE_LED_OFF & EASE_SINGLE_SHOT);
pratima_hb 0:4a9e3331b131 61 }
pratima_hb 0:4a9e3331b131 62 v1 = (value&0xFF00) >> 8;
pratima_hb 0:4a9e3331b131 63 v2 = (value&0x00FF);
pratima_hb 0:4a9e3331b131 64 ecat_spi.write(v1);
pratima_hb 0:4a9e3331b131 65 ecat_spi.write(v2);
pratima_hb 0:4a9e3331b131 66 // Chip must be deselected
pratima_hb 0:4a9e3331b131 67 ecat_cs = 1;
pratima_hb 0:4a9e3331b131 68 wait_us(2000); //sleep for 2 ms;
pratima_hb 0:4a9e3331b131 69 }
pratima_hb 0:4a9e3331b131 70
pratima_hb 2:77c2a6061e77 71 int16_t* EsmacatShield::get_ecat_registers(int16_t regs[8]) {
pratima_hb 0:4a9e3331b131 72 regs[0] = read_reg_value(1);
pratima_hb 0:4a9e3331b131 73 regs[1] = read_reg_value(2);
pratima_hb 0:4a9e3331b131 74 regs[2] = read_reg_value(3);
pratima_hb 0:4a9e3331b131 75 regs[3] = read_reg_value(4);
pratima_hb 0:4a9e3331b131 76 regs[4] = read_reg_value(5);
pratima_hb 0:4a9e3331b131 77 regs[5] = read_reg_value(6);
pratima_hb 0:4a9e3331b131 78 regs[6] = read_reg_value(7);
pratima_hb 0:4a9e3331b131 79 regs[7] = read_reg_value(0);
pratima_hb 0:4a9e3331b131 80 return(regs);
pratima_hb 0:4a9e3331b131 81 }
pratima_hb 0:4a9e3331b131 82
pratima_hb 2:77c2a6061e77 83 int16_t EsmacatShield::read_reg_value(int16_t read_addr)
pratima_hb 0:4a9e3331b131 84 {
pratima_hb 0:4a9e3331b131 85 uint16_t v2,v3;
pratima_hb 0:4a9e3331b131 86 // Chip must be selected
pratima_hb 0:4a9e3331b131 87 ecat_cs = 0;
pratima_hb 0:4a9e3331b131 88 wait_us(2000); //sleep for 2 ms;
pratima_hb 0:4a9e3331b131 89 read_addr = read_addr <<3;
pratima_hb 0:4a9e3331b131 90 ecat_spi.write( EASE_READ_REG|read_addr);
pratima_hb 0:4a9e3331b131 91 v2 = ecat_spi.write(0x00);
pratima_hb 0:4a9e3331b131 92 v3 = ecat_spi.write(0x00);
pratima_hb 0:4a9e3331b131 93 // Chip must be deselected
pratima_hb 0:4a9e3331b131 94 ecat_cs = 1;
pratima_hb 0:4a9e3331b131 95 wait_us(2000); //sleep for 2 ms;
pratima_hb 0:4a9e3331b131 96
pratima_hb 0:4a9e3331b131 97 return (v2<<8)+v3;
pratima_hb 0:4a9e3331b131 98 }
pratima_hb 0:4a9e3331b131 99
pratima_hb 0:4a9e3331b131 100 EsmacatShield::~EsmacatShield(void)
pratima_hb 0:4a9e3331b131 101 {
pratima_hb 0:4a9e3331b131 102 //empty block
pratima_hb 0:4a9e3331b131 103 }
pratima_hb 0:4a9e3331b131 104