65,7 → 65,9 |
unsigned char msgtype; |
unsigned char i = 0; |
unsigned char counter = 0; |
|
enum I2C_STATE i2c_state = I2C_STATE_IDLE; |
unsigned char i2c_bytes_to_read = 0; |
|
// 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; |
85,6 → 87,10 |
OSCCONbits.SCS = 0b00; // Use 96MHz PLL as primary clock source |
/* -------------------------------------------------------------------- */ |
|
// Set all ports as digial I/O |
ANCON0 = 0xFF; |
ANCON1 = 0x1F; |
|
uart_init(&uart_data); // Initialize the UART handler code |
xbee_init(&xbee_data); // Initialize the XBee handler code |
i2c_init(&i2c_data); // Initialize the I2C handler code |
104,18 → 110,14 |
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 _BASE_STATION |
i2c_configure_master(); |
i2c_configure_slave(0x5F); |
#endif |
#ifdef _REMOTE |
i2c_configure_slave(0x5F); |
i2c_configure_master(); |
#endif |
|
|
DBG_PRINT_MAIN("\r\nMain: Program Started\r\n"); |
|
// Loop and process recieved messages from interrupts |
159,6 → 161,7 |
} else { |
msgbuffer[0] = buffer_data.stored_length; |
} |
DBG_PRINT_MAIN("Main: (I2C Return 0x2) Returning %X\r\n", msgbuffer[0]); |
MQ_sendmsg_FromMainToHigh(length, MSGTYPE_I2C_REPLY, (void *) msgbuffer); |
break; |
case 0x4: |
187,6 → 190,15 |
break; |
case MSGTYPE_I2C_MASTER_SEND_COMPLETE: |
DBG_PRINT_MAIN("Main: I2C Master Send Complete\r\n"); |
#ifdef _BASE_STATION |
if (i2c_state == I2C_WAIT_WRITE_LENGTH_ACK) { |
i2c_master_recv(0x5F, 2); // Request 2 bytes |
i2c_state = I2C_WAIT_REPLY_LENGTH; |
} else if (i2c_state == I2C_WAIT_WRITE_DATA_ACK) { |
i2c_master_recv(0x5F, i2c_bytes_to_read+1); // Request # of bytes |
i2c_state = I2C_WAIT_REPLY_DATA; |
} |
#endif |
break; |
case MSGTYPE_I2C_MASTER_SEND_FAILED: |
DBG_PRINT_MAIN("Main: (ERROR) I2C Master Send Failed\r\n"); |
193,11 → 205,36 |
break; |
case MSGTYPE_I2C_MASTER_RECV_COMPLETE: |
DBG_PRINT_MAIN("Main: I2C Master Receive Complete\r\n"); |
DBG_PRINT_MAIN("Data: "); |
DBG_PRINT_MAIN("Main: (I2C Data) "); |
for (i = 0; i < length; i++) { |
DBG_PRINT_MAIN("%X ", msgbuffer[i]); |
} |
DBG_PRINT_MAIN("\r\n"); |
#ifdef _BASE_STATION |
if (i2c_state == I2C_WAIT_REPLY_LENGTH) { |
if (msgbuffer[1] == 0xFF) { |
i2c_master_recv(0x5F, 2); // Request again |
} else if (msgbuffer[1] != 0) { |
i2c_bytes_to_read = msgbuffer[1]; |
// Write [4,#] |
msgbuffer[0] = 4; |
i2c_master_send(0x5F, 2, msgbuffer); |
i2c_state = I2C_WAIT_WRITE_DATA_ACK; |
} else { |
i2c_state = I2C_STATE_IDLE; |
} |
} else if (i2c_state == I2C_WAIT_REPLY_DATA) { |
if (msgbuffer[1] == 0xFF) { |
i2c_master_recv(0x5F, i2c_bytes_to_read+1); // Request again |
} else { |
for (i = 1; i < length; i++) { |
led_driver_num(msgbuffer[i]); |
Delay10KTCYx(50); |
} |
i2c_state = I2C_STATE_IDLE; |
} |
} |
#endif |
break; |
case MSGTYPE_I2C_MASTER_RECV_FAILED: |
DBG_PRINT_MAIN("Main: (ERROR) I2C Master Receive Failed\r\n"); |
260,6 → 297,31 |
case MSGTYPE_XBEE_RX_FRAME_MODEM_STATUS: |
DBG_PRINT_MAIN("Main: XBee modem status frame\r\n"); |
frame_modem_status = (void *) msgbuffer; |
DBG_PRINT_MAIN("Status: %X (", frame_modem_status->status); |
switch(frame_modem_status->status) { |
case 0: |
DBG_PRINT_MAIN("Hardware Reset"); |
break; |
case 1: |
DBG_PRINT_MAIN("Watchdog Timer Reset"); |
break; |
case 2: |
DBG_PRINT_MAIN("Joined Network"); |
break; |
case 3: |
DBG_PRINT_MAIN("Disassociated"); |
break; |
case 6: |
DBG_PRINT_MAIN("Coordinator Started"); |
break; |
case 7: |
DBG_PRINT_MAIN("Network Security Key Updated"); |
break; |
case 0x11: |
DBG_PRINT_MAIN("Modem Config Changed While Joining"); |
break; |
} |
DBG_PRINT_MAIN(")\r\n"); |
break; |
/* -----------------------------------------------------------*/ |
}; |
282,7 → 344,7 |
timer3_enable(); |
#endif |
#ifdef _BASE_STATION |
timer0_enable(); |
// timer0_enable(); |
#endif |
break; |
case MSGTYPE_PORTB_4_UP: |
291,11 → 353,22 |
timer3_disable(); |
#endif |
#ifdef _BASE_STATION |
timer0_disable(); |
// timer0_disable(); |
#endif |
break; |
case MSGTYPE_PORTB_5_DOWN: |
DBG_PRINT_MAIN("Main: Port B5 Down\r\n"); |
#ifdef _BASE_STATION |
// if (i2c_state == I2C_STATE_IDLE) { |
// DBG_PRINT_MAIN("Main: Starting I2C Request\r\n"); |
// /* I2C Demo */ |
// // Write 2 bytes |
// msgbuffer[0] = 0x2; |
// msgbuffer[1] = 0x2; |
// i2c_master_send(0x5F, 2, msgbuffer); |
// i2c_state = I2C_WAIT_WRITE_LENGTH_ACK; |
// } |
#endif |
break; |
case MSGTYPE_PORTB_5_UP: |
DBG_PRINT_MAIN("Main: Port B5 Up\r\n"); |
312,10 → 385,13 |
case MSGTYPE_PORTB_7_UP: |
DBG_PRINT_MAIN("Main: Port B7 Up\r\n"); |
break; |
case MSGTYPE_INT1: |
// DBG_PRINT_MAIN("Main: INT1 Interrupt\r\n"); |
break; |
/* -----------------------------------------------------------*/ |
/* --- Timer Interrupt Handlers ------------------------------*/ |
case MSGTYPE_TIMER0: |
DBG_PRINT_MAIN("Main: Timer 0\r\n"); |
DBG_PRINT_MAIN("Main: Timer 0 Interrupt\r\n"); |
/* XBee Demo */ |
frame_tx_data = (void *) msgbuffer; |
frame_tx_data->frame_type = XBEE_TX_DATA_PACKET; |
326,6 → 402,7 |
frame_tx_data->broadcast_radius = 0; |
frame_tx_data->options = 0x01; // Disable ACK |
frame_tx_data->data[0] = counter; |
|
counter++; |
if (counter == 100) |
counter = 0; |
332,17 → 409,6 |
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()); |
break; |
// case MSGTYPE_ADC_NEWVALUE: |
// // Get the value in the ADC |