interrupt handling

Dependencies:  

main.cpp

Committer:
rwclough
Date:
2015-03-12
Revision:
3:eaae5433ab45
Parent:
2:bd5afc5aa139
Child:
4:9ab0d84bbd07

File content as of revision 3:eaae5433ab45:

/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
    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    "main.h"
#include    "readerComm.h"
#include    "interruptStuff.h"

SPI         spi(p25, p28, p29); // MOSI, MISO, SCLK
DigitalOut  CS(p19);            // 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  enable(p4);         // Control EN pin on TRF7970
DigitalOut  enable2(p3);        // Control EN2 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;

void blinkHeartbeatLED(void)
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
//  blinkHeartbeatLED()
//  Description:    Toogle the heartbeat LED.
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
{
    heartbeatLED = !heartbeatLED;
}   // End of blinkHeartbeatLED()

int main()
{
    // 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;
    testPin = 0;
    //  Setup serial communication
    pc.baud(115200);
//    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]);
*/

    
    //  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
    initialSettings();
//    printf("Initialized, ");

    //  Tri-state OOK pin
    ook_ask.input();    // Maybe add 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");
    */
    
    //  Setup interrupt from TRF7970
    setupIrq();
//    printf("IRQ setup, ");
    
//    printf("finished init.\r\n\r\n");

    while(TRUE) {
        iso15693FindTag();
    }
    
}   // End of main()