⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 driver_ds2104.c

📁 DS2410驱动程序
💻 C
字号:
/****************************************************************************
* 文 件 名  :Driver_Ds2104.c
* 版    本     : V1.0
* 连    接     :
* 功    能     :DS2104驱动头
*   作者       :(Reoger)
*   日	期      :2008/10/6
*---------------历史版本信息------------------------------------------
*  修 改 人: Kris
*  版    本    : 
*  日   期:2008/10/6
*  描   述:
****************************************************************************/
/****************************************************************************
*						包含文件
****************************************************************************/
#include "../../Config.h" 
/****************************************************************************
* 						使用宏
****************************************************************************/
// PIO12:set 0
#define DS2401_SET_0	CLR_PIO(15)	// 	(*PIOLDAT)=(*PIOLDAT)&0xffffefff
// PIO12:set 1
#define DS2401_SET_1	SET_PIO(15)	/// (*PIOLDAT)=(*PIOLDAT)|0x00001000
/****************************************************************************
* 						使用数据
****************************************************************************/
uint8 Ds2410_IDBuf_verify[8];
uint8 Ds2410_IDBuf[8];
uint8 FM3104_IDBuf[8];
extern SALVE_TYPE SalveRunSbuf;
const uchar  crc_table[256] = {
0, 	 94, 188,226, 97, 63,221,131,194,156,126, 32,163,253, 31, 65,
157,195, 33,127,252,162, 64, 30, 95, 1,227,189, 62, 96,130,220,
35,125,159,193, 66, 28,254,160,225,191, 93, 3,128,222, 60, 98,
190,224, 2, 92,223,129, 99, 61,124, 34,192,158, 29, 67,161,255,
70, 24,250,164, 39,121,155,197,132,218, 56,102,229,187, 89, 7,
219,133,103, 57,186,228, 6, 88, 25, 71,165,251,120, 38,196,154,
101, 59,217,135, 4, 90,184,230,167,249, 27, 69,198,152,122, 36,
248,166, 68, 26,153,199, 37,123, 58,100,134,216, 91, 5,231,185,
140,210, 48,110,237,179, 81, 15, 78, 16,242,172, 47,113,147,205,
17, 79,173,243,112, 46,204,146,211,141,111, 49,178,236, 14, 80,
175,241, 19, 77,206,144,114, 44,109, 51,209,143, 12, 82,176,238,
50,108,142,208, 83, 13,239,177,240,174, 76, 18,145,207, 45,115,
202,148,118, 40,171,245, 23, 73, 8, 86,180,234,105, 55,213,139,
87, 9,235,181, 54,104,138,212,149,203, 41,119,244,170, 72, 22,
233,183, 85, 11,136,214, 52,106, 43,117,151,201, 74, 20,246,168,
116, 42,200,150, 21, 75,169,247,182,232, 10, 84,215,137,107, 53
};
const uint32 CODER = 0x84479972;
/****************************************************************************
*						使用函数
****************************************************************************/
/***********************************************************************
* 名    称:void delay2401(u16 DelayTime)
* 功    能:DS2401用延时
* 入口参数:延时长度
* 出口参数:无
* 说明    :
***********************************************************************/
void delay2401(uint16 DelayTime)
{
	while(DelayTime--);
}
/***********************************************************************
* 名    称:void DS2401_Write(uchar mCmd)
* 功    能:DS2401写命令
* 入口参数:数据或命令
* 出口参数:无
* 说明    :使用单线制
***********************************************************************/
void DS2401_Write(uchar mCmd)
{
	uchar i;
	DWORD save_sfr;
	SET_PIO_OUTPUT( (0x1UL<<15));
	OS_ENTER_CRITICAL_EXT(save_sfr);

	for(i=0;i<8;i++)
	{
		DS2401_SET_0;
		delay2401(10);
		if(mCmd&0x01)
		{
			DS2401_SET_1;
		}
		
		delay2401(160);		// delay 80uS
		DS2401_SET_1;
		mCmd>>=1;
		delay2401(8);
	}
	OS_EXIT_CRITICAL_EXT(save_sfr);
}
/***********************************************************************
* 名    称:uint8 DS2401_ReadByte(void)
* 功    能:DS2401读数据
* 入口参数:无
* 出口参数:反回数据
* 说明    :使用单线制
***********************************************************************/
uint8 DS2401_ReadByte(void)
{
	uint8 i;
	uint8 retu=0;
	DWORD save_sfr;

	OS_ENTER_CRITICAL_EXT(save_sfr);

	for(i=0;i<8;i++)
	{
		retu>>=1;
		SET_PIO_OUTPUT( (0x1UL<<15) );
		
		DS2401_SET_0;
		delay2401(10);	// 2
		DS2401_SET_1;
		SET_PIO_INPUT( (0x1UL<<15) );
		delay2401(20);	// 32

		if(_XDWORD(PIOEDAT) & (0x1UL<<15) )
		{
			retu |= 0x80;
		}	
		delay2401(150);		// 200

	}
	OS_EXIT_CRITICAL_EXT(save_sfr);
	return(retu);
}
/***********************************************************************
* 名    称:void DS2401_ReadMulBytes(uint8 n)
* 功    能:DS2401读数多个据
* 入口参数:数据个数
* 出口参数:无
* 说明    :使用单线制
***********************************************************************/
void DS2401_ReadMulBytes(uint8 n)
{
	uint8 i;
	for(i=0;i<n;i++)
	{
		Ds2410_IDBuf[i] = DS2401_ReadByte();
		Ds2410_IDBuf_verify[i] = Ds2410_IDBuf[i];
		delay2401(5);
	}
}
/***********************************************************************
* 名    称:uint8 DS2401_CRC(uint8 num,uint8 *Src)
* 功    能:计算CRC校验
* 入口参数:CRC位数,CRC码缓冲
* 出口参数:无
* 说明    :使用单线制
***********************************************************************/
uint8 DS2401_CRC(uint8 num,uint8 *Src)
{
	uint8 i;
	uint8 crcdata = 0;
	for ( i = 0; i < num; i++ )
	{
		crcdata = crcdata ^ (*Src); 
		Src++;
		crcdata = crc_table[crcdata];
	}
	return crcdata;
}
/***********************************************************************
* 名    称:void InitDs2401(void)
* 功    能:初开始化2401
* 入口参数:无
* 出口参数:无
* 说明    :使用单线制
***********************************************************************/
void InitDs2401(void)
{
//	uchar temp; 
	DWORD save_sfr;
	SET_PIO_OUTPUT( (0x1UL<<15) );

	OS_ENTER_CRITICAL_EXT(save_sfr);
	DS2401_SET_0;
	delay2401(960);	// delay 480us
	DS2401_SET_1;
	delay2401(100);	// delay 50us
	SET_PIO_INPUT( (0x1UL<<15));
	delay2401(2);

	if((_XDWORD(PIOEDAT)&  (0x1UL<<15) ) == 0x00)
	{
		//DebugPrintf(("DS2401 init-success \n"));
	}	
	else
	{
		DebugPrintf(("init-fail \n"));
	}
	OS_EXIT_CRITICAL_EXT(save_sfr);
	
	delay2401(800);	
	
}
/***********************************************************************
* 名    称:BOOL DS2401_ReadID(void)
* 功    能:读2401ID
* 入口参数:无
* 出口参数:无
* 说明    :读2401中的ID
***********************************************************************/
BOOL DS2401_ReadID(void)
{
	uchar i;
	uchar try_num=3;
	while(try_num)
	{
		try_num--;
		InitDs2401();
		DS2401_Write(0x33);
		DS2401_ReadMulBytes(8);
		SalveRunSbuf.sn = 0;
		for(i=0;i<8;i++)
		{
			SalveRunSbuf.sn *= 10;
			SalveRunSbuf.sn += (Ds2410_IDBuf[i]%7);					// 计算SN号
		}
		SalveRunSbuf.sn += 31234567;
		// 检查数据的合法性
	//	if(Ds2410_IDBuf[0]!=0x01)	continue;
		if(Ds2410_IDBuf[7] != DS2401_CRC(7, Ds2410_IDBuf) )
		{
			DebugPrintf(("<%02X,%02X,>",(uchar)Ds2410_IDBuf[7],(uchar)DS2401_CRC(7, Ds2410_IDBuf)));
			continue;
		}
		//DebugPrintf(("<%02X,%02X,>",(uchar)Ds2410_IDBuf[7],(uchar)DS2401_CRC(7, Ds2410_IDBuf)));
		return TRUE;
	}
	return FALSE;
}
/***********************************************************************
* 名    称:void crack_2411(void)
* 功    能:加密算法
* 入口参数:无
* 出口参数:无
* 说明    :使用单线制
***********************************************************************/
void crack_2411(void)
{
	union{
		WORD i;
		BYTE ch[2];
	}temp;
	WORD result;
	temp.ch[0] = Ds2410_IDBuf[6], temp.ch[1] = Ds2410_IDBuf[5];
	result = (temp.i&BIT_SECTION(2,0))<<7;
	result |= (temp.i&BIT_SECTION(4,3))<<1;
	if(temp.i& (0x1UL<<5))	result |=  (0x1UL<<15);
	if(temp.i& (0x1UL<<6)) result|= (0x1UL<<13);
	if(temp.i& (0x1UL<<7)) result |=  (0x1UL<<0);
	result|= (temp.i&BIT_SECTION(0xa,8))>>7;
	if(temp.i& (0x1UL<<11) ) result|= (0x1UL<<12);
	if(temp.i& (0x1UL<<12)) result|= (0x1UL<<4);
	if(temp.i& (0x1UL<<13) ) result|=  (0x1UL<<14);
	if(temp.i& (0x1UL<<14) ) result|=  (0x1UL<<11);
	if(temp.i& (0x1UL<<15)) result |=  (0x1UL<<10);
	temp.i = result;
	Ds2410_IDBuf[6]=temp.ch[0], Ds2410_IDBuf[5]=temp.ch[1];
	Ds2410_IDBuf[4] ^=0x5e;

	temp.ch[0] = Ds2410_IDBuf[3], temp.ch[1] = Ds2410_IDBuf[2];
	result = (temp.i&BIT_SECTION(2,0))<<7;
	result |= (temp.i&BIT_SECTION(4,3))<<1;
	if(temp.i& (0x1UL<<5))	result |=  (0x1UL<<15);
	if(temp.i& (0x1UL<<6)) result|= (0x1UL<<13);
	if(temp.i& (0x1UL<<7)) result |=  (0x1UL<<0);
	result|= (temp.i&BIT_SECTION(0xa,8))>>7;
	if(temp.i& (0x1UL<<11) ) result|= (0x1UL<<12);
	if(temp.i& (0x1UL<<12)) result|= (0x1UL<<4);
	if(temp.i& (0x1UL<<13) ) result|=  (0x1UL<<14);
	if(temp.i& (0x1UL<<14) ) result|=  (0x1UL<<11);
	if(temp.i& (0x1UL<<15)) result |=  (0x1UL<<10);
	temp.i = result;
	Ds2410_IDBuf[3]=temp.ch[0]^0xe5, Ds2410_IDBuf[2]=temp.ch[1]^0xe5;

	temp.ch[0] = Ds2410_IDBuf[1], temp.ch[1] = Ds2410_IDBuf[0];
	result = (temp.i&BIT_SECTION(2,0))<<7;
	result |= (temp.i&BIT_SECTION(4,3))<<1;
	if(temp.i& (0x1UL<<5))	result |=  (0x1UL<<15);
	if(temp.i& (0x1UL<<6)) result|= (0x1UL<<13);
	if(temp.i& (0x1UL<<7)) result |=  (0x1UL<<0);
	result|= (temp.i&BIT_SECTION(0xa,8))>>7;
	if(temp.i& (0x1UL<<11) ) result|= (0x1UL<<12);
	if(temp.i& (0x1UL<<12)) result|= (0x1UL<<4);
	if(temp.i& (0x1UL<<13) ) result|=  (0x1UL<<14);
	if(temp.i& (0x1UL<<14) ) result|=  (0x1UL<<11);
	if(temp.i& (0x1UL<<15)) result |=  (0x1UL<<10);
	temp.i = result;
	Ds2410_IDBuf[1]=temp.ch[0]^0x1c, Ds2410_IDBuf[0]=temp.ch[1]^0x1c;

	Ds2410_IDBuf[7] = DS2401_CRC(7, Ds2410_IDBuf);
}
/***********************************************************************
* 名    称:BOOL test_2411(void)
* 功    能:
* 入口参数:无
* 出口参数:无
* 说明    :使用单线制
***********************************************************************/
BOOL test_2411(void)
{
	uint8 i;
	SET_PIO_OUTPUT( (0x1UL<<15) );
	
	if(!DS2401_ReadID())
	{
		DEBUG_STEP();
		return FALSE;
	}

	/*
	if(debug_level==0xff){
		int i;
		sel_con(0);
		uart0_Printf("read 2411: ");
		for(i=0; i<8; i++)	uart0_Printf("%02x ",Ds2410_IDBuf[i]);
		DebugPrintf(("read 2411: "));
		for(i=0; i<8; i++)	DebugPrintf(("%02x ",Ds2410_IDBuf[i]));
	}*/
	for(i=0; i<8; i++)
	{
		DebugPrintf(("%02x ", Ds2410_IDBuf[i]));
	}
	crack_2411();
	
	return TRUE;
}
/***********************************************************************
* 名    称:void DS2401_Jiami(void)
* 功    能:DS2401加密
* 入口参数:无
* 出口参数:无
* 说明    :密码A31933523
***********************************************************************/
void DS2401_Jiami(void)
{
	uint32 seck,coder;
	uint8 i,sbuf;
	uint8 recsbuf[10];
	i = 0;
	while(1)
	{
		if(UART0_ReceiveData_Out(&sbuf))
		{
 			recsbuf[i] = sbuf;
			if(recsbuf[0] != 'A') continue; 
			i++;
			if(i > 8)
			{
				//DebugPrintf_Use(("seck %s\n",recsbuf));
				coder = 0;
				for(i = 1;i<9;i++)
				{
					coder <<= 4;
					coder |= recsbuf[i] - '0';
				}
				//DebugPrintf_Use(("seck %x\n",coder));
				seck = coder ^ 0xf0f0f0f3;
				seck *= 1932;
				seck -= 19483744;
				seck /=53;
				seck ^= 0xf34233;
				seck *= 50;
				//DebugPrintf_Use(("seck %x\n",seck));
				if(seck == CODER)
				{
					DebugPrintf_Use(("R"));
					for(i=0;i<sizeof(FM3104_IDBuf);i++)
					{
						Ds2410_IDBuf[i] = Ds2410_IDBuf_verify[i];
					}
					crack_2411();
					FM3104_Write_Nchar(Ds2410_IDBuf,0x00,sizeof(Ds2410_IDBuf));
					break;
				}
			}
		}
	}
}
/***********************************************************************
* 名    称:void DS2401_Identify(void)
* 功    能:DS2401加密实现
* 入口参数:无
* 出口参数:无
* 说明    :读2401中的ID,
***********************************************************************/
void DS2401_Identify(void)
{
	uint8 i;
	FM3104_Read_Nchar(0x00,sizeof(FM3104_IDBuf),FM3104_IDBuf);
	DS2401_ReadID();
	crack_2411();
	//DebugPrintf_Use(("E"));
	//DS2401_Jiami();
	for(i=0;i<sizeof(FM3104_IDBuf);i++)
	{
		if(FM3104_IDBuf[i] != Ds2410_IDBuf[i])
		{
			DebugPrintf_Use(("E"));
			DS2401_Jiami();
			break;
		}
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -