10,6 → 10,7 |
#include "pwm.h" |
#include "delays.h" |
#include "pin_interrupts.h" |
#include "buffer.h" |
|
#pragma config WDTEN = OFF // Turn off watchdog timer |
#pragma config XINST = OFF // Turn off extended instruction set |
42,8 → 43,8 |
* |
* RB0 - XBee CTS (PPS) |
* RB1 - XBee RTS (PPS) |
* RB2 - XBee RX (PPS) |
* RB3 - XBee Tx (PPS) |
* RB2 - XBee Tx (PPS) |
* RB3 - XBee Rx (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) |
53,18 → 54,17 |
#pragma udata umain_1 |
static unsigned char msgbuffer[MSGLEN + 1]; |
#pragma udata umain_2 |
static I2C_DATA i2c_i; |
static I2C_DATA i2c_data; |
#pragma udata |
static XBEE_DATA xbee_c; |
static UART_DATA uart_c; |
static XBEE_DATA xbee_data; |
static UART_DATA uart_data; |
static BUFFER_DATA buffer_data; |
|
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 |
unsigned char counter = 0; |
|
// Pointers to allow parsing of xbee data from arbitrary byte array |
XBEE_RX_AT_COMMAND_RESPONSE_FRAME *frame_at_cmd_response; |
85,16 → 85,22 |
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 |
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 |
buffer_init(&buffer_data); |
// adc_init(); // Initialize the ADC |
MQ_init(); // Initialize message queues before enabling any interrupts |
timers_init(); // Initialize timers |
led_driver_init(); // Initialize the driver for the LED display |
port_b_int_init(); // Initialze Port B interrupt handler |
|
#ifdef _BASE_STATION |
intx_init(); // IR receiver input |
#endif |
#ifdef _REMOTE |
pwm_init(); // Initialize the PWM output driver |
timers_init(); // Initialize timers |
intx_init(); |
port_b_int_init(); // Initialze Port B interrupt handler |
#endif |
interrupt_enable(); // Enable high-priority interrupts and low-priority interrupts |
interrupt_init(); // Initialize the interrupt priorities |
|
103,54 → 109,15 |
ANCON1 = 0x1F; |
|
// Configure the hardware i2c device as a slave |
#ifdef _MASTER |
#ifdef _BASE_STATION |
i2c_configure_master(); |
#endif |
#ifdef _SLAVE |
#ifdef _REMOTE |
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 |
186,12 → 153,23 |
switch (msgbuffer[0]) { |
case 0x2: |
length = 1; |
msgbuffer[0] = 1; // Size |
// Return size of stored data in buffer |
if (buffer_data.stored_length > MSGLEN) { |
msgbuffer[0] = MSGLEN; |
} else { |
msgbuffer[0] = buffer_data.stored_length; |
} |
MQ_sendmsg_FromMainToHigh(length, MSGTYPE_I2C_REPLY, (void *) msgbuffer); |
break; |
case 0x4: |
length = 1; |
msgbuffer[0] = last_data_received; |
// Return data stored in buffer |
if (buffer_data.stored_length > MSGLEN) { |
length = MSGLEN; |
buffer_read(MSGLEN, msgbuffer); |
} else { |
length = buffer_data.stored_length; |
buffer_read(buffer_data.stored_length, msgbuffer); |
} |
MQ_sendmsg_FromMainToHigh(length, MSGTYPE_I2C_REPLY, (void *) msgbuffer); |
break; |
case 0x6: |
225,6 → 203,7 |
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"); |
245,7 → 224,9 |
DBG_PRINT_MAIN("%X ", frame_data_packet->data[i]); |
} |
DBG_PRINT_MAIN("\r\n"); |
last_data_received = frame_data_packet->data[0]; |
// Store received data into buffer |
buffer_insert(length - XBEE_RX_DATA_PACKET_FRAME_SIZE, frame_data_packet->data); |
// Send value of first byte received to LED display |
led_driver_num(frame_data_packet->data[0]); |
break; |
case MSGTYPE_XBEE_RX_DATA_TX_STATUS: |
281,12 → 262,37 |
frame_modem_status = (void *) msgbuffer; |
break; |
/* -----------------------------------------------------------*/ |
/* --- Port B Interrupt Handlers -----------------------------*/ |
}; |
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) { |
/* --- Port B Interrupt Handlers -----------------------------*/ |
case MSGTYPE_PORTB_4_DOWN: |
DBG_PRINT_MAIN("Main: Port B4 Down\r\n"); |
#ifdef _REMOTE |
timer3_enable(); |
#endif |
#ifdef _BASE_STATION |
timer0_enable(); |
#endif |
break; |
case MSGTYPE_PORTB_4_UP: |
DBG_PRINT_MAIN("Main: Port B4 Up\r\n"); |
#ifdef _REMOTE |
timer3_disable(); |
#endif |
#ifdef _BASE_STATION |
timer0_disable(); |
#endif |
break; |
case MSGTYPE_PORTB_5_DOWN: |
DBG_PRINT_MAIN("Main: Port B5 Down\r\n"); |
307,8 → 313,47 |
DBG_PRINT_MAIN("Main: Port B7 Up\r\n"); |
break; |
/* -----------------------------------------------------------*/ |
/* --- Timer Interrupt Handlers ------------------------------*/ |
case MSGTYPE_TIMER0: |
DBG_PRINT_MAIN("Main: Timer 0\r\n"); |
/* 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] = counter; |
counter++; |
if (counter == 100) |
counter = 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()); |
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); |
// |
// adc_start(); |
// break; |
default: |
DBG_PRINT_MAIN("Main: (ERROR) Unexpected msg in high queue, length = %d, type = %d\r\n", length, msgtype); |
DBG_PRINT_MAIN("Main: (ERROR) Unexpected msg in low queue, length = %d, type = %d\r\n", length, msgtype); |
for (i = 0; i < length; i++) { |
DBG_PRINT_MAIN("%X ", msgbuffer[i]); |
} |
317,63 → 362,5 |
}; |
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; |
// } |
} |
} |