__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 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	************/
	}
}
Esempio n. 3
0
void
iso14443bAnticollision(u08_t command, u08_t slots)
{
	u08_t	i = 0, collision = 0x00, j, found = 0;
	u32_t	k = 0;
	#ifdef ENABLE_HOST
		//u08_t rssi[2];
	#endif
	
	rx_error_flag = 0x00;

	buf[0] = 0x8F;
	buf[1] = 0x91;
	buf[2] = 0x3D;
	buf[3] = 0x00;
	buf[4] = 0x30;
	buf[5] = 0x05;
	buf[6] = 0x00;

	if(slots == 0x04)
	{	
		Trf797xEnableSlotCounter();
	}

	buf[7] = slots;

	if(command == 0xB1)
	{	
		buf[7] |= 0x08; 					// WUPB command else REQB command
	}

	i_reg = 0x01;

	Trf797xRawWrite(&buf[0], 8);

	IRQ_CLR;								// PORT2 interrupt flag clear
	IRQ_ON;

	j = 0;
	while((i_reg == 0x01) && (j < 2))
	{
		j++;
		McuCounterSet();			// TimerA set
		COUNT_VALUE = COUNT_1ms * 2;		// 2ms
		START_COUNTER;
		irq_flag = 0x00;
		while(irq_flag == 0x00)
		{
		}
	}										// wait for end of TX

	i_reg = 0x01;
	
	McuCounterSet();				// TimerA set
	COUNT_VALUE = COUNT_1ms * 2;			// 2ms
	START_COUNTER;

	for(i = 1; i < 17; i++)
	{	
		rxtx_state = 1;						// the response will be stored in buf[1] upwards

		while(i_reg == 0x01)				// wait for RX complete
		{	
			k++;
			if(k == 0xFFF0)
			{	
				i_reg = 0x00;
				rx_error_flag = 0x00;
				break;
			}
		}

		if(rx_error_flag == 0x02)
		{	
			i_reg = rx_error_flag;
		}
		
		if(i_reg == 0xFF)					// recieved SID in buffer
		{	
			if(stand_alone_flag == 1)
			{	
				found = 1;
			}
			else
			{	
				#ifdef ENABLE_HOST
				
					UartPutChar('[');
					for(j = 1; j < rxtx_state; j++)
					{	
						UartPutByte(buf[j]);
					}
					UartPutChar(']');

					//UartPutChar('(');
					//UartPutByte(rssi[0]);
					//UartPutChar(')');
					UartPutCrlf();
				#endif
			}
		}
		else if(i_reg == 0x02)				// collision occured
		{	
			if(stand_alone_flag == 0)
			{	
				#ifdef ENABLE_HOST
					UartPutChar('[');
					UartPutChar('z');
					UartPutChar(']');
				#endif
			}
			collision = 0x01;
		}
		else if(i_reg == 0x00)				// slot timeout
		{	
			if(stand_alone_flag == 0)
			{	
				#ifdef ENABLE_HOST
					//UartPutChar('[');
					//UartPutChar(']');
				#endif
			}
		}
		else
		{
		}

		if((slots == 0x00) || (slots == 0x01) || (slots == 0x02) || ((slots == 0x04) && (i == 16)))
		{	
			break;
		}

		iso14443bSlotMarkerCommand(i);

		i_reg = 0x01;

		if(stand_alone_flag == 0)
		{	
			#ifdef ENABLE_HOST
				//UartPutCrlf();
			#endif
		}
	}										// for

	if(slots == 0x04)
	{	
		Trf797xDisableSlotCounter();
	}

	IRQ_OFF;
	
	if(remote_flag == 0)
	{
		if(found == 1)
		{	
			LED_14443B_ON;
		}
		else
		{	
			LED_14443B_OFF;
		}
	}

	if(collision)
	{	
		iso14443bAnticollision(0x20, 0x02);	// Call this function for 16 timeslots
	}
}											// iso14443bAnticollision