//2018��2��2�ձ༭��֧��CPU��Ƭָ����ӷ�ʽ���� #include "../include/READER.h" #include "../include/CPU_CARD.h" #include #include "../include/READER_REG.h" #include "../include/rfid_main.h" #define CPU_DEBUG 0 struct CPU_CARD_STR CPU_CARD; #if 0 unsigned char CPU_CARD_EVENT( void ){ return FAIL; } #else unsigned char CPU_CARD_EVENT( void ) { unsigned char result; unsigned char SendBuffer[255]; unsigned char ReceiveBuffer[255]; unsigned char i; transmission_struct APDU; APDU.pSendBuffer = SendBuffer; /*���÷������� */ APDU.pReceiveBuffer = ReceiveBuffer; /*���ý������� */ result = CPU_Rats( &CPU_CARD.ATS.Length, CPU_CARD.ATS.Ats_Data ); if ( result != SUCCESS ) { SetCW( DISABLE ); printf( "-> RATS ERROR!\r\n" ); return result; } printf( "-> ATS = " ); for(i = 0;i < CPU_CARD.ATS.Length;i++) printf("%02X", CPU_CARD.ATS.Ats_Data[i]); printf( "\r\n" ); result = Ats_Process( CPU_CARD.ATS.Length, CPU_CARD.ATS.Ats_Data ); if ( result != SUCCESS ) { SetCW( DISABLE ); printf( "-> RATS ERROR!\r\n" ); return result; } memcpy( APDU.pSendBuffer, "\x00\xA4\x00\x00\x02\x3F\x00", 7 ); APDU.SendLength = 7; result = CPU_APDU( &APDU ); if ( result != SUCCESS ) { SetCW( DISABLE ); printf( "-> APDU ERROR!\r\n" ); return result; } printf( "->APDU = " ); for(i=0;i APDU ERROR!\r\n"); return result; } printf("-> APDU = "); for(i=0;i APDU ERROR!\r\n"); return result; } printf("-> Response = "); for(i=0;i APDU ERROR!\r\n"); return result; } printf("-> Response = "); for(i=0;i APDU ERROR!\r\n"); return result; } printf("-> Response = "); for(i=0;i APDU ERROR!\r\n" ); return result; } printf( "-> Response = " ); for(i=0;iSendLength,tpdu->pSendBuffer); ModifyReg(REG_TXCRCCON, BIT_CRCEN,ENABLE); ModifyReg(REG_RXCRCCON, BIT_CRCEN,ENABLE); // SetTimer(tpdu->Timeout); SetCommand(CMD_TRANSCEIVE); for(i = 0;i < tpdu->Timeout; i++) { DelayMs(1);//���յȴ���ʱ GetReg(REG_IRQ0,&irq0); if(irq0 & BIT_RXIRQ) { GetReg( REG_ERROR, &error ); /*Get Error Status */ error = error & (BIT_NODATAERR | BIT_COLLDET | BIT_PROTERR | BIT_INTEGERR); if(error != 0) return FAIL;//���յ������д��� GetReg(REG_FIFOLENGTH,&tpdu->ReceiveLength); if(tpdu->ReceiveLength > 0) { Read_FIFO(tpdu->ReceiveLength,tpdu->pReceiveBuffer); result = SUCCESS; return result; } } GetReg(REG_IRQ1,&irq1); // if(irq1 & BIT_TIMER1IRQ) // { // result = FAIL;//δ��timeoutʱ���ڽ��յ����� // } } result = FAIL;//δ��timeoutʱ���ڽ��յ����� return result; } #endif /****************************************************************/ /*����: Rats */ /*����: Request for answer to select */ /*����: */ /* */ /*���: */ /* ats_len�����յ�ATS���ݳ��� */ /* *ats�����յ���ATS����ָ�� */ /* OK: Ӧ����ȷ */ /* ERROR: Ӧ����� */ /****************************************************************/ #if 0 unsigned char CPU_Rats( unsigned char *ats_len, unsigned char *ats ) { return FAIL; } #else unsigned char CPU_Rats( unsigned char *ats_len, unsigned char *ats ) { unsigned char result; unsigned char outbuffer[2], inbuffer[64]; transmission_struct tpdu; tpdu.pSendBuffer = outbuffer; tpdu.pReceiveBuffer = inbuffer; tpdu.pSendBuffer[0] = 0xE0; /*Start byte */ tpdu.pSendBuffer[1] = 0x50; /*default = 0x50 */ tpdu.SendLength = 2; tpdu.Timeout = 160; //CPU_CARD.FWT = 160;//Ĭ����ʱʱ�� result = FM176XX_TPDU( &tpdu ); if ( result == SUCCESS ) { *ats_len = tpdu.ReceiveLength; memcpy( ats, tpdu.pReceiveBuffer, *ats_len ); } return (result); } #endif #if 0 unsigned char Ats_Process( unsigned char ats_len, unsigned char *ats )/* ATS���ݽ��� */ { return FAIL; } #else unsigned char Ats_Process( unsigned char ats_len, unsigned char *ats )/* ATS���ݽ��� */ { unsigned char result, offset; if ( (ats_len == ats[0]) || (ats_len > 2) )/*TL */ { CPU_CARD.FSCI = ats[1] & 0x0F; if ( CPU_CARD.FSCI == 0 ) CPU_CARD.FSC = 13; if ( CPU_CARD.FSCI == 1 ) CPU_CARD.FSC = 21; if ( CPU_CARD.FSCI == 2 ) CPU_CARD.FSC = 29; if ( CPU_CARD.FSCI == 3 ) CPU_CARD.FSC = 37; if ( CPU_CARD.FSCI == 4 ) CPU_CARD.FSC = 45; if ( CPU_CARD.FSCI == 5 ) CPU_CARD.FSC = 61; if ( CPU_CARD.FSCI == 6 ) CPU_CARD.FSC = 93; if ( CPU_CARD.FSCI == 7 ) CPU_CARD.FSC = 125; if ( CPU_CARD.FSCI > 7 ) CPU_CARD.FSC = 253; //CPU_CARD.FSC = 13; printf( "-> CPU_CARD.FSC = %02X\r\n",CPU_CARD.FSC ); } else { result = FAIL; return (result); } offset = 0; result = SUCCESS; if ( ats[1] & BIT5 ) /*TA */ { CPU_CARD.TA = ats[2]; offset++; } if ( ats[1] & BIT6 ) /*TB */ { CPU_CARD.TB = ats[2 + offset]; CPU_CARD.SFGI = CPU_CARD.TB & 0x0F; CPU_CARD.FWI = (CPU_CARD.TB >> 4) & 0x0F; printf( "-> CPU_CARD.SFGI = %02X\r\n",CPU_CARD.SFGI ); printf( "-> CPU_CARD.FWI = %02X\r\n",CPU_CARD.FWI); if ( CPU_CARD.FWI < 2 ) CPU_CARD.FWT = 1; if ( CPU_CARD.FWI == 2 ) CPU_CARD.FWT = 2; if ( CPU_CARD.FWI == 3 ) CPU_CARD.FWT = 3; if ( CPU_CARD.FWI == 4 ) CPU_CARD.FWT = 5; if ( CPU_CARD.FWI == 5 ) CPU_CARD.FWT = 10; if ( CPU_CARD.FWI == 6 ) CPU_CARD.FWT = 20; if ( CPU_CARD.FWI == 7 ) CPU_CARD.FWT = 40; if ( CPU_CARD.FWI == 8 ) CPU_CARD.FWT = 80; if ( CPU_CARD.FWI == 9 ) CPU_CARD.FWT = 160; if ( CPU_CARD.FWI == 10 ) CPU_CARD.FWT = 310; if ( CPU_CARD.FWI == 11 ) CPU_CARD.FWT = 620; if ( CPU_CARD.FWI == 12 ) CPU_CARD.FWT = 1240; if ( CPU_CARD.FWI == 13 ) CPU_CARD.FWT = 2480; if ( CPU_CARD.FWI > 13 ) CPU_CARD.FWT = 4950; offset++; } if ( ats[1] & BIT7 )/*TC */ { CPU_CARD.TC = ats[2 + offset]; offset++; } CPU_CARD.PCB = 0x02;/*PCB��ʼֵΪ0x02 */ return (result); } #endif #if 1 //xtell 同时把CPU_TPDU 和 CPU_NAk 用空函数替换 SPi正常 unsigned char CPU_NAK( transmission_struct *tpdu ) /* ��Ƭ����NAK */ { return FAIL; } #else unsigned char CPU_NAK( transmission_struct *tpdu ) /* ��Ƭ����NAK */ { unsigned char result, tpdu_send_buffer[255],tpdu_receive_buffer[255]; tpdu->pSendBuffer = tpdu_send_buffer; tpdu->pReceiveBuffer = tpdu_receive_buffer; tpdu->pSendBuffer [0] = 0xB0 | CPU_CARD.PCB; tpdu->SendLength = 1; /*printf( "NAK\r\n" );*/ result = FM176XX_TPDU( tpdu ); return (result); } #endif #if 0 unsigned char CPU_TPDU( transmission_struct *tpdu ) { return FAIL; } #else unsigned char CPU_TPDU( transmission_struct *tpdu ) { unsigned char result, i, pcb_byte; transmission_struct nak_tpdu; result = FM176XX_TPDU( tpdu ); for ( i = 0; i < 3; i++ ) { if ( result != SUCCESS ) { result = CPU_NAK( &nak_tpdu ); if(result == SUCCESS) { if(nak_tpdu.ReceiveLength > 0) { memcpy( &pcb_byte, nak_tpdu.pReceiveBuffer, 1 ); if((pcb_byte & 0xF0) == 0xA0)//R(ACK) { #if CPU_DEBUG printf("...pcb_byte = %02X\r\n",pcb_byte); printf("...CPU_CARD.PCB = %02X\r\n",CPU_CARD.PCB); #endif if((pcb_byte & 0x03) != CPU_CARD.PCB) { result = FM176XX_TPDU( tpdu ); } else { tpdu->pSendBuffer[0] = tpdu->pSendBuffer[0] ^ 0x01;// CPU_CARD.PCB = tpdu->pSendBuffer[0] & 0x03; result = FM176XX_TPDU( tpdu ); } } } } } else { break; } } return (result); } #endif /****************************************************************/ /*����: CPU_APDU */ /*����: �ú���ʵ��ת��APDU�����ʽ���� CPU card reset */ /*����: */ /* */ /*���: */ /* ats_len�����յ����ݳ��� */ /* ats�����յ�����ָ�� */ /* OK: Ӧ����ȷ */ /* ERROR: Ӧ����� */ /****************************************************************/ #if 1 //xtell 同时把CPU_TPDU 和 CPU_NAk 用空函数替换 SPi正常 unsigned char CPU_APDU( transmission_struct *apdu ) { return FAIL; } #else unsigned char CPU_APDU( transmission_struct *apdu ) { unsigned char result, pcb_byte, tpdu_send_buffer[255],tpdu_receive_buffer[255], i; unsigned char unsent_length; transmission_struct tpdu; tpdu.pSendBuffer = tpdu_send_buffer; tpdu.pReceiveBuffer = tpdu_receive_buffer; tpdu.Timeout = CPU_CARD.FWT; apdu->ReceiveLength = 0; unsent_length = apdu->SendLength; /*���ô��������ݳ��� */ for ( i = 0; i < 16; i++ ) /*�������̣�����С���鳤��16��������16���鷢�� */ { #if CPU_DEBUG printf("unsent_length = %02X\r\n",unsent_length); #endif if ( unsent_length < CPU_CARD.FSC ) { /*���������ݳ���С�����֡���ȣ��������д��������� */ tpdu.pSendBuffer[0] = CPU_CARD.PCB; /*PCB�ֽ�д��TPDU���� */ memcpy( tpdu.pSendBuffer + 1, apdu->pSendBuffer + apdu->SendLength - unsent_length, unsent_length ); /*APDU����д��TPDU���� */ tpdu.SendLength = unsent_length + 1; /*���ͳ�������1 */ #if CPU_DEBUG printf("--> "); for(i=0;ipSendBuffer + apdu->SendLength - unsent_length, CPU_CARD.FSC - 1 ); /*APDU����д��TPDU���� */ tpdu.SendLength = CPU_CARD.FSC; /*���鷢�ͳ��� */ #if CPU_DEBUG printf("..--> "); for(i=0;ipReceiveBuffer + apdu->ReceiveLength, tpdu.pReceiveBuffer + 1, tpdu.ReceiveLength - 1 ); apdu->ReceiveLength = apdu->ReceiveLength + tpdu.ReceiveLength - 1; return (SUCCESS); /*������ݽ������� */ } if ( (pcb_byte == 0x12) || (pcb_byte == 0x13) ) { memcpy( apdu->pReceiveBuffer + apdu->ReceiveLength, tpdu.pReceiveBuffer + 1, tpdu.ReceiveLength - 1 ); apdu->ReceiveLength = apdu->ReceiveLength + tpdu.ReceiveLength - 1; tpdu.pSendBuffer[0] = ( (pcb_byte & 0x03) | 0xA0) ^ 0x01; /*�յ�����֡������PCB�ֽ� */ tpdu.SendLength = 1; #if CPU_DEBUG printf("...--> = "); for(i=0;i = "); for(i=0;i