Subversion Repositories Code-Repo

Rev

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