Subversion Repositories Code-Repo

Rev

Rev 107 | Rev 111 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

#include "maindefs.h"
#include "msg_queues.h"
#include "interrupts.h"
#include "uart.h"
#include "i2c.h"
#include "adc.h"
#include "timers.h"
#include "xbee.h"
#include "led_driver.h"
#include "pwm.h"
#include "delays.h"
#include "pin_interrupts.h"

#pragma config WDTEN = OFF          // Turn off watchdog timer
#pragma config XINST = OFF          // Turn off extended instruction set
#pragma config OSC = HSPLL          // Use external oscillator (101)
#pragma config PLLDIV = 3           // Set PPL prescaler to 3 (to get 4MHz)
#pragma config CFGPLLEN = ON        // Enable PLL on startup
#pragma config PLLSEL = PLL96       // Use 96MHz PLL 4MHz -> 96MHz / 2 = 48MHz
//#pragma config SOSCSEL = HIGH       // High Power T1OSC/SOSC circuit selected
#pragma config ADCSEL = BIT12       // 12-bit ADrC
#pragma config IOL1WAY = OFF        // IOLOCK bit can be set and cleared as needed

/* ----------- IO Pins -----------
 * RA0 - LED Display Latch Enable (PPS)
 * RA1 - LED Display CLK (PPS)
 * RA2 - LED Display DIN (PPS)
 * RA3 - LED Display Output Enable (PPS)
 * RA4 - [CANNOT BE USED (VDDCORE/VCAP)]
 * RA5 - IR Reciever (PPS)
 * RA6 - Oscillator
 * RA7 - Oscillator
 * 
 * RC0 - PWM Output (IR) (PPS, Ports B and C only)
 * RC1 - PWM Output (IR) (PPS, Ports B and C only)
 * RC2 - LED Output (PPS, Ports B and C only)
 * RC3 - I2C SCL
 * RC4 - I2C SDA
 * RC5 - XBee Sleep (PPS)
 * RC6 - UART Debug Output
 * RC7 - UART Debug Input
 *
 * RB0 - XBee CTS (PPS)
 * RB1 - XBee RTS (PPS)
 * RB2 - XBee RX (PPS)
 * RB3 - XBee Tx (PPS)
 * RB4 - Button Input (Port B Interrupt on Change)
 * RB5 - Button Input (Port B Interrupt on Change)
 * RB6 - Button Input (Port B Interrupt on Change)
 * RB7 - Button Input (Port B Interrupt on Change)
 * ---------------------------- */

#pragma udata umain_1
static unsigned char msgbuffer[MSGLEN + 1];
#pragma udata umain_2
static I2C_DATA i2c_i;
#pragma udata
static XBEE_DATA xbee_c;
static UART_DATA uart_c;

void main(void) {
    char length;
    unsigned char msgtype;
    unsigned char i = 0;
    unsigned char last_data_received = 0;
    //    unsigned int adc_last_value;    // Holds 12 bit ADC value
    //    unsigned char adc_last_value_shifted;   // Holds upper 4 bits

    // Pointers to allow parsing of xbee data from arbitrary byte array
    XBEE_RX_AT_COMMAND_RESPONSE_FRAME *frame_at_cmd_response;
    XBEE_RX_DATA_PACKET_FRAME *frame_data_packet;
    XBEE_RX_DATA_TX_STATUS_FRAME *frame_tx_status;
    XBEE_RX_IO_DATA_SAMPLE_FRAME *frame_io_sample;
    XBEE_RX_EXPLICIT_COMMAND_FRAME *frame_explicit_cmd;
    XBEE_RX_REMOTE_AT_COMMAND_FRAME *frame_remote_at_cmd;
    XBEE_RX_ROUTE_RECORD_FRAME *frame_route_record;
    XBEE_RX_NODE_IDENTIFICATION_INDICATOR_FRAME *frame_node_identification;
    XBEE_RX_MODEM_STATUS_FRAME *frame_modem_status;

    XBEE_TX_DATA_PACKET_FRAME *frame_tx_data;

    /* --------------------- Oscillator Configuration --------------------- */
    //    OSCTUNEbits.PLLEN = 1;          // Enable 4x PLL
    //    OSCCONbits.IRCF = 0b111;        // Set INTOSC postscaler to 8MHz
    OSCCONbits.SCS = 0b00; // Use 96MHz PLL as primary clock source
    /* -------------------------------------------------------------------- */

    uart_init(&uart_c); // Initialize the UART handler code
    xbee_init(&xbee_c); // Initialize the XBee handler code
    i2c_init(&i2c_i); // Initialize the I2C handler code
    //    adc_init();                 // Initialize the ADC
    MQ_init(); // Initialize message queues before enabling any interrupts
    led_driver_init(); // Initialize the driver for the LED display
    pwm_init(); // Initialize the PWM output driver
    timers_init(); // Initialize timers
    intx_init();
    port_b_int_init(); // Initialze Port B interrupt handler
    interrupt_enable(); // Enable high-priority interrupts and low-priority interrupts
    interrupt_init(); // Initialize the interrupt priorities

    // Set all ports as digial I/O
    ANCON0 = 0xFF;
    ANCON1 = 0x1F;

    // Configure the hardware i2c device as a slave
#ifdef _MASTER
    i2c_configure_master();
#endif
#ifdef _SLAVE
    i2c_configure_slave(0x5F);
#endif

    DBG_PRINT_MAIN("\r\nMain: Program Started\r\n");

#ifdef _MASTER
    //     Delay a bit to allow XBees to start up
    for (i = 0; i < 20; i++)
        Delay10KTCYx(255);

    pwm_start();

    while (1) {
        /* XBee Demo */
        frame_tx_data = (void *) msgbuffer;
        frame_tx_data->frame_type = XBEE_TX_DATA_PACKET;
        frame_tx_data->frame_id = 0;
        frame_tx_data->destination_64.UPPER_32.long_value = 0x00000000;
        frame_tx_data->destination_64.LOWER_32.long_value = 0x00000000;
        frame_tx_data->destination_16.INT_16.int_value = 0x0000;
        frame_tx_data->broadcast_radius = 0;
        frame_tx_data->options = 0x01; // Disable ACK
        frame_tx_data->data[0] = i;
        i++;
        if (i == 100)
            i = 0;
        length = XBEE_TX_DATA_PACKET_FRAME_SIZE + 1;
        xbee_process_transmit_frame((void *) msgbuffer, length);
        Delay10KTCYx(100);

        /* I2C Demo */
        // Write 2 bytes
        msgbuffer[0] = 0x4;
        msgbuffer[1] = 0x2;
        i2c_master_send(0x5F, 2, msgbuffer);
        while (i2c_master_busy());

        // Read 2 bytes back
        i2c_master_recv(0x5F, 2);
        while (i2c_master_busy());
//        Delay10KTCYx(100);
    }
#endif

    // Loop and process recieved messages from interrupts
    while (1) {
        // Call a routine that blocks until either message queues are not empty
        MQ_wait_on_incoming_msg_queues();

        // Process high priority message queue
        length = MQ_recvmsg_ToMainFromHigh(MSGLEN, &msgtype, (void *) msgbuffer);
        if (length < 0) {
            // No message, check the error code to see if it is concern
            if (length != MSG_QUEUE_EMPTY) {
                DBG_PRINT_MAIN("Main: (ERROR) Bad high priority receive, code = %d\r\n", length);
            }
        } else {
            switch (msgtype) {
                    /* --- I2C Message Handlers ----------------------------------*/
                case MSGTYPE_OVERRUN:
                    DBG_PRINT_MAIN("Main: (ERROR) UART overrun detected, type = %d\r\n", msgtype);
                    break;
                case MSGTYPE_I2C_DBG:
                    DBG_PRINT_MAIN("Main: I2C Dbg Data Recieved: ");
                    for (i = 0; i < length; i++) {
                        DBG_PRINT_MAIN("%X ", msgbuffer[i]);
                    }
                    DBG_PRINT_MAIN("\r\n");
                    break;
                case MSGTYPE_I2C_DATA:
                    DBG_PRINT_MAIN("Main: I2C Data Recieved: ");
                    for (i = 0; i < length - 1; i++) {
                        DBG_PRINT_MAIN("%X ", msgbuffer[i]);
                    }
                    DBG_PRINT_MAIN(" Event Count: %d", msgbuffer[length - 1]);
                    DBG_PRINT_MAIN("\r\n");
                    switch (msgbuffer[0]) {
                        case 0x2:
                            length = 1;
                            msgbuffer[0] = 1; // Size
                            MQ_sendmsg_FromMainToHigh(length, MSGTYPE_I2C_REPLY, (void *) msgbuffer);
                            break;
                        case 0x4:
                            length = 1;
                            msgbuffer[0] = last_data_received;
                            MQ_sendmsg_FromMainToHigh(length, MSGTYPE_I2C_REPLY, (void *) msgbuffer);
                            break;
                        case 0x6:
                            break;
                        case 0x7:
                            break;
                        case 0x8:
                            break;
                        case 0x9:
                            break;
                        default:
                            DBG_PRINT_MAIN("Main: (ERROR) Unexpected message type recieved: %d\r\n", msgbuffer[0]);
                            break;
                    };
                    break;
                case MSGTYPE_I2C_MASTER_SEND_COMPLETE:
                    DBG_PRINT_MAIN("Main: I2C Master Send Complete\r\n");
                    break;
                case MSGTYPE_I2C_MASTER_SEND_FAILED:
                    DBG_PRINT_MAIN("Main: (ERROR) I2C Master Send Failed\r\n");
                    break;
                case MSGTYPE_I2C_MASTER_RECV_COMPLETE:
                    DBG_PRINT_MAIN("Main: I2C Master Receive Complete\r\n");
                    DBG_PRINT_MAIN("Data: ");
                    for (i = 0; i < length; i++) {
                        DBG_PRINT_MAIN("%X ", msgbuffer[i]);
                    }
                    DBG_PRINT_MAIN("\r\n");
                    break;
                case MSGTYPE_I2C_MASTER_RECV_FAILED:
                    DBG_PRINT_MAIN("Main: (ERROR) I2C Master Receive Failed\r\n");
                    break;
                    /* -----------------------------------------------------------*/
                    /* --- XBee Message Handlers ---------------------------------*/
                case MSGTYPE_XBEE_RX_AT_COMMAND_RESPONSE:
                    DBG_PRINT_MAIN("Main: XBee AT command frame\r\n");
                    frame_at_cmd_response = (void *) msgbuffer;
                    DBG_PRINT_MAIN("Command: %c%c\r\n", frame_at_cmd_response->command[0], frame_at_cmd_response->command[0]);
                    DBG_PRINT_MAIN("Status: %d\r\n", frame_at_cmd_response->command_status);
                    DBG_PRINT_MAIN("Data: ");
                    for (i = 0; i < length - XBEE_RX_AT_COMMAND_RESPONSE_FRAME_SIZE; i++) {
                        DBG_PRINT_MAIN("%X ", frame_data_packet->data[i]);
                    }
                    DBG_PRINT_MAIN("\r\n");
                    break;
                case MSGTYPE_XBEE_RX_DATA_PACKET:
                    DBG_PRINT_MAIN("Main: XBee data packet frame\r\n");
                    frame_data_packet = (void *) msgbuffer;
                    DBG_PRINT_MAIN("Data: ");
                    for (i = 0; i < length - XBEE_RX_DATA_PACKET_FRAME_SIZE; i++) {
                        DBG_PRINT_MAIN("%X ", frame_data_packet->data[i]);
                    }
                    DBG_PRINT_MAIN("\r\n");
                    last_data_received = frame_data_packet->data[0];
                    led_driver_num(frame_data_packet->data[0]);
                    break;
                case MSGTYPE_XBEE_RX_DATA_TX_STATUS:
                    DBG_PRINT_MAIN("Main: XBee TX status frame\r\n");
                    frame_tx_status = (void *) msgbuffer;
                    DBG_PRINT_MAIN("Destination: %X\r\n", frame_tx_status->destination_16);
                    DBG_PRINT_MAIN("Transmit Retry Count: %X\r\n", frame_tx_status->transmit_retry_count);
                    DBG_PRINT_MAIN("Delivery Status: %X\r\n", frame_tx_status->delivery_status);
                    DBG_PRINT_MAIN("Discovery Status: %X\r\n", frame_tx_status->discovery_status);
                    break;
                case MSGTYPE_XBEE_RX_IO_DATA_SAMPLE:
                    DBG_PRINT_MAIN("Main: XBee IO data sample frame\r\n");
                    frame_io_sample = (void *) msgbuffer;
                    break;
                case MSGTYPE_XBEE_RX_EXPLICIT_COMMAND:
                    DBG_PRINT_MAIN("Main: XBee explicit command frame\r\n");
                    frame_explicit_cmd = (void *) msgbuffer;
                    break;
                case MSGTYPE_XBEE_RX_REMOTE_AT_COMMAND_RESPONSE:
                    DBG_PRINT_MAIN("Main: XBee remote AT command response frame\r\n");
                    frame_remote_at_cmd = (void *) msgbuffer;
                    break;
                case MSGTYPE_XBEE_RX_ROUTE_RECORD:
                    DBG_PRINT_MAIN("Main: XBee route record frame\r\n");
                    frame_route_record = (void *) msgbuffer;
                    break;
                case MSGTYPE_XBEE_RX_NODE_IDENTIFICATION:
                    DBG_PRINT_MAIN("Main: XBee node identification frame\r\n");
                    frame_node_identification = (void *) msgbuffer;
                    break;
                case MSGTYPE_XBEE_RX_FRAME_MODEM_STATUS:
                    DBG_PRINT_MAIN("Main: XBee modem status frame\r\n");
                    frame_modem_status = (void *) msgbuffer;
                    break;
                    /* -----------------------------------------------------------*/
                    /* --- Port B Interrupt Handlers -----------------------------*/
                case MSGTYPE_PORTB_4_DOWN:
                    DBG_PRINT_MAIN("Main: Port B4 Down\r\n");
                    break;
                case MSGTYPE_PORTB_4_UP:
                    DBG_PRINT_MAIN("Main: Port B4 Up\r\n");
                    break;
                case MSGTYPE_PORTB_5_DOWN:
                    DBG_PRINT_MAIN("Main: Port B5 Down\r\n");
                    break;
                case MSGTYPE_PORTB_5_UP:
                    DBG_PRINT_MAIN("Main: Port B5 Up\r\n");
                    break;
                case MSGTYPE_PORTB_6_DOWN:
                    DBG_PRINT_MAIN("Main: Port B6 Down\r\n");
                    break;
                case MSGTYPE_PORTB_6_UP:
                    DBG_PRINT_MAIN("Main: Port B6 Up\r\n");
                    break;
                case MSGTYPE_PORTB_7_DOWN:
                    DBG_PRINT_MAIN("Main: Port B7 Down\r\n");
                    break;
                case MSGTYPE_PORTB_7_UP:
                    DBG_PRINT_MAIN("Main: Port B7 Up\r\n");
                    break;
                    /* -----------------------------------------------------------*/
                default:
                    DBG_PRINT_MAIN("Main: (ERROR) Unexpected msg in high queue, length = %d, type = %d\r\n", length, msgtype);
                    for (i = 0; i < length; i++) {
                        DBG_PRINT_MAIN("%X ", msgbuffer[i]);
                    }
                    DBG_PRINT_MAIN("\r\n");
                    break;
            };
            continue;
        }

        //        // Process low priority queue
        //        length = MQ_recvmsg_ToMainFromLow(MSGLEN, &msgtype, (void *) msgbuffer);
        //        if (length < 0) {
        //            // No message, check the error code to see if it is concern
        //            if (length != MSG_QUEUE_EMPTY) {
        //                DBG_PRINT_MAIN("Main: (ERROR) Bad low priority receive, code = %d\r\n", length);
        //            }
        //        } else {
        //            switch (msgtype) {
        //                case MSGTYPE_TIMER0:
        //                    DBG_PRINT_MAIN("Timer 0 Triggered\r\n");
        //                    // Write 2 bytes
        //                    msgbuffer[0] = 0x4;
        //                    msgbuffer[1] = 0x3;
        //                    i2c_master_send(0x5F, 2, msgbuffer);
        //                    while (i2c_master_busy());
        //
        //                    // Read 3 bytes
        //                    i2c_master_recv(0x5F, 3);
        //                    while (i2c_master_busy());
        //
        //                    break;
        //                break;
        //                case MSGTYPE_ADC_NEWVALUE:
        //                    // Get the value in the ADC
        //                    adc_last_value = *((unsigned int*)msgbuffer);
        //                    adc_last_value_shifted = adc_last_value >> 4;
        //                    DBG_PRINT_MAIN("Main: ADC Value = %d\r\n", adc_last_value);
        //
        //                    frame_tx_data = (void *) msgbuffer;
        //                    frame_tx_data->frame_type = XBEE_TX_DATA_PACKET;
        //                    frame_tx_data->frame_id = 0;
        //                    frame_tx_data->destination_64.UPPER_32.long_value = 0x00000000;
        //                    frame_tx_data->destination_64.LOWER_32.long_value = 0x00000000;
        //                    frame_tx_data->destination_16.INT_16.int_value = 0x0000;
        //                    frame_tx_data->broadcast_radius = 0;
        //                    frame_tx_data->options = 0x01;  // Disable ACK
        //
        //                    frame_tx_data->data[0] = adc_last_value_shifted;
        //
        //                    length = XBEE_TX_DATA_PACKET_FRAME_SIZE + 1;
        //
        //                    xbee_process_transmit_frame((void *) msgbuffer, length);
        //
        //                    Delay10KTCYx(255);
        //                    Delay10KTCYx(255);
        //                    Delay10KTCYx(255);
        //                    Delay10KTCYx(255);
        //
        //                    adc_start();
        //                    break;
        //                default:
        //                    DBG_PRINT_MAIN("Main: (ERROR) Unexpected msg in low queue, type = %d\r\n", msgtype);
        //                    break;
        //            };
        //            continue;
        //        }
    }
}