interrupt handling

Dependencies:  

Revision:
5:93c612f43ec2
Parent:
4:9ab0d84bbd07
Child:
6:3c510c297e2f
--- a/main.cpp	Fri Mar 20 19:51:01 2015 +0000
+++ b/main.cpp	Wed Apr 01 22:06:40 2015 +0000
@@ -16,12 +16,13 @@
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
 
 #include    "mbed.h"
-#include    "main.h"
+
+//#include    "NFC_7970.h"
 #include    "readerComm.h"
 #include    "interruptStuff.h"
 
 SPI         spi(p25, p28, p29); // MOSI, MISO, SCLK
-DigitalOut  CS(p19);            // Slave Select (SS)
+           // Slave Select (SS)
 Serial      pc(USBTX, USBRX);   // Serial communication over USB with PC
 DigitalOut  heartbeatLED(LED4); // "Heartbeat" LED
 DigitalOut  debug2LED(LED2);    // "Debug2" LED
@@ -29,8 +30,6 @@
 DigitalOut  debug1LED(LED1);    // "Debug1" LED
 DigitalInOut  ook_ask(p6);      // Control ASK/OOK pin on TRF7970
 DigitalOut  mod(p5);            // Control MOD pin on TRF7970
-DigitalOut  enable(p4);         // Control EN pin on TRF7970
-DigitalOut  enable2(p3);        // Control EN2 pin on TRF7970
 
 DigitalOut  testPin(p1);
 
@@ -49,7 +48,9 @@
 
 uint8_t     debugBuffer[1000];  // Capture data for analysis
 uint8_t     bufIdx=0;
+extern uint8_t    turnRFOn[2];  
 
+extern uint8_t    testcommand[2];
 void blinkHeartbeatLED(void)
 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 //  blinkHeartbeatLED()
@@ -60,20 +61,38 @@
 }   // End of blinkHeartbeatLED()
 
 int main()
-{
+{pc.baud(115200);
+    PowerUpNFC();
     // Power up sequence. See TRF7970A datasheet "SLOU43K August 2011 Revised April 20914" Figures 6-3 and 6-4.
-    CS = 0; enable2 = 0; enable = 0;
-    wait_ms(2);
-    CS = 1;
-    wait_ms(3);
-    enable2 = 1;
-    wait_ms(1);
-    enable = 1;
-    
-    mod = 0;
+ SpiInit1(); 
+ NFCInit();
+ SpiInit2(); 
+ wait_ms(15);
+ 
+turnRFOn[0] = CHIP_STATUS_CONTROL;
+turnRFOn[1] = CHIP_STATUS_CONTROL;
+//
+//trf797xReadSingle(&turnRFOn[1], 1);
+turnRFOn[1] &= 0x3F;
+turnRFOn[1] |= 0x2C;
+// Oroiginal code has 0x20 !!!
+trf797xReadSingle(turnRFOn, 1);
+turnRFOn[0] = CHIP_STATUS_CONTROL;
+turnRFOn[1] = CHIP_STATUS_CONTROL;
+
+turnRFOn[1] &= 0x3F;
+turnRFOn[1] |= 0x2C;
+trf797xWriteSingle(turnRFOn, 2);
+wait_ms(2);
+ 
+testcommand[0] = ISO_CONTROL;
+testcommand[1] = 0x02;                  // 6.78 MHz, OOK 100%
+trf797xWriteSingle(testcommand, 2);
+wait_ms(6); 
+//    mod = 0;                   
     testPin = 0;
     //  Setup serial communication
-    pc.baud(115200);
+    
 //    printf("\r\nInitialization: ");
 
 /*
@@ -86,48 +105,287 @@
     printf("Regulator Control: 0x%X\r\n", buffer[1]);
 */
 
-    
-    //  Setup LEDs
-    heartbeatLED = LED_OFF;
+   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);
-    }
-    */
+
+ook_ask.output();       //  Set On-Off Keying modulation 
+ook_ask = 1;
+
+//******************************************
+
+//testcommand[0] = ISO_CONTROL;
+//testcommand[1] = 0x21;                  
+//trf797xWriteSingle(testcommand, 2);
+//testcommand[0] = IRQ_STATUS;             
+//trf797xReadContinuous(testcommand, 2); 
+//turnRFOn[0] = CHIP_STATUS_CONTROL;
+//turnRFOn[1] = CHIP_STATUS_CONTROL;
+//turnRFOn[1] &= 0x3F;
+//turnRFOn[1] |= 0x21;
+//trf797xWriteSingle(turnRFOn, 2);
+//testcommand[0] = ISO_CONTROL;
+//testcommand[1] = 0x00;                  
+//trf797xWriteSingle(testcommand, 2);
+//testcommand[0] = ISO_CONTROL;
+//testcommand[1] = 0x00;                  
+//trf797xWriteSingle(testcommand, 2);
+//testcommand[0] = IRQ_STATUS;   
+//trf797xReadContinuous(testcommand, 2);  
+//turnRFOn[0] = CHIP_STATUS_CONTROL;        
+//trf797xReadSingle(&turnRFOn[0], 1);
+
+//
+//turnRFOn[0] = CHIP_STATUS_CONTROL; 
+//turnRFOn[1] = CHIP_STATUS_CONTROL; 
+//turnRFOn[1] &= 0x3F;
+//turnRFOn[1] |= 0x21;
+//trf797xWriteSingle(turnRFOn, 2);
+//testcommand[0] = IRQ_STATUS;             
+//trf797xReadContinuous(testcommand, 2); 
+//turnRFOn[0] = CHIP_STATUS_CONTROL;        
+//trf797xReadSingle(&turnRFOn[0], 1);
+//
+//
+//turnRFOn[0] = CHIP_STATUS_CONTROL; 
+//turnRFOn[1] = CHIP_STATUS_CONTROL; 
+//turnRFOn[1] &= 0x3F;
+//turnRFOn[1] |= 0x21;
+//trf797xWriteSingle(turnRFOn, 2);
+//testcommand[0] = IRQ_STATUS;             
+//trf797xReadContinuous(testcommand, 2); 
+//*********************************************
+
+RegisterReInitNFC();
+
+
+testcommand[0] = TX_TIMER_EPC_HIGH;
+//testcommand[1] = 0xC1;                  
+trf797xReadSingle(testcommand, 1);
+testcommand[0] = TX_TIMER_EPC_LOW ;
+//testcommand[1] = 0xC1;                  
+trf797xReadSingle(testcommand, 1);
+testcommand[0] = TX_PULSE_LENGTH_CONTROL ;
+//testcommand[1] = 0x00;                  
+trf797xReadSingle(testcommand, 1);
+testcommand[0] = RX_NO_RESPONSE_WAIT_TIME  ;
+//testcommand[1] = 0x30;                  
+trf797xReadSingle(testcommand, 1);
+testcommand[0] =  RX_WAIT_TIME ;
+//testcommand[1] = 0x1F;                  
+trf797xReadSingle(testcommand, 1);
+testcommand[0] = MODULATOR_CONTROL ;
+//testcommand[1] = 0x21;     //0x34 100%ook@13MHz             
+trf797xReadSingle(testcommand, 1);
+testcommand[0] = RX_SPECIAL_SETTINGS ;
+//testcommand[1] = 0x40;                  
+trf797xReadSingle(testcommand, 1);
+testcommand[0] = REGULATOR_CONTROL ;
+//testcommand[1] = 0x87;                  
+trf797xReadSingle(testcommand, 1);
+
+//}
+
+
+
+//RegisterReInitNFC();
+//
+ /* ook_ask.input(); //  Tri-state OOK pin
+ook_ask.mode(PullUp);*/
+//
+////readerInt.enable_irq();
+////========================================================================
+//
+
+while(1)
+{
+
+//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;                  //Reset FIFO(0x0F/0x8F)
+trf797xReadSingle(testcommand,1); 
+testcommand[0] = IRQ_STATUS;                  //Reset FIFO(0x0F/0x8F)
+trf797xReadSingle(testcommand,1);  
+//wait_us(240);                                                   //Wait 2ms
+//testcommand[0] = IRQ_STATUS;             //Read/Clear IRQ Status(0x0C=>0x6C)+dummy read
+//trf797xReadContinuous(testcommand, 2);
+//testcommand[0] = RESET;                  //Reset FIFO(0x0F/0x8F)
+//trf797xDirectCommand(testcommand);  
+
+ // Read second register as dummy read
+//
+wait_ms(5);
+testcommand[0] = IRQ_STATUS;                  //Reset FIFO(0x0F/0x8F)
+trf797xReadSingle(testcommand,1);
+testcommand[0] = IRQ_STATUS;                  //Reset FIFO(0x0F/0x8F)
+trf797xReadSingle(testcommand,1); 
+
+testcommand[0] = IRQ_STATUS;                  //Reset FIFO(0x0F/0x8F)
+trf797xReadSingle(testcommand,1); 
+testcommand[0] = RSSI_LEVELS;            //Read RSSI levels and oscillator status(0x0F/0x4F)           
+trf797xReadSingle(testcommand, 1); 
+testcommand[0] = FIFO_CONTROL;           //Read FIFO Status Register(0x1C/0x5C)    
+trf797xReadSingle(testcommand, 1);
+//     // Determine the number of bytes left in FIFO
+testcommand[0] = 0x7F & testcommand[0];
+//
+////buf[rxtxState] = FIFO; 
+buf[100] = FIFO;   
+//////testPin=1;           // Write the received bytes to the correct place in the buffer
+trf797xReadContinuous(&buf[100], testcommand[0]);
+
+
+////testPin=0;
+//testcommand[0] = RESET;                  //Reset FIFO(0x0F/0x8F)
+//trf797xDirectCommand(testcommand);  
+
+//testcommand[0] = 0x96;                  //DISABLE REVEIVER
+//trf797xDirectCommand(testcommand);
+////====================================================================8 clk cycles
+////testcommand[0] = IDLE;
+////trf797xDirectCommand(testcommand); 
+//
+//
+////====================================================================
+//testcommand[0] = 0x97;                  //ENABLE RECEIVER
+//trf797xDirectCommand(testcommand);
+////testcommand[0] = IDLE;
+////trf797xDirectCommand(testcommand);
+
+//====================================================================8 clk cycles
+
+//====================================================================8 clk cycles
+//testcommand[0] = IDLE;
+//trf797xDirectCommand(testcommand); 
+
+
+//====================================================================
+
+//
+////
+////
+////
+//buf[8] = FIFO;              // Write the received bytes to the correct place in the buffer
+////
+//trf797xReadContinuous(&buf[8], 10);//Read Continuous FIFO from 0x1F to 0x1F+0x0A(0x1F/0x7F)
+////                                        
+////                                                 //Wait 10 ms
+//testcommand[0] = IRQ_STATUS;             //Read/Clear IRQ Status(0x0C=>0x6C)+dummy read
+//trf797xReadContinuous(testcommand, 2);   // Read second register as dummy read
+//testcommand[0] = FIFO_CONTROL;           //Read FIFO Status Register(0x1C/0x5C)    
+//trf797xReadSingle(testcommand, 1);
+//testcommand[0] = RESET;                  //Reset FIFO(0x0F/0x8F)
+//trf797xDirectCommand(testcommand);       // FIFO must be reset before receiving the next response
+testcommand[0] = RSSI_LEVELS;            //Read RSSI levels and oscillator status(0x0F/0x4F)           
+trf797xReadSingle(testcommand, 1); 
+testcommand[0] = RESET;                  //Reset FIFO(0x0F/0x8F)
+trf797xDirectCommand(testcommand);  
+//trf797xStopDecoders();
+//trf797xRunDecoders();  
+//testcommand[0] = 0x94;                  //Reset FIFO(0x0F/0x8F)
+//trf797xDirectCommand(testcommand); 
+
+//testcommand[0] = IRQ_STATUS; 
+//trf797xReadContinuous(testcommand, 2); 
+
+
+}}     // Read RSSI levels
+//testcommand[0] = RESET;                  //Reset FIFO(0x0F/0x8F)
+//trf797xDirectCommand(testcommand);  //RESET
+
+//=========================================================================
+
+
+//}// end of while
+//}// end of main 
+/*==========================================================================
+Initialize the chipset ISO15693 and read UID:
+1) Reset
+[0x83]
+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] 
+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), 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]
+==============================================================================*/
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+    
+    ////  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
-    trf797xInitialSettings();
-//    printf("Initialized, ");
-
-    //  Tri-state OOK pin
-    ook_ask.input();
-    ook_ask.mode(PullUp);
-    
-    readerMode = 0x00;
-    
+ //   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");
@@ -141,16 +399,20 @@
 //    printf("\r\n");
     */
     
-    printf("\r\nFinished Init\r\n");
+   // printf("\r\nFinished Init\r\n");
     
     //  Setup interrupt from TRF7970
-    trf797xSetupIrq();
+  //  testPin=1;
+  //========================
+ //  trf797xSetupIrq();
+   //======================
+  // testPin=0;
 //    printf("IRQ setup, ");
     
 //    printf("finished init.\r\n\r\n");
 
-    while(TRUE) {
-        iso15693FindTag();
-    }
-    
-}   // End of main()
+  //  while(1) {                                    // changed TRUE to 1 $SG
+//       iso15693FindTag();
+//    }
+//    
+//}   // End of main()