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.c
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 #include "mcu_periph/sys_time.h"
26 
27 #ifdef SIM_UART
28 #include "sim_uart.h"
29 #endif
30 
33 
35 
36 #define AT_COMMAND_SEQUENCE "+++"
37 #define AT_SET_MY "ATMY"
38 #define AT_AP_MODE "ATAP1\r"
39 #define AT_EXIT "ATCN\r"
40 
42 {
43  char c[2];
44  int count = 0;
45 
46  while (TransportLink(XBEE_UART,ChAvailable()))
47  {
48  char cc = TransportLink(XBEE_UART,Getch());
49  if (count < 2)
50  c[count] = cc;
51  count++;
52  }
53 
54  if ((count > 2) && (c[0] == 'O') && (c[1] == 'K'))
55  return TRUE;
56 
57  return FALSE;
58 }
59 
61 
64 
66  sys_time_usleep(1250000);
67 
68  return xbee_text_reply_is_ok();
69 }
70 
71 #define XBeeUartSetBaudrate(_a) TransportLink(XBEE_UART,SetBaudrate(_a))
72 
73 
74 #if XBEE_BAUD == B9600
75  #define XBEE_BAUD_ALTERNATE B57600
76  #define XBEE_ATBD_CODE "ATBD3\rATWR\r"
77  #pragma message "Experimental: XBEE-API@9k6 auto-baudrate 57k6 -> 9k6 (stop ground link for correct operation)"
78 #elif XBEE_BAUD == B57600
79  #define XBEE_BAUD_ALTERNATE B9600
80  #define XBEE_ATBD_CODE "ATBD6\rATWR\r"
81  #pragma message "Experimental: XBEE-API@57k6 auto-baudrate 9k6 -> 57k6 (stop ground link for correct operation)"
82 #else
83  #warning XBEE-API Non default baudrate: auto-baud disabled
84 #endif
85 
86 
87 void xbee_init( void ) {
90 
91  // Empty buffer before init process
92  while (TransportLink(XBEE_UART,ChAvailable()))
93  TransportLink(XBEE_UART,Getch());
94 
95 #ifndef NO_XBEE_API_INIT
96 
97  sys_time_usleep(1250000);
98 
99  if (! xbee_try_to_enter_api() )
100  {
101  #ifdef XBEE_BAUD_ALTERNATE
102 
103  // Badly configured... try the alternate baudrate:
105  if ( xbee_try_to_enter_api() )
106  {
107  // The alternate baudrate worked,
108  XBeePrintString(XBEE_UART,XBEE_ATBD_CODE);
109  }
110  else
111  {
112  // Complete failure, none of the 2 baudrates result in any reply
113  // TODO: set LED?
114 
115  // Set the default baudrate, just in case everything is right
116  XBeeUartSetBaudrate(XBEE_BAUD);
117  XBeePrintString(XBEE_UART,"\r");
118  }
119 
120  #endif
121  // Continue changing settings until the EXIT is issued.
122  }
123 
125  XBeePrintString(XBEE_UART,AT_SET_MY);
126  uint16_t addr = XBEE_MY_ADDR;
127  XBeePrintHex16(XBEE_UART,addr);
128  XBeePrintString(XBEE_UART,"\r");
129 
130  XBeePrintString(XBEE_UART,AT_AP_MODE);
131 
132 #ifdef XBEE_INIT
133  XBeePrintString(XBEE_UART,XBEE_INIT);
134 #endif
135 
137  XBeePrintString(XBEE_UART,AT_EXIT);
138 
139  XBeeUartSetBaudrate(XBEE_BAUD);
140 
141 #endif
142 }
unsigned short uint16_t
Definition: types.h:16
#define TransportLink(_dev, _x)
Definition: transport.h:57
#define XBEE_MY_ADDR
Initialisation in API mode and setting of the local address FIXME: busy wait.
Definition: xbee.h:51
uint8_t xbee_cs
Definition: xbee.c:31
static void sys_time_usleep(uint32_t us)
Busy wait, in microseconds.
Definition: sys_time_arch.h:73
uint8_t xbee_rssi
Definition: xbee.c:32
#define AT_COMMAND_SEQUENCE
Definition: xbee.c:36
#define XBeePrintString(_dev, s)
Definition: xbee.h:233
struct xbee_transport xbee_tp
Definition: xbee.c:34
uint8_t status
Definition: xbee.h:153
#define XBeePrintHex16(_dev, x)
Definition: xbee.h:234
Simulation of uart transmission on an Unix pipe.
static uint8_t xbee_try_to_enter_api(void)
Definition: xbee.c:60
#define FALSE
Definition: imu_chimu.h:141
#define AT_AP_MODE
Definition: xbee.c:38
Architecture independent timing functions.
#define XBEE_UNINIT
Status of the API packet receiver automata.
Definition: xbee.h:143
#define TRUE
Definition: imu_chimu.h:144
#define AT_EXIT
Definition: xbee.c:39
unsigned char uint8_t
Definition: types.h:14
volatile bool_t msg_received
Definition: transport.h:45
static uint8_t xbee_text_reply_is_ok(void)
Definition: xbee.c:41
#define XBEE_ATBD_CODE
Definition: xbee.c:76
static struct point c
Definition: discsurvey.c:39
#define AT_SET_MY
Definition: xbee.c:37
void xbee_init(void)
Definition: xbee.c:87
#define XBEE_BAUD_ALTERNATE
Definition: xbee.c:75
struct transport trans
Definition: xbee.h:151
#define XBeeUartSetBaudrate(_a)
Definition: xbee.c:71