Subversion Repositories Code-Repo

Rev

Rev 111 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
107 Kevin 1
#include "maindefs.h"
2
#include "msg_queues.h"
3
#include "interrupts.h"
4
#include "uart.h"
5
#include "i2c.h"
6
#include "adc.h"
7
#include "timers.h"
8
#include "xbee.h"
9
#include "led_driver.h"
10
#include "pwm.h"
11
#include "delays.h"
12
#include "pin_interrupts.h"
111 Kevin 13
#include "buffer.h"
107 Kevin 14
 
15
#pragma config WDTEN = OFF          // Turn off watchdog timer
16
#pragma config XINST = OFF          // Turn off extended instruction set
17
#pragma config OSC = HSPLL          // Use external oscillator (101)
18
#pragma config PLLDIV = 3           // Set PPL prescaler to 3 (to get 4MHz)
19
#pragma config CFGPLLEN = ON        // Enable PLL on startup
20
#pragma config PLLSEL = PLL96       // Use 96MHz PLL 4MHz -> 96MHz / 2 = 48MHz
21
//#pragma config SOSCSEL = HIGH       // High Power T1OSC/SOSC circuit selected
22
#pragma config ADCSEL = BIT12       // 12-bit ADrC
23
#pragma config IOL1WAY = OFF        // IOLOCK bit can be set and cleared as needed
24
 
25
/* ----------- IO Pins -----------
26
 * RA0 - LED Display Latch Enable (PPS)
27
 * RA1 - LED Display CLK (PPS)
28
 * RA2 - LED Display DIN (PPS)
29
 * RA3 - LED Display Output Enable (PPS)
30
 * RA4 - [CANNOT BE USED (VDDCORE/VCAP)]
31
 * RA5 - IR Reciever (PPS)
32
 * RA6 - Oscillator
33
 * RA7 - Oscillator
34
 * 
35
 * RC0 - PWM Output (IR) (PPS, Ports B and C only)
36
 * RC1 - PWM Output (IR) (PPS, Ports B and C only)
37
 * RC2 - LED Output (PPS, Ports B and C only)
38
 * RC3 - I2C SCL
39
 * RC4 - I2C SDA
40
 * RC5 - XBee Sleep (PPS)
41
 * RC6 - UART Debug Output
42
 * RC7 - UART Debug Input
43
 *
44
 * RB0 - XBee CTS (PPS)
45
 * RB1 - XBee RTS (PPS)
111 Kevin 46
 * RB2 - XBee Tx (PPS)
47
 * RB3 - XBee Rx (PPS)
107 Kevin 48
 * RB4 - Button Input (Port B Interrupt on Change)
49
 * RB5 - Button Input (Port B Interrupt on Change)
50
 * RB6 - Button Input (Port B Interrupt on Change)
51
 * RB7 - Button Input (Port B Interrupt on Change)
52
 * ---------------------------- */
53
 
54
#pragma udata umain_1
55
static unsigned char msgbuffer[MSGLEN + 1];
56
#pragma udata umain_2
111 Kevin 57
static I2C_DATA i2c_data;
107 Kevin 58
#pragma udata
111 Kevin 59
static XBEE_DATA xbee_data;
60
static UART_DATA uart_data;
61
static BUFFER_DATA buffer_data;
107 Kevin 62
 
63
void main(void) {
64
    char length;
65
    unsigned char msgtype;
66
    unsigned char i = 0;
111 Kevin 67
    unsigned char counter = 0;
112 Kevin 68
    enum I2C_STATE i2c_state = I2C_STATE_IDLE;
69
    unsigned char i2c_bytes_to_read = 0;
70
 
107 Kevin 71
    // Pointers to allow parsing of xbee data from arbitrary byte array
72
    XBEE_RX_AT_COMMAND_RESPONSE_FRAME *frame_at_cmd_response;
73
    XBEE_RX_DATA_PACKET_FRAME *frame_data_packet;
74
    XBEE_RX_DATA_TX_STATUS_FRAME *frame_tx_status;
75
    XBEE_RX_IO_DATA_SAMPLE_FRAME *frame_io_sample;
76
    XBEE_RX_EXPLICIT_COMMAND_FRAME *frame_explicit_cmd;
77
    XBEE_RX_REMOTE_AT_COMMAND_FRAME *frame_remote_at_cmd;
78
    XBEE_RX_ROUTE_RECORD_FRAME *frame_route_record;
79
    XBEE_RX_NODE_IDENTIFICATION_INDICATOR_FRAME *frame_node_identification;
80
    XBEE_RX_MODEM_STATUS_FRAME *frame_modem_status;
81
 
82
    XBEE_TX_DATA_PACKET_FRAME *frame_tx_data;
83
 
84
    /* --------------------- Oscillator Configuration --------------------- */
85
    //    OSCTUNEbits.PLLEN = 1;          // Enable 4x PLL
86
    //    OSCCONbits.IRCF = 0b111;        // Set INTOSC postscaler to 8MHz
87
    OSCCONbits.SCS = 0b00; // Use 96MHz PLL as primary clock source
88
    /* -------------------------------------------------------------------- */
89
 
112 Kevin 90
    // Set all ports as digial I/O
91
    ANCON0 = 0xFF;
92
    ANCON1 = 0x1F;
93
 
111 Kevin 94
    uart_init(&uart_data); // Initialize the UART handler code
95
    xbee_init(&xbee_data); // Initialize the XBee handler code
96
    i2c_init(&i2c_data); // Initialize the I2C handler code
97
    buffer_init(&buffer_data);
107 Kevin 98
    //    adc_init();                 // Initialize the ADC
99
    MQ_init(); // Initialize message queues before enabling any interrupts
111 Kevin 100
    timers_init(); // Initialize timers
107 Kevin 101
    led_driver_init(); // Initialize the driver for the LED display
111 Kevin 102
    port_b_int_init(); // Initialze Port B interrupt handler
103
 
104
#ifdef _BASE_STATION
105
    intx_init();    // IR receiver input
106
#endif
107
#ifdef _REMOTE
107 Kevin 108
    pwm_init(); // Initialize the PWM output driver
111 Kevin 109
#endif
107 Kevin 110
    interrupt_enable(); // Enable high-priority interrupts and low-priority interrupts
111
    interrupt_init(); // Initialize the interrupt priorities
112
 
113
    // Configure the hardware i2c device as a slave
111 Kevin 114
#ifdef _BASE_STATION
112 Kevin 115
    i2c_configure_slave(0x5F);
107 Kevin 116
#endif
111 Kevin 117
#ifdef _REMOTE
112 Kevin 118
    i2c_configure_master();
107 Kevin 119
#endif
112 Kevin 120
 
107 Kevin 121
    DBG_PRINT_MAIN("\r\nMain: Program Started\r\n");
111 Kevin 122
 
107 Kevin 123
    // Loop and process recieved messages from interrupts
124
    while (1) {
125
        // Call a routine that blocks until either message queues are not empty
126
        MQ_wait_on_incoming_msg_queues();
127
 
128
        // Process high priority message queue
129
        length = MQ_recvmsg_ToMainFromHigh(MSGLEN, &msgtype, (void *) msgbuffer);
130
        if (length < 0) {
131
            // No message, check the error code to see if it is concern
132
            if (length != MSG_QUEUE_EMPTY) {
133
                DBG_PRINT_MAIN("Main: (ERROR) Bad high priority receive, code = %d\r\n", length);
134
            }
135
        } else {
136
            switch (msgtype) {
137
                    /* --- I2C Message Handlers ----------------------------------*/
138
                case MSGTYPE_OVERRUN:
139
                    DBG_PRINT_MAIN("Main: (ERROR) UART overrun detected, type = %d\r\n", msgtype);
140
                    break;
141
                case MSGTYPE_I2C_DBG:
142
                    DBG_PRINT_MAIN("Main: I2C Dbg Data Recieved: ");
143
                    for (i = 0; i < length; i++) {
144
                        DBG_PRINT_MAIN("%X ", msgbuffer[i]);
145
                    }
146
                    DBG_PRINT_MAIN("\r\n");
147
                    break;
148
                case MSGTYPE_I2C_DATA:
149
                    DBG_PRINT_MAIN("Main: I2C Data Recieved: ");
150
                    for (i = 0; i < length - 1; i++) {
151
                        DBG_PRINT_MAIN("%X ", msgbuffer[i]);
152
                    }
153
                    DBG_PRINT_MAIN(" Event Count: %d", msgbuffer[length - 1]);
154
                    DBG_PRINT_MAIN("\r\n");
155
                    switch (msgbuffer[0]) {
156
                        case 0x2:
157
                            length = 1;
111 Kevin 158
                            // Return size of stored data in buffer
159
                            if (buffer_data.stored_length > MSGLEN) {
160
                                msgbuffer[0] = MSGLEN;
161
                            } else {
162
                                msgbuffer[0] = buffer_data.stored_length;
163
                            }
112 Kevin 164
                            DBG_PRINT_MAIN("Main: (I2C Return 0x2) Returning %X\r\n", msgbuffer[0]);
107 Kevin 165
                            MQ_sendmsg_FromMainToHigh(length, MSGTYPE_I2C_REPLY, (void *) msgbuffer);
166
                            break;
167
                        case 0x4:
111 Kevin 168
                            // Return data stored in buffer
169
                            if (buffer_data.stored_length > MSGLEN) {
170
                                length = MSGLEN;
171
                                buffer_read(MSGLEN, msgbuffer);
172
                            } else {
173
                                length = buffer_data.stored_length;
174
                                buffer_read(buffer_data.stored_length, msgbuffer);
175
                            }
107 Kevin 176
                            MQ_sendmsg_FromMainToHigh(length, MSGTYPE_I2C_REPLY, (void *) msgbuffer);
177
                            break;
178
                        case 0x6:
179
                            break;
180
                        case 0x7:
181
                            break;
182
                        case 0x8:
183
                            break;
184
                        case 0x9:
185
                            break;
186
                        default:
187
                            DBG_PRINT_MAIN("Main: (ERROR) Unexpected message type recieved: %d\r\n", msgbuffer[0]);
188
                            break;
189
                    };
190
                    break;
191
                case MSGTYPE_I2C_MASTER_SEND_COMPLETE:
192
                    DBG_PRINT_MAIN("Main: I2C Master Send Complete\r\n");
112 Kevin 193
#ifdef _BASE_STATION
194
                    if (i2c_state == I2C_WAIT_WRITE_LENGTH_ACK) {
195
                        i2c_master_recv(0x5F, 2);   // Request 2 bytes
196
                        i2c_state = I2C_WAIT_REPLY_LENGTH;
197
                    } else if (i2c_state == I2C_WAIT_WRITE_DATA_ACK) {
198
                        i2c_master_recv(0x5F, i2c_bytes_to_read+1); // Request # of bytes
199
                        i2c_state = I2C_WAIT_REPLY_DATA;
200
                    }
201
#endif
107 Kevin 202
                    break;
203
                case MSGTYPE_I2C_MASTER_SEND_FAILED:
204
                    DBG_PRINT_MAIN("Main: (ERROR) I2C Master Send Failed\r\n");
205
                    break;
206
                case MSGTYPE_I2C_MASTER_RECV_COMPLETE:
207
                    DBG_PRINT_MAIN("Main: I2C Master Receive Complete\r\n");
112 Kevin 208
                    DBG_PRINT_MAIN("Main: (I2C Data) ");
107 Kevin 209
                    for (i = 0; i < length; i++) {
210
                        DBG_PRINT_MAIN("%X ", msgbuffer[i]);
211
                    }
212
                    DBG_PRINT_MAIN("\r\n");
112 Kevin 213
#ifdef _BASE_STATION
214
                    if (i2c_state == I2C_WAIT_REPLY_LENGTH) {
215
                        if (msgbuffer[1] == 0xFF) {
216
                            i2c_master_recv(0x5F, 2);   // Request again
217
                        } else if (msgbuffer[1] != 0) {
218
                            i2c_bytes_to_read = msgbuffer[1];
219
                            // Write [4,#]
220
                            msgbuffer[0] = 4;
221
                            i2c_master_send(0x5F, 2, msgbuffer);
222
                            i2c_state = I2C_WAIT_WRITE_DATA_ACK;
223
                        } else {
224
                            i2c_state = I2C_STATE_IDLE;
225
                        }
226
                    } else if (i2c_state == I2C_WAIT_REPLY_DATA) {
227
                        if (msgbuffer[1] == 0xFF) {
228
                            i2c_master_recv(0x5F, i2c_bytes_to_read+1); // Request again
229
                        } else {
230
                            for (i = 1; i < length; i++) {
231
                                led_driver_num(msgbuffer[i]);
232
                                Delay10KTCYx(50);
233
                            }
234
                            i2c_state = I2C_STATE_IDLE;
235
                        }
236
                    }
237
#endif
107 Kevin 238
                    break;
239
                case MSGTYPE_I2C_MASTER_RECV_FAILED:
240
                    DBG_PRINT_MAIN("Main: (ERROR) I2C Master Receive Failed\r\n");
241
                    break;
242
                    /* -----------------------------------------------------------*/
111 Kevin 243
 
107 Kevin 244
                    /* --- XBee Message Handlers ---------------------------------*/
245
                case MSGTYPE_XBEE_RX_AT_COMMAND_RESPONSE:
246
                    DBG_PRINT_MAIN("Main: XBee AT command frame\r\n");
247
                    frame_at_cmd_response = (void *) msgbuffer;
248
                    DBG_PRINT_MAIN("Command: %c%c\r\n", frame_at_cmd_response->command[0], frame_at_cmd_response->command[0]);
249
                    DBG_PRINT_MAIN("Status: %d\r\n", frame_at_cmd_response->command_status);
250
                    DBG_PRINT_MAIN("Data: ");
251
                    for (i = 0; i < length - XBEE_RX_AT_COMMAND_RESPONSE_FRAME_SIZE; i++) {
252
                        DBG_PRINT_MAIN("%X ", frame_data_packet->data[i]);
253
                    }
254
                    DBG_PRINT_MAIN("\r\n");
255
                    break;
256
                case MSGTYPE_XBEE_RX_DATA_PACKET:
257
                    DBG_PRINT_MAIN("Main: XBee data packet frame\r\n");
258
                    frame_data_packet = (void *) msgbuffer;
259
                    DBG_PRINT_MAIN("Data: ");
260
                    for (i = 0; i < length - XBEE_RX_DATA_PACKET_FRAME_SIZE; i++) {
261
                        DBG_PRINT_MAIN("%X ", frame_data_packet->data[i]);
262
                    }
263
                    DBG_PRINT_MAIN("\r\n");
111 Kevin 264
                    // Store received data into buffer
265
                    buffer_insert(length - XBEE_RX_DATA_PACKET_FRAME_SIZE, frame_data_packet->data);
266
                    // Send value of first byte received to LED display
107 Kevin 267
                    led_driver_num(frame_data_packet->data[0]);
268
                    break;
269
                case MSGTYPE_XBEE_RX_DATA_TX_STATUS:
270
                    DBG_PRINT_MAIN("Main: XBee TX status frame\r\n");
271
                    frame_tx_status = (void *) msgbuffer;
272
                    DBG_PRINT_MAIN("Destination: %X\r\n", frame_tx_status->destination_16);
273
                    DBG_PRINT_MAIN("Transmit Retry Count: %X\r\n", frame_tx_status->transmit_retry_count);
274
                    DBG_PRINT_MAIN("Delivery Status: %X\r\n", frame_tx_status->delivery_status);
275
                    DBG_PRINT_MAIN("Discovery Status: %X\r\n", frame_tx_status->discovery_status);
276
                    break;
277
                case MSGTYPE_XBEE_RX_IO_DATA_SAMPLE:
278
                    DBG_PRINT_MAIN("Main: XBee IO data sample frame\r\n");
279
                    frame_io_sample = (void *) msgbuffer;
280
                    break;
281
                case MSGTYPE_XBEE_RX_EXPLICIT_COMMAND:
282
                    DBG_PRINT_MAIN("Main: XBee explicit command frame\r\n");
283
                    frame_explicit_cmd = (void *) msgbuffer;
284
                    break;
285
                case MSGTYPE_XBEE_RX_REMOTE_AT_COMMAND_RESPONSE:
286
                    DBG_PRINT_MAIN("Main: XBee remote AT command response frame\r\n");
287
                    frame_remote_at_cmd = (void *) msgbuffer;
288
                    break;
289
                case MSGTYPE_XBEE_RX_ROUTE_RECORD:
290
                    DBG_PRINT_MAIN("Main: XBee route record frame\r\n");
291
                    frame_route_record = (void *) msgbuffer;
292
                    break;
293
                case MSGTYPE_XBEE_RX_NODE_IDENTIFICATION:
294
                    DBG_PRINT_MAIN("Main: XBee node identification frame\r\n");
295
                    frame_node_identification = (void *) msgbuffer;
296
                    break;
297
                case MSGTYPE_XBEE_RX_FRAME_MODEM_STATUS:
298
                    DBG_PRINT_MAIN("Main: XBee modem status frame\r\n");
299
                    frame_modem_status = (void *) msgbuffer;
112 Kevin 300
                    DBG_PRINT_MAIN("Status: %X (", frame_modem_status->status);
301
                    switch(frame_modem_status->status) {
302
                        case 0:
303
                            DBG_PRINT_MAIN("Hardware Reset");
304
                            break;
305
                        case 1:
306
                            DBG_PRINT_MAIN("Watchdog Timer Reset");
307
                            break;
308
                        case 2:
309
                            DBG_PRINT_MAIN("Joined Network");
310
                            break;
311
                        case 3:
312
                            DBG_PRINT_MAIN("Disassociated");
313
                            break;
314
                        case 6:
315
                            DBG_PRINT_MAIN("Coordinator Started");
316
                            break;
317
                        case 7:
318
                            DBG_PRINT_MAIN("Network Security Key Updated");
319
                            break;
320
                        case 0x11:
321
                            DBG_PRINT_MAIN("Modem Config Changed While Joining");
322
                            break;
323
                    }
324
                    DBG_PRINT_MAIN(")\r\n");
107 Kevin 325
                    break;
326
                    /* -----------------------------------------------------------*/
111 Kevin 327
            };
328
            continue;
329
        }
330
 
331
        // Process low priority queue
332
        length = MQ_recvmsg_ToMainFromLow(MSGLEN, &msgtype, (void *) msgbuffer);
333
        if (length < 0) {
334
            // No message, check the error code to see if it is concern
335
            if (length != MSG_QUEUE_EMPTY) {
336
                DBG_PRINT_MAIN("Main: (ERROR) Bad low priority receive, code = %d\r\n", length);
337
            }
338
        } else {
339
            switch (msgtype) {
340
                /* --- Port B Interrupt Handlers -----------------------------*/
107 Kevin 341
                case MSGTYPE_PORTB_4_DOWN:
109 Kevin 342
                    DBG_PRINT_MAIN("Main: Port B4 Down\r\n");
111 Kevin 343
#ifdef _REMOTE
344
                    timer3_enable();
345
#endif
346
#ifdef _BASE_STATION
112 Kevin 347
//                    timer0_enable();
111 Kevin 348
#endif
107 Kevin 349
                    break;
350
                case MSGTYPE_PORTB_4_UP:
109 Kevin 351
                    DBG_PRINT_MAIN("Main: Port B4 Up\r\n");
111 Kevin 352
#ifdef _REMOTE
353
                    timer3_disable();
354
#endif
355
#ifdef _BASE_STATION
112 Kevin 356
//                    timer0_disable();
111 Kevin 357
#endif
107 Kevin 358
                    break;
359
                case MSGTYPE_PORTB_5_DOWN:
109 Kevin 360
                    DBG_PRINT_MAIN("Main: Port B5 Down\r\n");
112 Kevin 361
#ifdef _BASE_STATION
362
//                    if (i2c_state == I2C_STATE_IDLE) {
363
//                        DBG_PRINT_MAIN("Main: Starting I2C Request\r\n");
364
//                        /* I2C Demo */
365
//                        // Write 2 bytes
366
//                        msgbuffer[0] = 0x2;
367
//                        msgbuffer[1] = 0x2;
368
//                        i2c_master_send(0x5F, 2, msgbuffer);
369
//                        i2c_state = I2C_WAIT_WRITE_LENGTH_ACK;
370
//                    }
371
#endif
107 Kevin 372
                    break;
373
                case MSGTYPE_PORTB_5_UP:
109 Kevin 374
                    DBG_PRINT_MAIN("Main: Port B5 Up\r\n");
107 Kevin 375
                    break;
376
                case MSGTYPE_PORTB_6_DOWN:
109 Kevin 377
                    DBG_PRINT_MAIN("Main: Port B6 Down\r\n");
107 Kevin 378
                    break;
379
                case MSGTYPE_PORTB_6_UP:
109 Kevin 380
                    DBG_PRINT_MAIN("Main: Port B6 Up\r\n");
107 Kevin 381
                    break;
382
                case MSGTYPE_PORTB_7_DOWN:
109 Kevin 383
                    DBG_PRINT_MAIN("Main: Port B7 Down\r\n");
107 Kevin 384
                    break;
385
                case MSGTYPE_PORTB_7_UP:
109 Kevin 386
                    DBG_PRINT_MAIN("Main: Port B7 Up\r\n");
107 Kevin 387
                    break;
112 Kevin 388
                case MSGTYPE_INT1:
389
//                    DBG_PRINT_MAIN("Main: INT1 Interrupt\r\n");
390
                    break;
107 Kevin 391
                    /* -----------------------------------------------------------*/
111 Kevin 392
                    /* --- Timer Interrupt Handlers ------------------------------*/
393
                case MSGTYPE_TIMER0:
112 Kevin 394
                    DBG_PRINT_MAIN("Main: Timer 0 Interrupt\r\n");
111 Kevin 395
                    /* XBee Demo */
396
                    frame_tx_data = (void *) msgbuffer;
397
                    frame_tx_data->frame_type = XBEE_TX_DATA_PACKET;
398
                    frame_tx_data->frame_id = 0;
399
                    frame_tx_data->destination_64.UPPER_32.long_value = 0x00000000;
400
                    frame_tx_data->destination_64.LOWER_32.long_value = 0x00000000;
401
                    frame_tx_data->destination_16.INT_16.int_value = 0x0000;
402
                    frame_tx_data->broadcast_radius = 0;
403
                    frame_tx_data->options = 0x01; // Disable ACK
404
                    frame_tx_data->data[0] = counter;
112 Kevin 405
 
111 Kevin 406
                    counter++;
407
                    if (counter == 100)
408
                        counter = 0;
409
                    length = XBEE_TX_DATA_PACKET_FRAME_SIZE + 1;
410
                    xbee_process_transmit_frame((void *) msgbuffer, length);
411
                    Delay10KTCYx(100);
412
                    break;
413
//                case MSGTYPE_ADC_NEWVALUE:
414
//                    // Get the value in the ADC
415
//                    adc_last_value = *((unsigned int*) msgbuffer);
416
//                    adc_last_value_shifted = adc_last_value >> 4;
417
//                    DBG_PRINT_MAIN("Main: ADC Value = %d\r\n", adc_last_value);
418
//
419
//                    adc_start();
420
//                    break;
107 Kevin 421
                default:
111 Kevin 422
                    DBG_PRINT_MAIN("Main: (ERROR) Unexpected msg in low queue, length = %d, type = %d\r\n", length, msgtype);
107 Kevin 423
                    for (i = 0; i < length; i++) {
424
                        DBG_PRINT_MAIN("%X ", msgbuffer[i]);
425
                    }
426
                    DBG_PRINT_MAIN("\r\n");
427
                    break;
428
            };
429
            continue;
430
        }
431
    }
432
}