示例#1
0
文件: Blinky.c 项目: izzitech/mg-exam
/****************************************************************************************
** 函数原型:    void M1Halt(void)
** 函数功能:    卡片休眠
** 入口参数:    -
** 出口参数:    -
** 返 回 值:    -
** 描   述:    
****************************************************************************************/
void M1Halt(void)
{
	char status = MI_COM_ERR;

	PcdHalt();
	status = PcdHalt();
	if (status == MI_OK)
	{
		status = 0;
	}
	else
	{
		status = 0xf1;
	}

	COM_Send(0,0,status);
}
示例#2
0
 /*******************************************************************************
* 描  述  :  对寻卡、防冲撞、选卡、发送卡号、读卡、写卡、修改密码进行操作
             成功则LED1灯亮
* 输  入  :  无
* 输  出  :  无
* 返  回  :  无
*******************************************************************************/
void ctrlprocess(void)
{
	unsigned char ii;
	char status;

	PcdReset();
	status=PcdRequest(PICC_REQIDL,&RevBuffer[0]);	//寻天线区内未进入休眠状态的卡,返回卡片类型 2字节
	if(status!=MI_OK)
	{
		return;
	}

	status=PcdAnticoll(&RevBuffer[2]);				//防冲撞,返回卡的序列号 4字节
	if(status!=MI_OK)
		return;
	memcpy(MLastSelectedSnr,&RevBuffer[2],4); 
	status=PcdSelect(MLastSelectedSnr);				//选卡
	if(status!=MI_OK)
	{
		InfoPrintf("%s -- %d exit\r\n",__FUNCTION__,__LINE__);
		return;
	}
	if(oprationcard==KEYCARD)						//修改密码
	{
		oprationcard=0;    
		status=PcdAuthState(PICC_AUTHENT1A,KuaiN,PassWd,MLastSelectedSnr);
		if(status!=MI_OK)
		{
			InfoPrintf("%s -- %d exit\r\n",__FUNCTION__,__LINE__);
			return;
		}
		status=PcdWrite(KuaiN,&NewKey[0]);
		if(status!=MI_OK)
		{
			InfoPrintf("%s -- %d exit\r\n",__FUNCTION__,__LINE__);
			return;
		}
		PcdHalt();
		InfoPrintf("modify password success\r\n");
	}
	else if(oprationcard==READCARD)					//读卡
	{
		oprationcard=0;
		status=PcdAuthState(PICC_AUTHENT1A,KuaiN,PassWd,MLastSelectedSnr);	 //验证A密匙
		if(status!=MI_OK)
		{
			InfoPrintf("%s -- %d exit\r\n",__FUNCTION__,__LINE__);
			return;
		}

		status=PcdRead(KuaiN,Read_Data);
		if(status!=MI_OK)
		{
			InfoPrintf("%s -- %d exit\r\n",__FUNCTION__,__LINE__);
			return;
		}

		DebugPrintf(OTHER_DBG,"read card key: ");
		debug_printf(OTHER_DBG,(unsigned char*)Read_Data,16);
		PcdHalt();
		port_data[port_id*5 + 0] = port_id + 1 +'0';
		port_data[port_id*5 + 1] = (Read_Data[0] / 10) % 10 + '0' ;
		port_data[port_id*5 + 2] = (Read_Data[0] % 10) + '0';
		port_data[port_id*5 + 3] = 0x0d;
		port_data[port_id*5 + 4] = 0x0a;
	}
	else if(oprationcard==WRITECARD)				//写卡
	{
		oprationcard=0;
		status=PcdAuthState(PICC_AUTHENT1A,KuaiN,PassWd,MLastSelectedSnr);//
		if(status!=MI_OK)
		{
			InfoPrintf("%s -- %d exit\r\n",__FUNCTION__,__LINE__);
			return;
		}
		status=PcdWrite(KuaiN,&WriteData[0]);
		if(status!=MI_OK)
		{
			InfoPrintf("%s -- %d exit\r\n",__FUNCTION__,__LINE__);
			return;
		}
		PcdHalt();
		InfoPrintf("write card success\r\n");
	} 
	else if(oprationcard==SENDID)					//发送卡号
	{
		oprationcard=0;
		for(ii=0;ii<4;ii++)
			cardID[ii] = MLastSelectedSnr[ii];
		InfoPrintf("send car id: ");
		debug_printf(NORM_DBG,(unsigned char*)cardID,4);
	}  
}
示例#3
0
	Rfid::RfidStatusCode Rfid::listen(void)
	{
		(*_pLinkToConsole)->printLog("start void Rfid::listen(void)");

		uint8_t SN[10];
		uint8_t SN_len=0;

		uint8_t use_gpio=1;
		uint8_t gpio=18;

		uint16_t CType=0;

		int tmp, i;
		char status;
		char str[255];
		char *p;
		char sn_str[23];

		while(_statusCode == RfidStatusCode::NONE)
		{
			status = find_tag(&CType);

			if(status==TAG_NOTAG)
			{
				usleep(200000);
				continue;
			}
			else if((status!=TAG_OK) && (status!=TAG_COLLISION))
			{
				continue;
			}

			if(select_tag_sn(SN, &SN_len)!=TAG_OK)
			{
				continue;
			}

			p=sn_str;
			for(tmp=0; tmp<SN_len; tmp++)
			{
				sprintf(p, "%02x", SN[tmp]);
				p+=2;
			}
			*(p++)=0;

			/*
			if(use_gpio){
				bcm2835_gpio_write(gpio, HIGH);
			}
			*/

			_outStream << "Tag found ";
			_outStream << "[type: " << setfill('0') << setw(4) << setbase(16) << CType << ", ";
			_outStream << "SNlen: " << setbase(10) << int(SN_len) << ", ";
			_outStream << "SN: " << int(SN) << "] ";
			_outStream << "SN: " << sn_str;
			(*_pLinkToConsole)->printOut(&_outStream);

			string serial(sn_str);
			rfidSignal(serial);

			p=str;
			PcdHalt();
			if (use_gpio){
				bcm2835_gpio_write(gpio, LOW);
			}
		}

		(*_pLinkToConsole)->printLog("end void Rfid::listen(void)");
	}
示例#4
0
int main(int argc, char *argv[]) 
{

	uid_t uid;
	uint8_t SN[10];
	uint16_t CType=0;
	uint8_t SN_len=0;
	char status;
	int tmp,i;

	char str[255];
	char *p;
	char sn_str[23];
	pid_t child;
	int max_page=0;
	uint8_t page_step=0;

	FILE * fmem_str;
	char save_mem=0;
	char fmem_path[255];
	uint8_t use_gpio=0;
	uint8_t gpio=255;
	uint32_t spi_speed=10000000L;
	int fd = -1 ;

//by myself
	unsigned char blockaddr = (unsigned char)atoi(argv[1]);
	unsigned char key_passwd = (unsigned char)atoi(argv[2]) ;
	int n = 0;
	unsigned char statu = 0;
	unsigned char data_test[16];

	for(n = 0; n < 6; n++) {
		data_test[n] = 0x00;
	}
	data_test[6] = 0xff ;
	data_test[7] = 0x07;
	data_test[8] = 0x80 ;
	data_test[9] = 0x69 ;

	for(n=10;n<16;n++){
		data_test[n] = 0xFF ;
	}
	
#if 1
	if(argc < 3) {
		printf("command error and try again like this : ./a.out 1 \n");
		return -1;
	}
#endif
	if( (fd = spi_open() )< 0) return -1 ;
	/*
	 * spi mode
	 */
	unsigned char mode = 3;
//	unsigned char mode = 1;
	unsigned char mode_t ;
	unsigned char bits = 8 ;
	unsigned int  speed = 120000000 ;
	int ret = -1 ;
	ret = ioctl(fd, SPI_IOC_WR_MODE, &mode);
	if (ret == -1)
		printf("can't set spi mode");

	ret = ioctl(fd, SPI_IOC_RD_MODE, &mode_t);
	if (ret == -1)
		printf("can't get spi mode");

	/*
	 * bits per word
	 */
	ret = ioctl(fd, SPI_IOC_WR_BITS_PER_WORD, &bits);
	if (ret == -1)
		printf("can't set bits per word");

	unsigned char bits_t ;
	ret = ioctl(fd, SPI_IOC_RD_BITS_PER_WORD, &bits_t);
	if (ret == -1)
		printf("can't get bits per word");

	/*
	 * max speed hz
	 */
	ret = ioctl(fd, SPI_IOC_WR_MAX_SPEED_HZ, &speed);
	if (ret == -1)
		printf("can't set max speed hz");

	unsigned int  speed_t ;
	ret = ioctl(fd, SPI_IOC_RD_MAX_SPEED_HZ, &speed_t);
	if (ret == -1)
		printf("can't get max speed hz");

	printf("spidev hw info : mode:%d  bits:%d speed:%d\n",mode_t,bits_t,speed_t) ;

//many changes in InitRc522 function, please check it.	
	printf("we will read block [%d] value !\n",blockaddr);

	printf("rc522 is soft_rest ....\n");
	PcdReset();
#if 1	
	printf("rc522 open antenna ... \n");
	usleep(100*1000);
	PcdAntennaOn();
	
	memset(Tmpbuff,0,20);

	printf("now find tag .....\n");
	while(1) {
	 statu = PcdRequest(PICC_REQALL,Tmpbuff);
	 if(statu == TAG_OK) {
	 	printf("find the tag and the tag type is :");
		for(n = 0; n < (sizeof(Tmpbuff) / sizeof(Tmpbuff[0])); n++) {
			printf("0x%x ",Tmpbuff[n]);
		}
		printf("\n");
	 }else {
	 	continue ;
	 }
	 
	 printf("Now is running Anticoll ....\n");
	 statu = PcdAnticoll(PICC_ANTICOLL1, Tmpbuff);
	 if(statu == TAG_OK) {
	 	printf("Anticoll success and the tag sn is ");
		for(n = 0; n < (sizeof(Tmpbuff) / sizeof(Tmpbuff[0])); n++) {
			printf("0x%x ",Tmpbuff[n]);
		}
		printf("\n");
	 }else {
		printf("Anticoll faild !\n");
	 	continue ;
	 }

	 printf("Now start select an tag .... \n");
	 statu = PcdSelect(PICC_ANTICOLL1, Tmpbuff);
	 if(statu == TAG_OK) {
	 	printf("select tag is success !\n");
	 }else {
	 	printf("sorry,select tag faild !\n");
		continue ;
	 }

	 printf("Now start authstate ... \n");
	 statu = PcdAuthState(PICC_AUTHENT1A, blockaddr, key, Tmpbuff);
	// statu = PcdAuthState(PICC_AUTHENT1B, blockaddr, key, Tmpbuff);
	 if(statu == TAG_OK) {
	 	printf("authstate success !\n");
	 }else {
	 	printf("authstate faild and continue find tag....\n");
		continue ;
	 }
#if 1
	 if(key_passwd == 3 ) {
		 printf("Now start write [%d] blockaddr... \n",key_passwd);
		 statu = PcdWrite(key_passwd, data_test);
		 if(statu == TAG_OK) {
				printf("write success !\n");
		 }else {
		 	printf("write faild !\n");
			continue ;
		 }

		statu = PcdRead(key_passwd,Tmpbuff);
	 if(statu == TAG_OK) {
	 	printf("read success and the data is ");
		for(n = 0; n < 16; n++) {
			printf("0x%x ",Tmpbuff[n]);
		}
		printf("\n");
	 }else {
	 	printf("read faild and continue find tag ....\n");
		continue ;
	 }

	 }else {
		 printf("[%d] address is only read !\n",blockaddr);
	 }
#endif
	 printf("Now start read [%d] addresss ... \n",blockaddr);
	 statu = PcdRead(blockaddr,Tmpbuff);
	 if(statu == TAG_OK) {
	 	printf("read success and the data is ");
		for(n = 0; n < 16; n++) {
			printf("0x%x ",Tmpbuff[n]);
		}
		printf("\n");
	 }else {
	 	printf("read faild and continue find tag ....\n");
		continue ;
	 }

	 printf("Now sleep the picc \n");
	 PcdHalt();
	 break ;
	}

//	InitRc522();
#if 0

	while(1) {
		status= find_tag(&CType);
		printf("status is [%d] :Ctype [%x] \n",status,CType) ;
		if (status==TAG_NOTAG) {
			printf("find tag again !\n") ;
			usleep(200000);
			continue;
		}else if ((status!=TAG_OK)&&(status!=TAG_COLLISION)) {
			printf("status!=TAG_OK)&&(status!=TAG_COLLISION again \n") ;
			continue;
		}
		printf("Ctype is %d\n",CType);
		if (select_tag_sn(SN,&SN_len)!=TAG_OK) {
			printf("select_tag_sn(SN,&SN_len)!=TAG_OK) again \n") ;
			continue;
		}

		read_tag_str(0,str);
		printf("str:%s\n",str) ;
		PcdHalt();
		break  ;
	}
#endif
#endif
	printf("now start close spi orpe.....\n");
	spi_close() ; 

	return 0;

}
示例#5
0
文件: MAIN.C 项目: buzai/MFRC522----
void main( )
{    
     unsigned char status,i;
	 unsigned int str[4]={0xD5,0x5E,0x2A,0xD9};
	 unsigned int temp,j;
	  
     InitializeSystem( );
     PcdReset();
     PcdAntennaOff(); 
     PcdAntennaOn(); 

     while ( 1 )
     {   


//		if(RI == 1) {  //如果收到.
//	
//	      RI = 0;      //清除标志.
//	
//	      UART_buff = SBUF;  //接收.
//	
//		  printf("rec id %x",UART_buff);
//	
//	    }
			

	 		


         status = PcdRequest(PICC_REQALL, g_ucTempbuf);//寻卡
         if (status != MI_OK)
         {    
             InitializeSystem( );
		     PcdReset();
		     PcdAntennaOff(); 
		     PcdAntennaOn(); 
		     continue;
         }
			     
			
         status = PcdAnticoll(g_ucTempbuf);//防冲撞
         if (status != MI_OK)
         {    continue;    }


        //D55E2AD9
		
//		printf("开始: %d \n",array_cmp(g_ucTempbuf,str));


		////////以下为超级终端打印出的内容////////////////////////
		if(array_cmp(g_ucTempbuf,str)==0){

	   		LED_GREEN = 0;
			delay1(100);
			LED_GREEN = 1;
			delay1(100);
			LED_GREEN = 0;
			delay1(200);
			LED_GREEN = 1;

			printf("卡号是 :");
				for(i=0;i<4;i++)
				{
					temp=g_ucTempbuf[i];
					printf("%X",temp);
					
	        	}
			printf("\n");
		}
		else{
			printf("卡号错误 错误的卡号是 :");
				for(i=0;i<4;i++)
				{
					temp=g_ucTempbuf[i];
					printf("%X",temp);
					
	        	}
			printf("\n");
		}

		////////////////////////////////////////////////////////


	    for(i=0;i<250;i++)//先输出一种频率的声音
        {
         for(j=0;j<80;j++);
         Speaker=~Speaker;
        }

		
						 		         
		PcdHalt();

    }
}
示例#6
0
void RFID_refresh()
{
	PcdHalt();
}