root / branches / wireless / code / projects / libwireless / xbee.c @ 1604
History | View | Annotate | Download (30.8 KB)
1 | 1576 | dsschult | /**
|
---|---|---|---|
2 | * Copyright (c) 2009 Colony Project
|
||
3 | *
|
||
4 | * Permission is hereby granted, free of charge, to any person
|
||
5 | * obtaining a copy of this software and associated documentation
|
||
6 | * files (the "Software"), to deal in the Software without
|
||
7 | * restriction, including without limitation the rights to use,
|
||
8 | * copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||
9 | * copies of the Software, and to permit persons to whom the
|
||
10 | * Software is furnished to do so, subject to the following
|
||
11 | * conditions:
|
||
12 | *
|
||
13 | * The above copyright notice and this permission notice shall be
|
||
14 | * included in all copies or substantial portions of the Software.
|
||
15 | *
|
||
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||
17 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
||
18 | * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||
19 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
||
20 | * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
||
21 | * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||
22 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
||
23 | * OTHER DEALINGS IN THE SOFTWARE.
|
||
24 | **/
|
||
25 | |||
26 | /**
|
||
27 | * @file xbee.c
|
||
28 | * @brief XBee Interface
|
||
29 | *
|
||
30 | * Implementation of low level communication with the XBee in API mode.
|
||
31 | *
|
||
32 | * @author Colony Project, CMU Robotics Club
|
||
33 | **/
|
||
34 | |||
35 | 1601 | dsschult | #include <string.h> |
36 | #include <avr/interrupt.h> |
||
37 | 1576 | dsschult | #include <time.h> |
38 | #include <serial.h> // TODO: integrate serial xbee functions into this file |
||
39 | 1601 | dsschult | #include "wl_defs.h" |
40 | #include "xbee.h" |
||
41 | 1576 | dsschult | |
42 | |||
43 | 1601 | dsschult | /* Internal Function Prototypes */
|
44 | 1576 | dsschult | |
45 | // TODO: convert all int references to int16_t syntax (see stdint.h)
|
||
46 | |||
47 | 1601 | dsschult | /* I/O Functions */
|
48 | static int8_t xbee_send(uint8_t* buf, uint16_t size);
|
||
49 | static int8_t xbee_send_string(unsigned char* c); |
||
50 | 1576 | dsschult | |
51 | 1601 | dsschult | /* Command Mode Functions */
|
52 | 1576 | dsschult | static int xbee_enter_command_mode(void); |
53 | static int xbee_exit_command_mode(void); |
||
54 | static int xbee_enter_api_mode(void); |
||
55 | static int xbee_exit_api_mode(void); |
||
56 | static int xbee_wait_for_string(char* s, int len); |
||
57 | static int xbee_wait_for_ok(void); |
||
58 | |||
59 | 1601 | dsschult | /* API Mode Functions */
|
60 | static int xbee_handle_packet(uint8_t* packet, uint16_t len); |
||
61 | static int xbee_handle_at_command_response(uint16_t command, uint8_t result, uint8_t len); |
||
62 | static int xbee_handle_at_command_response(uint16_t command, uint8_t result, uint8_t len); |
||
63 | 1576 | dsschult | static void xbee_handle_status(char status); |
64 | static int xbee_verify_checksum(char* packet, int len); |
||
65 | static char xbee_compute_checksum(char* packet, int len); |
||
66 | static int xbee_send_frame(char* buf, int len); |
||
67 | int xbee_send_read_at_command(char* command); |
||
68 | static int xbee_send_modify_at_command(char* command, char* value); |
||
69 | |||
70 | 1601 | dsschult | /* Buffer Manipulation Functions */
|
71 | int8_t xbee_basic_buf_add(uint8_t *ptr, uint8_t byte); |
||
72 | uint8_t xbee_basic_buf_get(uint8_t *ptr); |
||
73 | int8_t xbee_other_buf_add(uint8_t *ptr, uint8_t byte); |
||
74 | 1576 | dsschult | |
75 | |||
76 | 1601 | dsschult | /*Global Variables*/
|
77 | 1576 | dsschult | |
78 | 1591 | dsschult | // array for basic packets
|
79 | 1589 | alevkoy | static uint8_t xbee_basic_buf[PACKET_BUFFER_SIZE];
|
80 | 1591 | dsschult | |
81 | // beginning of first packet in basic buffer
|
||
82 | static uint8_t basic_buf_first = 0; |
||
83 | |||
84 | // byte after end of last packet in basic buffer (only access < basic_buf_last)
|
||
85 | // aka, the first free byte in basic buffer
|
||
86 | static uint8_t basic_buf_last = 0; |
||
87 | |||
88 | // array for other packets
|
||
89 | 1589 | alevkoy | static uint8_t xbee_other_buf[PACKET_BUFFER_SIZE];
|
90 | 1576 | dsschult | |
91 | 1591 | dsschult | // beginning of first packet in other buffer
|
92 | static uint8_t other_buf_first = 0; |
||
93 | |||
94 | // byte after end of last packet in other buffer (only access < other_buf_last)
|
||
95 | // aka, the first free byte in other buffer
|
||
96 | static uint8_t other_buf_last = 0; |
||
97 | |||
98 | 1600 | dsschult | // xbee status
|
99 | #define XBEE_API_OFF 0x0 |
||
100 | #define XBEE_API_ON 0x1 |
||
101 | #define XBEE_API_ESCAPE 0x2 |
||
102 | #define XBEE_NORMAL 0x00 |
||
103 | #define XBEE_COMMAND_WAIT 0x80 |
||
104 | #define XBEE_COMMAND_RESPONSE 0xC0 |
||
105 | 1604 | dsschult | #define XBEE_INITD 0x08 |
106 | 1600 | dsschult | static uint8_t xbee_status = XBEE_API_OFF|XBEE_NORMAL;
|
107 | |||
108 | // xbee command response (for PAN, channel, address, etc)
|
||
109 | static uint8_t xbee_command[4]; |
||
110 | |||
111 | 1591 | dsschult | // external ack handler (wireless_send.c)
|
112 | extern void ackhandle(uint8_t num,uint8_t val); |
||
113 | |||
114 | 1601 | dsschult | |
115 | 1587 | dsschult | /**@addtogroup xbee
|
116 | * @{ **/
|
||
117 | |||
118 | 1576 | dsschult | /*Function Implementations*/
|
119 | |||
120 | /**
|
||
121 | * Interrupt for the robot. Adds bytes received from the xbee
|
||
122 | * to the buffer.
|
||
123 | **/
|
||
124 | #ifndef FIREFLY
|
||
125 | 1591 | dsschult | #define PORT UDR1
|
126 | #define FLAG RXC1
|
||
127 | 1576 | dsschult | ISR(USART1_RX_vect) |
128 | #else
|
||
129 | 1591 | dsschult | #define PORT UDR0
|
130 | #define FLAG RXC0
|
||
131 | 1576 | dsschult | SIGNAL(SIG_USART0_RECV) |
132 | 1591 | dsschult | #endif
|
133 | 1576 | dsschult | { |
134 | 1591 | dsschult | // start of frame
|
135 | uint8_t apitype = PORT; // get frame start byte
|
||
136 | uint16_t i=0;
|
||
137 | uint16_t len=0;
|
||
138 | 1600 | dsschult | |
139 | // check that we're in API mode
|
||
140 | if (xbee_status&0x03 == XBEE_API_OFF || apitype != XBEE_FRAME_START) { |
||
141 | // not in API mode
|
||
142 | if (xbee_status&0xC0) == XBEE_COMMAND_WAIT) { |
||
143 | // get rest of command and put in basic buf
|
||
144 | xbee_basic_buf[0] = apitype;
|
||
145 | if (xbee_basic_buf[i] != '\r') { |
||
146 | while(i < PACKET_BUFFER_SIZE) {
|
||
147 | if (FLAG) {
|
||
148 | xbee_basic_buf[i] = PORT; |
||
149 | if (xbee_basic_buf[i] == '\r') |
||
150 | break;
|
||
151 | } |
||
152 | } |
||
153 | } |
||
154 | // signal handler that command response is done
|
||
155 | 1601 | dsschult | xbee_status = (xbee_status&0x3F)|XBEE_COMMAND_RESPONSE;
|
156 | 1600 | dsschult | } |
157 | return;
|
||
158 | } |
||
159 | |||
160 | 1591 | dsschult | // get length and type
|
161 | while(i<3) { |
||
162 | if (FLAG) {
|
||
163 | if (i==0) |
||
164 | len |= PORT<<8;
|
||
165 | else if (i==1) |
||
166 | len |= PORT; |
||
167 | else if (i==2) |
||
168 | apitype = PORT; |
||
169 | i++; |
||
170 | } |
||
171 | } |
||
172 | |||
173 | // do something based on the type
|
||
174 | i=1;
|
||
175 | switch(apitype) {
|
||
176 | 1600 | dsschult | case XBEE_FRAME_AT_COMMAND_RESPONSE:
|
177 | 1591 | dsschult | // AT command response
|
178 | 1600 | dsschult | if (xbee_status&0xC0 == XBEE_COMMAND_RESPONSE) |
179 | return; // we're currently processing a command, so drop the incoming one |
||
180 | 1591 | dsschult | uint16_t atcommand=0;
|
181 | uint8_t ptr=basic_buf_last; |
||
182 | while(i<len) {
|
||
183 | if (FLAG) {
|
||
184 | if (i==1) |
||
185 | apitype = PORT; // get frame id, but ignore it
|
||
186 | else if (i==2) |
||
187 | atcommand |= PORT<<8; // get command char1 |
||
188 | else if (i==3) |
||
189 | atcommand |= PORT; // get command char2
|
||
190 | else if (i==4) |
||
191 | apitype = PORT; // get status
|
||
192 | else {
|
||
193 | // put the command response on the basic buf temporarily
|
||
194 | 1600 | dsschult | if (xbee_basic_buf_add(&ptr,PORT) != 0) |
195 | 1591 | dsschult | break;
|
196 | } |
||
197 | i++; |
||
198 | } |
||
199 | 1600 | dsschult | } |
200 | // handle AT command
|
||
201 | xbee_handle_at_command_response(atcommand,apitype,i-5); // TODO: rewrite function |
||
202 | 1591 | dsschult | break;
|
203 | 1600 | dsschult | case XBEE_FRAME_TX_STATUS:
|
204 | 1591 | dsschult | // TX status
|
205 | uint8_t frameid=0;
|
||
206 | while(i<len) {
|
||
207 | if (FLAG) {
|
||
208 | if (i==1) |
||
209 | frameid = PORT; |
||
210 | else {
|
||
211 | ackhandle(frameid,PORT); // handle the status
|
||
212 | break;
|
||
213 | } |
||
214 | i++; |
||
215 | } |
||
216 | } |
||
217 | break;
|
||
218 | 1600 | dsschult | case XBEE_FRAME_RX_64:
|
219 | 1591 | dsschult | // receive a packet with 64bit address
|
220 | 1600 | dsschult | break; // TODO: implement this (even if we don't use it) |
221 | case XBEE_FRAME_RX_16:
|
||
222 | 1591 | dsschult | // receive a packet with 16bit address
|
223 | uint16_t source = 0;
|
||
224 | uint8_t framenum = 0;
|
||
225 | uint8_t group = 0;
|
||
226 | uint8_t ptr=basic_buf_last; |
||
227 | while(i<len) {
|
||
228 | if (FLAG) {
|
||
229 | if (i==1) |
||
230 | source |= PORT<<8; // get source hi byte |
||
231 | else if (i==2) |
||
232 | source |= PORT; // get source lo byte
|
||
233 | else if (i==3) |
||
234 | apitype = PORT; // get RSSI, and ignore
|
||
235 | else if (i==4) |
||
236 | apitype = PORT; // get options, and ignore
|
||
237 | else if (i==5) { |
||
238 | framenum = PORT; // get the frame number
|
||
239 | if (check_last_receive(source,framenum) != 0) { // TODO: write function |
||
240 | // we've already received this frame
|
||
241 | ptr = 0xFF; // signal to skip processing |
||
242 | break;
|
||
243 | } |
||
244 | } |
||
245 | else if (i==6) { |
||
246 | group = PORT; // get group number
|
||
247 | if (group == 0) { |
||
248 | ptr = basic_buf_last+1;
|
||
249 | // add source to buffer
|
||
250 | 1600 | dsschult | if (xbee_basic_buf_add(&ptr,(uint8_t)((source&0xFF00)>>8)) != 0) |
251 | 1591 | dsschult | break;
|
252 | 1600 | dsschult | if (xbee_basic_buf_add(&ptr,(uint8_t)(source&0x00FF)) != 0) |
253 | 1591 | dsschult | break;
|
254 | } else {
|
||
255 | ptr = other_buf_last+1;
|
||
256 | 1600 | dsschult | // add source and group to buffer
|
257 | if (xbee_other_buf_add(&ptr,group) != 0) |
||
258 | 1591 | dsschult | break;
|
259 | 1600 | dsschult | if (xbee_other_buf_add(&ptr,(uint8_t)((source&0xFF00)>>8)) != 0) |
260 | 1591 | dsschult | break;
|
261 | 1600 | dsschult | if (xbee_other_buf_add(&ptr,(uint8_t)(source&0x00FF)) != 0) |
262 | 1591 | dsschult | break;
|
263 | } |
||
264 | } |
||
265 | 1600 | dsschult | else { // TODO: handle escaped characters supported by APIv2 |
266 | 1591 | dsschult | // put packet data on the correct buffer
|
267 | 1600 | dsschult | if (group == 0 && xbee_basic_buf_add(&ptr,PORT) != 0) |
268 | break;
|
||
269 | else if (xbee_other_buf_add(&ptr,PORT) != 0) |
||
270 | break;
|
||
271 | 1591 | dsschult | } |
272 | i++; |
||
273 | } |
||
274 | } |
||
275 | 1600 | dsschult | if (ptr != 0xFF && i > 6) { |
276 | 1591 | dsschult | if (group == 0) { |
277 | xbee_basic_buf[basic_buf_last] = i-6; // set length |
||
278 | basic_buf_last = ptr; |
||
279 | } |
||
280 | else {
|
||
281 | xbee_other_buf[other_buf_last] = i-6; // set length |
||
282 | // check if we have a high priority group
|
||
283 | for(;;)
|
||
284 | if (HIGH_PRIORITY) {
|
||
285 | // handle receive now
|
||
286 | ptr = 0xFF;
|
||
287 | break;
|
||
288 | } |
||
289 | if (ptr != 0xFF) { |
||
290 | // handle receive later
|
||
291 | other_buf_last = ptr; |
||
292 | } |
||
293 | } |
||
294 | } |
||
295 | break;
|
||
296 | 1600 | dsschult | } // end of switch statement
|
297 | while (1) { |
||
298 | if (FLAG) {
|
||
299 | apitype = PORT; // get checksum, and ignore
|
||
300 | break;
|
||
301 | } |
||
302 | 1591 | dsschult | } |
303 | } // end of interrupt
|
||
304 | 1576 | dsschult | |
305 | |||
306 | 1600 | dsschult | /* adds a byte to the basic buffer */
|
307 | int8_t xbee_basic_buf_add(uint8_t *ptr, uint8_t byte) { |
||
308 | if (*ptr == basic_buf_first) {
|
||
309 | // buffer full
|
||
310 | WL_DEBUG_PRINT("basic buffer full\r\n");
|
||
311 | return -1; |
||
312 | } |
||
313 | xbee_basic_buf[(*ptr)++] = byte); |
||
314 | if (*ptr == PACKET_BUFFER_SIZE)
|
||
315 | *ptr = 0;
|
||
316 | return 0; |
||
317 | } |
||
318 | 1601 | dsschult | /* gets a byte from the basic buffer */
|
319 | uint8_t xbee_basic_buf_get(uint8_t *ptr) { |
||
320 | uint8_t byte = xbee_basic_buf[(*ptr)++]; |
||
321 | if (*ptr == PACKET_BUFFER_SIZE)
|
||
322 | *ptr = 0;
|
||
323 | return byte;
|
||
324 | } |
||
325 | 1600 | dsschult | /* adds a byte to the other buffer */
|
326 | int8_t xbee_other_buf_add(uint8_t *ptr, uint8_t byte) { |
||
327 | if (*ptr == other_buf_first) {
|
||
328 | // buffer full
|
||
329 | WL_DEBUG_PRINT("other buffer full\r\n");
|
||
330 | return -1; |
||
331 | } |
||
332 | xbee_other_buf[(*ptr)++] = byte; |
||
333 | if (ptr == PACKET_BUFFER_SIZE)
|
||
334 | *ptr = 0;
|
||
335 | return 0; |
||
336 | } |
||
337 | |||
338 | |||
339 | 1576 | dsschult | /**
|
340 | * Initializes the XBee library so that other functions may be used.
|
||
341 | **/
|
||
342 | 1604 | dsschult | int8_t xbee_init() |
343 | 1576 | dsschult | { |
344 | 1604 | dsschult | WL_DEBUG_PRINT("in xbee_init\n");
|
345 | |||
346 | if(xbee_status&0x08 == XBEE_INITD) { |
||
347 | return WL_ERROR_INIT_ALREADY_INITD;
|
||
348 | 1576 | dsschult | } |
349 | 1604 | dsschult | |
350 | // Set startup baud rate of 9600
|
||
351 | // Set frame format: 8data, 1stop bit, asynchronous normal mode
|
||
352 | // Enable receiver and transmitter and the receiving interrupt
|
||
353 | 1576 | dsschult | #ifdef FIREFLY
|
354 | 1604 | dsschult | UCSR0A |= (1<<U2X0);
|
355 | UBRR0H = 0x00;
|
||
356 | UBRR0L = 103;
|
||
357 | UCSR0C |= (1<<UCSZ00) | (1<<UCSZ01); |
||
358 | UCSR0B |= (1<<RXEN0) | (1<<TXEN0) | (1<<RXCIE0); |
||
359 | 1576 | dsschult | #else
|
360 | 1604 | dsschult | // Bayboard or robot
|
361 | UCSR1A |= (1<<U2X1);
|
||
362 | UBRR1H = 0x00;
|
||
363 | UBRR1L = 103;
|
||
364 | UCSR1C |= (1<<UCSZ10) | (1<<UCSZ11); |
||
365 | UCSR1B |= (1<<RXEN1) | (1<<TXEN1) | (1<<RXCIE1); |
||
366 | 1576 | dsschult | #endif
|
367 | sei(); |
||
368 | |||
369 | WL_DEBUG_PRINT("Entering command mode.\r\n");
|
||
370 | if (xbee_enter_command_mode() != 0) { |
||
371 | usb_puts("error entering command mode\r\n");
|
||
372 | return -1; |
||
373 | } |
||
374 | WL_DEBUG_PRINT("Entered command mode.\r\n");
|
||
375 | |||
376 | // reset baud rate
|
||
377 | WL_DEBUG_PRINT("Resetting Baud to ");
|
||
378 | WL_DEBUG_PRINT(XBEE_BAUD_STR); |
||
379 | WL_DEBUG_PRINT("\r\n");
|
||
380 | |||
381 | // set baud on xbee
|
||
382 | #if (XBEE_BAUD == 115200) |
||
383 | 1601 | dsschult | xbee_send_string("ATBD7\r");
|
384 | 1576 | dsschult | #elif (XBEE_BAUD == 57600) |
385 | 1601 | dsschult | xbee_send_string("ATBD6\r");
|
386 | 1576 | dsschult | #elif (XBEE_BAUD == 38400) |
387 | 1601 | dsschult | xbee_send_string("ATBD5\r");
|
388 | 1576 | dsschult | #elif (XBEE_BAUD == 19200) |
389 | 1601 | dsschult | xbee_send_string("ATBD4\r");
|
390 | 1576 | dsschult | #elif (XBEE_BAUD == 9600) |
391 | // already at this baud rate
|
||
392 | //xbee_send_string("ATBD3\r\n");
|
||
393 | #else
|
||
394 | WL_DEBUG_PRINT("undefined baud rate\r\n");
|
||
395 | 1601 | dsschult | return WL_ERROR_BAUD;
|
396 | 1576 | dsschult | #endif
|
397 | // exit command mode
|
||
398 | xbee_wait_for_ok(); |
||
399 | WL_DEBUG_PRINT("got ok from baud reset\r\n");
|
||
400 | 1601 | dsschult | xbee_send_string("ATCN\r");
|
401 | 1576 | dsschult | xbee_wait_for_ok(); |
402 | WL_DEBUG_PRINT("got ok from exiting command mode\r\n");
|
||
403 | |||
404 | // set UART baud
|
||
405 | 1604 | dsschult | #ifdef FIREFLY
|
406 | 1601 | dsschult | #if (XBEE_BAUD == 115200) |
407 | 1604 | dsschult | UBRR0H = 0x00;
|
408 | UBRR0L = 8;
|
||
409 | #elif (XBEE_BAUD == 57600) |
||
410 | UBRR0H = 0x00;
|
||
411 | UBRR0L = 16;
|
||
412 | #elif (XBEE_BAUD == 38400) |
||
413 | UBRR0H = 0x00;
|
||
414 | UBRR0L = 25;
|
||
415 | #elif (XBEE_BAUD == 19200) |
||
416 | UBRR0H = 0x00;
|
||
417 | UBRR0L = 51;
|
||
418 | #elif (XBEE_BAUD == 9600) |
||
419 | /* this is the default baud rate, so do nothing
|
||
420 | UBRR0H = 0x00;
|
||
421 | UBRR0L = 103;*/
|
||
422 | #else
|
||
423 | WL_DEBUG_PRINT("undefined baud rate\r\n");
|
||
424 | return WL_ERROR_BUAD;
|
||
425 | #endif
|
||
426 | #else // Bayboard or robot |
||
427 | #if (XBEE_BAUD == 115200) |
||
428 | 1576 | dsschult | UBRR1H = 0x00;
|
429 | UBRR1L = 8;
|
||
430 | #elif (XBEE_BAUD == 57600) |
||
431 | UBRR1H = 0x00;
|
||
432 | UBRR1L = 16;
|
||
433 | #elif (XBEE_BAUD == 38400) |
||
434 | UBRR1H = 0x00;
|
||
435 | UBRR1L = 25;
|
||
436 | #elif (XBEE_BAUD == 19200) |
||
437 | UBRR1H = 0x00;
|
||
438 | UBRR1L = 51;
|
||
439 | #elif (XBEE_BAUD == 9600) |
||
440 | /* this is the default baud rate, so do nothing
|
||
441 | UBRR1H = 0x00;
|
||
442 | 1604 | dsschult | UBRR1L = 103;*/
|
443 | 1601 | dsschult | #else
|
444 | WL_DEBUG_PRINT("undefined baud rate\r\n");
|
||
445 | return WL_ERROR_BUAD;
|
||
446 | 1576 | dsschult | #endif
|
447 | 1604 | dsschult | #endif
|
448 | 1576 | dsschult | delay_ms(50);
|
449 | |||
450 | // enter command mode
|
||
451 | WL_DEBUG_PRINT("entering command mode 2\r\n");
|
||
452 | xbee_send_string("+++");
|
||
453 | xbee_wait_for_ok(); |
||
454 | WL_DEBUG_PRINT("entered command mode 2\r\n");
|
||
455 | |||
456 | if (xbee_enter_api_mode() != 0) { |
||
457 | WL_DEBUG_PRINT("can't enter api mode\r\n");
|
||
458 | return -1; |
||
459 | } |
||
460 | |||
461 | WL_DEBUG_PRINT("Entered api mode.\r\n");
|
||
462 | |||
463 | if (xbee_exit_command_mode() != 0) { |
||
464 | WL_DEBUG_PRINT("can't exit command mode\r\n");
|
||
465 | return -1; |
||
466 | } |
||
467 | |||
468 | WL_DEBUG_PRINT("Left command mode.\r\n");
|
||
469 | |||
470 | 1601 | dsschult | // TODO: we should set the MY address to the robot address from eeprom
|
471 | 1576 | dsschult | |
472 | 1604 | dsschult | |
473 | // set status
|
||
474 | xbee_status |= XBEE_INITD; |
||
475 | |||
476 | 1601 | dsschult | return WL_SUCCESS;
|
477 | 1576 | dsschult | } |
478 | |||
479 | /**
|
||
480 | 1601 | dsschult | * Call when finished using the XBee library.
|
481 | 1576 | dsschult | **/
|
482 | 1604 | dsschult | int8_t xbee_terminate() |
483 | 1576 | dsschult | { |
484 | 1604 | dsschult | if (xbee_exit_api_mode() != WL_SUCCESS) {
|
485 | WL_DEBUG_PRINT("xbee termination failed\r\n");
|
||
486 | return WL_ERROR_TERMINATION_FAILED;
|
||
487 | } |
||
488 | xbee_status = xbee_status&0xF7; // clean initd status |
||
489 | return WL_SUCCESS;
|
||
490 | 1576 | dsschult | } |
491 | |||
492 | /**
|
||
493 | 1604 | dsschult | * Sends a character to the XBee.
|
494 | *
|
||
495 | * @param c the byte to send
|
||
496 | * @return 0 for success, nonzero for failure
|
||
497 | **/
|
||
498 | int xbee_putc(char c) { |
||
499 | if(!(xbee_status&XBEE_INITD))
|
||
500 | return WL_ERROR_LIBRARY_NOT_INITD;
|
||
501 | |||
502 | // Wait until buffer is clear for sending
|
||
503 | // Then load buffer with your character
|
||
504 | #ifdef FIREFLY
|
||
505 | loop_until_bit_is_set(UCSR0A, UDRE0); |
||
506 | UDR0 = c; |
||
507 | #else
|
||
508 | loop_until_bit_is_set(UCSR1A, UDRE1); |
||
509 | UDR1 = c; |
||
510 | #endif
|
||
511 | |||
512 | return WL_SUCCESS;
|
||
513 | } |
||
514 | |||
515 | /**
|
||
516 | * Returns the first byte in the buffer received from xbee.
|
||
517 | * This function blocks execution until a character has been
|
||
518 | * received. xbee_init must be called before this function
|
||
519 | * may be used.
|
||
520 | *
|
||
521 | * @return the first character in the xbee buffer, -1 on error
|
||
522 | *
|
||
523 | * @see xbee_init, xbee_getc_nb
|
||
524 | **/
|
||
525 | int16_t xbee_getc(void) {
|
||
526 | if(!(xbee_status&XBEE_INITD))
|
||
527 | return WL_ERROR_LIBRARY_NOT_INITD;
|
||
528 | |||
529 | // Wait for the receive buffer to be filled
|
||
530 | // Then read the receive buffer
|
||
531 | #ifdef FIREFLY
|
||
532 | loop_until_bit_is_set(UCSR0A, RXC0); |
||
533 | return UDR0;
|
||
534 | #else
|
||
535 | loop_until_bit_is_set(UCSR1A, RXC1); |
||
536 | return UDR1;
|
||
537 | #endif
|
||
538 | } |
||
539 | |||
540 | /**
|
||
541 | * Non blocking version of xbee_getc. If a byte is present in the buffer,
|
||
542 | * it is returned, otherwise -1 is returned immediately. xbee_init
|
||
543 | * must be called before this function can be used.
|
||
544 | *
|
||
545 | * @param c The received byte. This will be set if a byte has been received.
|
||
546 | *
|
||
547 | * @return -1 If no byte is available, 0 otherwise, positive for error
|
||
548 | *
|
||
549 | * @see xbee_getc
|
||
550 | **/
|
||
551 | int8_t xbee_getc_nb(uint8_t *c) { |
||
552 | if(!(xbee_status&XBEE_INITD))
|
||
553 | return WL_ERROR_LIBRARY_NOT_INITD;
|
||
554 | |||
555 | // check if the receive buffer is filled
|
||
556 | #ifdef FIREFLY
|
||
557 | if (UCSR0A & (1<<RXC0)) { |
||
558 | (*c) = UDR0; |
||
559 | #else
|
||
560 | if (UCSR1A & (1<<RXC1)) { |
||
561 | (*c) = UDR1; |
||
562 | #endif
|
||
563 | return WL_SUCCESS;
|
||
564 | } |
||
565 | return -1; // Return empty |
||
566 | } |
||
567 | |||
568 | /**
|
||
569 | 1576 | dsschult | * Send a buffer buf of size bytes to the XBee.
|
570 | *
|
||
571 | * @param buf the buffer of data to send
|
||
572 | * @param size the number of bytes to send
|
||
573 | **/
|
||
574 | 1600 | dsschult | static int8_t xbee_send(uint8_t* buf, uint16_t size)
|
575 | 1576 | dsschult | { |
576 | 1600 | dsschult | while(--size > 0) |
577 | if (xbee_putc(buf[i]) != WL_SUCCESS)
|
||
578 | return WL_ERROR_SEND;
|
||
579 | } |
||
580 | if (xbee_putc(buf[0]) != WL_SUCCESS) |
||
581 | return WL_ERROR_SEND;
|
||
582 | 1576 | dsschult | |
583 | 1600 | dsschult | return WL_SUCCESS;
|
584 | 1576 | dsschult | } |
585 | |||
586 | /**
|
||
587 | * Sends a string to the XBee.
|
||
588 | *
|
||
589 | * @param c the string to send to the XBEE
|
||
590 | **/
|
||
591 | 1600 | dsschult | static int8_t xbee_send_string(unsigned char* c) |
592 | 1576 | dsschult | { |
593 | return xbee_send(c, strlen(c));
|
||
594 | } |
||
595 | |||
596 | |||
597 | /**
|
||
598 | * Enter into command mode.
|
||
599 | **/
|
||
600 | 1600 | dsschult | static int8_t xbee_enter_command_mode()
|
601 | 1576 | dsschult | { |
602 | 1601 | dsschult | if (xbee_send_string("+++") != WL_SUCCESS) { |
603 | return WL_ERROR_XBEE_COMMAND;
|
||
604 | 1576 | dsschult | } |
605 | |||
606 | 1601 | dsschult | if (xbee_wait_for_ok() != WL_SUCCESS) {
|
607 | return WL_ERROR_XBEE_COMMAND;
|
||
608 | 1576 | dsschult | } |
609 | 1601 | dsschult | |
610 | return WL_SUCCESS;
|
||
611 | 1576 | dsschult | } |
612 | |||
613 | /**
|
||
614 | * Exit from command mode.
|
||
615 | **/
|
||
616 | 1600 | dsschult | static int8_t xbee_exit_command_mode()
|
617 | 1576 | dsschult | { |
618 | if (xbee_send_string("ATCN\r") != 0) { |
||
619 | 1604 | dsschult | return WL_ERROR_SEND;
|
620 | 1576 | dsschult | } |
621 | xbee_wait_for_ok(); |
||
622 | |||
623 | 1604 | dsschult | return WL_SUCCESS;
|
624 | 1576 | dsschult | } |
625 | |||
626 | /**
|
||
627 | * Enter API mode.
|
||
628 | **/
|
||
629 | 1604 | dsschult | static int8_t xbee_enter_api_mode()
|
630 | 1576 | dsschult | { |
631 | if (xbee_send_string("ATAP 1\r") != 0) { |
||
632 | 1604 | dsschult | return WL_ERROR_SEND;
|
633 | 1576 | dsschult | } |
634 | xbee_wait_for_ok(); |
||
635 | 1600 | dsschult | |
636 | xbee_status = (xbee_status&0xFC)|XBEE_API_ON;
|
||
637 | 1576 | dsschult | |
638 | 1604 | dsschult | return WL_SUCCESS;
|
639 | 1576 | dsschult | } |
640 | |||
641 | /**
|
||
642 | 1600 | dsschult | * Enter API mode 2.
|
643 | **/
|
||
644 | 1604 | dsschult | static int8_t xbee_enter_api_mode2()
|
645 | 1600 | dsschult | { |
646 | if (xbee_send_string("ATAP 2\r") != 0) { |
||
647 | 1604 | dsschult | return WL_ERROR_SEND;
|
648 | 1600 | dsschult | } |
649 | xbee_wait_for_ok(); |
||
650 | |||
651 | xbee_status = (xbee_status&0xFC)|XBEE_API_ESCAPE;
|
||
652 | |||
653 | 1604 | dsschult | return WL_SUCCESS;
|
654 | 1600 | dsschult | } |
655 | |||
656 | /**
|
||
657 | 1576 | dsschult | * Exit API mode.
|
658 | **/
|
||
659 | 1604 | dsschult | static int8_t xbee_exit_api_mode()
|
660 | 1576 | dsschult | { |
661 | 1604 | dsschult | if (xbee_status&0xC0 == XBEE_COMMAND_WAIT |
662 | || xbee_status&0xC0 == XBEE_COMMAND_RESPONSE)
|
||
663 | return WL_ERROR_XBEE_COMMAND; // can't do command right now |
||
664 | 1600 | dsschult | |
665 | 1604 | dsschult | int16_t i=0;
|
666 | // change status to command wait
|
||
667 | xbee_status = (xbee_status&0x3F)|XBEE_COMMAND_WAIT;
|
||
668 | xbee_send_modify_at_command("AP","0",1); // send command |
||
669 | // wait for up to 30 ms
|
||
670 | while(xbee_status&0xC0 != XBEE_COMMAND_RESPONSE && i++ < 10000) { |
||
671 | delay_us(1); // wait 3us |
||
672 | } |
||
673 | if (i < 1000 && xbee_command[0] == 'A' && xbee_command[1] == 'P') { |
||
674 | i = WL_SUCCESS; |
||
675 | else
|
||
676 | i = WL_ERROR_XBEE_COMMAND; // set error code
|
||
677 | xbee_status = (xbee_status&0x3C)|XBEE_API_OFF; // reset status |
||
678 | return (int8_t)i; // return |
||
679 | 1576 | dsschult | } |
680 | |||
681 | /**
|
||
682 | * Wait until the string "OK\r" is received from the XBee.
|
||
683 | **/
|
||
684 | static int xbee_wait_for_ok() |
||
685 | { |
||
686 | return xbee_wait_for_string("OK\r", 3); |
||
687 | } |
||
688 | |||
689 | /**
|
||
690 | * Delay until the specified string is received from
|
||
691 | 1604 | dsschult | * the XBee.
|
692 | 1576 | dsschult | *
|
693 | 1604 | dsschult | * Only works when not in API mode
|
694 | 1576 | dsschult | *
|
695 | * @param s the string to receive
|
||
696 | * @param len the length of the string
|
||
697 | **/
|
||
698 | 1600 | dsschult | static int8_t xbee_wait_for_string(char* s, int len) |
699 | 1576 | dsschult | { |
700 | 1600 | dsschult | uint8_t i=0;
|
701 | if (xbee_status&0x03 == XBEE_API_OFF) { |
||
702 | // wait until the response is received (only wait 1 second)
|
||
703 | while(xbee_status&0xC0 != XBEE_COMMAND_RESPONSE && i++ < 1000) { |
||
704 | delay_us(1);
|
||
705 | } |
||
706 | // check response
|
||
707 | if (i >= 1000 || memcmp(s,xbee_basic_buf,len) != 0) { |
||
708 | // bad response
|
||
709 | WL_DEBUG_PRINT("Bad response when waiting for string ");
|
||
710 | WL_DEBUG_PRINT(s); |
||
711 | WL_DEBUG_PRINT("\r\n");
|
||
712 | return -1; |
||
713 | } |
||
714 | 1601 | dsschult | |
715 | // clear response
|
||
716 | xbee_status = xbee_status&0x3F;
|
||
717 | 1600 | dsschult | } |
718 | 1576 | dsschult | |
719 | return 0; |
||
720 | } |
||
721 | |||
722 | /**
|
||
723 | 1600 | dsschult | * Delay until we receive a command response.
|
724 | * (either OK\r or some actual value)
|
||
725 | *
|
||
726 | * Only works when not in API mode
|
||
727 | *
|
||
728 | * @param s the string to store the response in
|
||
729 | * @param len the length of the string
|
||
730 | */
|
||
731 | static int8_t xbee_wait_for_response(char* s, int len) { |
||
732 | uint8_t i=0;
|
||
733 | if (xbee_status&0x03 == XBEE_API_OFF) { |
||
734 | // wait until the response is received (only wait 1 second)
|
||
735 | while(xbee_status&0xC0 != XBEE_COMMAND_RESPONSE && i++ < 1000) { |
||
736 | delay_us(1);
|
||
737 | } |
||
738 | // check response
|
||
739 | if (i >= 1000) |
||
740 | return -1; |
||
741 | } else {
|
||
742 | i=strcspn(xbee_basic_buf,"\r");
|
||
743 | if (i<PACKET_BUFFER_SIZE) {
|
||
744 | memcpy(s,xbee_basic_buf,i); |
||
745 | 1601 | dsschult | xbee_status = xbee_status&0x3F; // clear response |
746 | 1600 | dsschult | return 0; |
747 | } |
||
748 | else
|
||
749 | return -1; |
||
750 | } |
||
751 | } |
||
752 | // TODO: do something for API mode
|
||
753 | |||
754 | return 0; |
||
755 | } |
||
756 | |||
757 | 1601 | dsschult | /** TODO: since we don't use this, do we need it?
|
758 | *
|
||
759 | 1576 | dsschult | * Verifies that the packets checksum is correct.
|
760 | * (If the checksum is correct, the sum of the bytes
|
||
761 | * is 0xFF.)
|
||
762 | *
|
||
763 | * @param packet the packet received. This includes the first
|
||
764 | * three bytes, which are header information from the XBee.
|
||
765 | *
|
||
766 | * @param len The length of the packet received from the XBee
|
||
767 | *
|
||
768 | * @return 0 if the checksum is incorrect, nonzero
|
||
769 | * otherwise
|
||
770 | **/
|
||
771 | 1601 | dsschult | uint8_t xbee_verify_checksum(uint8_t* packet, uint16_t len) |
772 | 1576 | dsschult | { |
773 | 1601 | dsschult | uint8_t sum = 0;
|
774 | while(--len > 0) { |
||
775 | sum += packet[len]; |
||
776 | } |
||
777 | sum += packet[0];
|
||
778 | return (sum == 0xFF); |
||
779 | 1576 | dsschult | } |
780 | |||
781 | /**
|
||
782 | * Returns the checksum of the given packet.
|
||
783 | *
|
||
784 | * @param buf the data for the packet to send
|
||
785 | * @param len the length of the packet in bytes
|
||
786 | *
|
||
787 | * @return the checksum of the packet, which will
|
||
788 | * become the last byte sent in the packet
|
||
789 | **/
|
||
790 | 1604 | dsschult | uint8_t xbee_compute_checksum(uint8_t* buf, uint16_t len) |
791 | 1576 | dsschult | { |
792 | 1600 | dsschult | uint8_t sum = 0;
|
793 | while(--len > 0) { |
||
794 | sum += buf[len]; |
||
795 | } |
||
796 | sum += buf[0];
|
||
797 | 1576 | dsschult | return 0xFF - sum; |
798 | } |
||
799 | |||
800 | /**
|
||
801 | 1604 | dsschult | * Adds buf to the previous checksum total
|
802 | *
|
||
803 | * @param buf a byte buffer to add to the checksum
|
||
804 | * @param len the length of the buffer
|
||
805 | * @param sum the previous sum
|
||
806 | *
|
||
807 | * @return error code
|
||
808 | **/
|
||
809 | static int8_t xbee_checksum_add(uint8_t *buf, uint8_t len, uint8_t* sum) {
|
||
810 | if (buf == NULL || sum == NULL) |
||
811 | return WL_ERROR_ARGUMENT;
|
||
812 | while(--len > 0) { |
||
813 | *sum += buf[len]; |
||
814 | } |
||
815 | *sum += buf[0];
|
||
816 | } |
||
817 | |||
818 | |||
819 | /**
|
||
820 | * Sends header information. Header information includes
|
||
821 | * XBEE_FRAME_START and the packet length, as two bytes.
|
||
822 | *
|
||
823 | * @param type the packet type
|
||
824 | * @param len the size in bytes of the packet data
|
||
825 | *
|
||
826 | **/
|
||
827 | static int8_t xbee_send_header(uint8_t type, uint16_t len)
|
||
828 | { |
||
829 | //packet prefix
|
||
830 | if (xbee_putc(XBEE_FRAME_START) != WL_SUCCESS)
|
||
831 | return WL_ERROR_SEND;
|
||
832 | if (xbee_putc((uint8_t)((len & 0xFF00) >> 8)) != WL_SUCCESS) |
||
833 | return WL_ERROR_SEND;
|
||
834 | if (xbee_putc((uint8_t)(len & 0x00FF)) != WL_SUCCESS) |
||
835 | return WL_ERROR_SEND;
|
||
836 | |||
837 | if (xbee_send(buf, len) != WL_SUCCESS)
|
||
838 | return WL_ERROR_SEND;
|
||
839 | |||
840 | if (xbee_putc(checksum) != WL_SUCCESS)
|
||
841 | return WL_ERROR_SEND;
|
||
842 | |||
843 | return WL_SUCCESS;
|
||
844 | } |
||
845 | |||
846 | /**
|
||
847 | 1576 | dsschult | * Adds header information and checksum to the given
|
848 | * packet and sends it. Header information includes
|
||
849 | * XBEE_FRAME_START and the packet length, as two bytes.
|
||
850 | *
|
||
851 | * @param buf the packet data
|
||
852 | * @param len the size in bytes of the packet data
|
||
853 | *
|
||
854 | **/
|
||
855 | 1600 | dsschult | static int8_t xbee_send_frame(uint8_t* buf, uint16_t len)
|
856 | 1576 | dsschult | { |
857 | 1600 | dsschult | uint8_t checksum = xbee_compute_checksum(buf, len); |
858 | 1604 | dsschult | |
859 | //packet prefix
|
||
860 | if (xbee_putc(XBEE_FRAME_START) != WL_SUCCESS)
|
||
861 | return WL_ERROR_SEND;
|
||
862 | if (xbee_putc((uint8_t)((len & 0xFF00) >> 8)) != WL_SUCCESS) |
||
863 | return WL_ERROR_SEND;
|
||
864 | if (xbee_putc((uint8_t)(len & 0x00FF)) != WL_SUCCESS) |
||
865 | return WL_ERROR_SEND;
|
||
866 | 1576 | dsschult | |
867 | 1604 | dsschult | if (xbee_send(buf, len) != WL_SUCCESS)
|
868 | 1600 | dsschult | return WL_ERROR_SEND;
|
869 | 1604 | dsschult | |
870 | if (xbee_putc(checksum) != WL_SUCCESS)
|
||
871 | 1600 | dsschult | return WL_ERROR_SEND;
|
872 | 1604 | dsschult | |
873 | 1600 | dsschult | return WL_SUCCESS;
|
874 | 1576 | dsschult | } |
875 | |||
876 | /**
|
||
877 | * Sends an AT command to read a parameter.
|
||
878 | *
|
||
879 | * @param command the AT command to send. For exmaple,
|
||
880 | * use ID to read the PAN ID and MY to return the XBee ID.
|
||
881 | * See the XBee reference guide for a complete listing.
|
||
882 | **/
|
||
883 | 1600 | dsschult | int8_t xbee_send_read_at_command(uint8_t* command) |
884 | 1576 | dsschult | { |
885 | 1600 | dsschult | return xbee_send_modify_at_command(command, NULL, 0); |
886 | 1576 | dsschult | } |
887 | |||
888 | /**
|
||
889 | * Sends the given AT command.
|
||
890 | *
|
||
891 | * @param command the AT command to send (e.g., MY, ID)
|
||
892 | * @param value the value to pass as a parameter
|
||
893 | * (or NULL if there is no parameter)
|
||
894 | **/
|
||
895 | 1600 | dsschult | static int8_t xbee_send_modify_at_command(uint8_t* command, uint8_t* value, uint8_t len)
|
896 | 1576 | dsschult | { |
897 | 1600 | dsschult | uint8_t buf[12];
|
898 | 1576 | dsschult | |
899 | buf[0] = XBEE_FRAME_AT_COMMAND;
|
||
900 | buf[1] = 1; |
||
901 | buf[2] = command[0]; |
||
902 | buf[3] = command[1]; |
||
903 | if (value != NULL) |
||
904 | { |
||
905 | 1600 | dsschult | if (len > 8) |
906 | 1576 | dsschult | { |
907 | WL_DEBUG_PRINT("AT Command too large.\r\n");
|
||
908 | 1600 | dsschult | return WL_ERROR_ARGUMENT;
|
909 | 1576 | dsschult | } |
910 | 1600 | dsschult | memcpy(buf+4,value,len);
|
911 | 1576 | dsschult | } |
912 | |||
913 | 1600 | dsschult | return xbee_send_frame(buf, 4 + len); |
914 | 1576 | dsschult | } |
915 | |||
916 | /**
|
||
917 | * Send the specified packet.
|
||
918 | *
|
||
919 | * @param packet the packet data to send
|
||
920 | * @param len the number of bytes in the packet
|
||
921 | *
|
||
922 | * @param dest the ID of the XBee to send the packet to,
|
||
923 | * or XBEE_BROADCAST to send the message to all robots
|
||
924 | * in the PAN.
|
||
925 | *
|
||
926 | * @param options a combination of the flags
|
||
927 | * XBEE_OPTIONS_NONE, XBEE_OPTIONS_DISABLE_RESPONSE and
|
||
928 | * XBEE_OPTIONS_BROADCAST_ALL_PANS
|
||
929 | *
|
||
930 | * @param frame the frame number to associate this packet
|
||
931 | * with. This will be used to identify the response when
|
||
932 | * the XBee alerts us as to whether or not our message
|
||
933 | * was received.
|
||
934 | **/
|
||
935 | 1604 | dsschult | int8_t xbee_send_packet(uint8_t* packet, uint8_t len, uint16_t dest, uint8_t options, uint8_t frame) |
936 | 1576 | dsschult | { |
937 | 1604 | dsschult | uint8_t sum = XBEE_FRAME_TX_REQUEST_16; |
938 | uint8_t i = 0;
|
||
939 | 1576 | dsschult | |
940 | if (len > 100) |
||
941 | { |
||
942 | WL_DEBUG_PRINT("Packet is too large.\r\n");
|
||
943 | 1604 | dsschult | return WL_ERROR_ARGUMENT;
|
944 | 1576 | dsschult | } |
945 | 1604 | dsschult | |
946 | // calculate checksum
|
||
947 | for(;i<len;i++)
|
||
948 | sum += packet[len]; |
||
949 | sum += frame; |
||
950 | sum += (dest&0xFF00) >> 8; |
||
951 | sum += dest&0x00FF;
|
||
952 | sum += options; |
||
953 | sum = 0xFF - sum;
|
||
954 | 1576 | dsschult | |
955 | 1604 | dsschult | //packet prefix
|
956 | if (xbee_putc(XBEE_FRAME_START) != WL_SUCCESS)
|
||
957 | return WL_ERROR_SEND;
|
||
958 | if (xbee_putc(0x00) != WL_SUCCESS) |
||
959 | return WL_ERROR_SEND;
|
||
960 | if (xbee_putc(len+5) != WL_SUCCESS) |
||
961 | return WL_ERROR_SEND;
|
||
962 | |||
963 | //send header for TX request
|
||
964 | if (xbee_putc(XBEE_FRAME_TX_REQUEST_16) != WL_SUCCESS)
|
||
965 | return WL_ERROR_SEND;
|
||
966 | if (xbee_putc(frame) != WL_SUCCESS)
|
||
967 | return WL_ERROR_SEND;
|
||
968 | if (xbee_putc((uint8_t)((dest&0xFF00) >> 8)) != WL_SUCCESS) |
||
969 | return WL_ERROR_SEND;
|
||
970 | if (xbee_putc((uint8_t)(dest&0x00FF)) != WL_SUCCESS) |
||
971 | return WL_ERROR_SEND;
|
||
972 | if (xbee_putc(options) != WL_SUCCESS)
|
||
973 | return WL_ERROR_SEND;
|
||
974 | 1576 | dsschult | |
975 | 1604 | dsschult | // send packet
|
976 | if (xbee_send(packet, len) != WL_SUCCESS)
|
||
977 | return WL_ERROR_SEND;
|
||
978 | 1576 | dsschult | |
979 | 1604 | dsschult | // send checksum
|
980 | if (xbee_putc(sum) != WL_SUCCESS)
|
||
981 | return WL_ERROR_SEND;
|
||
982 | 1576 | dsschult | |
983 | 1604 | dsschult | return WL_SUCCESS;
|
984 | 1576 | dsschult | } |
985 | |||
986 | /**
|
||
987 | * Handles modem status packets.
|
||
988 | *
|
||
989 | * @param status the type of status packet received.
|
||
990 | **/
|
||
991 | void xbee_handle_status(char status) |
||
992 | { |
||
993 | switch (status)
|
||
994 | { |
||
995 | case 0: |
||
996 | WL_DEBUG_PRINT("XBee hardware reset.\r\n");
|
||
997 | break;
|
||
998 | case 1: |
||
999 | WL_DEBUG_PRINT("Watchdog timer reset.\r\n");
|
||
1000 | break;
|
||
1001 | case 2: |
||
1002 | WL_DEBUG_PRINT("Associated.\r\n");
|
||
1003 | break;
|
||
1004 | case 3: |
||
1005 | WL_DEBUG_PRINT("Disassociated.\r\n");
|
||
1006 | break;
|
||
1007 | case 4: |
||
1008 | WL_DEBUG_PRINT("Synchronization lost.\r\n");
|
||
1009 | break;
|
||
1010 | case 5: |
||
1011 | WL_DEBUG_PRINT("Coordinator realignment.\r\n");
|
||
1012 | break;
|
||
1013 | case 6: |
||
1014 | WL_DEBUG_PRINT("Coordinator started.\r\n");
|
||
1015 | break;
|
||
1016 | } |
||
1017 | } |
||
1018 | |||
1019 | /**
|
||
1020 | * Handles AT command response packets.
|
||
1021 | * @param command the two character AT command, e.g. MY or ID
|
||
1022 | * @param result 0 for success, 1 for an error
|
||
1023 | 1601 | dsschult | * @param len the length in bytes of extra
|
1024 | 1576 | dsschult | **/
|
1025 | 1601 | dsschult | static int8_t xbee_handle_at_command_response(uint16_t command, uint8_t result, uint8_t len)
|
1026 | 1576 | dsschult | { |
1027 | if (result == 1) |
||
1028 | { |
||
1029 | WL_DEBUG_PRINT("Error with AT");
|
||
1030 | WL_DEBUG_PRINT(command); |
||
1031 | WL_DEBUG_PRINT(" packet. Result = ");
|
||
1032 | switch(result) {
|
||
1033 | case 1: |
||
1034 | WL_DEBUG_PRINT("ERROR\r\n");
|
||
1035 | break;
|
||
1036 | case 2: |
||
1037 | WL_DEBUG_PRINT("Invalid Command\r\n");
|
||
1038 | break;
|
||
1039 | case 3: |
||
1040 | WL_DEBUG_PRINT("Invalid Parameter\r\n");
|
||
1041 | break;
|
||
1042 | } |
||
1043 | 1601 | dsschult | return WL_SUCCESS;
|
1044 | 1576 | dsschult | } |
1045 | WL_DEBUG_PRINT("AT");
|
||
1046 | WL_DEBUG_PRINT(command); |
||
1047 | WL_DEBUG_PRINT(" command was successful.\r\n");
|
||
1048 | |||
1049 | 1601 | dsschult | // TODO: program more command responses here (ND, etc)
|
1050 | switch(command) {
|
||
1051 | case ('I'<<8)+'D': // PAN |
||
1052 | case ('C'<<8)+'H': // channel |
||
1053 | case ('M'<<8)+'Y': // address |
||
1054 | // copy command to handler
|
||
1055 | xbee_command[0] = (command&0xFF00)>>8; |
||
1056 | xbee_command[1] = command&0x00FF; |
||
1057 | result = basic_buf_last; |
||
1058 | for(command=2;command<len+2;command++) |
||
1059 | xbee_command[command] = xbee_basic_buf_get(&result); |
||
1060 | break;
|
||
1061 | default:
|
||
1062 | WL_DEBUG_PRINT("unknown AT command");
|
||
1063 | } |
||
1064 | 1576 | dsschult | |
1065 | 1601 | dsschult | // signal handler that command response is done
|
1066 | xbee_status = (xbee_status&0x3F)|XBEE_COMMAND_RESPONSE;
|
||
1067 | 1576 | dsschult | |
1068 | 1601 | dsschult | return WL_SUCCESS;
|
1069 | 1576 | dsschult | } |
1070 | |||
1071 | /**
|
||
1072 | * Sets the personal area network id.
|
||
1073 | *
|
||
1074 | * @param id the new personal area network (PAN) id
|
||
1075 | **/
|
||
1076 | 1600 | dsschult | int8_t xbee_set_pan_id(uint16_t id) |
1077 | 1576 | dsschult | { |
1078 | 1600 | dsschult | if (xbee_status&0xC0 == XBEE_COMMAND_WAIT |
1079 | || xbee_status&0xC0 == XBEE_COMMAND_RESPONSE)
|
||
1080 | return WL_ERROR_XBEE_COMMAND; // can't do command right now |
||
1081 | |||
1082 | int16_t i=0;
|
||
1083 | // change status to command wait
|
||
1084 | xbee_status = (xbee_status&0x3F)|XBEE_COMMAND_WAIT;
|
||
1085 | xbee_send_modify_at_command("ID",id); // send command to set the channel |
||
1086 | // wait for up to 30 ms
|
||
1087 | while(xbee_status&0xC0 != XBEE_COMMAND_RESPONSE && i++ < 10000) { |
||
1088 | delay_us(1); // wait 3us |
||
1089 | } |
||
1090 | if (i < 1000 && xbee_command[0] == 'O' && xbee_command[1] == 'K') { |
||
1091 | i = WL_SUCCESS; |
||
1092 | else
|
||
1093 | i = WL_ERROR_XBEE_COMMAND; // set error code
|
||
1094 | xbee_status = xbee_status&0x3F; // reset status |
||
1095 | return (int8_t)i; // return |
||
1096 | 1576 | dsschult | } |
1097 | |||
1098 | /**
|
||
1099 | * Get the PAN ID for the XBee.
|
||
1100 | *
|
||
1101 | * @return the personal area network id, or
|
||
1102 | * XBEE_PAN_DEFAULT if it has not yet been set.
|
||
1103 | **/
|
||
1104 | 1600 | dsschult | uint16_t xbee_get_pan_id() |
1105 | 1576 | dsschult | { |
1106 | 1600 | dsschult | if (xbee_status&0xC0 == XBEE_COMMAND_WAIT |
1107 | || xbee_status&0xC0 == XBEE_COMMAND_RESPONSE)
|
||
1108 | return WL_ERROR_XBEE_COMMAND_16BIT; // can't do command right now |
||
1109 | |||
1110 | uint16_t i=0;
|
||
1111 | // change status to command wait
|
||
1112 | xbee_status = (xbee_status&0x3F)|XBEE_COMMAND_WAIT;
|
||
1113 | xbee_send_read_at_command("ID"); // send command to get the PAN |
||
1114 | // wait for up to 30 ms
|
||
1115 | while(xbee_status&0xC0 != XBEE_COMMAND_RESPONSE && i++ < 10000) { |
||
1116 | delay_us(1); // wait 3us |
||
1117 | } |
||
1118 | if (i < 1000 && xbee_command[0] == 'I' && xbee_command[1] == 'D') { |
||
1119 | i = (xbee_command[2]<<8)|xbee_command[3]; // get PAN |
||
1120 | else
|
||
1121 | i = WL_ERROR_XBEE_COMMAND_16BIT; // set error code
|
||
1122 | xbee_status = xbee_status&0x3F; // reset status |
||
1123 | return i; // return |
||
1124 | 1576 | dsschult | } |
1125 | |||
1126 | /**
|
||
1127 | * Set the channel the XBee is using.
|
||
1128 | *
|
||
1129 | * @param channel the channel the XBee will not use,
|
||
1130 | * between 0x0B and 0x1A
|
||
1131 | *
|
||
1132 | * @see xbee_get_channel
|
||
1133 | **/
|
||
1134 | 1600 | dsschult | int8_t xbee_set_channel(uint8_t channel) |
1135 | 1576 | dsschult | { |
1136 | if (channel < 0x0B || channel > 0x1A) |
||
1137 | { |
||
1138 | WL_DEBUG_PRINT("Channel out of range.\r\n");
|
||
1139 | return -1; |
||
1140 | } |
||
1141 | |||
1142 | 1600 | dsschult | if (xbee_status&0xC0 == XBEE_COMMAND_WAIT |
1143 | || xbee_status&0xC0 == XBEE_COMMAND_RESPONSE)
|
||
1144 | return WL_ERROR_XBEE_COMMAND; // can't do command right now |
||
1145 | |||
1146 | int16_t i=0;
|
||
1147 | // change status to command wait
|
||
1148 | xbee_status = (xbee_status&0x3F)|XBEE_COMMAND_WAIT;
|
||
1149 | xbee_send_modify_at_command("CH",channel); // send command to set the channel |
||
1150 | // wait for up to 30 ms
|
||
1151 | while(xbee_status&0xC0 != XBEE_COMMAND_RESPONSE && i++ < 10000) { |
||
1152 | delay_us(1); // wait 3us |
||
1153 | } |
||
1154 | if (i < 1000 && xbee_command[0] == 'O' && xbee_command[1] == 'K') { |
||
1155 | i = WL_SUCCESS; |
||
1156 | else
|
||
1157 | i = WL_ERROR_XBEE_COMMAND; // set error code
|
||
1158 | xbee_status = xbee_status&0x3F; // reset status |
||
1159 | return (int8_t)i; // return |
||
1160 | 1576 | dsschult | } |
1161 | |||
1162 | /**
|
||
1163 | * Returns the channel which the XBee is currently using.
|
||
1164 | *
|
||
1165 | * @return the channel the XBee is using
|
||
1166 | *
|
||
1167 | * @see xbee_set_channel
|
||
1168 | **/
|
||
1169 | 1600 | dsschult | int8_t xbee_get_channel(void)
|
1170 | 1576 | dsschult | { |
1171 | 1600 | dsschult | if (xbee_status&0xC0 == XBEE_COMMAND_WAIT |
1172 | || xbee_status&0xC0 == XBEE_COMMAND_RESPONSE)
|
||
1173 | return WL_ERROR_XBEE_COMMAND; // can't do command right now |
||
1174 | |||
1175 | int16_t i=0;
|
||
1176 | // change status to command wait
|
||
1177 | xbee_status = (xbee_status&0x3F)|XBEE_COMMAND_WAIT;
|
||
1178 | xbee_send_read_at_command("ID"); // send command to get the channel |
||
1179 | // wait for up to 30 ms
|
||
1180 | while(xbee_status&0xC0 != XBEE_COMMAND_RESPONSE && i++ < 10000) { |
||
1181 | delay_us(1); // wait 3us |
||
1182 | } |
||
1183 | if (i < 1000 && xbee_command[0] == 'C' && xbee_command[1] == 'H') { |
||
1184 | i = xbee_command[2]; // get channel |
||
1185 | else
|
||
1186 | i = WL_ERROR_XBEE_COMMAND; // set error code
|
||
1187 | xbee_status = xbee_status&0x3F; // reset status |
||
1188 | return i; // return |
||
1189 | 1576 | dsschult | } |
1190 | |||
1191 | /**
|
||
1192 | * Get the 16-bit address of the XBee.
|
||
1193 | * This is used to specify who to send messages to
|
||
1194 | * and who messages are from.
|
||
1195 | *
|
||
1196 | * @return the 16-bit address of the XBee.
|
||
1197 | **/
|
||
1198 | 1601 | dsschult | uint16_t xbee_get_address(void)
|
1199 | 1576 | dsschult | { |
1200 | 1600 | dsschult | if (xbee_status&0xC0 == XBEE_COMMAND_WAIT |
1201 | || xbee_status&0xC0 == XBEE_COMMAND_RESPONSE)
|
||
1202 | return WL_ERROR_XBEE_COMMAND_16BIT; // can't do command right now |
||
1203 | |||
1204 | uint16_t i=0;
|
||
1205 | // change status to command wait
|
||
1206 | xbee_status = (xbee_status&0x3F)|XBEE_COMMAND_WAIT;
|
||
1207 | xbee_send_read_at_command("MY"); // send command to get the address |
||
1208 | // wait for up to 30 ms
|
||
1209 | while(xbee_status&0xC0 != XBEE_COMMAND_RESPONSE && i++ < 10000) { |
||
1210 | delay_us(1); // wait 3us |
||
1211 | } |
||
1212 | if (i < 1000 && xbee_command[0] == 'M' && xbee_command[1] == 'Y') { |
||
1213 | i = (xbee_command[2]<<8)+xbee_command[3]; // get address |
||
1214 | else
|
||
1215 | i = WL_ERROR_XBEE_COMMAND_16BIT; // set error code
|
||
1216 | xbee_status = xbee_status&0x3F; // reset status |
||
1217 | return i; // return |
||
1218 | 1576 | dsschult | } |
1219 | |||
1220 | 1581 | dsschult | /**@} **/ // end xbee group |