Subversion Repositories Code-Repo

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
119 Kevin 1
#include "maindefs.h"
121 Kevin 2
#include "Adafruit_GFX.h"
119 Kevin 3
#include "interrupts.h"
4
#include "uart.h"
120 Kevin 5
#include "i2c.h"
121 Kevin 6
#include "spi.h"
7
#include "nfc.h"
8
#include "led_backpack.h"
9
#include "oled_ssd1306.h"
10
#include "oled_ssd1331.h"
123 Kevin 11
#include "adc.h"
127 Kevin 12
#include "xbee.h"
119 Kevin 13
#include <delays.h>
121 Kevin 14
#include <string.h>
119 Kevin 15
 
16
#pragma config WDTEN = OFF          // Turn off watchdog timer
17
#pragma config XINST = OFF          // Turn off extended instruction set
127 Kevin 18
#ifdef USE_EXT_OSC
119 Kevin 19
#pragma config OSC = HSPLL          // Use external oscillator (101)
20
#pragma config PLLDIV = 3           // Set PPL prescaler to 3 (to get 4MHz)
127 Kevin 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
119 Kevin 25
#pragma config CFGPLLEN = ON        // Enable PLL on startup
26
#pragma config PLLSEL = PLL96       // Use 96MHz PLL 4MHz -> 96MHz / 2 = 48MHz
27
//#pragma config SOSCSEL = HIGH       // High Power T1OSC/SOSC circuit selected
126 Kevin 28
//#pragma config ADCSEL = BIT12       // 12-bit ADrC
119 Kevin 29
//#pragma config IOL1WAY = OFF        // IOLOCK bit can be set and cleared as needed
30
 
31
/* ----------- IO Pins -----------
123 Kevin 32
 * RA0 - A/D X
33
 * RA1 - A/D Y
34
 * RA2 - A/D Z
119 Kevin 35
 * RA3 - 
36
 * RA4 - [CANNOT BE USED (VDDCORE/VCAP)]
37
 * RA5 - 
38
 * RA6 - Oscillator
39
 * RA7 - Oscillator
40
 * 
121 Kevin 41
 * RB0 - UART2 Tx
42
 * RB1 - UART2 Rx
43
 * RB2 - SPI2 MOSI
44
 * RB3 - SPI2 MISO
45
 * RB4 - SPI2 CLK
46
 * RB5 - SPI2 D/C
47
 * RB6 - SPI2 RESET
48
 * RB7 - SPI2 CS
119 Kevin 49
 *
121 Kevin 50
 * RC0 -
51
 * RC1 - IRQ
52
 * RC2 - Reset
53
 * RC3 - I2C CLK
54
 * RC4 - I2C DATA
55
 * RC5 -
56
 * RC6 - UART1 Tx
57
 * RC7 - UART1 Rx
119 Kevin 58
 * ---------------------------- */
59
 
121 Kevin 60
#ifdef _TEST_UART
119 Kevin 61
 
62
void main(void) {
121 Kevin 63
    unsigned char length = 0;
64
    unsigned char buffer[100];
65
 
66
    /* --------------------- Oscillator Configuration --------------------- */
67
    //    OSCTUNEbits.PLLEN = 1;          // Enable 4x PLL
68
    OSCCONbits.IRCF = 0b111; // Set INTOSC postscaler to 8MHz
69
    OSCCONbits.SCS = 0b00; // Use 96MHz PLL as primary clock source
70
    /* -------------------------------------------------------------------- */
71
 
72
    // Set all ports as digial I/O
73
    ANCON0 = 0xFF;
74
    ANCON1 = 0x1F;
75
 
76
    UART1_Init(); // Initialize the UART handler code
77
 
78
    interrupt_enable(); // Enable high-priority interrupts and low-priority interrupts
79
    interrupt_init(); // Initialize the interrupt priorities
80
 
81
    DBG_PRINT_MAIN("\r\nBegin Program\r\n");
82
 
83
    while (1) {
84
 
127 Kevin 85
        length = UART1_Read_Buffer((unsigned char *) buffer);
121 Kevin 86
        if (length != 0) {
87
            UART1_WriteB((char *) buffer, length);
88
        }
89
 
90
        Delay10KTCYx(255);
91
        Delay10KTCYx(255);
92
    }
93
}
94
#endif
95
 
96
#ifdef _TEST_I2C_MASTER
97
 
98
void main(void) {
119 Kevin 99
    unsigned char i = 0;
100
    unsigned char length = 0;
120 Kevin 101
    unsigned char result = 0;
119 Kevin 102
    unsigned char buffer[100];
121 Kevin 103
 
119 Kevin 104
    /* --------------------- Oscillator Configuration --------------------- */
121 Kevin 105
    //    OSCTUNEbits.PLLEN = 1;          // Enable 4x PLL
106
    OSCCONbits.IRCF = 0b111; // Set INTOSC postscaler to 8MHz
119 Kevin 107
    OSCCONbits.SCS = 0b00; // Use 96MHz PLL as primary clock source
108
    /* -------------------------------------------------------------------- */
109
 
110
    // Set all ports as digial I/O
111
    ANCON0 = 0xFF;
112
    ANCON1 = 0x1F;
113
 
114
    UART1_Init(); // Initialize the UART handler code
120 Kevin 115
    I2C_Init(); // Initialize the I2C handler code
121 Kevin 116
 
117
    I2C_Configure_Master();
118
 
119 Kevin 119
    interrupt_enable(); // Enable high-priority interrupts and low-priority interrupts
120
    interrupt_init(); // Initialize the interrupt priorities
121
 
122
    DBG_PRINT_MAIN("\r\nBegin Program\r\n");
123
 
121 Kevin 124
    while (1) {
125
 
126
        buffer[0] = 0xBB;
127
 
128
        I2C_Master_Send(0x30, 1, buffer);
129
        result = I2C_Get_Status();
130
        while (!result) {
131
            result = I2C_Get_Status();
132
        }
133
        DBG_PRINT_MAIN("S:%X ", result);
134
 
135
        I2C_Master_Recv(0x30, 2);
136
        result = I2C_Get_Status();
137
        while (!result) {
138
            result = I2C_Get_Status();
139
        }
140
        DBG_PRINT_MAIN("S:%X ", result);
141
        length = I2C_Read_Buffer(buffer);
142
        DBG_PRINT_MAIN("L:%d D:", length);
143
        for (i = 0; i < length; i++) {
144
            DBG_PRINT_MAIN("%c ", buffer[i]);
145
        }
146
 
147
        I2C_Master_Restart(0x30, 0xBB, 2);
148
        result = I2C_Get_Status();
149
        while (!result) {
150
            result = I2C_Get_Status();
151
        }
152
        DBG_PRINT_MAIN("S:%X ", result);
153
        length = I2C_Read_Buffer(buffer);
154
        DBG_PRINT_MAIN("L:%d D:", length);
155
        for (i = 0; i < length; i++) {
156
            DBG_PRINT_MAIN("%c ", buffer[i]);
157
        }
158
 
159
        DBG_PRINT_MAIN("\r\n");
160
 
161
        Delay10KTCYx(255);
162
        Delay10KTCYx(255);
163
    }
164
}
165
#endif
166
 
167
#ifdef _TEST_I2C_SLAVE
168
 
169
void main(void) {
170
    unsigned char i = 0;
171
    unsigned char length = 0;
172
    unsigned char result = 0;
173
    unsigned char buffer[100];
174
 
175
    /* --------------------- Oscillator Configuration --------------------- */
176
    //    OSCTUNEbits.PLLEN = 1;          // Enable 4x PLL
177
    OSCCONbits.IRCF = 0b111; // Set INTOSC postscaler to 8MHz
178
    OSCCONbits.SCS = 0b00; // Use 96MHz PLL as primary clock source
179
    /* -------------------------------------------------------------------- */
180
 
181
    // Set all ports as digial I/O
182
    ANCON0 = 0xFF;
183
    ANCON1 = 0x1F;
184
 
185
    UART1_Init(); // Initialize the UART handler code
186
    I2C_Init(); // Initialize the I2C handler code
187
 
120 Kevin 188
    I2C_Configure_Slave(0x30);
189
 
121 Kevin 190
    interrupt_enable(); // Enable high-priority interrupts and low-priority interrupts
191
    interrupt_init(); // Initialize the interrupt priorities
192
 
193
    DBG_PRINT_MAIN("\r\nBegin Program\r\n");
194
 
119 Kevin 195
    while (1) {
120 Kevin 196
 
197
        result = I2C_Get_Status();
198
        while (!result) {
199
            result = I2C_Get_Status();
119 Kevin 200
        }
120 Kevin 201
        DBG_PRINT_MAIN("%X ", result);
202
        length = I2C_Read_Buffer(buffer);
203
        DBG_PRINT_MAIN("%d ", length);
204
        for (i = 0; i < length; i++) {
205
            DBG_PRINT_MAIN("%X ", buffer[i]);
206
        }
207
        DBG_PRINT_MAIN("\r\n");
121 Kevin 208
 
209
        Delay10KTCYx(255);
210
        Delay10KTCYx(255);
119 Kevin 211
    }
121 Kevin 212
}
213
#endif
214
 
215
#ifdef _TEST_NFC
216
 
217
void main(void) {
126 Kevin 218
    unsigned char i, length = 0;
121 Kevin 219
 
220
    // NFC stuff
126 Kevin 221
    NFC_FIRMWARE_VERSION version;
222
    NFC_TargetDataMiFare cardData[2];
223
    NFC_TargetDataMiFare cardData_prev[2];
121 Kevin 224
 
225
    /* --------------------- Oscillator Configuration --------------------- */
226
    //    OSCTUNEbits.PLLEN = 1;          // Enable 4x PLL
227
    OSCCONbits.IRCF = 0b111; // Set INTOSC postscaler to 8MHz
228
    OSCCONbits.SCS = 0b00; // Use 96MHz PLL as primary clock source
229
    /* -------------------------------------------------------------------- */
230
 
231
    // Set all ports as digial I/O
232
    ANCON0 = 0xFF;
233
    ANCON1 = 0x1F;
234
 
235
    UART1_Init(); // Initialize the UART handler code
236
    I2C_Init(); // Initialize the I2C handler code
237
    NFC_Init(); // Initialize the NFC chip (uses I2C)
238
 
239
    I2C_Configure_Master(I2C_400KHZ);
240
 
241
    interrupt_enable(); // Enable high-priority interrupts and low-priority interrupts
242
    interrupt_init(); // Initialize the interrupt priorities
243
 
244
    DBG_PRINT_MAIN("\r\nBegin Program\r\n");
245
 
246
    version = NFC_getFirmwareVersion();
247
    while (!version.IC) {
248
        DBG_PRINT_MAIN("Waiting for NFC board..\r\n");
249
        Delay10KTCYx(3);
250
        version = NFC_getFirmwareVersion();
251
    }
252
    DBG_PRINT_MAIN("Found chip PN5%X\r\n", version.IC);
253
    DBG_PRINT_MAIN("Firmware ver. %d.%d\r\n", version.Ver, version.Rev);
254
    NFC_SAMConfig();
255
 
126 Kevin 256
    memset(cardData, 0, 24);
127 Kevin 257
 
121 Kevin 258
    while (1) {
126 Kevin 259
 
260
//        // This query will hang until the NFC chip replies (card detected)
261
//        length = NFC_readPassiveTargetID(cardData);
262
//        if (length) {
263
//            DBG_PRINT_MAIN("Cards Found: %u\r\n", length);
264
//            DBG_PRINT_MAIN("UID Length: %d bytes\r\n", cardData[0].NFCID_LEN);
265
//            DBG_PRINT_MAIN("UID: ");
266
//            for (i = 0; i < cardData[0].NFCID_LEN; i++) {
267
//                DBG_PRINT_MAIN("%02X ", cardData[0].NFCID[i]);
268
//            }
269
//            DBG_PRINT_MAIN("\r\n");
270
//            if (length == 2) {
271
//                DBG_PRINT_MAIN("UID Length: %d bytes\r\n", cardData[1].NFCID_LEN);
272
//                DBG_PRINT_MAIN("UID: ");
273
//                for (i = 0; i < cardData[1].NFCID_LEN; i++) {
274
//                    DBG_PRINT_MAIN("%02X ", cardData[1].NFCID[i]);
275
//                }
276
//                DBG_PRINT_MAIN("\r\n");
277
//            }
278
//        }
121 Kevin 279
 
127 Kevin 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
 
126 Kevin 300
        // This query will not wait for a detection before responding
301
        length = NFC_pollTargets(1, 1, cardData);
302
        if (!length) {
303
            memset(cardData_prev, 0, 24);
304
        } else if (length == 1) {
305
            if (memcmp(&cardData[0].NFCID, &cardData_prev[0].NFCID, cardData[0].NFCID_LEN) == 0) {
306
                // Do nothing
307
            } else if (memcmp(&cardData[0].NFCID, &cardData_prev[1].NFCID, cardData[0].NFCID_LEN) == 0) {
127 Kevin 308
                memcpy((char *) &cardData_prev[0], (const char *) &cardData[0], 12);
126 Kevin 309
            } else {
310
                DBG_PRINT_MAIN("UID: ");
311
                for (i = 0; i < cardData[0].NFCID_LEN; i++) {
312
                    DBG_PRINT_MAIN("%02X ", cardData[0].NFCID[i]);
313
                }
314
                DBG_PRINT_MAIN("\r\n");
127 Kevin 315
                memcpy((char *) &cardData_prev[0], (const char *) &cardData[0], 12);
121 Kevin 316
            }
126 Kevin 317
            memset(&cardData_prev[1], 0, 12);
318
        } else if (length == 2) {
319
            if (memcmp(&cardData[0].NFCID, &cardData_prev[0].NFCID, cardData[0].NFCID_LEN) == 0 &&
320
                    memcmp(&cardData[1].NFCID, &cardData_prev[1].NFCID, cardData[1].NFCID_LEN) == 0) {
321
                // Do nothing
322
            } else if (memcmp(&cardData[0].NFCID, &cardData_prev[1].NFCID, cardData[0].NFCID_LEN) == 0 &&
323
                    memcmp(&cardData[1].NFCID, &cardData_prev[0].NFCID, cardData[1].NFCID_LEN) == 0) {
127 Kevin 324
                memcpy((char *) &cardData_prev[0], (const char *) &cardData[0], 12);
325
                memcpy((char *) &cardData_prev[1], (const char *) &cardData[1], 12);
126 Kevin 326
            } else if (memcmp(&cardData[0].NFCID, &cardData_prev[0].NFCID, cardData[0].NFCID_LEN) == 0) {
327
                // First card matched
328
                DBG_PRINT_MAIN("UID2: ");
329
                for (i = 0; i < cardData[1].NFCID_LEN; i++) {
330
                    DBG_PRINT_MAIN("%02X ", cardData[1].NFCID[i]);
331
                }
332
                DBG_PRINT_MAIN("\r\n");
127 Kevin 333
                memcpy(&cardData_prev[1], (const char *) &cardData[1], 12);
126 Kevin 334
            } else if (memcmp(&cardData[1].NFCID, &cardData_prev[1].NFCID, cardData[1].NFCID_LEN) == 0) {
335
                // Second card matched
336
                DBG_PRINT_MAIN("UID1: ");
337
                for (i = 0; i < cardData[0].NFCID_LEN; i++) {
338
                    DBG_PRINT_MAIN("%02X ", cardData[0].NFCID[i]);
339
                }
340
                DBG_PRINT_MAIN("\r\n");
127 Kevin 341
                memcpy((char *) &cardData_prev[0], (const char *) &cardData[0], 12);
126 Kevin 342
            } else {
343
                // No match
344
                DBG_PRINT_MAIN("UID1: ");
345
                for (i = 0; i < cardData[0].NFCID_LEN; i++) {
346
                    DBG_PRINT_MAIN("%02X ", cardData[0].NFCID[i]);
347
                }
348
                DBG_PRINT_MAIN("\r\n");
127 Kevin 349
                memcpy((char *) &cardData_prev[0], (const char *) &cardData[0], 12);
126 Kevin 350
                DBG_PRINT_MAIN("UID2: ");
351
                for (i = 0; i < cardData[1].NFCID_LEN; i++) {
352
                    DBG_PRINT_MAIN("%02X ", cardData[1].NFCID[i]);
353
                }
354
                DBG_PRINT_MAIN("\r\n");
127 Kevin 355
                memcpy((char *) &cardData_prev[1], (const char *) &cardData[1], 12);
126 Kevin 356
            }
121 Kevin 357
        }
358
    }
359
}
360
#endif
361
 
362
#ifdef _TEST_LED_BACKPACK
363
 
364
void main(void) {
365
    unsigned char i = 0;
366
    unsigned char length = 0;
367
    unsigned char buffer[100];
368
    unsigned int counter = 0;
369
 
370
    /* --------------------- Oscillator Configuration --------------------- */
371
    //    OSCTUNEbits.PLLEN = 1;          // Enable 4x PLL
372
    OSCCONbits.IRCF = 0b111; // Set INTOSC postscaler to 8MHz
373
    OSCCONbits.SCS = 0b00; // Use 96MHz PLL as primary clock source
374
    /* -------------------------------------------------------------------- */
375
 
376
    // Set all ports as digial I/O
377
    ANCON0 = 0xFF;
378
    ANCON1 = 0x1F;
379
 
380
    UART1_Init(); // Initialize the UART handler code
381
    I2C_Init(); // Initialize the I2C handler code
382
    LED_Init(); // Initialize the LED backpack (uses I2C);
383
 
384
    I2C_Configure_Master(I2C_400KHZ);
385
 
386
    interrupt_enable(); // Enable high-priority interrupts and low-priority interrupts
387
    interrupt_init(); // Initialize the interrupt priorities
388
 
389
    DBG_PRINT_MAIN("\r\nBegin Program\r\n");
390
 
391
    LED_Start();
392
    LED_writeDigitNum(0, 1, 1);
393
    LED_writeDigitNum(1, 2, 0);
394
    LED_writeDigitNum(2, 3, 0);
395
    LED_writeDigitNum(3, 4, 0);
396
    LED_writeDisplay();
397
    for (i = 0; i < 15; i++) {
398
        LED_setBrightness(15 - i);
399
        Delay10KTCYx(100);
400
    }
401
    for (i = 0; i < 15; i++) {
402
        LED_setBrightness(i);
403
        Delay10KTCYx(100);
404
    }
405
    LED_blinkRate(HT16K33_BLINK_OFF);
406
 
407
    while (1) {
408
        LED_writeNum(counter);
409
        counter++;
410
        if (counter > 9999)
411
            counter = 0;
412
 
413
        //        Delay10KTCYx(255);
414
    }
415
}
416
#endif
417
 
418
#ifdef _TEST_SPI
419
 
420
void main(void) {
421
    unsigned char i = 0;
422
    unsigned char length = 0;
423
    unsigned char result = 0;
424
    unsigned char buffer[100];
425
    unsigned char test[8] = "ASDF123";
426
 
427
    /* --------------------- Oscillator Configuration --------------------- */
428
    //    OSCTUNEbits.PLLEN = 1;          // Enable 4x PLL
429
    OSCCONbits.IRCF = 0b111; // Set INTOSC postscaler to 8MHz
430
    OSCCONbits.SCS = 0b00; // Use 96MHz PLL as primary clock source
431
    /* -------------------------------------------------------------------- */
432
 
433
    // Set all ports as digial I/O
434
    ANCON0 = 0xFF;
435
    ANCON1 = 0x1F;
436
 
437
    UART1_Init(); // Initialize the UART handler code
438
    SPI2_Init(SPI2_FOSC_8); // Initialize the SPI module
439
 
440
    interrupt_enable(); // Enable high-priority interrupts and low-priority interrupts
441
    interrupt_init(); // Initialize the interrupt priorities
442
 
443
    DBG_PRINT_MAIN("\r\nBegin Program\r\n");
444
 
445
    while (1) {
446
 
447
        SPI2_Write(test, 7);
448
        while (result != 7) {
449
            length = SPI2_Buffer_Read(buffer);
450
            if (length) {
451
                result += length;
452
            }
453
        }
454
        result = 0;
455
 
456
        for (i = 0; i < result; i++) {
457
            DBG_PRINT_MAIN("%X ", buffer[i]);
458
        }
459
        DBG_PRINT_MAIN("\r\n");
460
 
461
        Delay10KTCYx(255);
462
        Delay10KTCYx(255);
463
    }
464
}
465
#endif
466
 
467
#ifdef _TEST_SSD1306_OLED
468
 
469
void main(void) {
470
    unsigned int i = 0;
471
    unsigned long l = 0;
472
 
473
    /* --------------------- Oscillator Configuration --------------------- */
474
    //    OSCTUNEbits.PLLEN = 1;          // Enable 4x PLL
475
    OSCCONbits.IRCF = 0b111; // Set INTOSC postscaler to 8MHz
476
    OSCCONbits.SCS = 0b00; // Use 96MHz PLL as primary clock source
477
    /* -------------------------------------------------------------------- */
478
 
479
    // Set all ports as digial I/O
480
    ANCON0 = 0xFF;
481
    ANCON1 = 0x1F;
482
 
483
    UART1_Init(); // Initialize the UART handler code
484
    SPI2_Init(SPI2_FOSC_8); // Initialize the SPI module
485
    SSD1306_Init(); // Initialize the OLED code
486
 
487
    interrupt_enable(); // Enable high-priority interrupts and low-priority interrupts
488
    interrupt_init(); // Initialize the interrupt priorities
489
 
490
    DBG_PRINT_MAIN("\r\nBegin Program\r\n");
491
 
492
    SSD1306_Begin(SSD1306_SWITCHCAPVCC);
493
 
494
    SSD1306_Display(); // Show splashscreen
495
 
496
    while (1) {
497
        //        Delay10KTCYx(255);
498
        //        Delay10KTCYx(255);
499
        //        SSD1306_Clear_Display();
500
        //        for (i = 0; i < 32; i++) {
501
        //            SSD1306_Draw_Pixel(i, i, WHITE);
502
        //        }
503
        //        SSD1306_Display();
504
 
505
        //        Delay10KTCYx(255);
506
        //        Delay10KTCYx(255);
507
        //        SSD1306_Clear_Display();
508
        //        SSD1306_Test_DrawLine();
509
        //        SSD1306_Display();
510
        //
511
        //        Delay10KTCYx(255);
512
        //        Delay10KTCYx(255);
513
        //        SSD1306_Clear_Display();
514
        //        SSD1306_Test_DrawRect();
515
        //        SSD1306_Display();
516
        //
517
        //        Delay10KTCYx(255);
518
        //        Delay10KTCYx(255);
519
        //        SSD1306_Clear_Display();
520
        //        SSD1306_Test_FillRect();
521
        //        SSD1306_Display();
522
        //
523
        //        Delay10KTCYx(255);
524
        //        Delay10KTCYx(255);
525
        //        SSD1306_Clear_Display();
526
        //        SSD1306_Test_DrawCircle();
527
        //        SSD1306_Display();
528
        //
529
        //        Delay10KTCYx(255);
530
        //        Delay10KTCYx(255);
531
        //        SSD1306_Clear_Display();
532
        //        GFX_fillCircle(GFX_width() / 2, GFX_height() / 2, 10, WHITE);
533
        //        SSD1306_Display();
534
        //
535
        //        Delay10KTCYx(255);
536
        //        Delay10KTCYx(255);
537
        //        SSD1306_Clear_Display();
538
        //        SSD1306_Test_DrawRoundRect();
539
        //        SSD1306_Display();
540
        //
541
        //        Delay10KTCYx(255);
542
        //        Delay10KTCYx(255);
543
        //        SSD1306_Clear_Display();
544
        //        SSD1306_Test_FillRoundRect();
545
        //        SSD1306_Display();
546
        //
547
        //        Delay10KTCYx(255);
548
        //        Delay10KTCYx(255);
549
        //        SSD1306_Clear_Display();
550
        //        SSD1306_Test_DrawTriangle();
551
        //        SSD1306_Display();
552
        //
553
        //        Delay10KTCYx(255);
554
        //        Delay10KTCYx(255);
555
        //        SSD1306_Clear_Display();
556
        //        SSD1306_Test_FillTriangle();
557
        //        SSD1306_Display();
558
 
559
        //        Delay10KTCYx(255);
560
        //        Delay10KTCYx(255);
561
        //        SSD1306_Clear_Display();
562
        //        SSD1306_Test_DrawChar();
563
        //        SSD1306_Display();
564
 
565
        //        Delay10KTCYx(255);
566
        //        Delay10KTCYx(255);
567
        //        SSD1306_Clear_Display();
568
        //        GFX_setTextSize(1);
569
        //        GFX_setTextColor(WHITE);
570
        //        GFX_setCursor(0,0);
571
        //        GFX_writeString("Hello World!\n");
572
        ////        GFX_setTextColorBG(BLACK, WHITE);
573
        //        i = 65535;
574
        //        GFX_writeString("%u %d\n", i, i);
575
        ////        GFX_setTextSize(2);
576
        ////        GFX_setTextColor(WHITE);
577
        //        l = 0xDEADBEEF;
578
        //        GFX_writeString("0x%X", (long)l);
579
        //        SSD1306_Display();
580
 
581
        SSD1306_Clear_Display();
582
        GFX_setRotation(0);
583
        GFX_setTextSize(1);
584
        GFX_setTextColor(SSD1306_WHITE);
585
        GFX_setCursor(0, 0);
586
        GFX_writeString("%u", i);
587
        i++;
588
        SSD1306_Display();
589
 
590
    }
591
}
592
#endif
593
 
594
#ifdef _TEST_SSD1331_OLED
595
 
596
void main(void) {
597
    unsigned int i = 0;
598
 
599
    /* --------------------- Oscillator Configuration --------------------- */
600
    //    OSCTUNEbits.PLLEN = 1;          // Enable 4x PLL
601
    OSCCONbits.IRCF = 0b111; // Set INTOSC postscaler to 8MHz
602
    OSCCONbits.SCS = 0b00; // Use 96MHz PLL as primary clock source
603
    /* -------------------------------------------------------------------- */
604
 
605
    // Set all ports as digial I/O
606
    ANCON0 = 0xFF;
607
    ANCON1 = 0x1F;
608
 
609
    UART1_Init(); // Initialize the UART handler code
610
    SPI2_Init(SPI2_FOSC_8); // Initialize the SPI module
611
    SSD1331_Init(); // Initialize the OLED code
612
 
613
    interrupt_enable(); // Enable high-priority interrupts and low-priority interrupts
614
    interrupt_init(); // Initialize the interrupt priorities
615
 
616
    DBG_PRINT_MAIN("\r\nBegin Program\r\n");
617
 
618
    SSD1331_Begin();
619
 
620
    while (1) {
621
 
622
        Delay10KTCYx(255);
623
        Delay10KTCYx(255);
624
        GFX_setRotation(0);
625
        SSD1331_Test_Pattern();
626
 
627
        Delay10KTCYx(255);
628
        Delay10KTCYx(255);
629
        GFX_clearScreen();
630
        GFX_setRotation(0);
631
        GFX_setCursor(0, 0);
632
        GFX_writeString("Lorem ipsum dolor sit amet, consectetur adipiscing elit. Curabitur adipiscing ante sed nibh tincidunt feugiat. Maecenas enim massa");
633
 
634
        Delay10KTCYx(255);
635
        Delay10KTCYx(255);
636
        GFX_clearScreen();
637
        GFX_setRotation(3);
638
        GFX_setCursor(0, 0);
639
        GFX_writeString("Lorem ipsum dolor sit amet, consectetur adipiscing elit. Curabitur adipiscing ante sed nibh tincidunt feugiat. Maecenas enim massa");
640
 
641
        Delay10KTCYx(255);
642
        Delay10KTCYx(255);
643
        GFX_setRotation(0);
644
        SSD1331_Test_DrawLines(SSD1331_YELLOW);
645
 
646
        Delay10KTCYx(255);
647
        Delay10KTCYx(255);
648
        GFX_setRotation(3);
649
        SSD1331_Test_DrawLines(SSD1331_BLUE);
650
 
651
        Delay10KTCYx(255);
652
        Delay10KTCYx(255);
653
        GFX_setRotation(0);
654
        SSD1331_Test_DrawRect(SSD1331_GREEN);
655
 
656
        Delay10KTCYx(255);
657
        Delay10KTCYx(255);
658
        GFX_setRotation(1);
659
        SSD1331_Test_DrawRect(SSD1331_RED);
660
 
661
        Delay10KTCYx(255);
662
        Delay10KTCYx(255);
663
        GFX_setRotation(2);
664
        SSD1331_Test_DrawRect(SSD1331_BLUE);
665
 
666
        Delay10KTCYx(255);
667
        Delay10KTCYx(255);
668
        GFX_setRotation(3);
669
        SSD1331_Test_DrawRect(SSD1331_YELLOW);
670
 
671
        Delay10KTCYx(255);
672
        Delay10KTCYx(255);
673
        GFX_setRotation(0);
674
        SSD1331_Test_FillRect(SSD1331_YELLOW, SSD1331_MAGENTA);
675
 
676
        Delay10KTCYx(255);
677
        Delay10KTCYx(255);
678
        GFX_setRotation(3);
679
        SSD1331_Test_FillRect(SSD1331_BLUE, SSD1331_GREEN);
680
 
681
        Delay10KTCYx(255);
682
        Delay10KTCYx(255);
683
        GFX_setRotation(0);
684
        GFX_clearScreen();
685
        SSD1331_Test_FillCircle(10, SSD1331_BLUE);
686
        SSD1331_Test_DrawCircle(10, SSD1331_WHITE);
687
 
688
        Delay10KTCYx(255);
689
        Delay10KTCYx(255);
690
        GFX_setRotation(3);
691
        GFX_clearScreen();
692
        SSD1331_Test_FillCircle(10, SSD1331_MAGENTA);
693
        SSD1331_Test_DrawCircle(10, SSD1331_YELLOW);
694
 
695
        Delay10KTCYx(255);
696
        Delay10KTCYx(255);
697
        GFX_setRotation(0);
698
        SSD1331_Test_DrawTria();
699
 
700
        Delay10KTCYx(255);
701
        Delay10KTCYx(255);
702
        GFX_setRotation(3);
703
        SSD1331_Test_DrawTria();
704
 
705
        Delay10KTCYx(255);
706
        Delay10KTCYx(255);
707
        GFX_setRotation(0);
708
        SSD1331_Test_DrawRoundRect();
709
 
710
        Delay10KTCYx(255);
711
        Delay10KTCYx(255);
712
        GFX_setRotation(3);
713
        SSD1331_Test_DrawRoundRect();
714
 
715
        //        GFX_clearScreen();
716
        //        GFX_setRotation(3);
717
        //        GFX_setCursor(0,0);
718
        //        GFX_setTextColorBG(SSD1331_WHITE, SSD1331_BLACK);
719
        //        GFX_writeString("%u", i);
720
        //        i++;
721
    }
722
}
723
#endif
724
 
123 Kevin 725
#ifdef _TEST_ADC
726
 
727
void main(void) {
126 Kevin 728
    unsigned int x, y, z;
123 Kevin 729
    unsigned char buffer[60];
730
 
731
    /* --------------------- Oscillator Configuration --------------------- */
732
    //    OSCTUNEbits.PLLEN = 1;          // Enable 4x PLL
733
    OSCCONbits.IRCF = 0b111; // Set INTOSC postscaler to 8MHz
734
    OSCCONbits.SCS = 0b00; // Use 96MHz PLL as primary clock source
735
    /* -------------------------------------------------------------------- */
736
 
737
    // Set all ports as digial I/O except for AN0-AN2 (pins 2-4)
738
    ANCON0 = 0xF8;
739
    ANCON1 = 0x1F;
740
 
741
    UART1_Init(); // Initialize the UART handler code
742
    SPI2_Init(SPI2_FOSC_8); // Initialize the SPI module
743
    SSD1331_Init(); // Initialize the SSD1331 OLED display (uses SPI2)
126 Kevin 744
    ADC_Init(ADC_TAD_20, ADC_FOSC_64);
123 Kevin 745
 
746
    //    I2C_Configure_Master(I2C_400KHZ);
747
    SSD1331_Begin();
748
 
749
    interrupt_enable(); // Enable high-priority interrupts and low-priority interrupts
750
    interrupt_init(); // Initialize the interrupt priorities
751
 
752
    DBG_PRINT_MAIN("\r\nBegin Program\r\n");
753
 
754
    memset(buffer, 0, 60);
755
    GFX_clearScreen();
756
    GFX_setRotation(3);
757
 
758
    while (1) {
759
        // ADC read from AN0-AN2 and prints to display
127 Kevin 760
        ADC_Start(ADC_CHANNEL_AN2);
126 Kevin 761
        GFX_fillRect(0, 0, GFX_width(), 8, SSD1331_BLACK);
762
        GFX_setCursor(0, 0);
123 Kevin 763
        while (!ADC_Get_Result(&x));
764
        GFX_writeString("X: %u", x);
765
 
766
        ADC_Start(ADC_CHANNEL_AN1);
126 Kevin 767
        GFX_fillRect(0, 8, GFX_width(), 8, SSD1331_BLACK);
768
        GFX_setCursor(0, 8);
123 Kevin 769
        while (!ADC_Get_Result(&y));
770
        GFX_writeString("Y: %u", y);
771
 
127 Kevin 772
        ADC_Start(ADC_CHANNEL_AN0);
126 Kevin 773
        GFX_fillRect(0, 16, GFX_width(), 8, SSD1331_BLACK);
774
        GFX_setCursor(0, 16);
123 Kevin 775
        while (!ADC_Get_Result(&z));
776
        GFX_writeString("Z: %u", z);
777
    }
778
}
779
 
780
#endif
781
 
127 Kevin 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
 
121 Kevin 898
#if !defined(_TEST_UART) && !defined(_TEST_I2C_MASTER) && \
899
    !defined(_TEST_I2C_SLAVE) && !defined(_TEST_SPI) && \
900
    !defined(_TEST_NFC) && !defined(_TEST_LED_BACKPACK) && \
123 Kevin 901
    !defined(_TEST_SSD1306_OLED) && !defined(_TEST_SSD1331_OLED) && \
127 Kevin 902
    !defined(_TEST_ADC) && !defined(_TEST_XBEE)
121 Kevin 903
 
904
void main(void) {
905
    unsigned char length = 0;
906
 
127 Kevin 907
    // NFC stuff
908
    NFC_FIRMWARE_VERSION version;
909
    NFC_TargetDataMiFare cardData[2];
910
    NFC_TargetDataMiFare cardData_prev[2];
911
 
121 Kevin 912
    /* --------------------- Oscillator Configuration --------------------- */
913
    //    OSCTUNEbits.PLLEN = 1;          // Enable 4x PLL
914
    OSCCONbits.IRCF = 0b111; // Set INTOSC postscaler to 8MHz
915
    OSCCONbits.SCS = 0b00; // Use 96MHz PLL as primary clock source
916
    /* -------------------------------------------------------------------- */
917
 
123 Kevin 918
    // Set all ports as digial I/O except for AN0-AN2 (pins 2-4)
919
    ANCON0 = 0xF8;
121 Kevin 920
    ANCON1 = 0x1F;
921
 
127 Kevin 922
    UART1_Init();
923
    I2C_Init();
924
    NFC_Init();
925
    SPI2_Init(SPI2_FOSC_8);
926
    SSD1331_Init();
121 Kevin 927
 
126 Kevin 928
    I2C_Configure_Master(I2C_400KHZ);
127 Kevin 929
 
121 Kevin 930
    interrupt_enable(); // Enable high-priority interrupts and low-priority interrupts
931
    interrupt_init(); // Initialize the interrupt priorities
932
 
933
    DBG_PRINT_MAIN("\r\nBegin Program\r\n");
934
 
126 Kevin 935
    SSD1331_Begin();
127 Kevin 936
    memset(cardData, 0, 24);
937
    memset(cardData_prev, 0, 24);
122 Kevin 938
    GFX_clearScreen();
127 Kevin 939
    GFX_setRotation(0);
940
    GFX_setCursor(0, 0);
122 Kevin 941
 
127 Kevin 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();
950
 
121 Kevin 951
    while (1) {
952
 
127 Kevin 953
        // This query will not wait for a detection before responding
954
        length = NFC_pollTargets(1, 1, cardData);
955
        if (!length) {
956
            memset(cardData_prev, 0, 24);
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
            }
966
            memset(&cardData_prev[1], 0, 12);
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
            }
126 Kevin 990
        }
121 Kevin 991
    }
992
}
993
#endif