interrupt handling

Dependencies:  

Files at this revision

API Documentation at this revision

Comitter:
soumi_ghsoh
Date:
Thu Apr 30 23:30:55 2015 +0000
Parent:
12:336524cf63de
Commit message:
2015apr30

Changed in this revision

host.c Show diff for this revision Revisions of this file
host.h Show diff for this revision Revisions of this file
interruptStuff.cpp Show diff for this revision Revisions of this file
interruptStuff.h Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
nRF51822.lib Show annotated file Show diff for this revision Revisions of this file
readerComm.cpp Show annotated file Show diff for this revision Revisions of this file
readerComm.h Show annotated file Show diff for this revision Revisions of this file
--- a/host.c	Fri Apr 10 23:16:44 2015 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,478 +0,0 @@
-////void trf797xInitialSettings(void)
-////// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
-//////  trf797xInitialSettings()
-////// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
-////{
-////    uint8_t mod_control[2];
-////    
-////    mod_control[0] = SOFT_INIT;
-////    trf797xDirectCommand(mod_control);
-////    
-////    mod_control[0] = IDLE;
-////    trf797xDirectCommand(mod_control);
-////    
-////    mod_control[0] = MODULATOR_CONTROL;
-////    mod_control[1] = 0x21;                  // 6.78 MHz, OOK 100%
-////    trf797xWriteSingle(mod_control, 2);
-////    
-////    mod_control[0] = MODULATOR_CONTROL;
-////    trf797xReadSingle(mod_control, 1);
-//////    printf("\r\nMOD: 0x%02X 0x%02X\r\n", mod_control[0], mod_control[1]);
-////    
-////    /*
-////    mod_control[0] = CHIP_STATUS_CONTROL;
-////    mod_control[1] = 0x01;
-////    trf797xWriteSingle(mod_control, 2);
-////    
-////    mod_control[0] = CHIP_STATUS_CONTROL;
-////    trf797xReadSingle(mod_control, 1);
-////    printf("CHIP: 0x%02X 0x%02X\r\n", mod_control[0], mod_control[1]);
-////    
-////    mod_control[0] = ISO_CONTROL;
-////    mod_control[1] = 0x02;
-////    trf797xWriteSingle(mod_control, 2);
-////    
-////    mod_control[0] = ISO_CONTROL;
-////    mod_control[1] = ISO_CONTROL;
-////    trf797xReadSingle(mod_control, 1);
-////    printf("ISO: 0x%02X 0x%02X\r\n", mod_control[0], mod_control[1]);
-////    
-////    mod_control[0] = RX_SPECIAL_SETTINGS;
-////    mod_control[1] = 0x40;
-////    trf797xWriteSingle(mod_control, 2);
-////    
-////    mod_control[0] = RX_SPECIAL_SETTINGS;
-////    mod_control[1] = RX_SPECIAL_SETTINGS;
-////    trf797xReadSingle(mod_control, 1);
-////    printf("RX: 0x%02X 0x%02X\r\n", mod_control[0], mod_control[1]);
-////    */
-////    /*
-////    command[0] = CHIP_STATUS_CONTROL;
-////    command[1] = CHIP_STATUS_CONTROL;
-////    command[1] = 0x01;                  // Set bit 0 => Vin = 5V
-////    trf797xWriteSingle(command, 2);
-////
-////    command[0] = CHIP_STATUS_CONTROL;
-////    command[1] = CHIP_STATUS_CONTROL;
-////    trf797xReadSingle(command, 1);
-////    printf("CHIP 0x%02X  0x%02X\r\n", command[0], command[1]);
-////    */
-////}   // End of trf797xInitialSettings()
-//
-//
-//
-//void trf797xTxNextSlot(void)
-//// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
-////  trf797xTxNextSlot()
-//// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
-//{
-//    command[0] = TRANSMIT_NEXT_SLOT;
-//    trf797xDirectCommand(command);
-//}
-//
-//void trf797xDisableSlotCounter(void)
-//// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
-////  trf797xDisableSlotCounter()
-//// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
-//{
-//    buf[40] = IRQ_MASK;
-//    buf[41] = IRQ_MASK;
-//    trf797xReadSingle(&buf[41], 1);
-//    buf[41] &= 0xFE;
-//    trf797xWriteSingle(&buf[40], 2);
-//}
-//
-//void trf797xReset(void)
-//// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
-////  trf797xReset()
-//// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
-//{
-//    command[0] = RESET;
-//    trf797xDirectCommand(command);
-//}
-//
-//void trf797xTurnRfOn(void)
-//// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
-////  trf797xTurnRfOn()
-//// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
-//{   
-//    command[0] = CHIP_STATUS_CONTROL;
-//    command[1] = CHIP_STATUS_CONTROL;
-//    trf797xReadSingle(&command[1], 1);
-//    command[1] &= 0x3F;
-//    command[1] |= 0x21; // Oroiginal code has 0x20 !!!
-////    printf("\r\nCHIP 0x%02X  0x%02X\r\n", command[0], command[1]);
-//    trf797xWriteSingle(command, 2);
-////    printf("\r\nCHIP 0x%02X  0x%02X\r\n", command[0], command[1]);
-//}   // End of trf797xTurnRfOn()
-//
-//void trf797xTurnRfOff(void)
-//// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
-////  trf797xTurnRfOff()
-//// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
-//{
-//    command[0] = CHIP_STATUS_CONTROL;
-//    command[1] = CHIP_STATUS_CONTROL;
-//    trf797xReadSingle(&command[1], 1);
-//    command[1] &= 0x1F;
-//    trf797xWriteSingle(command, 2);
-//}   // End of trf797xTurnRfOff()
-//
-//void trf797xWriteIsoControl(uint8_t iso_control)
-//// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
-////  trf797xWriteIsoControl()
-//// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
-//{
-//    uint8_t write[4];
-//    
-//    if ((iso_control & BIT5) == BIT5) {
-//        printf("iso_control bit5 != 0\r\n");
-//        return;
-//    }
-//    
-//    write[0] = ISO_CONTROL;
-//    write[1] = iso_control;
-//    write[1] &= ~BIT5;
-//    trf797xWriteSingle(write, 2);
-//    iso_control &= 0x1F;
-//}   // End of trf797xWriteIsoControl()
-//
-//void iso15693FindTag(void)
-//// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
-////  iso15693FindTag()
-////  Description:    Used to detect ISO15693 conforming tags.
-////                  If an ISO15693 conforming tag is found ISO15693LED is turned ON.
-////                  1) Turn RF ON
-////                  2) Perform a complete anticollision sequence
-////                  3) Turn RF OFF
-//// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
-//{  // testPin=1;
-//    trf797xTurnRfOn();
-//   // testPin=0;
-//    //=======================================================diable interrupt from trf797x 
-//   //  readerInt.disable_irq();                                // comment: has no effect in detectecting interrupt from trf797x, works without disabling too $SG
-//    //=======================================================diable interrupt from trf797x  $SG
-//   // testPin=1;
-//    trf797xWriteIsoControl(0x02);
-//   // testPin=0;
-//    wait_ms(6); // The VCD should wait at least 1 ms after it activated the
-//                // powering field before sending the first request, to
-//                // ensure that the VICCs are ready to receive it. (ISO15693-3)
-//    /*
-//    //  Field Level Test:
-//    buffer[0] = RUN_DECODERS;   // Enable receiver
-//    trf797xDirectCommand(buffer);
-//    buffer[0] = CHIP_STATUS_CONTROL;
-//    buffer[1] = 0x23;
-//    trf797xWriteSingle(buffer, 2);
-//    wait_ms(1);
-//    buffer[0] = CHECK_INTERNAL_RF;
-//    trf797xDirectCommand(buffer);
-//    buffer[0] = RSSI_LEVELS;
-//    trf797xReadSingle(buffer, 1);
-//    printf("RSSI: 0x%X\r\n", buffer[0]);
-//    //  Result: RSSI = 0x5C (Range is 0x40 to 0x7F)
-//    */
-//    
-//    flags = SIXTEEN_SLOTS;                  // SIXTEEN_SLOTS = 0x06
-////    flags = ONE_SLOT;                       // ONE_SLOT = 0x26
-//    buf[20] = 0x00;
-//  //  debug2LED = LED_ON;
-//      
-//    iso15693Anticollision(&buf[20],0x00);  // Send Inventory request
-//    
-//    trf797xTurnRfOff();
-//    trf797xResetIrqStatus();   
-//                    // Clear any IRQs
-//}   // End of iso15693FindTag()
-//
-//void iso15693Anticollision(uint8_t *mask, uint8_t length)
-//// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
-////  iso15693Anticollision()
-////  Description: Used to perform an inventory cycle of 1 or 16 timeslots.
-////               Send command, receive response and send response to host.
-////  Parameters: *mask =     mask value
-////              length =    number of significant bits of mask value
-//// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
-//{   
-//    uint8_t     i = 1, j = 1, command[2], no_slots, found = 0;
-//    uint8_t     *p_slot_no, slot_no[17];
-//    uint8_t     new_mask[8], new_length, mask_size;
-//    uint32_t    size;
-//    uint8_t     fifo_length = 0;
-//    uint16_t    k = 0;
-////    uint8_t     irqStatus[2], iso_control;
-//
-//    slot_no[0] = 0x00;
-//    if ((flags & BIT5) == 0x00) {                       // flags<5> is the number of slots testPin
-//        no_slots = 16;                                  // 16 slots if flags<5> is cleared
-//    }
-//    else {
-//        no_slots = 1;                                   // 1 slot if flags<5> is set
-//    }
-//    
-//    p_slot_no = &slot_no[0];                            // slot number pointer
-//    mask_size = (((length >> 2) + 1) >> 1);             // mask_size is 1 for length = 4 or 8
-//    buf[0] = 0x8F;                                      // RESET
-//    buf[1] = 0x91;                                      // send with CRC
-//    buf[2] = 0x3D;                                      // write continuous from 1D
-//    buf[5] = flags;                                     // ISO15693 flags
-//    buf[6] = 0x01;                                      // anticollision command code
-//    
-//    if (flags & 0x10) {                                 // mask_size is 2 for length = 12 or 16 and so on
-//        size = mask_size + 4;                           // mask value + mask length + afi + command code + flags
-//        buf[7] = afi;
-//        buf[8] = length;                                // mask length
-//        if (length > 0) {
-//            for(i = 0; i < mask_size; i++) {
-//                buf[9 + i] = *(mask + i);
-//            }
-//        }
-//        fifo_length = 9;
-//    }
-//    else {                                              // mask_size is 2 for length = 12 or 16 and so on
-//        size = mask_size + 3;                           // mask value + mask length + command code + flags
-//        buf[7] = length;                                // mask length
-//        if(length > 0) {
-//            for(i = 0; i < mask_size; i++) {
-//                buf[8 + i] = *(mask + i);
-//            }
-//        }
-//        fifo_length = 8;
-//    }
-//    
-//    buf[3] = (char) (size >> 8);
-//    buf[4] = (char) (size << 4);
-//  
-//  
-//    
-//    trf797xResetIrqStatus();
-//   
-//    //  ***** Original code sets up a 30 ms counter here *****
-//    //  ***** Original code enables IRQ here *****
-//    //  ***** nRF51422 clear IRQ register *****
-//    readerInt.enable_irq();
-//    
-//    trf797xRawWrite(&buf[0], mask_size + fifo_length);      // Write to FIFO
-//    
-//    //===================================================runs till here !$SG
-//    irqRegister = 0x01;
-//    irqFlag = 0x00; 
-//   
-//    
-// 
-//    //
-//   for (j = 1; j <= no_slots; j++) {                     // 1 or 16 available timeslots
-//       rxtxState = 1;
-//        //  ***** Original code sets up a 20 ms counter and starts it here *****
-//        //irqFlag = 0x00;
-//      // while(1)
-//        while(irqFlag == 0x00)
-//        { ; 
-//        }  
-//        
-//     //  ISO15693LED = LED_ON;
-//      //  wait_ms(1000); 
-//     //  ISO15693LED = LED_OFF;  
-//       irqRegister = 0x01;
-//                               // Wait for interrupt
-//        //  ***** Original code stops the counter here *****
-//  //      
-//        while(irqRegister == 0x01) {                    // Wait for RX complete
-//            k++;
-//           if (k == 0xFFF0) { //printf("k == 0xFFF0 _MAIN \r\n");  
-//               irqRegister = 0x00;
-//                rxErrorFlag = 0x00;
-//            }
-//       }
-//
-//
-//
-//
-////wait_us(1000);
-//
-//        command[0] = RSSI_LEVELS;                       // Read RSSI levels
-//        trf797xReadSingle(command, 1);
-//        
-//        switch (irqRegister) {
-//            case 0xFF:                  // If received UID in buffer             //0xff changed to 0x01 $SG
-//               found = 1;
-//                printf("\r\nISO15693: [ ");
-//                for (i=3; i < 11; i++) {
-//                    printf("%c", buf[i]);
-//                }
-//                printf(", %d ]\r\n", command[0]);
-//            break;
-//            
-//            case 0x02:   printf("collision occured _MAIN \r\n");               // Collision occurred
-//                p_slot_no++;            // Remember a collision was detected
-//                *p_slot_no = j;
-//            break;
-//            
-//            case 0x00:    // printf("Timer interrupt \r\n");             // Timer interrupt
-//               //  ***** Original code does nothing! *****
-//            break;
-//            
-//           default:
-//                //  ***** Original code does nothing *****
-//            break;
-//        }   // switch (irqRegister)
-//        
-//        command[0] = RESET;             // FIFO must be reset before receiving the next response
-//        trf797xDirectCommand(command);
-//        
-//        if ((no_slots == 16) && (j < 16)) {         // If 16 slots used then send EOF (next slot)
-//            trf797xStopDecoders();
-//            trf797xRunDecoders();
-//            command[0] = RESET;                 // new
-//            trf797xDirectCommand(command);      // new
-//           trf797xTxNextSlot();   // RESET must precede txNextSlot, see "TRF7970A FW Design Hints SLOA159.
-//        }
-//        else if ((no_slots == 16) && (j == 16)) {   // At end of slot 16, stop the slot counter
-//            trf797xStopDecoders();
-//            trf797xDisableSlotCounter();
-//        }
-//        else if (no_slots == 1) {                   // 1 slot is used
-//           break;
-//        }
-//    }   // for (j = 1; j <= no_slots; j++)
-//   //================================================ 
-//   //===================================================
-//    if (found == 1) {
-//        ISO15693LED = LED_ON;
-//   }
-//   else {printf("tag not read \r\n");
-//      ISO15693LED = LED_OFF;
-//   }
-////    
-//    new_length = length + 4;                        // The mask length is a multiple of 4 bits
-//    mask_size = (((new_length >> 2) + 1) >> 1);
-//   while ((*p_slot_no != 0x00) && (no_slots == 16) && (new_length < 61) && (slot_no[16] != 16)) {
-//        *p_slot_no = *p_slot_no - 1;
-//
-//       for(i = 0; i < 8; i++)
-//        {
-//           new_mask[i] = *(mask + i);              // First the whole mask is copied
-//       }
-//
-//       if((new_length & BIT2) == 0x00)
-//        {
-//            *p_slot_no = *p_slot_no << 4;
-//        }
-//        else
-//        {
-//            for(i = 7; i > 0; i--)
-//           {
-//               new_mask[i] = new_mask[i - 1];
-//           }
-//           new_mask[0] &= 0x00;
-//       }
-//        new_mask[0] |= *p_slot_no;                  // The mask is changed
-//       wait_ms(2);
-//
-//
-//        printf("Idx: 0x%02X  Buf: 0x%02X,  ", bufIdx, debugBuffer[bufIdx-1]);
-//
-//       iso15693Anticollision(&new_mask[0], new_length);    // Recursive call with new Mask
-//
-//       p_slot_no--;
-//       
-//   }
-//    //==================================================
-//    //  ***** Original code disables IRQ here *****
-//    //readerInt.disable_irq();
-//  //debug2LED = LED_OFF;
-//}   // End of iso15693Anticollision()
-///*
-//    Header file for main.cpp
-//*/
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-    
-    ////  Setup LEDs
-//    heartbeatLED = LED_OFF;
-//    ISO15693LED = LED_OFF;
-//    debug1LED = LED_OFF;
-//    debug2LED = LED_OFF;
-    
-    //for (uint8_t i=0; i<4; i++) {
-//        heartbeatLED = LED_ON;
-//       ISO15693LED = LED_ON;
-//       debug1LED = LED_ON;
-//       wait_ms(100);
-//       heartbeatLED = LED_OFF;
-//       ISO15693LED = LED_OFF;
-//       debug1LED = LED_OFF;
-//       wait_ms(100);
-//           }
-//    
-//    Ticker  heartbeat;
-//    heartbeat.attach(blinkHeartbeatLED, 1);
-//    printf("LEDs, ");
-   //=============================================================== 
-    //  Setup the SPI interface
- //   spi.format(8, 1);       // 8 bit data, mode = 1 (transition on rising edge, sample on falling edge)
-//    spi.frequency(1000000); // SCLK = 1 MHz
-////    printf("SPI, ");
-//    
-//    //  Set On-Off Keying modulation
-//    ook_ask.output();
-//    ook_ask = 1;
-////    printf("OOK, ");
-//    
-//    //  Apply initial settings to the TRF7970
-//    testPin=1;
-//    trf797xInitialSettings();
-////    printf("Initialized, ");
-//   testPin=0;
-//    //  Tri-state OOK pin
-//    ook_ask.input();
-//    ook_ask.mode(PullUp);
-//    
-//    readerMode = 0x00;
-   //============================================================= 
-    /*
-    //  Test: Write 0xAA to MODULATOR_CONTROL, then verify by reading MODULATOR_CONTROL.
-//    printf("\r\n");
-    buffer[0] = MODULATOR_CONTROL;
-    buffer[1] = 0xAA;
-//    printf("BEFORE WR: buffer[0]: %X  buffer[1]: %X\r\n", buffer[0], buffer[1]);
-    spiWriteSingle(buffer, 2);
-//    printf("AFTER WR:  buffer[0]: %X  buffer[1]: %X\r\n", buffer[0], buffer[1]);
-    spiReadSingle(buffer, 1);
-//    printf("AFTER RD:  buffer[0]: %X  buffer[1]: %X\r\n", buffer[0], buffer[1]);
-//    printf("\r\n");
-    */
-    
-   // printf("\r\nFinished Init\r\n");
-    
-    //  Setup interrupt from TRF7970
-  //  testPin=1;
-  //========================
- //  trf797xSetupIrq();
-   //======================
-  // testPin=0;
-//    printf("IRQ setup, ");
-    
-//    printf("finished init.\r\n\r\n");
-
-  //  while(1) {                                    // changed TRUE to 1 $SG
-//       iso15693FindTag();
-//    }
-//    
-//}   // End of main()
--- a/host.h	Fri Apr 10 23:16:44 2015 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,43 +0,0 @@
-//void trf797xStopDecoders(void);
-//void trf797xRunDecoders(void);
-//void trf797xTxNextSlot(void);
-//void trf797xtDisableSlotCounter(void);
-//void trf797xReset(void);
-//void trf797xTurnRfOn(void);
-//void trf797xTurnRfOff(void);
-//void trf797xWriteIsoControl(uint8_t iso_control);
-//void iso15693FindTag(void);
-//void iso15693Anticollision(uint8_t *mask, uint8_t length);
-//void trf797xInitialSettings(void);
-//uint8_t     irqRegister = 0x01; // Interrupt register
-//volatile uint8_t     irqFlag = 0x00;
-//uint8_t     rxErrorFlag = 0x00;
-//uint8_t     readerMode;         // Determines how interrupts will be handled
-//int16_t     nfc_state;
-//uint8_t     nfc_protocol;
-//uint8_t     active;
-//uint8_t     tagFlag;
-//uint8_t     irqCount = 0;
-
-//uint8_t     debugBuffer[1000];  // Capture data for analysis
-//uint8_t     bufIdx=0;
-//extern uint8_t    turnRFOn[2];  
-//
-//extern uint8_t    testcommand[2];
-//
-//uint8_t     buffer[2];
-//int8_t      rxtxState = 1;      // Transmit/Receive byte count
-//uint8_t     afi = 0;
-//uint8_t     flags = 0;                      // Stores the mask value (used in anticollision)
-//uint8_t     command[2];
-//uint8_t     temp;
-//extern  uint8_t         irqRegister;        // Interrupt register (main.cpp)
-//volatile extern  uint8_t         irqFlag;            // main.cpp
-//extern  uint8_t         rxErrorFlag;        // main.cpp
-//extern  uint8_t         readerMode;         // Determines how interrupts will be handled (main.cpp)
-//extern  int8_t          rxtxState;          // Transmit/Receive byte count (main.cpp)
-//extern  uint8_t         buffer[2];
-//extern  uint8_t tagFlag;
-//extern  DigitalIn   irqPin;
-//extern  uint8_t     debugBuffer[1000];  // Capture data for analysis
-//extern  uint8_t     bufIdx;
\ No newline at end of file
--- a/interruptStuff.cpp	Fri Apr 10 23:16:44 2015 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,436 +0,0 @@
-///* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
-//    Filename:       interruptHandler.cpp
-//    Description:    Handles interrupt from TRF7970
-//    Copyright (C)   2015 Gymtrack, Inc.
-//    Author:         Ron Clough
-//    Date:           2015-03-03
-//    
-//    Changes:
-//    Rev     Date        Who     Details
-//    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-//    0.0     2015-03-03  RWC     Original version.
-//    
-//* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
-//
-//#include    "mbed.h"
-////#include    "main.h"
-//#include    "interruptStuff.h"
-//#include    "readerComm.h"
-//
-//InterruptIn readerInt(p7);              // Interrupt from TRF7970
-////DigitalIn   irqPin(p7, PullDown);
-//extern  DigitalOut  debug1LED;     // "Debug" LED
-//extern  DigitalOut  debug2LED;
-//extern  DigitalOut  heartbeatLED;
-//extern  DigitalOut  testPin;
-//extern  Serial      pc;                 // main.cpp
-//extern  DigitalOut  ISO15693LED;
-//extern  uint8_t irqRegister;    // Interrupt register (main.cpp)
-//extern  uint8_t buf[300];
-//extern  int16_t nfc_state;
-//extern  uint8_t nfc_protocol;
-//extern  uint8_t active;
-//extern  int8_t  rxtxState;
-//extern  uint8_t rxErrorFlag;
-//extern  uint8_t tagFlag;
-//volatile extern  uint8_t irqFlag;
-//extern uint8_t irqCount;
-//
-//extern  uint8_t     buffer;
-//
-//extern  uint8_t     debugBuffer[1000];  // Capture data for analysis
-//extern  uint8_t     bufIdx;
-//
-//void trf797xSetupIrq(void)
-//// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
-////  trf797xSetupIrq()
-////  Description:    Interrupt from TRF7970 appears at pin 7 of 
-////                  on nRF51-DK eval board. trf7970IrqHandler() is called 
-////                  when a rising edge is detected.
-//// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
-//{   
-//    readerInt.rise(&trf797xIrqHandler);    // Interrupt on rising edge, call trf7970IrqHandler()
-//    readerInt.disable_irq();
-//}   // End of trf797xSetupIrq()
-//
-//void trf797xReadIrqStatus(uint8_t *buffer)
-//// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
-////  trf797xReadIrqStatus()
-//// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
-//{
-//    *buffer = IRQ_STATUS;
-//    *(buffer + 1) = IRQ_MASK;
-//    trf797xReadContinuous(buffer, 2);
-//}   // End of trf797xReadIrqStatus()
-//
-//void trf797xResetIrqStatus(void)
-//// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
-////  trf797xResetIrqStatus()
-//// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
-//{ 
-//    uint8_t irq_status[4];
-//    
-//    irq_status[0] = IRQ_STATUS;
-//    irq_status[1] = IRQ_MASK;
-//    trf797xReadContinuous(irq_status, 2);   // Read second register as dummy read
-//}   // End of trf797xResetIrqStatus()
-//
-//
-//
-///*======================================nfc mode SG
-//void trf797xNfcModeIsr(uint8_t *Register)
-//// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
-////  trf797xNfcModeIsr()
-////  bit0 - RF collision avoidance error
-////  bit1 - RF collision avoidance OK
-////  bit2 - Change in RF field level
-////  bit3 - SDD OK
-////  bit4 - Communication error
-////  bit5 - FIFO high/low
-////  bit6 - RX
-////  bit7 - TX
-////
-////  irqRegister is used for signaling to the protocol functions
-////  irqRegister = 0x00 - OK
-////  irqRegister = 0x01 - progress
-////  irqRegister = 0x02 - error
-////  irqRegister = 0xFF - end of RX
-//// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
-//{
-//    if (*Register == BIT0) {
-//        irqRegister = 0x02;
-//    }
-//    else if (*Register == BIT1) {
-//        irqRegister = 0x00;
-//    }
-//    else if (*Register == BIT3) {
-//        irqRegister = 0x00;
-//    }
-//    else if ((*Register  & BIT4) == BIT4) {
-//        *Register = RX_SPECIAL_SETTINGS;        // Check filter and gain
-//        trf797xReadSingle(Register, 1);
-//        trf797xStopDecoders();
-//        irqRegister = 0x02;
-//    }
-//    else if (*Register == BIT7) {              // TX complete
-//        irqRegister = 0x00;
-//        trf797xReset();
-//    }
-//    else if (*Register == 0x40 || *Register == 0x44) {  // RX flag means that EOF has been received and
-//                                                        // the number of unread bytes is in FIFOstatus register
-//        *Register = RX_SPECIAL_SETTINGS;                // Check filter and gain
-//        trf797xReadSingle(Register, 1);
-//        *Register = FIFO_CONTROL;
-//        trf797xReadSingle(Register, 1);
-//        *Register = 0x7F & *Register;
-//        buf[nfc_state] = FIFO;                          // Write the received bytes to the correct place in the buffer
-//        trf797xReadContinuous(&buf[nfc_state], *Register);
-//        nfc_state = nfc_state + *Register;
-//        trf797xReset();                                 // Reset the FIFO after last byte has been read out
-//        irqRegister = 0xFF;                             // Signal to the receive function that these are the last bytes
-//        trf797xResetIrqStatus();
-//    }
-//    else if ((*Register == 0x60) || (*Register == 0x64)) {  // RX and FIFO interrupt
-//        irqRegister = 0x01;
-//    }
-//    else if (*Register == 0x48) {                       // SDD complete
-//        irqRegister = 0x00;
-//    }
-//}   // End of trf797xNfcModeIsr()
-//*/
-//
-///*==============================================================================target SG
-//void trf797xTargetIsr(uint8_t *Register)
-//// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
-////  trf797xTargetIsr()
-//// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
-//{
-//    uint8_t command, i;
-//    
-//    if (*Register == 0x48) {
-//        TargetProtocol:
-//        // Determine the NFC target protocol
-//        command = NFC_TARGET_PROTOCOL;
-//        trf797xReadSingle(&command, 1);         //read the NFCtargetprotocol register
-//        command &= 0x3F;
-//        buf[50] = ISO_CONTROL;
-//        if (command == 0x09) {
-//            nfc_protocol = 0x01;
-//            *Register = FIFO_CONTROL;
-//            trf797xReadSingle(Register, 1);     // Determine the number of bytes left in FIFO
-//            *Register = 0x7F & *Register;
-//            buf[nfc_state] = FIFO;          // Write the received bytes to the correct place in the buffer
-//            trf797xReadContinuous(&buf[nfc_state], *Register);
-//            nfc_state = nfc_state + *Register;
-//            irqRegister = 0xFF;
-//        }
-//        else {
-//            *Register = FIFO_CONTROL;
-//            trf797xReadSingle(Register, 1);     // Determine the number of bytes left in FIFO
-//            *Register = 0x7F & *Register;
-//            buf[nfc_state] = FIFO;          // Write the received bytes to the correct place in the buffer
-//            trf797xReadContinuous(&buf[nfc_state], *Register);
-//            nfc_state = nfc_state + *Register;
-//            irqRegister = 0xFF;
-//            if ((command == 0x01)) {         // 14443A - active
-//                buf[51] = NFC106AC;
-//                trf797xWriteSingle(&buf[50], 2);
-//                nfc_protocol = 0x02;
-//                active = 0xFF;
-//            }
-//            else if (command == 0x05) {     // 14443B - tag emulation
-//                buf[51] = TAG14443B;
-//                trf797xWriteSingle(&buf[50], 2);
-//                nfc_protocol = 0x07;
-//                active = 0x00;
-//            }
-//            else if (command == 0x12) {
-//                if(buf[1] == 0x06) {        // Felica 212 - passive
-//                    buf[51] = NFC212PA;
-//                    trf797xWriteSingle(&buf[50], 2);
-//                    nfc_protocol = 0x03;
-//                    active = 0x00;
-//                }
-//                else {                          // Felica 212 - active
-//                    buf[51] = NFC212AC;
-//                    trf797xWriteSingle(&buf[50], 2);
-//                    nfc_protocol = 0x04;
-//                    active = 0xFF;
-//                }
-//            } // if (command == 0x12)
-//            else if (command == 0x13) {
-//                if(buf[1] == 0x06) {            //Felica 424 - passive
-//                    buf[51] = NFC424PA;
-//                    trf797xWriteSingle(&buf[50], 2);
-//                    nfc_protocol = 0x05;
-//                    active = 0x00;
-//                }
-//                else {                          // Felica 424 - active
-//                    buf[51] = NFC424AC;
-//                    trf797xWriteSingle(&buf[50], 2);
-//                    nfc_protocol = 0x06;
-//                    active = 0xFF;
-//                } // if (buf[1] == 0x06)
-//            } // if (command == 0x13)
-//        } // if (command == 0x09)
-//    } // if (*Register == 0x48)
-//    else if (*Register == 0x60) {
-//        irqRegister = 0x01;
-//        buf[nfc_state] = FIFO;
-//        trf797xReadContinuous(&buf[nfc_state], FIFO_MORE);  // Read FIFO_MORE bytes from FIFO
-//        nfc_state = nfc_state + FIFO_MORE;
-//        trf797xResetIrqStatus();
-//    }
-//    else if ((*Register & BIT3) == BIT3) {     // SDD finished
-//        irqRegister = 0xFF;
-//        for(i = 0; i < 255; i++);
-//        nfc_protocol = 0x01;
-//        trf797xReset();
-//        trf797xStopDecoders();
-//        trf797xRunDecoders();
-//    }
-//    else if (*Register == 0x44) {
-//        trf797xReadIrqStatus(Register);
-//        if (*Register == 0x40) {
-//            goto TargetProtocol;
-//        }
-//        else if (*Register == 0x00) {
-//            goto RFfieldChange;
-//        }
-//    }
-//    else if (*Register == BIT2) {                       // RF field change
-//        RFfieldChange:
-//        trf797xReset();
-//        trf797xStopDecoders();
-//        trf797xRunDecoders();
-//    }
-//    trf797xResetIrqStatus();
-//}   // End of trf797xTargetIsr()
-//*/
-//void trf797xISR(uint8_t *irq_status)
-//// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
-////  trf797xISR()
-////  Description:    The Interrupt Handler determines how the IRQ should
-////                  be handled. The Trf797x IRQ status register is read
-////                  to determine the cause of the IRQ.
-////                  Conditions are checked and appropriate actions taken. 
-//// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
-//{ 
-//
-//debug2LED = LED_ON;
-//printf(" IRQ_STATUS %d \r\n",irq_status[0]);
-//  printf(" IRQ_MASK %d \r\n",irq_status[1]); 
-//    if (*irq_status == 0xA0) {  printf( "BIT5 and BIT7, TX active and only 3 bytes left in FIFO \r\n");              // BIT5 and BIT7, TX active and only 3 bytes left in FIFO
-//        irqRegister = 0x00;
-//    }
-//    else if (*irq_status == BIT7) {  printf( " TX complete \r\n");          // TX complete // always comes here SG 
-//       
-//        irqRegister = 0x00;
-//       trf797xReset();                      // Reset the FIFO after TX
-//   }
-//    else if ((*irq_status & BIT1) == BIT1) {printf( "Collision error \r\n"); // Collision error // comes here when tag is detected SG 
-//       
-//       irqRegister = 0x02;                  // RX complete
-//        trf797xStopDecoders();
-//       trf797xReset();
-//        trf797xResetIrqStatus();
-////        //  IRQ_CLR;
-//    }
-//   else if (*irq_status == BIT6) {   printf( "BIT6 INTERRUPT \r\n");    // RX flag means that EOF has been received // does not come here 3G
-//                                            // and the number of unread bytes is in FIFOstatus register
-//        
-//       if (rxErrorFlag == 0x02) {
-//            irqRegister = 0x02;
-//           return;
-//        }
-//        *irq_status = FIFO_CONTROL;
-//        trf797xReadSingle(irq_status, 1);       // Determine the number of bytes left in FIFO
-//        *irq_status = 0x7F & *irq_status;
-//        buf[rxtxState] = FIFO;              // Write the received bytes to the correct place in the buffer
-//        trf797xReadContinuous(&buf[rxtxState], *irq_status);
-//       rxtxState = rxtxState + *irq_status;
-//        trf797xReset();                     // Reset the FIFO after last byte has been read out
-//        irqRegister = 0xFF;                 // Signal to the receive funnction that these are the last bytes
-//    }
-//   else if (*irq_status == 0x60) { printf( "RX active and 9 bytes already in FIFO \r\n");         // RX active and 9 bytes already in FIFO
-//       
-//        irqRegister = 0x01;
-//        buf[rxtxState] = FIFO;
-//        trf797xReadContinuous(&buf[rxtxState], 86); // Read 86 bytes from FIFO
-//        rxtxState = rxtxState + 86;
-//        if (readerInt) {  //ISO15693LED = LED_ON;                         // If IRQ pin high         // chnaged irqpin to readerInt
-//           trf797xReadIrqStatus(irq_status);
-////            // IRQ_CLR
-//            if (*irq_status == 0x40) {                          // End of recieve
-//                *irq_status = FIFO_CONTROL;
-//                trf797xReadSingle(irq_status, 1);                   // Determine the number of bytes left in FIFO
-//                *irq_status = 0x7F & *irq_status;
-//                buf[rxtxState] = FIFO;                          // Write the received bytes to the correct place in the buffer
-//                trf797xReadContinuous(&buf[rxtxState], *irq_status);
-//                rxtxState = rxtxState + *irq_status;
-//               irqRegister = 0xFF;                             // Signal to the receive funnction that these are the last bytes
-//               trf797xReset();                                 // Reset the FIFO after last byte has been read out
-//            }
-//            else if(*irq_status == 0x50) { printf( "End of receive and error \r\n");                        // End of receive and error
-//                irqRegister = 0x02;
-//           }
-//        }
-//        else {
-//            trf797xReadIrqStatus(irq_status);
-//           if(irq_status[0] == 0x00) {
-//               irqRegister = 0xFF;
-//           }
-//       }
-//    }
-//   else if ((*irq_status & BIT4) == BIT4) {   printf( "BIT4 INTERRUPT \r\n");    // CRC error
-//        if ((*irq_status & BIT5) == BIT5) {  printf( "BIT5 INTERRUPT \r\n");   
-//           irqRegister = 0x01;                 // RX active
-//            rxErrorFlag = 0x02;
-//        }
-//       if ((*irq_status & BIT6) == BIT6) { printf( "BIT4/BIT 6 INTERRUPT \r\n");     // 4 Bit receive
-//            buf[200] = FIFO;                    // Write the received bytes to the correct place in the buffer
-//           trf797xReadContinuous(&buf[200], 1);
-//            trf797xReset();
-//           irqRegister = 0x02;                 // End of RX
-//            rxErrorFlag = 0x02;
-//        }
-//       else
-//       {
-//            irqRegister = 0x02;                 // End of RX
-//        }
-//   }
-//    else if ((*irq_status & BIT2) == BIT2) { printf( "BIT2 INTERRUPT \r\n");  // Byte framing error
-//        if ((*irq_status & BIT5) == BIT5) {
-//            irqRegister = 0x01;                 // RX active
-//            rxErrorFlag = 0x02;
-//        }
-//       else
-//            irqRegister = 0x02;                 // End of RX
-//    }
-//    else if ((*irq_status == BIT0)) {printf( "No response interrupt \r\n");          // No response interrupt
-//        irqRegister = 0x00;
-//    }
-//    else {                                     // Interrupt register not properly set
-//        printf ("Interrupt Error\r\n");
-//
-//        irqRegister = 0x02;
-//        trf797xStopDecoders();                         // Reset the FIFO after TX
-//        trf797xReset();
-//        trf797xResetIrqStatus();
-//        //  Original code clears the IRQ flag here.
-//    }
-//  //  wait_ms(10);
-////debug2LED = LED_OFF;
-////=================================================================================
-//}   // End of trf797xISR()
-//
-//
-////============================interrupt call back test
-//void trf797xIrqHandlerCallback(void)  
-// {  wait_ms(10);
-//    debug1LED = LED_OFF; 
-//    
-//    }
-////==============================SG   
-//
-//
-//
-//
-//
-//void trf797xIrqHandler(void)
-//// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
-////  trf7970IrqHandler()
-////  __interrupt void Trf797xPortB(void) in original source file trf797x.c
-//// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
-//{   //======================works $SG
-//    debug1LED = LED_ON; 
-//   // uint8_t    irqStatus[2], iso_control;
-//    irqFlag = 0x02;
-//  // trf797xIrqHandlerCallback();
-//  //=====================================================================================
-// // do {
-////        
-////       iso_control = ISO_CONTROL;
-////        trf797xReadSingle(&iso_control, 1);
-////       trf797xReadIrqStatus(irqStatus);  //reads IRQ_STATUS register SG
-////       
-////      if (*irqStatus == 0xA0) {                   // TX active and only 3 bytes left in FIFO
-////           break;
-////       }
-////        
-////       if ((iso_control & BIT5) != BIT5) {         // RFID mode
-////          
-////           trf797xISR(irqStatus);}
-//     
-//     //==========================================================================     
-//     //  }
-////       else {                                      // NFC mode
-////          if ((iso_control & BIT2) == BIT2) { 
-////             // Tag emulation
-////               tagFlag = 1;
-////                trf797xNfcModeIsr(irqStatus);
-////                
-////            }
-////            else {
-////               if ((iso_control & BIT4) == BIT4) { // Active mode
-////                   
-////                    trf797xNfcModeIsr(irqStatus);
-////                    
-////              }
-////               else {                             // Passive mode
-////                    trf797xTargetIsr(irqStatus);
-////                    
-////              }
-////           }
-////        }
-//   // } while(readerInt);    // While IRQ pin is high ********** chnaged irq to readerInt
-//   
-// // wait_ms(10);
-// debug1LED = LED_OFF;
-// 
-//}
-//    
-// 
-//    
-//    
-//   
--- a/interruptStuff.h	Fri Apr 10 23:16:44 2015 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,34 +0,0 @@
-/*
-    Header file for interruptHandler.cpp
-*/
-
-//  Prototypes
-void trf797xSetupIrq(void);
-void trf797xReadIrqStatus(uint8_t *buffer);
-void trf797xResetIrqStatus(void);
-void trf797xNfcModeIsr(uint8_t *Register);
-void trf797xTargetIsr(uint8_t *Register);
-void trf797xISR(uint8_t *irq_status);
-void trf797xIrqHandler(void);
-
-//  NFC and tag emulation settings
-#define NFC106AC    0x29
-#define NFC212AC    0x2A
-#define NFC424AC    0x2B
-#define NFC106PA    0x21
-#define NFC212PA    0x22
-#define NFC424PA    0x23
-
-#define TAG14443A   0x24
-#define TAG14443B   0x25
-#define TAG15693    0x26
-#define TAGFelica   0x27
-
-#define TAG106      0x00
-#define TAG212      0x21
-#define TAG424      0x42
-#define TAG848      0x63
-
-#define FIFO_LEN    32
-#define FIFO_MORE   26
-
--- a/main.cpp	Fri Apr 10 23:16:44 2015 +0000
+++ b/main.cpp	Thu Apr 30 23:30:55 2015 +0000
@@ -1,18 +1,18 @@
 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
     Filename:       main.cpp
-    Description:    Interface nRF51-DK eval board to TRF7970 eval board 
-                    to test the suitability of the TRF7970 NFC chip 
+    Description:    Interface nRF51-DK eval board to TRF7970 eval board
+                    to test the suitability of the TRF7970 NFC chip
                     for use in Gymtrack products.
                     The nRF51-DK board has an nRF51422 MCU.
     Copyright (C)   2015 Gymtrack, Inc.
     Author:         Ron Clough
     Date:           2015-02-26
-    
+
     Changes:
     Rev     Date        Who     Details
     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     0.0     2015-02-26  RWC     Original version.
-    
+
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
 
 #include    "mbed.h"
@@ -22,73 +22,137 @@
 #include "BLEDevice.h"
 #include "HeartRateService.h"
 #include "DeviceInformationService.h"
-SPI         spi(p25, p28, p29); // MOSI, MISO, SCLK
-           // Slave Select (SS)
-Serial      pc(USBTX, USBRX);   // Serial communication over USB with PC
-DigitalOut  heartbeatLED(LED4); // "Heartbeat" LED
-DigitalOut  debug2LED(LED2);    // "Debug2" LED
-DigitalOut  ISO15693LED(LED3);  // "Detected ISO15693 tag" LED
-DigitalOut  debug1LED(LED1);    // "Debug1" LED
+#include "GT_Timer.h"
+#include "CompareTime.h"
+SPI         spi(p7, p8, p3); // MOSI, MISO, SCLK
+// Slave Select (SS)
+#define BLED_PIN p17
+#define RLED_PIN p19
+#define GLED_PIN p18
+DigitalOut BLED(BLED_PIN);
+DigitalOut RLED(RLED_PIN);
+DigitalOut GLED(GLED_PIN);
+
+Serial      pc(p24, p25);   // Serial communication over USB with PC
+//DigitalOut  heartbeatLED(LED4); // "Heartbeat" LED
+//DigitalOut  debug2LED(LED2);    // "Debug2" LED
+//DigitalOut  ISO15693LED(LED3);  // "Detected ISO15693 tag" LED
+//DigitalOut  debug1LED(LED1);    // "Debug1" LED
 extern DigitalOut  EN;           // Control EN pin on TRF7970
 extern DigitalOut  EN2;        // Control EN2 pin on TRF7970
-extern DigitalOut  CS; 
-
+extern DigitalOut  CS;
+extern DigitalOut AO_7970;
+extern DigitalOut MOD_7970;
 //DigitalInOut  ook_ask(p6);      // Control ASK/OOK pin on TRF7970
 //DigitalOut  mod(p5);            // Control MOD pin on TRF7970
-InterruptIn     readerInt(p7); 
-DigitalOut  testPin(p1);
+InterruptIn     readerInt(p28);
+//DigitalOut  testPin(p1);
 BLEDevice  ble;
 extern uint8_t     buf[300];
 uint8_t noBytes=0;
-extern uint8_t    turnRFOn[2]; 
-extern uint8_t    testcommand[2]; 
+extern uint8_t    turnRFOn[2];
+extern uint8_t    testcommand[2];
 extern uint8_t     found;
-
+extern int nfc;
 bool tagFound=0;
 //uint8_t     WAIT=0;
-
+int next_reading_tag=0;
 int main()
-{testPin=0;
-pc.baud(115200);
+{
+    //testPin=0;
+    pc.baud(115200);
+    AO_7970= 0;
+    MOD_7970=0;
 //=============================================================ble test
- ble.init();
-// ble.onDisconnection(disconnectionCallback);
- //
-//    /* Setup advertising. */
-    ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE);
-//  //  ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t *)uuid16_list, sizeof(uuid16_list));
-    ble.accumulateAdvertisingPayload(GapAdvertisingData::GENERIC_HEART_RATE_SENSOR);
-//   // ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME));
-    ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
-    ble.setAdvertisingInterval(1000);
-    ble.startAdvertising();
+// ble.init();
+//// ble.onDisconnection(disconnectionCallback);
+// //
+////    /* Setup advertising. */
+//    ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE);
+////  //  ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t *)uuid16_list, sizeof(uuid16_list));
+//    ble.accumulateAdvertisingPayload(GapAdvertisingData::GENERIC_HEART_RATE_SENSOR);
+////   // ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME));
+//    ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
+//    ble.setAdvertisingInterval(1000);
+//    ble.startAdvertising();
 //===============================================================*/
 
-readerInt.rise(&handlerNFC);
-CS=1;
-SpiInit(); //spi=1Mhz
-PowerUpNFC();
-NFCInit();
-RegisterReInitNFC();
-RegistersReadNFC();
-PowerDownNFC();                      //SleepNFC() //StandByNFC()
+    readerInt.rise(&handlerNFC);
+    CS=1;
+    SpiInit(); //spi=1Mhz
+    PowerUpNFC();
+    NFCInit();
+    RegisterReInitNFC();
+    RegistersReadNFC();
+//PowerDownNFC();                      //SleepNFC() //StandByNFC()
 //SleepNFC();
+    BLED=0;
+    /** Print the text
+            *
+            * @groups PollNFC
+            *
+            *   1 on success,
+            *   0 on serial error
+            */
+
+    while(1) {
+      //  PowerUpNFC();
+     // NFCInit();
+//        RegisterReInitNFC();
+        
+
+        while(tagFound==0) {
+          //  printf("tag nt found \r\n");
+            //BLED=1;
+//testcommand[0] = 0x17;
+//trf797xDirectCommand(testcommand);
+//testcommand[0] = 0x18;
+//trf797xDirectCommand(testcommand);
+//wait_ms(5);
+//testcommand[0] = RSSI_LEVELS;            //Read RSSI levels and oscillator status(0x0F/0x4F)
+//trf797xReadSingle(testcommand, 1);
+//printf("RSSI:%X \r\n", testcommand[0]);
+//tagFound=PollNFC();
 
-while(1)
-{ 
-tagFound=PollNFC();
+            tagFound=PollNFC();
+           // RegisterReInitNFC();
+//wait_ms(5);
+            //int tag= CompareTime( gt_read_ms(),next_reading_tag,50);
+//
+//            if (tag<0) {
+//                ;
+//            } else {
+//                    RLED=1;
+//                    wait_ms(5);
+//                    RLED=0;
+//                next_reading_tag = gt_read_ms();
+             //   printf("time out : no tag \r\n");
+//                break;
+//
+//            }
+        }
+//
+        if(tagFound==1) {nfc=0;
+            //printf("read single block:");
+            BLED=1;
+            for(uint8_t i=0; i<noBytes; i++)
+                printf("%X ", buf[i]);
+            printf("\r\n");
+            printf("tag found \r\n");
+            BLED=0;
+            tagFound=0;
+//////
+//////// //break;
+        } //else {
+//            printf("tag nt found \r\n");
+//        }
+//////
 
-if(tagFound==1)
-{//printf("read single block:");
- for(uint8_t i=0; i<noBytes; i++)
- printf("%X ", buf[i]);
- printf("\r\n");
- printf("tag found \r\n");
- break;
- }
- else wait(1);
-} 
- 
+        
+       PowerDownNFC();
+     //   wait(2);
+    }
+
 }
 
 
--- a/nRF51822.lib	Fri Apr 10 23:16:44 2015 +0000
+++ b/nRF51822.lib	Thu Apr 30 23:30:55 2015 +0000
@@ -1,1 +1,1 @@
-http://developer.mbed.org/users/soumi_ghsoh/code/nRF51822/#030804500597
+http://developer.mbed.org/users/soumi_ghsoh/code/nRF51822/#3ae49ec9e716
--- a/readerComm.cpp	Fri Apr 10 23:16:44 2015 +0000
+++ b/readerComm.cpp	Thu Apr 30 23:30:55 2015 +0000
@@ -6,12 +6,12 @@
     Copyright (C)   2015 Gymtrack, Inc.
     Author:         Ron Clough
     Date:           2015-02-27
-    
+
     Changes:
     Rev     Date        Who     Details
     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     0.0     2015-02-27  RWC     Original version.
-    
+
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
 /*==========================================================================
 Initialize the chipset ISO15693 and read UID:
@@ -20,16 +20,16 @@
 2) Write Modulator and SYS_CLK Control Register (0x09) (13.56Mhz SYS_CLK and default Clock 13.56Mhz))
 [0x09 0x31]
 3) Configure Mode ISO Control Register (0x01) to 0x02 (ISO15693 high bit rate, one subcarrier, 1 out of 4)
-[0x01 0x02] 
+[0x01 0x02]
 4) Turn RF ON (Chip Status Control Register (0x00))
 [0x40 r] [0x00 0x20] [0x40 r]
 5) Inventory Command (see Figure 5-20. Inventory Command Sent From MCU to TRF7970A)
-5-1) Send Inventory(8B), Wait 2ms, Read/Clear IRQ Status(0x0C=>0x6C)+dummy read, 
-Read FIFO Status Register(0x1C/0x5C), Read Continuous FIFO from 0x1F to 0x1F+0x0A(0x1F/0x7F), 
-Read/Clear IRQ Status(0x0C=>0x6C)+dummy read, Read FIFO Status Register(0x1C/0x5C), 
+5-1) Send Inventory(8B), Wait 2ms, Read/Clear IRQ Status(0x0C=>0x6C)+dummy read,
+Read FIFO Status Register(0x1C/0x5C), Read Continuous FIFO from 0x1F to 0x1F+0x0A(0x1F/0x7F),
+Read/Clear IRQ Status(0x0C=>0x6C)+dummy read, Read FIFO Status Register(0x1C/0x5C),
 Reset FIFO(0x0F/0x8F), Read RSSI levels and oscillator status(0x0F/0x4F)
 [0x8F 0x91 0x3D 0x00 0x30 0x26 0x01 0x00] %:2 [0x6C r:2] [0x5C r] [0x7F r:10] %:10 [0x6C r:2] [0x5C r] [0x8F] [0x4F r]
-==============================================================================*/   
+==============================================================================*/
 
 
 
@@ -39,23 +39,29 @@
 #include    "mbed.h"
 #include    "readerComm.h"
 uint8_t     sg=0;
-DigitalOut  EN(p4);           // Control EN pin on TRF7970
-DigitalOut  EN2(p3);        // Control EN2 pin on TRF7970
-DigitalOut  CS(p19); 
-uint8_t    turnRFOn[2];  
+DigitalOut  EN(p0);           // Control EN pin on TRF7970
+DigitalOut  EN2(p4);        // Control EN2 pin on TRF7970
+DigitalOut  CS(p9);
+uint8_t    turnRFOn[2];
 uint8_t    testcommand[2];
 extern uint8_t noBytes;
 extern  SPI             spi;                // main.cpp
 extern  Serial          pc;                 // main.cpp
 uint8_t         buf[300];           // main.cpp
 uint8_t     found=0;
+uint8_t rssi_flag=0;
+uint8_t rssi=0;
+extern DigitalOut BLED;
+DigitalOut AO_7970(p29);
+DigitalOut MOD_7970(p30);
+int nfc=0;
 //extern bool tagFound=0;
- 
-extern  DigitalOut  debug1LED;
-extern  DigitalOut  debug2LED;
-extern  DigitalOut  ISO15693LED;
-extern  DigitalOut  heartbeatLED;
-extern  DigitalOut  testPin;
+
+//extern  DigitalOut  debug1LED;
+//extern  DigitalOut  debug2LED;
+//extern  DigitalOut  ISO15693LED;
+//extern  DigitalOut  heartbeatLED;
+//extern  DigitalOut  testPin;
 uint8_t     temp;
 uint8_t     command[2];
 
@@ -84,7 +90,7 @@
 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 {
     uint8_t i=0;
-    
+
     CS = SELECT;
     while(length > 0) {
         *buffer = (0x1F & *buffer);     // Register address
@@ -95,13 +101,13 @@
         }
     }
     CS = DESELECT;
-   
+
 }   // End of trf797xWriteSingle()
 
 void trf797xReadSingle(uint8_t *buffer, uint8_t number)
 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 //  trf797xReadSingle()
-//  Description:    Reads specified reader chip registers and 
+//  Description:    Reads specified reader chip registers and
 //                  writes register contents to *buffer.
 //  Parameters:     *buffer =   addresses of the registers.
 //                  number  =   number of registers.
@@ -130,7 +136,8 @@
 //  Parameters:     *buffer =   address of first register.
 //                  length =    number of registers to read.
 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
-{  //==================tested wrk $sg
+{
+    //==================tested wrk $sg
     CS = SELECT;
     *buffer = (0x60 | *buffer);     // Address, read, continuous
     *buffer = (0x7F & *buffer);     // Register address
@@ -142,7 +149,7 @@
     }
 //    spi.write(0x00); spi.write(0x00);   // 16 clock cycles, see TRF7970A FW Design Hints SLOA159 section 7.3
     CS = DESELECT;
-  
+
     //=====================tested it wrks $sg
 }   // End of trf797xReadContinuous()
 
@@ -183,12 +190,15 @@
 
 
 void PowerUpNFC2(void)
-{//CS = 1;
+{
+    //CS = 1;
 // wait_ms(4);
 // EN = 1;
 // EN2=1;
 
-  CS = 0; EN2 = 0; EN = 0;
+    CS = 0;
+    EN2 = 0;
+    EN = 0;
     wait_ms(2);
     CS = 1;
     wait_ms(3);
@@ -197,313 +207,374 @@
     EN = 1;
 }
 void PowerUpNFC(void)
-{ //CS = 1;
+{
+    //CS = 1;
 // wait_ms(4);
 // EN = 1;
-EN2 = 1;
-wait_ms(1);
-EN=1;
+    EN2 = 1;
+    wait_ms(1);
+    EN=1;
 }
 void PowerDownNFC(void)
-{///CS=1;
-   EN=0;
-   EN2=0;
+{
+    ///CS=1;
+    EN=0;
+    EN2=0;
 //wait_ms(1);
- //EN2= 0;   PowerDown Mode
- //EN2=1;    SleepMode
- //CS=0;
+//EN2= 0;   PowerDown Mode
+//EN2=1;    SleepMode
+//CS=0;
 }
 void SleepNFC(void)
-{ EN=0;
-   EN2=0;
-    }
+{
+    EN=0;
+    EN2=0;
+}
 void StandByNFC(void)
 {
     turnRFOn[0] = CHIP_STATUS_CONTROL;
-turnRFOn[1] = CHIP_STATUS_CONTROL;
+    turnRFOn[1] = CHIP_STATUS_CONTROL;
 
-turnRFOn[1] &= 0x3F;
-turnRFOn[1] |= 0x80;
-trf797xWriteSingle(turnRFOn, 2);
+    turnRFOn[1] &= 0x3F;
+    turnRFOn[1] |= 0x80;
+    trf797xWriteSingle(turnRFOn, 2);
 }
 
 void SpiInit1(void)
 {
-spi.format(8, 1);       // 8 bit data, mode = 1 (transition on rising edge, sample on falling edge)
-spi.frequency(250000);  
+    spi.format(8, 1);       // 8 bit data, mode = 1 (transition on rising edge, sample on falling edge)
+    spi.frequency(250000);
 }
 
 void SpiInit(void)
 {
-spi.format(8, 1);       // 8 bit data, mode = 1 (transition on rising edge, sample on falling edge)
-spi.frequency(1000000);  
+    spi.format(8, 1);       // 8 bit data, mode = 1 (transition on rising edge, sample on falling edge)
+    spi.frequency(1000000);
 }
 
 void NFCInit(void)
-{testPin=1;
+{
+    //testPin=1;
 //wait_ms(2);
-testcommand[0] = SOFT_INIT;
-trf797xDirectCommand(testcommand); 
-wait_ms(2);
-testcommand[0] = IDLE;
-trf797xDirectCommand(testcommand); 
-wait_ms(2);
-testcommand[0] = MODULATOR_CONTROL;
-testcommand[1] = 0x21;                  // 6.78 MHz, OOK 100%
-trf797xWriteSingle(testcommand, 2);
+
+    testcommand[0] = SOFT_INIT;
+    trf797xDirectCommand(testcommand);
+    wait_ms(2);
+    testcommand[0] = IDLE;
+    trf797xDirectCommand(testcommand);
+    wait_ms(2);
+//testcommand[0] =NFC_TARGET_LEVEL  ;
+//testcommand[1] = 0x00;
+//trf797xWriteSingle(testcommand, 2);
+    testcommand[0] = MODULATOR_CONTROL;
+    testcommand[1] = 0x81;                  // 6.78 MHz, OOK 100%
+    trf797xWriteSingle(testcommand, 2);
 //wait_ms(2);
-testcommand[0] = MODULATOR_CONTROL;             
-trf797xReadSingle(testcommand, 1);
-turnRFOn[0] = CHIP_STATUS_CONTROL;
-turnRFOn[1] = CHIP_STATUS_CONTROL;
-turnRFOn[1] &= 0x3F;
-turnRFOn[1] |= 0x20;
+    testcommand[0] = MODULATOR_CONTROL;
+    trf797xReadSingle(testcommand, 1);
+    testcommand[0] = REGULATOR_CONTROL ;
+    testcommand[1] = 0x07;
+    trf797xWriteSingle(testcommand, 2);
+    turnRFOn[0] = CHIP_STATUS_CONTROL;
+    turnRFOn[1] = CHIP_STATUS_CONTROL;
+    turnRFOn[1] &= 0x3F;
+    turnRFOn[1] |= 0x00;
 
 // Oroiginal code has 0x20 !!!
-trf797xReadSingle(turnRFOn, 1);
-turnRFOn[0] = CHIP_STATUS_CONTROL;
-turnRFOn[1] = CHIP_STATUS_CONTROL;
-turnRFOn[1] &= 0x3F;
-turnRFOn[1] |= 0x20;
-trf797xWriteSingle(turnRFOn, 2);
+    trf797xReadSingle(turnRFOn, 1);
+    turnRFOn[0] = CHIP_STATUS_CONTROL;
+    turnRFOn[1] = CHIP_STATUS_CONTROL;
+    turnRFOn[1] &= 0x3F;
+    turnRFOn[1] |= 0x00;
+    trf797xWriteSingle(turnRFOn, 2);
 //wait_ms(2);
- 
-testcommand[0] = ISO_CONTROL;
-testcommand[1] = 0x02;                  // 6.78 MHz, OOK 100%
-trf797xWriteSingle(testcommand, 2);
-testcommand[0] = IRQ_MASK; 
-testcommand[1] = 0x3F;                 
-trf797xWriteSingle(testcommand, 2);
-//wait_ms(6); 
-testPin=0;}
+
+    trf797xWriteSingle(testcommand, 2);
+    testcommand[0] = ISO_CONTROL;
+    testcommand[1] = 0x02;                  // 6.78 MHz, OOK 100%
+    trf797xWriteSingle(testcommand, 2);
+    testcommand[0] = IRQ_MASK;
+    testcommand[1] = 0x3F;
+    trf797xWriteSingle(testcommand, 2);
+//wait_ms(6);
+//testPin=0;
+}
 
 void RegisterReInitNFC(void)
-{testcommand[0] = TX_TIMER_EPC_HIGH;
-testcommand[1] = 0xC1;                  
-trf797xWriteSingle(testcommand, 2);
-testcommand[0] = TX_TIMER_EPC_LOW ;
-testcommand[1] = 0xC1;                  
-trf797xWriteSingle(testcommand, 2);
-testcommand[0] = TX_PULSE_LENGTH_CONTROL ;
-testcommand[1] = 0x00;                  
-trf797xWriteSingle(testcommand, 2);
-testcommand[0] = RX_NO_RESPONSE_WAIT_TIME  ;
-testcommand[1] = 0x30;                  
-trf797xWriteSingle(testcommand, 2);
-testcommand[0] =  RX_WAIT_TIME ;
-testcommand[1] = 0x1F;                  
-trf797xWriteSingle(testcommand, 2);
-testcommand[0] = MODULATOR_CONTROL ;
-testcommand[1] = 0x21;     //0x34 100%ook@13MHz             
-trf797xWriteSingle(testcommand, 2);
-testcommand[0] = RX_SPECIAL_SETTINGS ;
-testcommand[1] = 0x40;                  
-trf797xWriteSingle(testcommand, 2);
-testcommand[0] = REGULATOR_CONTROL ;
-testcommand[1] = 0x87;                  
-trf797xWriteSingle(testcommand, 2);
+{
+    //testcommand[0] =NFC_TARGET_LEVEL  ;
+//testcommand[1] = 0x00;
+//trf797xWriteSingle(testcommand, 2);
+    testcommand[0] = TX_TIMER_EPC_HIGH;
+    testcommand[1] = 0xC1;
+    trf797xWriteSingle(testcommand, 2);
+    testcommand[0] = TX_TIMER_EPC_LOW ;
+    testcommand[1] = 0xC1;
+    trf797xWriteSingle(testcommand, 2);
+    testcommand[0] = TX_PULSE_LENGTH_CONTROL ;
+    testcommand[1] = 0x00;
+    trf797xWriteSingle(testcommand, 2);
+    testcommand[0] = RX_NO_RESPONSE_WAIT_TIME  ;
+    testcommand[1] = 0x30;
+    trf797xWriteSingle(testcommand, 2);
+    testcommand[0] =  RX_WAIT_TIME ;
+    testcommand[1] = 0x1F;
+    trf797xWriteSingle(testcommand, 2);
+    testcommand[0] = MODULATOR_CONTROL ;
+    testcommand[1] = 0x81;     //0x34 100%ook@13MHz
+    trf797xWriteSingle(testcommand, 2);
+    testcommand[0] = RX_SPECIAL_SETTINGS ;
+    testcommand[1] = 0x40;
+    trf797xWriteSingle(testcommand, 2);
+    testcommand[0] = REGULATOR_CONTROL ;
+    testcommand[1] = 0x07;
+    trf797xWriteSingle(testcommand, 2);
 }
 
 
 void RegistersReadNFC(void)
-{turnRFOn[0] = CHIP_STATUS_CONTROL;
-trf797xReadSingle(turnRFOn, 1);
-testcommand[0] = ISO_CONTROL;
-trf797xReadSingle(testcommand, 1);
-testcommand[0] = TX_TIMER_EPC_HIGH;          //0xC1; 
-trf797xReadSingle(testcommand, 1);     
-testcommand[0] = TX_TIMER_EPC_LOW ;          //0xC1; 
-trf797xReadSingle(testcommand, 1);     
-testcommand[0] = TX_PULSE_LENGTH_CONTROL ;   //0x00; 
-trf797xReadSingle(testcommand, 1);  
-testcommand[0] = RX_NO_RESPONSE_WAIT_TIME  ; //0x30;
-trf797xReadSingle(testcommand, 1);              
-testcommand[0] =  RX_WAIT_TIME ;             //0x1F;
-trf797xReadSingle(testcommand, 1);  
-testcommand[0] = MODULATOR_CONTROL ;         //0x21; 
-trf797xReadSingle(testcommand, 1);    
-testcommand[0] = RX_SPECIAL_SETTINGS ;       //0x40;
-trf797xReadSingle(testcommand, 1);
-testcommand[0] = REGULATOR_CONTROL ;        //0x87; 
-trf797xReadSingle(testcommand, 1);
+{
+    turnRFOn[0] = CHIP_STATUS_CONTROL;
+    trf797xReadSingle(turnRFOn, 1);
+    testcommand[0] = ISO_CONTROL;
+    trf797xReadSingle(testcommand, 1);
+    testcommand[0] = TX_TIMER_EPC_HIGH;          //0xC1;
+    trf797xReadSingle(testcommand, 1);
+    testcommand[0] = TX_TIMER_EPC_LOW ;          //0xC1;
+    trf797xReadSingle(testcommand, 1);
+    testcommand[0] = TX_PULSE_LENGTH_CONTROL ;   //0x00;
+    trf797xReadSingle(testcommand, 1);
+    testcommand[0] = RX_NO_RESPONSE_WAIT_TIME  ; //0x30;
+    trf797xReadSingle(testcommand, 1);
+    testcommand[0] =  RX_WAIT_TIME ;             //0x1F;
+    trf797xReadSingle(testcommand, 1);
+    testcommand[0] = MODULATOR_CONTROL ;         //0x21;
+    trf797xReadSingle(testcommand, 1);
+    testcommand[0] = RX_SPECIAL_SETTINGS ;       //0x40;
+    trf797xReadSingle(testcommand, 1);
+    testcommand[0] = REGULATOR_CONTROL ;        //0x87;
+    trf797xReadSingle(testcommand, 1);
 }
 
 void InventoryReqNFC(void)
 {
 //send inventory command==================================================
-buf[0]=0x8F; //Send Inventory(8B)[0x8F 0x91 0x3D 0x00 0x30 0x26 0x01 0x00] 
-buf[1]=0x91; 
-buf[2]=0x3D;
-buf[3]=0x00;
-buf[4]=0x30;
-buf[5]=0x26;
-buf[6]=0x01;
-buf[7]=0x00;
-trf797xRawWrite(&buf[0],8); 
-//wait_ms(2); 
-testcommand[0] = IRQ_STATUS;                  
-trf797xReadSingle(testcommand,1); 
-testcommand[0] = IRQ_STATUS;                  
-trf797xReadSingle(testcommand,1);  
-wait_ms(5);
-/*====================================read tag ID
-testcommand[0] = IRQ_STATUS;                  
-trf797xReadSingle(testcommand,1);
-testcommand[0] = IRQ_STATUS;                  
-trf797xReadSingle(testcommand,1); 
-testcommand[0] = FIFO_COUNTER;           //Read FIFO Status Register(0x1C/0x5C)    
-trf797xReadSingle(testcommand, 1);
-testcommand[0] = 0x7F & testcommand[0];   // Determine the number of bytes left in FIFO
-buf[0] = FIFO;   
-trf797xReadContinuous(&buf[0], testcommand[0]);
-testcommand[0] = RSSI_LEVELS;            //Read RSSI levels and oscillator status(0x0F/0x4F)           
-trf797xReadSingle(testcommand, 1); 
-testcommand[0] = IRQ_STATUS;                  
-trf797xReadSingle(testcommand,1); 
-testcommand[0] = IRQ_STATUS;                  
-trf797xReadSingle(testcommand,1);  
-testcommand[0] = RESET;                  //Reset FIFO(0x0F/0x8F)
-trf797xDirectCommand(testcommand);  
-trf797xStopDecoders();
-trf797xRunDecoders();  */
+    buf[0]=0x8F; //Send Inventory(8B)[0x8F 0x91 0x3D 0x00 0x30 0x26 0x01 0x00]
+    buf[1]=0x91;
+    buf[2]=0x3D;
+    buf[3]=0x00;
+    buf[4]=0x30;
+    buf[5]=0x26;
+    buf[6]=0x01;
+    buf[7]=0x00;
+    trf797xRawWrite(&buf[0],8);
+//read rssi register and interchange rx input to main and aux receiver blocks
+//{add code block here
+//    }
+    wait_ms(2);
+    testcommand[0] = IRQ_STATUS;
+    trf797xReadSingle(testcommand,1);
+    testcommand[0] = IRQ_STATUS;
+    trf797xReadSingle(testcommand,1);
+    wait_ms(5);
+//testcommand[0] = CHECK_EXTERNAL_RF;
+//trf797xDirectCommand(testcommand);
+//wait_ms(1);
+    testcommand[0] = RSSI_LEVELS;            //Read RSSI levels and oscillator status(0x0F/0x4F)
+    trf797xReadSingle(testcommand, 1);
+    printf("RSSI:%X \r\n", testcommand[0]);
+    /*====================================read tag ID
+    testcommand[0] = IRQ_STATUS;
+    trf797xReadSingle(testcommand,1);
+    testcommand[0] = IRQ_STATUS;
+    trf797xReadSingle(testcommand,1);
+    testcommand[0] = FIFO_COUNTER;           //Read FIFO Status Register(0x1C/0x5C)
+    trf797xReadSingle(testcommand, 1);
+    testcommand[0] = 0x7F & testcommand[0];   // Determine the number of bytes left in FIFO
+    buf[0] = FIFO;
+    trf797xReadContinuous(&buf[0], testcommand[0]);
+    testcommand[0] = RSSI_LEVELS;            //Read RSSI levels and oscillator status(0x0F/0x4F)
+    trf797xReadSingle(testcommand, 1);
+    testcommand[0] = IRQ_STATUS;
+    trf797xReadSingle(testcommand,1);
+    testcommand[0] = IRQ_STATUS;
+    trf797xReadSingle(testcommand,1);
+    testcommand[0] = RESET;                  //Reset FIFO(0x0F/0x8F)
+    trf797xDirectCommand(testcommand);
+    trf797xStopDecoders();
+    trf797xRunDecoders();  */
 //=====================================read Tag ID
 //wait(1);
 }
 
 void FindNFC(uint8_t *irqStatus)
-{//found=0; 
+{
+    //found=0;
 //static uint8_t sg;
-//printf("%X \r\n", *irqStatus); 
- 
-  switch(*irqStatus)
-  {case BIT0: found=BIT0; WAIT=10; break;
-   case BIT1: found=BIT1; break;
-  // case BIT2://found=BIT2; // break;
-   //case BIT3:found=BIT3; WAIT=0;break;
+//printf("%X \r\n", *irqStatus);
+
+    switch(*irqStatus) {
+        case BIT0:
+            found=BIT0;
+            WAIT=10;
+            break;
+        case BIT1:
+            found=BIT1;
+            break;
+            // case BIT2://found=BIT2; // break;
+            //case BIT3:found=BIT3; WAIT=0;break;
 //   case BIT4:found=BIT4; WAIT=0;break;
 //   case BIT5:found=BIT5; WAIT=0;break;
-   case 0x40: found=0x40; break;
-   case BIT7: found=BIT7; break;
-   default: found=0;
-}  
+        case 0x40:
+            found=0x40;
+            break;
+        case BIT7:
+            found=BIT7;
+            break;
+        default:
+            found=0;
+    }
 
 
-}  
+}
 void handlerNFC(void)
 {
-testcommand[0] = IRQ_STATUS;                  
-trf797xReadSingle(testcommand,1);
-FindNFC(testcommand);
+    testcommand[0] = IRQ_STATUS;
+    trf797xReadSingle(testcommand,1);
+    FindNFC(testcommand);
 //wait_ms(1);
 //=============================use if trf7970a irq_status is not cleared
-//testcommand[0] = IRQ_STATUS;                  
-//trf797xReadSingle(testcommand,1); 
+//testcommand[0] = IRQ_STATUS;
+//trf797xReadSingle(testcommand,1);
 //=============================
 }
 
 
 void MemReadReqNFC(void)
-{testcommand[0] = RX_NO_RESPONSE_WAIT_TIME  ;
-testcommand[1] = 0xFF;  
-trf797xWriteSingle(testcommand, 2);
- //send inventory command==================================================
-buf[0]=0x8F; //Send Inventory(8B)[0x8F 0x91 0x3D 0x00 0x30 0x26 0x01 0x00] 
-buf[1]=0x91; 
-buf[2]=0x3D;
-buf[3]=0x00;
-buf[4]=0x30;
-buf[5]=0x02;
-buf[6]=0x20;
-buf[7]=0x00;
-trf797xRawWrite(&buf[0],8); 
-//wait_ms(2); 
+{
+    testcommand[0] = RX_NO_RESPONSE_WAIT_TIME  ;
+    testcommand[1] = 0xFF;
+    trf797xWriteSingle(testcommand, 2);
+//send inventory command==================================================
+    buf[0]=0x8F; //Send Inventory(8B)[0x8F 0x91 0x3D 0x00 0x30 0x26 0x01 0x00]
+    buf[1]=0x91;
+    buf[2]=0x3D;
+    buf[3]=0x00;
+    buf[4]=0x30;
+    buf[5]=0x02;
+    buf[6]=0x20;
+    buf[7]=0x00;
+    trf797xRawWrite(&buf[0],8);
+
+//read rssi register and interchange rx input to main and aux receiver blocks
+//{add code block here
+//    }
+//wait_ms(2);
 //testPin=1;
 //=========================use to clear irq_status register of trf7970a
-//testcommand[0] = IRQ_STATUS;                  
-//trf797xReadSingle(testcommand,1); 
-//testcommand[0] = IRQ_STATUS;                  
-//trf797xReadSingle(testcommand,1);  
+//testcommand[0] = IRQ_STATUS;
+//trf797xReadSingle(testcommand,1);
+//testcommand[0] = IRQ_STATUS;
+//trf797xReadSingle(testcommand,1);
 //==========================
-wait_ms(5);
+    wait_ms(5);
+//testcommand[0] = CHECK_EXTERNAL_RF;
+//trf797xDirectCommand(testcommand);
+//wait_us(100);
+    testcommand[0] = RSSI_LEVELS;
+    //Read RSSI levels and oscillator status(0x0F/0x4F)
+    trf797xReadSingle(testcommand, 1);
+    rssi=testcommand[0];
+    
+    printf("RSSI:%X \r\n", testcommand[0]);
 }
-void ReadNFC(void) 
-{/*========================================== read irqstatus reg. of trf7970a
-//testcommand[0] = IRQ_STATUS;                clear irqstatus reg of trf7970a  
-//trf797xReadSingle(testcommand,1);
-//testcommand[0] = IRQ_STATUS;                  
-//trf797xReadSingle(testcommand,1);         
-===========================================*/
+void ReadNFC(void)
+{
+    /*========================================== read irqstatus reg. of trf7970a
+    //testcommand[0] = IRQ_STATUS;                clear irqstatus reg of trf7970a
+    //trf797xReadSingle(testcommand,1);
+    //testcommand[0] = IRQ_STATUS;
+    //trf797xReadSingle(testcommand,1);
+    ===========================================*/
 
-noBytes = FIFO_COUNTER;           //Read FIFO Status Register(0x1C/0x5C)    
-trf797xReadSingle(&noBytes, 1);
-noBytes = 0x7F & noBytes;   // Determine the number of bytes left in FIFO
-buf[0] = FIFO;   
-trf797xReadContinuous(&buf[0],noBytes);
+    noBytes = FIFO_COUNTER;           //Read FIFO Status Register(0x1C/0x5C)
+    trf797xReadSingle(&noBytes, 1);
+    noBytes = 0x7F & noBytes;   // Determine the number of bytes left in FIFO
+    buf[0] = FIFO;
+    trf797xReadContinuous(&buf[0],noBytes);
 //==use if trf7970a irq_status is not cleared
-//testcommand[0] = IRQ_STATUS;                  
-//trf797xReadSingle(testcommand,1); 
-//testcommand[0] = IRQ_STATUS;                  
-//trf797xReadSingle(testcommand,1);  
+//testcommand[0] = IRQ_STATUS;
+//trf797xReadSingle(testcommand,1);
+//testcommand[0] = IRQ_STATUS;
+//trf797xReadSingle(testcommand,1);
 //=========================================
-testcommand[0] = RESET;                  //Reset FIFO(0x0F/0x8F)
-trf797xDirectCommand(testcommand);  
+    testcommand[0] = RESET;                  //Reset FIFO(0x0F/0x8F)
+    trf797xDirectCommand(testcommand);
 
-//wait(1);   
+//wait(1);
 }
 
-bool PollNFC(void)
-{ 
-CS=1;
-PowerUpNFC();
-//////wait_ms(1);
-NFCInit();
-RegisterReInitNFC();
-MemReadReqNFC(); //InventoryReqNFC()
+
+
+
+
+
+
+
 
-wait_ms(WAIT);
+bool PollNFC(void)
+{
+    printf("PollNFC \r\n");
+//CS=1;
+  //  PowerUpNFC();
+//////wait_ms(1);
+   // NFCInit();
+   // RegisterReInitNFC();
+    MemReadReqNFC();
+    printf("back from MemReadReqNFC \r\n");
+    if (rssi>0x50)
+    {rssi_flag=1;}
+    else{rssi_flag=0;}
+     nfc++;
+//InventoryReqNFC();
+    wait_ms(WAIT);
 
-switch(found)
-{case BIT0:
- printf("no response:");
- printf("%X \r\n",found);
- found=0;
- WAIT=0;
- break;
+    switch(found) {
+        case BIT0:
+            printf("no response:");
+            printf("%X \r\n",found);
+            found=0;
+            WAIT=0;
+            break;
 // //case BIT1:
 // //case BIT2:
 // //case BIT3:
 //// case BIT4:
 //// case BIT5:
- case BIT6:
- ReadNFC();
- PowerDownNFC();                       //SleepNFC() //StandByNFC()
- found=1;
- WAIT=0;
- break;
- case BIT7:   
- printf("tx complete:");
- printf("%X \r\n",found);
- found=0;
- WAIT=0;
-break;
-default: found=0;
-}
-//
-//
-////InventoryReqNFC();
-////if(found==1)
-////{
-////ReadNFC();
-////printf("tag id:");
-////for(uint8_t i=0; i<noBytes; i++)
-////printf("%X ", buf[i]);
-////printf("\r\n");
-////found=0;
-////}
-////else 
-//{printf("tnf2 \r\n");
-//}
+        case BIT6:
+            ReadNFC();
+//PowerDownNFC();                       //SleepNFC() //StandByNFC()
+            found=1;
+            WAIT=0;
+            break;
+        case BIT7:
+            printf("tx complete:");
+            printf("%X \r\n",found);
+            found=0;
+            WAIT=0;
+            break;
+        default:
+            if((nfc>10)&&(rssi_flag=1));
+            {rssi_flag=0;
+             nfc=0;
+                PowerUpNFC();
+                NFCInit();
+                RegisterReInitNFC();
+                printf("resetting 7970 \r\n:");
+                found=0;
+            }
+    }
 
-return found;
+    return found;
 
 }
\ No newline at end of file
--- a/readerComm.h	Fri Apr 10 23:16:44 2015 +0000
+++ b/readerComm.h	Thu Apr 30 23:30:55 2015 +0000
@@ -1,3 +1,33 @@
+
+//#define EN_7970_PIN p0
+//#define DETECT_METAL_PIN p1
+//#define METAL_THRESH_PIN p2
+//#define SPI_CLK_PIN p3
+//#define EN2_7970_PIN p4
+//#define VUSBMON_PIN p5
+//#define VBATMON_PIN p6
+//#define SPI_MOSI_PIN p7
+//#define SPI_MISO_PIN p8
+//#define CS_7970_PIN p9
+//#define TP1_PIN p10 //use this one for the serial port TX
+//#define WD_CONTROL_PIN p11
+//#define WATCHDOG_PIN p12
+//#define CHARGING_PIN p13
+//#define SCL_PIN p14
+//#define SDA_PIN p15
+//#define ADXL345_INT_PIN p16
+//#define BLED_PIN p17
+//#define GLED_PIN p18
+//#define RLED_PIN p19
+//#define UNUSED_20_PIN p20
+//#define DETECT_METAL_INT_PIN p21
+//#define COILPWR_PIN p22
+//#define UNUSED_23_PIN p23
+//#define TP4_PIN p24
+//#define TP5_PIN p25
+//#define IRQ_7970_PIN p28
+//#define AO_7970_PIN p29
+//#define MOD_7970_PIN p30
 
 //  Prototypes
 //void SpiInit1(void);                                               // SPI 250 Khz