| Line 1... |
Line 1... |
| 1 |
#include "maindefs.h"
|
1 |
#include "maindefs.h"
|
| 2 |
#include "xbee.h"
|
2 |
#include "xbee.h"
|
| 3 |
#include <string.h>
|
3 |
#include <string.h>
|
| 4 |
|
4 |
|
| 5 |
static XBEE_DATA xbee_data;
|
5 |
static XBEE_DATA xbee_data;
|
| - |
|
6 |
static XBEE_DATA *xbee_data_p = &xbee_data;
|
| 6 |
static void *xbee_data_frame;
|
7 |
static void *xbee_data_frame;
|
| 7 |
static void *xbee_frame;
|
8 |
static void *xbee_frame;
|
| 8 |
|
9 |
|
| 9 |
/* Initialize variables used by this library */
|
10 |
/* Initialize variables used by this library */
|
| 10 |
void XBee_Init() {
|
11 |
void XBee_Init() {
|
| Line 12... |
Line 13... |
| 12 |
XBEE_RTS_TRIS = 0; // RB1 is RTS, set by PIC
|
13 |
XBEE_RTS_TRIS = 0; // RB1 is RTS, set by PIC
|
| 13 |
|
14 |
|
| 14 |
XBEE_CTS_LAT = 0; // Pin set high to signal stop sending data to XBee
|
15 |
XBEE_CTS_LAT = 0; // Pin set high to signal stop sending data to XBee
|
| 15 |
XBEE_RTS_LAT = 0; // Pin set high to indicate stop sending data to PIC
|
16 |
XBEE_RTS_LAT = 0; // Pin set high to indicate stop sending data to PIC
|
| 16 |
|
17 |
|
| 17 |
xbee_data.dataind = 0;
|
18 |
xbee_data_p->dataind = 0;
|
| 18 |
xbee_data.checksum_sum = 0;
|
19 |
xbee_data_p->checksum_sum = 0;
|
| 19 |
xbee_data.frame_rdy = 0;
|
20 |
xbee_data_p->frame_rdy = 0;
|
| 20 |
xbee_data.escape_flag = 0;
|
21 |
xbee_data_p->escape_flag = 0;
|
| 21 |
xbee_data.read_state = XBEE_STATE_READ_START;
|
22 |
xbee_data_p->read_state = XBEE_STATE_READ_START;
|
| 22 |
|
23 |
|
| 23 |
// memset(&xbee_data, 0, 32);
|
24 |
// memset(&xbee_data, 0, 32);
|
| 24 |
|
25 |
|
| 25 |
// Grab a pointer to where the unique frame array starts
|
26 |
// Grab a pointer to where the unique frame array starts
|
| 26 |
xbee_data_frame = &(xbee_data.rcv_frame.FRAME);
|
27 |
xbee_data_frame = &(xbee_data_p->rcv_frame.FRAME);
|
| 27 |
xbee_frame = &(xbee_data.rcv_frame);
|
28 |
xbee_frame = &(xbee_data_p->rcv_frame);
|
| 28 |
}
|
29 |
}
|
| 29 |
|
30 |
|
| 30 |
/* Here we handle the serial input from the UART interrupt */
|
31 |
/* Here we handle the serial input from the UART interrupt */
|
| 31 |
void XBee_Serial_In(unsigned char c) {
|
32 |
void XBee_Serial_In(unsigned char c) {
|
| 32 |
// For some reason writing the length straight to xbee_data doesnt seem to work
|
33 |
// For some reason writing the length straight to xbee_data doesnt seem to work
|
| 33 |
// so we work around it by pointing to the length bytes directly
|
34 |
// so we work around it by pointing to the length bytes directly
|
| 34 |
XBEE_ADDRESS_16 *length = xbee_frame + 1;
|
35 |
XBEE_ADDRESS_16 *length = xbee_frame + 1;
|
| 35 |
|
36 |
|
| 36 |
#ifdef XBEE_USE_ESCAPE_CHAR
|
37 |
#ifdef XBEE_USE_ESCAPE_CHAR
|
| 37 |
if (c == XBEE_ESCAPE_CHAR) {
|
38 |
if (c == XBEE_ESCAPE_CHAR) {
|
| 38 |
// Next byte needs is an escaped char
|
39 |
// Next byte needs is an escaped char
|
| 39 |
xbee_data.escape_flag = 1;
|
40 |
xbee_data_p->escape_flag = 1;
|
| 40 |
return;
|
41 |
return;
|
| 41 |
}
|
42 |
}
|
| 42 |
|
43 |
|
| 43 |
if (xbee_data.escape_flag) {
|
44 |
if (xbee_data_p->escape_flag) {
|
| 44 |
// XOR byte with 0x20 to get escaped char
|
45 |
// XOR byte with 0x20 to get escaped char
|
| 45 |
c ^= XBEE_ESCAPE_VAL;
|
46 |
c ^= XBEE_ESCAPE_VAL;
|
| 46 |
xbee_data.escape_flag = 0;
|
47 |
xbee_data_p->escape_flag = 0;
|
| 47 |
}
|
48 |
}
|
| 48 |
#endif
|
49 |
#endif
|
| 49 |
|
- |
|
| 50 |
// Reset on start bit and start saving data
|
50 |
// Reset on start bit and start saving data
|
| 51 |
if (c == XBEE_START_DELIMITER) {
|
51 |
if (c == XBEE_START_DELIMITER) {
|
| 52 |
// On detect start delimiter, clear out initial array
|
52 |
// On detect start delimiter, clear out initial array
|
| 53 |
xbee_data.dataind = 0;
|
53 |
xbee_data_p->dataind = 0;
|
| 54 |
xbee_data.checksum_sum = 0;
|
54 |
xbee_data_p->checksum_sum = 0;
|
| 55 |
xbee_data.frame_rdy = 0;
|
55 |
xbee_data_p->frame_rdy = 0;
|
| 56 |
xbee_data.read_state = XBEE_STATE_READ_LENGTH_HIGH;
|
56 |
xbee_data_p->read_state = XBEE_STATE_READ_LENGTH_HIGH;
|
| 57 |
*((unsigned char *)xbee_frame) = XBEE_START_DELIMITER;
|
57 |
*((unsigned char *)xbee_frame) = XBEE_START_DELIMITER;
|
| 58 |
} else {
|
58 |
} else {
|
| 59 |
switch (xbee_data.read_state) {
|
59 |
switch (xbee_data_p->read_state) {
|
| 60 |
case XBEE_STATE_READ_START:
|
60 |
case XBEE_STATE_READ_START:
|
| 61 |
// Do nothing and wait till start bit is read
|
61 |
// Do nothing and wait till start bit is read
|
| 62 |
break;
|
62 |
break;
|
| 63 |
case XBEE_STATE_READ_LENGTH_HIGH:
|
63 |
case XBEE_STATE_READ_LENGTH_HIGH:
|
| 64 |
// Read length (MSB)
|
64 |
// Read length (MSB)
|
| 65 |
length->INT_16.char_value[1] = c;
|
65 |
length->INT_16.char_value[1] = c;
|
| 66 |
xbee_data.read_state = XBEE_STATE_READ_LENGTH_LOW;
|
66 |
xbee_data_p->read_state = XBEE_STATE_READ_LENGTH_LOW;
|
| 67 |
break;
|
67 |
break;
|
| 68 |
case XBEE_STATE_READ_LENGTH_LOW:
|
68 |
case XBEE_STATE_READ_LENGTH_LOW:
|
| 69 |
// Read length (LSB)
|
69 |
// Read length (LSB)
|
| 70 |
length->INT_16.char_value[0] = c;
|
70 |
length->INT_16.char_value[0] = c;
|
| 71 |
xbee_data.read_state = XBEE_STATE_READ_FRAME_DATA;
|
71 |
xbee_data_p->read_state = XBEE_STATE_READ_FRAME_DATA;
|
| 72 |
break;
|
72 |
break;
|
| 73 |
case XBEE_STATE_READ_FRAME_DATA:
|
73 |
case XBEE_STATE_READ_FRAME_DATA:
|
| 74 |
// Read unique frame data
|
74 |
// Read unique frame data
|
| 75 |
if (xbee_data.dataind < xbee_data.rcv_frame.length.INT_16.int_value) {
|
75 |
if (xbee_data_p->dataind < xbee_data_p->rcv_frame.length.INT_16.int_value) {
|
| 76 |
*((char*) xbee_data_frame + xbee_data.dataind) = c;
|
76 |
*((char*) xbee_data_frame + xbee_data_p->dataind) = c;
|
| 77 |
xbee_data.checksum_sum += c;
|
77 |
xbee_data_p->checksum_sum += c;
|
| 78 |
xbee_data.dataind++;
|
78 |
xbee_data_p->dataind++;
|
| 79 |
}
|
79 |
}
|
| 80 |
// If total length is read, the next byte is the expected checksum
|
80 |
// If total length is read, the next byte is the expected checksum
|
| 81 |
if (xbee_data.dataind == xbee_data.rcv_frame.length.INT_16.int_value) {
|
81 |
if (xbee_data_p->dataind == xbee_data_p->rcv_frame.length.INT_16.int_value) {
|
| 82 |
xbee_data.read_state = XBEE_STATE_READ_CHECKSUM;
|
82 |
xbee_data_p->read_state = XBEE_STATE_READ_CHECKSUM;
|
| 83 |
}
|
83 |
}
|
| 84 |
break;
|
84 |
break;
|
| 85 |
case XBEE_STATE_READ_CHECKSUM:
|
85 |
case XBEE_STATE_READ_CHECKSUM:
|
| 86 |
// Calculate and compare checksum
|
86 |
// Calculate and compare checksum
|
| 87 |
if (0xFF - xbee_data.checksum_sum == c) {
|
87 |
if (0xFF - xbee_data_p->checksum_sum == c) {
|
| 88 |
// Frame was recieved successfully
|
88 |
// Frame was recieved successfully
|
| 89 |
xbee_data.frame_rdy = 1;
|
89 |
xbee_data_p->frame_rdy = 1;
|
| 90 |
// XBee_Process_Received_Frame();
|
90 |
// XBee_Process_Received_Frame();
|
| 91 |
} else {
|
91 |
} else {
|
| 92 |
// If checksum does not match, drop frame
|
92 |
// If checksum does not match, drop frame
|
| 93 |
DBG_PRINT_XBEE("XBEE: checksum mismatch\r\n");
|
93 |
DBG_PRINT_XBEE("XBEE: checksum mismatch\r\n");
|
| 94 |
}
|
94 |
}
|
| 95 |
xbee_data.read_state = XBEE_STATE_READ_START;
|
95 |
xbee_data_p->read_state = XBEE_STATE_READ_START;
|
| 96 |
break;
|
96 |
break;
|
| 97 |
}
|
97 |
}
|
| 98 |
}
|
98 |
}
|
| 99 |
}
|
99 |
}
|
| 100 |
|
100 |
|
| 101 |
/* This processes the frame data within the interrupt. Dont use this. */
|
101 |
/* This processes the frame data within the interrupt. Dont use this. */
|
| 102 |
void XBee_Process_Received_Frame() {
|
102 |
void XBee_Process_Received_Frame() {
|
| 103 |
// DBG_PRINT_XBEE("Length: %d\r\n", xbee_data.rcv_frame.length.INT_16.int_value);
|
103 |
// DBG_PRINT_XBEE("Length: %d\r\n", xbee_data_p->rcv_frame.length.INT_16.int_value);
|
| 104 |
// Here we process the received frame depending on the frame type
|
104 |
// Here we process the received frame depending on the frame type
|
| 105 |
switch (*((unsigned char *) xbee_data_frame)) {
|
105 |
switch (*((unsigned char *) xbee_data_frame)) {
|
| 106 |
case XBEE_RX_AT_COMMAND_RESPONSE:
|
106 |
case XBEE_RX_AT_COMMAND_RESPONSE:
|
| 107 |
DBG_PRINT_XBEE("XBEE: parsing recieved AT command response frame\r\n");
|
107 |
DBG_PRINT_XBEE("XBEE: parsing recieved AT command response frame\r\n");
|
| 108 |
break;
|
108 |
break;
|
| Line 134... |
Line 134... |
| 134 |
DBG_PRINT_XBEE("XBEE: (ERROR) unrecognized frame type\r\n");
|
134 |
DBG_PRINT_XBEE("XBEE: (ERROR) unrecognized frame type\r\n");
|
| 135 |
}
|
135 |
}
|
| 136 |
}
|
136 |
}
|
| 137 |
|
137 |
|
| 138 |
unsigned int XBee_Get_Received_Frame(unsigned char *frame) {
|
138 |
unsigned int XBee_Get_Received_Frame(unsigned char *frame) {
|
| 139 |
if (!xbee_data.frame_rdy) {
|
139 |
if (!xbee_data_p->frame_rdy) {
|
| 140 |
return 0;
|
140 |
return 0;
|
| 141 |
} else {
|
141 |
} else {
|
| 142 |
memcpy(frame, xbee_data_frame, xbee_data.rcv_frame.length.INT_16.int_value);
|
142 |
memcpy(frame, xbee_data_frame, xbee_data_p->rcv_frame.length.INT_16.int_value);
|
| 143 |
xbee_data.frame_rdy = 0; // Reset frame ready status
|
143 |
xbee_data_p->frame_rdy = 0; // Reset frame ready status
|
| 144 |
return xbee_data.rcv_frame.length.INT_16.int_value;
|
144 |
return xbee_data_p->rcv_frame.length.INT_16.int_value;
|
| 145 |
}
|
145 |
}
|
| 146 |
}
|
146 |
}
|
| 147 |
|
147 |
|
| 148 |
void XBee_Process_Transmit_Frame(unsigned char *data, unsigned char length) {
|
148 |
void XBee_Process_Transmit_Frame(unsigned char *data, unsigned char length) {
|
| 149 |
#ifdef XBEE_USE_ESCAPE_CHAR
|
149 |
#ifdef XBEE_USE_ESCAPE_CHAR
|