xbee.cpp
Go to the documentation of this file.
1 /*
2 MIT LICENSE
3 
4 Copyright 2014-2019 Inertial Sense, Inc. - http://inertialsense.com
5 
6 Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files(the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions :
7 
8 The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
9 
10 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT, IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
11 */
12 
13 #include <asf.h>
14 #include <string>
15 #include "conf_board.h"
16 #include "drivers/d_usartDMA.h"
17 #include "globals.h"
18 #include "../../../src/ISComm.h"
19 #include "xbee.h"
20 
21 using namespace std;
22 
23 
25 {
26  AT_COMMAND_FRAME = 0x08, // AT Command frame
27  AT_COMMAND_QUEUE_FRAME = 0x09, // AT Command - Queue Parameter Value frame
28  TX_REQUEST_FRAME = 0x10, // Transmit Request frame
29  TX_EXPLICIT_REQUEST_FRAME = 0x11, // Explicit Addressing Command frame
30  AT_REMOTE_COMMAND_FRAME = 0x17, // Remote AT Command Request frame
31  AT_SECURE_REMOTE_COMMAND_FRAME = 0x18, // Secure Remote AT Command Request frame
32  AT_COMMAND_RESPONSE_FRAME = 0x88, // AT Command Response frame
33  RX_INDICATOR_FRAME = 0x90, // RX Indicator frame
34  RX_EXPLICIT_INDICATOR_FRAME = 0x91, // Explicit Rx Indicator frame
35  NODE_ID_FRAME = 0x95, // Node Identification Indicator frame
36  REMOTE_COMMAND_RESPONSE_FRAME = 0x97, // Remote Command Response frame
37 };
38 
40 #define XBEE_APPLY_CHANGES "AC"
41 #define XBEE_SOFTWARE_RESET "FR"
42 #define XBEE_RESTORE_DEFAULTS "RE"
43 #define XBEE_WRITE_VALUES "WR"
44 #define XBEE_EXIT_COMMAND_MODE "CN"
45 
47 #define XBEE_PREAMBLE_ID "HP"
48 #define XBEE_NETWORK_ID "ID"
49 #define XBEE_BCAST_MULTITX "MT"
50 #define XBEE_UCAST_RETRIES "RR"
51 #define XBEE_TX_OPTIONS "TO"
52 #define XBEE_TX_POWER_LEVEL "PL"
53 #define XBEE_BAUD_RATE "BD"
54 #define XBEE_PACKETIZATION_TO "RO"
55 #define XBEE_API_ENABLE "AP"
56 #define XBEE_SLEEP_MODE "SM"
57 
59 #define XBEE_RSSI = "DB"
60 
61 
64 {
65  uint8_t preamble_id = 23;
66  uint16_t network_id = 45;
67  uint8_t bcast_multi_tx = 0;
68  uint8_t ucast_retries = 0;
69  uint8_t tx_options = 0x40; // Point to point/multipoint
70  uint8_t tx_power_level = 2; // 2 = 30dBm (full power)
71  uint8_t baud_rate = 7; // 7 = 115,200
72  uint8_t packetization_to = 0;
73  uint8_t api_enable = 1;
74  uint8_t sleep_options = 1; // Use I/O pin to sleep
75 };
76 
79 {
80  uint8_t rssi;
81 };
82 
83 
84 enum
85 {
95 };
96 
97 // #define BUF_SIZE 256
98 
101 
102 
103 // uint8_t xbee_buf[BUF_SIZE];
104 // static uint8_t s_last_api_command[3] = {0};
105 static int s_at_command_idx = 0;
106 
107 static bool s_at_ok = 0;
108 static int s_timer_ms = 0;
109 static int s_baud_rate = 115200;
111 
112 
113 
114 static void send_at_command(string cmd)
115 {
116  cmd = string("AT") + cmd + "\r";
117  comWrite(EVB2_PORT_XBEE, (uint8_t *)(cmd.c_str()), cmd.size(), LED_XBEE_TXD_PIN);
118 }
119 
120 
121 static bool send_next_at_command()
122 {
123  switch(s_at_command_idx++)
124  {
125  case 0: send_at_command( XBEE_RESTORE_DEFAULTS ); break;
126  case 1: send_at_command( XBEE_PREAMBLE_ID + to_string(g_flashCfg->radioPID) ); break;
127  case 2: send_at_command( XBEE_NETWORK_ID + to_string(g_flashCfg->radioNID) ); break;
128  case 3: send_at_command( XBEE_TX_OPTIONS + string("0x40") ); break;
129  case 4: send_at_command( XBEE_TX_POWER_LEVEL + to_string(g_flashCfg->radioPowerLevel) ); break;
130  case 5: send_at_command( XBEE_BCAST_MULTITX + string("0") ); break; // sets multi-transmit to off
131  case 6: send_at_command( XBEE_UCAST_RETRIES + string("0") ); break; // sets retry to off
132  case 7: send_at_command( XBEE_BAUD_RATE + to_string(xbee_diff.baud_rate) ); break; // baud rate doesn't change until XBEE_EXIT_COMMAND_MODE is sent
133  case 8:
134  if(s_baud_rate!=115200)
135  { // Update flash IF baud is not at 115200 so next time it starts faster
137  break;
138  }
139  else
140  { // Continue to next command
142  }
143  // This must be the last command sent
144  case 9: send_at_command( XBEE_EXIT_COMMAND_MODE ); break;
145 
146 // case 3: send_at_command( XBEE_BCAST_MULTITX + to_string(xbee_diff.bcast_multitx_val) ); break;
147 // case 4: send_at_command( XBEE_UCAST_RETRIES + to_string(xbee_diff.ucast_retries_val) ); break;
148 // case 6: send_at_command( XBEE_PACKETIZATION_TO + to_string(xbee_diff.packetization_to) ); break;
149 // case 7: send_at_command( XBEE_API_ENABLE + to_string(xbee_diff.api_enable) ); break;
150 // case 8: send_at_command( XBEE_SLEEP_MODE + to_string(xbee_diff.sleep_options) ); break;
151 // case 10: send_at_command( XBEE_WRITE_VALUES ); break;
152  default: return true; // done
153  }
154 
155  // Not done
156  return false;
157 }
158 
159 
160 // uint8_t xbee_packetize(uint8_t *raw_buf, uint8_t len_raw_buf, uint8_t *pack_buf) {
161 // raw_buf[0] = 0x7E; // Start delimiter
162 // raw_buf[1] = 0x00; // MSB
163 // raw_buf[2] = len_raw_buf; // LSB
164 //
165 // uint32_t checksum;
166 //
167 // pack_buf = raw_buf - 3;
168 //
169 // for(size_t i = 0 ; i < len_raw_buf ; ++i ) {
170 // checksum += raw_buf[i];
171 // }
172 //
173 // pack_buf[len_raw_buf+3] = 0xFF - (checksum & 0xFF); // Checksum calculation
174 //
175 // return len_raw_buf+3; // Length of packetized buffer
176 // }
177 
178 static void xbee_receive(is_comm_instance_t *comm)
179 {
180  if( xbee_runtime_mode()
181 // || g_flashCfg->cbPreset == EVB2_CB_PRESET_USB_HUB_RS232
182 // || g_flashCfg->cbPreset == EVB2_CB_PRESET_USB_HUB_RS422
183  )
184  { // Normal communications
186  }
187  else
188  { // XBee configuration/command mode
189 #define BUF_SIZE 256 //USB CDC buffer size is 320
190  uint8_t buf[BUF_SIZE];
191  int len;
192  if((len = comRead(EVB2_PORT_XBEE, buf, BUF_SIZE, LED_XBEE_RXD_PIN)) <= 0)
193  { // No data
194  return;
195  }
196 
197  // Look for AT "OK\r" response
198  while(buf[0] == 0x4F && len < 3)
199  { // Ensure we have at lease 3 bytes
200  len += comRead(EVB2_PORT_XBEE, &buf[len], BUF_SIZE-len, LED_XBEE_RXD_PIN); //Read new com data into buffer after original data and update len value
201  }
202  if(memcmp(reinterpret_cast<char *>(buf), "OK\r", 3) == 0)
203  { // AT command mode
204  s_at_ok = 1; // Make sure to reset this when you go to the next command
205  }
206 
207 #if 0
208  if(buf[0] == 0x7E)
209  { // API packet
210  uint8_t lsb = buf[2];
211 
212  switch (buf[3])
213  {
215  s_last_api_command[0] = buf[5]; // First AT character
216  s_last_api_command[1] = buf[6]; // Second AT character
217  s_last_api_command[2] = buf[7]; // Command status
218 
219  if(buf[5] == ('D') && buf[6] == ('B'))
220  {
221  xbee_read.rssi = buf[8];
222  }
223  break;
224 
225  case RX_INDICATOR_FRAME:
226  com_bridge_forward(EVB2_PORT_XBEE, buf + 15, lsb - 12); // Forward data into com bridge
227  break;
228  }
229  }
230  else
231 #endif
232  }
233 }
234 
235 
236 void xbee_init(void)
237 {
238  s_baud_rate = 115200;
243 }
244 
245 
247 {
249  { // Don't run XBee if its disabled. Enabled inside board_IO_config().
250  return;
251  }
252 
253  switch(s_xstate)
254  {
255  default: // XSTATE_RUN_TIME_DEFAULT
258  break;
259 
260  case XSTATE_START_CONFIG:
262  LED_OFF(LED_XBEE_RXD_PIN); // green off
263  LED_OFF(LED_XBEE_TXD_PIN); // red off
265  s_xstate++;
266  break;
267 
269  if((g_comm_time_ms - s_timer_ms) < 1500)
270  {
271  LED_OFF(LED_XBEE_RXD_PIN); // green off
272  LED_OFF(LED_XBEE_TXD_PIN); // red off
273  break;
274  }
275 
276  comWrite(EVB2_PORT_XBEE, (uint8_t *)"+++", 3, LED_XBEE_TXD_PIN);
277  LED_ON(LED_XBEE_RXD_PIN); // green on
278  LED_ON(LED_XBEE_TXD_PIN); // red on (orange)
280  s_xstate++;
281  break;
282 
284  if((g_comm_time_ms - s_timer_ms) < 1200)
285  {
286  break; // Wait
287  }
288 
289  if(s_at_ok)
290  { // Received OK
291  s_at_command_idx = 0;
294  break;
295  }
296  if(s_baud_rate == 115200)
297  { // Retry config at 9600 baud
298  s_baud_rate = 9600;
300  break;
301  }
303  break;
304 
306  if(s_at_ok)
307  { // Last command OK. Send next.
308  s_at_ok = 0;
311  {
313  }
314  }
315  else
316  {
317  if((g_comm_time_ms - s_timer_ms) > 1000)
318  { // Timeout failure
320  }
321  }
322  break;
323 
325  // Failure to enter AT command mode
330  break;
331 
333  if((g_comm_time_ms - s_timer_ms) > 1000)
334  {
336  }
337  LED_OFF(LED_XBEE_RXD_PIN); // green off
338  LED_ON(LED_XBEE_TXD_PIN); // red on
339  break;
340 
342  // Completed configuration!
347  break;
348 
350  if((g_comm_time_ms - s_timer_ms) > 1000)
351  {
353  }
354  LED_ON(LED_XBEE_RXD_PIN); // green on
355  LED_OFF(LED_XBEE_TXD_PIN); // red off
356  break;
357  }
358 
359  xbee_receive(comm);
360 }
361 
362 
363 
365 {
367 }
368 
369 
#define LED_ON(led)
Definition: user_board.h:236
xbee_diff_commands xbee_diff
Definition: xbee.cpp:99
int serSetBaudRate(int serialNum, int baudrate)
Change USART baudrate. 0 on success, -1 on failure.
Definition: d_usartDMA.c:995
#define LED_OFF(led)
Definition: user_board.h:235
#define BUF_SIZE
uint8_t rssi
Definition: xbee.cpp:80
void com_bridge_smart_forward(uint32_t srcPort, uint32_t ledPin)
int comWrite(int serialNum, const unsigned char *buf, int size, uint32_t ledPin)
uint32_t evbStatus
Definition: data_sets.h:2952
int xbee_runtime_mode(void)
Definition: xbee.cpp:364
uint32_t radioNID
Definition: data_sets.h:3038
static void xbee_receive(is_comm_instance_t *comm)
Definition: xbee.cpp:178
uint8_t baud_rate
Definition: xbee.cpp:71
void xbee_step(is_comm_instance_t *comm)
Definition: xbee.cpp:246
#define XBEE_EXIT_COMMAND_MODE
Definition: xbee.cpp:44
xbee_read_commands xbee_read
Definition: xbee.cpp:100
void xbee_init(void)
Definition: xbee.cpp:236
#define XBEE_BAUD_RATE
Definition: xbee.cpp:53
void com_bridge_forward(uint32_t srcPort, uint8_t *buf, int len)
static bool s_at_ok
Definition: xbee.cpp:107
#define XBEE_NETWORK_ID
Definition: xbee.cpp:48
static void send_at_command(string cmd)
Definition: xbee.cpp:114
uint32_t radioPowerLevel
Definition: data_sets.h:3041
#define LED_XBEE_TXD_PIN
Definition: user_board.h:283
#define XBEE_WRITE_VALUES
Definition: xbee.cpp:43
xbee_frame_id
Definition: xbee.cpp:24
#define XBEE_UCAST_RETRIES
Definition: xbee.cpp:50
#define XBEE_RESTORE_DEFAULTS
Definition: xbee.cpp:42
evb_status_t g_status
Definition: globals.c:21
Board configuration.
uint32_t g_comm_time_ms
Definition: globals.c:32
static bool send_next_at_command()
Definition: xbee.cpp:121
int comRead(int serialNum, unsigned char *buf, int bufSize, uint32_t ledPin)
#define LED_XBEE_RXD_PIN
Definition: user_board.h:282
#define XBEE_PREAMBLE_ID
Definition: xbee.cpp:47
static int s_timer_ms
Definition: xbee.cpp:108
evb_flash_cfg_t * g_flashCfg
Definition: globals.c:22
#define XBEE_TX_OPTIONS
Definition: xbee.cpp:51
static int s_baud_rate
Definition: xbee.cpp:109
static int s_xstate
Definition: xbee.cpp:110
Autogenerated API include file for the Atmel Software Framework (ASF)
#define XBEE_TX_POWER_LEVEL
Definition: xbee.cpp:52
uint32_t radioPID
Definition: data_sets.h:3035
#define XBEE_BCAST_MULTITX
Definition: xbee.cpp:49
static int s_at_command_idx
Definition: xbee.cpp:105


inertial_sense_ros
Author(s):
autogenerated on Sat Sep 19 2020 03:19:05