
//时钟芯片底层驱动类
//修改日期: 2015.4.12
//加上了一个 isSafe 变量, 防止事件轮询进程读取时间时被 OS_run 读取秒标志中断导致时序丢失

unit var
{
	//[i] interface_clock clock0;
	public unit clock0
	{
		public const bool CanChange = true;
		public link bool isSafe;
		
		public link int32 second;
		public link int32 minute;
		public link int32 hour;
		public link int32 day;
		public link int32 month;
		public link int32 year;
	}
	clock0.isSafe = isSafe;
	clock0.second = second;
	clock0.minute = minute;
	clock0.hour = hour;
	clock0.day = day;
	clock0.month = month;
	clock0.year = year;
	
	public link unit CLK {}
	public link unit DAT {}
	public link unit RST {}
	
	basic.CLK = CLK;
	basic.DAT = DAT;
	basic.RST = RST;
	basic =
	#include "basic.txt"
	
	//以下变量存放时间信息,分配在ds1302中(虚拟变量)
	
	//[i] var_int32_w year;
	public v_ds1302user int32 year =	#addr 0b1000_1100; 	//年
	//[i] var_int32_w month;
	public v_ds1302user int32 month =	#addr 0b1000_1000;	//月份
	//[i] var_int32_w day;
	public v_ds1302user int32 day =	#addr 0b1000_0110; 	//天
	//[i] var_int32_w hour;
	public v_ds1302user int32 hour =	#addr 0b1000_0100; 	//小时
	//[i] var_int32_w minute;
	public v_ds1302user int32 minute =	#addr 0b1000_0010;	//分
	//[i] var_int32_w second;
	public v_ds1302user int32 second =	#addr 0b1000_0000; 	//秒
	//[i] var_int32_w week;
	public v_ds1302user int32 week =	#addr 0b1000_1010; 	//星期
	
	public v_ds1302 uint8 byte_second =		#addr 0b1000_0000; 	//底层秒
	public v_ds1302 uint8 byte_control =	#addr 0b1000_1110; 	//控制
	public v_ds1302 uint8 byte_charge =		#addr 0b1001_0000; 	//涓流充电
	
	//[i] var_uint8 OS_EventFlag;
	public uint8 OS_EventFlag;
	//[i] event time_changed;
	
	//---------------------
	//[i] var_uint8 OS_time;
	public uint8 OS_time;
	
	uint8 last_time;
	
	public bool isSafe;
	
	//---------------------------------------------------
	//初始化
	//[i] function_void OS_init;
	public void OS_init()
	{
		basic.OS_init();
		
		//初始化, 注意需要加上写保护开关
		byte_control = 0x00;
		byte_second = byte_second & 0B0111_1111;
		byte_charge = 0B1010_1011;
		byte_control = 0x80;
		
		OS_time = 50;
		last_time = 0;
		OS_EventFlag = 0;
		
		isSafe = true;
	}
	//---------------------------------------------------
	//[i] function_void OS_run;
	public void OS_run()
	{
		if( !isSafe ) return;
		
		uint8 current_time = basic.get_uint8( 0b1000_0000 );
		if( last_time != current_time ) {
			OS_EventFlag.0(bit) = 1;
			last_time = current_time;
		}
	}
	//---------------------------------------------------
	//[i] function_void set_time time;
	void set_time( [#.code int8*?] time )
	{
		int32 D1000 = 1000;
		int32 D100 = 100;
		int32 D10 = 10;
		
		year = (time[0]-'0')*D1000 + (time[1]-'0')*D100 + (time[2]-'0')*D10 + (time[3]-'0');
		month = (time[5]-'0')*D10 + (time[6]-'0');
		day = (time[8]-'0')*D10 + (time[9]-'0');
		
		hour = (time[11]-'0')*D10 + (time[12]-'0');
		minute = (time[14]-'0')*D10 + (time[15]-'0');
		second = (time[17]-'0')*D10 + (time[18]-'0');
	}
	//---------------------------------------------------
	//[i] function_bool time_equal Time;
	public bool time_equal( [int8*?] time )
	{
		if( time[0] != 0 && time[0] != year ) return false;
		if( time[1] != 0 && time[1] != month ) return false;
		if( time[2] != 0 && time[2] != day ) return false;
		if( time[3] != hour ) return false;
		if( time[4] != minute ) return false;
		if( time[5] != second ) return false;
		return true;
	}
	//---------------------------------------------------
	void set_uint32( uint8 addr, uint32 data0 )
	{
		//这里通过取余运算连同年份的 20XX 前缀也消去了
		data0 % 100;
		
		uint8 data = (uint8)(uint16)data0;
		uint8 d = (data/10) << 4 + data%10;
		
		isSafe = false;
		byte_control = 0x00;
		basic.set_uint8( addr, d );
		byte_control = 0x80;
		isSafe = true;
	}
	uint32 get_uint32( uint8 addr )
	{
		isSafe = false;
		uint8 d = basic.get_uint8( addr );
		isSafe = true;
		
		d = (d>>4)*10 + d&0x0f;
		if( addr == 0b1000_1100 ) {
			return 2000 + d;
		} else {
			return d;
		}
	}
	//---------------------------------------------------
	//虚拟变量类型,用于在ds1302中定义变量,读写接口指向basic
	memory v_ds1302
	{
		target = basic;
		type = [ uint8 ] uint8;
	}
	//虚拟变量类型,用于在ds1302中定义整数变量,读写接口指向所在组件
	memory v_ds1302user
	{
		type = [ uint8 ] uint32;
	}
}









