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