#include "DSP2833x_Device.h"     // DSP2833x Headerfile Include File
#include "cntrl_adr.h"
#include "RS485.h"
#include "BIOS_DSP.h"
#include "Message.h"
#include "filter_bat2.h"
#include "measure.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"

extern void DSP28x_usDelay(Uint32 Count);

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

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

void main()
{
  int i,j,mask;
  static unsigned int cancount[2], cancell[2];
  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);
	InitSeeprom();
	timer_Init();
	clear_mem();
	EnableInterrupts();

	setup_adc();
	Init_sensors();

	I2CA_Init(); pause_us(500000);
	Load_caliber();

	Load_params();
	Init_packMask();

	LastMode = Addrr;

    EALLOW;
    SysCtrlRegs.WDCR= 0x2F;
    EDIS;

	MAY=1;

	while(1)
	{
		if(CanGO)
		{		
			CanGO=0;
			for(i=0;i<2;i++)
			if(Cancount[i])
			if(++cancount[i] >= Cancount[i]) 
			if(cancount[0])
			{
				cancount[i] = 0; 
				while(1)
				{
					if(cancell[i] >= 0x80) cancell[i]=0;
					mask = Maska[i][cancell[i]/16] >> (cancell[i]%16);
					if(!mask) cancell[i] = (cancell[i] + 0x10) & 0xFFF0;
					else
					{
						while(!(mask & 1))
						{
							cancell[i]++; mask >>= 1;
						}
						break;
				}	}
				CAN_send(0,modbus,cancell[i]);
				cancell[i]+=3; 
		}	}

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

		if(cLoadParam)
		{
			cLoadParam=0;
			Load_params();
		}

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

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

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

			j = get_command(rs);

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

					default:   break;
}	}	}	}	}

