Project

General

Profile

Revision 1591

wireless: update to the receive buffers

View differences:

branches/wireless/code/projects/libwireless/wireless_receive.c
38 38
#include "wireless.h"
39 39
#include "xbee.h"
40 40

  
41
// the receive functions
42

  
43 41
/**
44
 * @addtogroup wireless Wireless
45
 * @{
42
 * Definition for wireless library receive packet structure
43
 *
44
 * Basic Buffer Packet:
45
 *   byte 1: length of data
46
 *   bytes 2-3: source
47
 *   bytes 4-n: data
48
 *
49
 * Other Buffer Packet:
50
 *   byte 1: length of data
51
 *   byte 2: group number
52
 *   bytes 3-4: source
53
 *   bytes 5-n: data
54
 *
46 55
 **/
47 56

  
48 57
extern uint8_t xbee_basic_buf[PACKET_BUFFER_SIZE];  // buffer for basic-group packets
......
51 60
extern uint8_t basic_buf_last;	// end of last packet in basic buffer
52 61
extern uint8_t other_buf_first; // beginning of first packet in other buffer
53 62
extern uint8_t other_buf_last;  // end of last packet in other buffer
63
 
64
 
65
// the receive functions
54 66

  
55 67
/**
68
 * @addtogroup wireless Wireless
69
 * @{
70
 **/
71

  
72
/**
56 73
 * the main receive function (similar to wl_do)
57 74
 * 
58 75
 * when called, this function will receive the next packet on the default packet group
branches/wireless/code/projects/libwireless/xbee.c
110 110
volatile int buffer_first = 0;
111 111

  
112 112

  
113
//used to store packets as they are read
113
// old xbee buf
114 114
static char xbee_buf[PACKET_BUFFER_SIZE];
115
static int currentBufPos = 0;
116

  
117
// array for basic packets
115 118
static uint8_t xbee_basic_buf[PACKET_BUFFER_SIZE];
119

  
120
// beginning of first packet in basic buffer
121
static uint8_t basic_buf_first = 0;
122

  
123
// byte after end of last packet in basic buffer (only access < basic_buf_last)
124
// aka, the first free byte in basic buffer
125
static uint8_t basic_buf_last = 0;
126

  
127
// array for other packets
116 128
static uint8_t xbee_other_buf[PACKET_BUFFER_SIZE];
117
static int currentBufPos = 0;
118
static uint8_t basic_buf_first = 0;	// beginning of first packet in basic buffer
119
static uint8_t basic_buf_last = 0;	// end of last packet in basic buffer
120
static uint8_t other_buf_first = 0; // beginning of first packet in other buffer
121
static uint8_t other_buf_last = 0;  // end of last packet in other buffer
122 129

  
130
// beginning of first packet in other buffer
131
static uint8_t other_buf_first = 0;
132

  
133
// byte after end of last packet in other buffer (only access < other_buf_last)
134
// aka, the first free byte in other buffer
135
static uint8_t other_buf_last = 0;
136

  
123 137
//XBee status
124 138
static unsigned int xbee_panID = XBEE_PAN_DEFAULT;
125 139
static unsigned int xbee_pending_panID = XBEE_PAN_DEFAULT;
......
128 142
static volatile unsigned int xbee_address = 0;
129 143

  
130 144

  
145
// external ack handler (wireless_send.c)
146
extern void ackhandle(uint8_t num,uint8_t val);
147

  
131 148
/**@addtogroup xbee
132 149
 * @{ **/
133 150

  
......
141 158
 * to the buffer.
142 159
 **/
143 160
#ifndef FIREFLY
161
#define PORT UDR1
162
#define FLAG RXC1
144 163
ISR(USART1_RX_vect)
145
{
146
	char c = UDR1;
147
	arrival_buf[buffer_last] = c;
148
	int t = buffer_last + 1;
149
	if (t == XBEE_BUFFER_SIZE)
150
		t = 0;
151
	if (t == buffer_first)
152
	{
153
		WL_DEBUG_PRINT("\nOut of space in buffer.\n");
154
	}
155
	buffer_last = t;
156
}
157 164
#else
165
#define PORT UDR0
166
#define FLAG RXC0
158 167
SIGNAL(SIG_USART0_RECV)
168
#endif
159 169
{
160
	char c = UDR0;
161
	arrival_buf[buffer_last] = c;
162
	int t = buffer_last + 1;
163
	if (t == XBEE_BUFFER_SIZE)
164
		t = 0;
165
	if (t == buffer_first)
166
	{
167
		WL_DEBUG_PRINT("Out of space in buffer.\n");
168
	}
169
	buffer_last = t;
170
}
171
#endif
170
  // start of frame
171
	uint8_t apitype = PORT; // get frame start byte
172
  uint16_t i=0;
173
  uint16_t len=0;
174
  // get length and type
175
  while(i<3) {
176
    if (FLAG) {
177
      if (i==0)
178
        len |= PORT<<8;
179
      else if (i==1)
180
        len |= PORT;
181
      else if (i==2)
182
        apitype = PORT;
183
      i++;
184
    }
185
  }
186
  
187
  // do something based on the type
188
  i=1;
189
  switch(apitype) {
190
  case 0x88:
191
    // AT command response
192
    uint16_t atcommand=0;
193
    uint8_t ptr=basic_buf_last;
194
    while(i<len) {
195
      if (FLAG) {
196
        if (i==1)
197
          apitype = PORT; // get frame id, but ignore it
198
        else if (i==2)
199
          atcommand |= PORT<<8; // get command char1
200
        else if (i==3)
201
          atcommand |= PORT; // get command char2
202
        else if (i==4)
203
          apitype = PORT; // get status
204
        else {
205
          // put the command response on the basic buf temporarily
206
          if (ptr == basic_buf_first) {
207
            // buffer full
208
            WL_DEBUG_PRINT("basic buffer full\r\n");
209
            break;
210
          }
211
          xbee_basic_buf[ptr++]=PORT;
212
          if (ptr == PACKET_BUFFER_SIZE)
213
            ptr = 0;
214
        }
215
        i++;
216
      }
217
    }
218
    // handle AT command
219
    xbee_handle_at_command_response(atcommand,apitype,xbee_basic_buf+basic_buf_last,len-5);
220
    break;
221
  case 0x89:
222
    // TX status
223
    uint8_t frameid=0;
224
    while(i<len) {
225
      if (FLAG) {
226
        if (i==1)
227
          frameid = PORT;
228
        else {
229
          ackhandle(frameid,PORT); // handle the status
230
          break;
231
        }
232
        i++;
233
      }
234
    }
235
    break;
236
  case 0x80:
237
    // receive a packet with 64bit address
238
    break; // not implemented yet
239
  case 0x81:
240
    // receive a packet with 16bit address
241
    uint16_t source = 0;
242
    uint8_t framenum = 0;
243
    uint8_t group = 0;
244
    uint8_t ptr=basic_buf_last;
245
    while(i<len) {
246
      if (FLAG) {
247
        if (i==1)
248
          source |= PORT<<8; // get source hi byte
249
        else if (i==2)
250
          source |= PORT; // get source lo byte
251
        else if (i==3)
252
          apitype = PORT; // get RSSI, and ignore
253
        else if (i==4)
254
          apitype = PORT; // get options, and ignore
255
        else if (i==5) {
256
          framenum = PORT; // get the frame number
257
          if (check_last_receive(source,framenum) != 0) {  // TODO: write function
258
            // we've already received this frame
259
            ptr = 0xFF; // signal to skip processing
260
            break;
261
          }
262
        }
263
        else if (i==6) {
264
          group = PORT; // get group number
265
          if (group == 0) {
266
            ptr = basic_buf_last+1;
267
            // add source to buffer
268
            if (ptr == basic_buf_first) {
269
              // buffer full
270
              WL_DEBUG_PRINT("basic buffer full\r\n");
271
              break;
272
            }
273
            xbee_basic_buf[ptr++] = (uint8_t)((source&0xFF00)>>8);
274
            if (ptr == PACKET_BUFFER_SIZE)
275
              ptr = 0;
276
            if (ptr == basic_buf_first) {
277
              // buffer full
278
              WL_DEBUG_PRINT("basic buffer full\r\n");
279
              break;
280
            }
281
            xbee_basic_buf[ptr++] = (uint8_t)(source&0x00FF);
282
            if (ptr == PACKET_BUFFER_SIZE)
283
              ptr = 0;
284
          } else {
285
            ptr = other_buf_last+1;
286
            // add source and group to buffer            
287
            if (ptr == other_buf_first) {
288
              // buffer full
289
              WL_DEBUG_PRINT("other buffer full\r\n");
290
              break;
291
            }
292
            xbee_other_buf[ptr++] = group;
293
            if (ptr == PACKET_BUFFER_SIZE)
294
              ptr = 0;
295
            if (ptr == other_buf_first) {
296
              // buffer full
297
              WL_DEBUG_PRINT("other buffer full\r\n");
298
              break;
299
            }
300
            xbee_other_buf[ptr++] = (uint8_t)((source&0xFF00)>>8);
301
            if (ptr == PACKET_BUFFER_SIZE)
302
              ptr = 0;
303
            if (ptr == other_buf_first) {
304
              // buffer full
305
              WL_DEBUG_PRINT("other buffer full\r\n");
306
              break;
307
            }
308
            xbee_other_buf[ptr++] = (uint8_t)(source&0x00FF);
309
            if (ptr == PACKET_BUFFER_SIZE)
310
              ptr = 0;
311
          }
312
        }
313
        else {
314
          // put packet data on the correct buffer
315
          if (group == 0) {
316
            if (ptr == basic_buf_first) {
317
              // buffer full
318
              WL_DEBUG_PRINT("basic buffer full\r\n");
319
              break;
320
            }
321
            xbee_basic_buf[ptr++]=PORT;
322
            if (ptr == PACKET_BUFFER_SIZE)
323
              ptr = 0;
324
          } else {
325
            if (ptr == other_buf_first) {
326
              // buffer full
327
              WL_DEBUG_PRINT("other buffer full\r\n");
328
              break;
329
            }
330
            xbee_other_buf[ptr++]=PORT;
331
            if (ptr == PACKET_BUFFER_SIZE)
332
              ptr = 0;
333
          }
334
        }
335
        i++;
336
      }
337
    }
338
    if (ptr != 0xFF) {
339
      if (group == 0) {
340
        xbee_basic_buf[basic_buf_last] = i-6; // set length
341
        basic_buf_last = ptr;
342
      }
343
      else {        
344
        xbee_other_buf[other_buf_last] = i-6; // set length
345
        // check if we have a high priority group
346
        for(;;)
347
          if (HIGH_PRIORITY) {
348
            // handle receive now
349
            ptr = 0xFF;
350
            break;
351
          }        
352
        if (ptr != 0xFF) {
353
          // handle receive later
354
          other_buf_last = ptr;
355
        }
356
      }
357
    }
358
    break;
359
  }
360
} // end of interrupt
172 361

  
173 362
#else
174 363

  
branches/wireless/code/projects/libwireless/wireless_send.c
39 39

  
40 40

  
41 41
/**
42
 * Definition for wireless library packet structure
42
 * Definition for wireless library send packet structure
43 43
 * byte 1: length of packet
44 44
 * byte 2: frame number
45 45
 * byte 3: group code 
......
257 257
  }
258 258
}
259 259

  
260

  
261
/* ack handler */
262
void ackhandle(uint8_t num,uint8_t val) {
263
  switch(val) {
264
  case 0:
265
    // success
266
    setack(num,ACK_OK);
267
    break;
268
  case 1:
269
    // no ack
270
    setack(num,ACK_FAILURE);
271
    break;
272
  case 2:
273
    // CCA failure
274
    setack(num,CCA_FAILURE);
275
    break;
276
}
277

  

Also available in: Unified diff