

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;
}






