Revision 1624
wl: update to unit tests, still doesn't work
xbee.c | ||
---|---|---|
132 | 132 |
* Interrupt for the robot. Adds bytes received from the xbee |
133 | 133 |
* to the buffer. |
134 | 134 |
**/ |
135 |
|
|
135 | 136 |
#ifndef FIREFLY |
136 |
#define PORT UDR1 |
|
137 |
#define FLAG RXC1 |
|
138 | 137 |
ISR(USART1_RX_vect) |
138 |
{ |
|
139 |
WL_DEBUG_PRINT("\nOut of space in buffer.\n"); |
|
140 |
/*char c = UDR1; |
|
141 |
arrival_buf[buffer_last] = c; |
|
142 |
int t = buffer_last + 1; |
|
143 |
if (t == XBEE_BUFFER_SIZE) |
|
144 |
t = 0; |
|
145 |
if (t == buffer_first) |
|
146 |
{ |
|
147 |
|
|
148 |
} |
|
149 |
buffer_last = t;*/ |
|
150 |
} |
|
139 | 151 |
#else |
152 |
SIGNAL(SIG_USART0_RECV) |
|
153 |
{ |
|
154 |
WL_DEBUG_PRINT("Out of space in buffer.\n"); |
|
155 |
/*char c = UDR0; |
|
156 |
arrival_buf[buffer_last] = c; |
|
157 |
int t = buffer_last + 1; |
|
158 |
if (t == XBEE_BUFFER_SIZE) |
|
159 |
t = 0; |
|
160 |
if (t == buffer_first) |
|
161 |
{ |
|
162 |
} |
|
163 |
buffer_last = t;*/ |
|
164 |
} |
|
165 |
#endif |
|
166 |
|
|
167 |
|
|
168 |
#include <lights.h> |
|
169 |
//#ifndef FIREFLY |
|
170 |
//#define PORT UDR1 |
|
171 |
//#define FLAG RXC1 |
|
172 |
//ISR(USART1_RX_vect) |
|
173 |
//#else |
|
140 | 174 |
#define PORT UDR0 |
141 | 175 |
#define FLAG RXC0 |
142 |
SIGNAL(SIG_USART0_RECV) |
|
143 |
#endif |
|
144 |
{ |
|
176 |
/*SIGNAL(SIG_USART0_RECV) |
|
177 |
{//#endif |
|
145 | 178 |
// start of frame |
146 |
uint8_t apitype = PORT; // get frame start byte
|
|
179 |
uint8_t apitype = PORT; // get frame start byte
|
|
147 | 180 |
uint16_t i=0; |
148 | 181 |
uint16_t len=0; |
149 | 182 |
|
183 |
orb_set_color(RED); |
|
184 |
|
|
185 |
usb_puts("xbee_interrupt"); |
|
186 |
|
|
187 |
WL_DEBUG_PRINT("in interrupt|status="); |
|
188 |
WL_DEBUG_PRINT_HEX(xbee_status); |
|
189 |
|
|
150 | 190 |
// check that we're in API mode |
151 | 191 |
if (getStatus(XBEE_API_MASK) == XBEE_API_OFF || apitype != XBEE_FRAME_START) { |
152 | 192 |
// not in API mode |
193 |
WL_DEBUG_PRINT("|API OFF"); |
|
153 | 194 |
if (getStatus(XBEE_COMMAND_MASK) == XBEE_COMMAND_WAIT) { |
154 | 195 |
// get rest of command and put in basic buf |
155 | 196 |
xbee_basic_buf[0] = apitype; |
... | ... | |
162 | 203 |
} |
163 | 204 |
} |
164 | 205 |
} |
206 |
WL_DEBUG_PRINT("got packet="); |
|
207 |
WL_DEBUG_PRINT(xbee_basic_buf); |
|
165 | 208 |
// signal handler that command response is done |
166 | 209 |
setStatus(XBEE_COMMAND_MASK,XBEE_COMMAND_RESPONSE); |
167 | 210 |
} |
211 |
WL_DEBUG_PRINT("return\r\n"); |
|
168 | 212 |
return; |
169 |
} |
|
213 |
} |
|
214 |
WL_DEBUG_PRINT("|API ON"); |
|
170 | 215 |
|
171 | 216 |
// get length and type |
172 | 217 |
while(i<3) { |
... | ... | |
180 | 225 |
i++; |
181 | 226 |
} |
182 | 227 |
} |
228 |
WL_DEBUG_PRINT("|len="); |
|
229 |
WL_DEBUG_PRINT_INT(len); |
|
230 |
WL_DEBUG_PRINT("|apitype="); |
|
231 |
WL_DEBUG_PRINT_HEX(apitype); |
|
232 |
WL_DEBUG_PRINT("\r\n"); |
|
183 | 233 |
|
184 | 234 |
// do something based on the type |
185 | 235 |
i=1; |
... | ... | |
311 | 361 |
break; |
312 | 362 |
} |
313 | 363 |
} |
314 |
} // end of interrupt |
|
364 |
WL_DEBUG_PRINT("return\r\n"); |
|
365 |
} // end of interrupt |
|
366 |
*/ |
|
315 | 367 |
|
316 |
|
|
317 | 368 |
/* adds a byte to the basic buffer */ |
318 | 369 |
int8_t xbee_basic_buf_add(uint8_t *ptr, uint8_t byte) { |
319 | 370 |
if (*ptr == basic_buf_first) { |
... | ... | |
375 | 426 |
**/ |
376 | 427 |
int8_t xbee_init() |
377 | 428 |
{ |
378 |
WL_DEBUG_PRINT("in xbee_init\n");
|
|
429 |
WL_DEBUG_PRINT("in xbee_init\n");
|
|
379 | 430 |
|
380 | 431 |
if(getStatus(XBEE_NOT_INITD) != XBEE_NOT_INITD) { |
381 | 432 |
return WL_ERROR_INIT_ALREADY_INITD; |
... | ... | |
386 | 437 |
lastPacket[i].source = lastPacket[i].framenum = 0; |
387 | 438 |
|
388 | 439 |
// Set startup baud rate of 9600 |
389 |
// Set frame format: 8data, 1stop bit, asynchronous normal mode
|
|
440 |
// Set frame format: 8data, 1stop bit, asynchronous normal mode
|
|
390 | 441 |
// Enable receiver and transmitter and the receiving interrupt |
391 | 442 |
#ifdef FIREFLY |
392 | 443 |
UCSR0A |= (1<<U2X0); |
393 | 444 |
UBRR0H = 0x00; |
394 | 445 |
UBRR0L = 103; |
395 | 446 |
UCSR0C |= (1<<UCSZ00) | (1<<UCSZ01); |
396 |
UCSR0B |= (1<<RXEN0) | (1<<TXEN0) | (1<<RXCIE0);
|
|
447 |
UCSR0B |= (1<<RXEN0) | (1<<TXEN0) | (1<<RXCIE0);
|
|
397 | 448 |
#else |
398 | 449 |
// Bayboard or robot |
399 | 450 |
UCSR1A |= (1<<U2X1); |
400 | 451 |
UBRR1H = 0x00; |
401 | 452 |
UBRR1L = 103; |
402 | 453 |
UCSR1C |= (1<<UCSZ10) | (1<<UCSZ11); |
403 |
UCSR1B |= (1<<RXEN1) | (1<<TXEN1) | (1<<RXCIE1);
|
|
454 |
UCSR1B |= (1<<RXEN1) | (1<<TXEN1) | (1<<RXCIE1);
|
|
404 | 455 |
#endif |
405 |
sei();
|
|
456 |
sei();
|
|
406 | 457 |
|
407 |
WL_DEBUG_PRINT("Entering command mode.\r\n");
|
|
408 |
if (xbee_enter_command_mode() != 0) {
|
|
458 |
WL_DEBUG_PRINT("Entering command mode.\r\n");
|
|
459 |
if (xbee_enter_command_mode() != 0) {
|
|
409 | 460 |
WL_DEBUG_PRINT("error entering command mode\r\n"); |
410 |
return -1;
|
|
411 |
}
|
|
412 |
WL_DEBUG_PRINT("Entered command mode.\r\n");
|
|
461 |
return -1;
|
|
462 |
}
|
|
463 |
WL_DEBUG_PRINT("Entered command mode.\r\n");
|
|
413 | 464 |
|
414 |
// reset baud rate |
|
415 |
WL_DEBUG_PRINT("Resetting Baud to "); |
|
416 |
WL_DEBUG_PRINT(XBEE_BAUD_STR); |
|
417 |
WL_DEBUG_PRINT("\r\n"); |
|
418 | 465 |
|
419 | 466 |
// set baud on xbee |
420 | 467 |
#if (XBEE_BAUD == 115200) |
... | ... | |
494 | 541 |
|
495 | 542 |
if (xbee_enter_api_mode() != 0) { |
496 | 543 |
WL_DEBUG_PRINT("can't enter api mode\r\n"); |
497 |
return -1;
|
|
498 |
}
|
|
544 |
return -1;
|
|
545 |
}
|
|
499 | 546 |
|
500 |
WL_DEBUG_PRINT("Entered api mode.\r\n");
|
|
547 |
WL_DEBUG_PRINT("Entered api mode.\r\n");
|
|
501 | 548 |
|
502 |
if (xbee_exit_command_mode() != 0) {
|
|
549 |
if (xbee_exit_command_mode() != 0) {
|
|
503 | 550 |
WL_DEBUG_PRINT("can't exit command mode\r\n"); |
504 |
return -1; |
|
505 |
} |
|
506 |
|
|
507 |
WL_DEBUG_PRINT("Left command mode.\r\n"); |
|
551 |
return -1; |
|
552 |
} |
|
508 | 553 |
|
554 |
WL_DEBUG_PRINT("Left command mode.\r\n"); |
|
555 |
|
|
509 | 556 |
// TODO: we should set the MY address to the robot address from eeprom |
510 | 557 |
|
511 | 558 |
|
512 | 559 |
// set status |
513 | 560 |
setStatus(XBEE_NOT_INITD,XBEE_COMMAND_NONE); |
514 | 561 |
|
515 |
return WL_SUCCESS;
|
|
562 |
return WL_SUCCESS;
|
|
516 | 563 |
} |
517 | 564 |
|
518 | 565 |
/** |
... | ... | |
520 | 567 |
**/ |
521 | 568 |
int8_t xbee_terminate() |
522 | 569 |
{ |
523 |
if (xbee_exit_api_mode() != WL_SUCCESS) { |
|
570 |
int ret=xbee_exit_api_mode(); |
|
571 |
if (ret != WL_SUCCESS) { |
|
524 | 572 |
WL_DEBUG_PRINT("xbee termination failed\r\n"); |
573 |
WL_DEBUG_PRINT_INT(ret); |
|
574 |
WL_DEBUG_PRINT("|\r\n"); |
|
525 | 575 |
return WL_ERROR_TERMINATION_FAILED; |
526 | 576 |
} |
527 | 577 |
setStatus(XBEE_NOT_INITD,XBEE_NOT_INITD); // clean initd status |
... | ... | |
545 | 595 |
loop_until_bit_is_set(UCSR1A, UDRE1); |
546 | 596 |
UDR1 = c; |
547 | 597 |
#endif |
548 |
|
|
598 |
|
|
549 | 599 |
return WL_SUCCESS; |
550 | 600 |
} |
551 | 601 |
|
... | ... | |
607 | 657 |
int8_t xbee_send(uint8_t* buf, uint16_t size) |
608 | 658 |
{ |
609 | 659 |
uint16_t i=0; // check if we need this variable |
610 |
while(i<size) {
|
|
611 |
if (xbee_putc(buf[i++]) != WL_SUCCESS)
|
|
660 |
while(i<size) {
|
|
661 |
if (xbee_putc(buf[i++]) != WL_SUCCESS)
|
|
612 | 662 |
return WL_ERROR_SEND; |
613 |
}
|
|
663 |
}
|
|
614 | 664 |
if (xbee_putc(buf[0]) != WL_SUCCESS) |
615 | 665 |
return WL_ERROR_SEND; |
616 | 666 |
|
617 |
return WL_SUCCESS;
|
|
667 |
return WL_SUCCESS;
|
|
618 | 668 |
} |
619 | 669 |
|
620 | 670 |
/** |
... | ... | |
624 | 674 |
**/ |
625 | 675 |
static int8_t xbee_send_string(uint8_t* c) |
626 | 676 |
{ |
627 |
return xbee_send(c, strlen((char*)c));
|
|
677 |
return xbee_send(c, strlen((char*)c));
|
|
628 | 678 |
} |
629 | 679 |
|
630 | 680 |
|
... | ... | |
633 | 683 |
**/ |
634 | 684 |
static int8_t xbee_enter_command_mode(void) |
635 | 685 |
{ |
636 |
if (xbee_send_string((uint8_t*)"+++") != WL_SUCCESS) {
|
|
637 |
return WL_ERROR_XBEE_COMMAND;
|
|
638 |
}
|
|
686 |
if (xbee_send_string((uint8_t*)"+++") != WL_SUCCESS) {
|
|
687 |
return WL_ERROR_XBEE_COMMAND;
|
|
688 |
}
|
|
639 | 689 |
|
640 |
if (xbee_wait_for_ok() != WL_SUCCESS) {
|
|
641 |
return WL_ERROR_XBEE_COMMAND;
|
|
642 |
}
|
|
690 |
if (xbee_wait_for_ok() != WL_SUCCESS) {
|
|
691 |
return WL_ERROR_XBEE_COMMAND;
|
|
692 |
}
|
|
643 | 693 |
|
644 |
return WL_SUCCESS;
|
|
694 |
return WL_SUCCESS;
|
|
645 | 695 |
} |
646 | 696 |
|
647 | 697 |
/** |
... | ... | |
649 | 699 |
**/ |
650 | 700 |
static int8_t xbee_exit_command_mode() |
651 | 701 |
{ |
652 |
if (xbee_send_string((uint8_t*)"ATCN\r") != 0) {
|
|
653 |
return WL_ERROR_SEND;
|
|
654 |
}
|
|
655 |
xbee_wait_for_ok();
|
|
702 |
if (xbee_send_string((uint8_t*)"ATCN\r") != 0) {
|
|
703 |
return WL_ERROR_SEND;
|
|
704 |
}
|
|
705 |
xbee_wait_for_ok();
|
|
656 | 706 |
|
657 |
return WL_SUCCESS;
|
|
707 |
return WL_SUCCESS;
|
|
658 | 708 |
} |
659 | 709 |
|
660 | 710 |
/** |
661 | 711 |
* Enter API mode. |
662 | 712 |
**/ |
663 | 713 |
static int8_t xbee_enter_api_mode(void) { |
664 |
if (xbee_send_string((uint8_t*)"ATAP 1\r") != 0) {
|
|
665 |
return WL_ERROR_SEND;
|
|
666 |
}
|
|
667 |
xbee_wait_for_ok();
|
|
714 |
if (xbee_send_string((uint8_t*)"ATAP 1\r") != 0) {
|
|
715 |
return WL_ERROR_SEND;
|
|
716 |
}
|
|
717 |
xbee_wait_for_ok();
|
|
668 | 718 |
|
669 | 719 |
setStatus(XBEE_API_MASK,XBEE_API_ON); // set status |
670 | 720 |
|
671 |
return WL_SUCCESS;
|
|
721 |
return WL_SUCCESS;
|
|
672 | 722 |
} |
673 | 723 |
|
674 | 724 |
/** |
675 | 725 |
* Enter API mode 2. |
676 | 726 |
**/ |
677 | 727 |
static int8_t xbee_enter_api_mode2(void) { |
678 |
if (xbee_send_string((uint8_t*)"ATAP 2\r") != 0) {
|
|
679 |
return WL_ERROR_SEND;
|
|
680 |
}
|
|
681 |
xbee_wait_for_ok();
|
|
728 |
if (xbee_send_string((uint8_t*)"ATAP 2\r") != 0) {
|
|
729 |
return WL_ERROR_SEND;
|
|
730 |
}
|
|
731 |
xbee_wait_for_ok();
|
|
682 | 732 |
|
683 | 733 |
setStatus(XBEE_API_MASK,XBEE_API_ESCAPE); // set status |
684 | 734 |
|
685 |
return WL_SUCCESS;
|
|
735 |
return WL_SUCCESS;
|
|
686 | 736 |
} |
687 | 737 |
|
688 | 738 |
/** |
... | ... | |
690 | 740 |
**/ |
691 | 741 |
static int8_t xbee_exit_api_mode() |
692 | 742 |
{ |
693 |
if (getStatus(XBEE_COMMAND_MASK) == XBEE_COMMAND_WAIT
|
|
743 |
if (getStatus(XBEE_COMMAND_MASK) == XBEE_COMMAND_WAIT
|
|
694 | 744 |
|| getStatus(XBEE_COMMAND_MASK) == XBEE_COMMAND_RESPONSE) |
695 | 745 |
return WL_ERROR_XBEE_COMMAND; // can't do command right now |
696 | 746 |
|
... | ... | |
707 | 757 |
else |
708 | 758 |
i = WL_ERROR_XBEE_COMMAND; // set error code |
709 | 759 |
setStatus(XBEE_API_MASK,XBEE_API_OFF); // reset status |
710 |
return (int8_t)i; // return
|
|
760 |
return (int8_t)i; // return
|
|
711 | 761 |
} |
712 | 762 |
|
713 | 763 |
/** |
... | ... | |
715 | 765 |
**/ |
716 | 766 |
static int8_t xbee_wait_for_ok() |
717 | 767 |
{ |
718 |
return xbee_wait_for_string((uint8_t*)"OK\r", 3);
|
|
768 |
return xbee_wait_for_string((uint8_t*)"OK\r", 3);
|
|
719 | 769 |
} |
720 | 770 |
|
721 | 771 |
/** |
... | ... | |
747 | 797 |
setStatus(XBEE_COMMAND_MASK,XBEE_COMMAND_NONE); // clear status |
748 | 798 |
} |
749 | 799 |
|
750 |
return 0;
|
|
800 |
return 0;
|
|
751 | 801 |
} |
752 | 802 |
|
753 | 803 |
/** |
... | ... | |
801 | 851 |
**/ |
802 | 852 |
int8_t xbee_verify_checksum(uint8_t* packet, uint16_t len) |
803 | 853 |
{ |
804 |
uint8_t sum = 0;
|
|
805 |
while(--len > 0) {
|
|
806 |
sum += packet[len];
|
|
854 |
uint8_t sum = 0;
|
|
855 |
while(--len > 0) {
|
|
856 |
sum += packet[len];
|
|
807 | 857 |
} |
808 | 858 |
sum += packet[0]; |
809 |
return (sum == 0xFF);
|
|
859 |
return (sum == 0xFF);
|
|
810 | 860 |
} |
811 | 861 |
|
812 | 862 |
/** |
... | ... | |
820 | 870 |
**/ |
821 | 871 |
uint8_t xbee_compute_checksum(uint8_t* buf, uint16_t len) |
822 | 872 |
{ |
823 |
uint8_t sum = 0;
|
|
824 |
while(--len > 0) {
|
|
825 |
sum += buf[len];
|
|
873 |
uint8_t sum = 0;
|
|
874 |
while(--len > 0) {
|
|
875 |
sum += buf[len];
|
|
826 | 876 |
} |
827 | 877 |
sum += buf[0]; |
828 |
return 0xFF - sum;
|
|
878 |
return 0xFF - sum;
|
|
829 | 879 |
} |
830 | 880 |
|
831 | 881 |
/** |
... | ... | |
841 | 891 |
if (buf == NULL || sum == NULL) |
842 | 892 |
return WL_ERROR_ARGUMENT; |
843 | 893 |
while(--len > 0) { |
844 |
*sum += buf[len];
|
|
894 |
*sum += buf[len];
|
|
845 | 895 |
} |
846 | 896 |
*sum += buf[0]; |
847 | 897 |
return WL_SUCCESS; |
... | ... | |
866 | 916 |
if (xbee_putc((uint8_t)(len & 0x00FF)) != WL_SUCCESS) |
867 | 917 |
return WL_ERROR_SEND; |
868 | 918 |
|
869 |
return WL_SUCCESS;
|
|
919 |
return WL_SUCCESS;
|
|
870 | 920 |
} |
871 | 921 |
|
872 | 922 |
/** |
... | ... | |
880 | 930 |
**/ |
881 | 931 |
static int8_t xbee_send_frame(uint8_t* buf, uint16_t len) |
882 | 932 |
{ |
883 |
uint8_t checksum = xbee_compute_checksum(buf, len);
|
|
933 |
uint8_t checksum = xbee_compute_checksum(buf, len);
|
|
884 | 934 |
|
885 | 935 |
//packet prefix |
886 | 936 |
if (xbee_putc(XBEE_FRAME_START) != WL_SUCCESS) |
... | ... | |
890 | 940 |
if (xbee_putc((uint8_t)(len & 0x00FF)) != WL_SUCCESS) |
891 | 941 |
return WL_ERROR_SEND; |
892 | 942 |
|
893 |
if (xbee_send(buf, len) != WL_SUCCESS)
|
|
894 |
return WL_ERROR_SEND;
|
|
895 |
|
|
896 |
if (xbee_putc(checksum) != WL_SUCCESS)
|
|
897 |
return WL_ERROR_SEND;
|
|
898 |
|
|
899 |
return WL_SUCCESS;
|
|
943 |
if (xbee_send(buf, len) != WL_SUCCESS)
|
|
944 |
return WL_ERROR_SEND;
|
|
945 |
|
|
946 |
if (xbee_putc(checksum) != WL_SUCCESS)
|
|
947 |
return WL_ERROR_SEND;
|
|
948 |
|
|
949 |
return WL_SUCCESS;
|
|
900 | 950 |
} |
901 | 951 |
|
902 | 952 |
/** |
... | ... | |
908 | 958 |
**/ |
909 | 959 |
int8_t xbee_send_read_at_command(uint8_t* command) |
910 | 960 |
{ |
911 |
return xbee_send_modify_at_command(command, NULL, 0);
|
|
961 |
return xbee_send_modify_at_command(command, NULL, 0);
|
|
912 | 962 |
} |
913 | 963 |
|
914 | 964 |
/** |
... | ... | |
920 | 970 |
**/ |
921 | 971 |
static int8_t xbee_send_modify_at_command(uint8_t* command, uint8_t* value, uint8_t len) |
922 | 972 |
{ |
923 |
uint8_t buf[12];
|
|
973 |
uint8_t buf[12];
|
|
924 | 974 |
|
925 |
buf[0] = XBEE_FRAME_AT_COMMAND;
|
|
926 |
buf[1] = 1;
|
|
927 |
buf[2] = command[0];
|
|
928 |
buf[3] = command[1];
|
|
929 |
if (value != NULL)
|
|
930 |
{
|
|
931 |
if (len > 8)
|
|
932 |
{
|
|
933 |
WL_DEBUG_PRINT("AT Command too large.\r\n");
|
|
934 |
return WL_ERROR_ARGUMENT;
|
|
935 |
}
|
|
975 |
buf[0] = XBEE_FRAME_AT_COMMAND;
|
|
976 |
buf[1] = 1;
|
|
977 |
buf[2] = command[0];
|
|
978 |
buf[3] = command[1];
|
|
979 |
if (value != NULL)
|
|
980 |
{
|
|
981 |
if (len > 8)
|
|
982 |
{
|
|
983 |
WL_DEBUG_PRINT("AT Command too large.\r\n");
|
|
984 |
return WL_ERROR_ARGUMENT;
|
|
985 |
}
|
|
936 | 986 |
memcpy(buf+4,value,len); |
937 |
}
|
|
987 |
}
|
|
938 | 988 |
|
939 |
return xbee_send_frame(buf, 4 + len);
|
|
989 |
return xbee_send_frame(buf, 4 + len);
|
|
940 | 990 |
} |
941 | 991 |
|
942 | 992 |
/** |
... | ... | |
960 | 1010 |
**/ |
961 | 1011 |
int8_t xbee_send_packet(uint8_t* packet, uint8_t len, uint16_t dest, uint8_t options, uint8_t frame) |
962 | 1012 |
{ |
963 |
uint8_t sum = XBEE_FRAME_TX_REQUEST_16;
|
|
1013 |
uint8_t sum = XBEE_FRAME_TX_REQUEST_16;
|
|
964 | 1014 |
uint8_t i = 0; |
965 | 1015 |
|
966 |
if (len > 100)
|
|
967 |
{
|
|
968 |
WL_DEBUG_PRINT("Packet is too large.\r\n");
|
|
969 |
return WL_ERROR_ARGUMENT;
|
|
970 |
}
|
|
1016 |
if (len > 100)
|
|
1017 |
{
|
|
1018 |
WL_DEBUG_PRINT("Packet is too large.\r\n");
|
|
1019 |
return WL_ERROR_ARGUMENT;
|
|
1020 |
}
|
|
971 | 1021 |
|
972 | 1022 |
// calculate checksum |
973 |
for(;i<len;i++)
|
|
974 |
sum += packet[len];
|
|
1023 |
for(;i<len;i++)
|
|
1024 |
sum += packet[len];
|
|
975 | 1025 |
sum += frame; |
976 | 1026 |
sum += (dest&0xFF00) >> 8; |
977 | 1027 |
sum += dest&0x00FF; |
978 | 1028 |
sum += options; |
979 |
sum = 0xFF - sum;
|
|
1029 |
sum = 0xFF - sum;
|
|
980 | 1030 |
|
981 |
//packet prefix
|
|
1031 |
//packet prefix
|
|
982 | 1032 |
if (xbee_putc(XBEE_FRAME_START) != WL_SUCCESS) |
983 | 1033 |
return WL_ERROR_SEND; |
984 | 1034 |
if (xbee_putc(0x00) != WL_SUCCESS) |
... | ... | |
986 | 1036 |
if (xbee_putc(len+5) != WL_SUCCESS) |
987 | 1037 |
return WL_ERROR_SEND; |
988 | 1038 |
|
989 |
//send header for TX request
|
|
1039 |
//send header for TX request
|
|
990 | 1040 |
if (xbee_putc(XBEE_FRAME_TX_REQUEST_16) != WL_SUCCESS) |
991 | 1041 |
return WL_ERROR_SEND; |
992 | 1042 |
if (xbee_putc(frame) != WL_SUCCESS) |
... | ... | |
999 | 1049 |
return WL_ERROR_SEND; |
1000 | 1050 |
|
1001 | 1051 |
// send packet |
1002 |
if (xbee_send(packet, len) != WL_SUCCESS)
|
|
1003 |
return WL_ERROR_SEND;
|
|
1052 |
if (xbee_send(packet, len) != WL_SUCCESS)
|
|
1053 |
return WL_ERROR_SEND;
|
|
1004 | 1054 |
|
1005 | 1055 |
// send checksum |
1006 |
if (xbee_putc(sum) != WL_SUCCESS)
|
|
1007 |
return WL_ERROR_SEND;
|
|
1056 |
if (xbee_putc(sum) != WL_SUCCESS)
|
|
1057 |
return WL_ERROR_SEND;
|
|
1008 | 1058 |
|
1009 |
return WL_SUCCESS;
|
|
1059 |
return WL_SUCCESS;
|
|
1010 | 1060 |
} |
1011 | 1061 |
|
1012 | 1062 |
/** |
... | ... | |
1016 | 1066 |
**/ |
1017 | 1067 |
void xbee_handle_status(uint8_t status) |
1018 | 1068 |
{ |
1019 |
switch (status)
|
|
1020 |
{
|
|
1021 |
case 0:
|
|
1022 |
WL_DEBUG_PRINT("XBee hardware reset.\r\n");
|
|
1023 |
break;
|
|
1024 |
case 1:
|
|
1025 |
WL_DEBUG_PRINT("Watchdog timer reset.\r\n");
|
|
1026 |
break;
|
|
1027 |
case 2:
|
|
1028 |
WL_DEBUG_PRINT("Associated.\r\n");
|
|
1029 |
break;
|
|
1030 |
case 3:
|
|
1031 |
WL_DEBUG_PRINT("Disassociated.\r\n");
|
|
1032 |
break;
|
|
1033 |
case 4:
|
|
1034 |
WL_DEBUG_PRINT("Synchronization lost.\r\n");
|
|
1035 |
break;
|
|
1036 |
case 5:
|
|
1037 |
WL_DEBUG_PRINT("Coordinator realignment.\r\n");
|
|
1038 |
break;
|
|
1039 |
case 6:
|
|
1040 |
WL_DEBUG_PRINT("Coordinator started.\r\n");
|
|
1041 |
break;
|
|
1042 |
}
|
|
1069 |
switch (status)
|
|
1070 |
{
|
|
1071 |
case 0:
|
|
1072 |
WL_DEBUG_PRINT("XBee hardware reset.\r\n");
|
|
1073 |
break;
|
|
1074 |
case 1:
|
|
1075 |
WL_DEBUG_PRINT("Watchdog timer reset.\r\n");
|
|
1076 |
break;
|
|
1077 |
case 2:
|
|
1078 |
WL_DEBUG_PRINT("Associated.\r\n");
|
|
1079 |
break;
|
|
1080 |
case 3:
|
|
1081 |
WL_DEBUG_PRINT("Disassociated.\r\n");
|
|
1082 |
break;
|
|
1083 |
case 4:
|
|
1084 |
WL_DEBUG_PRINT("Synchronization lost.\r\n");
|
|
1085 |
break;
|
|
1086 |
case 5:
|
|
1087 |
WL_DEBUG_PRINT("Coordinator realignment.\r\n");
|
|
1088 |
break;
|
|
1089 |
case 6:
|
|
1090 |
WL_DEBUG_PRINT("Coordinator started.\r\n");
|
|
1091 |
break;
|
|
1092 |
}
|
|
1043 | 1093 |
} |
1044 | 1094 |
|
1045 | 1095 |
/** |
... | ... | |
1050 | 1100 |
**/ |
1051 | 1101 |
static int8_t xbee_handle_at_command_response(uint16_t command, uint8_t result, uint8_t len) |
1052 | 1102 |
{ |
1053 |
if (result == 1)
|
|
1054 |
{
|
|
1055 |
WL_DEBUG_PRINT("Error with AT");
|
|
1056 |
WL_DEBUG_PRINT(command);
|
|
1057 |
WL_DEBUG_PRINT(" packet. Result = ");
|
|
1103 |
if (result == 1)
|
|
1104 |
{
|
|
1105 |
WL_DEBUG_PRINT("Error with AT");
|
|
1106 |
WL_DEBUG_PRINT(command);
|
|
1107 |
WL_DEBUG_PRINT(" packet. Result = ");
|
|
1058 | 1108 |
switch(result) { |
1059 | 1109 |
case 1: |
1060 | 1110 |
WL_DEBUG_PRINT("ERROR\r\n"); |
... | ... | |
1067 | 1117 |
break; |
1068 | 1118 |
} |
1069 | 1119 |
return WL_SUCCESS; |
1070 |
}
|
|
1071 |
WL_DEBUG_PRINT("AT");
|
|
1072 |
WL_DEBUG_PRINT(command);
|
|
1073 |
WL_DEBUG_PRINT(" command was successful.\r\n");
|
|
1120 |
}
|
|
1121 |
WL_DEBUG_PRINT("AT");
|
|
1122 |
WL_DEBUG_PRINT(command);
|
|
1123 |
WL_DEBUG_PRINT(" command was successful.\r\n");
|
|
1074 | 1124 |
|
1075 | 1125 |
// TODO: program more command responses here (ND, etc) |
1076 | 1126 |
switch(command) { |
... | ... | |
1101 | 1151 |
**/ |
1102 | 1152 |
int8_t xbee_set_pan_id(uint16_t id) |
1103 | 1153 |
{ |
1104 |
if (getStatus(XBEE_COMMAND_MASK) == XBEE_COMMAND_WAIT
|
|
1154 |
if (getStatus(XBEE_COMMAND_MASK) == XBEE_COMMAND_WAIT
|
|
1105 | 1155 |
|| getStatus(XBEE_COMMAND_MASK) == XBEE_COMMAND_RESPONSE) |
1106 | 1156 |
return WL_ERROR_XBEE_COMMAND; // can't do command right now |
1107 | 1157 |
|
... | ... | |
1118 | 1168 |
else |
1119 | 1169 |
i = WL_ERROR_XBEE_COMMAND; // set error code |
1120 | 1170 |
setStatus(XBEE_COMMAND_MASK,XBEE_COMMAND_NONE); // reset status |
1121 |
return (int8_t)i; // return
|
|
1171 |
return (int8_t)i; // return
|
|
1122 | 1172 |
} |
1123 | 1173 |
|
1124 | 1174 |
/** |
... | ... | |
1129 | 1179 |
**/ |
1130 | 1180 |
uint16_t xbee_get_pan_id() |
1131 | 1181 |
{ |
1132 |
if (getStatus(XBEE_COMMAND_MASK) == XBEE_COMMAND_WAIT
|
|
1182 |
if (getStatus(XBEE_COMMAND_MASK) == XBEE_COMMAND_WAIT
|
|
1133 | 1183 |
|| getStatus(XBEE_COMMAND_MASK) == XBEE_COMMAND_RESPONSE) |
1134 | 1184 |
return WL_ERROR_XBEE_COMMAND_16BIT; // can't do command right now |
1135 | 1185 |
|
... | ... | |
1146 | 1196 |
else |
1147 | 1197 |
i = WL_ERROR_XBEE_COMMAND_16BIT; // set error code |
1148 | 1198 |
setStatus(XBEE_COMMAND_MASK,XBEE_COMMAND_NONE); // reset status |
1149 |
return i; // return
|
|
1199 |
return i; // return
|
|
1150 | 1200 |
} |
1151 | 1201 |
|
1152 | 1202 |
/** |
... | ... | |
1159 | 1209 |
**/ |
1160 | 1210 |
int8_t xbee_set_channel(uint8_t channel) |
1161 | 1211 |
{ |
1162 |
if (channel < 0x0B || channel > 0x1A)
|
|
1163 |
{
|
|
1164 |
WL_DEBUG_PRINT("Channel out of range.\r\n");
|
|
1165 |
return -1;
|
|
1166 |
}
|
|
1212 |
if (channel < 0x0B || channel > 0x1A)
|
|
1213 |
{
|
|
1214 |
WL_DEBUG_PRINT("Channel out of range.\r\n");
|
|
1215 |
return -1;
|
|
1216 |
}
|
|
1167 | 1217 |
|
1168 |
if (getStatus(XBEE_COMMAND_MASK) == XBEE_COMMAND_WAIT
|
|
1218 |
if (getStatus(XBEE_COMMAND_MASK) == XBEE_COMMAND_WAIT
|
|
1169 | 1219 |
|| getStatus(XBEE_COMMAND_MASK) == XBEE_COMMAND_RESPONSE) |
1170 | 1220 |
return WL_ERROR_XBEE_COMMAND; // can't do command right now |
1171 | 1221 |
|
... | ... | |
1182 | 1232 |
else |
1183 | 1233 |
i = WL_ERROR_XBEE_COMMAND; // set error code |
1184 | 1234 |
setStatus(XBEE_COMMAND_MASK,XBEE_COMMAND_NONE); // reset status |
1185 |
return (int8_t)i; // return
|
|
1235 |
return (int8_t)i; // return
|
|
1186 | 1236 |
} |
1187 | 1237 |
|
1188 | 1238 |
/** |
... | ... | |
1194 | 1244 |
**/ |
1195 | 1245 |
int8_t xbee_get_channel(void) |
1196 | 1246 |
{ |
1197 |
if (getStatus(XBEE_COMMAND_MASK) == XBEE_COMMAND_WAIT
|
|
1247 |
if (getStatus(XBEE_COMMAND_MASK) == XBEE_COMMAND_WAIT
|
|
1198 | 1248 |
|| getStatus(XBEE_COMMAND_MASK) == XBEE_COMMAND_RESPONSE) |
1199 | 1249 |
return WL_ERROR_XBEE_COMMAND; // can't do command right now |
1200 | 1250 |
|
... | ... | |
1211 | 1261 |
else |
1212 | 1262 |
i = WL_ERROR_XBEE_COMMAND; // set error code |
1213 | 1263 |
setStatus(XBEE_COMMAND_MASK,XBEE_COMMAND_NONE); // reset status |
1214 |
return i; // return
|
|
1264 |
return i; // return
|
|
1215 | 1265 |
} |
1216 | 1266 |
|
1217 | 1267 |
/** |
... | ... | |
1223 | 1273 |
**/ |
1224 | 1274 |
uint16_t xbee_get_address(void) |
1225 | 1275 |
{ |
1226 |
if (getStatus(XBEE_COMMAND_MASK) == XBEE_COMMAND_WAIT
|
|
1276 |
if (getStatus(XBEE_COMMAND_MASK) == XBEE_COMMAND_WAIT
|
|
1227 | 1277 |
|| getStatus(XBEE_COMMAND_MASK) == XBEE_COMMAND_RESPONSE) |
1228 | 1278 |
return WL_ERROR_XBEE_COMMAND_16BIT; // can't do command right now |
1229 | 1279 |
|
... | ... | |
1240 | 1290 |
else |
1241 | 1291 |
i = WL_ERROR_XBEE_COMMAND_16BIT; // set error code |
1242 | 1292 |
setStatus(XBEE_COMMAND_MASK,XBEE_COMMAND_NONE); // reset status |
1243 |
return i; // return
|
|
1293 |
return i; // return
|
|
1244 | 1294 |
} |
1245 | 1295 |
|
1246 | 1296 |
/**@} **/ // end xbee group |
Also available in: Unified diff