interrupt handling

Dependencies:  

Revision:
4:9ab0d84bbd07
Parent:
3:eaae5433ab45
Child:
5:93c612f43ec2
--- a/readerComm.cpp	Thu Mar 12 15:05:36 2015 +0000
+++ b/readerComm.cpp	Fri Mar 20 19:51:01 2015 +0000
@@ -23,7 +23,6 @@
 uint8_t     flags = 0;                      // Stores the mask value (used in anticollision)
 uint8_t     command[2];
 uint8_t     temp;
-extern  DigitalOut      ISO15693LED;        // ISO15693 LED
 extern  SPI             spi;                // main.cpp
 extern  Serial          pc;                 // main.cpp
 extern  DigitalOut      CS;                 // main.cpp
@@ -45,30 +44,73 @@
 
 extern  DigitalOut  testPin;
 
-void initialSettings(void)
+extern  uint8_t     debugBuffer[1000];  // Capture data for analysis
+extern  uint8_t     bufIdx;
+
+void trf797xInitialSettings(void)
 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
-//  initialSettings()
+//  trf797xInitialSettings()
 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 {
     uint8_t mod_control[2];
     
     mod_control[0] = SOFT_INIT;
-    spiDirectCommand(mod_control);
+    trf797xDirectCommand(mod_control);
     
     mod_control[0] = IDLE;
-    spiDirectCommand(mod_control);
+    trf797xDirectCommand(mod_control);
     
     mod_control[0] = MODULATOR_CONTROL;
     mod_control[1] = 0x21;                  // 6.78 MHz, OOK 100%
-    spiWriteSingle(mod_control, 2);
+    trf797xWriteSingle(mod_control, 2);
     
     mod_control[0] = MODULATOR_CONTROL;
-    spiReadSingle(mod_control, 1);
-}   // End of initialSettings()
+    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);
 
-void spiDirectCommand(uint8_t *buffer)
+    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 trf797xDirectCommand(uint8_t *buffer)
 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
-//  spiDirectCommand()
+//  trf797xDirectCommand()
 //  Description:    Transmit a Direct Command to the reader chip.
 //  Parameter:      *buffer =   the direct command.
 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
@@ -77,15 +119,12 @@
     *buffer = (0x9F & *buffer);     // Setup command mode
     CS = SELECT;
     spi.write(*buffer);
-    
-    spi.write(0x00);
-    
     CS = DESELECT;
-}   // End of spiDirectCommand()
+}   // End of trf797xDirectCommand()
 
-void spiWriteSingle(uint8_t *buffer, uint8_t length)
+void trf797xWriteSingle(uint8_t *buffer, uint8_t length)
 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
-//  spiWriteSingle()
+//  trf797xWriteSingle()
 //  Description:    Writes to specified reader registers.
 //  Parameters:     *buffer =   addresses of the registers followed by the
 //                              contents to write.
@@ -104,11 +143,11 @@
         }
     }
     CS = DESELECT;
-}   // End of spiWriteSingle()
+}   // End of trf797xWriteSingle()
 
-void spiReadSingle(uint8_t *buffer, uint8_t number)
+void trf797xReadSingle(uint8_t *buffer, uint8_t number)
 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
-//  spiReadSingle()
+//  trf797xReadSingle()
 //  Description:    Reads specified reader chip registers and 
 //                  writes register contents to *buffer.
 //  Parameters:     *buffer =   addresses of the registers.
@@ -125,14 +164,16 @@
         number--;
     }
     CS = DESELECT;
-}   // End of spiReadSingle()
+}   // End of trf797xReadSingle()
 
-void spiReadContinuous(uint8_t *buffer, uint8_t length)
+void trf797xReadContinuous(uint8_t *buffer, uint8_t length)
 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
-//  spiReadContinuous()
+//  trf797xReadContinuous()
 //  Description:    Used in SPI mode to read a specified number of
 //                  reader chip registers from a specified address upwards.
 //                  Contents of the registers are stored in *buffer.
+//                  1) Read register(s)
+//                  2) Write contents to *buffer
 //  Parameters:     *buffer =   address of first register.
 //                  length =    number of registers to read.
 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
@@ -146,15 +187,13 @@
         buffer++;
         length--;
     }
-    
-    spi.write(0x00); spi.write(0x00);   // 16 clock cycles, see TRF7970A FW Design Hints SLOA159 section 7.3
-    
+//    spi.write(0x00); spi.write(0x00);   // 16 clock cycles, see TRF7970A FW Design Hints SLOA159 section 7.3
     CS = DESELECT;
-}   // End of spiReadContinuous()
+}   // End of trf797xReadContinuous()
 
-void spiRawWrite(uint8_t *buffer, uint8_t length)
+void trf797xRawWrite(uint8_t *buffer, uint8_t length)
 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
-//  rawWrite()
+//  trf797xRawWrite()
 //  Description:    Used in SPI mode to write direct to the reader chip.
 //  Parameters:     *buffer =   raw data
 //                  length =    number of data bytes
@@ -167,84 +206,86 @@
         length--;
     }
     CS = DESELECT;
-}   // End of spiRawWrite()
+}   // End of trf797xRawWrite()
 
-void stopDecoders(void)
+void trf797xStopDecoders(void)
 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
-//  stopDecoders()
+//  trf797xStopDecoders()
 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 {
     command[0] = STOP_DECODERS;
-    spiDirectCommand(command);
+    trf797xDirectCommand(command);
 }
 
-void runDecoders(void)
+void trf797xRunDecoders(void)
 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
-//  runDecoders()
+//  trf797xRunDecoders()
 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 {
     command[0] = RUN_DECODERS;
-    spiDirectCommand(command);
+    trf797xDirectCommand(command);
 }
 
-void txNextSlot(void)
+void trf797xTxNextSlot(void)
 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
-//  txNextSlot()
+//  trf797xTxNextSlot()
 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 {
     command[0] = TRANSMIT_NEXT_SLOT;
-    spiDirectCommand(command);
+    trf797xDirectCommand(command);
 }
 
-void disableSlotCounter(void)
+void trf797xDisableSlotCounter(void)
 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
-//  disableSlotCounter()
+//  trf797xDisableSlotCounter()
 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 {
     buf[40] = IRQ_MASK;
     buf[41] = IRQ_MASK;
-    spiReadSingle(&buf[41], 1);
+    trf797xReadSingle(&buf[41], 1);
     buf[41] &= 0xFE;
-    spiWriteSingle(&buf[40], 2);
+    trf797xWriteSingle(&buf[40], 2);
 }
 
-void trf7970Reset(void)
+void trf797xReset(void)
 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
-//  trf7970Reset()
+//  trf797xReset()
 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 {
     command[0] = RESET;
-    spiDirectCommand(command);
+    trf797xDirectCommand(command);
 }
 
-void turnRfOn(void)
+void trf797xTurnRfOn(void)
 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
-//  turnRfOn()
+//  trf797xTurnRfOn()
 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 {   
     command[0] = CHIP_STATUS_CONTROL;
     command[1] = CHIP_STATUS_CONTROL;
-    spiReadSingle(&command[1], 1);
+    trf797xReadSingle(&command[1], 1);
     command[1] &= 0x3F;
-    command[1] |= 0x20;
-    spiWriteSingle(command, 2);
-}   // End of turnRfOn()
+    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 turnRfOff(void)
+void trf797xTurnRfOff(void)
 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
-//  turnRfOff()
+//  trf797xTurnRfOff()
 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 {
     command[0] = CHIP_STATUS_CONTROL;
     command[1] = CHIP_STATUS_CONTROL;
-    spiReadSingle(&command[1], 1);
+    trf797xReadSingle(&command[1], 1);
     command[1] &= 0x1F;
-    spiWriteSingle(command, 2);
-}   // End of turnRfOff()
+    trf797xWriteSingle(command, 2);
+}   // End of trf797xTurnRfOff()
 
-void writeIsoControl(uint8_t iso_control)
+void trf797xWriteIsoControl(uint8_t iso_control)
 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
-//  writeIsoControl()
+//  trf797xWriteIsoControl()
 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 {
     uint8_t write[4];
@@ -257,9 +298,9 @@
     write[0] = ISO_CONTROL;
     write[1] = iso_control;
     write[1] &= ~BIT5;
-    spiWriteSingle(write, 2);
+    trf797xWriteSingle(write, 2);
     iso_control &= 0x1F;
-}   // End of writeIsoControl()
+}   // End of trf797xWriteIsoControl()
 
 void iso15693FindTag(void)
 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
@@ -271,34 +312,38 @@
 //                  3) Turn RF OFF
 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 {
-    turnRfOn();
-    writeIsoControl(0x02);
+    trf797xTurnRfOn();
+    trf797xWriteIsoControl(0x02);
     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;
-    spiWriteSingle(buffer, 2);
+    trf797xWriteSingle(buffer, 2);
     wait_ms(1);
     buffer[0] = CHECK_INTERNAL_RF;
-    spiDirectCommand(buffer);
+    trf797xDirectCommand(buffer);
     buffer[0] = RSSI_LEVELS;
-    spiReadSingle(buffer, 1);
+    trf797xReadSingle(buffer, 1);
     printf("RSSI: 0x%X\r\n", buffer[0]);
-    //  Result: RSSI = 0x40 (Range is 0x40 to 0x7F)
+    //  Result: RSSI = 0x5C (Range is 0x40 to 0x7F)
     */
+    
     flags = SIXTEEN_SLOTS;                  // SIXTEEN_SLOTS = 0x06
+//    flags = ONE_SLOT;                       // ONE_SLOT = 0x26
     buf[20] = 0x00;
-    Iso15693Anticollision(&buf[20], 0x00);  // Send Inventory request
-    turnRfOff();
-    resetIrqStatus();                       // Clear any IRQs
+    iso15693Anticollision(&buf[20], 0x00);  // Send Inventory request
+    trf797xTurnRfOff();
+    trf797xResetIrqStatus();                       // Clear any IRQs
 }   // End of iso15693FindTag()
 
-void Iso15693Anticollision(uint8_t *mask, uint8_t length)
+void iso15693Anticollision(uint8_t *mask, uint8_t length)
 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
-//  Iso15693Anticollision()
+//  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
@@ -310,8 +355,8 @@
     uint8_t     new_mask[8], new_length, mask_size;
     uint32_t    size;
     uint8_t     fifo_length = 0;
-    uint16_t    k;
-    uint8_t     irqStatus[2], iso_control;
+    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 indicator
@@ -354,25 +399,16 @@
     buf[3] = (char) (size >> 8);
     buf[4] = (char) (size << 4);
 
-    resetIrqStatus();
+    trf797xResetIrqStatus();
     //  ***** Original code sets up a 30 ms counter here *****
     //  ***** Original code enables IRQ here *****
     //  ***** nRF51422 clear IRQ register *****
     readerInt.enable_irq();
-    spiRawWrite(&buf[0], mask_size + fifo_length);      // Write to FIFO
+    trf797xRawWrite(&buf[0], mask_size + fifo_length);      // Write to FIFO
     irqRegister = 0x01;
     irqFlag = 0x00;
     //  ***** Original code starts the counter here *****
     
-    /*
-    //  Continuously read the IRQ STATUS register
-    while(1) {
-        readIrqStatus(irqStatus);
-        printf("IRQ: 0x%X\r\n", irqStatus[1]);
-        wait_ms(100);
-    }
-    */
-    
     //  Wait for end-of-TX interrupt:
     //  IRQ_STATUS<7> set at start of TX, but
     //  IRQ pin doesn't go high until TX ends.
@@ -382,11 +418,15 @@
     // ***** Original code stops the counter here *****
     
     //  Begin: This code used to be in interruptStuff.cpp/trf7970IrqHandler()
-    
+    /*
     do {
         iso_control = ISO_CONTROL;
-        spiReadSingle(&iso_control, 1);
-        readIrqStatus(irqStatus);
+        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
             
@@ -394,23 +434,25 @@
         }
         
         if ((iso_control & BIT5) != BIT5) {         // RFID mode
-            howToHandleTrf7970Irq(irqStatus);
+            trf797xISR(irqStatus);
         }
         else {                                      // NFC mode
             if ((iso_control & BIT2) == BIT2) {     // Tag emulation
                 tagFlag = 1;
-                nfcModeIrq(irqStatus);
+                trf797xNfcModeIsr(irqStatus);
             }
             else {
                 if ((iso_control & BIT4) == BIT4) { // Active mode
-                    nfcModeIrq(irqStatus);
+                    trf797xNfcModeIsr(irqStatus);
                 }
                 else {                              // Passive mode
-                    targetIrq(irqStatus);
+                    trf797xTargetIsr(irqStatus);
                 }
             }
         }
     } while(irqPin);    // While IRQ pin is high **********
+    */
+//    ISO15693LED = !ISO15693LED;
     
     //  End: This code used to be in interruptStuff.cpp/trf7970IrqHandler()
     
@@ -419,7 +461,6 @@
         //  ***** Original code sets up a 20 ms counter and starts it here *****
         irqFlag = 0x00;
         while(irqFlag == 0x00);                         // Wait for interrupt
-        
         //  ***** Original code stops the counter here *****
         while(irqRegister == 0x01) {                    // Wait for RX complete
             k++;
@@ -429,7 +470,7 @@
             }
         }
         command[0] = RSSI_LEVELS;                       // Read RSSI levels
-        spiReadSingle(command, 1);
+        trf797xReadSingle(command, 1);
         
         switch (irqRegister) {
             case 0xFF:                  // If received UID in buffer
@@ -456,16 +497,18 @@
         }   // switch (irqRegister)
         
         command[0] = RESET;             // FIFO must be reset before receiving the next response
-        spiDirectCommand(command);
+        trf797xDirectCommand(command);
         
         if ((no_slots == 16) && (j < 16)) {         // If 16 slots used then send EOF (next slot)
-            stopDecoders();
-            runDecoders();
-            txNextSlot();   // RESET must precede txNextSlot, see "TRF7970A FW Design Hints SLOA159.
+            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
-            stopDecoders();
-            disableSlotCounter();
+            trf797xStopDecoders();
+            trf797xDisableSlotCounter();
         }
         else if (no_slots == 1) {                   // 1 slot is used
             break;
@@ -474,7 +517,6 @@
     
     if (found == 1) {
         ISO15693LED = LED_ON;
-//        printf("Found ISO15693 tag\r\n");
     }
     else {
         ISO15693LED = LED_OFF;
@@ -505,11 +547,14 @@
         new_mask[0] |= *p_slot_no;                  // The mask is changed
         wait_ms(2);
 
-        Iso15693Anticollision(&new_mask[0], new_length);    // Recursive call with new Mask
+
+        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();
     
-}   // End of Iso15693Anticollision()
+}   // End of iso15693Anticollision()