interrupt handling

Dependencies:  

Committer:
rwclough
Date:
Thu Mar 12 15:05:36 2015 +0000
Revision:
3:eaae5433ab45
Parent:
2:bd5afc5aa139
Child:
4:9ab0d84bbd07
Finally getting many, many IRQs. Now to see what's going on :)

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 3:eaae5433ab45 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 2:bd5afc5aa139 50 void blinkHeartbeatLED(void)
rwclough 1:1eb96189824d 51 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
rwclough 1:1eb96189824d 52 // blinkHeartbeatLED()
rwclough 1:1eb96189824d 53 // Description: Toogle the heartbeat LED.
rwclough 1:1eb96189824d 54 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
rwclough 2:bd5afc5aa139 55 {
rwclough 1:1eb96189824d 56 heartbeatLED = !heartbeatLED;
rwclough 1:1eb96189824d 57 } // End of blinkHeartbeatLED()
rwclough 0:5622c60e9d3a 58
rwclough 2:bd5afc5aa139 59 int main()
rwclough 2:bd5afc5aa139 60 {
rwclough 3:eaae5433ab45 61 // Power up sequence. See TRF7970A datasheet "SLOU43K August 2011 Revised April 20914" Figures 6-3 and 6-4.
rwclough 1:1eb96189824d 62 CS = 0; enable2 = 0; enable = 0;
rwclough 1:1eb96189824d 63 wait_ms(2);
rwclough 2:bd5afc5aa139 64 CS = 1;
rwclough 1:1eb96189824d 65 wait_ms(3);
rwclough 1:1eb96189824d 66 enable2 = 1;
rwclough 1:1eb96189824d 67 wait_ms(1);
rwclough 1:1eb96189824d 68 enable = 1;
rwclough 1:1eb96189824d 69
rwclough 1:1eb96189824d 70 mod = 0;
rwclough 3:eaae5433ab45 71 testPin = 0;
rwclough 1:1eb96189824d 72 // Setup serial communication
rwclough 0:5622c60e9d3a 73 pc.baud(115200);
rwclough 3:eaae5433ab45 74 // printf("\r\nInitialization: ");
rwclough 3:eaae5433ab45 75
rwclough 3:eaae5433ab45 76 /*
rwclough 3:eaae5433ab45 77 // After power up sequence, Chip Status = 0x81, Regulator Control = 0x80
rwclough 3:eaae5433ab45 78 *buffer = CHIP_STATUS_CONTROL;
rwclough 3:eaae5433ab45 79 spiReadContinuous(buffer, 2);
rwclough 3:eaae5433ab45 80 printf("\r\nChip Status: 0x%X\r\n", buffer[1]);
rwclough 3:eaae5433ab45 81 *buffer = REGULATOR_CONTROL;
rwclough 3:eaae5433ab45 82 spiReadContinuous(buffer, 2);
rwclough 3:eaae5433ab45 83 printf("Regulator Control: 0x%X\r\n", buffer[1]);
rwclough 3:eaae5433ab45 84 */
rwclough 3:eaae5433ab45 85
rwclough 1:1eb96189824d 86
rwclough 3:eaae5433ab45 87 // Setup LEDs
rwclough 1:1eb96189824d 88 heartbeatLED = LED_OFF;
rwclough 2:bd5afc5aa139 89 ISO15693LED = LED_OFF;
rwclough 3:eaae5433ab45 90 debug1LED = LED_OFF;
rwclough 3:eaae5433ab45 91 debug2LED = LED_OFF;
rwclough 3:eaae5433ab45 92 /*
rwclough 3:eaae5433ab45 93 for (uint8_t i=0; i<4; i++) {
rwclough 3:eaae5433ab45 94 heartbeatLED = LED_ON;
rwclough 3:eaae5433ab45 95 ISO15693LED = LED_ON;
rwclough 3:eaae5433ab45 96 debug1LED = LED_ON;
rwclough 3:eaae5433ab45 97 wait_ms(100);
rwclough 3:eaae5433ab45 98 heartbeatLED = LED_OFF;
rwclough 3:eaae5433ab45 99 ISO15693LED = LED_OFF;
rwclough 3:eaae5433ab45 100 debug1LED = LED_OFF;
rwclough 3:eaae5433ab45 101 wait_ms(100);
rwclough 3:eaae5433ab45 102 }
rwclough 3:eaae5433ab45 103 */
rwclough 3:eaae5433ab45 104 // Ticker heartbeat;
rwclough 3:eaae5433ab45 105 // heartbeat.attach(blinkHeartbeatLED, 1);
rwclough 3:eaae5433ab45 106 // printf("LEDs, ");
rwclough 1:1eb96189824d 107
rwclough 1:1eb96189824d 108 // Setup the SPI interface
rwclough 3:eaae5433ab45 109 spi.format(8, 1); // 8 bit data, mode = 1 (transition on rising edge, sample on falling edge)
rwclough 1:1eb96189824d 110 spi.frequency(1000000); // SCLK = 1 MHz
rwclough 3:eaae5433ab45 111 // printf("SPI, ");
rwclough 1:1eb96189824d 112
rwclough 1:1eb96189824d 113 // Set On-Off Keying modulation
rwclough 3:eaae5433ab45 114 ook_ask.output();
rwclough 1:1eb96189824d 115 ook_ask = 1;
rwclough 3:eaae5433ab45 116 // printf("OOK, ");
rwclough 1:1eb96189824d 117
rwclough 2:bd5afc5aa139 118 // Apply initial settings to the TRF7970
rwclough 2:bd5afc5aa139 119 initialSettings();
rwclough 3:eaae5433ab45 120 // printf("Initialized, ");
rwclough 3:eaae5433ab45 121
rwclough 3:eaae5433ab45 122 // Tri-state OOK pin
rwclough 3:eaae5433ab45 123 ook_ask.input(); // Maybe add pullup *****
rwclough 3:eaae5433ab45 124
rwclough 3:eaae5433ab45 125 readerMode = 0x00;
rwclough 3:eaae5433ab45 126
rwclough 3:eaae5433ab45 127 /*
rwclough 3:eaae5433ab45 128 // Test: Write 0xAA to MODULATOR_CONTROL, then verify by reading MODULATOR_CONTROL.
rwclough 3:eaae5433ab45 129 // printf("\r\n");
rwclough 3:eaae5433ab45 130 buffer[0] = MODULATOR_CONTROL;
rwclough 3:eaae5433ab45 131 buffer[1] = 0xAA;
rwclough 3:eaae5433ab45 132 // printf("BEFORE WR: buffer[0]: %X buffer[1]: %X\r\n", buffer[0], buffer[1]);
rwclough 3:eaae5433ab45 133 spiWriteSingle(buffer, 2);
rwclough 3:eaae5433ab45 134 // printf("AFTER WR: buffer[0]: %X buffer[1]: %X\r\n", buffer[0], buffer[1]);
rwclough 3:eaae5433ab45 135 spiReadSingle(buffer, 1);
rwclough 3:eaae5433ab45 136 // printf("AFTER RD: buffer[0]: %X buffer[1]: %X\r\n", buffer[0], buffer[1]);
rwclough 3:eaae5433ab45 137 // printf("\r\n");
rwclough 3:eaae5433ab45 138 */
rwclough 1:1eb96189824d 139
rwclough 1:1eb96189824d 140 // Setup interrupt from TRF7970
rwclough 2:bd5afc5aa139 141 setupIrq();
rwclough 3:eaae5433ab45 142 // printf("IRQ setup, ");
rwclough 1:1eb96189824d 143
rwclough 3:eaae5433ab45 144 // printf("finished init.\r\n\r\n");
rwclough 3:eaae5433ab45 145
rwclough 3:eaae5433ab45 146 while(TRUE) {
rwclough 3:eaae5433ab45 147 iso15693FindTag();
rwclough 3:eaae5433ab45 148 }
rwclough 1:1eb96189824d 149
rwclough 1:1eb96189824d 150 } // End of main()