Project

General

Profile

Revision 1611

wl updates: compile issues; now finished (for now)

View differences:

xbee.c
35 35
#include <string.h>
36 36
#include <avr/interrupt.h>
37 37
#include <time.h>
38
#include <serial.h> // TODO: integrate serial xbee functions into this file
39 38
#include "wl_defs.h"
39
#include "wireless.h"
40 40
#include "xbee.h"
41 41

  
42 42

  
......
45 45
// TODO: convert all int references to int16_t syntax (see stdint.h)
46 46

  
47 47
/* I/O Functions */
48
static int8_t xbee_send_string(unsigned char* c);
48
static int8_t xbee_send_string(uint8_t* c);
49 49

  
50 50
/* Command Mode Functions */
51
static int xbee_enter_command_mode(void);
52
static int xbee_exit_command_mode(void);
53
static int xbee_enter_api_mode(void);
54
static int xbee_exit_api_mode(void);
55
static int xbee_wait_for_string(char* s, uint16_t len);
56
static int xbee_wait_for_ok(void);
51
static int8_t xbee_enter_command_mode(void);
52
static int8_t xbee_exit_command_mode(void);
53
static int8_t xbee_enter_api_mode(void);
54
static int8_t xbee_exit_api_mode(void);
55
static int8_t xbee_wait_for_string(uint8_t* s, uint16_t len);
56
static int8_t xbee_wait_for_ok(void);
57 57

  
58 58
/* API Mode Functions */
59
static int xbee_handle_packet(uint8_t* packet, uint16_t len);
60
static int xbee_handle_at_command_response(uint16_t command, uint8_t result, uint8_t len);
61
static int xbee_handle_at_command_response(uint16_t command, uint8_t result, uint8_t len);
59
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);
61
static int8_t xbee_handle_at_command_response(uint16_t command, uint8_t result, uint8_t len);
62 62
static void xbee_handle_status(uint8_t status);
63
static int xbee_verify_checksum(uint8_t* packet, uint16_t len);
64
static char xbee_compute_checksum(uint8_t* packet, uint16_t len);
65
static int xbee_send_frame(uint8_t* buf, int len);
66
int xbee_send_read_at_command(uint8_t* command);
67
static int xbee_send_modify_at_command(uint8_t* command, uint8_t* value, uint8_t len);
63
static int8_t xbee_verify_checksum(uint8_t* packet, uint16_t len);
64
static uint8_t xbee_compute_checksum(uint8_t* packet, uint16_t len);
65
static int8_t xbee_send_frame(uint8_t* buf, uint16_t len);
66
int8_t xbee_send_read_at_command(uint8_t* command);
67
static int8_t xbee_send_modify_at_command(uint8_t* command, uint8_t* value, uint8_t len);
68 68

  
69 69
/* Buffer Manipulation Functions */
70 70
int8_t xbee_basic_buf_add(uint8_t *ptr, uint8_t byte);
......
172 172
  // do something based on the type
173 173
  i=1;
174 174
  switch(apitype) {
175
  case XBEE_FRAME_AT_COMMAND_RESPONSE:
175
  case XBEE_FRAME_AT_COMMAND_RESPONSE: {
176 176
    // AT command response
177 177
    if ((xbee_status&0xC0) == XBEE_COMMAND_RESPONSE)
178 178
      return; // we're currently processing a command, so drop the incoming one
......
198 198
    }  
199 199
    // handle AT command  
200 200
    xbee_handle_at_command_response(atcommand,apitype,i-5); // TODO: rewrite function
201
    break;
202
  case XBEE_FRAME_TX_STATUS:
201
    break; }
202
  case XBEE_FRAME_TX_STATUS: {
203 203
    // TX status
204
    if (0) { WL_DEBUG_PRINT("test"); } // why do I need this statement?
205 204
    uint8_t frame_id = 0;
206 205
    while(i<len) {
207 206
      if (FLAG) {
......
214 213
        i++;
215 214
      }
216 215
    }
217
    break;
218
  case XBEE_FRAME_RX_64:
216
    break; }
217
  case XBEE_FRAME_RX_64: {
219 218
    // receive a packet with 64bit address
220
    break; // TODO: implement this (even if we don't use it)
221
  case XBEE_FRAME_RX_16:
219
    break; } // TODO: implement this (even if we don't use it)
220
  case XBEE_FRAME_RX_16: {
222 221
    // receive a packet with 16bit address
223 222
    uint16_t source = 0;
224 223
    uint8_t framenum = 0;
......
292 291
        }
293 292
      }
294 293
    }
295
    break;
294
    break; }
296 295
  } // end of switch statement
297 296
  while (1) {
298 297
    if (FLAG) {
......
310 309
    WL_DEBUG_PRINT("basic buffer full\r\n");
311 310
    return -1;
312 311
  }
313
  xbee_basic_buf[(*ptr)++] = byte);
312
  xbee_basic_buf[(*ptr)++] = byte;
314 313
  if (*ptr == PACKET_BUFFER_SIZE)
315 314
    *ptr = 0;
316 315
  return 0;
......
330 329
    return -1;
331 330
  }
332 331
  xbee_other_buf[(*ptr)++] = byte;
333
  if (ptr == PACKET_BUFFER_SIZE)
332
  if (*ptr == PACKET_BUFFER_SIZE)
334 333
    *ptr = 0;
335 334
  return 0;
336 335
}
......
343 342
{
344 343
	WL_DEBUG_PRINT("in xbee_init\n");
345 344
  
346
  if(xbee_status&0x08 == XBEE_INITD) {
345
  if((xbee_status&0x08) == XBEE_INITD) {
347 346
    return WL_ERROR_INIT_ALREADY_INITD;
348 347
  }
349 348
  
......
368 367

  
369 368
	WL_DEBUG_PRINT("Entering command mode.\r\n");
370 369
	if (xbee_enter_command_mode() != 0) {
371
    usb_puts("error entering command mode\r\n");
370
    WL_DEBUG_PRINT("error entering command mode\r\n");
372 371
		return -1;
373 372
	}
374 373
	WL_DEBUG_PRINT("Entered command mode.\r\n");
......
380 379
  
381 380
  // set baud on xbee
382 381
#if (XBEE_BAUD == 115200)
383
  xbee_send_string("ATBD7\r");
382
  xbee_send_string((uint8_t*)"ATBD7\r");
383
  xbee_send_string((uint8_t*)"ATBD7\r");
384 384
#elif (XBEE_BAUD == 57600)
385
  xbee_send_string("ATBD6\r");
385
  xbee_send_string((uint8_t*)"ATBD6\r");
386 386
#elif (XBEE_BAUD == 38400)
387
  xbee_send_string("ATBD5\r");
387
  xbee_send_string((uint8_t*)"ATBD5\r");
388 388
#elif (XBEE_BAUD == 19200)
389
  xbee_send_string("ATBD4\r");
389
  xbee_send_string((uint8_t*)"ATBD4\r");
390 390
#elif (XBEE_BAUD == 9600)
391 391
  // already at this baud rate
392 392
  //xbee_send_string("ATBD3\r\n");
......
397 397
  // exit command mode
398 398
  xbee_wait_for_ok();
399 399
  WL_DEBUG_PRINT("got ok from baud reset\r\n");
400
  xbee_send_string("ATCN\r");
400
  xbee_send_string((uint8_t*)"ATCN\r");
401 401
  xbee_wait_for_ok();
402 402
  WL_DEBUG_PRINT("got ok from exiting command mode\r\n");
403 403
  
......
449 449

  
450 450
  // enter command mode
451 451
  WL_DEBUG_PRINT("entering command mode 2\r\n");
452
  xbee_send_string("+++");
452
  xbee_send_string((uint8_t*)"+++");
453 453
  xbee_wait_for_ok();
454 454
  WL_DEBUG_PRINT("entered command mode 2\r\n");
455 455
  
......
495 495
 * @param c the byte to send
496 496
 * @return 0 for success, nonzero for failure
497 497
 **/
498
int xbee_putc(char c) {
498
int8_t xbee_putc(uint8_t c) {
499 499
  if(!(xbee_status&XBEE_INITD))
500 500
    return WL_ERROR_LIBRARY_NOT_INITD;
501 501

  
......
573 573
 **/
574 574
static int8_t xbee_send(uint8_t* buf, uint16_t size)
575 575
{
576
	while(--size > 0)
577
		if (xbee_putc(buf[i]) != WL_SUCCESS)
576
  uint16_t i=0; // check if we need this variable
577
	while(i<size) {
578
		if (xbee_putc(buf[i++]) != WL_SUCCESS)
578 579
      return WL_ERROR_SEND;
579 580
	}  
580 581
  if (xbee_putc(buf[0]) != WL_SUCCESS)
......
588 589
 *
589 590
 * @param c the string to send to the XBEE
590 591
 **/
591
static int8_t xbee_send_string(unsigned char* c)
592
static int8_t xbee_send_string(uint8_t* c)
592 593
{
593
	return xbee_send(c, strlen(c));
594
	return xbee_send(c, strlen((char*)c));
594 595
}
595 596

  
596 597

  
597 598
/**
598 599
 * Enter into command mode.
599 600
 **/
600
static int8_t xbee_enter_command_mode()
601
static int8_t xbee_enter_command_mode(void)
601 602
{
602
	if (xbee_send_string("+++") != WL_SUCCESS) {
603
	if (xbee_send_string((uint8_t*)"+++") != WL_SUCCESS) {
603 604
		return WL_ERROR_XBEE_COMMAND;
604 605
	}
605 606

  
......
615 616
 **/
616 617
static int8_t xbee_exit_command_mode()
617 618
{
618
	if (xbee_send_string("ATCN\r") != 0) {
619
	if (xbee_send_string((uint8_t*)"ATCN\r") != 0) {
619 620
		return WL_ERROR_SEND;
620 621
	}
621 622
	xbee_wait_for_ok();
......
626 627
/**
627 628
 * Enter API mode.
628 629
 **/
629
static int8_t xbee_enter_api_mode()
630
{
631
	if (xbee_send_string("ATAP 1\r") != 0) {
630
static int8_t xbee_enter_api_mode(void) {
631
	if (xbee_send_string((uint8_t*)"ATAP 1\r") != 0) {
632 632
		return WL_ERROR_SEND;
633 633
	}
634 634
	xbee_wait_for_ok();
......
641 641
/**
642 642
 * Enter API mode 2.
643 643
 **/
644
static int8_t xbee_enter_api_mode2()
645
{
646
	if (xbee_send_string("ATAP 2\r") != 0) {
644
static int8_t xbee_enter_api_mode2(void) {
645
	if (xbee_send_string((uint8_t*)"ATAP 2\r") != 0) {
647 646
		return WL_ERROR_SEND;
648 647
	}
649 648
	xbee_wait_for_ok();
......
658 657
 **/
659 658
static int8_t xbee_exit_api_mode()
660 659
{
661
	if (xbee_status&0xC0 == XBEE_COMMAND_WAIT
662
    || xbee_status&0xC0 == XBEE_COMMAND_RESPONSE)
660
	if ((xbee_status&0xC0) == XBEE_COMMAND_WAIT
661
    || (xbee_status&0xC0) == XBEE_COMMAND_RESPONSE)
663 662
    return WL_ERROR_XBEE_COMMAND; // can't do command right now
664 663
  
665 664
  int16_t i=0;
666 665
  // change status to command wait
667 666
  xbee_status = (xbee_status&0x3F)|XBEE_COMMAND_WAIT;
668
  xbee_send_modify_at_command("AP","0",1); // send command
667
  xbee_send_modify_at_command((uint8_t*)"AP",(uint8_t*)"0",1); // send command
669 668
  // wait for up to 30 ms
670
  while(xbee_status&0xC0 != XBEE_COMMAND_RESPONSE && i++ < 10000) {
669
  while((xbee_status&0xC0) != XBEE_COMMAND_RESPONSE && i++ < 10000) {
671 670
    delay_us(1); // wait 3us
672 671
  }
673
  if (i < 1000 && xbee_command[0] == 'A' && xbee_command[1] == 'P') {
672
  if (i < 1000 && xbee_command[0] == 'A' && xbee_command[1] == 'P')
674 673
    i = WL_SUCCESS;
675 674
  else
676 675
    i = WL_ERROR_XBEE_COMMAND; // set error code
......
681 680
/**
682 681
 * Wait until the string "OK\r" is received from the XBee.
683 682
 **/
684
static int xbee_wait_for_ok()
683
static int8_t xbee_wait_for_ok()
685 684
{
686
	return xbee_wait_for_string("OK\r", 3);
685
	return xbee_wait_for_string((uint8_t*)"OK\r", 3);
687 686
}
688 687

  
689 688
/**
......
695 694
 * @param s the string to receive
696 695
 * @param len the length of the string
697 696
 **/
698
static int8_t xbee_wait_for_string(char* s, int len)
697
static int8_t xbee_wait_for_string(uint8_t* s, uint16_t len)
699 698
{
700 699
  uint8_t i=0;
701
  if (xbee_status&0x03 == XBEE_API_OFF) {
700
  if ((xbee_status&0x03) == XBEE_API_OFF) {
702 701
    // wait until the response is received (only wait 1 second)
703
    while(xbee_status&0xC0 != XBEE_COMMAND_RESPONSE && i++ < 1000) {
702
    while((xbee_status&0xC0) != XBEE_COMMAND_RESPONSE && i++ < 1000) {
704 703
      delay_us(1);
705 704
    }
706 705
    // check response
......
728 727
 * @param s the string to store the response in
729 728
 * @param len the length of the string
730 729
 */
731
static int8_t xbee_wait_for_response(char* s, int len) {
730
static int8_t xbee_wait_for_response(uint8_t* s, int16_t len) {
732 731
  uint8_t i=0;
733
  if (xbee_status&0x03 == XBEE_API_OFF) {
732
  if ((xbee_status&0x03) == XBEE_API_OFF) {
734 733
    // wait until the response is received (only wait 1 second)
735
    while(xbee_status&0xC0 != XBEE_COMMAND_RESPONSE && i++ < 1000) {
734
    while((xbee_status&0xC0) != XBEE_COMMAND_RESPONSE && i++ < 1000) {
736 735
      delay_us(1);
737 736
    }
738 737
    // check response
739
    if (i >= 1000)
738
    if (i >= 1000) {
740 739
      return -1;
741 740
    } else {
742
      i=strcspn(xbee_basic_buf,"\r");
741
      i=strcspn((char*)xbee_basic_buf,"\r");
743 742
      if (i<PACKET_BUFFER_SIZE) {
744 743
        memcpy(s,xbee_basic_buf,i);
745 744
        xbee_status = xbee_status&0x3F; // clear response
......
768 767
 * @return 0 if the checksum is incorrect, nonzero
769 768
 * otherwise
770 769
 **/
771
uint8_t xbee_verify_checksum(uint8_t* packet, uint16_t len)
770
int8_t xbee_verify_checksum(uint8_t* packet, uint16_t len)
772 771
{
773 772
	uint8_t sum = 0;
774 773
	while(--len > 0) {
......
834 833
  if (xbee_putc((uint8_t)(len & 0x00FF)) != WL_SUCCESS)
835 834
    return WL_ERROR_SEND;
836 835

  
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 836
	return WL_SUCCESS;
844 837
}
845 838

  
......
988 981
 *
989 982
 * @param status the type of status packet received.
990 983
 **/
991
void xbee_handle_status(char status)
984
void xbee_handle_status(uint8_t status)
992 985
{
993 986
	switch (status)
994 987
	{
......
1075 1068
 **/
1076 1069
int8_t xbee_set_pan_id(uint16_t id)
1077 1070
{
1078
	if (xbee_status&0xC0 == XBEE_COMMAND_WAIT
1079
    || xbee_status&0xC0 == XBEE_COMMAND_RESPONSE)
1071
	if ((xbee_status&0xC0) == XBEE_COMMAND_WAIT
1072
    || (xbee_status&0xC0) == XBEE_COMMAND_RESPONSE)
1080 1073
    return WL_ERROR_XBEE_COMMAND; // can't do command right now
1081 1074
  
1082 1075
  int16_t i=0;
1083 1076
  // change status to command wait
1084 1077
  xbee_status = (xbee_status&0x3F)|XBEE_COMMAND_WAIT;
1085
  xbee_send_modify_at_command("ID",id,2); // send command to set the channel
1078
  xbee_send_modify_at_command((uint8_t*)"ID",(uint8_t*)(&id),2); // send command to set the channel
1086 1079
  // wait for up to 30 ms
1087
  while(xbee_status&0xC0 != XBEE_COMMAND_RESPONSE && i++ < 10000) {
1080
  while((xbee_status&0xC0) != XBEE_COMMAND_RESPONSE && i++ < 10000) {
1088 1081
    delay_us(1); // wait 3us
1089 1082
  }
1090
  if (i < 1000 && xbee_command[0] == 'O' && xbee_command[1] == 'K') {
1083
  if (i < 1000 && xbee_command[0] == 'O' && xbee_command[1] == 'K')
1091 1084
    i = WL_SUCCESS;
1092 1085
  else
1093 1086
    i = WL_ERROR_XBEE_COMMAND; // set error code
......
1103 1096
 **/
1104 1097
uint16_t xbee_get_pan_id()
1105 1098
{
1106
  if (xbee_status&0xC0 == XBEE_COMMAND_WAIT
1107
    || xbee_status&0xC0 == XBEE_COMMAND_RESPONSE)
1099
  if ((xbee_status&0xC0) == XBEE_COMMAND_WAIT
1100
    || (xbee_status&0xC0) == XBEE_COMMAND_RESPONSE)
1108 1101
    return WL_ERROR_XBEE_COMMAND_16BIT; // can't do command right now
1109 1102
  
1110 1103
  uint16_t i=0;
1111 1104
  // change status to command wait
1112 1105
  xbee_status = (xbee_status&0x3F)|XBEE_COMMAND_WAIT;
1113
  xbee_send_read_at_command("ID"); // send command to get the PAN
1106
  xbee_send_read_at_command((uint8_t*)"ID"); // send command to get the PAN
1114 1107
  // wait for up to 30 ms
1115
  while(xbee_status&0xC0 != XBEE_COMMAND_RESPONSE && i++ < 10000) {
1108
  while((xbee_status&0xC0) != XBEE_COMMAND_RESPONSE && i++ < 10000) {
1116 1109
    delay_us(1); // wait 3us
1117 1110
  }
1118
  if (i < 1000 && xbee_command[0] == 'I' && xbee_command[1] == 'D') {
1111
  if (i < 1000 && xbee_command[0] == 'I' && xbee_command[1] == 'D')
1119 1112
    i = (xbee_command[2]<<8)|xbee_command[3]; // get PAN
1120 1113
  else
1121 1114
    i = WL_ERROR_XBEE_COMMAND_16BIT; // set error code
......
1139 1132
		return -1;
1140 1133
	}
1141 1134

  
1142
	if (xbee_status&0xC0 == XBEE_COMMAND_WAIT
1143
    || xbee_status&0xC0 == XBEE_COMMAND_RESPONSE)
1135
	if ((xbee_status&0xC0) == XBEE_COMMAND_WAIT
1136
    || (xbee_status&0xC0) == XBEE_COMMAND_RESPONSE)
1144 1137
    return WL_ERROR_XBEE_COMMAND; // can't do command right now
1145 1138
  
1146 1139
  int16_t i=0;
1147 1140
  // change status to command wait
1148 1141
  xbee_status = (xbee_status&0x3F)|XBEE_COMMAND_WAIT;
1149
  xbee_send_modify_at_command("CH",channel,1); // send command to set the channel
1142
  xbee_send_modify_at_command((uint8_t*)"CH",&channel,1); // send command to set the channel
1150 1143
  // wait for up to 30 ms
1151 1144
  while((xbee_status&0xC0) != XBEE_COMMAND_RESPONSE && i++ < 10000) {
1152 1145
    delay_us(1); // wait 3us
......
1175 1168
  int16_t i=0;
1176 1169
  // change status to command wait
1177 1170
  xbee_status = (xbee_status&0x3F)|XBEE_COMMAND_WAIT;
1178
  xbee_send_read_at_command("ID"); // send command to get the channel
1171
  xbee_send_read_at_command((uint8_t*)"ID"); // send command to get the channel
1179 1172
  // wait for up to 30 ms
1180 1173
  while((xbee_status&0xC0) != XBEE_COMMAND_RESPONSE && i++ < 10000) {
1181 1174
    delay_us(1); // wait 3us
......
1204 1197
  uint16_t i=0;
1205 1198
  // change status to command wait
1206 1199
  xbee_status = (xbee_status&0x3F)|XBEE_COMMAND_WAIT;
1207
  xbee_send_read_at_command("MY"); // send command to get the address
1200
  xbee_send_read_at_command((uint8_t*)"MY"); // send command to get the address
1208 1201
  // wait for up to 30 ms
1209 1202
  while((xbee_status&0xC0) != XBEE_COMMAND_RESPONSE && i++ < 10000) {
1210 1203
    delay_us(1); // wait 3us

Also available in: Unified diff