void InitRc522(void) { FUNCTION() ; // in PcdRest function many changes PcdReset(); // PcdAntennaOn(); }
/******************************************************************************* * 描 述 : 初始化RC522,开启天线并设置RC522的工作方式为 ISO14443_A * 输 入 : 无 * 输 出 : 无 * 返 回 : 无 *******************************************************************************/ static void InitRc522(void) { PcdReset(); PcdAntennaOff(); PcdAntennaOn(); M500PcdConfigISOType( 'A' ); }
/*---------------------------------------------------------------------------- MAIN function *----------------------------------------------------------------------------*/ int main (void) { unsigned long status, current_tick, last_tick, i, j; unsigned char data[5]; unsigned char temp; // SysTick_Config(SystemCoreClock/1000); /* Generate IRQ each 1 ms */ init_mfrc500(); SER_init(); /* UART#1 Initialization */ SSP_IOConfig(); SSP_Init(); // init_eint(); printf("Welcome\r\n"); if(PcdReset() != MI_OK); { printf("PcdReset Fail\r\n"); while(1); } while(1); }
void RC522_Init(void) { RC522_GPIO_Config(); //引脚初始化 SPI2_Init(); //SPI初始化 PcdReset(); //复位模块 PcdAntennaOff(); //关闭天线 PcdAntennaOn(); //打开天线 M500PcdConfigISOType( 'A' );//设置工作方式 }
void init_mfrc500(void) { int i; char status; LPC_IOCON->PIO0_4 &= ~0x07; /* SSP SSEL is a GPIO pin */ /* port0, bit 15 is set to GPIO output and high */ GPIOSetDir( PORT0, 4, 1 ); GPIOSetValue( PORT0, 4, 1 ); for(i=0; i<500000; i++); GPIOSetValue( PORT0, 4, 0 ); DelayMs(100); DelayMs(100); status = PcdReset(); if (status != MI_OK) { DelayMs(10); #ifdef DEBUG_UART printf("mfrc500 1st fail\r\n"); #endif status = PcdReset(); } if (status != MI_OK) { DelayMs(10); #ifdef DEBUG_UART printf("mfrc500 2nd fail\r\n"); #endif status = PcdReset(); } if (status == MI_OK) { #ifdef DEBUG_UART printf("mfrc500 init ok\r\n"); #endif DelayMs(5500); // DelayMs(80); } }
void main( void ) { WDTCTL = WDTPW + WDTHOLD ; // 关看门狗 BoardConfig(0xf0); Init_Port(); InitUART(); _EINT() ; PcdReset();//复位RC522 PcdAntennaOn();//开启天线发射 DisplayConfigMenu(); while(1) { Send1Char('>'); Send1Char('\n'); i=Get1Char(); HandleConfigMenu(i); } }
/******************************************************************************* * 描 述 : 对寻卡、防冲撞、选卡、发送卡号、读卡、写卡、修改密码进行操作 成功则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); } }
void InitRc522(void) { PcdReset(); PcdAntennaOn(); }
/**************************************************************************************************** *函数名称: rc522_ioctl *函数参数: filp: 文件指针 * cmd: 控制命令 * arg: 传递的参数 *函数返回值: 成功返回0 错误返回-1 *函数描述: spi控制函数 ****************************************************************************************************/ static int rc522_ioctl(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg) { int ret = 0; #if 0 read_block_num = 0; /* 初始化为0 */ g_read_cmd = -1; /* 初始化为-1 */ g_write_cmd = -1; printk("cmd is %x\r\n", cmd); switch(cmd) { case RC522_RESET_CMD: /* 复位 */ PcdReset(); break; case RC522_AntennaOff_CMD: /* 关闭天线 */ PcdAntennaOff(); break; case RC522_AntennaON_CMD: /* 开启天线 */ PcdAntennaOn(); break; case RC522_Request_CMD: /* 寻卡 */ g_read_cmd = RC522_Request_CMD; /* 寻卡标志 */ break; case RC522_Anticoll_CMD: g_read_cmd = RC522_Anticoll_CMD; /* 防冲撞 */ break; case RC522_Select_CMD: g_write_cmd = RC522_Select_CMD; /* 选定卡片 */ break; case RC522_AuthState_CMD: g_write_cmd = RC522_AuthState_CMD; /* 验证卡片密码 */ break; case RC522_Write_CMD: g_write_cmd = RC522_Write_CMD; /* 写块 */ break; case RC522_Read_CMD: /* 读块 */ g_read_cmd = RC522_Read_CMD; read_block_num = arg; /* 需要读取的块 */ break; case RC522_BakValue_CMD: /* 块备份 */ g_write_cmd = RC522_BakValue_CMD; break; default: printk(KERN_ERR "cmd is Invalid!\r\n"); ret = -1; } #endif return ret; }
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; }
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(); } }