__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;
	}

}
Esempio n. 2
0
void
Iso14443bFindTag(void)
{	
	Trf797xTurnRfOn();
	
	Trf797xWriteIsoControl(0x0C);
	
	// When a PICC is exposed to an unmodulated operating field
	// it shall be able to accept a quest within 5 ms.
	// PCDs should periodically present an unmodulated field of at least
	// 5,1 ms duration. (ISO14443-3)
	McuDelayMillisecond(6);

	iso14443bAnticollision(0xB0, 0x04); // 16 slots (0xB0, then 0x04 for 16 slots or 0x00 for 1 slot)

	Trf797xTurnRfOff();

	Trf797xResetIrqStatus();
}
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. 4
0
void hydranfc_scan_vicinity(t_hydra_console *con)
{
	static uint8_t data_buf[VICINITY_UID_MAX];
	uint8_t fifo_size;
	int i;

	/* End Test delay */
	irq_count = 0;

	/* Test ISO15693 read UID */
	Trf797xInitialSettings();
	Trf797xReset();

	/* Write Modulator and SYS_CLK Control Register (0x09) (13.56Mhz SYS_CLK and default Clock 13.56Mhz)) */
	data_buf[0] = MODULATOR_CONTROL;
	data_buf[1] = 0x31;
	Trf797xWriteSingle(data_buf, 2);

	/* Configure Mode ISO Control Register (0x01) to 0x02 (ISO15693 high bit rate, one subcarrier, 1 out of 4) */
	data_buf[0] = ISO_CONTROL;
	data_buf[1] = 0x02;
	Trf797xWriteSingle(data_buf, 2);

	/* Configure Test Settings 1 to BIT6/0x40 => MOD Pin becomes receiver subcarrier output (Digital Output for RX/TX) */
	/*
	data_buf[0] = TEST_SETTINGS_1;
	data_buf[1] = BIT6;
	Trf797xWriteSingle(data_buf, 2);

	data_buf[0] = TEST_SETTINGS_1;
	Trf797xReadSingle(data_buf, 1);
	if (data_buf[0] != 0x40)
	{
		cprintf(con, "Error Test Settings Register(0x1A) read=0x%02lX (shall be 0x40)\r\n", (uint32_t)data_buf[0]);
		err++;
	}
	*/

	/* Turn RF ON (Chip Status Control Register (0x00)) */
	Trf797xTurnRfOn();

	McuDelayMillisecond(10);

	/* Send Inventory(3B) and receive data + UID */
	data_buf[0] = 0x26; /* Request Flags */
	data_buf[1] = 0x01; /* Inventory Command */
	data_buf[2] = 0x00; /* Mask */

	fifo_size = Trf797x_transceive_bytes(data_buf, 3, data_buf, VICINITY_UID_MAX,
					     10, /* 10ms TX/RX Timeout (shall be less than 10ms (6ms) in High Speed) */
					     1); /* CRC enabled */
	if (fifo_size > 0) {
		/* fifo_size should be 10. */
		cprintf(con, "UID:");
		for (i = 0; i < fifo_size; i++)
			cprintf(con, " 0x%02lX", (uint32_t)data_buf[i]);
		cprintf(con, "\r\n");

		/* Read RSSI levels and oscillator status(0x0F/0x4F) */
		data_buf[0] = RSSI_LEVELS;
		Trf797xReadSingle(data_buf, 1);
		if (data_buf[0] < 0x40) {
			cprintf(con, "RSSI error: 0x%02lX (should be > 0x40)\r\n", (uint32_t)data_buf[0]);
		}
	}

	/* Turn RF OFF (Chip Status Control Register (0x00)) */
	Trf797xTurnRfOff();

	/*
	cprintf(con, "irq_count: 0x%02ld\r\n", (uint32_t)irq_count);
	irq_count = 0;
	*/
}
Esempio n. 5
0
static bool init_gpio(t_hydra_console *con)
{
	/* PA7 as Input connected to TRF7970A MOD Pin */
	// palSetPadMode(GPIOA, 7, PAL_MODE_INPUT);

	/* Configure NFC/TRF7970A in SPI mode with Chip Select */
	/* TRF7970A IO0 (To set to "0" for SPI) */
	palClearPad(GPIOA, 3);
	palSetPadMode(GPIOA, 3, PAL_MODE_OUTPUT_PUSHPULL | PAL_STM32_OSPEED_MID1);

	/* TRF7970A IO1 (To set to "1" for SPI) */
	palSetPad(GPIOA, 2);
	palSetPadMode(GPIOA, 2, PAL_MODE_OUTPUT_PUSHPULL | PAL_STM32_OSPEED_MID1);

	/* TRF7970A IO2 (To set to "1" for SPI) */
	palSetPad(GPIOC, 0);
	palSetPadMode(GPIOC, 0, PAL_MODE_OUTPUT_PUSHPULL | PAL_STM32_OSPEED_MID1);

	/*
	 * Initializes the SPI driver 1. The SPI1 signals are routed as follows:
	 * Shall be configured as SPI Slave for TRF7970A NFC data sampling on MOD pin.
	 * NSS. (Not used use Software).
	 * PA5 - SCK.(AF5)  => Connected to TRF7970A SYS_CLK pin
	 * PA6 - MISO.(AF5) (Not Used)
	 * PA7 - MOSI.(AF5) => Connected to TRF7970A MOD pin
	 */
	/* spiStart() is done in sniffer see sniffer.c */
	/* SCK.     */
	palSetPadMode(GPIOA, 5, PAL_MODE_ALTERNATE(5) | PAL_STM32_OSPEED_MID1);
	/* MISO. Not used/Not connected */
	palSetPadMode(GPIOA, 6, PAL_MODE_ALTERNATE(5) | PAL_STM32_OSPEED_MID1);
	/* MOSI. connected to TRF7970A MOD Pin */
	palSetPadMode(GPIOA, 7, PAL_MODE_ALTERNATE(5) | PAL_STM32_OSPEED_MID1);

	/*
	 * Initializes the SPI driver 2. The SPI2 signals are routed as follow:
	 * PC1 - NSS.
	 * PB10 - SCK.
	 * PC2 - MISO.
	 * PC3 - MOSI.
	 * Used for communication with TRF7970A in SPI mode with NSS.
	 */
	mode_con1.proto.dev_gpio_pull = MODE_CONFIG_DEV_GPIO_NOPULL;
	mode_con1.proto.dev_speed = 5; /* 5 250 000 Hz */
	mode_con1.proto.dev_phase = 1;
	mode_con1.proto.dev_polarity = 0;
	mode_con1.proto.dev_bit_lsb_msb = DEV_SPI_FIRSTBIT_MSB;
	mode_con1.proto.dev_mode = DEV_SPI_MASTER;
	bsp_spi_init(BSP_DEV_SPI2, &mode_con1.proto);

	/* NSS - ChipSelect. */
	palSetPad(GPIOC, 1);
	palSetPadMode(GPIOC, 1, PAL_MODE_OUTPUT_PUSHPULL | PAL_STM32_OSPEED_MID1);
	/* SCK.     */
	palSetPadMode(GPIOB, 10, PAL_MODE_ALTERNATE(5) | PAL_STM32_OSPEED_MID1);
	/* MISO.    */
	palSetPadMode(GPIOC, 2, PAL_MODE_ALTERNATE(5) | PAL_STM32_OSPEED_MID1);
	/* MOSI.    */
	palSetPadMode(GPIOC, 3, PAL_MODE_ALTERNATE(5) | PAL_STM32_OSPEED_MID1);

	/* Enable TRF7970A EN=1 (EN2 is already equal to GND) */
	palClearPad(GPIOB, 11);
	palSetPadMode(GPIOB, 11, PAL_MODE_OUTPUT_PUSHPULL | PAL_STM32_OSPEED_MID1);
	McuDelayMillisecond(2);

	palSetPad(GPIOB, 11);
	/* After setting EN=1 wait at least 21ms */
	McuDelayMillisecond(21);

	if (!hydranfc_test_shield()) {
		if(con != NULL)
			cprintf(con, "HydraNFC not found.\r\n");
		return FALSE;
	}

	/* Configure K1/2/3/4 Buttons as Input */
	palSetPadMode(GPIOB, 7, PAL_MODE_INPUT);
	palSetPadMode(GPIOB, 6, PAL_MODE_INPUT);
	palSetPadMode(GPIOB, 8, PAL_MODE_INPUT);
	palSetPadMode(GPIOB, 9, PAL_MODE_INPUT);

	/* Configure D2/3/4/5 LEDs as Output */
	D2_OFF;
	D3_OFF;
	D4_OFF;
	D5_OFF;
	palSetPadMode(GPIOB, 0, PAL_MODE_OUTPUT_PUSHPULL | PAL_STM32_OSPEED_MID1);
	palSetPadMode(GPIOB, 3, PAL_MODE_OUTPUT_PUSHPULL | PAL_STM32_OSPEED_MID1);
	palSetPadMode(GPIOB, 4, PAL_MODE_OUTPUT_PUSHPULL | PAL_STM32_OSPEED_MID1);
	palSetPadMode(GPIOB, 5, PAL_MODE_OUTPUT_PUSHPULL | PAL_STM32_OSPEED_MID1);

	/* Activates the EXT driver 1. */
	if(con != NULL)
		extStart(&EXTD1, &extcfg);

	return TRUE;
}