interrupt handling

Dependencies:  

readerComm.cpp

Committer:
soumi_ghsoh
Date:
2015-04-02
Revision:
6:3c510c297e2f
Parent:
5:93c612f43ec2
Child:
7:96baf1b2fd07

File content as of revision 6:3c510c297e2f:

/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
    Filename:       readerComm.cpp
    Description:    Functions used to communicate with the TRF7970 eval bd.
                    Communication is by means of an SPI interface between
                    the nRF51-DK board (nRF51422 MCU) and the TRF7970 eval bd.
    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:
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]
==============================================================================*/   






#include    "mbed.h"
#include    "readerComm.h"
#include    "interruptStuff.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  InterruptIn     readerInt;          // main.cpp
extern  int8_t          rxtxState;          // Transmit/Receive byte count (main.cpp)
extern  uint8_t         buf[300];           // main.cpp
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  uint8_t         buffer[2];

extern  uint8_t tagFlag;
extern  DigitalIn   irqPin;
extern  DigitalOut  debug1LED;
extern  DigitalOut  debug2LED;
extern  DigitalOut  ISO15693LED;
extern  DigitalOut  heartbeatLED;

extern  DigitalOut  testPin;

extern  uint8_t     debugBuffer[1000];  // Capture data for analysis
extern  uint8_t     bufIdx;



void trf797xDirectCommand(uint8_t *buffer)
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
//  trf797xDirectCommand()
//  Description:    Transmit a Direct Command to the reader chip.
//  Parameter:      *buffer =   the direct command.
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
{
    *buffer = (0x80 | *buffer);     // Setup command mode
    *buffer = (0x9F & *buffer);     // Setup command mode
    CS = SELECT;
    spi.write(*buffer);
    CS = DESELECT;
}   // End of trf797xDirectCommand()

void trf797xWriteSingle(uint8_t *buffer, uint8_t length)
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
//  trf797xWriteSingle()
//  Description:    Writes to specified reader registers.
//  Parameters:     *buffer =   addresses of the registers followed by the
//                              contents to write.
//                  length =    number of registers * 2.
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
{
    uint8_t i=0;
    
    CS = SELECT;
    while(length > 0) {
        *buffer = (0x1F & *buffer);     // Register address
        for(i = 0; i < 2; i++) {
            spi.write(*buffer);
            buffer++;
            length--;
        }
    }
    CS = DESELECT;
   
}   // End of trf797xWriteSingle()

void trf797xReadSingle(uint8_t *buffer, uint8_t number)
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
//  trf797xReadSingle()
//  Description:    Reads specified reader chip registers and 
//                  writes register contents to *buffer.
//  Parameters:     *buffer =   addresses of the registers.
//                  number  =   number of registers.
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
{
    CS = SELECT;
    while(number > 0) {
        *buffer = (0x40 | *buffer);     // Address, read, single
        *buffer = (0x5F & *buffer);     // Register address
        spi.write(*buffer);
        *buffer = spi.write(0x00);      // *buffer <- register contents
        buffer++;
        number--;
    }
    CS = DESELECT;
}   // End of trf797xReadSingle()

void trf797xReadContinuous(uint8_t *buffer, uint8_t length)
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
//  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.
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
{  //==================tested wrk $sg
    CS = SELECT;
    *buffer = (0x60 | *buffer);     // Address, read, continuous
    *buffer = (0x7F & *buffer);     // Register address
    spi.write(*buffer);
    while(length > 0) {
        *buffer = spi.write(0x00);
        buffer++;
        length--;
    }
//    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)
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
//  trf797xRawWrite()
//  Description:    Used in SPI mode to write direct to the reader chip.
//  Parameters:     *buffer =   raw data
//                  length =    number of data bytes
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
{
    CS = SELECT;
    while(length > 0) {
        temp = spi.write(*buffer);
        buffer++;
        length--;
    }
    CS = DESELECT;
}   // End of trf797xRawWrite()

void trf797xStopDecoders(void)
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
//  trf797xStopDecoders()
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
{
    command[0] = STOP_DECODERS;
    trf797xDirectCommand(command);
}

void trf797xRunDecoders(void)
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
//  trf797xRunDecoders()
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
{
    command[0] = RUN_DECODERS;
    trf797xDirectCommand(command);
}


void PowerUpNFC(void)
{CS = 1;
 wait_ms(4);
 enable = 1;
}
void PowerDownNFC(void)
{enable = 0;
}

void SpiInit1(void)
{
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);  
}

void NFCInit(void)
{
//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);
//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;
// 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);
//wait_ms(2);
 
testcommand[0] = ISO_CONTROL;
testcommand[1] = 0x02;                  // 6.78 MHz, OOK 100%
trf797xWriteSingle(testcommand, 2);
//wait_ms(6); 
}

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);
}


void RegistersReadNFC(void)
{
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);
testcommand[0] = IRQ_STATUS;                  
trf797xReadSingle(testcommand,1);
testcommand[0] = IRQ_STATUS;                  
trf797xReadSingle(testcommand,1); 
testcommand[0] = FIFO_CONTROL;           //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();  
//wait(1);
}  
//void PollNFC(void)
//{
//}

void ReadNFC(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); 
testPin=1;
testcommand[0] = IRQ_STATUS;                  
trf797xReadSingle(testcommand,1); 
testcommand[0] = IRQ_STATUS;                  
trf797xReadSingle(testcommand,1);  
wait_ms(5);
testcommand[0] = IRQ_STATUS;                  
trf797xReadSingle(testcommand,1);
testcommand[0] = IRQ_STATUS;                  
trf797xReadSingle(testcommand,1); 
testcommand[0] = FIFO_CONTROL;           //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]);
testPin=0;
testcommand[0] = IRQ_STATUS;                  
trf797xReadSingle(testcommand,1); 
testcommand[0] = IRQ_STATUS;                  
trf797xReadSingle(testcommand,1);  
testcommand[0] = RESET;                  //Reset FIFO(0x0F/0x8F)
trf797xDirectCommand(testcommand);  
//wait(1);   
}