Subversion Repositories Code-Repo

Rev

Rev 114 | Rev 116 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 114 Rev 115
Line 25... Line 25...
25
#pragma config XINST = OFF          // Turn off extended instruction set
25
#pragma config XINST = OFF          // Turn off extended instruction set
26
#pragma config OSC = INTOSCPLL      // Use internal oscillator
26
#pragma config OSC = INTOSCPLL      // Use internal oscillator
27
#pragma config IOL1WAY = OFF        // IOLOCK bit can be set and cleared as needed
27
#pragma config IOL1WAY = OFF        // IOLOCK bit can be set and cleared as needed
28
#endif
28
#endif
29
 
29
 
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
 
-
 
36
/* ----------- IO Pins -----------
30
/* ----------- IO Pins -----------
37
 * RA0 - LED Display Latch Enable (V1) or Display CLK (V2) (PPS)
31
 * RA0 - LED Display Latch Enable (V1) or Display CLK (V2) (PPS)
38
 * RA1 - LED Display CLK (V1) (PPS)    or Display DIN (V2) (PPS)
32
 * RA1 - LED Display CLK (V1) (PPS)    or Display DIN (V2) (PPS)
39
 * RA2 - LED Display DIN (V1) (PPS)    or Display Latch Enable (V2)
33
 * RA2 - LED Display DIN (V1) (PPS)    or Display Latch Enable (V2)
40
 * RA3 - LED Display Output Enable
34
 * RA3 - LED Display Output Enable
Line 71... Line 65...
71
    I2C_DATA i2c_data;
65
    I2C_DATA i2c_data;
72
    BUFFER_DATA buffer_data;
66
    BUFFER_DATA buffer_data;
73
    int length;
67
    int length;
74
    unsigned char msgtype;
68
    unsigned char msgtype;
75
    unsigned char i = 0;
69
    unsigned char i = 0;
-
 
70
#ifdef _BASE_STATION
76
    unsigned char counter = 0;
71
    volatile unsigned char IR_receive_flag = 0;
77
    unsigned char IR_receive_flag = 0;
72
    unsigned char i2c_last_req_size = 0;
-
 
73
    XBEE_ADDRESS_64 last_recv_address = {0};
-
 
74
    XBEE_ADDRESS_16 remote_address_16;
-
 
75
    XBEE_ADDRESS_64 remote_address_64;
-
 
76
#endif
-
 
77
#ifdef _REMOTE
78
    unsigned int xbee_parent_address = 0;
78
    unsigned int xbee_parent_address_16 = 0;
79
    enum I2C_STATE i2c_state = I2C_STATE_IDLE;
79
    enum I2C_STATE i2c_state = I2C_STATE_IDLE;
80
    enum XBEE_STATE xbee_state = XBEE_STATE_WAITING_TO_JOIN;
80
    enum XBEE_STATE xbee_state = XBEE_STATE_WAITING_TO_JOIN;
81
    XBEE_ADDRESS_64 last_recv_address = {0};
81
    XBEE_ADDRESS_16 parent_address_16;
82
    XBEE_ADDRESS_16 converter16;
82
    XBEE_ADDRESS_64 parent_address_64;
83
    
83
#endif
-
 
84
 
84
    // Pointers to allow parsing of xbee data from arbitrary byte array
85
    // Pointers to allow parsing of xbee data from arbitrary byte array
-
 
86
    XBEE_TX_DATA_PACKET_FRAME *frame_tx_data;
-
 
87
    XBEE_TX_AT_COMMAND_FRAME *frame_tx_at_command;
-
 
88
    
85
    XBEE_RX_AT_COMMAND_RESPONSE_FRAME *frame_at_cmd_response;
89
    XBEE_RX_AT_COMMAND_RESPONSE_FRAME *frame_at_cmd_response;
86
    XBEE_RX_DATA_PACKET_FRAME *frame_data_packet;
90
    XBEE_RX_DATA_PACKET_FRAME *frame_data_packet;
87
    XBEE_RX_DATA_TX_STATUS_FRAME *frame_tx_status;
91
    XBEE_RX_DATA_TX_STATUS_FRAME *frame_tx_status;
88
    XBEE_RX_IO_DATA_SAMPLE_FRAME *frame_io_sample;
92
    XBEE_RX_IO_DATA_SAMPLE_FRAME *frame_io_sample;
89
    XBEE_RX_EXPLICIT_COMMAND_FRAME *frame_explicit_cmd;
93
    XBEE_RX_EXPLICIT_COMMAND_FRAME *frame_explicit_cmd;
90
    XBEE_RX_REMOTE_AT_COMMAND_FRAME *frame_remote_at_cmd;
94
    XBEE_RX_REMOTE_AT_COMMAND_FRAME *frame_remote_at_cmd;
91
    XBEE_RX_ROUTE_RECORD_FRAME *frame_route_record;
95
    XBEE_RX_ROUTE_RECORD_FRAME *frame_route_record;
92
    XBEE_RX_NODE_IDENTIFICATION_INDICATOR_FRAME *frame_node_identification;
96
    XBEE_RX_NODE_IDENTIFICATION_INDICATOR_FRAME *frame_node_identification;
93
    XBEE_RX_MODEM_STATUS_FRAME *frame_modem_status;
97
    XBEE_RX_MODEM_STATUS_FRAME *frame_modem_status;
94
 
-
 
95
    XBEE_TX_DATA_PACKET_FRAME *frame_tx_data;
-
 
96
    XBEE_TX_AT_COMMAND_FRAME *frame_tx_at_command;
-
 
97
 
98
    
98
    /* --------------------- Oscillator Configuration --------------------- */
99
    /* --------------------- Oscillator Configuration --------------------- */
99
    OSCTUNEbits.PLLEN = 1;          // Enable 4x PLL
100
    OSCTUNEbits.PLLEN = 1;          // Enable 4x PLL
100
    OSCCONbits.IRCF = 0b111;        // Set INTOSC postscaler to 8MHz
101
    OSCCONbits.IRCF = 0b111;        // Set INTOSC postscaler to 8MHz
101
    OSCCONbits.SCS = 0b00; // Use PLL as primary clock source
102
    OSCCONbits.SCS = 0b00; // Use PLL as primary clock source
102
    /* -------------------------------------------------------------------- */
103
    /* -------------------------------------------------------------------- */
Line 121... Line 122...
121
 
122
 
122
    interrupt_enable(); // Enable high-priority interrupts and low-priority interrupts
123
    interrupt_enable(); // Enable high-priority interrupts and low-priority interrupts
123
    interrupt_init(); // Initialize the interrupt priorities
124
    interrupt_init(); // Initialize the interrupt priorities
124
#ifdef _BASE_STATION
125
#ifdef _BASE_STATION
125
    i2c_configure_slave(BASE_STATION_ADDRESS);
126
    i2c_configure_slave(BASE_STATION_ADDRESS);
-
 
127
    led_driver_num(BASE_STATION_ADDRESS);
126
#endif
128
#endif
127
#ifdef _REMOTE
129
#ifdef _REMOTE
128
    i2c_configure_master(); // Configure the hardware i2c device as a master
130
    i2c_configure_master(); // Configure the hardware i2c device as a master
129
    imu_init();
131
    imu_init();
130
#endif
132
#endif
Line 179... Line 181...
179
                            if (buffer_data.stored_length > MSGLEN) {
181
                            if (buffer_data.stored_length > MSGLEN) {
180
                                msgbuffer[0] = MSGLEN;
182
                                msgbuffer[0] = MSGLEN;
181
                            } else {
183
                            } else {
182
                                msgbuffer[0] = buffer_data.stored_length;
184
                                msgbuffer[0] = buffer_data.stored_length;
183
                            }
185
                            }
-
 
186
                            i2c_last_req_size = msgbuffer[0];
184
                            DBG_PRINT_MAIN("Main: (I2C Return 0x2) Returning %d bytes\r\n", length);
187
                            DBG_PRINT_MAIN("Main: (I2C Return 0x2) Returning %d\r\n", msgbuffer[0]);
185
                            MQ_sendmsg_FromMainToHigh(length, MSGTYPE_I2C_REPLY, (void *) msgbuffer);
188
                            MQ_sendmsg_FromMainToHigh(length, MSGTYPE_I2C_REPLY, (void *) msgbuffer);
186
                            break;
189
                            break;
187
                        case 0x4:
190
                        case 0x4:
188
                            // Return data stored in buffer
191
                            // 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);
192
                            buffer_read(i2c_last_req_size, msgbuffer);
195
                            }
-
 
196
                            DBG_PRINT_MAIN("Main: (I2C Return 0x4) Returning %d bytes\r\n", length);
193
                            DBG_PRINT_MAIN("Main: (I2C Return 0x4) Returning %d bytes\r\n", i2c_last_req_size);
197
                            MQ_sendmsg_FromMainToHigh(length, MSGTYPE_I2C_REPLY, (void *) msgbuffer);
194
                            MQ_sendmsg_FromMainToHigh(i2c_last_req_size, MSGTYPE_I2C_REPLY, (void *) msgbuffer);
198
                            pwm_LED_off();  // Turn off LED if it was on
195
                            pwm_LED_off();  // Turn off LED if it was on
199
                            break;
196
                            break;
200
                        case 0x6:
197
                        case 0x6:
-
 
198
                            // Return status of IR signal input
-
 
199
                            length = 1;
-
 
200
                            msgbuffer[0] = IR_receive_flag;
-
 
201
                            DBG_PRINT_MAIN("Main: Returning IR state %d\r\n", IR_receive_flag);
-
 
202
                            MQ_sendmsg_FromMainToHigh(length, MSGTYPE_I2C_REPLY, (void *) msgbuffer);
201
                            break;
203
                            break;
202
                        case 0x7:
204
                        case 0x7:
-
 
205
                            // Send value to specified remote
-
 
206
 
-
 
207
                            remote_address_64.UPPER_32.char_value[0] = msgbuffer[1];
-
 
208
                            remote_address_64.UPPER_32.char_value[1] = msgbuffer[2];
-
 
209
                            remote_address_64.UPPER_32.char_value[2] = msgbuffer[3];
-
 
210
                            remote_address_64.UPPER_32.char_value[3] = msgbuffer[4];
-
 
211
                            remote_address_64.LOWER_32.char_value[0] = msgbuffer[5];
-
 
212
                            remote_address_64.LOWER_32.char_value[1] = msgbuffer[6];
-
 
213
                            remote_address_64.LOWER_32.char_value[2] = msgbuffer[7];
-
 
214
                            remote_address_64.LOWER_32.char_value[3] = msgbuffer[8];
-
 
215
                            remote_address_16.INT_16.char_value[0] = msgbuffer[9];
-
 
216
                            remote_address_16.INT_16.char_value[1] = msgbuffer[10];
-
 
217
                            DBG_PRINT_MAIN("Main: Sending %X to (64) %08lX %08lX (16) %04X\r\n",
203
                            break;
218
                                    msgbuffer[11],
-
 
219
                                    remote_address_64.UPPER_32.long_value,
-
 
220
                                    remote_address_64.LOWER_32.long_value,
-
 
221
                                    remote_address_16.INT_16.int_value);
-
 
222
 
204
                        case 0x8:
223
                            frame_tx_data = (void *) msgbuffer;
-
 
224
                            frame_tx_data->data[0] = msgbuffer[11];
-
 
225
                            frame_tx_data->frame_type = XBEE_TX_DATA_PACKET;
205
                            break;
226
                            frame_tx_data->frame_id = 0;
-
 
227
                            frame_tx_data->destination_64.UPPER_32.long_value = remote_address_64.UPPER_32.long_value;
-
 
228
                            frame_tx_data->destination_64.LOWER_32.long_value = remote_address_64.LOWER_32.long_value;
-
 
229
                            ConvertEndian64(&frame_tx_data->destination_64);
-
 
230
                            frame_tx_data->destination_16.INT_16.int_value = remote_address_16.INT_16.int_value;
-
 
231
                            ConvertEndian16(&frame_tx_data->destination_16);
206
                        case 0x9:
232
                            frame_tx_data->broadcast_radius = 0;
-
 
233
                            frame_tx_data->options = 0x01; // Disable ACK
-
 
234
 
-
 
235
                            length = XBEE_TX_DATA_PACKET_FRAME_SIZE + 1;
-
 
236
                            xbee_process_transmit_frame((void *) msgbuffer, length);
207
                            break;
237
                            break;
208
                        default:
238
                        default:
209
                            DBG_PRINT_MAIN("Main: (ERROR) Unexpected message type recieved: %d\r\n", msgbuffer[0]);
239
                            DBG_PRINT_MAIN("Main: (ERROR) Unexpected message type recieved: %d\r\n", msgbuffer[0]);
210
                            break;
240
                            break;
211
                    };
241
                    };
Line 239... Line 269...
239
                        wake();
269
                        wake();
240
                        // Send data to base station
270
                        // Send data to base station
241
                        frame_tx_data = (void *) msgbuffer;
271
                        frame_tx_data = (void *) msgbuffer;
242
                        frame_tx_data->frame_type = XBEE_TX_DATA_PACKET;
272
                        frame_tx_data->frame_type = XBEE_TX_DATA_PACKET;
243
                        frame_tx_data->frame_id = 0;
273
                        frame_tx_data->frame_id = 0;
244
                        frame_tx_data->destination_64.UPPER_32.long_value = 0x00000000;
274
                        frame_tx_data->destination_64.UPPER_32.long_value = parent_address_64.UPPER_32.long_value;
245
                        frame_tx_data->destination_64.LOWER_32.long_value = 0x00000000;
275
                        frame_tx_data->destination_64.LOWER_32.long_value = parent_address_64.LOWER_32.long_value;
246
                        ConvertEndian64(&frame_tx_data->destination_64);
276
                        ConvertEndian64(&frame_tx_data->destination_64);
247
                        frame_tx_data->destination_16.INT_16.int_value = xbee_parent_address;
277
                        frame_tx_data->destination_16.INT_16.int_value = xbee_parent_address_16;
248
                        ConvertEndian16(&frame_tx_data->destination_16);
278
                        ConvertEndian16(&frame_tx_data->destination_16);
249
                        frame_tx_data->broadcast_radius = 0;
279
                        frame_tx_data->broadcast_radius = 0;
250
                        frame_tx_data->options = 0x01;  // Disable ACK
280
                        frame_tx_data->options = 0x01;  // Disable ACK
251
                        
281
                        
252
                        length = XBEE_TX_DATA_PACKET_FRAME_SIZE + buffer_data.stored_length;
282
                        length = XBEE_TX_DATA_PACKET_FRAME_SIZE + buffer_data.stored_length;
Line 261... Line 291...
261
#endif
291
#endif
262
                    /* -----------------------------------------------------------*/
292
                    /* -----------------------------------------------------------*/
263
 
293
 
264
                    /* --- XBee Message Handlers ---------------------------------*/
294
                    /* --- XBee Message Handlers ---------------------------------*/
265
                case MSGTYPE_XBEE_RX_AT_COMMAND_RESPONSE:
295
                case MSGTYPE_XBEE_RX_AT_COMMAND_RESPONSE:
266
                    DBG_PRINT_MAIN("Main: XBee AT command response frame\r\n");
296
                    DBG_PRINT_MAIN("\r\n");
267
                    frame_at_cmd_response = (void *) msgbuffer;
297
                    frame_at_cmd_response = (void *) msgbuffer;
-
 
298
                    DBG_PRINT_MAIN("Main: XBee AT command response frame\r\n");
268
                    DBG_PRINT_MAIN("Command: %c%c\r\n", frame_at_cmd_response->command[0], frame_at_cmd_response->command[1]);
299
                    DBG_PRINT_MAIN("Command: %c%c\r\n", frame_at_cmd_response->command[0], frame_at_cmd_response->command[1]);
269
                    DBG_PRINT_MAIN("Status: %d\r\n", frame_at_cmd_response->command_status);
300
                    DBG_PRINT_MAIN("Status: %d\r\n", frame_at_cmd_response->command_status);
270
                    DBG_PRINT_MAIN("Data: ");
301
                    DBG_PRINT_MAIN("Data: ");
271
                    for (i = 0; i < length - XBEE_RX_AT_COMMAND_RESPONSE_FRAME_SIZE; i++) {
302
                    for (i = 0; i < length - XBEE_RX_AT_COMMAND_RESPONSE_FRAME_SIZE; i++) {
272
                        DBG_PRINT_MAIN("%02X ", frame_data_packet->data[i]);
303
                        DBG_PRINT_MAIN("%02X ", frame_at_cmd_response->data[i]);
273
                    }
304
                    }
274
                    DBG_PRINT_MAIN("\r\n");
305
                    DBG_PRINT_MAIN("\r\n");
275
#ifdef _REMOTE
306
#ifdef _REMOTE
-
 
307
                    // Parse response from 'MP' command
276
                    if (frame_at_cmd_response->command[0] == 'M' &&
308
                    if (frame_at_cmd_response->command[0] == 'M' &&
277
                            frame_at_cmd_response->command[1] == 'P') {
309
                            frame_at_cmd_response->command[1] == 'P') {
278
                        converter16.INT_16.char_value[0] = frame_at_cmd_response->data[0];
310
                        parent_address_16.INT_16.char_value[0] = frame_at_cmd_response->data[0];
279
                        converter16.INT_16.char_value[1] = frame_at_cmd_response->data[1];
311
                        parent_address_16.INT_16.char_value[1] = frame_at_cmd_response->data[1];
280
                        ConvertEndian16(&converter16);
312
                        ConvertEndian16(&parent_address_16);
281
                        xbee_parent_address = converter16.INT_16.int_value;
313
                        xbee_parent_address_16 = parent_address_16.INT_16.int_value;
-
 
314
 
-
 
315
                        DBG_PRINT_MAIN("Main: XBee parent address (16): %04X\r\n", xbee_parent_address_16);
-
 
316
                        // Set broadcast radius to 1
-
 
317
                        frame_tx_at_command = (void *) msgbuffer;
-
 
318
                        frame_tx_at_command->frame_type = XBEE_TX_AT_COMMAND;
-
 
319
                        frame_tx_at_command->frame_id = 1;
-
 
320
                        frame_tx_at_command->command[0] = 'B';
-
 
321
                        frame_tx_at_command->command[1] = 'H';
-
 
322
                        frame_tx_at_command->data[0] = 1;
-
 
323
                        length = XBEE_TX_AT_COMMAND_FRAME_SIZE + 1;
-
 
324
                        xbee_process_transmit_frame((void *) msgbuffer, length);
-
 
325
 
-
 
326
                        // Query for parent node info
-
 
327
                        frame_tx_at_command = (void *) msgbuffer;
-
 
328
                        frame_tx_at_command->frame_type = XBEE_TX_AT_COMMAND;
-
 
329
                        frame_tx_at_command->frame_id = 1;
-
 
330
                        frame_tx_at_command->command[0] = 'N';
-
 
331
                        frame_tx_at_command->command[1] = 'D';
-
 
332
                        length = XBEE_TX_AT_COMMAND_FRAME_SIZE;
-
 
333
                        xbee_process_transmit_frame((void *) msgbuffer, length);
-
 
334
                    }
-
 
335
                    // Parse response from 'ND' command
-
 
336
                    else if (frame_at_cmd_response->command[0] == 'N' &&
-
 
337
                            frame_at_cmd_response->command[1] == 'D') {
-
 
338
                        parent_address_64.UPPER_32.char_value[0] = frame_at_cmd_response->data[2];
-
 
339
                        parent_address_64.UPPER_32.char_value[1] = frame_at_cmd_response->data[3];
-
 
340
                        parent_address_64.UPPER_32.char_value[2] = frame_at_cmd_response->data[4];
-
 
341
                        parent_address_64.UPPER_32.char_value[3] = frame_at_cmd_response->data[5];
-
 
342
                        parent_address_64.LOWER_32.char_value[0] = frame_at_cmd_response->data[6];
-
 
343
                        parent_address_64.LOWER_32.char_value[1] = frame_at_cmd_response->data[7];
-
 
344
                        parent_address_64.LOWER_32.char_value[2] = frame_at_cmd_response->data[8];
-
 
345
                        parent_address_64.LOWER_32.char_value[3] = frame_at_cmd_response->data[9];
-
 
346
                        ConvertEndian64(&parent_address_64);
-
 
347
 
-
 
348
                        DBG_PRINT_MAIN("Main: XBee parent address (64): %08lX %08lX\r\n",
-
 
349
                                parent_address_64.UPPER_32.long_value,
-
 
350
                                parent_address_64.LOWER_32.long_value);
-
 
351
                        
282
                        xbee_state = XBEE_STATE_JOINED_NETWORK;
352
                        xbee_state = XBEE_STATE_JOINED_NETWORK;
-
 
353
                        // Turn off LED after XBee has joined network
-
 
354
                        pwm_LED_off();
283
                    }
355
                    }
284
#endif
356
#endif
285
                    break;
357
                    break;
286
                case MSGTYPE_XBEE_RX_DATA_PACKET:
358
                case MSGTYPE_XBEE_RX_DATA_PACKET:
287
                    wake();
359
                    wake();
Line 300... Line 372...
300
                        DBG_PRINT_MAIN("%02X ", frame_data_packet->data[i]);
372
                        DBG_PRINT_MAIN("%02X ", frame_data_packet->data[i]);
301
                    }
373
                    }
302
                    DBG_PRINT_MAIN("\r\n");
374
                    DBG_PRINT_MAIN("\r\n");
303
#ifdef _BASE_STATION
375
#ifdef _BASE_STATION
304
 
376
 
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 ||
377
                    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) {
378
                            frame_data_packet->source_64.UPPER_32.long_value != last_recv_address.UPPER_32.long_value) {
308
                        // Receive data from new remote
379
                        // Save remote's 64 and 16 bit addresses
-
 
380
                        buffer_insert_one(RETURNID_NEW_REMOTE_SRC);
309
                        buffer_insert(4, frame_data_packet->source_64.UPPER_32.char_value);
381
                        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);
382
                        buffer_insert(4, frame_data_packet->source_64.LOWER_32.char_value);
-
 
383
                        buffer_insert(2, frame_data_packet->source_16.INT_16.char_value);
311
                        // Save data
384
                        // Save data
312
                        buffer_insert(length - XBEE_RX_DATA_PACKET_FRAME_SIZE, frame_data_packet->data);
385
                        buffer_insert(length - XBEE_RX_DATA_PACKET_FRAME_SIZE, frame_data_packet->data);
313
                        // Save address of sender
386
                        // Save address of sender
314
                        last_recv_address.UPPER_32.long_value = frame_data_packet->source_64.UPPER_32.long_value;
387
                        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;
388
                        last_recv_address.LOWER_32.long_value = frame_data_packet->source_64.LOWER_32.long_value;
Line 319... Line 392...
319
                    }
392
                    }
320
                    
393
                    
321
                    // Send value of first byte received to LED display
394
                    // Send value of first byte received to LED display
322
                    led_driver_num(frame_data_packet->data[0]);
395
                    led_driver_num(frame_data_packet->data[0]);
323
#endif
396
#endif
-
 
397
#ifdef _REMOTE
-
 
398
                    led_driver_num(frame_data_packet->data[0]);
-
 
399
#endif
324
                    sleep();
400
                    sleep();
325
                    break;
401
                    break;
326
                case MSGTYPE_XBEE_RX_DATA_TX_STATUS:
402
                case MSGTYPE_XBEE_RX_DATA_TX_STATUS:
327
                    DBG_PRINT_MAIN("Main: XBee TX status frame\r\n");
403
                    DBG_PRINT_MAIN("Main: XBee TX status frame\r\n");
328
                    frame_tx_status = (void *) msgbuffer;
404
                    frame_tx_status = (void *) msgbuffer;
Line 363... Line 439...
363
                    DBG_PRINT_MAIN("Remote 64: %08lX-%08lX\r\n",
439
                    DBG_PRINT_MAIN("Remote 64: %08lX-%08lX\r\n",
364
                            frame_node_identification->remote_64.UPPER_32.long_value,
440
                            frame_node_identification->remote_64.UPPER_32.long_value,
365
                            frame_node_identification->remote_64.LOWER_32.long_value
441
                            frame_node_identification->remote_64.LOWER_32.long_value
366
                            );
442
                            );
367
                    DBG_PRINT_MAIN("Remote 16: %04X\r\n", frame_node_identification->remote_16.INT_16.int_value);
443
                    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);
444
                    DBG_PRINT_MAIN("Parent 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);
445
                    DBG_PRINT_MAIN("Receive Options: %02X\r\n", frame_node_identification->recieve_options);
-
 
446
                    buffer_insert_one(RETURNID_NEW_REMOTE_CON);
-
 
447
                    buffer_insert(4, frame_node_identification->remote_64.UPPER_32.char_value);
-
 
448
                    buffer_insert(4, frame_node_identification->remote_64.LOWER_32.char_value);
-
 
449
                    buffer_insert(2, frame_node_identification->remote_16.INT_16.char_value);
370
                    break;
450
                    break;
371
                case MSGTYPE_XBEE_RX_FRAME_MODEM_STATUS:
451
                case MSGTYPE_XBEE_RX_FRAME_MODEM_STATUS:
372
                    DBG_PRINT_MAIN("Main: XBee modem status frame\r\n");
452
                    DBG_PRINT_MAIN("Main: XBee modem status frame\r\n");
373
                    frame_modem_status = (void *) msgbuffer;
453
                    frame_modem_status = (void *) msgbuffer;
374
                    DBG_PRINT_MAIN("Status: %02X (", frame_modem_status->status);
454
                    DBG_PRINT_MAIN("Status: %02X (", frame_modem_status->status);
375
                    switch(frame_modem_status->status) {
455
                    switch(frame_modem_status->status) {
376
                        case 0:
456
                        case 0:
377
                            DBG_PRINT_MAIN("Hardware Reset");
457
                            DBG_PRINT_MAIN("Hardware Reset");
-
 
458
#ifdef _REMOTE
378
                            xbee_state = XBEE_STATE_WAITING_TO_JOIN;
459
                            xbee_state = XBEE_STATE_WAITING_TO_JOIN;
-
 
460
#endif
379
                            break;
461
                            break;
380
                        case 1:
462
                        case 1:
381
                            DBG_PRINT_MAIN("Watchdog Timer Reset");
463
                            DBG_PRINT_MAIN("Watchdog Timer Reset");
382
                            break;
464
                            break;
383
                        case 2:
465
                        case 2:
384
                            DBG_PRINT_MAIN("Joined Network");
466
                            DBG_PRINT_MAIN("Joined Network");
385
#ifdef _BASE_STATION
-
 
386
                            xbee_state = XBEE_STATE_JOINED_NETWORK;
-
 
387
#endif
-
 
388
#ifdef _REMOTE
467
#ifdef _REMOTE
389
                            // Query for parent's address
468
                            // Query for parent's address
390
                            frame_tx_at_command = (void *) msgbuffer;
469
                            frame_tx_at_command = (void *) msgbuffer;
391
                            frame_tx_at_command->frame_type = XBEE_TX_AT_COMMAND;
470
                            frame_tx_at_command->frame_type = XBEE_TX_AT_COMMAND;
392
                            frame_tx_at_command->frame_id = 1;
471
                            frame_tx_at_command->frame_id = 1;
393
                            frame_tx_at_command->command[0] = 'M';
472
                            frame_tx_at_command->command[0] = 'M';
394
                            frame_tx_at_command->command[1] = 'P';
473
                            frame_tx_at_command->command[1] = 'P';
395
//                            length = XBEE_TX_AT_COMMAND_FRAME_SIZE;
474
                            length = XBEE_TX_AT_COMMAND_FRAME_SIZE;
396
                            xbee_process_transmit_frame((void *) msgbuffer, 4);
475
                            xbee_process_transmit_frame((void *) msgbuffer, length);
397
                            // Turn off LED after XBee has joined network
-
 
398
                            pwm_LED_off();
-
 
399
#endif
476
#endif
400
                            break;
477
                            break;
401
                        case 3:
478
                        case 3:
402
                            DBG_PRINT_MAIN("Disassociated");
479
                            DBG_PRINT_MAIN("Disassociated");
-
 
480
                            pwm_LED_on();
403
                            break;
481
                            break;
404
                        case 6:
482
                        case 6:
405
                            DBG_PRINT_MAIN("Coordinator Started");
483
                            DBG_PRINT_MAIN("Coordinator Started");
406
                            break;
484
                            break;
407
                        case 7:
485
                        case 7:
Line 413... Line 491...
413
                    }
491
                    }
414
                    DBG_PRINT_MAIN(")\r\n");
492
                    DBG_PRINT_MAIN(")\r\n");
415
                    break;
493
                    break;
416
                    /* -----------------------------------------------------------*/
494
                    /* -----------------------------------------------------------*/
417
            };
495
            };
418
            continue;
496
//            continue;
419
        }
497
        }
420
 
498
 
421
        // Process low priority queue
499
        // Process low priority queue
422
        length = MQ_recvmsg_ToMainFromLow(MSGLEN, &msgtype, (void *) msgbuffer);
500
        length = MQ_recvmsg_ToMainFromLow(MSGLEN, &msgtype, (void *) msgbuffer);
423
        if (length < 0) {
501
        if (length < 0) {
Line 425... Line 503...
425
            if (length != MSG_QUEUE_EMPTY) {
503
            if (length != MSG_QUEUE_EMPTY) {
426
                DBG_PRINT_MAIN("Main: (ERROR) Bad low priority receive, code = %d\r\n", length);
504
                DBG_PRINT_MAIN("Main: (ERROR) Bad low priority receive, code = %d\r\n", length);
427
            }
505
            }
428
        } else {
506
        } else {
429
            switch (msgtype) {
507
            switch (msgtype) {
-
 
508
                case MSGTYPE_INT1:
-
 
509
#ifdef _BASE_STATION
-
 
510
                    wake();
-
 
511
                    IR_receive_flag = 1;
-
 
512
                    pwm_LED_on();
-
 
513
//                    timer2_enable();    // Enable timer 2 to turn off LED
-
 
514
                    sleep();
-
 
515
//                    DBG_PRINT_MAIN("Main: INT1 Interrupt\r\n");
-
 
516
#endif
-
 
517
                    break;
430
                /* --- Port B Interrupt Handlers -----------------------------*/
518
                /* --- Port B Interrupt Handlers -----------------------------*/
431
                case MSGTYPE_PORTB_4_DOWN:
519
                case MSGTYPE_PORTB_4_DOWN:
432
                    DBG_PRINT_MAIN("Main: Port B4 Down\r\n");
520
                    DBG_PRINT_MAIN("Main: Port B4 Down\r\n");
433
#ifdef _REMOTE
521
#ifdef _REMOTE
434
                    if (xbee_state == XBEE_STATE_JOINED_NETWORK) {
522
                    if (xbee_state == XBEE_STATE_JOINED_NETWORK) {
Line 447... Line 535...
447
                        
535
                        
448
                        // Send remaining buffer data to base station
536
                        // Send remaining buffer data to base station
449
                        frame_tx_data = (void *) msgbuffer;
537
                        frame_tx_data = (void *) msgbuffer;
450
                        frame_tx_data->frame_type = XBEE_TX_DATA_PACKET;
538
                        frame_tx_data->frame_type = XBEE_TX_DATA_PACKET;
451
                        frame_tx_data->frame_id = 0;
539
                        frame_tx_data->frame_id = 0;
452
                        frame_tx_data->destination_64.UPPER_32.long_value = 0x00000000;
540
                        frame_tx_data->destination_64.UPPER_32.long_value = parent_address_64.UPPER_32.long_value;
453
                        frame_tx_data->destination_64.LOWER_32.long_value = 0x00000000;
541
                        frame_tx_data->destination_64.LOWER_32.long_value = parent_address_64.LOWER_32.long_value;
454
                        ConvertEndian64(&frame_tx_data->destination_64);
542
                        ConvertEndian64(&frame_tx_data->destination_64);
455
                        frame_tx_data->destination_16.INT_16.int_value = xbee_parent_address;
543
                        frame_tx_data->destination_16.INT_16.int_value = xbee_parent_address_16;
456
                        ConvertEndian16(&frame_tx_data->destination_16);
544
                        ConvertEndian16(&frame_tx_data->destination_16);
457
                        frame_tx_data->broadcast_radius = 0;
545
                        frame_tx_data->broadcast_radius = 0;
458
                        frame_tx_data->options = 0x01;  // Disable ACK
546
                        frame_tx_data->options = 0x01;  // Disable ACK
459
                        
547
                        
460
                        length = XBEE_TX_DATA_PACKET_FRAME_SIZE + buffer_data.stored_length;
548
                        length = XBEE_TX_DATA_PACKET_FRAME_SIZE + buffer_data.stored_length;
Line 474... Line 562...
474
                        wake(); // Wake up all components
562
                        wake(); // Wake up all components
475
                        timer3_enable(); // Enable PWM timer
563
                        timer3_enable(); // Enable PWM timer
476
                        frame_tx_data = (void *) msgbuffer;
564
                        frame_tx_data = (void *) msgbuffer;
477
                        frame_tx_data->frame_type = XBEE_TX_DATA_PACKET;
565
                        frame_tx_data->frame_type = XBEE_TX_DATA_PACKET;
478
                        frame_tx_data->frame_id = 0;
566
                        frame_tx_data->frame_id = 0;
479
                        frame_tx_data->destination_64.UPPER_32.long_value = 0x00000000;
567
                        frame_tx_data->destination_64.UPPER_32.long_value = parent_address_64.UPPER_32.long_value;
480
                        frame_tx_data->destination_64.LOWER_32.long_value = 0x00000000;
568
                        frame_tx_data->destination_64.LOWER_32.long_value = parent_address_64.LOWER_32.long_value;
481
                        ConvertEndian64(&frame_tx_data->destination_64);
569
                        ConvertEndian64(&frame_tx_data->destination_64);
482
                        frame_tx_data->destination_16.INT_16.int_value = xbee_parent_address;
570
                        frame_tx_data->destination_16.INT_16.int_value = xbee_parent_address_16;
483
                        ConvertEndian16(&frame_tx_data->destination_16);
571
                        ConvertEndian16(&frame_tx_data->destination_16);
484
                        frame_tx_data->broadcast_radius = 0;
572
                        frame_tx_data->broadcast_radius = 0;
485
                        frame_tx_data->options = 0x01; // Disable ACK
573
                        frame_tx_data->options = 0x01; // Disable ACK
486
                        frame_tx_data->data[0] = RETURNID_CONN;
574
                        frame_tx_data->data[0] = RETURNID_CONN;
487
 
575
 
Line 505... Line 593...
505
                    if (xbee_state == XBEE_STATE_JOINED_NETWORK) {
593
                    if (xbee_state == XBEE_STATE_JOINED_NETWORK) {
506
                        wake(); // Wake up all components
594
                        wake(); // Wake up all components
507
                        frame_tx_data = (void *) msgbuffer;
595
                        frame_tx_data = (void *) msgbuffer;
508
                        frame_tx_data->frame_type = XBEE_TX_DATA_PACKET;
596
                        frame_tx_data->frame_type = XBEE_TX_DATA_PACKET;
509
                        frame_tx_data->frame_id = 0;
597
                        frame_tx_data->frame_id = 0;
510
                        frame_tx_data->destination_64.UPPER_32.long_value = 0x00000000;
598
                        frame_tx_data->destination_64.UPPER_32.long_value = parent_address_64.UPPER_32.long_value;
511
                        frame_tx_data->destination_64.LOWER_32.long_value = 0x00000000;
599
                        frame_tx_data->destination_64.LOWER_32.long_value = parent_address_64.LOWER_32.long_value;
512
                        ConvertEndian64(&frame_tx_data->destination_64);
600
                        ConvertEndian64(&frame_tx_data->destination_64);
513
                        frame_tx_data->destination_16.INT_16.int_value = xbee_parent_address;
601
                        frame_tx_data->destination_16.INT_16.int_value = xbee_parent_address_16;
514
                        ConvertEndian16(&frame_tx_data->destination_16);
602
                        ConvertEndian16(&frame_tx_data->destination_16);
515
                        frame_tx_data->broadcast_radius = 0;
603
                        frame_tx_data->broadcast_radius = 0;
516
                        frame_tx_data->options = 0x01; // Disable ACK
604
                        frame_tx_data->options = 0x01; // Disable ACK
517
                        frame_tx_data->data[0] = RETURNID_BTN1;
605
                        frame_tx_data->data[0] = RETURNID_BTN2;
518
 
606
 
519
                        length = XBEE_TX_DATA_PACKET_FRAME_SIZE + 1;
607
                        length = XBEE_TX_DATA_PACKET_FRAME_SIZE + 1;
520
                        xbee_process_transmit_frame((void *) msgbuffer, length);
608
                        xbee_process_transmit_frame((void *) msgbuffer, length);
521
                    }
609
                    }
522
#endif
610
#endif
Line 535... Line 623...
535
                    if (xbee_state == XBEE_STATE_JOINED_NETWORK) {
623
                    if (xbee_state == XBEE_STATE_JOINED_NETWORK) {
536
                        wake(); // Wake up all components
624
                        wake(); // Wake up all components
537
                        frame_tx_data = (void *) msgbuffer;
625
                        frame_tx_data = (void *) msgbuffer;
538
                        frame_tx_data->frame_type = XBEE_TX_DATA_PACKET;
626
                        frame_tx_data->frame_type = XBEE_TX_DATA_PACKET;
539
                        frame_tx_data->frame_id = 0;
627
                        frame_tx_data->frame_id = 0;
540
                        frame_tx_data->destination_64.UPPER_32.long_value = 0x00000000;
628
                        frame_tx_data->destination_64.UPPER_32.long_value = parent_address_64.UPPER_32.long_value;
541
                        frame_tx_data->destination_64.LOWER_32.long_value = 0x00000000;
629
                        frame_tx_data->destination_64.LOWER_32.long_value = parent_address_64.LOWER_32.long_value;
542
                        ConvertEndian64(&frame_tx_data->destination_64);
630
                        ConvertEndian64(&frame_tx_data->destination_64);
543
                        frame_tx_data->destination_16.INT_16.int_value = xbee_parent_address;
631
                        frame_tx_data->destination_16.INT_16.int_value = xbee_parent_address_16;
544
                        ConvertEndian16(&frame_tx_data->destination_16);
632
                        ConvertEndian16(&frame_tx_data->destination_16);
545
                        frame_tx_data->broadcast_radius = 0;
633
                        frame_tx_data->broadcast_radius = 0;
546
                        frame_tx_data->options = 0x01; // Disable ACK
634
                        frame_tx_data->options = 0x01; // Disable ACK
547
                        frame_tx_data->data[0] = RETURNID_BTN2;
635
                        frame_tx_data->data[0] = RETURNID_BTN1;
548
 
636
 
549
                        length = XBEE_TX_DATA_PACKET_FRAME_SIZE + 1;
637
                        length = XBEE_TX_DATA_PACKET_FRAME_SIZE + 1;
550
                        xbee_process_transmit_frame((void *) msgbuffer, length);
638
                        xbee_process_transmit_frame((void *) msgbuffer, length);;
551
                    }
639
                    }
552
#endif
640
#endif
553
                    break;
641
                    break;
554
                case MSGTYPE_PORTB_7_UP:
642
                case MSGTYPE_PORTB_7_UP:
555
                    DBG_PRINT_MAIN("Main: Port B7 Up\r\n");
643
                    DBG_PRINT_MAIN("Main: Port B7 Up\r\n");
Line 557... Line 645...
557
                    if (xbee_state == XBEE_STATE_JOINED_NETWORK) {
645
                    if (xbee_state == XBEE_STATE_JOINED_NETWORK) {
558
                        sleep();
646
                        sleep();
559
                    }
647
                    }
560
#endif
648
#endif
561
                    break;
649
                    break;
562
                case MSGTYPE_INT1:
-
 
563
#ifdef _BASE_STATION
-
 
564
                    IR_receive_flag = 1;
-
 
565
                    pwm_LED_toggle();
-
 
566
                    Delay10KTCYx(1);
-
 
567
                    pwm_LED_toggle();
-
 
568
                    sleep();
-
 
569
//                    DBG_PRINT_MAIN("Main: INT1 Interrupt\r\n");
-
 
570
#endif
-
 
571
                    break;
-
 
572
                    /* -----------------------------------------------------------*/
650
                    /* -----------------------------------------------------------*/
573
                    /* --- Timer Interrupt Handlers ------------------------------*/
651
                    /* --- Timer Interrupt Handlers ------------------------------*/
574
                case MSGTYPE_TIMER0:
652
                case MSGTYPE_TIMER0:
575
                    DBG_PRINT_MAIN("Main: Timer 0 Interrupt\r\n");
653
                    DBG_PRINT_MAIN("Main: Timer 0 Interrupt\r\n");
576
                    IR_receive_flag = 0;
654
                    pwm_LED_off();
-
 
655
                    IR_receive_flag = 0;    // Reset IR receive flag
-
 
656
                    INTCON3bits.INT1IE = 1; // Turn on INT1 interrupt
577
                    sleep_enable();
657
                    sleep_enable();
578
                    break;
658
                    break;
579
                case MSGTYPE_TIMER1:
659
                case MSGTYPE_TIMER1:
580
                    DBG_PRINT_MAIN("Main: Timer 1 Interrupt\r\n");
660
                    DBG_PRINT_MAIN("Main: Timer 1 Interrupt\r\n");
581
#ifdef _REMOTE
661
#ifdef _REMOTE
Line 611... Line 691...
611
                        imu_read_acc();
691
                        imu_read_acc();
612
                        i2c_state = I2C_STATE_READ_ACC;
692
                        i2c_state = I2C_STATE_READ_ACC;
613
                    }
693
                    }
614
#endif
694
#endif
615
                    break;
695
                    break;
-
 
696
                case MSGTYPE_TIMER2:
-
 
697
                    DBG_PRINT_MAIN("Main: Timer 2 Interrupt\r\n");
-
 
698
#ifdef _BASE_STATION
-
 
699
                    IR_receive_flag = 0;
-
 
700
                    pwm_LED_off();
-
 
701
                    INTCON3bits.INT1IE = 1; // Turn on INT1 interrupt
-
 
702
#endif
-
 
703
                    break;
616
//                case MSGTYPE_ADC_NEWVALUE:
704
//                case MSGTYPE_ADC_NEWVALUE:
617
//                    // Get the value in the ADC
705
//                    // Get the value in the ADC
618
//                    adc_last_value = *((unsigned int*) msgbuffer);
706
//                    adc_last_value = *((unsigned int*) msgbuffer);
619
//                    adc_last_value_shifted = adc_last_value >> 4;
707
//                    adc_last_value_shifted = adc_last_value >> 4;
620
//                    DBG_PRINT_MAIN("Main: ADC Value = %d\r\n", adc_last_value);
708
//                    DBG_PRINT_MAIN("Main: ADC Value = %d\r\n", adc_last_value);