

const uint16 max_length = 20;

//const int32 baud = 9600;
//const int32 baud = 38400;
//const int32 baud = 115200;

[uint8*max_length] buffer;
uint8 length;
uint8 ErrNumber;
uint8 DataLength;

public [->uint8 * 32] FlagList;
bool DataIsOK;
bool Connected;
public bool isBusy;

public const int32 XTAL = $freq$;

//--Զڴģ
public memory linka
{
	type = [uint16] uint32;
}
void set_uint32( uint16 addr, uint32 data )
{
	SetValue( addr.8(uint8), addr.0(uint8), data );
}
uint32 get_uint32( uint16 addr )
{
	return GetValue( addr.8(uint8), 0x80 + addr.0(uint8) );
}

//---------------------------------------------------
//оƬ, :
// SetBaud( uint16 );
// uint8 UDR
// ж INT_UART
// ȴڽյ	RECEIVE()
// ȴڷ	WAIT_SEND_OK()
#include "$chip$.txt"

//----------------------------------------------------
//ڳʼ,:9600,8λ,1ֹͣλ,ûżУ
public void OS_init()
{
	RDE_DIR = 1;
	RDE_OUT = 0;
	
	RXD_DIR = 0;
	RXD_OUT = 1;
	
	TXD_DIR = 1;
	TXD_OUT = 1;
	
	//ò
	SetBaud( baud );
	
	length = 0;
	
	ErrNumber = 0;
	DataLength = 0;
	DataIsOK = false;
	
	Connected = false;
	
	isBusy = false;
}
//----------------------------------------------------
//ݽж
INTERRUPT_UART
void RXC()
{
	decode();
}
//---------------------------------------------------
//Э
public void decode()
{
	//жϳжȡ UDR,˳½ж
	buffer[length] = UDR;
	length + 1;
	
	//+++++++++++++++++++++++
	//ͨЭ 0x55 N * * * ... * * * SUM
	uint8 L = length;
	uint8 Head = buffer[0];
	
	//жЭͷ
	if( L == 1 && Head != 0x55 ) {
		length = 0;
		return;
	}
	if( L == 2 ) {
		DataLength = buffer[1];
		return;
	}
	if( L < DataLength + 3 ) return;
	uint8 CMD = buffer[2];
	uint8 SUM = buffer[L - 1];
	
	length = 0;
	
	//ͨЭ
	//+++++++++++++++++++++++
	
	//ͨű־
	if( DataLength == 0 ) {
		StartWrite();
		
		//ǰֽ
		SendNullData();
		
		if( Connected ) {
			//WriteByte( 0x55 );
		}
		else {
			//WriteByte( 0xAA );
		}
		EndWrite();
		
		//Ѿ, Źȴλ
		if( Connected ) {
			#asm "CLI"
			OPEN_WATCHDOG();
			forever {}
		}
		Connected = true;
		return;
	}
	//GUIЭ, Ϊ¼ͨ
	if( CMD == 0 ) {
		DataIsOK = true;
		return;
	}
	if( CMD == 1 ) {
		uint8 ID = buffer[3];
		uint8 EI = buffer[4];
		FlagList[ID] = FlagList[ID] | EI;
		return;
	}
}
//---------------------------------------------------
//
public void Connect()
{
	//ϵͳ
	
	StartWrite();
	SendNullData();
	
	forever {
		WriteByte( 0x55 );
		WriteByte( 0x02 );
		WriteByte( 0x00 );
		WriteByte( 0x00 );
		WriteByte( 0x00 );
		
		loop( 1500 ) {
			loop( 250 ) {
				if( RECEIVE() ) {
					decode();
					if( Connected ) {
						loop( 20 ) loop( 200 ) loop( 200 ) {}
						return;
					}
				}
			}
		}
	}
	EndWrite();
}
//---------------------------------------------------
//һַ
public void SetString( uint8 ID, uint8 Command, [#.code uint8*?] string )
{
	isBusy = true;
	
	StartWrite();
	WriteByte( 0xAA );
	WriteByte( 0 );
	WriteByte( ID ); //ָIDĿؼ
	WriteByte( Command ); //
	
	int16 i = 0;
	do {
		uint8 char = string[i];
		i + 1;
		WriteByte( char );
	}
	while( char != 0 );
	
	EndWrite();
	
	isBusy = false;
}
//---------------------------------------------------
//һ
public void SetValue( uint8 ID, uint8 Command, uint32 data )
{
	isBusy = true;
	
	StartWrite();
	WriteByte( 0x55 );
	WriteByte( 6 );
	WriteByte( ID ); //ָIDĿؼ
	WriteByte( Command ); //
	WriteByte( data.0(uint8) );
	WriteByte( data.8(uint8) );
	WriteByte( data.16(uint8) );
	WriteByte( data.24(uint8) );
	WriteByte( 0 );
	EndWrite();
	
	isBusy = false;
}
//---------------------------------------------------
//ȡһ
//2015.8.2
//ע: ȴʱҪݲʵ
//115200: 1000 (ʱʧ)
//9600: 5000 (ȶ, ûҪ·͵)
//: 5000 ǲ, Ϊ 50000 ûٳֹ

public uint32 GetValue( uint8 vID, uint8 Command )
{
	isBusy = true;
	
	reconnect:
	StartWrite();
	WriteByte( 0x55 );
	WriteByte( 2 );
	WriteByte( vID ); //ָֿ
	WriteByte( Command ); //ȡ
	WriteByte( 0 );
	EndWrite();
	
	uint16 tick = 0;
	while( !DataIsOK ) {
		tick + 1;
		if( tick == 50000 ) {
			//#..ָʾƷת();
			goto reconnect;
		}
	}
	DataIsOK = false;
	uint32 D;
	D.0(uint8) = buffer[4];
	D.8(uint8) = buffer[5];
	D.16(uint8) = buffer[6];
	D.24(uint8) = buffer[7];
	
	isBusy = false;
	return D;
}
//----------------------------------------------------
//ǰЧֽ, ǰֽΪ. Ϊʱһֽڻᶪʧ
void SendNullData()
{
	WriteByte( 0x00 );
	WriteByte( 0x00 );
}
//----------------------------------------------------
//һֽ
void WriteByte( uint8 data )
{
	WAIT_SEND_OK();
	UDR = data;
	//delay(); ʱҪ! ȥ֮ԽյǷд
}
//---------------------------------------------------
//485ͨʱҪʱ
void StartWrite()
{
	RDE_OUT = 1;
}
void EndWrite()
{
	//repeat 200 times; ʱȶ
	loop( 500 ) {}
	
	RDE_OUT = 0;
}






