Project

General

Profile

Revision 1825

Wireless: updated library to make it more readable and to reduce possible errors with acking (untested changes, but shouldn't be broken or something is very wrong).

View differences:

branches/wireless/code/projects/libwireless/xbee.c
31 31
 *
32 32
 * @author Colony Project, CMU Robotics Club
33 33
 **/
34
// TODO Why are we including lights? Because lights are awesome. No, really, figure this out.
34 35
#include <lights.h>
35 36

  
36 37
#include <string.h>
......
123 124
#define XBEE_COMMAND_MASK 0xC0
124 125
#define XBEE_NOT_INITD 0xF0
125 126
#define LAST_PACKET_MASK 0x0F
126
uint8_t xbee_status = XBEE_NOT_INITD;
127
uint8_t xbee_status = XBEE_NOT_INITD; //This is the super-byte
127 128

  
128 129
// xbee command response (for PAN, channel, address, etc)
129 130
static uint8_t xbee_command[8];
......
198 199
    
199 200
  // get length and type
200 201
  while(i<3) {
201
    if (FLAG) {
202
    if (FLAG) { //if there is a byte to read //checks for escape characters in API mode 2. This repeats below (should be a function)
202 203
      if (i==0) {
203
        if (getStatus(XBEE_API_MASK) == XBEE_API_ESCAPE
204
            && apitype == ESCAPE_MARKER)
204
        if (getStatus(XBEE_API_MASK) == XBEE_API_ESCAPE             && apitype == ESCAPE_MARKER) //apitype is used here as dummy var
205 205
          apitype = PORT ^ ESCAPE_XOR;
206 206
        else {
207 207
          apitype = PORT;
......
250 250
    uint8_t ptr=basic_buf_last;
251 251
    uint8_t status=0;
252 252
    while(i<len) {
253
      if (FLAG) {
253
      if (FLAG) {  //more escape characters
254 254
        if (i==1) {
255 255
          if (getStatus(XBEE_API_MASK) == XBEE_API_ESCAPE
256 256
              && apitype == ESCAPE_MARKER)
......
332 332
    for(uint8_t j=0;j<(len-5);j++)
333 333
      WL_DEBUG_PRINT_HEX(xbee_basic_buf_get(&ptr2));
334 334
    WL_DEBUG_PRINT_P("|\r\n");
335
	//end of DEBUG_PRINT
335 336
    xbee_handle_at_command_response(atcommand,status,len-5);
336 337
    break; }
337 338
  case XBEE_FRAME_STATUS: {
......
379 380
                && apitype == ESCAPE_MARKER)
380 381
              continue; // get escaped character
381 382
          }
382
          ackhandle(frame_id,apitype); // handle the status
383
          ackhandle(frame_id,apitype); // handle the status (stored in apitype)
383 384
          break;
384 385
        }
385 386
        i++;
......
481 482
            WL_DEBUG_PRINT_P("|ptr2=");
482 483
            WL_DEBUG_PRINT_INT(ptr);
483 484
          
484
          if (group == 0) {
485
          if (group == 0) { //writes the packet to the buffer
485 486
            WL_DEBUG_PRINT_P("|last1=");
486 487
            WL_DEBUG_PRINT_INT(basic_buf_last);
487 488
            WL_DEBUG_PRINT_P("|ptr1=");
......
720 721

  
721 722
/** status functions **/
722 723
inline uint8_t getStatus(uint8_t mask) { return xbee_status&mask; }
723
void setStatus(uint8_t mask,uint8_t value) { xbee_status = ((xbee_status&(~mask))|value); }
724
void setStatus(uint8_t mask,uint8_t value) {
725
  xbee_status = ((xbee_status&(~mask))|value); //xbee is the super-byte
726
}
724 727

  
725 728

  
726 729
/**
......
751 754
  // Set startup baud rate of 9600
752 755
  // Set frame format: 8data, 1stop bit, asynchronous normal mode
753 756
  // Enable receiver and transmitter and the receiving interrupt
757
  // DO NOT TOUCH
754 758
#ifdef FIREFLY
755 759
  UBRR0H = 0x00;
756 760
  UBRR0L = 103;
......
1099 1103
}
1100 1104

  
1101 1105
/**
1102
 * Enter API mode.
1106
 * Enter API mode 1.
1103 1107
 **/
1104 1108
static int8_t xbee_enter_api_mode(void) {
1105 1109
  if (xbee_send_string((uint8_t*)"ATAP 1\r") != 0) {
......
1236 1240
  if (getStatus(XBEE_API_MASK) == XBEE_API_OFF) {
1237 1241
    // wait until the response is received (only wait 1 second)
1238 1242
    while(getStatus(XBEE_COMMAND_MASK) != XBEE_COMMAND_RESPONSE && i++ < 1000) {
1239
      delay_us(1);
1243
      delay_us(1); //3 us
1240 1244
    }
1241 1245
    // check response
1242 1246
    if (i >= 1000) {
......
1411 1415
}
1412 1416

  
1413 1417
/**
1414
 * Send the specified packet.
1418
 * Send the specified packet. (wl_send depreciates this)
1415 1419
 *
1416 1420
 * @param packet the packet data to send
1417 1421
 * @param len the number of bytes in the packet
......
1647 1651
/**
1648 1652
 * Set the channel the XBee is using.
1649 1653
 *
1650
 * @param channel the channel the XBee will not use,
1654
 * @param channel the channel the XBee will use,
1651 1655
 * between 0x0B and 0x1A
1652 1656
 *
1657
 * @reutrn success or error code
1658
 *
1653 1659
 * @see xbee_get_channel
1654 1660
 **/
1655 1661
int8_t xbee_set_channel(uint8_t channel)
branches/wireless/code/projects/libwireless/wireless_send.c
99 99
  packet[3] = XBEE_OPTIONS_NONE;
100 100
  packet[4] = nextframe;
101 101
  packet[5] = group;
102
  nextframe = (nextframe == 0xFF)?1:nextframe+1; // increment frame number
103 102
  
104
  // set scope
103
  // set scope, defaults to send to PAN
105 104
  if (scope == GLOBAL)
106
    packet[3] &= XBEE_OPTIONS_BROADCAST_ALL_PANS;
105
    packet[3] |= XBEE_OPTIONS_BROADCAST_ALL_PANS;
107 106
  else if (scope != PAN) {
108 107
    WL_DEBUG_PRINT_P("Error - bad scope in core send function\r\n");
109 108
    return WL_ERROR_SCOPE;
110 109
  }
111 110
    
112
  // set mode
111
  // set mode, defaults to RELIABLE
113 112
  if (mode == FAST) {
114
    packet[3] &= XBEE_OPTIONS_DISABLE_RESPONSE;
113
    packet[3] |= XBEE_OPTIONS_DISABLE_RESPONSE;
115 114
  } else if (mode != RELIABLE) {
116 115
    WL_DEBUG_PRINT_P("Error - bad mode in core send function\r\n");
117 116
    return WL_ERROR_MODE;
......
152 151
    return ret; // no frame number
153 152
  } else if (mode == RELIABLE) {
154 153
    setack(nextframe,SENDING); // set status to SENDING
154
    nextframe = (nextframe == 0xFF)?1:nextframe+1; // increment frame number
155 155
    // save packet on sending buffer
156 156
    scope = send_buf_last; // use as ptr to send buffer    
157 157
    if (send_buf_add(&scope,length+6) != WL_SUCCESS) { // add length
158 158
      WL_DEBUG_PRINT_P("Error adding length to sending buffer\r\n");
159
      ret = WL_ERROR_SENDING_BUFFER_FULL;
159
      return WL_ERROR_SENDING_BUFFER_FULL;
160 160
    }    
161
    for(mode=0;mode<6;mode++) { // add header
161
    for(mode=0;mode<6;mode++) { // add header //mode is being used as a counter to save space
162 162
      if (send_buf_add(&scope,packet[mode]) != WL_SUCCESS) {
163 163
        WL_DEBUG_PRINT_P("Error adding header to sending buffer\r\n");
164
        ret = WL_ERROR_SENDING_BUFFER_FULL;
164
        return WL_ERROR_SENDING_BUFFER_FULL;
165 165
      }
166 166
    }
167
    for(mode=0;mode<length;mode++) { // add data
167
    for(mode=0;mode<length;mode++) { // add data //mode is being used as a counter to save space
168 168
      if (send_buf_add(&scope,data[mode]) != WL_SUCCESS) {
169 169
        WL_DEBUG_PRINT_P("Error adding data to sendig buffer\r\n");
170
        ret = WL_ERROR_SENDING_BUFFER_FULL;
170
        return WL_ERROR_SENDING_BUFFER_FULL;
171 171
      }
172 172
    }
173 173
    if (send_buf_add(&scope,0) != WL_SUCCESS) { // add num retries=0
174 174
      WL_DEBUG_PRINT_P("Error adding num_retries to sending buffer\r\n");
175
      ret = WL_ERROR_SENDING_BUFFER_FULL;
175
      return WL_ERROR_SENDING_BUFFER_FULL;
176 176
    }
177 177
    send_buf_last = scope;
178 178
    send_buf_num_packets++;
......
254 254
 **/
255 255
uint8_t wl_ack_error(void) {
256 256
  WL_DEBUG_PRINT_P("entering wl_ack_error function");
257
  uint8_t val=0,i=1;
257
  uint8_t val=0,i=1;//i=1 because if i=0, then the xbee won't give an ack
258 258
  
259
  while(1) {
260
    if (((ack_buf[i/4])&(0x2<<(i%4))) != 0) // if the ack is in an error state
259
  for(i = 1; i < 255; i++){
260
	if (((ack_buf[i/4])&(0x2<<(i%4))) != 0) // if the ack is in an error state
261 261
      val++;
262
    if (i==255)
263
      break;
264
    i++;
265
  }  
262
  }
266 263
  WL_DEBUG_PRINT_P("|num_errors:");
267 264
  WL_DEBUG_PRINT_INT(val);
268 265
  WL_DEBUG_PRINT_P("\r\n");
......
297 294
/**
298 295
 * acknowledgement reset
299 296
 * reset the acknowledgement buffer
297
 * TODO: Also needs to reset packet buffer
300 298
 **/
301 299
void wl_ack_reset(void) {
302 300
  WL_DEBUG_PRINT_P("entering wl_ack_reset function\r\n");
......
338 336
}
339 337

  
340 338

  
341
/* ack handler */
339
/* ack handler 
340
 *
341
 * @param num the ack number
342
 * @param val {SUCCESS,ACK_FAILURE,CCA_FAILURE,PURGED}
343
 * 		will be either SUCCESS or FAILURE
344
*/
342 345
void ackhandle(uint8_t num,uint8_t val) {
343 346
  uint8_t len;
344 347
  uint8_t packets = 0;
......
356 359
    // success
357 360
    setack(num,ACK_OK); // set status code
358 361
    // remove from buffer
359
    val = send_buf_first;
362
    val = send_buf_first; //val is no longer the send status
363

  
360 364
    while (1) {
361 365
      len = send_buf_get(&val);
362 366
      WL_DEBUG_PRINT_P("|send_packet_buf_pos:");
branches/wireless/code/projects/libwireless/wl_defs.h
53 53

  
54 54
// ***** TODO: these next two defines should be moved to makefile options
55 55

  
56
//comment out this line if using a computer hooked up to an xbee
57 56
#define ROBOT
58 57

  
59 58
//uncomment this line for debug information
branches/wireless/code/projects/libwireless/xbee.h
87 87
 * @name xbee frame types
88 88
 * @{ **/
89 89

  
90
// TODO: add comments for all of these definitions
90
// TODO: add doxygen comments for all of these definitions
91 91

  
92 92
/*Frame Types*/
93 93
#define XBEE_FRAME_START 0x7E

Also available in: Unified diff