__interrupt void PORT1_ISR (void) { P1IFG &= ~0x08; ///> P1.3 Interrupt Flag clear while( P1IN == 0x08 ); ///> debounce on P1.3 McuDelayMillisecond( MODE_SWITCH_DELAY_MS ); ///> 250ms delay to avoid accidental mdoe switch P1IFG &= ~0x08; ///> P1.3 Interrupt Flag clear /** * If edit mode is enabled: * reset edit mode flag, * reset empty scan counter, * reset delay counter, * print out mode message, * turn off edit mode LED. * */ if( edit_mode == 1 ) { edit_mode=0; empty_scan_cnt = 0; delay_factor = 0; UartSendCString("[MODE] Verification Mode"); UartPutCrlf(); LED_14443B_OFF; } /** * If edit mode is disbaled: * set edit mode flag, * reset empty scan counter, * reset delay counter, * print out mode message. * */ else if( edit_mode == 0 ) { edit_mode=1; empty_scan_cnt = 0; delay_factor = 0; UartSendCString("[MODE] Edit Mode"); UartPutCrlf(); ///> In debug mode, print out all patients currently stored in database if( DEBUG_MODE == 1 ) { print_patients(); } ///> turn on edit mode LED LED_14443B_ON; } }
void Type2Command(u08_t *pbuf) { switch (*pbuf) { case 0x30: Type2ReadFourBlocks(*(pbuf+1)); break; case 0xA2: Type2WriteOneBlock(*(pbuf+1)); break; case 0x31: Type2ReadTwoBlocks(*(pbuf+1)); break; case 0xA1: Type2WriteTwoBlocks(*(pbuf+1)); break; default: UartSendCString("Unknown NFC Type 2 command.\r\n"); } }
void main(void) { // WDT ~350ms, ACLK=1.5kHz, interval timer WDTCTL = WDT_ADLY_16; // Enable WDT interrupt IE1 |= WDTIE; SLAVE_SELECT_PORT_SET; SLAVE_SELECT_HIGH; ENABLE_PORT_SET; ENABLE_TRF; // wait until TRF7970A system clock started McuDelayMillisecond(2); // settings for communication with TRF7970A Trf7970CommunicationSetup(); // Set Clock Frequency and Modulation Trf7970InitialSettings(); // set the DCO to 8 MHz McuOscSel(1); // Re-configure the USART with this external clock Trf7970ReConfig(); // Configure UART UartSetup(); /************ Smart Medical NFC Scanner Project ************/ McuDelayMillisecond(5); UartSendCString("[INFO] NFC Reader ENABLED."); UartPutCrlf(); McuDelayMillisecond(2); P1SEL &= ~0x08; // Select Port 1 P1.3 (push button) P1DIR &= ~0x08; // Port 1 P1.3 (push button) as input, 0 is input P1REN |= 0x08; // Enable Port P1.3 (push button) pull-up resistor P1IE |= 0x08; // Port 1 Interrupt Enable P1.3 (push button) P1IFG &= ~0x08; // Clear interrupt flag /************ Smart Medical NFC Scanner Project ************/ // General enable interrupts __bis_SR_register(GIE); // indicates that setting are done enable = 1; // stand alone mode stand_alone_flag = 1; // launchpad LED1 P1DIR |= BIT0; //init function for the patient array init_patient(); P1IN&=BIT3; ///< Port 1.3 (left button) as input as mode switch while(1) { Tag_Count = 0; IRQ_OFF; DISABLE_TRF; // Enter LPM3 __bis_SR_register(LPM3_bits); // launchpad LED1 - Toggle (heartbeat) P1OUT ^= BIT0; // Clear IRQ Flags before enabling TRF7970A IRQ_CLR; IRQ_ON; ENABLE_TRF; /************ Smart Medical NFC Scanner Project ************/ // Must wait at least 4.8 ms to allow TRF7970A to initialize. __delay_cycles(40000); #ifdef ENABLE15693 found_tag_ISO15693 = Iso15693FindTag( edit_mode ); ///< Scan for 15693 tags #endif #ifdef ENABLE14443A found_tag_ISO14443a = Iso14443aFindTag( edit_mode ); ///< Scan for 14443A tags #endif /* We are not using 14443B type tag #ifdef ENABLE14443B //Iso14443bFindTag(); // Scan for 14443B tags #endif */ /** * Write total number of tags read to UART */ if(Tag_Count > 0){ Tag_Count = UartNibble2Ascii(Tag_Count & 0x0F); ///< convert to ASCII UartSendCString("[INFO] Tags Found: "); UartPutChar(Tag_Count); UartPutCrlf(); UartPutCrlf(); } /** * If either type of tag is found: * reset empty scan counter, * reset delay factor, * delay MCU to prevent duplicate scan, * reset tag found counter for both types of tag. * */ if( ( found_tag_ISO15693 == 1 ) || ( found_tag_ISO14443a == 1 ) ) { empty_scan_cnt = 0; delay_factor = 0; McuDelayMillisecond( SCAN_DELAY_INIT_MS ); found_tag_ISO15693 = 0; found_tag_ISO14443a = 0; } /** * If edit mode is disabled: * delay MCU by an increasing amount of time based on * delay_factor and initial delay, * increase empty scan counter. * */ if( edit_mode == 0 ) { //Dynamic delay for power saving McuDelayMillisecond( delay_factor*SCAN_DELAY_INIT_MS ); //increment empty scan counter empty_scan_cnt++; } /** * If debug mode is enabled: * print out empty scan count. * */ if( DEBUG_MODE == 1 ) { char buf[20]; sprintf( buf, "[DEBUG] Scan#%d\r", empty_scan_cnt ); UartSendCString( buf ); } /** * After 20 consecutive empty scan: * reset empty scan counter, * increment of delay counter if it's under threshold, * print out message for additional delay occurrence. * */ if( empty_scan_cnt >= 20 ) { empty_scan_cnt = 0; if( delay_factor < 4 ) { delay_factor++; UartSendCString( "[DEBUG] No TAG in range, additional 500ms delay added\n" ); } } /************ Smart Medical NFC Scanner Project ************/ } }
//----------------------------------------------// //The NFCActiveMain function calls the other NFC// //functions for Felica (active) mode. // //----------------------------------------------// u08_t NfcMain(u08_t *pbuf, u08_t tag) { u08_t i = 0, command[2]; u08_t code = 0; #if DBG_NFC command[0] = TEST_SETTINGS_1; //subcarrier output command[1] = 0x40; Trf797xWriteSingle(command, 2); command[0] = MODULATOR_CONTROL; //analog output command[1] = MODULATOR_CONTROL; Trf797xReadSingle(&command[1], 1); command[1] |= BIT3; Trf797xWriteSingle(command, 2); #endif reader_mode = 0xFF; //rxdataPointer = 0; //RXlen = 0; Trf797xResetIrqStatus(); //function call for single address read IRQ_ON; /* if ((*(pbuf + 2) == 0x00) || (*(pbuf + 2) == 0x02) || (*(pbuf + 2) == 0x04) || (*(pbuf + 2) == 0x06) || (*(pbuf + 2) == 0x08) || (*(pbuf + 2) == 0x0A) || (*(pbuf + 2) == 0x16)) { command[0] = ISOControl; command[1] = NFCIso; WriteSingle(command, 2); } */ switch(*(pbuf + 2)) { case 0x00: //ATR_REQ //send_cstring("ATR\r\n"); do { i++; if(i > 3) { code = 1; goto EXIT; } InitiatorRequest(pbuf); }while((i_reg != 0xFF) && (buf[1] != 0xD5) && (buf[2] != 0x01)); break; case 0x02: //WUP_REQ //send_cstring("WUP\r\n"); do { i++; if(i > 3) { code = 1; goto EXIT; } InitiatorRequest(pbuf); }while((i_reg != 0xFF) && (buf[1] != 0xD5) && (buf[2] != 0x03)); break; case 0x04: //PSL_REQ //send_cstring("PSL\r\n"); do { i++; if(i > 3) { code = 1; goto EXIT; } InitiatorRequest(pbuf); }while((i_reg != 0xFF) && (buf[1] != 0xD5) && (buf[2] != 0x05)); break; case 0x06: //DEP_REQ //send_cstring("DEP\r\n"); while(!InitiatorDepRequest(pbuf)) { i++; if(i > 1) { code = 1; goto EXIT; } }//while break; case 0x08: //DSL_REQ //send_cstring("DSL\r\n"); do { i++; if(i > 3) { code = 1; goto EXIT; } InitiatorRequest(pbuf); }while((i_reg != 0xFF) && (buf[1] != 0xD5) && (buf[2] != 0x09)); break; case 0x0A: //RLS_REQ //send_cstring("RLS\r\n"); do { i++; if(i > 3) { code = 1; goto EXIT; } InitiatorRequest(pbuf); }while((i_reg != 0xFF) && (buf[1] != 0xD5) && (buf[1] != 0x0B)); break; case 0x0C: //Select target mode, CID is recieved with the command //send_cstring("target\r\n"); nfc_iso = ISO_CONTROL; Trf797xReadSingle(&nfc_iso, 1); reader_mode = 0xF0; //set for target iterrupt handling TargetAnticollision(pbuf, tag); break; case 0x16: //Data excahange //kputchar('v'); //kputchar('v'); do { i++; if(i > 1) { code = 1; goto EXIT; } //kputchar('x'); //kputchar('x'); InitiatorRequest(pbuf); //kputchar('w'); //Put_byte(NFCstate); //kputchar('w'); //Put_byte(i_reg); //Put_byte(buf[1]); //Put_byte(buf[2]); //kputchar('w'); }while((i_reg != 0xFF) && (buf[1] != 0xD5) && (buf[2] != 0x09)); //kputchar('z'); //kputchar('z'); break; case 0xF0: //select initiator for passive operation active = 0x00; //NFCIso = ISOControl; //ReadSingle(&NFCIso, 1); #if DBG_NFC command[0] = TEST_SETTINGS_1; //subcarrier output command[1] = 0x40; Trf797xWriteSingle(command, 2); command[0] = MODULATOR_CONTROL; //analog output command[1] = MODULATOR_CONTROL; Trf797xReadSingle(&command[1], 1); command[1] |= BIT3; Trf797xWriteSingle(command, 2); #endif break; case 0xF1: //select initiator for active operation active = 0xFF; //NFCIso = ISOControl; //ReadSingle(&NFCIso, 1); //send_cstring("active\r\n"); command[0] = CHIP_STATE_CONTROL; command[1] = 0x01; Trf797xWriteSingle(command, 2); break; default: break; }//switch IRQ_OFF; UartSendCString("[00]"); return(code); EXIT: IRQ_OFF; UartSendCString("[]"); return(code); }//NfcMain
void Type2ReadTwoBlocks(u08_t rd2b) { u08_t i = 0, command[4]; buf[0] = 0x8F; // reset FIFO buf[1] = 0x91; // send with CRC buf[2] = 0x3D; // write continuous from register buf[3] = 0x00; // value for register 1D buf[4] = 0x20; // register 1E (# of bytes to be transmitted) buf[5] = 0x31; // RD2B command //buf[6+30] = rd2b; // addressed block buf[200] = 80; // tell apart from Ack/Nack rx_error_flag = 0x00; command[0] = SPECIAL_FUNCTION; command[1] = SPECIAL_FUNCTION; Trf797xReadSingle(&command[1], 1); command[1] |= BIT2; // enable 4-bit receive McuCounterSet(); // TimerA set COUNT_VALUE = COUNT_1ms * 5; // 5ms IRQ_CLR; // PORT2 interrupt flag clear IRQ_ON; Trf797xReset(); // FIFO has to be reset before recieving the next response Trf797xRawWrite(&buf[0], 7); // set Special Function Register i_reg = 0x01; START_COUNTER; // start timer up mode irq_flag = 0x00; while(irq_flag == 0x00) // wait for end of TX interrupt { } Trf797xWriteSingle(command, 2); // enable 4-bit receive rxtx_state = 1; // the response will be stored in buf[1] upwards McuCounterSet(); // TimerA set COUNT_VALUE = COUNT_1ms * 10; START_COUNTER; // start timer up mode i_reg = 0x01; while(i_reg == 0x01) // wait for interrupt { } if(rx_error_flag == 0x02) { i_reg = 0x02; } if(i_reg == 0xFF) // recieved response { UartPutChar('['); for(i = 1; i < rxtx_state; i++) { UartPutByte(buf[i]); } UartPutChar(']'); } else if(i_reg == 0x02) { if(buf[200] < 0x20) // not acknowledged { UartSendCString("[NACK]"); } else // collision occured { UartPutChar('['); UartPutChar('z'); UartPutChar(']'); } } else if(i_reg == 0x00) // no responce { UartPutChar('['); UartPutChar(']'); } else { } command[0] = SPECIAL_FUNCTION; command[1] = SPECIAL_FUNCTION; Trf797xReadSingle(&command[1], 1); command[1] &= ~BIT2; Trf797xWriteSingle(command, 2); // disable 4-bit receive }
void Type2WriteOneBlock(u08_t wr1b) { u08_t command[10]; //for(i=10; i>4; i--) // write parameter in right frameposition //{ // buf[i+2] = buf[i]; //} buf[0] = 0x8F; // reset FIFO buf[1] = 0x91; // send with CRC buf[2] = 0x3D; // write continuous from register buf[3] = 0x00; // value for register 1D buf[4] = 0x60; // register 1E (# of bytes to be transmitted) buf[5] = 0xA2; // WR2B command //buf[6] = wr1b; buf[200] = 80; // tell apart from Ack/Nack rx_error_flag = 0x00; command[0] = SPECIAL_FUNCTION; command[1] = SPECIAL_FUNCTION; Trf797xReadSingle(&command[1], 1); command[1] |= BIT2; Trf797xWriteSingle(command, 2); // enable 4-bit receive Trf797xRawWrite(&buf[0], 11); IRQ_CLR; // PORT2 interrupt flag clear IRQ_ON; i_reg = 0x01; rxtx_state = 1; // the response will be stored in buf[1] upwards // wait for end of transmit while(i_reg == 0x01) { McuCounterSet(); COUNT_VALUE = COUNT_1ms * 5; // for 10 ms TIMEOUT START_COUNTER; // start timer up mode irq_flag = 0x00; while(irq_flag == 0x00) // wait for interrupt { } } i_reg = 0x01; McuCounterSet(); // TimerA set COUNT_VALUE = COUNT_1ms * 6; // 6ms START_COUNTER; while(i_reg == 0x01) // wait for RX complete { } if(rx_error_flag == 0x02) { i_reg = 0x02; } if(i_reg == 0x02) { if(buf[200] < 0x20) // not acknowledged { UartSendCString("[NACK]"); } else if((buf[200] & 0xF0) == 0xA0) // acknowledged { UartSendCString("[ACK]"); } else // collision occured { UartPutChar('['); UartPutChar('z'); UartPutChar(']'); } } else if(i_reg == 0x00) // no response { UartPutChar('['); UartPutChar(']'); } else { } command[0] = SPECIAL_FUNCTION; command[1] = SPECIAL_FUNCTION; Trf797xReadSingle(&command[1], 1); command[1] &= ~BIT2; Trf797xWriteSingle(command, 2); // disable 4-bit receive }