interrupt handling

Dependencies:  

main.cpp

Committer:
soumi_ghsoh
Date:
2015-04-01
Revision:
5:93c612f43ec2
Parent:
4:9ab0d84bbd07
Child:
6:3c510c297e2f

File content as of revision 5:93c612f43ec2:

/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
    Filename:       main.cpp
    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"

//#include    "NFC_7970.h"
#include    "readerComm.h"
#include    "interruptStuff.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
DigitalInOut  ook_ask(p6);      // Control ASK/OOK pin on TRF7970
DigitalOut  mod(p5);            // Control MOD pin on TRF7970

DigitalOut  testPin(p1);

uint8_t     buffer[2];
int8_t      rxtxState = 1;      // Transmit/Receive byte count
uint8_t     buf[300];
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];
void blinkHeartbeatLED(void)
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
//  blinkHeartbeatLED()
//  Description:    Toogle the heartbeat LED.
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
{
    heartbeatLED = !heartbeatLED;
}   // 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.
 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
    
//    printf("\r\nInitialization: ");

/*
    //  After power up sequence, Chip Status = 0x81, Regulator Control = 0x80
    *buffer = CHIP_STATUS_CONTROL;
    spiReadContinuous(buffer, 2);
    printf("\r\nChip Status: 0x%X\r\n", buffer[1]);
    *buffer = REGULATOR_CONTROL;
    spiReadContinuous(buffer, 2);
    printf("Regulator Control: 0x%X\r\n", buffer[1]);
*/

   heartbeatLED = LED_OFF;
    ISO15693LED = LED_OFF;
    debug1LED = LED_OFF;
    debug2LED = LED_OFF;

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
//    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()