Exemple #1
0
void main(void) {
    
    int i;
    char status;
    char str[100];
    
    init(); // initialize PIC
    init_soft_uart();
    init_SPI(); 
    
    
    GIE = 0; // disable all PIC's interrupts 
    
    LATB2 = 0; // LED0 off
    for (i=0;i<100;i++) __delay_ms(10);
    
    uart_write_s("init ok !\n\r");
    
    init_RC522();
        
    while (1)
    {
        status = MFRC522_Request(PICC_REQIDL, str); // check if card is present
        if (status == MI_OK)
        {
            uart_write_s("ok card ! \n\r");
            
            if (MFRC522_ReadCardSerial(str) == MI_OK) // get UID
            {
                uart_write_s("UID = ");     // and display it
                uart_write_hex_c(*(str+0));
                uart_write_hex_c(*(str+1));
                uart_write_hex_c(*(str+2));
                uart_write_hex_c(*(str+3));
                uart_write_s("\n\r");
            }
            
            LATB2 = 1;                         // blink LED0
            for (i=0;i<10;i++) __delay_ms(10); // .
            LATB2 = 0;                         // .
            for (i=0;i<10;i++) __delay_ms(10); // .
            LATB2 = 1;                         // .
            for (i=0;i<10;i++) __delay_ms(10); // .
            LATB2 = 0;                         // .
            for (i=0;i<10;i++) __delay_ms(10); // .
            LATB2 = 1;                         // .
            for (i=0;i<10;i++) __delay_ms(10); // .
            LATB2 = 0;                         // .
            for (i=0;i<10;i++) __delay_ms(10); // .
        }
        
        //for (i=0;i<50;i++) __delay_ms(10);
        LATB2 = 1;      // Blink LED0, visual normal operation works, before watchdog dev
        __delay_ms(10);
        LATB2 = 0;
        __delay_ms(10);
    }
    
    return;
}
//0x0044 = Mifare_UltraLight
//0x0004 = Mifare_One (S50)
//0x0002 = Mifare_One (S70)
//0x0008 = Mifare_Pro (X)
//0x0344 = Mifare_DESFire
char MFRC522_isCard( char *TagType )
{
  if (MFRC522_Request( PICC_REQIDL, TagType ) == MI_OK)
    return 1;
  else
    return 0;
}
uint8_t MFRC522_Check(uint8_t * id) 
{
	uint8_t status;
	status = MFRC522_Request(PICC_REQIDL, id);					// Find cards, return card type
	if (status == MI_OK) status = MFRC522_Anticoll(id);			// Card detected. Anti-collision, return card serial number 4 bytes
	MFRC522_Halt();												// Command card into hibernation 
	return status;
}
Exemple #4
0
void main(void)
{
	unsigned char i=0;
	unsigned char status;
	unsigned char str[MAX_LEN];
	unsigned char RC_size;
	unsigned char blockAddr;
	unsigned char mynum[20];
	unsigned char serNum[7];

	uint8_t RCV;
	int resulta=0;
	int fd;
	int a=0;



	fd=Init_SPI("/dev/spidev0.0");
	setup_io();

	SET_INPUT(25);
	SET_OUTPUT(25);

	ApiInit(fd);
	MFRC522_Init();

	a=Read_MFRC522(VersionReg);
	printf("MFRC Version Read %02X \r\n",a);


	//	for(a=0;a<10;a++)
	//	{
	//		RCV=Read_MFRC522(VersionReg);
	//		//RCV=SPI_transfer(fd,a);
	//		printf("RCV %02X \r\n",RCV);
	//		SET_BIT(25);
	//        sleep(1);
	//        CLR_BIT(25);
	//        sleep(1);
	//	}
	while(1)
	{
		status = MFRC522_Request(PICC_REQIDL, str);
		if(resulta==status){

		}else{
			resulta=status;
		}


		if (status == MI_OK)
		{
			mifare_tag_type(str);
			//usleep(1000);
		}

		status = MFRC522_Anticoll(str);
		memcpy(serNum, str,5);
		if (status == MI_OK)
		{
			printf("UID :");
			for(i=0;i<5;i++)
			{
				printf("[");
				printf("%02X",serNum[i]);
				printf("]");
			}
			printf("\n");
			//usleep(1000);
		}

		RC_size=MFRC522_SelectTag(serNum);
		if(RC_size !=0)
		{

			printf("Select_OK Size %02d \n",RC_size);
			puts("");
			puts("");

		}

	}

	close(fd);

}
static int rc522_open(struct inode *inode, struct file *file)
{
	uint8_t str[MAX_LEN];
	uint8_t writeData[16]={1, 2, 3, 4, 0};
	uint8_t i;
	uint8_t status;
	uint8_t size;
	uint8_t blockAddr;	//选择操作的块地址0~63
	//扇区A密码,16个扇区,每个扇区密码6Byte
	uint8_t sectorKeyA[16][16] = {{0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF},};	

	printk("rc522_open\r\n");
	
	SPI_Init();
	MFRC522_Init();

	while (1)
	{
		//寻卡,返回卡类型	
		status = MFRC522_Request(PICC_REQIDL, str);	
		if (status == MI_OK)
		{
			printk("%d \r\n", str[0]);
			printk("%d \r\n", str[1]);
		}

		//防冲撞,返回卡的序列号 4字节
		status = MFRC522_Anticoll(str);
		memcpy(serNum, str, 5);
		if (status == MI_OK)
		{
			printk("%d \r\n", serNum[0]);
			printk("%d \r\n", serNum[1]);
			printk("%d \r\n", serNum[2]);
			printk("%d \r\n", serNum[3]);
			printk("%d \r\n", serNum[4]);
		}

		//选卡,返回卡容量
		size = MFRC522_SelectTag(serNum);
		if (size != 0)
		{
			printk("size is %d", size);
		}

		//写
		blockAddr = 1;		//数据块		
		status = MFRC522_Auth(PICC_AUTHENT1A, blockAddr, sectorKeyA[blockAddr/4], serNum);	//认证
		if (status == MI_OK)
		{
			//写数据
			status = MFRC522_Write(blockAddr, writeData);
		}

		//读
		blockAddr = 1;		//数据块		
		status = MFRC522_Auth(PICC_AUTHENT1A, blockAddr, sectorKeyA[blockAddr/4], serNum);	//认证
		if (status == MI_OK)
		{
			//读数据
			status = MFRC522_Read(blockAddr, str);
			if (status == MI_OK)
			{
				for (i=0; i<16; i++)
				{
					printk("%d ", str[i]);
				}
			}
		}

		MFRC522_Halt();			//命令卡片进入休眠状态*/
	}	
}