#include "DSP2833x_Device.h"     // DSP2833x Headerfile Include File
#include "DSP2833x_SWPrioritizedIsrLevels.h" 

#include "RS485.h"
#include "filter_bat2.h"
#include "measure.h"
#include "message.h"
#include "package.h"
#include "peripher.h"
#include "crc16.h"
#include "isolatio.h"
#include <math.h>	//	Это чтобы мерить амплитуду! sqrt без этого будет крив!!!

OPTOCANAL opt[2];

void DCLK(int i, int x)
{
	x=!x;

	if(i)
	{
		if(x)	GpioDataRegs.GPASET.bit.GPIO26=1;
		else	GpioDataRegs.GPACLEAR.bit.GPIO26=1;
	}
	else
	{
		if(x)	GpioDataRegs.GPBSET.bit.GPIO32=1;
		else	GpioDataRegs.GPBCLEAR.bit.GPIO32=1;
}	}

int DIN(int i)
{
	if(i)	return 	!GpioDataRegs.GPBDAT.bit.GPIO52;
	else	return 	!GpioDataRegs.GPADAT.bit.GPIO23;
}       

void BLIN(int i)
{
	if(i)	GpioDataRegs.GPBTOGGLE.bit.GPIO53=1;
	else	GpioDataRegs.GPATOGGLE.bit.GPIO24=1;
}       

interrupt void cpu_timer1_isr_ISOL(void)
{
  ERROR error;
  float Riso=0,kff=1; 
  static float fRiso[2];
  static int ist[2] = {1,1};
  long numb=0;
  int i;
  static unsigned int  count_ready=0;

	EALLOW;
	CpuTimer1.InterruptCount++;
	IER |= MINT13;                 // Set "global" priority
	EINT;
  	EDIS;    // This is needed to disable write to EALLOW protected registers

	ServiceDog();

	if(++count_ready >= period_ready)
	{
		count_ready=0;

		if((!sig.bit.Error)|(cTestLamp))	toggle_READY();
		else								set_READY();
	}

	for(i=0;i<2;i++)
	{
		if(sens_error[i].bit.Bypas)
		{
			sens_error[i].all = 0;		
			sens_error[i].bit.Bypas = 1;
			Modbus[i+DATASTART].all = 0;
			continue;
		}

		if(opt[i].Wait)
		{
			opt[i].Wait--; 
			opt[i].bit = 0;
			opt[i].clk = 0; 
			DCLK(i,0);
			continue;
		}

		opt[i].clk=!opt[i].clk; 
		DCLK(i,opt[i].clk);
		if(!opt[i].clk) 
		{
	    	opt[i].Numb = (opt[i].Numb<<1) | DIN(i);
			if(++opt[i].bit>=32) 
			{
				error.all = 0;

				opt[i].Wait = (TELE_FREQ/1000)*optopowse;
				opt[!i].Wait =(TELE_FREQ/2000)*optopowse;
				if(get_crc32(&(opt[i].Numb)))
				{
					numb = opt[i].Numb;
					numb = numb / 256;	// удаляем контрольную сумму
					Riso=numb;

					if(ist[i]) { kff=1; ist[i]=0; }
					else kff = optofiltr;
					fRiso[i] += (Riso-fRiso[i])/kff;

					numb = (long)fRiso[i];
					Modbus[i*2+0x10].all = (int)(numb & 0xFFFF);
					Modbus[i*2+0x11].all = (int)(numb>>16);

					Riso=numb;
					Riso = Riso/256;	// предположим
					Modbus[i+DATASTART].all = Riso;
					opt[i].ers = 0; BLIN(i);
				}
				else
				{
					if(++opt[i].ers > 20)
					{
						opt[i].ers = 20;
						error.bit.Tear = 1;
					}
				}
			
				reset_errs(i,error);

		}	}
	}

	sig.all = chk.all;
	chk.all = 0;

}
void timer_Init()
{
	EALLOW;  // This is needed to write to EALLOW protected registers
	PieVectTable.XINT13 = &cpu_timer1_isr_ISOL;
	EDIS;    // This is needed to disable write to EALLOW protected registers

	ConfigCpuTimer(&CpuTimer1, SYSCLKOUT/1000000, 1000000/TELE_FREQ);
	CpuTimer1Regs.TCR.all = 0x4020; // Use write-only instruction to set TSS bit = 0
	IER |= M_INT13;

	period_ready = TELE_FREQ / (READY_FREQ * 2);

}
