tag_stat select_tag_sn(uint8_t * sn, uint8_t * len){ if (PcdAnticoll(PICC_ANTICOLL1,buff)!=TAG_OK) {return TAG_ERR;} if (PcdSelect(PICC_ANTICOLL1,buff)!=TAG_OK) {return TAG_ERR;} if (buff[0]==0x88) { memcpy(sn,&buff[1],3); if (PcdAnticoll(PICC_ANTICOLL2,buff)!=TAG_OK) { return TAG_ERR;} if (PcdSelect(PICC_ANTICOLL2,buff)!=TAG_OK) {return TAG_ERR;} if (buff[0]==0x88) { memcpy(sn+3,&buff[1],3); if (PcdAnticoll(PICC_ANTICOLL3,buff)!=TAG_OK) { return TAG_ERR;} if (PcdSelect(PICC_ANTICOLL3,buff)!=TAG_OK) {return TAG_ERR;} memcpy(sn+6,buff,4); *len=10; }else{ memcpy(sn+3,buff,4); *len=7; } }else{ memcpy(sn,&buff[0],4); *len=4; } return TAG_OK; }
/**************************************************************************************** ** 函数原型: void M1Identify(void) ** 函数功能: 卡片识别,包含寻卡、防冲突和选择卡 ** 入口参数: - ** 出口参数: - ** 返 回 值: - ** 描 述: ****************************************************************************************/ void M1Identify(void) { char status; status = PcdRequest(PICC_REQIDL, c_CardType); if (status != MI_OK ) { PcdRequest(PICC_REQIDL, c_CardType); #ifdef DEBUG_UART printf("PcdRequest fail 0x%x, CardType 0x%x%x\r\n", status, c_CardType[1], c_CardType[0]); #endif } PcdAnticoll(c_CardSrlNum); if (MI_OK == PcdSelect(c_CardSrlNum, sak1)) { #ifdef DEBUG_UART printf("PcdSelect success\r\n"); #endif status = 0; } else { #ifdef DEBUG_UART printf("PcdSelect fail\r\n"); #endif status = 0x01; //选卡失败 } COM_Send(c_CardSrlNum,4,status); }
/**************************************************************************************************** *函数名称: rc522_read *函数参数: filp: 文件指针 * buf: 数据读取存储数组 * size: 需要读取的数据长度 * pos: 文件偏移 *函数返回值: 成功返回0 错误返回-1 *函数描述: spi读取数据函数 ****************************************************************************************************/ static ssize_t rc522_read(struct file *filp, char __user * buf, size_t size, loff_t *pos) { int ret = 0; #if 0 uint8_t read_buf[20] = {0}; /* 读取数据暂存数组 */ switch (g_read_cmd) { case RC522_Request_CMD: ret = PcdRequest(PICC_REQALL, read_buf); /* 寻找卡 */ printk("PcdRequest = %d\r\n",ret); if (ret < 0) printk(KERN_ERR "PcdRequest error!\r\n"); break; case RC522_Anticoll_CMD: ret = PcdAnticoll(read_buf);//防冲撞 if (ret != MI_OK) printk(KERN_ERR "PcdAnticoll error!\r\n"); copy_to_user( buf, read_buf, size ); break; case RC522_Read_CMD: ret = PcdRead( read_block_num, read_buf );//读块 if (ret < 0) printk(KERN_ERR "PcdRead error!\r\n"); copy_to_user( buf, read_buf, size ); default: printk(KERN_ERR "cmd is Invalid!\r\n"); } #endif return ret; }
/**************************************************************************************** ** 函数原型: void PcdIdentify(void) ** 函数功能: 卡片识别,包含寻卡、防冲突和选择卡 ** 入口参数: - ** 出口参数: - ** 返 回 值: - ** 描 述: ****************************************************************************************/ void PcdIdentify(void) { char status; status = PcdRequest(PICC_REQIDL, c_CardType); if (status != MI_OK ) { #ifdef DEBUG_UART printf("PcdRequest fail 0x%x\r\n", status); #endif status = PcdRequest(PICC_REQIDL, c_CardType); } if (status == MI_OK) { #ifdef DEBUG_UART printf("PcdRequest OK 0x%x\r\n", status); #endif status = PcdAnticoll(c_CardSrlNum); #ifdef DEBUG_UART printf("PcdAnticoll status 0x%x\r\n", status); #endif } status = PcdSelect(c_CardSrlNum, sak1); if (status == MI_OK) { #ifdef DEBUG_UART printf("PcdSelect success\r\n"); #endif } else { #ifdef DEBUG_UART printf("PcdSelect fail 0x%x\r\n", status); #endif } }
void Auto_Reader(void) { while(1) { if(PcdRequest(0x52,Temp)==MI_OK) { if(Temp[0]==0x04&&Temp[1]==0x00) PutString("MFOne-S50"); else if(Temp[0]==0x02&&Temp[1]==0x00) PutString("MFOne-S70"); else if(Temp[0]==0x44&&Temp[1]==0x00) PutString("MF-UltraLight"); else if(Temp[0]==0x08&&Temp[1]==0x00) PutString("MF-Pro"); else if(Temp[0]==0x44&&Temp[1]==0x03) PutString("MF Desire"); else PutString("Unknown"); if(PcdAnticoll(UID)==MI_OK) { PutString0("Card Id is:"); tochar(UID[0]); tochar(UID[1]); tochar(UID[2]); tochar(UID[3]); while (!(IFG1 & UTXIFG0)); TXBUF0 = '\n'; //发送换行指令 RED_LED_ON ; Delay(200) ; RED_LED_OFF ; Delay(200) ; } } else GRE_LED_OFF ; } }
/******************************************************************************* * 描 述 : 对寻卡、防冲撞、选卡、发送卡号、读卡、写卡、修改密码进行操作 成功则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); } }
char handle_soltic(char *s,int timeout) { unsigned char temp[18]; portTickType xLastWakeTime; char i=0; char status_solt=2; xLastWakeTime = xTaskGetTickCount(); #ifdef debug printf("start solt ic card!\r\n"); #endif do { i++; status_solt=PcdRequest(PICC_REQIDL,temp); vTaskDelayUntil( &xLastWakeTime, ( timeout / portTICK_RATE_MS ) ); } while((status_solt!=0)&&(i<50)); i=0; if(0==status_solt) { // #ifdef debug printf("PcdRequest OK!\r\n"); #endif do { i++; status_solt=PcdAnticoll(temp); vTaskDelayUntil( &xLastWakeTime, ( timeout / portTICK_RATE_MS ) ); } while((status_solt!=0)&&(i<10)); i=0; if(0==status_solt) { // #ifdef debug printf("PcdAnticoll is OK \r\n"); #endif memcpy(s,temp,4); buzzer(100); return 0; } else { #ifdef debug printf("PcdAnticoll error \r\n"); #endif buzzer(200); return 2; } } else { //认为是没有刷卡,或者是无效卡 #ifdef debug printf("solt ic card error!\r\n"); #endif } return 1; }
int main(void) { // u8 x=0; unsigned char status; u8 Data[16]; u8 i; u8 k=0;//读写错误重试次数 u8 j; USART1_Config(); //printf("USART1_Config success\n"); SysTick_Init(); //printf("SysTick_Init success\n"); SPI_FLASH_Init(); //printf("SPI_FLASH_Init success\n"); InitRc522(); //printf("InitRc522 success\n"); TIM3_PWM_Init(); //printf("TIM3_PWM_Init success\n"); GPIO_OUT_Init(); //NVIC_Configuration(); EXTI_PB1_Config(); EXTI_PB2_Config(); BEEP_Init(); beep_ok(); while(1) { status = PcdRequest(PICC_REQIDL,CT);/*扫描卡*/ status = PcdAnticoll(SN);/*防冲撞*/ //printf("%d",status); if (status==MI_OK) { k=0; sn=((SN[0]<<24)|(SN[1]<<16)|(SN[2]<<8)|(SN[3])); //printf("MI_OK\n"); //printf("%x\n",sn); if((sn==YANGCHENGUANG)||(sn==LIUZHIYU)||(sn==CHENDUYU)||(sn==WUPENG)||(sn==BLUECARD)) { //printf("OPEN DOOR\n"); beep_ok(); opendoor(); } } else { k++; if((k++)>20) { SPI_FLASH_Init(); //printf("SPI_FLASH_Init success\n"); Reset_RC522(); //printf("InitRc522 success\n"); k=0; } } delay_ms(100); } }
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(); } }