Paparazzi UAS  v5.0.5_stable-7-g4b8bbb7
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 #else
93 #define XBeeTransportPutDoubleByAddr(_dev, _byte) { \
94  XBeeTransportPut4ByteByAddr(_dev, (const uint8_t*)_byte); \
95  XBeeTransportPut4ByteByAddr(_dev, (const uint8_t*)_byte+4); \
96  }
97 #endif
98 
99 
100 #define XBeeTransportPutInt8ByAddr(_dev, _x) XBeeTransportPut1ByteByAddr(_dev, _x)
101 #define XBeeTransportPutUint8ByAddr(_dev, _x) XBeeTransportPut1ByteByAddr(_dev, (const uint8_t*)_x)
102 #define XBeeTransportPutInt16ByAddr(_dev, _x) XBeeTransportPut2ByteByAddr(_dev, (const uint8_t*)_x)
103 #define XBeeTransportPutUint16ByAddr(_dev, _x) XBeeTransportPut2ByteByAddr(_dev, (const uint8_t*)_x)
104 #define XBeeTransportPutInt32ByAddr(_dev, _x) XBeeTransportPut4ByteByAddr(_dev, (const uint8_t*)_x)
105 #define XBeeTransportPutUint32ByAddr(_dev, _x) XBeeTransportPut4ByteByAddr(_dev, (const uint8_t*)_x)
106 #define XBeeTransportPutFloatByAddr(_dev, _x) XBeeTransportPut4ByteByAddr(_dev, (const uint8_t*)_x)
107 #define XBeeTransportPutNamedUint8(_dev, _name, _byte) XBeeTransportPutUint8(_dev, _byte)
108 
109 #define XBeeTransportPutArray(_dev, _put, _n, _x) { \
110  uint8_t _i; \
111  XBeeTransportPutUint8(_dev, _n); \
112  for(_i = 0; _i < _n; _i++) { \
113  _put(_dev, &_x[_i]); \
114  } \
115 }
116 
117 #define XBeeTransportPutInt16Array(_dev, _n, _x) XBeeTransportPutArray(_dev, XBeeTransportPutInt16ByAddr, _n, _x)
118 
119 #define XBeeTransportPutUint16Array(_dev, _n, _x) XBeeTransportPutArray(_dev, XBeeTransportPutUint16ByAddr, _n, _x)
120 #define XBeeTransportPutUint8Array(_dev, _n, _x) XBeeTransportPutArray(_dev, XBeeTransportPutUint8ByAddr, _n, _x)
121 #define XBeeTransportPutFloatArray(_dev, _n, _x) XBeeTransportPutArray(_dev, XBeeTransportPutFloatByAddr, _n, _x)
122 #define XBeeTransportPutDoubleArray(_dev, _n, _x) XBeeTransportPutArray(_dev, XBeeTransportPutDoubleByAddr, _n, _x)
123 
124 
125 
126 #define XBeeTransportHeader(_dev, _len) { \
127  XBeeTransportPut1Byte(_dev, XBEE_START); \
128  uint8_t payload_len = XBeeAPISizeOf(_dev, _len); \
129  XBeeTransportPut2Bytes(_dev, payload_len); \
130  xbee_cs = 0; \
131  XBeeTransportPutTXHeader(_dev); \
132 }
133 
134 #define XBeeTransportTrailer(_dev) { \
135  xbee_cs = 0xff - xbee_cs; \
136  XBeeTransportPut1Byte(_dev, xbee_cs); \
137  XBeeTransportSendMessage(_dev) \
138 }
139 
140 
141 
143 #define XBEE_UNINIT 0
144 #define XBEE_GOT_START 1
145 #define XBEE_GOT_LENGTH_MSB 2
146 #define XBEE_GOT_LENGTH_LSB 3
147 #define XBEE_GOT_PAYLOAD 4
148 
150  // generic interface
151  struct transport trans;
152  // specific pprz transport variables
156 };
157 
158 extern struct xbee_transport xbee_tp;
159 
161 static inline void parse_xbee( struct xbee_transport * t, uint8_t c ) {
162  switch (t->status) {
163  case XBEE_UNINIT:
164  if (c == XBEE_START)
165  t->status++;
166  break;
167  case XBEE_GOT_START:
168  if (t->trans.msg_received) {
169  t->trans.ovrn++;
170  goto error;
171  }
172  t->trans.payload_len = c<<8;
173  t->status++;
174  break;
175  case XBEE_GOT_LENGTH_MSB:
176  t->trans.payload_len |= c;
177  t->status++;
178  t->payload_idx = 0;
179  t->cs=0;
180  break;
181  case XBEE_GOT_LENGTH_LSB:
182  t->trans.payload[t->payload_idx] = c;
183  t->cs += c;
184  t->payload_idx++;
185  if (t->payload_idx == t->trans.payload_len)
186  t->status++;
187  break;
188  case XBEE_GOT_PAYLOAD:
189  if (c + t->cs != 0xff)
190  goto error;
191  t->trans.msg_received = TRUE;
192  goto restart;
193  break;
194  default:
195  goto error;
196  }
197  return;
198  error:
199  t->trans.error++;
200  restart:
201  t->status = XBEE_UNINIT;
202  return;
203 }
204 
206 static inline void xbee_parse_payload(struct xbee_transport * t) {
207  switch (t->trans.payload[0]) {
208  case XBEE_RX_ID:
209  case XBEE_TX_ID: /* Useful if A/C is connected to the PC with a cable */
211  uint8_t i;
212  for(i = XBEE_RFDATA_OFFSET; i < t->trans.payload_len; i++)
215  break;
216  default:
217  return;
218  }
219 }
220 
221 #define XBeeBuffer(_dev) TransportLink(_dev,ChAvailable())
222 #define ReadXBeeBuffer(_dev,_trans) { while (TransportLink(_dev,ChAvailable())&&!(_trans.trans.msg_received)) parse_xbee(&(_trans),TransportLink(_dev,Getch())); }
223 #define XBeeCheckAndParse(_dev,_trans) { \
224  if (XBeeBuffer(_dev)) { \
225  ReadXBeeBuffer(_dev,_trans); \
226  if (_trans.trans.msg_received) { \
227  xbee_parse_payload(&(_trans)); \
228  _trans.trans.msg_received = FALSE; \
229  } \
230  } \
231 }
232 
233 #define XBeePrintString(_dev, s) TransportLink(_dev,PrintString(s))
234 #define XBeePrintHex16(_dev, x) TransportLink(_dev,PrintHex16(x))
235 
236 #endif /* XBEE_H */
struct xbee_transport xbee_tp
Definition: xbee.c:34
generic transport header
#define XBEE_GOT_START
Definition: xbee.h:144
#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:145
uint8_t status
Definition: xbee.h:153
uint8_t cs
Definition: xbee.h:155
#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:147
static void parse_xbee(struct xbee_transport *t, uint8_t c)
Parsing a XBee API frame.
Definition: xbee.h:161
uint8_t payload_idx
Definition: xbee.h:154
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:143
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:206
#define XBEE_GOT_LENGTH_LSB
Definition: xbee.h:146
uint8_t error
Definition: transport.h:47
static struct point c
Definition: discsurvey.c:39
struct transport trans
Definition: xbee.h:151