Subversion Repositories Code-Repo

Rev

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