Subversion Repositories Code-Repo

Rev

Rev 113 | Rev 115 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
113 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"
13
#include "buffer.h"
14
#include "imu.h"
15
#include "sleep.h"
16
 
114 Kevin 17
#ifdef _REMOTE
113 Kevin 18
#pragma config WDTEN = OFF          // Turn off watchdog timer
19
#pragma config XINST = OFF          // Turn off extended instruction set
20
#pragma config OSC = HSPLL          // Use external oscillator (101)
21
#pragma config IOL1WAY = OFF        // IOLOCK bit can be set and cleared as needed
114 Kevin 22
#endif
23
#ifdef _BASE_STATION
24
#pragma config WDTEN = OFF          // Turn off watchdog timer
25
#pragma config XINST = OFF          // Turn off extended instruction set
26
#pragma config OSC = INTOSCPLL      // Use internal oscillator
27
#pragma config IOL1WAY = OFF        // IOLOCK bit can be set and cleared as needed
28
#endif
113 Kevin 29
 
114 Kevin 30
/* ----- To Do -----
31
 * Verify polling base station over I2C works
32
 * Implement send data to remote on corresponding I2C command
33
 * Verify operation with multiple base stations
34
 * -------------- */
35
 
113 Kevin 36
/* ----------- IO Pins -----------
37
 * RA0 - LED Display Latch Enable (V1) or Display CLK (V2) (PPS)
114 Kevin 38
 * RA1 - LED Display CLK (V1) (PPS)    or Display DIN (V2) (PPS)
39
 * RA2 - LED Display DIN (V1) (PPS)    or Display Latch Enable (V2)
113 Kevin 40
 * RA3 - LED Display Output Enable
41
 * RA4 - [CANNOT BE USED (VDDCORE/VCAP)]
42
 * RA5 - IR Reciever (PPS)
43
 * RA6 - Oscillator
44
 * RA7 - Oscillator
45
 * 
46
 * RC0 - PWM Output (IR) (PPS, Ports B and C only)
47
 * RC1 - PWM Output (IR) (PPS, Ports B and C only)
48
 * RC2 - LED Output (PPS, Ports B and C only)
49
 * RC3 - I2C SCL
50
 * RC4 - I2C SDA
51
 * RC5 - XBee Sleep (PPS)
52
 * RC6 - UART Debug Output
53
 * RC7 - UART Debug Input
54
 *
55
 * RB0 - XBee CTS (PPS)
56
 * RB1 - XBee RTS (PPS)
57
 * RB2 - XBee Tx (PPS)
58
 * RB3 - XBee Rx (PPS)
59
 * RB4 - Button Input (Port B Interrupt on Change)
60
 * RB5 - Button Input (Port B Interrupt on Change)
61
 * RB6 - Button Input (Port B Interrupt on Change)
62
 * RB7 - Button Input (Port B Interrupt on Change)
63
 * ---------------------------- */
64
 
65
#pragma udata msgbuffer
66
unsigned char msgbuffer[MSGLEN];
67
#pragma udata
68
 
69
void main(void) {
70
    XBEE_DATA xbee_data;
71
    I2C_DATA i2c_data;
72
    BUFFER_DATA buffer_data;
114 Kevin 73
    int length;
113 Kevin 74
    unsigned char msgtype;
75
    unsigned char i = 0;
76
    unsigned char counter = 0;
114 Kevin 77
    unsigned char IR_receive_flag = 0;
78
    unsigned int xbee_parent_address = 0;
113 Kevin 79
    enum I2C_STATE i2c_state = I2C_STATE_IDLE;
80
    enum XBEE_STATE xbee_state = XBEE_STATE_WAITING_TO_JOIN;
114 Kevin 81
    XBEE_ADDRESS_64 last_recv_address = {0};
82
    XBEE_ADDRESS_16 converter16;
113 Kevin 83
 
84
    // Pointers to allow parsing of xbee data from arbitrary byte array
85
    XBEE_RX_AT_COMMAND_RESPONSE_FRAME *frame_at_cmd_response;
86
    XBEE_RX_DATA_PACKET_FRAME *frame_data_packet;
87
    XBEE_RX_DATA_TX_STATUS_FRAME *frame_tx_status;
88
    XBEE_RX_IO_DATA_SAMPLE_FRAME *frame_io_sample;
89
    XBEE_RX_EXPLICIT_COMMAND_FRAME *frame_explicit_cmd;
90
    XBEE_RX_REMOTE_AT_COMMAND_FRAME *frame_remote_at_cmd;
91
    XBEE_RX_ROUTE_RECORD_FRAME *frame_route_record;
92
    XBEE_RX_NODE_IDENTIFICATION_INDICATOR_FRAME *frame_node_identification;
93
    XBEE_RX_MODEM_STATUS_FRAME *frame_modem_status;
94
 
95
    XBEE_TX_DATA_PACKET_FRAME *frame_tx_data;
114 Kevin 96
    XBEE_TX_AT_COMMAND_FRAME *frame_tx_at_command;
113 Kevin 97
 
98
    /* --------------------- Oscillator Configuration --------------------- */
99
    OSCTUNEbits.PLLEN = 1;          // Enable 4x PLL
100
    OSCCONbits.IRCF = 0b111;        // Set INTOSC postscaler to 8MHz
114 Kevin 101
    OSCCONbits.SCS = 0b00; // Use PLL as primary clock source
113 Kevin 102
    /* -------------------------------------------------------------------- */
103
 
104
    // Set all ports as digial I/O
105
    ANCON0 = 0xFF;
106
    ANCON1 = 0x1F;
107
 
108
    uart_init(); // Initialize the UART handler code
109
    xbee_init(&xbee_data); // Initialize the XBee handler code
110
    i2c_init(&i2c_data); // Initialize the I2C handler code
111
    buffer_init(&buffer_data);
112
    //    adc_init();                 // Initialize the ADC
113
    MQ_init(); // Initialize message queues before enabling any interrupts
114
    timers_init(); // Initialize timers
115
    led_driver_init(); // Initialize the driver for the LED display
116
    port_b_int_init(); // Initialze Port B interrupt handler
114 Kevin 117
#ifdef _BASE_STATION
118
    intx_init();    // IR receiver input
119
#endif
113 Kevin 120
    pwm_init(); // Initialize the PWM output driver
121
 
122
    interrupt_enable(); // Enable high-priority interrupts and low-priority interrupts
123
    interrupt_init(); // Initialize the interrupt priorities
114 Kevin 124
#ifdef _BASE_STATION
125
    i2c_configure_slave(BASE_STATION_ADDRESS);
126
#endif
127
#ifdef _REMOTE
113 Kevin 128
    i2c_configure_master(); // Configure the hardware i2c device as a master
129
    imu_init();
114 Kevin 130
#endif
113 Kevin 131
 
132
    DBG_PRINT_MAIN("\r\nMain: Program Started\r\n");
133
 
114 Kevin 134
#ifdef _BASE_STATION
135
    sleep();
136
#endif
137
#ifdef _REMOTE
113 Kevin 138
    // Turn on LED until XBee is connected to network
114 Kevin 139
    pwm_LED_on();
140
#endif
113 Kevin 141
 
142
    // Loop and process recieved messages from interrupts
143
    while (1) {
144
        // Call a routine that blocks until either message queues are not empty
145
        MQ_wait_on_incoming_msg_queues();
146
 
147
        // Process high priority message queue
148
        length = MQ_recvmsg_ToMainFromHigh(MSGLEN, &msgtype, (void *) msgbuffer);
149
        if (length < 0) {
150
            // No message, check the error code to see if it is concern
151
            if (length != MSG_QUEUE_EMPTY) {
152
                DBG_PRINT_MAIN("Main: (ERROR) Bad high priority receive, code = %d\r\n", length);
153
            }
154
        } else {
155
            switch (msgtype) {
156
                    /* --- I2C Message Handlers ----------------------------------*/
114 Kevin 157
#ifdef _BASE_STATION
113 Kevin 158
                case MSGTYPE_OVERRUN:
159
                    DBG_PRINT_MAIN("Main: (ERROR) UART overrun detected, type = %d\r\n", msgtype);
160
                    break;
161
                case MSGTYPE_I2C_DBG:
162
                    DBG_PRINT_MAIN("Main: I2C Dbg Data Recieved: ");
163
                    for (i = 0; i < length; i++) {
114 Kevin 164
                        DBG_PRINT_MAIN("%02X ", msgbuffer[i]);
113 Kevin 165
                    }
166
                    DBG_PRINT_MAIN("\r\n");
167
                    break;
168
                case MSGTYPE_I2C_DATA:
169
                    DBG_PRINT_MAIN("Main: I2C Data Recieved: ");
170
                    for (i = 0; i < length - 1; i++) {
114 Kevin 171
                        DBG_PRINT_MAIN("%02X ", msgbuffer[i]);
113 Kevin 172
                    }
173
                    DBG_PRINT_MAIN(" Event Count: %d", msgbuffer[length - 1]);
174
                    DBG_PRINT_MAIN("\r\n");
175
                    switch (msgbuffer[0]) {
176
                        case 0x2:
177
                            length = 1;
178
                            // Return size of stored data in buffer
179
                            if (buffer_data.stored_length > MSGLEN) {
180
                                msgbuffer[0] = MSGLEN;
181
                            } else {
182
                                msgbuffer[0] = buffer_data.stored_length;
183
                            }
114 Kevin 184
                            DBG_PRINT_MAIN("Main: (I2C Return 0x2) Returning %d bytes\r\n", length);
113 Kevin 185
                            MQ_sendmsg_FromMainToHigh(length, MSGTYPE_I2C_REPLY, (void *) msgbuffer);
186
                            break;
187
                        case 0x4:
188
                            // Return data stored in buffer
189
                            if (buffer_data.stored_length > MSGLEN) {
190
                                length = MSGLEN;
191
                                buffer_read(MSGLEN, msgbuffer);
192
                            } else {
193
                                length = buffer_data.stored_length;
194
                                buffer_read(buffer_data.stored_length, msgbuffer);
195
                            }
114 Kevin 196
                            DBG_PRINT_MAIN("Main: (I2C Return 0x4) Returning %d bytes\r\n", length);
113 Kevin 197
                            MQ_sendmsg_FromMainToHigh(length, MSGTYPE_I2C_REPLY, (void *) msgbuffer);
114 Kevin 198
                            pwm_LED_off();  // Turn off LED if it was on
113 Kevin 199
                            break;
200
                        case 0x6:
201
                            break;
202
                        case 0x7:
203
                            break;
204
                        case 0x8:
205
                            break;
206
                        case 0x9:
207
                            break;
208
                        default:
209
                            DBG_PRINT_MAIN("Main: (ERROR) Unexpected message type recieved: %d\r\n", msgbuffer[0]);
210
                            break;
211
                    };
212
                    break;
114 Kevin 213
#endif
214
#ifdef _REMOTE
113 Kevin 215
                case MSGTYPE_I2C_MASTER_SEND_COMPLETE:
216
                    DBG_PRINT_MAIN("Main: I2C Master Send Complete\r\n");
217
                    break;
218
                case MSGTYPE_I2C_MASTER_SEND_FAILED:
219
                    DBG_PRINT_MAIN("Main: (ERROR) I2C Master Send Failed\r\n");
220
                    break;
221
                case MSGTYPE_I2C_MASTER_RECV_COMPLETE:
222
                    DBG_PRINT_MAIN("Main: I2C Master Receive Complete\r\n");
223
                    DBG_PRINT_MAIN("Main: (I2C Data) ");
224
                    for (i = 0; i < length; i++) {
114 Kevin 225
                        DBG_PRINT_MAIN("%02X ", msgbuffer[i]);
113 Kevin 226
                    }
114 Kevin 227
                    DBG_PRINT_MAIN("\r\n");
228
 
229
                    // Insert recorded value into buffer
230
                    if (i2c_state == I2C_STATE_READ_ACC) {
231
                        buffer_insert_one(RETURNID_ACC);
232
                        buffer_insert(6, msgbuffer);
233
                    } else if (i2c_state == I2C_STATE_READ_GYRO) {
234
                        buffer_insert_one(RETURNID_GYRO);
235
                        buffer_insert(6, msgbuffer);
236
                    }
113 Kevin 237
 
114 Kevin 238
                    if (buffer_free_space() < 7) {
239
                        wake();
113 Kevin 240
                        // Send data to base station
114 Kevin 241
                        frame_tx_data = (void *) msgbuffer;
242
                        frame_tx_data->frame_type = XBEE_TX_DATA_PACKET;
243
                        frame_tx_data->frame_id = 0;
244
                        frame_tx_data->destination_64.UPPER_32.long_value = 0x00000000;
245
                        frame_tx_data->destination_64.LOWER_32.long_value = 0x00000000;
246
                        ConvertEndian64(&frame_tx_data->destination_64);
247
                        frame_tx_data->destination_16.INT_16.int_value = xbee_parent_address;
248
                        ConvertEndian16(&frame_tx_data->destination_16);
249
                        frame_tx_data->broadcast_radius = 0;
250
                        frame_tx_data->options = 0x01;  // Disable ACK
113 Kevin 251
 
114 Kevin 252
                        length = XBEE_TX_DATA_PACKET_FRAME_SIZE + buffer_data.stored_length;
253
                        buffer_read(buffer_data.stored_length, frame_tx_data->data);
254
                        xbee_process_transmit_frame((void *) msgbuffer, length);
113 Kevin 255
                    }
114 Kevin 256
 
113 Kevin 257
                    break;
258
                case MSGTYPE_I2C_MASTER_RECV_FAILED:
259
                    DBG_PRINT_MAIN("Main: (ERROR) I2C Master Receive Failed\r\n");
260
                    break;
114 Kevin 261
#endif
113 Kevin 262
                    /* -----------------------------------------------------------*/
263
 
264
                    /* --- XBee Message Handlers ---------------------------------*/
265
                case MSGTYPE_XBEE_RX_AT_COMMAND_RESPONSE:
114 Kevin 266
                    DBG_PRINT_MAIN("Main: XBee AT command response frame\r\n");
113 Kevin 267
                    frame_at_cmd_response = (void *) msgbuffer;
114 Kevin 268
                    DBG_PRINT_MAIN("Command: %c%c\r\n", frame_at_cmd_response->command[0], frame_at_cmd_response->command[1]);
113 Kevin 269
                    DBG_PRINT_MAIN("Status: %d\r\n", frame_at_cmd_response->command_status);
270
                    DBG_PRINT_MAIN("Data: ");
271
                    for (i = 0; i < length - XBEE_RX_AT_COMMAND_RESPONSE_FRAME_SIZE; i++) {
114 Kevin 272
                        DBG_PRINT_MAIN("%02X ", frame_data_packet->data[i]);
113 Kevin 273
                    }
274
                    DBG_PRINT_MAIN("\r\n");
114 Kevin 275
#ifdef _REMOTE
276
                    if (frame_at_cmd_response->command[0] == 'M' &&
277
                            frame_at_cmd_response->command[1] == 'P') {
278
                        converter16.INT_16.char_value[0] = frame_at_cmd_response->data[0];
279
                        converter16.INT_16.char_value[1] = frame_at_cmd_response->data[1];
280
                        ConvertEndian16(&converter16);
281
                        xbee_parent_address = converter16.INT_16.int_value;
282
                        xbee_state = XBEE_STATE_JOINED_NETWORK;
283
                    }
284
#endif
113 Kevin 285
                    break;
286
                case MSGTYPE_XBEE_RX_DATA_PACKET:
114 Kevin 287
                    wake();
113 Kevin 288
                    DBG_PRINT_MAIN("Main: XBee data packet frame\r\n");
289
                    frame_data_packet = (void *) msgbuffer;
114 Kevin 290
                    ConvertEndian64(&frame_data_packet->source_64);
291
                    ConvertEndian16(&frame_data_packet->source_16);
292
                    DBG_PRINT_MAIN("Source 64: %08lX-%08lX\r\n",
293
                            frame_data_packet->source_64.UPPER_32.long_value,
294
                            frame_data_packet->source_64.LOWER_32.long_value
295
                            );
296
                    DBG_PRINT_MAIN("Source 16: %04X\r\n", frame_data_packet->source_16.INT_16.int_value);
297
                    DBG_PRINT_MAIN("Options: 0x%02X\r\n", frame_data_packet->recieve_options);
113 Kevin 298
                    DBG_PRINT_MAIN("Data: ");
299
                    for (i = 0; i < length - XBEE_RX_DATA_PACKET_FRAME_SIZE; i++) {
114 Kevin 300
                        DBG_PRINT_MAIN("%02X ", frame_data_packet->data[i]);
113 Kevin 301
                    }
302
                    DBG_PRINT_MAIN("\r\n");
114 Kevin 303
#ifdef _BASE_STATION
304
 
305
                    DBG_PRINT_MAIN("Buffer Free Space: %d\r\n", buffer_free_space());
306
                    if (frame_data_packet->source_64.LOWER_32.long_value != last_recv_address.LOWER_32.long_value ||
307
                            frame_data_packet->source_64.UPPER_32.long_value != last_recv_address.UPPER_32.long_value) {
308
                        // Receive data from new remote
309
                        buffer_insert(4, frame_data_packet->source_64.UPPER_32.char_value);
310
                        buffer_insert(4, frame_data_packet->source_64.LOWER_32.char_value);
311
                        // Save data
312
                        buffer_insert(length - XBEE_RX_DATA_PACKET_FRAME_SIZE, frame_data_packet->data);
313
                        // Save address of sender
314
                        last_recv_address.UPPER_32.long_value = frame_data_packet->source_64.UPPER_32.long_value;
315
                        last_recv_address.LOWER_32.long_value = frame_data_packet->source_64.LOWER_32.long_value;
316
                    } else {
317
                        // Receive data from same remote that previously sent data
318
                        buffer_insert(length - XBEE_RX_DATA_PACKET_FRAME_SIZE, frame_data_packet->data);
319
                    }
320
 
113 Kevin 321
                    // Send value of first byte received to LED display
322
                    led_driver_num(frame_data_packet->data[0]);
114 Kevin 323
#endif
324
                    sleep();
113 Kevin 325
                    break;
326
                case MSGTYPE_XBEE_RX_DATA_TX_STATUS:
327
                    DBG_PRINT_MAIN("Main: XBee TX status frame\r\n");
328
                    frame_tx_status = (void *) msgbuffer;
114 Kevin 329
                    DBG_PRINT_MAIN("Destination: %02X\r\n", frame_tx_status->destination_16);
330
                    DBG_PRINT_MAIN("Transmit Retry Count: %02X\r\n", frame_tx_status->transmit_retry_count);
331
                    DBG_PRINT_MAIN("Delivery Status: %02X\r\n", frame_tx_status->delivery_status);
332
                    DBG_PRINT_MAIN("Discovery Status: %02X\r\n", frame_tx_status->discovery_status);
113 Kevin 333
                    break;
334
                case MSGTYPE_XBEE_RX_IO_DATA_SAMPLE:
335
                    DBG_PRINT_MAIN("Main: XBee IO data sample frame\r\n");
336
                    frame_io_sample = (void *) msgbuffer;
337
                    break;
338
                case MSGTYPE_XBEE_RX_EXPLICIT_COMMAND:
339
                    DBG_PRINT_MAIN("Main: XBee explicit command frame\r\n");
340
                    frame_explicit_cmd = (void *) msgbuffer;
341
                    break;
342
                case MSGTYPE_XBEE_RX_REMOTE_AT_COMMAND_RESPONSE:
343
                    DBG_PRINT_MAIN("Main: XBee remote AT command response frame\r\n");
344
                    frame_remote_at_cmd = (void *) msgbuffer;
345
                    break;
346
                case MSGTYPE_XBEE_RX_ROUTE_RECORD:
347
                    DBG_PRINT_MAIN("Main: XBee route record frame\r\n");
348
                    frame_route_record = (void *) msgbuffer;
349
                    break;
350
                case MSGTYPE_XBEE_RX_NODE_IDENTIFICATION:
351
                    DBG_PRINT_MAIN("Main: XBee node identification frame\r\n");
352
                    frame_node_identification = (void *) msgbuffer;
114 Kevin 353
                    ConvertEndian64(&frame_node_identification->source_64);
354
                    ConvertEndian64(&frame_node_identification->remote_64);
355
                    ConvertEndian16(&frame_node_identification->source_16);
356
                    ConvertEndian16(&frame_node_identification->remote_16);
357
                    ConvertEndian16(&frame_node_identification->parent_16);
358
                    DBG_PRINT_MAIN("Source 64: %08lX-%08lX\r\n",
359
                            frame_node_identification->source_64.UPPER_32.long_value,
360
                            frame_node_identification->source_64.LOWER_32.long_value
361
                            );
362
                    DBG_PRINT_MAIN("Source 16: %04X\r\n", frame_node_identification->source_16.INT_16.int_value);
363
                    DBG_PRINT_MAIN("Remote 64: %08lX-%08lX\r\n",
364
                            frame_node_identification->remote_64.UPPER_32.long_value,
365
                            frame_node_identification->remote_64.LOWER_32.long_value
366
                            );
367
                    DBG_PRINT_MAIN("Remote 16: %04X\r\n", frame_node_identification->remote_16.INT_16.int_value);
368
                    DBG_PRINT_MAIN("Remote 16: %04X\r\n", frame_node_identification->parent_16.INT_16.int_value);
369
                    DBG_PRINT_MAIN("Receive Options: %02X\r\n", frame_node_identification->recieve_options);
113 Kevin 370
                    break;
371
                case MSGTYPE_XBEE_RX_FRAME_MODEM_STATUS:
372
                    DBG_PRINT_MAIN("Main: XBee modem status frame\r\n");
373
                    frame_modem_status = (void *) msgbuffer;
114 Kevin 374
                    DBG_PRINT_MAIN("Status: %02X (", frame_modem_status->status);
113 Kevin 375
                    switch(frame_modem_status->status) {
376
                        case 0:
377
                            DBG_PRINT_MAIN("Hardware Reset");
378
                            xbee_state = XBEE_STATE_WAITING_TO_JOIN;
379
                            break;
380
                        case 1:
381
                            DBG_PRINT_MAIN("Watchdog Timer Reset");
382
                            break;
383
                        case 2:
384
                            DBG_PRINT_MAIN("Joined Network");
114 Kevin 385
#ifdef _BASE_STATION
113 Kevin 386
                            xbee_state = XBEE_STATE_JOINED_NETWORK;
114 Kevin 387
#endif
388
#ifdef _REMOTE
389
                            // Query for parent's address
390
                            frame_tx_at_command = (void *) msgbuffer;
391
                            frame_tx_at_command->frame_type = XBEE_TX_AT_COMMAND;
392
                            frame_tx_at_command->frame_id = 1;
393
                            frame_tx_at_command->command[0] = 'M';
394
                            frame_tx_at_command->command[1] = 'P';
395
//                            length = XBEE_TX_AT_COMMAND_FRAME_SIZE;
396
                            xbee_process_transmit_frame((void *) msgbuffer, 4);
113 Kevin 397
                            // Turn off LED after XBee has joined network
114 Kevin 398
                            pwm_LED_off();
399
#endif
113 Kevin 400
                            break;
401
                        case 3:
402
                            DBG_PRINT_MAIN("Disassociated");
403
                            break;
404
                        case 6:
405
                            DBG_PRINT_MAIN("Coordinator Started");
406
                            break;
407
                        case 7:
408
                            DBG_PRINT_MAIN("Network Security Key Updated");
409
                            break;
410
                        case 0x11:
411
                            DBG_PRINT_MAIN("Modem Config Changed While Joining");
412
                            break;
413
                    }
414
                    DBG_PRINT_MAIN(")\r\n");
415
                    break;
416
                    /* -----------------------------------------------------------*/
417
            };
418
            continue;
419
        }
420
 
421
        // Process low priority queue
422
        length = MQ_recvmsg_ToMainFromLow(MSGLEN, &msgtype, (void *) msgbuffer);
423
        if (length < 0) {
424
            // No message, check the error code to see if it is concern
425
            if (length != MSG_QUEUE_EMPTY) {
426
                DBG_PRINT_MAIN("Main: (ERROR) Bad low priority receive, code = %d\r\n", length);
427
            }
428
        } else {
429
            switch (msgtype) {
430
                /* --- Port B Interrupt Handlers -----------------------------*/
431
                case MSGTYPE_PORTB_4_DOWN:
432
                    DBG_PRINT_MAIN("Main: Port B4 Down\r\n");
114 Kevin 433
#ifdef _REMOTE
434
                    if (xbee_state == XBEE_STATE_JOINED_NETWORK) {
435
                        wake(); // Wake up all components
436
                        timer1_enable(); // Set timer to start data polling
437
                        pwm_LED_on();
438
                    }
439
#endif
113 Kevin 440
                    break;
441
                case MSGTYPE_PORTB_4_UP:
442
                    DBG_PRINT_MAIN("Main: Port B4 Up\r\n");
114 Kevin 443
#ifdef _REMOTE
444
                    if (xbee_state == XBEE_STATE_JOINED_NETWORK) {
445
                        wake();
446
                        timer1_disable(); // Stop data polling timer
447
 
448
                        // Send remaining buffer data to base station
449
                        frame_tx_data = (void *) msgbuffer;
450
                        frame_tx_data->frame_type = XBEE_TX_DATA_PACKET;
451
                        frame_tx_data->frame_id = 0;
452
                        frame_tx_data->destination_64.UPPER_32.long_value = 0x00000000;
453
                        frame_tx_data->destination_64.LOWER_32.long_value = 0x00000000;
454
                        ConvertEndian64(&frame_tx_data->destination_64);
455
                        frame_tx_data->destination_16.INT_16.int_value = xbee_parent_address;
456
                        ConvertEndian16(&frame_tx_data->destination_16);
457
                        frame_tx_data->broadcast_radius = 0;
458
                        frame_tx_data->options = 0x01;  // Disable ACK
459
 
460
                        length = XBEE_TX_DATA_PACKET_FRAME_SIZE + buffer_data.stored_length;
461
                        buffer_read(buffer_data.stored_length, frame_tx_data->data);
462
                        xbee_process_transmit_frame((void *) msgbuffer, length);
463
 
464
                        i2c_state = I2C_STATE_IDLE;
465
                        pwm_LED_off();
466
                        sleep();
467
                    }
468
#endif
113 Kevin 469
                    break;
470
                case MSGTYPE_PORTB_5_DOWN:
471
                    DBG_PRINT_MAIN("Main: Port B5 Down\r\n");
114 Kevin 472
#ifdef _REMOTE
473
                    if (xbee_state == XBEE_STATE_JOINED_NETWORK) {
474
                        wake(); // Wake up all components
475
                        timer3_enable(); // Enable PWM timer
476
                        frame_tx_data = (void *) msgbuffer;
477
                        frame_tx_data->frame_type = XBEE_TX_DATA_PACKET;
478
                        frame_tx_data->frame_id = 0;
479
                        frame_tx_data->destination_64.UPPER_32.long_value = 0x00000000;
480
                        frame_tx_data->destination_64.LOWER_32.long_value = 0x00000000;
481
                        ConvertEndian64(&frame_tx_data->destination_64);
482
                        frame_tx_data->destination_16.INT_16.int_value = xbee_parent_address;
483
                        ConvertEndian16(&frame_tx_data->destination_16);
484
                        frame_tx_data->broadcast_radius = 0;
485
                        frame_tx_data->options = 0x01; // Disable ACK
486
                        frame_tx_data->data[0] = RETURNID_CONN;
487
 
488
                        length = XBEE_TX_DATA_PACKET_FRAME_SIZE + 1;
489
                        xbee_process_transmit_frame((void *) msgbuffer, length);
490
                    }
491
#endif
113 Kevin 492
                    break;
493
                case MSGTYPE_PORTB_5_UP:
494
                    DBG_PRINT_MAIN("Main: Port B5 Up\r\n");
114 Kevin 495
#ifdef _REMOTE
496
                    if (xbee_state == XBEE_STATE_JOINED_NETWORK) {
497
                        timer3_disable(); // Disable PWM timer
498
                        sleep();
499
                    }
500
#endif
113 Kevin 501
                    break;
502
                case MSGTYPE_PORTB_6_DOWN:
503
                    DBG_PRINT_MAIN("Main: Port B6 Down\r\n");
114 Kevin 504
#ifdef _REMOTE
505
                    if (xbee_state == XBEE_STATE_JOINED_NETWORK) {
506
                        wake(); // Wake up all components
507
                        frame_tx_data = (void *) msgbuffer;
508
                        frame_tx_data->frame_type = XBEE_TX_DATA_PACKET;
509
                        frame_tx_data->frame_id = 0;
510
                        frame_tx_data->destination_64.UPPER_32.long_value = 0x00000000;
511
                        frame_tx_data->destination_64.LOWER_32.long_value = 0x00000000;
512
                        ConvertEndian64(&frame_tx_data->destination_64);
513
                        frame_tx_data->destination_16.INT_16.int_value = xbee_parent_address;
514
                        ConvertEndian16(&frame_tx_data->destination_16);
515
                        frame_tx_data->broadcast_radius = 0;
516
                        frame_tx_data->options = 0x01; // Disable ACK
517
                        frame_tx_data->data[0] = RETURNID_BTN1;
518
 
519
                        length = XBEE_TX_DATA_PACKET_FRAME_SIZE + 1;
520
                        xbee_process_transmit_frame((void *) msgbuffer, length);
521
                    }
522
#endif
113 Kevin 523
                    break;
524
                case MSGTYPE_PORTB_6_UP:
525
                    DBG_PRINT_MAIN("Main: Port B6 Up\r\n");
114 Kevin 526
#ifdef _REMOTE
527
                    if (xbee_state == XBEE_STATE_JOINED_NETWORK) {
528
                        sleep();
529
                    }
530
#endif
113 Kevin 531
                    break;
532
                case MSGTYPE_PORTB_7_DOWN:
533
                    DBG_PRINT_MAIN("Main: Port B7 Down\r\n");
114 Kevin 534
#ifdef _REMOTE
535
                    if (xbee_state == XBEE_STATE_JOINED_NETWORK) {
536
                        wake(); // Wake up all components
537
                        frame_tx_data = (void *) msgbuffer;
538
                        frame_tx_data->frame_type = XBEE_TX_DATA_PACKET;
539
                        frame_tx_data->frame_id = 0;
540
                        frame_tx_data->destination_64.UPPER_32.long_value = 0x00000000;
541
                        frame_tx_data->destination_64.LOWER_32.long_value = 0x00000000;
542
                        ConvertEndian64(&frame_tx_data->destination_64);
543
                        frame_tx_data->destination_16.INT_16.int_value = xbee_parent_address;
544
                        ConvertEndian16(&frame_tx_data->destination_16);
545
                        frame_tx_data->broadcast_radius = 0;
546
                        frame_tx_data->options = 0x01; // Disable ACK
547
                        frame_tx_data->data[0] = RETURNID_BTN2;
548
 
549
                        length = XBEE_TX_DATA_PACKET_FRAME_SIZE + 1;
550
                        xbee_process_transmit_frame((void *) msgbuffer, length);
551
                    }
552
#endif
113 Kevin 553
                    break;
554
                case MSGTYPE_PORTB_7_UP:
555
                    DBG_PRINT_MAIN("Main: Port B7 Up\r\n");
114 Kevin 556
#ifdef _REMOTE
557
                    if (xbee_state == XBEE_STATE_JOINED_NETWORK) {
558
                        sleep();
559
                    }
560
#endif
113 Kevin 561
                    break;
562
                case MSGTYPE_INT1:
114 Kevin 563
#ifdef _BASE_STATION
564
                    IR_receive_flag = 1;
565
                    pwm_LED_toggle();
566
                    Delay10KTCYx(1);
567
                    pwm_LED_toggle();
568
                    sleep();
113 Kevin 569
//                    DBG_PRINT_MAIN("Main: INT1 Interrupt\r\n");
114 Kevin 570
#endif
113 Kevin 571
                    break;
572
                    /* -----------------------------------------------------------*/
573
                    /* --- Timer Interrupt Handlers ------------------------------*/
574
                case MSGTYPE_TIMER0:
575
                    DBG_PRINT_MAIN("Main: Timer 0 Interrupt\r\n");
114 Kevin 576
                    IR_receive_flag = 0;
577
                    sleep_enable();
113 Kevin 578
                    break;
579
                case MSGTYPE_TIMER1:
580
                    DBG_PRINT_MAIN("Main: Timer 1 Interrupt\r\n");
114 Kevin 581
#ifdef _REMOTE
582
//                    /* XBee Demo */
583
//                    frame_tx_data = (void *) msgbuffer;
584
//                    frame_tx_data->frame_type = XBEE_TX_DATA_PACKET;
585
//                    frame_tx_data->frame_id = 0;
586
//                    frame_tx_data->destination_64.UPPER_32.long_value = 0x00000000;
587
//                    frame_tx_data->destination_64.LOWER_32.long_value = 0x00000000;
588
//                    ConvertEndian64(&frame_tx_data->destination_64);
589
//                    frame_tx_data->destination_16.INT_16.int_value = xbee_parent_address;
590
//                    ConvertEndian16(&frame_tx_data->destination_16);
591
//                    frame_tx_data->broadcast_radius = 1;
592
//                    frame_tx_data->options = 0x01; // Disable ACK
593
//                    frame_tx_data->data[0] = counter;
594
//
595
//                    led_driver_num(counter);
596
//                    counter++;
597
//                    if (counter == 100)
598
//                        counter = 0;
599
//
600
//                    length = XBEE_TX_DATA_PACKET_FRAME_SIZE + 1;
601
//                    xbee_process_transmit_frame((void *) msgbuffer, length);
113 Kevin 602
 
603
                    /* Read values from accelerometer and gyroscope */
604
                    if (i2c_state == I2C_STATE_READ_GYRO) {
605
                        imu_read_acc();
606
                        i2c_state = I2C_STATE_READ_ACC;
607
                    } else if (i2c_state == I2C_STATE_READ_ACC) {
608
                        imu_read_gyro();
609
                        i2c_state = I2C_STATE_READ_GYRO;
610
                    } else {    // I2C_STATE_IDLE
611
                        imu_read_acc();
612
                        i2c_state = I2C_STATE_READ_ACC;
613
                    }
114 Kevin 614
#endif
113 Kevin 615
                    break;
114 Kevin 616
//                case MSGTYPE_ADC_NEWVALUE:
617
//                    // Get the value in the ADC
618
//                    adc_last_value = *((unsigned int*) msgbuffer);
619
//                    adc_last_value_shifted = adc_last_value >> 4;
620
//                    DBG_PRINT_MAIN("Main: ADC Value = %d\r\n", adc_last_value);
621
//
622
//                    adc_start();
623
//                    break;
113 Kevin 624
                default:
625
                    DBG_PRINT_MAIN("Main: (ERROR) Unexpected msg in low queue, length = %d, type = %d\r\n", length, msgtype);
626
                    for (i = 0; i < length; i++) {
627
                        DBG_PRINT_MAIN("%X ", msgbuffer[i]);
628
                    }
629
                    DBG_PRINT_MAIN("\r\n");
630
                    break;
631
            };
632
            continue;
633
        }
634
    }
114 Kevin 635
}