#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"

WORDE Modbus[ANSWER_LEN+1];   
WORDE reply[REPLY_LEN];

unsigned int param[ANSWER_LEN+1];

LONGE* outputs;

int DataAnalog1,DataAnalog2;

ERROR * sens_error;
int * sens_hi_edge;
int * sens_lo_edge;

unsigned int Maska[3][9];

void Default_params()
{
  unsigned int i;

	for(i=0;i<ANSWER_LEN;i++)
	{
		Modbus[i].all = 0;	
	}

	Brightness = 5;

	LastMode = Addrr;

	Cancount[0] = 10;	//	пауза между посылками CAN
	Cancount[1] = 95;	//	пауза между посылками CAN
	Cancount[2] = 1;	//	пауза между посылками CAN

	if(Desk==dsk_COMM)
	{
		sens_hi_edge[0 ] = 55;	
		sens_hi_edge[1 ] = 55;	
		sens_hi_edge[2 ] = 55;	
		sens_hi_edge[3 ] = 55;
			
		sens_hi_edge[4 ] = 65;	
		sens_hi_edge[5 ] = 65;	

		sens_hi_edge[7 ] = 50;	
		sens_hi_edge[8 ] = 50;	
		sens_hi_edge[9 ] = 50;
		sens_hi_edge[10] = 50;	
		sens_hi_edge[11] = 50;	

		sens_lo_edge[0 ] = 50;
		sens_lo_edge[1 ] = 50;
		sens_lo_edge[2 ] = 50;
		sens_lo_edge[3 ] = 50;

		sens_lo_edge[4 ] = 60;
		sens_lo_edge[5 ] = 60;

		sens_lo_edge[7 ] = 45;
		sens_lo_edge[8 ] = 45;
		sens_lo_edge[9 ] = 45;
		sens_lo_edge[10] = 45;
		sens_lo_edge[11] = 45;
	}

	if(Currentoz)
	{
		for(i=12;i<16;i++)
		{
			sens_hi_edge[i] = 2428;	
			sens_lo_edge[i] = 1462;
		}

		Modbus[12].bit.bitE = 1;	//	Ignore
		Modbus[13].bit.bitE = 1;	//	Ignore
		Modbus[14].bit.bitE = 1;	//	Ignore
		Modbus[15].bit.bitE = 1;	//	Ignore
	}

	if(Mode==adr_ISOL)
	{
		optopowse = 200;
		optofiltr = 20;	//200;
	}

	if(Mode==adr_SHKF)
	{
		Modbus[0x02].bit.bitE = 1;	//	Ignore климат контроль
		Modbus[0x03].bit.bitE = 1;	//	Ignore климат контроль

		for(i=0;i<17;i++)
		{
			if(sens_type[i]==POWER_380)
			{	sens_lo_edge[i] = 300;
				sens_hi_edge[i] = 430;	}
			if(sens_type[i]==POWER_220)
			{	sens_lo_edge[i] = 170;
				sens_hi_edge[i] = 250;	}
			if(sens_type[i]==POWER_31)
			{	sens_lo_edge[i] = 20;
				sens_hi_edge[i] = 40;	}
			if(sens_type[i]==POWER_24 || sens_type[i]==VIRT_24)
			{	sens_lo_edge[i] = 15;
				sens_hi_edge[i] = 30;	}
			if(sens_type[i]==POWER_15)
			{	sens_lo_edge[i] = 10;
				sens_hi_edge[i] = 20;	}
			if(sens_type[i]==TERMO_AD)
			{	sens_lo_edge[i] = 60;
				sens_hi_edge[i] = 65;
}	}	}	}

void Load_params()
{
  unsigned int i,crc;

	sens_error =	((ERROR *)&Modbus[start_sens_error]);
	sens_hi_edge =	((int *)&Modbus[start_sens_hi_edge]);
	sens_lo_edge =	((int *)&Modbus[start_sens_lo_edge]);

	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;

		if(Desk==dsk_ISOL)
		{
			if(optopowse==0) optopowse=200;
			if(optofiltr==0) optofiltr=20;	//200
	}	}
	else
	{
		Default_params();
		Commands=0;
		Save_params();
	}

	for(i=0;i<4;i++)	Zeroes[i] = adc0[12+i];
}

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;

	if(Desk==dsk_COMM)
	{
		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;
		}
		for(i=0;i<4;i++)	Zeroes[i] = adc0[12+i];
	}
	if(Desk==dsk_SHKF)
	for(i=0;i<17;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] = 1635; buf[1] = 240;	}
			else						{ buf[0] = 0;	 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))
		{
			if(sens_type[i]==TERMO_AD)	{ buf[0] = 4019; buf[1] = 590;	} else
			if(sens_type[i]==POWER_380)	{ buf[0] = 2450; buf[1] = 3800;	} else
			if(sens_type[i]==POWER_220)	{ buf[0] = 2450; buf[1] = 2200;	} else
			if(sens_type[i]==VIRT_24)	{ buf[0] = 1;    buf[1] = 240;	} else
										{ buf[0] = 2450; buf[1] = 240;	}
			
		}

//if(sens_type[i]==VIRT_24)	{ buf[0] = 1;    buf[1] = 240;	}

		adcHI = buf[0];
		tmpHI = buf[1];
//-------------------------------------------------------------------
		adc0[i] = adcLOW;
		tmp0[i] = tmpLOW;
		tmpK[i] = (tmpHI - tmpLOW)/(adcHI - adcLOW);
	}
}


/***************************************************************/
/*  Передача данных по протоколу ModBus - команда 3
    Чтение ачеек данных    		*/
/***************************************************************/
void ReceiveCommandModbus3(RS_DATA *rs_arr)
{
  unsigned int crc, Address_MB, Length_MB, 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];

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

	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++)
	{
   		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);
}
