Revision 1587
wireless: core send function and ack function done
branches/wireless/code/projects/libwireless/wireless.h | ||
---|---|---|
76 | 76 |
/**@brief high priority, handle immediately **/ |
77 | 77 |
#define HIGH_PRIORITY UINT8_C(1) |
78 | 78 |
|
79 |
/**@brief packet still in sending phase **/ |
|
80 |
#define SENDING UINT8_C(0) |
|
81 |
|
|
82 |
/**@brief packet was send successfully **/ |
|
83 |
#define ACK_OK UINT8_C(1) |
|
84 |
|
|
85 |
/**@brief packet failure - no acknowledgment **/ |
|
86 |
#define ACK_FAILURE UINT8_C(2) |
|
87 |
|
|
88 |
/**@brief packet failure - network too busy **/ |
|
89 |
#define CCA_FAILURE UINT8_C(3) |
|
90 |
|
|
79 | 91 |
/**@} **/ // end defines group |
80 | 92 |
|
81 | 93 |
|
... | ... | |
112 | 124 |
// the ack function |
113 | 125 |
|
114 | 126 |
/**@brief Returns the number of acknowledgment errors. **/ |
115 |
int8_t wl_ack_error(void); |
|
127 |
uint8_t wl_ack_error(void);
|
|
116 | 128 |
|
117 | 129 |
/**@brief Checks a specific packet for the acknowledgement status. **/ |
118 | 130 |
int8_t wl_ack_check(uint8_t packet); |
branches/wireless/code/projects/libwireless/xbee.c | ||
---|---|---|
59 | 59 |
#include <string.h> |
60 | 60 |
|
61 | 61 |
|
62 |
/**@addtogroup xbee |
|
63 |
* @{ **/ |
|
64 |
|
|
65 |
/**@defgroup xbee_const xbee constants |
|
66 |
* @{ **/ |
|
67 |
|
|
68 |
// TODO: move constants to xbee.h so they can be used publicly |
|
69 |
|
|
70 |
/**@brief The port to use the XBee from on the computer. **/ |
|
71 |
#ifndef ROBOT |
|
72 |
#define XBEE_PORT_DEFAULT "/dev/ttyUSB1" |
|
73 |
#endif |
|
74 |
|
|
75 |
/**@name xbee options |
|
76 |
* @{ **/ |
|
77 |
|
|
78 |
/**@brief Unset PAN, uses XBee default **/ |
|
79 |
#define XBEE_PAN_DEFAULT 0xFFFF |
|
80 |
/**@brief Unset channel, uses XBee default **/ |
|
81 |
#define XBEE_CHANNEL_DEFAULT 0 |
|
82 |
/**@brief Broadcast to all robots in the PAN **/ |
|
83 |
#define XBEE_BROADCAST 0xFFFF |
|
84 |
/**@brief No special options **/ |
|
85 |
#define XBEE_OPTIONS_NONE 0x00 |
|
86 |
/**@brief Do not receive a TX_STATUS message from this packet **/ |
|
87 |
#define XBEE_OPTIONS_DISABLE_RESPONSE 0x01 |
|
88 |
/**@brief Send the packet to all PANS **/ |
|
89 |
#define XBEE_OPTIONS_BROADCAST_ALL_PANS 0x04 |
|
90 |
/**@brief A transmit status packet **/ |
|
91 |
#define XBEE_TX_STATUS 0x89 |
|
92 |
/**@brief A packet received from another XBee **/ |
|
93 |
#define XBEE_RX 0x81 |
|
94 |
|
|
95 |
/**@} |
|
96 |
* @name xbee frame types |
|
97 |
* @{ **/ |
|
98 |
|
|
99 |
// TODO: add comments for all of these definitions |
|
100 |
#define XBEE_FRAME_START 0x7E |
|
101 |
#define XBEE_GET_PACKET_TIMEOUT 1000 |
|
102 |
|
|
103 |
/*Frame Types*/ |
|
104 |
#define XBEE_FRAME_STATUS 0x8A |
|
105 |
#define XBEE_FRAME_AT_COMMAND 0x08 |
|
106 |
#define XBEE_FRAME_AT_COMMAND_RESPONSE 0x88 |
|
107 |
#define XBEE_FRAME_TX_REQUEST_64 0x00 |
|
108 |
#define XBEE_FRAME_TX_REQUEST_16 0x01 |
|
109 |
#define XBEE_FRAME_TX_STATUS XBEE_TX_STATUS |
|
110 |
#define XBEE_FRAME_RX_64 0x80 |
|
111 |
#define XBEE_FRAME_RX_16 XBEE_RX |
|
112 |
|
|
113 |
/** @} **/ |
|
114 |
|
|
115 |
|
|
116 |
// TODO: is this a good size? |
|
117 |
/*Buffer sizes*/ |
|
118 |
#define XBEE_BUFFER_SIZE 128 |
|
119 |
#define PACKET_BUFFER_SIZE 108 |
|
120 |
|
|
121 |
/**@} **/ // end const group |
|
122 |
|
|
123 | 62 |
/*Internal Function Prototypes*/ |
124 | 63 |
|
125 | 64 |
// TODO: convert all int references to int16_t syntax (see stdint.h) |
... | ... | |
183 | 122 |
static volatile unsigned int xbee_address = 0; |
184 | 123 |
|
185 | 124 |
|
125 |
/**@addtogroup xbee |
|
126 |
* @{ **/ |
|
127 |
|
|
128 |
|
|
186 | 129 |
/*Function Implementations*/ |
187 | 130 |
|
188 | 131 |
#ifdef ROBOT |
branches/wireless/code/projects/libwireless/wireless_send.c | ||
---|---|---|
35 | 35 |
|
36 | 36 |
#include "wl_defs.h" |
37 | 37 |
#include "wireless.h" |
38 |
#include <string.h> |
|
38 | 39 |
|
39 | 40 |
|
41 |
/* Definition for wireless library packet structure |
|
42 |
* byte 1: frame number |
|
43 |
* byte 2: group code |
|
44 |
* bytes 3-n: data |
|
45 |
* |
|
46 |
* Definition for ack buffer |
|
47 |
* 2 bit system: 0=still sending |
|
48 |
* 1=OK |
|
49 |
* 2=ack failure |
|
50 |
* 3=CCA failure |
|
51 |
*/ |
|
52 |
|
|
53 |
|
|
54 |
/* global variables */ |
|
55 |
uint8_t nextframe = 1; |
|
56 |
uint8_t ack_buf[64]; |
|
57 |
|
|
58 |
/* private function prototypes */ |
|
59 |
void setack(uint8_t num,uint8_t val); |
|
60 |
|
|
61 |
|
|
40 | 62 |
// the send functions |
41 | 63 |
|
42 | 64 |
/** |
... | ... | |
57 | 79 |
* |
58 | 80 |
* @return positive packet number for tracking acks, or error code (TBD) |
59 | 81 |
**/ |
60 |
int16_t wl_send(char *data, uint8_t length, uint8_t group, uint8_t scope, uint16_t dest, uint8_t mode) { |
|
82 |
int16_t wl_send(uint8_t *data, uint8_t length, uint8_t group, uint8_t scope, uint16_t dest, uint8_t mode) { |
|
83 |
uint8_t packet[length+2]; |
|
84 |
uint8_t options = XBEE_OPTIONS_NONE; |
|
85 |
int16_t ret_val = (int16_t)nextframe; |
|
86 |
|
|
87 |
// build packet |
|
88 |
packet[0] = nextframe; |
|
89 |
packet[1] = group; |
|
90 |
memcpy(packet+2,data,length); |
|
91 |
|
|
92 |
// set options |
|
93 |
if (scope == GLOBAL) |
|
94 |
options &= XBEE_OPTIONS_BROADCAST_ALL_PANS; |
|
95 |
if (mode == FAST) { |
|
96 |
options &= XBEE_OPTIONS_DISABLE_RESPONSE; |
|
97 |
ret_val = 0; |
|
98 |
setack(nextframe,ACK_OK); // save in ack system |
|
99 |
} else if (mode == RELIABLE) { |
|
100 |
// save in ack system |
|
101 |
setack(nextframe,SENDING); // save in ack system |
|
102 |
} else { |
|
103 |
WL_DEBUG_PRINT("Error - bad mode in core send function\r\n"); |
|
104 |
return WL_ERROR_MODE; |
|
105 |
} |
|
61 | 106 |
|
62 |
return 0; |
|
107 |
// send the packet |
|
108 |
if (xbee_send_packet(packet,length,dest,options,(uint_8)ret_val) != 0) { |
|
109 |
WL_DEBUG_PRINT("Error sending packet from core send function\r\n"); |
|
110 |
return WL_ERROR_SEND; |
|
111 |
} |
|
112 |
|
|
113 |
// increment frame number |
|
114 |
nextframe = (nextframe == 0xFF)?1:nextframe+1; |
|
115 |
|
|
116 |
|
|
117 |
return ret_val; // return frame number for ack tracking |
|
63 | 118 |
} |
64 | 119 |
|
65 | 120 |
/** |
... | ... | |
129 | 184 |
* |
130 | 185 |
* @return the # of packets lost (up to 255) |
131 | 186 |
**/ |
132 |
int8_t wl_ack_error(void) { |
|
187 |
uint8_t wl_ack_error(void) { |
|
188 |
uint_8 val=0,i=1; |
|
189 |
|
|
190 |
while(1) { |
|
191 |
if (ack_buf[i/4]&(0x3<<(i%4)) != 0) |
|
192 |
val++; |
|
193 |
if (i==255) |
|
194 |
break; |
|
195 |
i++; |
|
196 |
} |
|
133 | 197 |
|
134 |
return 0;
|
|
198 |
return val;
|
|
135 | 199 |
} |
136 | 200 |
|
137 | 201 |
/** |
138 | 202 |
* acknowledgement error check |
139 | 203 |
* check if a specific packet has been lost |
140 |
* note: buffer will overflow ever 255 packets |
|
204 |
* note: buffer will overflow every 255 packets
|
|
141 | 205 |
* |
142 |
* @param packet number |
|
206 |
* @param packet packet number
|
|
143 | 207 |
* |
144 |
* @return 0=still sending,1=ack received,-1=ack failure
|
|
208 |
* @return {SENDING,ACK_OK,ACK_FAILURE,CCA_FAILURE}
|
|
145 | 209 |
**/ |
146 | 210 |
int8_t wl_ack_check(uint8_t packet) { |
147 |
|
|
148 |
return 0; |
|
211 |
if (packet == 0) { |
|
212 |
// no ack information here |
|
213 |
WL_DEBIG_PRINT("packet number cannot be 0\r\n"); |
|
214 |
return WL_ERROR_ARGUMENT; |
|
215 |
} |
|
216 |
|
|
217 |
// check ack |
|
218 |
return ack_buf[packet/4]&(0x3<<(packet%4)); |
|
149 | 219 |
} |
150 | 220 |
|
151 | 221 |
/** |
... | ... | |
153 | 223 |
* reset the acknowledgement buffer |
154 | 224 |
**/ |
155 | 225 |
void wl_ack_reset(void) { |
156 |
|
|
226 |
memset(ack_buf,0,64); |
|
157 | 227 |
} |
158 | 228 |
|
159 | 229 |
|
... | ... | |
162 | 232 |
|
163 | 233 |
// Define all private functions down here |
164 | 234 |
|
235 |
/* |
|
236 |
* sets 2 bits in the ack_buf |
|
237 |
* |
|
238 |
* @param num the ack number |
|
239 |
* @param val {SENDING,ACK_OK,ACK_FAILURE,CCA_FAILURE} |
|
240 |
*/ |
|
241 |
void setack(uint8_t num,uint8_t val) { |
|
242 |
switch(num%4) { |
|
243 |
case 0: |
|
244 |
ack_buf[num/4] &= (0xFC|val); |
|
245 |
break; |
|
246 |
case 1: |
|
247 |
ack_buf[num/4] &= (0xF3|(val<<2)); |
|
248 |
break; |
|
249 |
case 2: |
|
250 |
ack_buf[num/4] &= (0xCF|(val<<4)); |
|
251 |
break; |
|
252 |
case 3: |
|
253 |
ack_buf[num/4] &= (0x3F|(val<<6)); |
|
254 |
break; |
|
255 |
} |
|
256 |
} |
|
165 | 257 |
|
166 |
|
branches/wireless/code/projects/libwireless/wl_defs.h | ||
---|---|---|
93 | 93 |
#define WL_ERROR_BAD_GROUP INT8_C(-11) |
94 | 94 |
|
95 | 95 |
/** @brief Error code for a bad scope **/ |
96 |
#define WL_ERROR_SCOPE INIT8_C(-12)
|
|
96 |
#define WL_ERROR_SCOPE INT8_C(-12) |
|
97 | 97 |
|
98 | 98 |
/** @brief Error code for a bad robot address **/ |
99 |
#define WL_ERROR_ADDRESS INIT8_C(-13)
|
|
99 |
#define WL_ERROR_ADDRESS INT8_C(-13) |
|
100 | 100 |
|
101 | 101 |
/** @brief Error code for a bad mode **/ |
102 |
#define WL_ERROR_MODE INIT8_C(-14)
|
|
102 |
#define WL_ERROR_MODE INT8_C(-14) |
|
103 | 103 |
|
104 | 104 |
|
105 | 105 |
/**@} */ // end error group |
branches/wireless/code/projects/libwireless/xbee.h | ||
---|---|---|
54 | 54 |
* @{ |
55 | 55 |
**/ |
56 | 56 |
|
57 |
/**@defgroup xbee_const xbee constants |
|
58 |
* @brief These are constants used for the xbee module. |
|
59 |
* @{ **/ |
|
57 | 60 |
|
61 |
// TODO: move constants to xbee.h so they can be used publicly |
|
62 |
|
|
63 |
/**@brief The port to use the XBee from on the computer. **/ |
|
64 |
#ifndef ROBOT |
|
65 |
#define XBEE_PORT_DEFAULT "/dev/ttyUSB1" |
|
66 |
#endif |
|
67 |
|
|
68 |
/**@name xbee options |
|
69 |
* @{ **/ |
|
70 |
|
|
71 |
/**@brief Unset PAN, uses XBee default **/ |
|
72 |
#define XBEE_PAN_DEFAULT 0xFFFF |
|
73 |
/**@brief Unset channel, uses XBee default **/ |
|
74 |
#define XBEE_CHANNEL_DEFAULT 0 |
|
75 |
/**@brief Broadcast to all robots in the PAN **/ |
|
76 |
#define XBEE_BROADCAST 0xFFFF |
|
77 |
/**@brief No special options **/ |
|
78 |
#define XBEE_OPTIONS_NONE 0x00 |
|
79 |
/**@brief Do not receive a TX_STATUS message from this packet **/ |
|
80 |
#define XBEE_OPTIONS_DISABLE_RESPONSE 0x01 |
|
81 |
/**@brief Send the packet to all PANS **/ |
|
82 |
#define XBEE_OPTIONS_BROADCAST_ALL_PANS 0x04 |
|
83 |
/**@brief A transmit status packet **/ |
|
84 |
#define XBEE_TX_STATUS 0x89 |
|
85 |
/**@brief A packet received from another XBee **/ |
|
86 |
#define XBEE_RX 0x81 |
|
87 |
|
|
88 |
/**@} |
|
89 |
* @name xbee frame types |
|
90 |
* @{ **/ |
|
91 |
|
|
92 |
// TODO: add comments for all of these definitions |
|
93 |
#define XBEE_FRAME_START 0x7E |
|
94 |
#define XBEE_GET_PACKET_TIMEOUT 1000 |
|
95 |
|
|
96 |
/*Frame Types*/ |
|
97 |
#define XBEE_FRAME_STATUS 0x8A |
|
98 |
#define XBEE_FRAME_AT_COMMAND 0x08 |
|
99 |
#define XBEE_FRAME_AT_COMMAND_RESPONSE 0x88 |
|
100 |
#define XBEE_FRAME_TX_REQUEST_64 0x00 |
|
101 |
#define XBEE_FRAME_TX_REQUEST_16 0x01 |
|
102 |
#define XBEE_FRAME_TX_STATUS XBEE_TX_STATUS |
|
103 |
#define XBEE_FRAME_RX_64 0x80 |
|
104 |
#define XBEE_FRAME_RX_16 XBEE_RX |
|
105 |
|
|
106 |
/** @} **/ |
|
107 |
|
|
108 |
|
|
109 |
// TODO: is this a good size? |
|
110 |
/*Buffer sizes*/ |
|
111 |
#define XBEE_BUFFER_SIZE 128 |
|
112 |
#define PACKET_BUFFER_SIZE 108 |
|
113 |
|
|
114 |
/**@} **/ // end const group |
|
115 |
|
|
116 |
/** |
|
117 |
* @defgroup xbee_funcs Xbee Functions |
|
118 |
* @brief These are the public xbee functions. |
|
119 |
* @{ |
|
120 |
**/ |
|
121 |
|
|
58 | 122 |
// TODO: convert all int references to int16_t syntax (see stdint.h) |
59 | 123 |
|
60 | 124 |
/**@brief Initialize the XBee library **/ |
... | ... | |
80 | 144 |
/**@brief Reset XBee **/ |
81 | 145 |
int xbee_reset(void); |
82 | 146 |
|
83 |
/**@}**/ //end defgroup
|
|
147 |
/**@} **/ //end xbee_funcs group
|
|
84 | 148 |
|
149 |
/**@} **/ //end defgroup |
|
150 |
|
|
85 | 151 |
#endif |
Also available in: Unified diff