Paparazzi UAS  v5.8.2_stable-0-g6260b7c
Paparazzi is a free software Unmanned Aircraft System.
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
xbee.c
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2006 Pascal Brisset, Antoine Drouin
3  * Copyright (C) 2014 Gautier Hattenberger <gautier.hattenberger@enac.fr>
4  *
5  * This file is part of paparazzi.
6  *
7  * paparazzi is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2, or (at your option)
10  * any later version.
11  *
12  * paparazzi is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with paparazzi; see the file COPYING. If not, see
19  * <http://www.gnu.org/licenses/>.
20  *
21  */
22 
28 #include "mcu_periph/sys_time.h"
32 
34 #define GROUND_STATION_ADDR 0x100
35 
36 #define XBEE_MY_ADDR AC_ID
37 
39 #define TX_OPTIONS 0x00
40 #define NO_FRAME_ID 0
41 #define XBEE_API_OVERHEAD 5 /* start + len_msb + len_lsb + API_id + checksum */
42 
44 
45 #define AT_COMMAND_SEQUENCE "+++"
46 #define AT_SET_MY "ATMY"
47 #define AT_AP_MODE "ATAP1\r"
48 #define AT_EXIT "ATCN\r"
49 
52 static void put_1byte(struct xbee_transport *trans, struct link_device *dev, const uint8_t byte)
53 {
54  trans->cs_tx += byte;
55  dev->put_byte(dev->periph, byte);
56 }
57 
58 static void put_bytes(struct xbee_transport *trans, struct link_device *dev,
59  enum TransportDataType type __attribute__((unused)), enum TransportDataFormat format __attribute__((unused)),
60  uint8_t len, const void *bytes)
61 {
62  const uint8_t *b = (const uint8_t *) bytes;
63  int i;
64  for (i = 0; i < len; i++) {
65  put_1byte(trans, dev, b[i]);
66  }
67 }
68 
69 static void put_named_byte(struct xbee_transport *trans, struct link_device *dev,
70  enum TransportDataType type __attribute__((unused)), enum TransportDataFormat format __attribute__((unused)),
71  uint8_t byte, const char *name __attribute__((unused)))
72 {
73  put_1byte(trans, dev, byte);
74 }
75 
76 static uint8_t size_of(struct xbee_transport *trans __attribute__((unused)), uint8_t len)
77 {
78  // message length: payload + API overhead + XBEE TX overhead (868 or 2.4)
79  return len + XBEE_API_OVERHEAD + XBEE_TX_OVERHEAD;
80 }
81 
82 static void start_message(struct xbee_transport *trans, struct link_device *dev, uint8_t payload_len)
83 {
84  dev->put_byte(dev->periph, XBEE_START);
85  const uint16_t len = payload_len + XBEE_API_OVERHEAD;
86  dev->put_byte(dev->periph, (len >> 8));
87  dev->put_byte(dev->periph, (len & 0xff));
88  trans->cs_tx = 0;
89  const uint8_t header[] = XBEE_TX_HEADER;
90  put_bytes(trans, dev, DL_TYPE_UINT8, DL_FORMAT_SCALAR, XBEE_TX_OVERHEAD + 1, header);
91  dev->nb_msgs++;
92 }
93 
94 static void end_message(struct xbee_transport *trans, struct link_device *dev)
95 {
96  trans->cs_tx = 0xff - trans->cs_tx;
97  dev->put_byte(dev->periph, trans->cs_tx);
98  dev->send_message(dev->periph);
99 }
100 
101 static void overrun(struct xbee_transport *trans __attribute__((unused)),
102  struct link_device *dev)
103 {
104  dev->nb_ovrn++;
105 }
106 
107 static void count_bytes(struct xbee_transport *trans __attribute__((unused)),
108  struct link_device *dev, uint8_t bytes)
109 {
110  dev->nb_bytes += bytes;
111 }
112 
113 static int check_available_space(struct xbee_transport *trans __attribute__((unused)), struct link_device *dev,
114  uint8_t bytes)
115 {
116  return dev->check_free_space(dev->periph, bytes);
117 }
118 
120 {
121  char c[2];
122  int count = 0;
123 
124  while (dev->char_available(dev->periph)) {
125  char cc = dev->get_byte(dev->periph);
126  if (count < 2) {
127  c[count] = cc;
128  }
129  count++;
130  }
131 
132  if ((count > 2) && (c[0] == 'O') && (c[1] == 'K')) {
133  return TRUE;
134  }
135 
136  return FALSE;
137 }
138 
140 {
141 
144 
146  sys_time_usleep(1250000);
147 
148  return xbee_text_reply_is_ok(dev);
149 }
150 
151 
152 #if XBEE_BAUD == B9600
153 #define XBEE_BAUD_ALTERNATE B57600
154 #define XBEE_ATBD_CODE "ATBD3\rATWR\r"
155 #pragma message "Experimental: XBEE-API@9k6 auto-baudrate 57k6 -> 9k6 (stop ground link for correct operation)"
156 #elif XBEE_BAUD == B57600
157 #define XBEE_BAUD_ALTERNATE B9600
158 #define XBEE_ATBD_CODE "ATBD6\rATWR\r"
159 #pragma message "Experimental: XBEE-API@57k6 auto-baudrate 9k6 -> 57k6 (stop ground link for correct operation)"
160 #else
161 #pragma message "XBEE-API Non default baudrate: auto-baud disabled."
162 #endif
163 
164 
165 void xbee_init(void)
166 {
177  xbee_tp.trans_tx.impl = (void *)(&xbee_tp);
178 
179  struct link_device *dev = &((XBEE_UART).device);
180 
181  // Empty buffer before init process
182  while (dev->char_available(dev->periph)) {
183  dev->get_byte(dev->periph);
184  }
185 
186 #ifndef NO_XBEE_API_INIT
187 
188  sys_time_usleep(1250000);
189 
190  if (! xbee_try_to_enter_api(dev)) {
191 #ifdef XBEE_BAUD_ALTERNATE
192 
193  // Badly configured... try the alternate baudrate:
194  uart_periph_set_baudrate(&(XBEE_UART),
195  XBEE_BAUD_ALTERNATE); // FIXME add set_baudrate to generic device, assuming uart for now
196  if (xbee_try_to_enter_api(dev)) {
197  // The alternate baudrate worked,
199  } else {
200  // Complete failure, none of the 2 baudrates result in any reply
201  // TODO: set LED?
202 
203  // Set the default baudrate, just in case everything is right
204  uart_periph_set_baudrate(&(XBEE_UART), XBEE_BAUD); // FIXME add set_baudrate to generic device, assuming uart for now
205  print_string(dev, "\r");
206  }
207 
208 #endif
209  // Continue changing settings until the EXIT is issued.
210  }
211 
213  print_string(dev, AT_SET_MY);
214  uint16_t addr = XBEE_MY_ADDR;
215  print_hex16(dev, addr);
216  print_string(dev, "\r");
217 
218  print_string(dev, AT_AP_MODE);
219 
220 #ifdef XBEE_INIT
221  print_string(dev, XBEE_INIT);
222 #endif
223 
225  print_string(dev, AT_EXIT);
226 
227  uart_periph_set_baudrate(&(XBEE_UART), XBEE_BAUD); // FIXME add set_baudrate to generic device, assuming uart for now
228 
229 #endif
230 }
unsigned short uint16_t
Definition: types.h:16
static uint8_t size_of(struct xbee_transport *trans, uint8_t len)
Definition: xbee.c:76
volatile bool_t msg_received
message received flag
Definition: transport.h:42
TransportDataFormat
Data format (scalar or array)
Definition: transport.h:66
end_message_t end_message
transport trailer
Definition: transport.h:95
#define XBEE_API_OVERHEAD
Definition: xbee.c:41
static int check_available_space(struct xbee_transport *trans, struct link_device *dev, uint8_t bytes)
Definition: xbee.c:113
uint8_t(* size_of_t)(void *, uint8_t)
Function pointers definition.
Definition: transport.h:76
void(* end_message_t)(void *, struct link_device *)
Definition: transport.h:83
static uint8_t xbee_try_to_enter_api(struct link_device *dev)
Definition: xbee.c:139
#define AT_COMMAND_SEQUENCE
Definition: xbee.c:45
static void put_bytes(struct xbee_transport *trans, struct link_device *dev, enum TransportDataType type, enum TransportDataFormat format, uint8_t len, const void *bytes)
Definition: xbee.c:58
static void start_message(struct xbee_transport *trans, struct link_device *dev, uint8_t payload_len)
Definition: xbee.c:82
struct xbee_transport xbee_tp
Definition: xbee.c:43
static void end_message(struct xbee_transport *trans, struct link_device *dev)
Definition: xbee.c:94
size_of_t size_of
get size of payload with transport header and trailer
Definition: transport.h:90
overrun_t overrun
overrun
Definition: transport.h:96
void(* overrun_t)(void *, struct link_device *)
Definition: transport.h:84
TransportDataType
Data type.
Definition: transport.h:48
count_bytes_t count_bytes
count bytes to send
Definition: transport.h:97
static void print_hex16(struct link_device *dev, uint16_t c)
Definition: uart_print.h:74
#define FALSE
Definition: std.h:5
#define AT_AP_MODE
Definition: xbee.c:47
void uart_periph_set_baudrate(struct uart_periph *periph, uint32_t baud)
Definition: uart_arch.c:216
static void overrun(struct xbee_transport *trans, struct link_device *dev)
Definition: xbee.c:101
#define TRUE
Definition: std.h:4
#define XBEE_MY_ADDR
Aircraft address.
Definition: xbee.c:36
static uint8_t xbee_text_reply_is_ok(struct link_device *dev)
Definition: xbee.c:119
Architecture independent timing functions.
uint8_t status
Definition: xbee.h:58
void(* start_message_t)(void *, struct link_device *, uint8_t)
Definition: transport.h:82
void(* put_named_byte_t)(void *, struct link_device *, enum TransportDataType, enum TransportDataFormat, uint8_t, const char *)
Definition: transport.h:80
static void put_1byte(struct xbee_transport *trans, struct link_device *dev, const uint8_t byte)
Xbee protocol implementation.
Definition: xbee.c:52
start_message_t start_message
transport header
Definition: transport.h:94
put_named_byte_t put_named_byte
send a single byte or its name
Definition: transport.h:93
#define XBEE_START
Definition: xbee.h:41
#define XBEE_UNINIT
Status of the API packet receiver automata.
Definition: xbee.h:48
static const struct usb_device_descriptor dev
Definition: usb_ser_hw.c:69
#define AT_EXIT
Definition: xbee.c:48
int(* check_available_space_t)(void *, struct link_device *, uint8_t)
Definition: transport.h:77
static void sys_time_usleep(uint32_t us)
Definition: sys_time_arch.h:55
void(* put_bytes_t)(void *, struct link_device *, enum TransportDataType, enum TransportDataFormat, uint8_t, const void *)
Definition: transport.h:78
unsigned char uint8_t
Definition: types.h:14
void * impl
pointer to parent implementation
Definition: transport.h:98
static void count_bytes(struct xbee_transport *trans, struct link_device *dev, uint8_t bytes)
Definition: xbee.c:107
static void print_string(struct link_device *dev, char *s)
Definition: uart_print.h:38
struct transport_rx trans_rx
Definition: xbee.h:56
check_available_space_t check_available_space
check if transmit buffer is not full
Definition: transport.h:91
put_bytes_t put_bytes
send bytes
Definition: transport.h:92
#define XBEE_ATBD_CODE
Definition: xbee.c:154
uint8_t cs_tx
Definition: xbee.h:65
static void put_named_byte(struct xbee_transport *trans, struct link_device *dev, enum TransportDataType type, enum TransportDataFormat format, uint8_t byte, const char *name)
Definition: xbee.c:69
Maxstream XBee serial input and output.
void(* count_bytes_t)(void *, struct link_device *, uint8_t)
Definition: transport.h:85
#define AT_SET_MY
Definition: xbee.c:46
#define XBEE_TX_HEADER
Definition: xbee24.h:36
void xbee_init(void)
Initialisation in API mode and setting of the local address FIXME: busy wait.
Definition: xbee.c:165
#define XBEE_TX_OVERHEAD
Definition: xbee24.h:35
#define XBEE_BAUD_ALTERNATE
Definition: xbee.c:153
struct transport_tx trans_tx
Definition: xbee.h:63