Paparazzi UAS  v5.2.2_stable-0-gd6b9f29
Paparazzi is a free software Unmanned Aircraft System.
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
xbee.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2006 Pascal Brisset, Antoine Drouin
3  *
4  * This file is part of paparazzi.
5  *
6  * paparazzi is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2, or (at your option)
9  * any later version.
10  *
11  * paparazzi is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with paparazzi; see the file COPYING. If not, write to
18  * the Free Software Foundation, 59 Temple Place - Suite 330,
19  * Boston, MA 02111-1307, USA.
20  *
21  */
22 
23 /* Maxstream XBee serial input and output */
24 
25 #ifndef XBEE_H
26 #define XBEE_H
27 
29 #include "generated/airframe.h"
30 
31 #ifdef XBEE868
33 #else /* Not 868 */
35 #endif
37 
39 #define XBEE_START 0x7e
40 #define TX_OPTIONS 0x00
41 #define NO_FRAME_ID 0
42 
44 #define GROUND_STATION_ADDR 0x100
45 
46 extern uint8_t xbee_cs;
47 extern uint8_t xbee_rssi;
48 
51 #define XBEE_MY_ADDR AC_ID
52 void xbee_init( void );
53 
54 /* 5 = Start + len_msb + len_lsb + API_id + checksum */
55 #define XBeeAPISizeOf(_dev, _x) (_x+5)
56 
57 #define XBeeTransportCheckFreeSpace(_dev, x) TransportLink(_dev, CheckFreeSpace(x))
58 #define XBeeTransportPut1Byte(_dev, x) TransportLink(_dev, Transmit(x))
59 #define XBeeTransportSendMessage(_dev) TransportLink(_dev, SendMessage())
60 
61 #define XBeeTransportPutUint8(_dev, _x) { \
62  xbee_cs += _x; \
63  XBeeTransportPut1Byte(_dev, _x); \
64 }
65 
66 #define XBeeTransportPut1ByteByAddr(_dev, _byte) { \
67  uint8_t _x = *(_byte); \
68  XBeeTransportPutUint8(_dev, _x); \
69  }
70 
71 #define XBeeTransportPut2Bytes(_dev, _x) { \
72  uint16_t x16 = _x; \
73  XBeeTransportPut1Byte(_dev, x16>>8); \
74  XBeeTransportPut1Byte(_dev, x16 & 0xff); \
75 }
76 
77 #define XBeeTransportPut2ByteByAddr(_dev, _byte) { \
78  XBeeTransportPut1ByteByAddr(_dev, _byte); \
79  XBeeTransportPut1ByteByAddr(_dev, (const uint8_t*)_byte+1); \
80  }
81 
82 #define XBeeTransportPut4ByteByAddr(_dev, _byte) { \
83  XBeeTransportPut2ByteByAddr(_dev, _byte); \
84  XBeeTransportPut2ByteByAddr(_dev, (const uint8_t*)_byte+2); \
85  }
86 
87 #ifdef __IEEE_BIG_ENDIAN /* From machine/ieeefp.h */
88 #define XBeeTransportPutDoubleByAddr(_dev, _byte) { \
89  XBeeTransportPut4ByteByAddr(_dev, (const uint8_t*)_byte+4); \
90  XBeeTransportPut4ByteByAddr(_dev, (const uint8_t*)_byte); \
91  }
92 #define XBeeTransportPutUint64ByAddr(_dev, _byte) { \
93  XBeeTransportPut4ByteByAddr(_dev, (const uint8_t*)_byte+4); \
94  XBeeTransportPut4ByteByAddr(_dev, (const uint8_t*)_byte); \
95  }
96 #define XBeeTransportPutInt64ByAddr(_dev, _byte) { \
97  XBeeTransportPut4ByteByAddr(_dev, (const uint8_t*)_byte+4); \
98  XBeeTransportPut4ByteByAddr(_dev, (const uint8_t*)_byte); \
99  }
100 #else
101 #define XBeeTransportPutDoubleByAddr(_dev, _byte) { \
102  XBeeTransportPut4ByteByAddr(_dev, (const uint8_t*)_byte); \
103  XBeeTransportPut4ByteByAddr(_dev, (const uint8_t*)_byte+4); \
104  }
105 #define XBeeTransportPutUint64ByAddr(_dev, _byte) { \
106  XBeeTransportPut4ByteByAddr(_dev, (const uint8_t*)_byte); \
107  XBeeTransportPut4ByteByAddr(_dev, (const uint8_t*)_byte+4); \
108  }
109 #define XBeeTransportPutInt64ByAddr(_dev, _byte) { \
110  XBeeTransportPut4ByteByAddr(_dev, (const uint8_t*)_byte); \
111  XBeeTransportPut4ByteByAddr(_dev, (const uint8_t*)_byte+4); \
112  }
113 #endif
114 
115 
116 #define XBeeTransportPutInt8ByAddr(_dev, _x) XBeeTransportPut1ByteByAddr(_dev, _x)
117 #define XBeeTransportPutUint8ByAddr(_dev, _x) XBeeTransportPut1ByteByAddr(_dev, (const uint8_t*)_x)
118 #define XBeeTransportPutInt16ByAddr(_dev, _x) XBeeTransportPut2ByteByAddr(_dev, (const uint8_t*)_x)
119 #define XBeeTransportPutUint16ByAddr(_dev, _x) XBeeTransportPut2ByteByAddr(_dev, (const uint8_t*)_x)
120 #define XBeeTransportPutInt32ByAddr(_dev, _x) XBeeTransportPut4ByteByAddr(_dev, (const uint8_t*)_x)
121 #define XBeeTransportPutUint32ByAddr(_dev, _x) XBeeTransportPut4ByteByAddr(_dev, (const uint8_t*)_x)
122 #define XBeeTransportPutFloatByAddr(_dev, _x) XBeeTransportPut4ByteByAddr(_dev, (const uint8_t*)_x)
123 #define XBeeTransportPutNamedUint8(_dev, _name, _byte) XBeeTransportPutUint8(_dev, _byte)
124 #define XBeeTransportPutCharByAddr(_dev, _x) XBeeTransportPut1ByteByAddr(_dev, (const uint8_t*)_x)
125 
126 #define XBeeTransportPutArray(_dev, _put, _n, _x) { \
127  uint8_t _i; \
128  XBeeTransportPutUint8(_dev, _n); \
129  for(_i = 0; _i < _n; _i++) { \
130  _put(_dev, &_x[_i]); \
131  } \
132 }
133 
134 #define XBeeTransportPutInt8Array(_dev, _n, _x) XBeeTransportPutArray(_dev, XBeeTransportPutInt8ByAddr, _n, _x)
135 #define XBeeTransportPutUint8Array(_dev, _n, _x) XBeeTransportPutArray(_dev, XBeeTransportPutUint8ByAddr, _n, _x)
136 
137 #define XBeeTransportPutCharArray(_dev, _n, _x) XBeeTransportPutArray(_dev, XBeeTransportPutCharByAddr, _n, _x)
138 
139 #define XBeeTransportPutInt16Array(_dev, _n, _x) XBeeTransportPutArray(_dev, XBeeTransportPutInt16ByAddr, _n, _x)
140 #define XBeeTransportPutUint16Array(_dev, _n, _x) XBeeTransportPutArray(_dev, XBeeTransportPutUint16ByAddr, _n, _x)
141 
142 #define XBeeTransportPutInt32Array(_dev, _n, _x) XBeeTransportPutArray(_dev, XBeeTransportPutInt32ByAddr, _n, _x)
143 #define XBeeTransportPutUint32Array(_dev, _n, _x) XBeeTransportPutArray(_dev, XBeeTransportPutUint32ByAddr, _n, _x)
144 
145 #define XBeeTransportPutFloatArray(_dev, _n, _x) XBeeTransportPutArray(_dev, XBeeTransportPutFloatByAddr, _n, _x)
146 
147 #define XBeeTransportPutInt64Array(_dev, _n, _x) XBeeTransportPutArray(_dev, XBeeTransportPutInt64ByAddr, _n, _x)
148 #define XBeeTransportPutUint64Array(_dev, _n, _x) XBeeTransportPutArray(_dev, XBeeTransportPutUint64ByAddr, _n, _x)
149 
150 #define XBeeTransportPutDoubleArray(_dev, _n, _x) XBeeTransportPutArray(_dev, XBeeTransportPutDoubleByAddr, _n, _x)
151 
152 
153 #define XBeeTransportPutFixedArray(_dev, _put, _n, _x) { \
154  uint8_t _i; \
155  for(_i = 0; _i < _n; _i++) { \
156  _put(_dev, &_x[_i]); \
157  } \
158 }
159 
160 #define XBeeTransportPutInt8FixedArray(_dev, _n, _x) XBeeTransportPutFixedArray(_dev, XBeeTransportPutInt8ByAddr, _n, _x)
161 #define XBeeTransportPutUint8FixedArray(_dev, _n, _x) XBeeTransportPutFixedArray(_dev, XBeeTransportPutUint8ByAddr, _n, _x)
162 
163 #define XBeeTransportPutCharFixedArray(_dev, _n, _x) XBeeTransportPutFixedArray(_dev, XBeeTransportPutCharByAddr, _n, _x)
164 
165 #define XBeeTransportPutInt16FixedArray(_dev, _n, _x) XBeeTransportPutFixedArray(_dev, XBeeTransportPutInt16ByAddr, _n, _x)
166 #define XBeeTransportPutUint16FixedArray(_dev, _n, _x) XBeeTransportPutFixedArray(_dev, XBeeTransportPutUint16ByAddr, _n, _x)
167 
168 #define XBeeTransportPutInt32FixedArray(_dev, _n, _x) XBeeTransportPutFixedArray(_dev, XBeeTransportPutInt32ByAddr, _n, _x)
169 #define XBeeTransportPutUint32FixedArray(_dev, _n, _x) XBeeTransportPutFixedArray(_dev, XBeeTransportPutUint32ByAddr, _n, _x)
170 
171 #define XBeeTransportPutFloatFixedArray(_dev, _n, _x) XBeeTransportPutFixedArray(_dev, XBeeTransportPutFloatByAddr, _n, _x)
172 
173 #define XBeeTransportPutInt64FixedArray(_dev, _n, _x) XBeeTransportPutFixedArray(_dev, XBeeTransportPutInt64ByAddr, _n, _x)
174 #define XBeeTransportPutUint64FixedArray(_dev, _n, _x) XBeeTransportPutFixedArray(_dev, XBeeTransportPutUint64ByAddr, _n, _x)
175 
176 #define XBeeTransportPutDoubleFixedArray(_dev, _n, _x) XBeeTransportPutFixedArray(_dev, XBeeTransportPutDoubleByAddr, _n, _x)
177 
178 
179 #define XBeeTransportHeader(_dev, _len) { \
180  XBeeTransportPut1Byte(_dev, XBEE_START); \
181  uint8_t payload_len = XBeeAPISizeOf(_dev, _len); \
182  XBeeTransportPut2Bytes(_dev, payload_len); \
183  xbee_cs = 0; \
184  XBeeTransportPutTXHeader(_dev); \
185 }
186 
187 #define XBeeTransportTrailer(_dev) { \
188  xbee_cs = 0xff - xbee_cs; \
189  XBeeTransportPut1Byte(_dev, xbee_cs); \
190  XBeeTransportSendMessage(_dev) \
191 }
192 
193 
194 
196 #define XBEE_UNINIT 0
197 #define XBEE_GOT_START 1
198 #define XBEE_GOT_LENGTH_MSB 2
199 #define XBEE_GOT_LENGTH_LSB 3
200 #define XBEE_GOT_PAYLOAD 4
201 
203  // generic interface
204  struct transport trans;
205  // specific pprz transport variables
209 };
210 
211 extern struct xbee_transport xbee_tp;
212 
214 static inline void parse_xbee( struct xbee_transport * t, uint8_t c ) {
215  switch (t->status) {
216  case XBEE_UNINIT:
217  if (c == XBEE_START)
218  t->status++;
219  break;
220  case XBEE_GOT_START:
221  if (t->trans.msg_received) {
222  t->trans.ovrn++;
223  goto error;
224  }
225  t->trans.payload_len = c<<8;
226  t->status++;
227  break;
228  case XBEE_GOT_LENGTH_MSB:
229  t->trans.payload_len |= c;
230  t->status++;
231  t->payload_idx = 0;
232  t->cs=0;
233  break;
234  case XBEE_GOT_LENGTH_LSB:
235  t->trans.payload[t->payload_idx] = c;
236  t->cs += c;
237  t->payload_idx++;
238  if (t->payload_idx == t->trans.payload_len)
239  t->status++;
240  break;
241  case XBEE_GOT_PAYLOAD:
242  if (c + t->cs != 0xff)
243  goto error;
244  t->trans.msg_received = TRUE;
245  goto restart;
246  break;
247  default:
248  goto error;
249  }
250  return;
251  error:
252  t->trans.error++;
253  restart:
254  t->status = XBEE_UNINIT;
255  return;
256 }
257 
259 static inline void xbee_parse_payload(struct xbee_transport * t) {
260  switch (t->trans.payload[0]) {
261  case XBEE_RX_ID:
262  case XBEE_TX_ID: /* Useful if A/C is connected to the PC with a cable */
264  uint8_t i;
265  for(i = XBEE_RFDATA_OFFSET; i < t->trans.payload_len; i++)
268  break;
269  default:
270  return;
271  }
272 }
273 
274 #define XBeeBuffer(_dev) TransportLink(_dev,ChAvailable())
275 #define ReadXBeeBuffer(_dev,_trans) { while (TransportLink(_dev,ChAvailable())&&!(_trans.trans.msg_received)) parse_xbee(&(_trans),TransportLink(_dev,Getch())); }
276 #define XBeeCheckAndParse(_dev,_trans) { \
277  if (XBeeBuffer(_dev)) { \
278  ReadXBeeBuffer(_dev,_trans); \
279  if (_trans.trans.msg_received) { \
280  xbee_parse_payload(&(_trans)); \
281  _trans.trans.msg_received = FALSE; \
282  } \
283  } \
284 }
285 
286 #define XBeePrintString(_dev, s) TransportLink(_dev,PrintString(s))
287 #define XBeePrintHex16(_dev, x) TransportLink(_dev,PrintHex16(x))
288 
289 #endif /* XBEE_H */
struct xbee_transport xbee_tp
Definition: xbee.c:34
generic transport header
#define XBEE_GOT_START
Definition: xbee.h:197
#define XBEE_TX_ID
Definition: xbee24.h:28
#define XbeeGetRSSI(_payload)
Definition: xbee24.h:43
Generic transport header.
Definition: transport.h:39
#define XBEE_GOT_LENGTH_MSB
Definition: xbee.h:198
uint8_t status
Definition: xbee.h:206
uint8_t cs
Definition: xbee.h:208
#define XBEE_RFDATA_OFFSET
Definition: xbee24.h:30
void xbee_init(void)
Definition: xbee.c:87
bool_t dl_msg_available
Definition: main_demo5.c:56
uint8_t xbee_cs
Definition: xbee.c:31
#define XBEE_GOT_PAYLOAD
Definition: xbee.h:200
static void parse_xbee(struct xbee_transport *t, uint8_t c)
Parsing a XBee API frame.
Definition: xbee.h:214
uint8_t payload_idx
Definition: xbee.h:207
uint8_t xbee_rssi
Definition: xbee.c:32
volatile uint8_t payload_len
Definition: transport.h:43
uint8_t payload[TRANSPORT_PAYLOAD_LEN]
Definition: transport.h:41
#define XBEE_START
Constants for the API protocol.
Definition: xbee.h:39
#define TRUE
Definition: imu_chimu.h:144
#define XBEE_UNINIT
Status of the API packet receiver automata.
Definition: xbee.h:196
unsigned char uint8_t
Definition: types.h:14
volatile bool_t msg_received
Definition: transport.h:45
uint8_t dl_buffer[MSG_SIZE]
Definition: main_demo5.c:59
uint8_t ovrn
Definition: transport.h:47
#define XBEE_RX_ID
Definition: xbee24.h:29
static void xbee_parse_payload(struct xbee_transport *t)
Parsing a frame data and copy the payload to the datalink buffer.
Definition: xbee.h:259
#define XBEE_GOT_LENGTH_LSB
Definition: xbee.h:199
uint8_t error
Definition: transport.h:47
struct transport trans
Definition: xbee.h:204