#include "DSP2833x_Device.h"     // DSP2833x Headerfile Include File
#include "package.h"
#include "RS485.h"
#include "crc16.h"
#include "cntrl_adr.h"
#include "bios_dsp.h"      
#include "filter_bat2.h"
#include "measure.h"
#include "message.h"


#include "ADC.h"
#include "peripher.h"

#include "ecan.h"
#include "spise2p.h"
#include "i2c.h"

int modbus[ANSWER_LEN+1];
unsigned int param[ANSWER_LEN+1];

int calibr[32];

LONGE* outputs;

void Default_params()
{
  unsigned int i;

	for(i=0;i<ANSWER_LEN;i++)
	{
		modbus[i] = 0;	
	}

	Brightness = 8;

	LastMode = Addrr;

	Cancount[m_FAST] = 10;	//	пауза между посылками CAN
	Cancount[m_SLOW] = 200;	//	пауза между посылками CAN

	for(i=0;i<24;i++)
	{
		if(sens_type[i]==TERMO_AD)
		{
			sens_hi_edge[i] = 600;	
			sens_lo_edge[i] = 500;
}	}	}

void Load_params()
{
  unsigned int i,crc;

	Seeprom_read(0x3FFF-(ANSWER_LEN+1), param, (ANSWER_LEN+1)*2);
	crc = get_crc16(param,ANSWER_LEN);
	if(	(crc==param[ANSWER_LEN]) && 
		(crc !=0xFFFF) && 
		(Addrr == param[126])	)
	{
		for(i=0;i<ANSWER_LEN;i++) Modbus[i].all = param[i];
		Commands=0;
	}
	else
	{
		Default_params();
		Commands=0;
		Save_params();
}	}

void Save_params()
{
  unsigned int i,dif=0;
	
	for(i=0;i<ANSWER_LEN;i++)
	if(param[i] != (unsigned int)Modbus[i].all)
	{
		param[i] = Modbus[i].all;
		dif=1;
	}	
	if(dif)
	{
		param[ANSWER_LEN] = get_crc16(param,ANSWER_LEN);
		Seeprom_write(0x3FFF-(ANSWER_LEN+1),param,(ANSWER_LEN+1)*2);
	}
}

void Load_caliber()
{
  unsigned int buf[2],crc;
  int i,line;
  int adcLOW,adcHI;
  float tmpLOW,tmpHI;

	for(i=0;i<12;i++)
	{
		line = i*4;
//-------------------------------------------------------------------
		buf[0]= I2CA_ReadData(line);	DSP28x_usDelay(500);
		buf[1]= I2CA_ReadData(line+1);	DSP28x_usDelay(500);
		crc =	I2CA_ReadData(line+70);	DSP28x_usDelay(500);
		if(crc!=get_crc16(buf,2))
		{
			if(sens_type[i]==TERMO_AD)	{	buf[0] = 330;	buf[1] = 30;	}
			else						{	buf[0] = 0;		buf[1] = 273;	}	
		}
		adcLOW = buf[0];
		tmpLOW = buf[1];
//-------------------------------------------------------------------
		buf[0]= I2CA_ReadData(line+2);	DSP28x_usDelay(500);
		buf[1]= I2CA_ReadData(line+3);	DSP28x_usDelay(500);
		crc =	I2CA_ReadData(line+72);	DSP28x_usDelay(500);
		if(crc!=get_crc16(buf,2))
		{
			if(sens_type[i]==TERMO_AD)	{	buf[0] = 4000;	buf[1] = 360;	}
			else						{	buf[0] = 3790;	buf[1] = 473;	}	
		}
		adcHI = buf[0];
		tmpHI = buf[1];
//-------------------------------------------------------------------
		adc0[i] = adcLOW;
		tmp0[i] = tmpLOW;
		tmpK[i] = (tmpHI - tmpLOW)/(adcHI - adcLOW);
	}
		
	for(i=12;i<16;i++)
	{
		line = i*4;
//-------------------------------------------------------------------
		buf[0]= I2CA_ReadData(line);	DSP28x_usDelay(500);
		buf[1]= I2CA_ReadData(line+1);	DSP28x_usDelay(500);
		crc =	I2CA_ReadData(line+70);	DSP28x_usDelay(500);
		if(crc!=get_crc16(buf,2))
		{
			buf[0] = 2047;	buf[1] = 0;
		}
		adcLOW = buf[0];
		tmpLOW = buf[1];
//-------------------------------------------------------------------
		buf[0]= I2CA_ReadData(line+2);	DSP28x_usDelay(500);
		buf[1]= I2CA_ReadData(line+3);	DSP28x_usDelay(500);
		crc =	I2CA_ReadData(line+72);	DSP28x_usDelay(500);
		if(crc!=get_crc16(buf,2))
		{
			buf[0] = 2310;	buf[1] = 0;	// #define cur_K	2.31//2.352
		}
		adcHI = buf[0];
		tmpHI = buf[1];
//-------------------------------------------------------------------
		adc0[i] = adcLOW;
		tmp0[i] = tmpLOW;
		tmpK[i] = adcHI/1000.0;
}	}

void Save_caliber()
{
  unsigned int buf[2],crc;
  int i,line;

	for(i=12;i<16;i++)
	{
		line = i*4;

		buf[0] = adc0[i];	buf[1] = 0;
		crc=get_crc16(buf,2);

		I2CA_WriteData(line,   buf[0]);	DSP28x_usDelay(100000L);
		I2CA_WriteData(line+1, buf[1]);	DSP28x_usDelay(100000L);
		I2CA_WriteData(line+70,crc);	DSP28x_usDelay(100000L);

		buf[0] = tmpK[i]*1000;	buf[1] = 0;
		crc=get_crc16(buf,2);

		I2CA_WriteData(line+2, buf[0]);	DSP28x_usDelay(100000);
		I2CA_WriteData(line+3, buf[1]);	DSP28x_usDelay(100000L);
		I2CA_WriteData(line+72,crc);	DSP28x_usDelay(100000L);
	}
	i=1;
}
/***************************************************************/
/*  Передача данных по протоколу ModBus - команда 3
    Чтение ачеек данных    		*/
/***************************************************************/
void ReceiveCommandModbus3(RS_DATA *rs_arr)
{
  unsigned int crc, Address_MB, Length_MB, cal, i;

//	получили начальный адрес чтениа
	Address_MB =/*(rs_arr->RS_Header[2] << 8) |*/ rs_arr->RS_Header[3];

//	получили количество слов данных
	Length_MB = (rs_arr->RS_Header[4] << 8) | rs_arr->RS_Header[5];

	cal = rs_arr->RS_Header[2];

	/////////////////////////////////////////////////
	// Отсылка
	/* Посчитали контрольную сумму перед самой посылкой */	

	rs_arr->buffer[0] =  CNTRL_ADDR;
	rs_arr->buffer[1] =  CMD_MODBUS_3;
	rs_arr->buffer[2] =  Length_MB*2;

	for (i=0;i<Length_MB;i++)
	{
		if(cal)
		{
	   		rs_arr->buffer[3+i*2  ]=HIBYTE(calibr[Address_MB+i-start_data]);
	   		rs_arr->buffer[3+i*2+1]=LOBYTE(calibr[Address_MB+i-start_data]);
		}
		else
		{
	   		rs_arr->buffer[3+i*2  ]=(Modbus[Address_MB+i].byt.byte_hi);
	   		rs_arr->buffer[3+i*2+1]=(Modbus[Address_MB+i].byt.byte_lo);
	}	}

    crc = 0xffff;
	crc = get_crc_16(crc, rs_arr->buffer, Length_MB*2+3);

	rs_arr->buffer[Length_MB*2+3] = LOBYTE(crc);
	rs_arr->buffer[Length_MB*2+4] = HIBYTE(crc);
	
	rs_arr->buffer[Length_MB*2+5] = 0;
	rs_arr->buffer[Length_MB*2+6] = 0;     
	rs_arr->buffer[Length_MB*2+7] = 0;
	rs_arr->buffer[Length_MB*2+8] = 0;     

	rs_arr->flag_TIMEOUT_to_Send=true;
	RS_Send(rs_arr, rs_arr->buffer, Length_MB*2+8);

	return;
}

void ReceiveCommandModbus6(RS_DATA *rs_arr)
{
  unsigned int Address_MB, Data_MB, i;

	/////////////////////////////////////////////////
	// Отсылка
	/* Отправлаем назад то же самое */	

	for (i=0;i<8;i++)
	rs_arr->buffer[i] = rs_arr->RS_Header[i];

//	получили начальный адрес записи
	Address_MB = (/*(rs_arr->RS_Header[2] << 8) | */rs_arr->RS_Header[3]);

//	получили слово данных
	Data_MB = (rs_arr->RS_Header[4] << 8) | rs_arr->RS_Header[5];

	Modbus[Address_MB].all = Data_MB;

	rs_arr->flag_TIMEOUT_to_Send=true;
	RS_Send(rs_arr, rs_arr->buffer, 10);
}
