Revision 1638
wireless: fixed API mode 1. Can now enter and exit API mode properly. Can also set and read MY address.
xbee.c | ||
---|---|---|
57 | 57 |
|
58 | 58 |
/* API Mode Functions */ |
59 | 59 |
//TODO: does this exist? static int8_t xbee_handle_packet(uint8_t* packet, uint16_t len); |
60 |
static int8_t xbee_handle_at_command_response(uint16_t command, uint8_t result, uint8_t len);
|
|
60 |
static int8_t xbee_handle_at_command_response(uint16_t command, uint8_t status, uint8_t len);
|
|
61 | 61 |
static void xbee_handle_status(uint8_t status); |
62 | 62 |
static int8_t xbee_verify_checksum(uint8_t* packet, uint16_t len); |
63 | 63 |
static uint8_t xbee_compute_checksum(uint8_t* packet, uint16_t len); |
... | ... | |
88 | 88 |
static uint8_t xbee_basic_buf[PACKET_BUFFER_SIZE]; |
89 | 89 |
|
90 | 90 |
// beginning of first packet in basic buffer |
91 |
static uint8_t basic_buf_first = 0; |
|
91 |
static uint8_t basic_buf_first = 0xFF;
|
|
92 | 92 |
|
93 | 93 |
// byte after end of last packet in basic buffer (only access < basic_buf_last) |
94 | 94 |
// aka, the first free byte in basic buffer |
... | ... | |
98 | 98 |
static uint8_t xbee_other_buf[PACKET_BUFFER_SIZE]; |
99 | 99 |
|
100 | 100 |
// beginning of first packet in other buffer |
101 |
static uint8_t other_buf_first = 0; |
|
101 |
static uint8_t other_buf_first = 0xFF;
|
|
102 | 102 |
|
103 | 103 |
// byte after end of last packet in other buffer (only access < other_buf_last) |
104 | 104 |
// aka, the first free byte in other buffer |
... | ... | |
118 | 118 |
uint8_t xbee_status = XBEE_NOT_INITD; |
119 | 119 |
|
120 | 120 |
// xbee command response (for PAN, channel, address, etc) |
121 |
static uint8_t xbee_command[4];
|
|
121 |
static uint8_t xbee_command[8];
|
|
122 | 122 |
|
123 | 123 |
// external ack handler (wireless_send.c) |
124 | 124 |
void ackhandle(uint8_t num,uint8_t val) { return; } // disabled |
125 | 125 |
|
126 | 126 |
// message buffer |
127 |
uint8_t step=99; |
|
128 | 127 |
uint8_t message[100]; |
129 | 128 |
|
130 | 129 |
|
... | ... | |
139 | 138 |
**/ |
140 | 139 |
|
141 | 140 |
|
142 |
//#ifndef FIREFLY
|
|
141 |
#ifndef FIREFLY |
|
143 | 142 |
#define PORT UDR1 |
144 |
#define FLAG RXC1
|
|
143 |
#define FLAG (UCSR1A & (1<<RXC1))
|
|
145 | 144 |
ISR(USART1_RX_vect) { |
146 |
//#else |
|
147 |
//#define PORT UDR0 |
|
148 |
//#define FLAG RXC0 |
|
149 |
//SIGNAL(SIG_USART0_RECV) |
|
150 |
//#endif |
|
151 |
step=0; |
|
145 |
#else |
|
146 |
#define PORT UDR0 |
|
147 |
#define FLAG (UCSR0A & (1<<RXC0)) |
|
148 |
SIGNAL(SIG_USART0_RECV) |
|
149 |
#endif |
|
152 | 150 |
// start of frame |
153 | 151 |
uint8_t apitype = PORT; // get frame start byte |
154 | 152 |
uint16_t i=0; |
155 | 153 |
uint16_t len=0; |
156 | 154 |
|
157 |
usb_puts("xbee_interrupt"); |
|
158 |
step=0; |
|
159 |
|
|
160 |
WL_DEBUG_PRINT("in interrupt|status="); |
|
155 |
/*WL_DEBUG_PRINT("in interrupt|status="); |
|
161 | 156 |
WL_DEBUG_PRINT_HEX(xbee_status); |
162 | 157 |
WL_DEBUG_PRINT("|frame_start="); |
163 | 158 |
WL_DEBUG_PRINT_HEX(apitype); |
... | ... | |
168 | 163 |
WL_DEBUG_PRINT("|API ON"); |
169 | 164 |
} else { |
170 | 165 |
WL_DEBUG_PRINT("|API ERROR"); |
171 |
} |
|
166 |
}*/
|
|
172 | 167 |
|
173 | 168 |
// check that we're in API mode |
174 | 169 |
if (getStatus(XBEE_API_MASK) == XBEE_API_OFF || apitype != XBEE_FRAME_START) { |
175 | 170 |
// not in API mode |
176 | 171 |
WL_DEBUG_PRINT("|api off branch"); |
177 |
step=1; |
|
172 |
|
|
178 | 173 |
if (getStatus(XBEE_COMMAND_MASK) == XBEE_COMMAND_WAIT) { |
179 | 174 |
// get rest of command and put in basic buf |
180 | 175 |
xbee_basic_buf[i] = apitype; |
... | ... | |
184 | 179 |
xbee_basic_buf[i] = PORT; |
185 | 180 |
if (xbee_basic_buf[i] == '\r') |
186 | 181 |
break; |
182 |
i++; |
|
187 | 183 |
} |
188 |
i++; |
|
189 | 184 |
} |
190 | 185 |
} |
191 | 186 |
WL_DEBUG_PRINT("got packet, len="); |
192 | 187 |
WL_DEBUG_PRINT_INT(i); |
193 | 188 |
WL_DEBUG_PRINT("str="); |
194 | 189 |
WL_DEBUG_PRINT(xbee_basic_buf); |
195 |
step=2; |
|
196 | 190 |
// signal handler that command response is done |
197 | 191 |
setStatus(XBEE_COMMAND_MASK,XBEE_COMMAND_RESPONSE); |
198 | 192 |
} |
199 | 193 |
WL_DEBUG_PRINT("return\r\n"); |
200 |
step=3; |
|
201 | 194 |
return; |
202 | 195 |
} |
203 |
WL_DEBUG_PRINT("|api on branch"); |
|
204 |
step=10; |
|
196 |
//WL_DEBUG_PRINT("|api on branch"); |
|
205 | 197 |
|
206 | 198 |
// get length and type |
207 |
apitype = 0; |
|
208 |
while((len==0 && i<100)||i<(len+3)) { |
|
199 |
char buf[100]; |
|
200 |
uint8_t buf_pos = 18; |
|
201 |
while(i<3) { |
|
209 | 202 |
if (FLAG) { |
210 | 203 |
if (i==0) { |
211 | 204 |
len |= PORT<<8; |
212 |
//WL_DEBUG_PRINT("|len_hi:0x"); |
|
213 |
//WL_DEBUG_PRINT_HEX(PORT); |
|
205 |
buf[0] = 'l'; |
|
206 |
buf[1] = 'e'; |
|
207 |
buf[2] = 'n'; |
|
208 |
buf[3] = '_'; |
|
209 |
buf[4] = 'h'; |
|
210 |
buf[5] = 'i'; |
|
211 |
buf[6] = '|'; |
|
212 |
//WL_DEBUG_PRINT("|len_hi"); |
|
214 | 213 |
} else if (i==1) { |
215 | 214 |
len |= PORT; |
216 |
//WL_DEBUG_PRINT("|len_low:0x"); |
|
217 |
//WL_DEBUG_PRINT_HEX(PORT); |
|
215 |
buf[7] = 'l'; |
|
216 |
buf[8] = 'e'; |
|
217 |
buf[9] = 'n'; |
|
218 |
buf[10] = '_'; |
|
219 |
buf[11] = 'l'; |
|
220 |
buf[12] = 'o'; |
|
221 |
buf[13] = '|'; |
|
222 |
//WL_DEBUG_PRINT("|len_lo"); |
|
218 | 223 |
} else if (i==2) { |
219 |
//WL_DEBUG_PRINT("|GOT APITYPE:0x"); |
|
220 |
//WL_DEBUG_PRINT_HEX(PORT); |
|
221 | 224 |
apitype = PORT; |
225 |
buf[14] = 'a'; |
|
226 |
buf[15] = 'p'; |
|
227 |
buf[16] = 'i'; |
|
228 |
buf[17] = '|'; |
|
229 |
//WL_DEBUG_PRINT("|GOT APITYPE"); |
|
222 | 230 |
} else { |
223 |
WL_DEBUG_PRINT("|GOT NEXT:0x");
|
|
224 |
WL_DEBUG_PRINT_HEX(PORT);
|
|
231 |
buf[buf_pos++] = PORT;
|
|
232 |
//WL_DEBUG_PRINT("|GOT NEXT");
|
|
225 | 233 |
} |
226 | 234 |
i++; |
227 | 235 |
} |
228 | 236 |
} |
229 |
WL_DEBUG_PRINT("|len="); |
|
230 |
WL_DEBUG_PRINT_INT(len); |
|
231 |
WL_DEBUG_PRINT("|apitype="); |
|
232 |
WL_DEBUG_PRINT_HEX(apitype); |
|
233 |
WL_DEBUG_PRINT("\r\n"); |
|
234 |
step=11; |
|
235 | 237 |
|
236 | 238 |
// do something based on the type |
237 | 239 |
i=1; |
... | ... | |
239 | 241 |
case XBEE_FRAME_AT_COMMAND_RESPONSE: { |
240 | 242 |
// AT command response |
241 | 243 |
if (getStatus(XBEE_COMMAND_MASK) == XBEE_COMMAND_RESPONSE) |
242 |
return; // we're currently processing a command, so drop the incoming one |
|
244 |
return; // we're currently processing a command, so drop the incoming one ***** TODO: we need to read the whole length of the frame to do this right ****
|
|
243 | 245 |
uint16_t atcommand=0; |
244 | 246 |
uint8_t ptr=basic_buf_last; |
245 | 247 |
while(i<len) { |
... | ... | |
259 | 261 |
} |
260 | 262 |
i++; |
261 | 263 |
} |
262 |
} |
|
263 |
// handle AT command |
|
264 |
xbee_handle_at_command_response(atcommand,apitype,i-5); // TODO: rewrite function |
|
264 |
} |
|
265 |
// handle AT command |
|
266 |
WL_DEBUG_PRINT("i="); |
|
267 |
WL_DEBUG_PRINT_INT(i); |
|
268 |
WL_DEBUG_PRINT("|len="); |
|
269 |
WL_DEBUG_PRINT_INT(len); |
|
270 |
WL_DEBUG_PRINT("|status:"); |
|
271 |
WL_DEBUG_PRINT_INT(apitype); |
|
272 |
WL_DEBUG_PRINT("|atcommand:"); |
|
273 |
WL_DEBUG_PRINT_CHAR((uint8_t)(atcommand>>8)); |
|
274 |
WL_DEBUG_PRINT_CHAR((uint8_t)(atcommand&0xFF)); |
|
275 |
WL_DEBUG_PRINT("|buf="); |
|
276 |
uint8_t ptr2 = basic_buf_last; |
|
277 |
for(uint8_t j=0;j<i;j++) |
|
278 |
WL_DEBUG_PRINT_HEX(xbee_basic_buf_get(&ptr2)); |
|
279 |
WL_DEBUG_PRINT("|\r\n"); |
|
280 |
xbee_handle_at_command_response(atcommand,apitype,len-5); // TODO: rewrite function |
|
265 | 281 |
break; } |
266 | 282 |
case XBEE_FRAME_TX_STATUS: { |
267 | 283 |
// TX status |
... | ... | |
365 | 381 |
break; |
366 | 382 |
} |
367 | 383 |
} |
368 |
WL_DEBUG_PRINT("return\r\n"); |
|
369 |
step=255; |
|
384 |
buf[buf_pos] = '\0'; |
|
385 |
WL_DEBUG_PRINT("xbee_interrupt_api_mode|"); |
|
386 |
WL_DEBUG_PRINT(buf); |
|
387 |
WL_DEBUG_PRINT("|len="); |
|
388 |
WL_DEBUG_PRINT_INT(len); |
|
389 |
WL_DEBUG_PRINT("|apitype="); |
|
390 |
WL_DEBUG_PRINT_HEX(apitype); |
|
391 |
WL_DEBUG_PRINT("|return\r\n"); |
|
370 | 392 |
} // end of interrupt |
371 | 393 |
|
372 | 394 |
|
... | ... | |
457 | 479 |
// Set startup baud rate of 9600 |
458 | 480 |
// Set frame format: 8data, 1stop bit, asynchronous normal mode |
459 | 481 |
// Enable receiver and transmitter and the receiving interrupt |
460 |
/*#ifdef FIREFLY |
|
461 |
UCSR0A |= (1<<U2X0); |
|
482 |
#ifdef FIREFLY |
|
462 | 483 |
UBRR0H = 0x00; |
463 | 484 |
UBRR0L = 103; |
485 |
UCSR0A |= (1<<U2X0); |
|
464 | 486 |
UCSR0C |= (1<<UCSZ00) | (1<<UCSZ01); |
465 | 487 |
UCSR0B |= (1<<RXEN0) | (1<<TXEN0) | (1<<RXCIE); |
466 |
#else*/ |
|
467 |
// Bayboard or robot |
|
468 |
|
|
488 |
#else |
|
489 |
// Bayboard or robot |
|
469 | 490 |
UBRR1H = 0x00; // baud rate |
470 | 491 |
UBRR1L = 103; // baud rate |
471 | 492 |
UCSR1A |= (1<<U2X1); // double transmit speed |
... | ... | |
473 | 494 |
UCSR1B |= (1<<RXEN1)|(1<<TXEN1); |
474 | 495 |
|
475 | 496 |
// Set frame format: 8data, 1stop bit, asynchronous normal mode |
476 |
//UCSR1C |= (1<<UCSZ10) | (1<<UCSZ11);
|
|
497 |
UCSR1C |= (1<<UCSZ10) | (1<<UCSZ11); |
|
477 | 498 |
|
478 |
//UCSR1C |= (1<<UMSEL1) | (1<<UCSZ10) | (1<<UCSZ11); |
|
479 |
//UCSR1C |= (1 << URSEL) | (1 << UCSZ0) | (1 << UCSZ1); |
|
480 | 499 |
UCSR1B |= (1<<RXCIE1); // enable receive, transmit (1<<RXCIE) |
481 |
WL_DEBUG_PRINT("receive interrupt flag="); |
|
482 |
WL_DEBUG_PRINT_HEX(UCSR1B&(1<<RXCIE1)); |
|
483 |
WL_DEBUG_PRINT("|\r\n"); |
|
500 |
#endif |
|
484 | 501 |
sei(); |
485 | 502 |
|
486 | 503 |
// enter command mode |
... | ... | |
557 | 574 |
return WL_ERROR_BUAD; |
558 | 575 |
#endif |
559 | 576 |
#endif |
560 |
// wait one second for the baud change to stabalize
|
|
561 |
delay_ms(1000);
|
|
577 |
// wait half second for the baud change to stabalize
|
|
578 |
delay_ms(500);
|
|
562 | 579 |
|
563 | 580 |
// enter command mode |
564 | 581 |
WL_DEBUG_PRINT("entering command mode 2\r\n"); |
... | ... | |
566 | 583 |
xbee_wait_for_ok(); |
567 | 584 |
WL_DEBUG_PRINT("entered command mode 2\r\n"); |
568 | 585 |
|
569 |
/*if (xbee_enter_api_mode() != 0) {
|
|
586 |
if (xbee_enter_api_mode() != 0) { |
|
570 | 587 |
WL_DEBUG_PRINT("can't enter api mode\r\n"); |
571 | 588 |
return -1; |
572 |
}*/
|
|
573 |
xbee_send_string((uint8_t*)"ATAP 1\r"); |
|
574 |
xbee_wait_for_ok(); |
|
589 |
} |
|
590 |
/*xbee_send_string((uint8_t*)"ATAP 1\r");
|
|
591 |
xbee_wait_for_ok();*/
|
|
575 | 592 |
|
576 |
WL_DEBUG_PRINT("Entered api mode.\r\n"); |
|
593 |
/*WL_DEBUG_PRINT("Entered api mode.\r\n");
|
|
577 | 594 |
|
578 | 595 |
if (xbee_exit_command_mode() != 0) { |
579 | 596 |
WL_DEBUG_PRINT("can't exit command mode\r\n"); |
... | ... | |
587 | 604 |
WL_DEBUG_PRINT_HEX(getStatus(0xFF)); |
588 | 605 |
WL_DEBUG_PRINT("\r\n"); |
589 | 606 |
|
590 |
setStatus(XBEE_API_MASK,XBEE_API_ON); // set status |
|
607 |
setStatus(XBEE_API_MASK,XBEE_API_ON); // set status to API_ON (API mode 1)*/
|
|
591 | 608 |
|
609 |
#ifdef WL_DEBUG |
|
592 | 610 |
if (getStatus(XBEE_API_MASK) == XBEE_API_OFF) { |
593 | 611 |
WL_DEBUG_PRINT("|API OFF|"); |
594 | 612 |
} else if (getStatus(XBEE_API_MASK) == XBEE_API_ON) { |
... | ... | |
596 | 614 |
} else { |
597 | 615 |
WL_DEBUG_PRINT("|API ERROR|"); |
598 | 616 |
} |
617 |
#endif |
|
599 | 618 |
|
600 |
WL_DEBUG_PRINT("after status="); |
|
601 |
WL_DEBUG_PRINT_HEX(getStatus(0xFF)); |
|
602 |
WL_DEBUG_PRINT("\r\n"); |
|
603 | 619 |
|
604 |
|
|
605 | 620 |
// TODO: we should set the MY address to the robot address from eeprom |
606 |
if (xbee_send_modify_at_command((uint8_t*)"MY",(uint8_t*)"4",1) != WL_SUCCESS) { |
|
621 |
uint16_t i=0; |
|
622 |
setStatus(XBEE_COMMAND_MASK,XBEE_COMMAND_WAIT); |
|
623 |
uint8_t newmy = 4; |
|
624 |
if (xbee_send_modify_at_command((uint8_t*)"MY",&newmy,1) != WL_SUCCESS) { |
|
607 | 625 |
WL_DEBUG_PRINT("setting MY address failed\r\n"); |
608 | 626 |
} |
627 |
// wait for up to 30 ms |
|
628 |
while(getStatus(XBEE_COMMAND_MASK) != XBEE_COMMAND_RESPONSE && i++ < 10000) { |
|
629 |
delay_us(1); // wait 3us |
|
630 |
} |
|
631 |
if (i < 1000 && xbee_command[0] == 'M' && xbee_command[1] == 'Y') { |
|
632 |
WL_DEBUG_PRINT("setting MY address successful\r\n"); |
|
633 |
} else |
|
634 |
WL_DEBUG_PRINT("setting MY address failed\r\n"); |
|
635 |
setStatus(XBEE_COMMAND_MASK,XBEE_COMMAND_NONE); // reset status |
|
609 | 636 |
|
610 |
if (xbee_get_address() == WL_ERROR_XBEE_COMMAND_16BIT) { |
|
637 |
|
|
638 |
uint16_t address = xbee_get_address(); |
|
639 |
if (address == WL_ERROR_XBEE_COMMAND_16BIT) { |
|
611 | 640 |
WL_DEBUG_PRINT("getting MY address failed\r\n"); |
612 | 641 |
} |
613 |
WL_DEBUG_PRINT("MY address successful\r\n"); |
|
642 |
WL_DEBUG_PRINT("MY address:"); |
|
643 |
WL_DEBUG_PRINT_HEX((uint8_t)(address>>8)); |
|
644 |
WL_DEBUG_PRINT_HEX((uint8_t)(address&0xFF)); |
|
645 |
WL_DEBUG_PRINT("\r\n"); |
|
614 | 646 |
|
615 |
|
|
616 |
// wait one second for the baud change to stabalize |
|
617 |
delay_ms(500); |
|
618 |
|
|
619 |
// enter command mode |
|
620 |
WL_DEBUG_PRINT("entering command mode 3\r\n"); |
|
621 |
xbee_send_string((uint8_t*)"+++"); |
|
622 |
xbee_wait_for_ok(); |
|
623 |
WL_DEBUG_PRINT("entered command mode 3\r\n"); |
|
624 |
xbee_exit_command_mode(); |
|
625 |
|
|
626 |
|
|
627 | 647 |
return WL_SUCCESS; |
628 | 648 |
} |
629 | 649 |
|
... | ... | |
654 | 674 |
// Wait until buffer is clear for sending |
655 | 675 |
// Then load buffer with your character |
656 | 676 |
|
657 |
WL_DEBUG_PRINT("xbee_putc:"); |
|
677 |
/*WL_DEBUG_PRINT("xbee_putc:");
|
|
658 | 678 |
WL_DEBUG_PRINT_HEX(c); |
659 |
WL_DEBUG_PRINT("|\r\n"); |
|
679 |
WL_DEBUG_PRINT("|\r\n");*/
|
|
660 | 680 |
|
661 |
/*#ifdef FIREFLY
|
|
681 |
#ifdef FIREFLY |
|
662 | 682 |
loop_until_bit_is_set(UCSR0A, UDRE0); |
663 | 683 |
UDR0 = c; |
664 |
#else*/
|
|
684 |
#else |
|
665 | 685 |
loop_until_bit_is_set(UCSR1A, UDRE1); |
666 |
//WL_DEBUG_PRINT_HEX(UCSR1A&TXC1); |
|
667 | 686 |
UDR1 = c; |
668 |
//#endif
|
|
687 |
#endif |
|
669 | 688 |
|
670 | 689 |
return WL_SUCCESS; |
671 | 690 |
} |
... | ... | |
773 | 792 |
if (xbee_send_string((uint8_t*)"ATCN\r") != 0) { |
774 | 793 |
return WL_ERROR_SEND; |
775 | 794 |
} |
776 |
xbee_wait_for_ok(); |
|
795 |
if (xbee_wait_for_ok() != WL_SUCCESS) { |
|
796 |
WL_DEBUG_PRINT("failed to exit command mode\r\n"); |
|
797 |
return WL_ERROR_SEND; |
|
798 |
} |
|
777 | 799 |
|
778 | 800 |
return WL_SUCCESS; |
779 | 801 |
} |
... | ... | |
785 | 807 |
if (xbee_send_string((uint8_t*)"ATAP 1\r") != 0) { |
786 | 808 |
return WL_ERROR_SEND; |
787 | 809 |
} |
788 |
if (xbee_wait_for_ok() != 0) {
|
|
810 |
if (xbee_wait_for_ok() != WL_SUCCESS) {
|
|
789 | 811 |
WL_DEBUG_PRINT("failed to enter API mode\r\n"); |
790 | 812 |
return WL_ERROR_SEND; |
791 | 813 |
} |
814 |
if (xbee_exit_command_mode() != WL_SUCCESS) { |
|
815 |
WL_DEBUG_PRINT("failed to enter API mode\r\n"); |
|
816 |
return WL_ERROR_SEND; |
|
817 |
} |
|
792 | 818 |
WL_DEBUG_PRINT("got OK after entering API mode\r\n"); |
793 | 819 |
|
820 |
setStatus(XBEE_API_MASK,XBEE_API_ON); // set status |
|
821 |
|
|
794 | 822 |
return WL_SUCCESS; |
795 | 823 |
} |
796 | 824 |
|
... | ... | |
801 | 829 |
if (xbee_send_string((uint8_t*)"ATAP 2\r") != 0) { |
802 | 830 |
return WL_ERROR_SEND; |
803 | 831 |
} |
804 |
if (xbee_wait_for_ok() != 0) {
|
|
832 |
if (xbee_wait_for_ok() != WL_SUCCESS) {
|
|
805 | 833 |
WL_DEBUG_PRINT("failed to enter API mode2\r\n"); |
806 | 834 |
return WL_ERROR_SEND; |
807 | 835 |
} |
836 |
if (xbee_exit_command_mode() != WL_SUCCESS) { |
|
837 |
WL_DEBUG_PRINT("failed to enter API mode2\r\n"); |
|
838 |
return WL_ERROR_SEND; |
|
839 |
} |
|
808 | 840 |
WL_DEBUG_PRINT("got OK after entering API mode2\r\n"); |
809 | 841 |
|
810 | 842 |
setStatus(XBEE_API_MASK,XBEE_API_ESCAPE); // set status |
... | ... | |
825 | 857 |
int16_t i=0; |
826 | 858 |
// change status to command wait |
827 | 859 |
setStatus(XBEE_COMMAND_MASK,XBEE_COMMAND_WAIT); |
828 |
xbee_send_modify_at_command((uint8_t*)"AP",(uint8_t*)"0",1); // send command |
|
860 |
if (xbee_send_modify_at_command((uint8_t*)"AP",(uint8_t*)(&i),1) != WL_SUCCESS) { // send command |
|
861 |
WL_DEBUG_PRINT("error sending AP 0 command\r\n"); |
|
862 |
setStatus(XBEE_COMMAND_MASK,XBEE_COMMAND_NONE); // reset status |
|
863 |
return WL_ERROR_XBEE_COMMAND; |
|
864 |
} |
|
829 | 865 |
// wait for up to 30 ms |
830 | 866 |
while(getStatus(XBEE_COMMAND_MASK) != XBEE_COMMAND_RESPONSE && i++ < 10000) { |
831 | 867 |
delay_us(1); // wait 3us |
... | ... | |
837 | 873 |
WL_DEBUG_PRINT("failed to exit API mode\r\n"); |
838 | 874 |
i = WL_ERROR_XBEE_COMMAND; // set error code |
839 | 875 |
} |
840 |
setStatus(XBEE_API_MASK,XBEE_API_OFF); // reset status |
|
876 |
setStatus(XBEE_COMMAND_MASK,XBEE_COMMAND_NONE); // reset status |
|
877 |
setStatus(XBEE_API_MASK,XBEE_API_OFF); |
|
878 |
|
|
841 | 879 |
return (int8_t)i; // return |
842 | 880 |
} |
843 | 881 |
|
... | ... | |
846 | 884 |
**/ |
847 | 885 |
static int8_t xbee_wait_for_ok() |
848 | 886 |
{ |
849 |
return xbee_wait_for_string((uint8_t*)"OK\r", 3); |
|
887 |
//delay_ms(1000); |
|
888 |
//return WL_SUCCESS; |
|
889 |
return xbee_wait_for_string((uint8_t*)"OK", 2); |
|
850 | 890 |
} |
851 | 891 |
|
852 | 892 |
/** |
... | ... | |
879 | 919 |
setStatus(XBEE_COMMAND_MASK,XBEE_COMMAND_NONE); // clear status |
880 | 920 |
} |
881 | 921 |
|
882 |
WL_DEBUG_PRINT("|\r\ndone waiting for string. interrupt status="); |
|
883 |
WL_DEBUG_PRINT_INT(step); |
|
884 |
WL_DEBUG_PRINT("\r\n"); |
|
922 |
WL_DEBUG_PRINT("done waiting for string.\r\n"); |
|
885 | 923 |
|
886 | 924 |
return 0; |
887 | 925 |
} |
888 | 926 |
|
889 | 927 |
/** |
890 | 928 |
* Delay until we receive a command response. |
891 |
* (either OK\r or some actual value)
|
|
929 |
* Then copy the response to S and set the len
|
|
892 | 930 |
* |
893 | 931 |
* Only works when not in API mode |
894 | 932 |
* |
... | ... | |
909 | 947 |
i=strcspn((char*)xbee_basic_buf,"\r"); |
910 | 948 |
if (i<PACKET_BUFFER_SIZE) { |
911 | 949 |
memcpy(s,xbee_basic_buf,i); |
950 |
len = i; |
|
912 | 951 |
setStatus(XBEE_COMMAND_MASK,XBEE_COMMAND_NONE); // clear response |
913 | 952 |
return 0; |
914 | 953 |
} |
... | ... | |
1021 | 1060 |
//packet prefix |
1022 | 1061 |
if (xbee_putc(XBEE_FRAME_START) != WL_SUCCESS) |
1023 | 1062 |
return WL_ERROR_SEND; |
1024 |
if (xbee_putc((uint8_t)((len & 0xFF00) >> 8)) != WL_SUCCESS)
|
|
1063 |
if (xbee_putc((uint8_t)(len>>8)) != WL_SUCCESS)
|
|
1025 | 1064 |
return WL_ERROR_SEND; |
1026 |
if (xbee_putc((uint8_t)(len & 0x00FF)) != WL_SUCCESS)
|
|
1065 |
if (xbee_putc((uint8_t)(len&0xFF)) != WL_SUCCESS)
|
|
1027 | 1066 |
return WL_ERROR_SEND; |
1028 | 1067 |
|
1029 | 1068 |
if (xbee_send(buf, len) != WL_SUCCESS) |
... | ... | |
1184 | 1223 |
* @param result 0 for success, 1 for an error |
1185 | 1224 |
* @param len the length in bytes of extra |
1186 | 1225 |
**/ |
1187 |
static int8_t xbee_handle_at_command_response(uint16_t command, uint8_t result, uint8_t len)
|
|
1226 |
static int8_t xbee_handle_at_command_response(uint16_t command, uint8_t status, uint8_t len)
|
|
1188 | 1227 |
{ |
1189 |
WL_DEBUG_PRINT("HANDLE AT COMMAND\r\ninterrupt status="); |
|
1190 |
WL_DEBUG_PRINT_INT(step); |
|
1191 |
WL_DEBUG_PRINT("\r\n"); |
|
1192 |
if (result == 1) |
|
1228 |
WL_DEBUG_PRINT("HANDLE AT COMMAND\r\n"); |
|
1229 |
if (status != 0) |
|
1193 | 1230 |
{ |
1194 | 1231 |
WL_DEBUG_PRINT("Error with AT"); |
1195 | 1232 |
WL_DEBUG_PRINT(command); |
1196 | 1233 |
WL_DEBUG_PRINT(" packet. Result = "); |
1197 |
switch(result) {
|
|
1234 |
switch(status) {
|
|
1198 | 1235 |
case 1: |
1199 | 1236 |
WL_DEBUG_PRINT("ERROR\r\n"); |
1200 | 1237 |
break; |
... | ... | |
1208 | 1245 |
return WL_SUCCESS; |
1209 | 1246 |
} |
1210 | 1247 |
WL_DEBUG_PRINT("AT"); |
1211 |
WL_DEBUG_PRINT(command); |
|
1212 |
WL_DEBUG_PRINT(" command was successful.\r\n"); |
|
1248 |
WL_DEBUG_PRINT_CHAR((uint8_t)(command>>8)); |
|
1249 |
WL_DEBUG_PRINT_CHAR((uint8_t)(command)); |
|
1250 |
WL_DEBUG_PRINT(" command is being handled\r\n"); |
|
1213 | 1251 |
|
1214 | 1252 |
// TODO: program more command responses here (ND, etc) |
1215 | 1253 |
switch(command) { |
1216 | 1254 |
case ('I'<<8)+'D': // PAN |
1217 | 1255 |
case ('C'<<8)+'H': // channel |
1218 | 1256 |
case ('M'<<8)+'Y': // address |
1257 |
case ('A'<<8)+'P': // api mode |
|
1219 | 1258 |
// copy command to handler |
1220 | 1259 |
xbee_command[0] = (command&0xFF00)>>8; |
1221 | 1260 |
xbee_command[1] = command&0x00FF; |
1222 |
result = basic_buf_last; |
|
1223 |
for(command=2;command<len+2;command++) |
|
1224 |
xbee_command[command] = xbee_basic_buf_get(&result); |
|
1261 |
uint8_t ptr = basic_buf_last; |
|
1262 |
for(command=2;command<len+2;command++) { |
|
1263 |
xbee_command[command] = xbee_basic_buf_get(&ptr); |
|
1264 |
if (xbee_command[command] == '\r') |
|
1265 |
break; // done with command |
|
1266 |
WL_DEBUG_PRINT_HEX(xbee_command[command]); |
|
1267 |
} |
|
1268 |
xbee_command[command] = '\0'; |
|
1269 |
WL_DEBUG_PRINT("len="); |
|
1270 |
WL_DEBUG_PRINT_INT(len); |
|
1271 |
WL_DEBUG_PRINT("ID,CH,or MY command result:"); |
|
1272 |
WL_DEBUG_PRINT(xbee_command); |
|
1273 |
WL_DEBUG_PRINT("\r\n"); |
|
1225 | 1274 |
break; |
1226 | 1275 |
default: |
1227 | 1276 |
WL_DEBUG_PRINT("unknown AT command"); |
1228 | 1277 |
} |
1229 | 1278 |
|
1230 | 1279 |
// signal handler that command response is done |
1231 |
setStatus(XBEE_COMMAND_MASK,XBEE_COMMAND_NONE);
|
|
1280 |
setStatus(XBEE_COMMAND_MASK,XBEE_COMMAND_RESPONSE);
|
|
1232 | 1281 |
|
1233 | 1282 |
return WL_SUCCESS; |
1234 | 1283 |
} |
... | ... | |
1368 | 1417 |
|
1369 | 1418 |
uint16_t i=0; |
1370 | 1419 |
// change status to command wait |
1371 |
WL_DEBUG_PRINT("\r\nbefore status="); |
|
1420 |
/*WL_DEBUG_PRINT("\r\nbefore status=");
|
|
1372 | 1421 |
WL_DEBUG_PRINT_HEX(getStatus(0xFF)); |
1373 |
WL_DEBUG_PRINT("\r\n"); |
|
1422 |
WL_DEBUG_PRINT("\r\n");*/
|
|
1374 | 1423 |
setStatus(XBEE_COMMAND_MASK,XBEE_COMMAND_WAIT); |
1375 |
if (getStatus(XBEE_API_MASK) == XBEE_API_OFF) { |
|
1424 |
/*if (getStatus(XBEE_API_MASK) == XBEE_API_OFF) {
|
|
1376 | 1425 |
WL_DEBUG_PRINT("|API OFF|"); |
1377 | 1426 |
} else if (getStatus(XBEE_API_MASK) == XBEE_API_ON) { |
1378 | 1427 |
WL_DEBUG_PRINT("|API ON|"); |
... | ... | |
1381 | 1430 |
} |
1382 | 1431 |
WL_DEBUG_PRINT("after status="); |
1383 | 1432 |
WL_DEBUG_PRINT_HEX(getStatus(0xFF)); |
1384 |
WL_DEBUG_PRINT("\r\n"); |
|
1433 |
WL_DEBUG_PRINT("\r\n");*/
|
|
1385 | 1434 |
xbee_send_read_at_command((uint8_t*)"MY"); // send command to get the address |
1386 | 1435 |
// wait for up to 30 ms |
1387 | 1436 |
while(getStatus(XBEE_COMMAND_MASK) != XBEE_COMMAND_RESPONSE && i++ < 10000) { |
1388 | 1437 |
delay_us(1); // wait 3us |
1389 | 1438 |
} |
1390 |
if (i < 1000 && xbee_command[0] == 'M' && xbee_command[1] == 'Y') |
|
1391 |
i = (xbee_command[2]<<8)+xbee_command[3]; // get address |
|
1392 |
else |
|
1439 |
|
|
1440 |
if (i < 1000 && xbee_command[0] == 'M' && xbee_command[1] == 'Y') { |
|
1441 |
i = (xbee_command[2]<<8) | (xbee_command[3]); |
|
1442 |
} else |
|
1393 | 1443 |
i = WL_ERROR_XBEE_COMMAND_16BIT; // set error code |
1394 | 1444 |
setStatus(XBEE_COMMAND_MASK,XBEE_COMMAND_NONE); // reset status |
1395 |
WL_DEBUG_PRINT("get MY address, interrupt status="); |
|
1396 |
WL_DEBUG_PRINT_INT(step); |
|
1397 |
WL_DEBUG_PRINT("\r\n"); |
|
1445 |
WL_DEBUG_PRINT("get MY address\r\n"); |
|
1398 | 1446 |
return i; // return |
1399 | 1447 |
} |
1400 | 1448 |
|
Also available in: Unified diff