Subversion Repositories Code-Repo

Rev

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

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