/*
 * ethercat_slave_buffer.h
 *
 * Created: 10/03/2021 13:10:51
 *  Author: Nick-XMG
 */ 


#ifndef ETHERCAT_SLAVE_DEF_H_
#define ETHERCAT_SLAVE_DEF_H_

#include "ethercat_e54.h"

//Write To Ecat Total Bytes (38 bytes)
//write (2 Bytes)
static volatile uint8_t  *status				=&ram_buffer[ram_wr_start];
static volatile uint8_t  *state				=(((uint8_t *)&ram_buffer[ram_wr_start])+1);
//Joint (10 Bytes)
static volatile int16_t  *joint_rel_position	=&ram_buffer[ram_wr_start+1];
static volatile int16_t  *joint_revolution		=&ram_buffer[ram_wr_start+2];
static volatile int16_t  *joint_abs_position	=&ram_buffer[ram_wr_start+3];
static volatile int16_t  *joint_speed			=&ram_buffer[ram_wr_start+4];
static volatile int16_t  *joint_torque			=&ram_buffer[ram_wr_start+5];
// Motor (24+1+1) = 26
static volatile int16_t  *motor_rel_revolutions=&ram_buffer[ram_wr_start+6];
static volatile int16_t  *motor_rel_position	=&ram_buffer[ram_wr_start+7];
static volatile int16_t  *motor_abs_position	=&ram_buffer[ram_wr_start+8];
static volatile int16_t  *motor_dutyCycle		=&ram_buffer[ram_wr_start+9];
static volatile int16_t  *motor_speed			=&ram_buffer[ram_wr_start+10];
static volatile int16_t  *motor_torque			=&ram_buffer[ram_wr_start+11];
static volatile int16_t  *motor_currentPHA		=&ram_buffer[ram_wr_start+12]; 
static volatile int16_t  *motor_currentPHB		=&ram_buffer[ram_wr_start+13]; 
static volatile int16_t  *motor_currentPHC		=&ram_buffer[ram_wr_start+14]; 
static volatile int16_t  *motor_currentBUS		=&ram_buffer[ram_wr_start+15]; 
static volatile uint8_t  *hall_state			=&ram_buffer[ram_wr_start+16];
static volatile uint8_t  *Spare_byte1			=(((uint8_t *)&ram_buffer[ram_wr_start+16])+1);
static volatile int16_t  *Spare_1				=&ram_buffer[ram_wr_start+17];
static volatile int16_t  *Spare_2				=&ram_buffer[ram_wr_start+18];

//Read From Ecat Total (35 Bytes)
// (1 Byte)
static volatile uint8_t *control_mode			=&ram_buffer[ram_rd_start];
static volatile uint8_t *control_set			=(((uint8_t *)&ram_buffer[ram_rd_start])+1);
// (34 Byte)
static volatile int16_t *desired_position		=&ram_buffer[ram_rd_start+1];
static volatile int16_t *desired_speed			=&ram_buffer[ram_rd_start+2];
static volatile int16_t *desired_torque		=&ram_buffer[ram_rd_start+3];
static volatile int16_t *i_kp					=&ram_buffer[ram_rd_start+4];
static volatile int16_t *i_ki					=&ram_buffer[ram_rd_start+5];
static volatile int16_t *v_kp					=&ram_buffer[ram_rd_start+6];
static volatile int16_t *v_kd					=&ram_buffer[ram_rd_start+7];
static volatile int16_t *p_kp					=&ram_buffer[ram_rd_start+8];
static volatile int16_t *p_ki					=&ram_buffer[ram_rd_start+9];
static volatile uint16_t *ReductionRatio		=&ram_buffer[ram_rd_start+10];
static volatile int16_t *max_torque			=&ram_buffer[ram_rd_start+11];
static volatile int16_t *max_current			=&ram_buffer[ram_rd_start+12];
static volatile int16_t *max_velocity			=&ram_buffer[ram_rd_start+13];
static volatile int16_t *spare1				=&ram_buffer[ram_rd_start+14];
static volatile int16_t *spare2				=&ram_buffer[ram_rd_start+15];
static volatile int16_t *spare3				=&ram_buffer[ram_rd_start+16];
static volatile int16_t *spare4				=&ram_buffer[ram_rd_start+17];

inline void comms_check(void)
{
	*status					= 1;
	*state					= 0;
	*joint_rel_position		= 3;
	*joint_revolution	    = 4;
	*joint_abs_position		= 5;
	*joint_speed			= 6;
	*joint_torque			= 7;
	*motor_rel_revolutions	= 8;
	*motor_rel_position		= 9;
	*motor_abs_position		= 10;
	*motor_dutyCycle		= 11;
	*motor_speed			= 12;
	*motor_torque			= 13;
	*motor_currentPHA		= 14;
	*motor_currentPHB		= 15;
	*motor_currentPHC		= 16;
	*motor_currentBUS		= 17;
	*hall_state				= 18;
	*Spare_byte1			= 19;
	*Spare_1				= 20;
	*Spare_2				= 21;
}

inline void clear_comms_buffer(void)
{
	memset(ram_buffer, 0, ram_rd_start);
}

#endif /* ETHERCAT_SLAVE_DEF_H_ */