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

#include "cntrl_adr.h"
#include "RS485.h"
#include "BIOS_DSP.h"
#include "filter_bat2.h"
#include "measure.h"
#include "Message.h"
#include "package.h"

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

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

#include "ecan.h"
#include "log_to_mem.h"

#include "measure.h"
#include "isolatio.h"

extern void DSP28x_usDelay(Uint32 Count);

int kaka[3]={64,0,0};

void main()
{
  int i,j;
  static int canpowse=0,cancount[3],cancell[3],circ[3];
  RS_DATA * rs;


	InitSysCtrl();

//	Disable CPU interrupts and clear all CPU interrupt flags:
	DINT;

	InitPieCtrl();

	IER = 0x0000;
	IFR = 0x0000;

	InitPieVectTable();

	init_zone7();

	setup_leds_line();
	led1_on();
	led2_off();
	for (i=0;i<10;i++)
	{
		pause_us(50000);
		led2_toggle();
		led1_toggle();
	}
	led1_off();
	led2_off();

	get_Mode();
	set_cntrl_addr(Mode,16);
	create_uart_vars(sizeof(CMD_TO_TMS));
	setup_uart(COM_1,115200);
	setup_uart(COM_2,115200);
	Init_Can(0,Addrr);
	Init_Seeprom();
	clear_mem();

	EnableInterrupts();

	if(Desk!=dsk_ISOL)
	{
		setup_adc();
		Init_sensors();
		I2CA_Init(); pause_us(500000);
		Load_caliber();
	}
	else
	{
		timer_Init();
		Init_optic();
	}

	Load_params();
	Init_packMask();

	LastMode = Addrr;

	for(i=0;i<3;i++)
	{
		cancount[i]= 0;
		cancell[i] = CAN_send_start;
	}

    EALLOW;
    SysCtrlRegs.WDCR= 0x2F;
    EDIS;

	MAY=1;

	while(1)
	{
		if(canpowse) canpowse--;
		else
		{		
			canpowse = 0x1000;
			for(i=0;i<3;i++)
			{
				if(cancount[i]) cancount[i]--;
				else
				{
					cancount[i] = Cancount[i]; circ[i] = 0;
	
					while(	!((Maska[i][cancell[i]/16]>>(cancell[i]%16))&1) && circ[i] < 2 )
					if(cancell[i]>CAN_send_finis) 
					{
						cancell[i] = CAN_send_start; circ[i]++;
					}
					else cancell[i]++;
	
					if(cancell[i]<=CAN_send_finis && circ[i] < 2) 
					{
						CAN_send(0,(int *)Modbus,cancell[i]);
						cancell[i]+=3; 
		}	}	}	}

		if(cSaveParam)
		{
			cSaveParam=0;
			Save_params();
		}

		if(cReadCal)
		{
			cReadCal=0;
			Load_caliber();
		}

		if(cDefParam)
		{
			cDefParam=0;
			Default_params();
		}

		get_Inputs();

		if(Desk!=dsk_SHKF)
		{	
			Modbus[23].all = Inputs.wrd.word_0;
		}

		for(i=0;i<2;i++)
		{
			if(i)	rs = &rs_a;
			else	rs = &rs_b;

			j = get_command(rs);

			if(j!=-1)
			switch(j)
			{
				case CMD_INIT:		init(rs);		led2_toggle();break;	// начальные установки
				case CMD_INITLOAD:	initload(rs);	led2_toggle();break;	// настройка загрузки
				case CMD_RUN:		run(rs);		led2_toggle();break;	// загрузить блок
				case CMD_LOAD:		load(rs);		led2_toggle();break;	// загрузить блок
				case CMD_PEEK:		peek(rs);		led2_toggle();break;	// прочитать  ачейку памати
				case CMD_POKE:		poke(rs);		led2_toggle();break;	// записать в ачейку памати
				case CMD_UPLOAD:	upload(rs);		led2_toggle();break;	// передать блок памати
				case CMD_EXTEND:	extendbios(rs);	led2_toggle();break;	// расширенные команды дла биоса

		    	case CMD_TFLASH:	tflash(rs);	led2_toggle();break;	// прошить TMS

//		    	case CMD_STD:       ReceiveCommand(rs);			led2_toggle();break;
		    	case CMD_MODBUS_3: 	ReceiveCommandModbus3(rs);	led2_toggle();break;
		    	case CMD_MODBUS_6: 	ReceiveCommandModbus6(rs);	led2_toggle();break;

				default:   break;
}	}	}	}			

