Subversion Repositories Code-Repo

Rev

Rev 126 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 126 Rev 127
Line 7... Line 7...
7
#include "nfc.h"
7
#include "nfc.h"
8
#include "led_backpack.h"
8
#include "led_backpack.h"
9
#include "oled_ssd1306.h"
9
#include "oled_ssd1306.h"
10
#include "oled_ssd1331.h"
10
#include "oled_ssd1331.h"
11
#include "adc.h"
11
#include "adc.h"
12
#include <usart.h>
12
#include "xbee.h"
13
#include <delays.h>
13
#include <delays.h>
14
#include <string.h>
14
#include <string.h>
15
 
15
 
16
#pragma config WDTEN = OFF          // Turn off watchdog timer
16
#pragma config WDTEN = OFF          // Turn off watchdog timer
17
#pragma config XINST = OFF          // Turn off extended instruction set
17
#pragma config XINST = OFF          // Turn off extended instruction set
-
 
18
#ifdef USE_EXT_OSC
18
#pragma config OSC = HSPLL          // Use external oscillator (101)
19
#pragma config OSC = HSPLL          // Use external oscillator (101)
19
#pragma config PLLDIV = 3           // Set PPL prescaler to 3 (to get 4MHz)
20
#pragma config PLLDIV = 3           // Set PPL prescaler to 3 (to get 4MHz)
-
 
21
#else
-
 
22
#pragma config OSC = INTOSCPLL          // Use internal oscillator
-
 
23
#pragma config PLLDIV = 2           // Set PPL prescaler to 2 (to get 4MHz)
-
 
24
#endif
20
#pragma config CFGPLLEN = ON        // Enable PLL on startup
25
#pragma config CFGPLLEN = ON        // Enable PLL on startup
21
#pragma config PLLSEL = PLL96       // Use 96MHz PLL 4MHz -> 96MHz / 2 = 48MHz
26
#pragma config PLLSEL = PLL96       // Use 96MHz PLL 4MHz -> 96MHz / 2 = 48MHz
22
//#pragma config SOSCSEL = HIGH       // High Power T1OSC/SOSC circuit selected
27
//#pragma config SOSCSEL = HIGH       // High Power T1OSC/SOSC circuit selected
23
//#pragma config ADCSEL = BIT12       // 12-bit ADrC
28
//#pragma config ADCSEL = BIT12       // 12-bit ADrC
24
//#pragma config IOL1WAY = OFF        // IOLOCK bit can be set and cleared as needed
29
//#pragma config IOL1WAY = OFF        // IOLOCK bit can be set and cleared as needed
Line 75... Line 80...
75
 
80
 
76
    DBG_PRINT_MAIN("\r\nBegin Program\r\n");
81
    DBG_PRINT_MAIN("\r\nBegin Program\r\n");
77
 
82
 
78
    while (1) {
83
    while (1) {
79
 
84
 
80
        length = UART1_Read((char *) buffer);
85
        length = UART1_Read_Buffer((unsigned char *) buffer);
81
        if (length != 0) {
86
        if (length != 0) {
82
            UART1_WriteB((char *) buffer, length);
87
            UART1_WriteB((char *) buffer, length);
83
        }
88
        }
84
 
89
 
85
        Delay10KTCYx(255);
90
        Delay10KTCYx(255);
Line 247... Line 252...
247
    DBG_PRINT_MAIN("Found chip PN5%X\r\n", version.IC);
252
    DBG_PRINT_MAIN("Found chip PN5%X\r\n", version.IC);
248
    DBG_PRINT_MAIN("Firmware ver. %d.%d\r\n", version.Ver, version.Rev);
253
    DBG_PRINT_MAIN("Firmware ver. %d.%d\r\n", version.Ver, version.Rev);
249
    NFC_SAMConfig();
254
    NFC_SAMConfig();
250
 
255
 
251
    memset(cardData, 0, 24);
256
    memset(cardData, 0, 24);
252
    
257
 
253
    while (1) {
258
    while (1) {
254
        
259
        
255
//        // This query will hang until the NFC chip replies (card detected)
260
//        // This query will hang until the NFC chip replies (card detected)
256
//        length = NFC_readPassiveTargetID(cardData);
261
//        length = NFC_readPassiveTargetID(cardData);
257
//        if (length) {
262
//        if (length) {
Line 270... Line 275...
270
//                }
275
//                }
271
//                DBG_PRINT_MAIN("\r\n");
276
//                DBG_PRINT_MAIN("\r\n");
272
//            }
277
//            }
273
//        }
278
//        }
274
 
279
 
-
 
280
        //        // This query will hang until the NFC chip replies (card detected)
-
 
281
        //        length = NFC_readPassiveTargetID(cardData);
-
 
282
        //        if (length) {
-
 
283
        //            DBG_PRINT_MAIN("Cards Found: %u\r\n", length);
-
 
284
        //            DBG_PRINT_MAIN("UID Length: %d bytes\r\n", cardData[0].NFCID_LEN);
-
 
285
        //            DBG_PRINT_MAIN("UID: ");
-
 
286
        //            for (i = 0; i < cardData[0].NFCID_LEN; i++) {
-
 
287
        //                DBG_PRINT_MAIN("%02X ", cardData[0].NFCID[i]);
-
 
288
        //            }
-
 
289
        //            DBG_PRINT_MAIN("\r\n");
-
 
290
        //            if (length == 2) {
-
 
291
        //                DBG_PRINT_MAIN("UID Length: %d bytes\r\n", cardData[1].NFCID_LEN);
-
 
292
        //                DBG_PRINT_MAIN("UID: ");
-
 
293
        //                for (i = 0; i < cardData[1].NFCID_LEN; i++) {
-
 
294
        //                    DBG_PRINT_MAIN("%02X ", cardData[1].NFCID[i]);
-
 
295
        //                }
-
 
296
        //                DBG_PRINT_MAIN("\r\n");
-
 
297
        //            }
-
 
298
        //        }
-
 
299
 
275
        // This query will not wait for a detection before responding
300
        // This query will not wait for a detection before responding
276
        length = NFC_pollTargets(1, 1, cardData);
301
        length = NFC_pollTargets(1, 1, cardData);
277
        if (!length) {
302
        if (!length) {
278
            memset(cardData_prev, 0, 24);
303
            memset(cardData_prev, 0, 24);
279
        } else if (length == 1) {
304
        } else if (length == 1) {
280
            if (memcmp(&cardData[0].NFCID, &cardData_prev[0].NFCID, cardData[0].NFCID_LEN) == 0) {
305
            if (memcmp(&cardData[0].NFCID, &cardData_prev[0].NFCID, cardData[0].NFCID_LEN) == 0) {
281
                // Do nothing
306
                // Do nothing
282
            } else if (memcmp(&cardData[0].NFCID, &cardData_prev[1].NFCID, cardData[0].NFCID_LEN) == 0) {
307
            } else if (memcmp(&cardData[0].NFCID, &cardData_prev[1].NFCID, cardData[0].NFCID_LEN) == 0) {
283
                memcpy((char *)&cardData_prev[0], (const char *)&cardData[0], 12);
308
                memcpy((char *) &cardData_prev[0], (const char *) &cardData[0], 12);
284
            } else {
309
            } else {
285
                DBG_PRINT_MAIN("UID: ");
310
                DBG_PRINT_MAIN("UID: ");
286
                for (i = 0; i < cardData[0].NFCID_LEN; i++) {
311
                for (i = 0; i < cardData[0].NFCID_LEN; i++) {
287
                    DBG_PRINT_MAIN("%02X ", cardData[0].NFCID[i]);
312
                    DBG_PRINT_MAIN("%02X ", cardData[0].NFCID[i]);
288
                }
313
                }
289
                DBG_PRINT_MAIN("\r\n");
314
                DBG_PRINT_MAIN("\r\n");
290
                memcpy((char *)&cardData_prev[0], (const char *)&cardData[0], 12);
315
                memcpy((char *) &cardData_prev[0], (const char *) &cardData[0], 12);
291
            }
316
            }
292
            memset(&cardData_prev[1], 0, 12);
317
            memset(&cardData_prev[1], 0, 12);
293
        } else if (length == 2) {
318
        } else if (length == 2) {
294
            if (memcmp(&cardData[0].NFCID, &cardData_prev[0].NFCID, cardData[0].NFCID_LEN) == 0 &&
319
            if (memcmp(&cardData[0].NFCID, &cardData_prev[0].NFCID, cardData[0].NFCID_LEN) == 0 &&
295
                    memcmp(&cardData[1].NFCID, &cardData_prev[1].NFCID, cardData[1].NFCID_LEN) == 0) {
320
                    memcmp(&cardData[1].NFCID, &cardData_prev[1].NFCID, cardData[1].NFCID_LEN) == 0) {
296
                // Do nothing
321
                // Do nothing
297
            } else if (memcmp(&cardData[0].NFCID, &cardData_prev[1].NFCID, cardData[0].NFCID_LEN) == 0 &&
322
            } else if (memcmp(&cardData[0].NFCID, &cardData_prev[1].NFCID, cardData[0].NFCID_LEN) == 0 &&
298
                    memcmp(&cardData[1].NFCID, &cardData_prev[0].NFCID, cardData[1].NFCID_LEN) == 0) {
323
                    memcmp(&cardData[1].NFCID, &cardData_prev[0].NFCID, cardData[1].NFCID_LEN) == 0) {
299
                memcpy((char *)&cardData_prev[0], (const char *)&cardData[0], 12);
324
                memcpy((char *) &cardData_prev[0], (const char *) &cardData[0], 12);
300
                memcpy((char *)&cardData_prev[1], (const char *)&cardData[1], 12);
325
                memcpy((char *) &cardData_prev[1], (const char *) &cardData[1], 12);
301
            } else if (memcmp(&cardData[0].NFCID, &cardData_prev[0].NFCID, cardData[0].NFCID_LEN) == 0) {
326
            } else if (memcmp(&cardData[0].NFCID, &cardData_prev[0].NFCID, cardData[0].NFCID_LEN) == 0) {
302
                // First card matched
327
                // First card matched
303
                DBG_PRINT_MAIN("UID2: ");
328
                DBG_PRINT_MAIN("UID2: ");
304
                for (i = 0; i < cardData[1].NFCID_LEN; i++) {
329
                for (i = 0; i < cardData[1].NFCID_LEN; i++) {
305
                    DBG_PRINT_MAIN("%02X ", cardData[1].NFCID[i]);
330
                    DBG_PRINT_MAIN("%02X ", cardData[1].NFCID[i]);
306
                }
331
                }
307
                DBG_PRINT_MAIN("\r\n");
332
                DBG_PRINT_MAIN("\r\n");
308
                memcpy(&cardData_prev[1], (const char *)&cardData[1], 12);
333
                memcpy(&cardData_prev[1], (const char *) &cardData[1], 12);
309
            } else if (memcmp(&cardData[1].NFCID, &cardData_prev[1].NFCID, cardData[1].NFCID_LEN) == 0) {
334
            } else if (memcmp(&cardData[1].NFCID, &cardData_prev[1].NFCID, cardData[1].NFCID_LEN) == 0) {
310
                // Second card matched
335
                // Second card matched
311
                DBG_PRINT_MAIN("UID1: ");
336
                DBG_PRINT_MAIN("UID1: ");
312
                for (i = 0; i < cardData[0].NFCID_LEN; i++) {
337
                for (i = 0; i < cardData[0].NFCID_LEN; i++) {
313
                    DBG_PRINT_MAIN("%02X ", cardData[0].NFCID[i]);
338
                    DBG_PRINT_MAIN("%02X ", cardData[0].NFCID[i]);
314
                }
339
                }
315
                DBG_PRINT_MAIN("\r\n");
340
                DBG_PRINT_MAIN("\r\n");
316
                memcpy((char *)&cardData_prev[0], (const char *)&cardData[0], 12);
341
                memcpy((char *) &cardData_prev[0], (const char *) &cardData[0], 12);
317
            } else {
342
            } else {
318
                // No match
343
                // No match
319
                DBG_PRINT_MAIN("UID1: ");
344
                DBG_PRINT_MAIN("UID1: ");
320
                for (i = 0; i < cardData[0].NFCID_LEN; i++) {
345
                for (i = 0; i < cardData[0].NFCID_LEN; i++) {
321
                    DBG_PRINT_MAIN("%02X ", cardData[0].NFCID[i]);
346
                    DBG_PRINT_MAIN("%02X ", cardData[0].NFCID[i]);
322
                }
347
                }
323
                DBG_PRINT_MAIN("\r\n");
348
                DBG_PRINT_MAIN("\r\n");
324
                memcpy((char *)&cardData_prev[0], (const char *)&cardData[0], 12);
349
                memcpy((char *) &cardData_prev[0], (const char *) &cardData[0], 12);
325
                DBG_PRINT_MAIN("UID2: ");
350
                DBG_PRINT_MAIN("UID2: ");
326
                for (i = 0; i < cardData[1].NFCID_LEN; i++) {
351
                for (i = 0; i < cardData[1].NFCID_LEN; i++) {
327
                    DBG_PRINT_MAIN("%02X ", cardData[1].NFCID[i]);
352
                    DBG_PRINT_MAIN("%02X ", cardData[1].NFCID[i]);
328
                }
353
                }
329
                DBG_PRINT_MAIN("\r\n");
354
                DBG_PRINT_MAIN("\r\n");
330
                memcpy((char *)&cardData_prev[1], &cardData[1], 12);
355
                memcpy((char *) &cardData_prev[1], (const char *) &cardData[1], 12);
331
            }
356
            }
332
        }
357
        }
333
    }
358
    }
334
}
359
}
335
#endif
360
#endif
Line 730... Line 755...
730
    GFX_clearScreen();
755
    GFX_clearScreen();
731
    GFX_setRotation(3);
756
    GFX_setRotation(3);
732
 
757
 
733
    while (1) {
758
    while (1) {
734
        // ADC read from AN0-AN2 and prints to display
759
        // ADC read from AN0-AN2 and prints to display
735
        ADC_Start(ADC_CHANNEL_AN0);
760
        ADC_Start(ADC_CHANNEL_AN2);
736
        GFX_fillRect(0, 0, GFX_width(), 8, SSD1331_BLACK);
761
        GFX_fillRect(0, 0, GFX_width(), 8, SSD1331_BLACK);
737
        GFX_setCursor(0, 0);
762
        GFX_setCursor(0, 0);
738
        while (!ADC_Get_Result(&x));
763
        while (!ADC_Get_Result(&x));
739
        GFX_writeString("X: %u", x);
764
        GFX_writeString("X: %u", x);
740
 
765
 
Line 742... Line 767...
742
        GFX_fillRect(0, 8, GFX_width(), 8, SSD1331_BLACK);
767
        GFX_fillRect(0, 8, GFX_width(), 8, SSD1331_BLACK);
743
        GFX_setCursor(0, 8);
768
        GFX_setCursor(0, 8);
744
        while (!ADC_Get_Result(&y));
769
        while (!ADC_Get_Result(&y));
745
        GFX_writeString("Y: %u", y);
770
        GFX_writeString("Y: %u", y);
746
 
771
 
747
        ADC_Start(ADC_CHANNEL_AN2);
772
        ADC_Start(ADC_CHANNEL_AN0);
748
        GFX_fillRect(0, 16, GFX_width(), 8, SSD1331_BLACK);
773
        GFX_fillRect(0, 16, GFX_width(), 8, SSD1331_BLACK);
749
        GFX_setCursor(0, 16);
774
        GFX_setCursor(0, 16);
750
        while (!ADC_Get_Result(&z));
775
        while (!ADC_Get_Result(&z));
751
        GFX_writeString("Z: %u", z);
776
        GFX_writeString("Z: %u", z);
752
    }
777
    }
753
}
778
}
754
 
779
 
755
#endif
780
#endif
756
 
781
 
-
 
782
#ifdef _TEST_XBEE
-
 
783
 
-
 
784
void main(void) {
-
 
785
    unsigned int i, length = 0;
-
 
786
    unsigned char buffer[100];
-
 
787
 
-
 
788
    XBEE_RX_DATA_PACKET_FRAME *rx_data_frame;
-
 
789
 
-
 
790
    /* --------------------- Oscillator Configuration --------------------- */
-
 
791
    //    OSCTUNEbits.PLLEN = 1;          // Enable 4x PLL
-
 
792
    OSCCONbits.IRCF = 0b111; // Set INTOSC postscaler to 8MHz
-
 
793
    OSCCONbits.SCS = 0b00; // Use 96MHz PLL as primary clock source
-
 
794
    /* -------------------------------------------------------------------- */
-
 
795
 
-
 
796
    // Set all ports as digial I/O
-
 
797
    ANCON0 = 0xFF;
-
 
798
    ANCON1 = 0x1F;
-
 
799
 
-
 
800
    UART1_Init(); // Initialize the UART handler code
-
 
801
    XBee_Init();
-
 
802
 
-
 
803
    interrupt_enable(); // Enable high-priority interrupts and low-priority interrupts
-
 
804
    interrupt_init(); // Initialize the interrupt priorities
-
 
805
 
-
 
806
    DBG_PRINT_MAIN("\r\nBegin Program\r\n");
-
 
807
 
-
 
808
    while (1) {
-
 
809
 
-
 
810
//#define _ROUTER
-
 
811
#define _COORDINATOR
-
 
812
 
-
 
813
#ifdef _ROUTER
-
 
814
        XBEE_TX_DATA_PACKET_FRAME *tx_data_frame;
-
 
815
        tx_data_frame = (void *) buffer;
-
 
816
        tx_data_frame->frame_type = XBEE_TX_DATA_PACKET;
-
 
817
        tx_data_frame->frame_id = 1;
-
 
818
        tx_data_frame->destination_64.UPPER_32.long_value = 0x00000000;
-
 
819
        tx_data_frame->destination_64.LOWER_32.long_value = 0x00000000;
-
 
820
        tx_data_frame->destination_16.INT_16.int_value = 0xFEFF;
-
 
821
        tx_data_frame->broadcast_radius = 0;
-
 
822
        tx_data_frame->options = 0;
-
 
823
        tx_data_frame->data[0] = 0x54;
-
 
824
        tx_data_frame->data[1] = 0x78;
-
 
825
        tx_data_frame->data[2] = 0x32;
-
 
826
        tx_data_frame->data[3] = 0x43;
-
 
827
        tx_data_frame->data[4] = 0x6F;
-
 
828
        tx_data_frame->data[5] = 0x6F;
-
 
829
        tx_data_frame->data[6] = 0x72;
-
 
830
        tx_data_frame->data[7] = 0x11;
-
 
831
        XBee_Process_Transmit_Frame(buffer, XBEE_TX_DATA_PACKET_FRAME_SIZE + 8);
-
 
832
        
-
 
833
        Delay10KTCYx(255);
-
 
834
        Delay10KTCYx(255);
-
 
835
        Delay10KTCYx(255);
-
 
836
        Delay10KTCYx(255);
-
 
837
        Delay10KTCYx(255);
-
 
838
        Delay10KTCYx(255);
-
 
839
        Delay10KTCYx(255);
-
 
840
        Delay10KTCYx(255);
-
 
841
#endif
-
 
842
 
-
 
843
#ifdef _COORDINATOR
-
 
844
        length = XBee_Get_Received_Frame(buffer);
-
 
845
        if (length != 0) {
-
 
846
            switch (*(unsigned char *) buffer) {
-
 
847
                case XBEE_RX_AT_COMMAND_RESPONSE:
-
 
848
                    DBG_PRINT_MAIN("XBEE: parsing recieved AT command response frame\r\n");
-
 
849
                    break;
-
 
850
                case XBEE_RX_DATA_PACKET:
-
 
851
                    rx_data_frame = (void *)buffer;
-
 
852
                    DBG_PRINT_MAIN("XBEE: parsing recieved data recieved frame\r\n");
-
 
853
                    XBee_ConvertEndian64(&(rx_data_frame->source_64));
-
 
854
                    XBee_ConvertEndian16(&(rx_data_frame->source_16));
-
 
855
                    DBG_PRINT_MAIN("Source 64: %08lX %08lX  Source 16: %04X  Options: %X\r\n", \\
-
 
856
                            rx_data_frame->source_64.UPPER_32.long_value, \\
-
 
857
                            rx_data_frame->source_64.LOWER_32.long_value, \\
-
 
858
                            rx_data_frame->source_16.INT_16.int_value, \\
-
 
859
                            rx_data_frame->recieve_options);
-
 
860
                    DBG_PRINT_MAIN("Data: ");
-
 
861
                    for (i = 0; i < length - XBEE_RX_DATA_PACKET_FRAME_SIZE; i++) {
-
 
862
                        DBG_PRINT_MAIN("%02X ", rx_data_frame->data[i]);
-
 
863
                    }
-
 
864
                    DBG_PRINT_MAIN("\r\n");
-
 
865
                    break;
-
 
866
                case XBEE_RX_DATA_TX_STATUS:
-
 
867
                    DBG_PRINT_MAIN("XBEE: parsing recieved TX status frame\r\n");
-
 
868
                    break;
-
 
869
                case XBEE_RX_IO_DATA_SAMPLE:
-
 
870
                    DBG_PRINT_MAIN("XBEE: parsing recieved IO data sample frame\r\n");
-
 
871
                    break;
-
 
872
                case XBEE_RX_EXPLICIT_COMMAND:
-
 
873
                    DBG_PRINT_MAIN("XBEE: parsing recieved explicit command frame\r\n");
-
 
874
                    break;
-
 
875
                case XBEE_RX_REMOTE_AT_COMMAND_RESPONSE:
-
 
876
                    DBG_PRINT_MAIN("XBEE: parsing recieved remote AT command frame\r\n");
-
 
877
                    break;
-
 
878
                case XBEE_RX_ROUTE_RECORD:
-
 
879
                    DBG_PRINT_MAIN("XBEE: parsing recieved route record frame\r\n");
-
 
880
                    break;
-
 
881
                case XBEE_RX_NODE_IDENTIFICATION:
-
 
882
                    DBG_PRINT_MAIN("XBEE: parsing recieved node identification frame\r\n");
-
 
883
                    break;
-
 
884
                case XBEE_RX_FRAME_MODEM_STATUS:
-
 
885
                    DBG_PRINT_MAIN("XBEE: parsing recieved modem status frame\r\n");
-
 
886
                    break;
-
 
887
                default:
-
 
888
                    DBG_PRINT_MAIN("??\r\n");
-
 
889
                    break;
-
 
890
            }
-
 
891
        }
-
 
892
#endif
-
 
893
 
-
 
894
    }
-
 
895
}
-
 
896
#endif
-
 
897
 
757
#if !defined(_TEST_UART) && !defined(_TEST_I2C_MASTER) && \
898
#if !defined(_TEST_UART) && !defined(_TEST_I2C_MASTER) && \
758
    !defined(_TEST_I2C_SLAVE) && !defined(_TEST_SPI) && \
899
    !defined(_TEST_I2C_SLAVE) && !defined(_TEST_SPI) && \
759
    !defined(_TEST_NFC) && !defined(_TEST_LED_BACKPACK) && \
900
    !defined(_TEST_NFC) && !defined(_TEST_LED_BACKPACK) && \
760
    !defined(_TEST_SSD1306_OLED) && !defined(_TEST_SSD1331_OLED) && \
901
    !defined(_TEST_SSD1306_OLED) && !defined(_TEST_SSD1331_OLED) && \
761
    !defined(_TEST_ADC)
902
    !defined(_TEST_ADC) && !defined(_TEST_XBEE)
762
 
903
 
763
void main(void) {
904
void main(void) {
764
    unsigned char length = 0;
905
    unsigned char length = 0;
-
 
906
 
-
 
907
    // NFC stuff
-
 
908
    NFC_FIRMWARE_VERSION version;
765
    unsigned char buffer[60];
909
    NFC_TargetDataMiFare cardData[2];
-
 
910
    NFC_TargetDataMiFare cardData_prev[2];
766
 
911
 
767
    /* --------------------- Oscillator Configuration --------------------- */
912
    /* --------------------- Oscillator Configuration --------------------- */
768
    //    OSCTUNEbits.PLLEN = 1;          // Enable 4x PLL
913
    //    OSCTUNEbits.PLLEN = 1;          // Enable 4x PLL
769
    OSCCONbits.IRCF = 0b111; // Set INTOSC postscaler to 8MHz
914
    OSCCONbits.IRCF = 0b111; // Set INTOSC postscaler to 8MHz
770
    OSCCONbits.SCS = 0b00; // Use 96MHz PLL as primary clock source
915
    OSCCONbits.SCS = 0b00; // Use 96MHz PLL as primary clock source
Line 772... Line 917...
772
 
917
 
773
    // Set all ports as digial I/O except for AN0-AN2 (pins 2-4)
918
    // Set all ports as digial I/O except for AN0-AN2 (pins 2-4)
774
    ANCON0 = 0xF8;
919
    ANCON0 = 0xF8;
775
    ANCON1 = 0x1F;
920
    ANCON1 = 0x1F;
776
 
921
 
777
    UART1_Init(); // Initialize the UART handler code
922
    UART1_Init();
778
    I2C_Init(); // Initialize the I2C handler code
923
    I2C_Init();
-
 
924
    NFC_Init();
779
    SPI2_Init(SPI2_FOSC_8); // Initialize the SPI module
925
    SPI2_Init(SPI2_FOSC_8);
780
    SSD1331_Init(); // Initialize the SSD1331 OLED display (uses SPI2)
926
    SSD1331_Init();
781
    //    NFC_Init(); // Initialize the NFC chip (uses I2C)
-
 
782
    LED_Init(); // Initialize the LED backpack (uses I2C)
-
 
783
 
927
 
784
    I2C_Configure_Master(I2C_400KHZ);
928
    I2C_Configure_Master(I2C_400KHZ);
785
    
929
 
786
    interrupt_enable(); // Enable high-priority interrupts and low-priority interrupts
930
    interrupt_enable(); // Enable high-priority interrupts and low-priority interrupts
787
    interrupt_init(); // Initialize the interrupt priorities
931
    interrupt_init(); // Initialize the interrupt priorities
788
 
932
 
789
    DBG_PRINT_MAIN("\r\nBegin Program\r\n");
933
    DBG_PRINT_MAIN("\r\nBegin Program\r\n");
790
 
934
 
791
    SSD1331_Begin();
935
    SSD1331_Begin();
792
    LED_Start();
936
    memset(cardData, 0, 24);
793
    
-
 
794
    memset(buffer, 0, 60);
937
    memset(cardData_prev, 0, 24);
795
    GFX_clearScreen();
938
    GFX_clearScreen();
796
    GFX_setRotation(3);
939
    GFX_setRotation(0);
-
 
940
    GFX_setCursor(0, 0);
-
 
941
 
-
 
942
    version = NFC_getFirmwareVersion();
-
 
943
    while (!version.IC) {
-
 
944
        GFX_appendString("Waiting for NFC board..\r");
-
 
945
        Delay10KTCYx(3);
-
 
946
        version = NFC_getFirmwareVersion();
-
 
947
    }
-
 
948
    GFX_appendString("PN5%X Ver. %d.%d\r", version.IC, version.Ver, version.Rev);
-
 
949
    NFC_SAMConfig();
797
 
950
 
798
    while (1) {
951
    while (1) {
799
 
952
 
800
        // Reads input from UART and prints to display
953
        // This query will not wait for a detection before responding
801
        length = UART1_Read_Buffer(buffer);
954
        length = NFC_pollTargets(1, 1, cardData);
802
        if (length != 0) {
955
        if (!length) {
-
 
956
            memset(cardData_prev, 0, 24);
803
            buffer[length] = 0;
957
        } else if (length == 1) {
-
 
958
            if (memcmp(&cardData[0].NFCID, &cardData_prev[0].NFCID, cardData[0].NFCID_LEN) == 0) {
-
 
959
                // Do nothing
-
 
960
            } else if (memcmp(&cardData[0].NFCID, &cardData_prev[1].NFCID, cardData[0].NFCID_LEN) == 0) {
-
 
961
                memcpy((char *) &cardData_prev[0], (const char *) &cardData[0], 12);
-
 
962
            } else {
-
 
963
                GFX_appendString("UID: %02X %02X %02X %02X\n", cardData[0].NFCID[0], cardData[0].NFCID[1], cardData[0].NFCID[2], cardData[0].NFCID[3]);
-
 
964
                memcpy((char *) &cardData_prev[0], (const char *) &cardData[0], 12);
-
 
965
            }
804
            GFX_appendString("%s", buffer);
966
            memset(&cardData_prev[1], 0, 12);
805
            LED_writeNum(length-1);
967
        } else if (length == 2) {
-
 
968
            if (memcmp(&cardData[0].NFCID, &cardData_prev[0].NFCID, cardData[0].NFCID_LEN) == 0 &&
-
 
969
                    memcmp(&cardData[1].NFCID, &cardData_prev[1].NFCID, cardData[1].NFCID_LEN) == 0) {
-
 
970
                // Do nothing
-
 
971
            } else if (memcmp(&cardData[0].NFCID, &cardData_prev[1].NFCID, cardData[0].NFCID_LEN) == 0 &&
-
 
972
                    memcmp(&cardData[1].NFCID, &cardData_prev[0].NFCID, cardData[1].NFCID_LEN) == 0) {
-
 
973
                memcpy((char *) &cardData_prev[0], (const char *) &cardData[0], 12);
-
 
974
                memcpy((char *) &cardData_prev[1], (const char *) &cardData[1], 12);
-
 
975
            } else if (memcmp(&cardData[0].NFCID, &cardData_prev[0].NFCID, cardData[0].NFCID_LEN) == 0) {
-
 
976
                // First card matched
-
 
977
                GFX_appendString("UID: %02X %02X %02X %02X\n", cardData[1].NFCID[0], cardData[1].NFCID[1], cardData[1].NFCID[2], cardData[1].NFCID[3]);
-
 
978
                memcpy(&cardData_prev[1], (const char *) &cardData[1], 12);
-
 
979
            } else if (memcmp(&cardData[1].NFCID, &cardData_prev[1].NFCID, cardData[1].NFCID_LEN) == 0) {
-
 
980
                // Second card matched
-
 
981
                GFX_appendString("UID: %02X %02X %02X %02X\n", cardData[0].NFCID[0], cardData[0].NFCID[1], cardData[0].NFCID[2], cardData[0].NFCID[3]);
-
 
982
                memcpy((char *) &cardData_prev[0], (const char *) &cardData[0], 12);
-
 
983
            } else {
-
 
984
                // No match
-
 
985
                GFX_appendString("UID: %02X %02X %02X %02X\n", cardData[0].NFCID[0], cardData[0].NFCID[1], cardData[0].NFCID[2], cardData[0].NFCID[3]);
-
 
986
                memcpy((char *) &cardData_prev[0], (const char *) &cardData[0], 12);
-
 
987
                GFX_appendString("UID: %02X %02X %02X %02X\n", cardData[1].NFCID[0], cardData[1].NFCID[1], cardData[1].NFCID[2], cardData[1].NFCID[3]);
-
 
988
                memcpy((char *) &cardData_prev[1], (const char *) &cardData[1], 12);
-
 
989
            }
806
        }
990
        }
807
    }
991
    }
808
}
992
}
809
#endif
993
#endif