interrupt handling

Dependencies:  

Committer:
rwclough
Date:
Fri Mar 20 19:51:01 2015 +0000
Revision:
4:9ab0d84bbd07
Parent:
3:eaae5433ab45
Child:
5:93c612f43ec2
Progress as of Friday 2015-03-20.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
rwclough 1:1eb96189824d 1 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
rwclough 2:bd5afc5aa139 2 Filename: main.cpp
rwclough 1:1eb96189824d 3 Description: Interface nRF51-DK eval board to TRF7970 eval board
rwclough 1:1eb96189824d 4 to test the suitability of the TRF7970 NFC chip
rwclough 1:1eb96189824d 5 for use in Gymtrack products.
rwclough 1:1eb96189824d 6 The nRF51-DK board has an nRF51422 MCU.
rwclough 1:1eb96189824d 7 Copyright (C) 2015 Gymtrack, Inc.
rwclough 1:1eb96189824d 8 Author: Ron Clough
rwclough 1:1eb96189824d 9 Date: 2015-02-26
rwclough 1:1eb96189824d 10
rwclough 1:1eb96189824d 11 Changes:
rwclough 1:1eb96189824d 12 Rev Date Who Details
rwclough 1:1eb96189824d 13 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
rwclough 1:1eb96189824d 14 0.0 2015-02-26 RWC Original version.
rwclough 1:1eb96189824d 15
rwclough 1:1eb96189824d 16 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
rwclough 0:5622c60e9d3a 17
rwclough 1:1eb96189824d 18 #include "mbed.h"
rwclough 1:1eb96189824d 19 #include "main.h"
rwclough 1:1eb96189824d 20 #include "readerComm.h"
rwclough 2:bd5afc5aa139 21 #include "interruptStuff.h"
rwclough 0:5622c60e9d3a 22
rwclough 1:1eb96189824d 23 SPI spi(p25, p28, p29); // MOSI, MISO, SCLK
rwclough 2:bd5afc5aa139 24 DigitalOut CS(p19); // Slave Select (SS)
rwclough 1:1eb96189824d 25 Serial pc(USBTX, USBRX); // Serial communication over USB with PC
rwclough 3:eaae5433ab45 26 DigitalOut heartbeatLED(LED4); // "Heartbeat" LED
rwclough 3:eaae5433ab45 27 DigitalOut debug2LED(LED2); // "Debug2" LED
rwclough 3:eaae5433ab45 28 DigitalOut ISO15693LED(LED3); // "Detected ISO15693 tag" LED
rwclough 3:eaae5433ab45 29 DigitalOut debug1LED(LED1); // "Debug1" LED
rwclough 4:9ab0d84bbd07 30 DigitalInOut ook_ask(p6); // Control ASK/OOK pin on TRF7970
rwclough 1:1eb96189824d 31 DigitalOut mod(p5); // Control MOD pin on TRF7970
rwclough 1:1eb96189824d 32 DigitalOut enable(p4); // Control EN pin on TRF7970
rwclough 1:1eb96189824d 33 DigitalOut enable2(p3); // Control EN2 pin on TRF7970
rwclough 0:5622c60e9d3a 34
rwclough 3:eaae5433ab45 35 DigitalOut testPin(p1);
rwclough 3:eaae5433ab45 36
rwclough 2:bd5afc5aa139 37 uint8_t buffer[2];
rwclough 2:bd5afc5aa139 38 int8_t rxtxState = 1; // Transmit/Receive byte count
rwclough 2:bd5afc5aa139 39 uint8_t buf[300];
rwclough 2:bd5afc5aa139 40 uint8_t irqRegister = 0x01; // Interrupt register
rwclough 3:eaae5433ab45 41 volatile uint8_t irqFlag = 0x00;
rwclough 2:bd5afc5aa139 42 uint8_t rxErrorFlag = 0x00;
rwclough 3:eaae5433ab45 43 uint8_t readerMode; // Determines how interrupts will be handled
rwclough 2:bd5afc5aa139 44 int16_t nfc_state;
rwclough 2:bd5afc5aa139 45 uint8_t nfc_protocol;
rwclough 2:bd5afc5aa139 46 uint8_t active;
rwclough 2:bd5afc5aa139 47 uint8_t tagFlag;
rwclough 3:eaae5433ab45 48 uint8_t irqCount = 0;
rwclough 0:5622c60e9d3a 49
rwclough 4:9ab0d84bbd07 50 uint8_t debugBuffer[1000]; // Capture data for analysis
rwclough 4:9ab0d84bbd07 51 uint8_t bufIdx=0;
rwclough 4:9ab0d84bbd07 52
rwclough 2:bd5afc5aa139 53 void blinkHeartbeatLED(void)
rwclough 1:1eb96189824d 54 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
rwclough 1:1eb96189824d 55 // blinkHeartbeatLED()
rwclough 1:1eb96189824d 56 // Description: Toogle the heartbeat LED.
rwclough 1:1eb96189824d 57 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
rwclough 2:bd5afc5aa139 58 {
rwclough 1:1eb96189824d 59 heartbeatLED = !heartbeatLED;
rwclough 1:1eb96189824d 60 } // End of blinkHeartbeatLED()
rwclough 0:5622c60e9d3a 61
rwclough 2:bd5afc5aa139 62 int main()
rwclough 2:bd5afc5aa139 63 {
rwclough 3:eaae5433ab45 64 // Power up sequence. See TRF7970A datasheet "SLOU43K August 2011 Revised April 20914" Figures 6-3 and 6-4.
rwclough 1:1eb96189824d 65 CS = 0; enable2 = 0; enable = 0;
rwclough 1:1eb96189824d 66 wait_ms(2);
rwclough 2:bd5afc5aa139 67 CS = 1;
rwclough 1:1eb96189824d 68 wait_ms(3);
rwclough 1:1eb96189824d 69 enable2 = 1;
rwclough 1:1eb96189824d 70 wait_ms(1);
rwclough 1:1eb96189824d 71 enable = 1;
rwclough 1:1eb96189824d 72
rwclough 1:1eb96189824d 73 mod = 0;
rwclough 3:eaae5433ab45 74 testPin = 0;
rwclough 1:1eb96189824d 75 // Setup serial communication
rwclough 0:5622c60e9d3a 76 pc.baud(115200);
rwclough 3:eaae5433ab45 77 // printf("\r\nInitialization: ");
rwclough 3:eaae5433ab45 78
rwclough 3:eaae5433ab45 79 /*
rwclough 3:eaae5433ab45 80 // After power up sequence, Chip Status = 0x81, Regulator Control = 0x80
rwclough 3:eaae5433ab45 81 *buffer = CHIP_STATUS_CONTROL;
rwclough 3:eaae5433ab45 82 spiReadContinuous(buffer, 2);
rwclough 3:eaae5433ab45 83 printf("\r\nChip Status: 0x%X\r\n", buffer[1]);
rwclough 3:eaae5433ab45 84 *buffer = REGULATOR_CONTROL;
rwclough 3:eaae5433ab45 85 spiReadContinuous(buffer, 2);
rwclough 3:eaae5433ab45 86 printf("Regulator Control: 0x%X\r\n", buffer[1]);
rwclough 3:eaae5433ab45 87 */
rwclough 3:eaae5433ab45 88
rwclough 1:1eb96189824d 89
rwclough 3:eaae5433ab45 90 // Setup LEDs
rwclough 1:1eb96189824d 91 heartbeatLED = LED_OFF;
rwclough 2:bd5afc5aa139 92 ISO15693LED = LED_OFF;
rwclough 3:eaae5433ab45 93 debug1LED = LED_OFF;
rwclough 3:eaae5433ab45 94 debug2LED = LED_OFF;
rwclough 3:eaae5433ab45 95 /*
rwclough 3:eaae5433ab45 96 for (uint8_t i=0; i<4; i++) {
rwclough 3:eaae5433ab45 97 heartbeatLED = LED_ON;
rwclough 3:eaae5433ab45 98 ISO15693LED = LED_ON;
rwclough 3:eaae5433ab45 99 debug1LED = LED_ON;
rwclough 3:eaae5433ab45 100 wait_ms(100);
rwclough 3:eaae5433ab45 101 heartbeatLED = LED_OFF;
rwclough 3:eaae5433ab45 102 ISO15693LED = LED_OFF;
rwclough 3:eaae5433ab45 103 debug1LED = LED_OFF;
rwclough 3:eaae5433ab45 104 wait_ms(100);
rwclough 3:eaae5433ab45 105 }
rwclough 3:eaae5433ab45 106 */
rwclough 3:eaae5433ab45 107 // Ticker heartbeat;
rwclough 3:eaae5433ab45 108 // heartbeat.attach(blinkHeartbeatLED, 1);
rwclough 3:eaae5433ab45 109 // printf("LEDs, ");
rwclough 1:1eb96189824d 110
rwclough 1:1eb96189824d 111 // Setup the SPI interface
rwclough 3:eaae5433ab45 112 spi.format(8, 1); // 8 bit data, mode = 1 (transition on rising edge, sample on falling edge)
rwclough 1:1eb96189824d 113 spi.frequency(1000000); // SCLK = 1 MHz
rwclough 3:eaae5433ab45 114 // printf("SPI, ");
rwclough 1:1eb96189824d 115
rwclough 1:1eb96189824d 116 // Set On-Off Keying modulation
rwclough 3:eaae5433ab45 117 ook_ask.output();
rwclough 1:1eb96189824d 118 ook_ask = 1;
rwclough 3:eaae5433ab45 119 // printf("OOK, ");
rwclough 1:1eb96189824d 120
rwclough 2:bd5afc5aa139 121 // Apply initial settings to the TRF7970
rwclough 4:9ab0d84bbd07 122 trf797xInitialSettings();
rwclough 3:eaae5433ab45 123 // printf("Initialized, ");
rwclough 3:eaae5433ab45 124
rwclough 3:eaae5433ab45 125 // Tri-state OOK pin
rwclough 4:9ab0d84bbd07 126 ook_ask.input();
rwclough 4:9ab0d84bbd07 127 ook_ask.mode(PullUp);
rwclough 3:eaae5433ab45 128
rwclough 3:eaae5433ab45 129 readerMode = 0x00;
rwclough 3:eaae5433ab45 130
rwclough 3:eaae5433ab45 131 /*
rwclough 3:eaae5433ab45 132 // Test: Write 0xAA to MODULATOR_CONTROL, then verify by reading MODULATOR_CONTROL.
rwclough 3:eaae5433ab45 133 // printf("\r\n");
rwclough 3:eaae5433ab45 134 buffer[0] = MODULATOR_CONTROL;
rwclough 3:eaae5433ab45 135 buffer[1] = 0xAA;
rwclough 3:eaae5433ab45 136 // printf("BEFORE WR: buffer[0]: %X buffer[1]: %X\r\n", buffer[0], buffer[1]);
rwclough 3:eaae5433ab45 137 spiWriteSingle(buffer, 2);
rwclough 3:eaae5433ab45 138 // printf("AFTER WR: buffer[0]: %X buffer[1]: %X\r\n", buffer[0], buffer[1]);
rwclough 3:eaae5433ab45 139 spiReadSingle(buffer, 1);
rwclough 3:eaae5433ab45 140 // printf("AFTER RD: buffer[0]: %X buffer[1]: %X\r\n", buffer[0], buffer[1]);
rwclough 3:eaae5433ab45 141 // printf("\r\n");
rwclough 3:eaae5433ab45 142 */
rwclough 1:1eb96189824d 143
rwclough 4:9ab0d84bbd07 144 printf("\r\nFinished Init\r\n");
rwclough 4:9ab0d84bbd07 145
rwclough 1:1eb96189824d 146 // Setup interrupt from TRF7970
rwclough 4:9ab0d84bbd07 147 trf797xSetupIrq();
rwclough 3:eaae5433ab45 148 // printf("IRQ setup, ");
rwclough 1:1eb96189824d 149
rwclough 3:eaae5433ab45 150 // printf("finished init.\r\n\r\n");
rwclough 3:eaae5433ab45 151
rwclough 3:eaae5433ab45 152 while(TRUE) {
rwclough 3:eaae5433ab45 153 iso15693FindTag();
rwclough 3:eaae5433ab45 154 }
rwclough 1:1eb96189824d 155
rwclough 1:1eb96189824d 156 } // End of main()