Subversion Repositories Code-Repo

Rev

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

Rev 111 Rev 112
Line 63... Line 63...
63
void main(void) {
63
void main(void) {
64
    char length;
64
    char length;
65
    unsigned char msgtype;
65
    unsigned char msgtype;
66
    unsigned char i = 0;
66
    unsigned char i = 0;
67
    unsigned char counter = 0;
67
    unsigned char counter = 0;
-
 
68
    enum I2C_STATE i2c_state = I2C_STATE_IDLE;
-
 
69
    unsigned char i2c_bytes_to_read = 0;
68
 
70
    
69
    // Pointers to allow parsing of xbee data from arbitrary byte array
71
    // Pointers to allow parsing of xbee data from arbitrary byte array
70
    XBEE_RX_AT_COMMAND_RESPONSE_FRAME *frame_at_cmd_response;
72
    XBEE_RX_AT_COMMAND_RESPONSE_FRAME *frame_at_cmd_response;
71
    XBEE_RX_DATA_PACKET_FRAME *frame_data_packet;
73
    XBEE_RX_DATA_PACKET_FRAME *frame_data_packet;
72
    XBEE_RX_DATA_TX_STATUS_FRAME *frame_tx_status;
74
    XBEE_RX_DATA_TX_STATUS_FRAME *frame_tx_status;
73
    XBEE_RX_IO_DATA_SAMPLE_FRAME *frame_io_sample;
75
    XBEE_RX_IO_DATA_SAMPLE_FRAME *frame_io_sample;
Line 83... Line 85...
83
    //    OSCTUNEbits.PLLEN = 1;          // Enable 4x PLL
85
    //    OSCTUNEbits.PLLEN = 1;          // Enable 4x PLL
84
    //    OSCCONbits.IRCF = 0b111;        // Set INTOSC postscaler to 8MHz
86
    //    OSCCONbits.IRCF = 0b111;        // Set INTOSC postscaler to 8MHz
85
    OSCCONbits.SCS = 0b00; // Use 96MHz PLL as primary clock source
87
    OSCCONbits.SCS = 0b00; // Use 96MHz PLL as primary clock source
86
    /* -------------------------------------------------------------------- */
88
    /* -------------------------------------------------------------------- */
87
 
89
 
-
 
90
    // Set all ports as digial I/O
-
 
91
    ANCON0 = 0xFF;
-
 
92
    ANCON1 = 0x1F;
-
 
93
    
88
    uart_init(&uart_data); // Initialize the UART handler code
94
    uart_init(&uart_data); // Initialize the UART handler code
89
    xbee_init(&xbee_data); // Initialize the XBee handler code
95
    xbee_init(&xbee_data); // Initialize the XBee handler code
90
    i2c_init(&i2c_data); // Initialize the I2C handler code
96
    i2c_init(&i2c_data); // Initialize the I2C handler code
91
    buffer_init(&buffer_data);
97
    buffer_init(&buffer_data);
92
    //    adc_init();                 // Initialize the ADC
98
    //    adc_init();                 // Initialize the ADC
Line 102... Line 108...
102
    pwm_init(); // Initialize the PWM output driver
108
    pwm_init(); // Initialize the PWM output driver
103
#endif
109
#endif
104
    interrupt_enable(); // Enable high-priority interrupts and low-priority interrupts
110
    interrupt_enable(); // Enable high-priority interrupts and low-priority interrupts
105
    interrupt_init(); // Initialize the interrupt priorities
111
    interrupt_init(); // Initialize the interrupt priorities
106
 
112
 
107
    // Set all ports as digial I/O
-
 
108
    ANCON0 = 0xFF;
-
 
109
    ANCON1 = 0x1F;
-
 
110
 
-
 
111
    // Configure the hardware i2c device as a slave
113
    // Configure the hardware i2c device as a slave
112
#ifdef _BASE_STATION
114
#ifdef _BASE_STATION
113
    i2c_configure_master();
115
    i2c_configure_slave(0x5F);
114
#endif
116
#endif
115
#ifdef _REMOTE
117
#ifdef _REMOTE
116
    i2c_configure_slave(0x5F);
118
    i2c_configure_master();
117
#endif
119
#endif
118
 
120
    
119
    DBG_PRINT_MAIN("\r\nMain: Program Started\r\n");
121
    DBG_PRINT_MAIN("\r\nMain: Program Started\r\n");
120
    
122
    
121
    // Loop and process recieved messages from interrupts
123
    // Loop and process recieved messages from interrupts
122
    while (1) {
124
    while (1) {
123
        // Call a routine that blocks until either message queues are not empty
125
        // Call a routine that blocks until either message queues are not empty
Line 157... Line 159...
157
                            if (buffer_data.stored_length > MSGLEN) {
159
                            if (buffer_data.stored_length > MSGLEN) {
158
                                msgbuffer[0] = MSGLEN;
160
                                msgbuffer[0] = MSGLEN;
159
                            } else {
161
                            } else {
160
                                msgbuffer[0] = buffer_data.stored_length;
162
                                msgbuffer[0] = buffer_data.stored_length;
161
                            }
163
                            }
-
 
164
                            DBG_PRINT_MAIN("Main: (I2C Return 0x2) Returning %X\r\n", msgbuffer[0]);
162
                            MQ_sendmsg_FromMainToHigh(length, MSGTYPE_I2C_REPLY, (void *) msgbuffer);
165
                            MQ_sendmsg_FromMainToHigh(length, MSGTYPE_I2C_REPLY, (void *) msgbuffer);
163
                            break;
166
                            break;
164
                        case 0x4:
167
                        case 0x4:
165
                            // Return data stored in buffer
168
                            // Return data stored in buffer
166
                            if (buffer_data.stored_length > MSGLEN) {
169
                            if (buffer_data.stored_length > MSGLEN) {
Line 185... Line 188...
185
                            break;
188
                            break;
186
                    };
189
                    };
187
                    break;
190
                    break;
188
                case MSGTYPE_I2C_MASTER_SEND_COMPLETE:
191
                case MSGTYPE_I2C_MASTER_SEND_COMPLETE:
189
                    DBG_PRINT_MAIN("Main: I2C Master Send Complete\r\n");
192
                    DBG_PRINT_MAIN("Main: I2C Master Send Complete\r\n");
-
 
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
190
                    break;
202
                    break;
191
                case MSGTYPE_I2C_MASTER_SEND_FAILED:
203
                case MSGTYPE_I2C_MASTER_SEND_FAILED:
192
                    DBG_PRINT_MAIN("Main: (ERROR) I2C Master Send Failed\r\n");
204
                    DBG_PRINT_MAIN("Main: (ERROR) I2C Master Send Failed\r\n");
193
                    break;
205
                    break;
194
                case MSGTYPE_I2C_MASTER_RECV_COMPLETE:
206
                case MSGTYPE_I2C_MASTER_RECV_COMPLETE:
195
                    DBG_PRINT_MAIN("Main: I2C Master Receive Complete\r\n");
207
                    DBG_PRINT_MAIN("Main: I2C Master Receive Complete\r\n");
196
                    DBG_PRINT_MAIN("Data: ");
208
                    DBG_PRINT_MAIN("Main: (I2C Data) ");
197
                    for (i = 0; i < length; i++) {
209
                    for (i = 0; i < length; i++) {
198
                        DBG_PRINT_MAIN("%X ", msgbuffer[i]);
210
                        DBG_PRINT_MAIN("%X ", msgbuffer[i]);
199
                    }
211
                    }
200
                    DBG_PRINT_MAIN("\r\n");
212
                    DBG_PRINT_MAIN("\r\n");
-
 
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
201
                    break;
238
                    break;
202
                case MSGTYPE_I2C_MASTER_RECV_FAILED:
239
                case MSGTYPE_I2C_MASTER_RECV_FAILED:
203
                    DBG_PRINT_MAIN("Main: (ERROR) I2C Master Receive Failed\r\n");
240
                    DBG_PRINT_MAIN("Main: (ERROR) I2C Master Receive Failed\r\n");
204
                    break;
241
                    break;
205
                    /* -----------------------------------------------------------*/
242
                    /* -----------------------------------------------------------*/
Line 258... Line 295...
258
                    frame_node_identification = (void *) msgbuffer;
295
                    frame_node_identification = (void *) msgbuffer;
259
                    break;
296
                    break;
260
                case MSGTYPE_XBEE_RX_FRAME_MODEM_STATUS:
297
                case MSGTYPE_XBEE_RX_FRAME_MODEM_STATUS:
261
                    DBG_PRINT_MAIN("Main: XBee modem status frame\r\n");
298
                    DBG_PRINT_MAIN("Main: XBee modem status frame\r\n");
262
                    frame_modem_status = (void *) msgbuffer;
299
                    frame_modem_status = (void *) msgbuffer;
-
 
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");
263
                    break;
325
                    break;
264
                    /* -----------------------------------------------------------*/
326
                    /* -----------------------------------------------------------*/
265
            };
327
            };
266
            continue;
328
            continue;
267
        }
329
        }
Line 280... Line 342...
280
                    DBG_PRINT_MAIN("Main: Port B4 Down\r\n");
342
                    DBG_PRINT_MAIN("Main: Port B4 Down\r\n");
281
#ifdef _REMOTE
343
#ifdef _REMOTE
282
                    timer3_enable();
344
                    timer3_enable();
283
#endif
345
#endif
284
#ifdef _BASE_STATION
346
#ifdef _BASE_STATION
285
                    timer0_enable();
347
//                    timer0_enable();
286
#endif
348
#endif
287
                    break;
349
                    break;
288
                case MSGTYPE_PORTB_4_UP:
350
                case MSGTYPE_PORTB_4_UP:
289
                    DBG_PRINT_MAIN("Main: Port B4 Up\r\n");
351
                    DBG_PRINT_MAIN("Main: Port B4 Up\r\n");
290
#ifdef _REMOTE
352
#ifdef _REMOTE
291
                    timer3_disable();
353
                    timer3_disable();
292
#endif
354
#endif
293
#ifdef _BASE_STATION
355
#ifdef _BASE_STATION
294
                    timer0_disable();
356
//                    timer0_disable();
295
#endif
357
#endif
296
                    break;
358
                    break;
297
                case MSGTYPE_PORTB_5_DOWN:
359
                case MSGTYPE_PORTB_5_DOWN:
298
                    DBG_PRINT_MAIN("Main: Port B5 Down\r\n");
360
                    DBG_PRINT_MAIN("Main: Port B5 Down\r\n");
-
 
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
299
                    break;
372
                    break;
300
                case MSGTYPE_PORTB_5_UP:
373
                case MSGTYPE_PORTB_5_UP:
301
                    DBG_PRINT_MAIN("Main: Port B5 Up\r\n");
374
                    DBG_PRINT_MAIN("Main: Port B5 Up\r\n");
302
                    break;
375
                    break;
303
                case MSGTYPE_PORTB_6_DOWN:
376
                case MSGTYPE_PORTB_6_DOWN:
Line 310... Line 383...
310
                    DBG_PRINT_MAIN("Main: Port B7 Down\r\n");
383
                    DBG_PRINT_MAIN("Main: Port B7 Down\r\n");
311
                    break;
384
                    break;
312
                case MSGTYPE_PORTB_7_UP:
385
                case MSGTYPE_PORTB_7_UP:
313
                    DBG_PRINT_MAIN("Main: Port B7 Up\r\n");
386
                    DBG_PRINT_MAIN("Main: Port B7 Up\r\n");
314
                    break;
387
                    break;
-
 
388
                case MSGTYPE_INT1:
-
 
389
//                    DBG_PRINT_MAIN("Main: INT1 Interrupt\r\n");
-
 
390
                    break;
315
                    /* -----------------------------------------------------------*/
391
                    /* -----------------------------------------------------------*/
316
                    /* --- Timer Interrupt Handlers ------------------------------*/
392
                    /* --- Timer Interrupt Handlers ------------------------------*/
317
                case MSGTYPE_TIMER0:
393
                case MSGTYPE_TIMER0:
318
                    DBG_PRINT_MAIN("Main: Timer 0\r\n");
394
                    DBG_PRINT_MAIN("Main: Timer 0 Interrupt\r\n");
319
                    /* XBee Demo */
395
                    /* XBee Demo */
320
                    frame_tx_data = (void *) msgbuffer;
396
                    frame_tx_data = (void *) msgbuffer;
321
                    frame_tx_data->frame_type = XBEE_TX_DATA_PACKET;
397
                    frame_tx_data->frame_type = XBEE_TX_DATA_PACKET;
322
                    frame_tx_data->frame_id = 0;
398
                    frame_tx_data->frame_id = 0;
323
                    frame_tx_data->destination_64.UPPER_32.long_value = 0x00000000;
399
                    frame_tx_data->destination_64.UPPER_32.long_value = 0x00000000;
324
                    frame_tx_data->destination_64.LOWER_32.long_value = 0x00000000;
400
                    frame_tx_data->destination_64.LOWER_32.long_value = 0x00000000;
325
                    frame_tx_data->destination_16.INT_16.int_value = 0x0000;
401
                    frame_tx_data->destination_16.INT_16.int_value = 0x0000;
326
                    frame_tx_data->broadcast_radius = 0;
402
                    frame_tx_data->broadcast_radius = 0;
327
                    frame_tx_data->options = 0x01; // Disable ACK
403
                    frame_tx_data->options = 0x01; // Disable ACK
328
                    frame_tx_data->data[0] = counter;
404
                    frame_tx_data->data[0] = counter;
-
 
405
                    
329
                    counter++;
406
                    counter++;
330
                    if (counter == 100)
407
                    if (counter == 100)
331
                        counter = 0;
408
                        counter = 0;
332
                    length = XBEE_TX_DATA_PACKET_FRAME_SIZE + 1;
409
                    length = XBEE_TX_DATA_PACKET_FRAME_SIZE + 1;
333
                    xbee_process_transmit_frame((void *) msgbuffer, length);
410
                    xbee_process_transmit_frame((void *) msgbuffer, length);
334
                    Delay10KTCYx(100);
411
                    Delay10KTCYx(100);
335
 
-
 
336
//                    /* I2C Demo */
-
 
337
//                    // Write 2 bytes
-
 
338
//                    msgbuffer[0] = 0x4;
-
 
339
//                    msgbuffer[1] = 0x2;
-
 
340
//                    i2c_master_send(0x5F, 2, msgbuffer);
-
 
341
//                    while (i2c_master_busy());
-
 
342
//
-
 
343
//                    // Read 2 bytes back
-
 
344
//                    i2c_master_recv(0x5F, 2);
-
 
345
//                    while (i2c_master_busy());
-
 
346
                    break;
412
                    break;
347
//                case MSGTYPE_ADC_NEWVALUE:
413
//                case MSGTYPE_ADC_NEWVALUE:
348
//                    // Get the value in the ADC
414
//                    // Get the value in the ADC
349
//                    adc_last_value = *((unsigned int*) msgbuffer);
415
//                    adc_last_value = *((unsigned int*) msgbuffer);
350
//                    adc_last_value_shifted = adc_last_value >> 4;
416
//                    adc_last_value_shifted = adc_last_value >> 4;