interrupt handling

Dependencies:  

Revision:
5:93c612f43ec2
Parent:
4:9ab0d84bbd07
Child:
6:3c510c297e2f
--- a/readerComm.cpp	Fri Mar 20 19:51:01 2015 +0000
+++ b/readerComm.cpp	Wed Apr 01 22:06:40 2015 +0000
@@ -17,15 +17,20 @@
 #include    "mbed.h"
 #include    "readerComm.h"
 #include    "interruptStuff.h"
-#include    "main.h"
-
+//#include    "main.h"
+DigitalOut  enable(p4);         // Control EN pin on TRF7970
+//DigitalOut  enable2(p3);        // Control EN2 pin on TRF7970
+DigitalOut  CS(p19); 
+uint8_t    turnRFOn[2];  
+uint8_t    testcommand[2];
 uint8_t     afi = 0;
 uint8_t     flags = 0;                      // Stores the mask value (used in anticollision)
 uint8_t     command[2];
 uint8_t     temp;
+
 extern  SPI             spi;                // main.cpp
 extern  Serial          pc;                 // main.cpp
-extern  DigitalOut      CS;                 // main.cpp
+//extern  DigitalOut      CS;                 // main.cpp
 extern  InterruptIn     readerInt;          // main.cpp
 extern  int8_t          rxtxState;          // Transmit/Receive byte count (main.cpp)
 extern  uint8_t         buf[300];           // main.cpp
@@ -143,6 +148,7 @@
         }
     }
     CS = DESELECT;
+   
 }   // End of trf797xWriteSingle()
 
 void trf797xReadSingle(uint8_t *buffer, uint8_t number)
@@ -177,7 +183,7 @@
 //  Parameters:     *buffer =   address of first register.
 //                  length =    number of registers to read.
 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
-{
+{  //==================tested wrk $sg
     CS = SELECT;
     *buffer = (0x60 | *buffer);     // Address, read, continuous
     *buffer = (0x7F & *buffer);     // Register address
@@ -189,6 +195,8 @@
     }
 //    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()
 
 void trf797xRawWrite(uint8_t *buffer, uint8_t length)
@@ -311,9 +319,15 @@
 //                  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)
@@ -336,9 +350,13 @@
     flags = SIXTEEN_SLOTS;                  // SIXTEEN_SLOTS = 0x06
 //    flags = ONE_SLOT;                       // ONE_SLOT = 0x26
     buf[20] = 0x00;
-    iso15693Anticollision(&buf[20], 0x00);  // Send Inventory request
+  //  debug2LED = LED_ON;
+      
+    iso15693Anticollision(&buf[20],0x00);  // Send Inventory request
+    
     trf797xTurnRfOff();
-    trf797xResetIrqStatus();                       // Clear any IRQs
+    trf797xResetIrqStatus();   
+                    // Clear any IRQs
 }   // End of iso15693FindTag()
 
 void iso15693Anticollision(uint8_t *mask, uint8_t length)
@@ -349,7 +367,7 @@
 //  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;
@@ -359,7 +377,7 @@
 //    uint8_t     irqStatus[2], iso_control;
 
     slot_no[0] = 0x00;
-    if ((flags & BIT5) == 0x00) {                       // flags<5> is the number of slots indicator
+    if ((flags & BIT5) == 0x00) {                       // flags<5> is the number of slots testPin
         no_slots = 16;                                  // 16 slots if flags<5> is cleared
     }
     else {
@@ -398,83 +416,60 @@
     
     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
-    irqRegister = 0x01;
-    irqFlag = 0x00;
-    //  ***** Original code starts the counter here *****
     
-    //  Wait for end-of-TX interrupt:
-    //  IRQ_STATUS<7> set at start of TX, but
-    //  IRQ pin doesn't go high until TX ends.
-    
-    while(irqFlag == 0x00) {};
-    
-    // ***** Original code stops the counter here *****
+    //===================================================runs till here !$SG
+    irqRegister = 0x01;
+    irqFlag = 0x00; 
+   
     
-    //  Begin: This code used to be in interruptStuff.cpp/trf7970IrqHandler()
-    /*
-    do {
-        iso_control = ISO_CONTROL;
-        trf797xReadSingle(&iso_control, 1);
-        trf797xReadIrqStatus(irqStatus);
-        
-//        debugBuffer[bufIdx] = irqStatus[1];
-//        bufIdx++;
-//        heartbeatLED = !heartbeatLED;
-        
-        if (*irqStatus == 0xA0) {                   // TX active and only 3 bytes left in FIFO
-            
-            break;
-        }
+ 
+    //
+   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)
+        { ; 
+        }  
         
-        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(irqPin);    // While IRQ pin is high **********
-    */
-//    ISO15693LED = !ISO15693LED;
-    
-    //  End: This code used to be in interruptStuff.cpp/trf7970IrqHandler()
-    
-    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(irqFlag == 0x00);                         // Wait for interrupt
+     //  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) {
-                irqRegister = 0x00;
+           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
-                found = 1;
+            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]);
@@ -482,16 +477,16 @@
                 printf(", %d ]\r\n", command[0]);
             break;
             
-            case 0x02:                  // Collision occurred
+            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:                  // Timer interrupt
-                //  ***** Original code does nothing! *****
+            case 0x00:    // printf("Timer interrupt \r\n");             // Timer interrupt
+               //  ***** Original code does nothing! *****
             break;
             
-            default:
+           default:
                 //  ***** Original code does nothing *****
             break;
         }   // switch (irqRegister)
@@ -504,57 +499,165 @@
             trf797xRunDecoders();
             command[0] = RESET;                 // new
             trf797xDirectCommand(command);      // new
-            trf797xTxNextSlot();   // RESET must precede txNextSlot, see "TRF7970A FW Design Hints SLOA159.
+           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;
+           break;
         }
     }   // for (j = 1; j <= no_slots; j++)
-    
+   //================================================ 
+   //===================================================
     if (found == 1) {
         ISO15693LED = LED_ON;
-    }
-    else {
-        ISO15693LED = LED_OFF;
-    }
-    
+   }
+   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)) {
+   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++)
+       for(i = 0; i < 8; i++)
         {
-            new_mask[i] = *(mask + i);              // First the whole mask is copied
-        }
+           new_mask[i] = *(mask + i);              // First the whole mask is copied
+       }
 
-        if((new_length & BIT2) == 0x00)
+       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[i] = new_mask[i - 1];
+           }
+           new_mask[0] &= 0x00;
+       }
         new_mask[0] |= *p_slot_no;                  // The mask is changed
-        wait_ms(2);
+       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
+       iso15693Anticollision(&new_mask[0], new_length);    // Recursive call with new Mask
 
-        p_slot_no--;
-    }
+       p_slot_no--;
+       
+   }
+    //==================================================
     //  ***** Original code disables IRQ here *****
     //readerInt.disable_irq();
-    
+  //debug2LED = LED_OFF;
 }   // End of iso15693Anticollision()
+/*
+    Header file for main.cpp
+*/
+
+
+void PowerUpNFC(void)
+{// CS = 0; enable2 = 0; enable = 0;
+   CS = 1;
+   wait_ms(8);
+   // 
+   // wait_ms(3);
+   // enable2 = 1;
+   // wait_ms(3);
+    enable = 1;
+  
+}
+
+void SpiInit1(void)
+{
+spi.format(8, 1);       // 8 bit data, mode = 1 (transition on rising edge, sample on falling edge)
+spi.frequency(250000);  
+}
+
+void SpiInit2(void)
+{
+spi.format(8, 1);       // 8 bit data, mode = 1 (transition on rising edge, sample on falling edge)
+spi.frequency(1000000);  
+}
+
+void NFCInit(void)
+{wait_ms(2);
+    //RESET =================================================================
+//testcommand[0]=RESET;
+//trf797xDirectCommand(testcommand); 
+//=======================================================================
+
+
+testcommand[0] = SOFT_INIT;
+trf797xDirectCommand(testcommand); 
+ wait_ms(2);
+testcommand[0] = IDLE;
+trf797xDirectCommand(testcommand); 
+wait_ms(2);
+//testcommand[0] = IDLE;
+//trf797xDirectCommand(testcommand); 
+////Write Modulator and SYS_CLK Control Register (0x09) ====================
+////(13.56Mhz SYS_CLK and default Clock 13.56Mhz))==========================
+testcommand[0] = MODULATOR_CONTROL;
+testcommand[1] = 0x21;                  // 6.78 MHz, OOK 100%
+trf797xWriteSingle(testcommand, 2);
+//////========================================================================
+wait_ms(2);
+
+testcommand[0] = MODULATOR_CONTROL;             
+trf797xReadSingle(testcommand, 1);
+//====================================================================8 clk cycles
+//testcommand[0] = IDLE;
+//trf797xDirectCommand(testcommand); 
+//testcommand[0] = IDLE;
+//trf797xDirectCommand(testcommand);
+//====================================================================
+////Turn RF ON (Chip Status Control Register (0x00))========================
+
+//Configure Mode ISO Control Register (0x01) to 0x02====================== 
+//(ISO15693 high bit rate, one subcarrier, 1 out of 4)====================
+
+//testcommand[0]=0x0F;//reset
+//trf797xDirectCommand(testcommand);
+//====================================================================8 clk cycles
+//testcommand[0] = IDLE;
+//trf797xDirectCommand(testcommand); 
+//testcommand[0] = IDLE;
+//trf797xDirectCommand(testcommand);
+
+//====================================================================
+
+////========================================================================
+}
+
+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] = 0x0F;                  
+trf797xWriteSingle(testcommand, 2);
+testcommand[0] = MODULATOR_CONTROL ;
+testcommand[1] = 0x21;     //0x34 100%ook@13MHz             
+trf797xWriteSingle(testcommand, 2);
+testcommand[0] = RX_SPECIAL_SETTINGS ;
+testcommand[1] = 0x41;                  
+trf797xWriteSingle(testcommand, 2);
+testcommand[0] = REGULATOR_CONTROL ;
+testcommand[1] = 0x87;                  
+trf797xWriteSingle(testcommand, 2);
+}
\ No newline at end of file