Skip to content

Commit

Permalink
[new] XBee auto-baudrate in api mode
Browse files Browse the repository at this point in the history
  • Loading branch information
dewagter committed Feb 25, 2013
1 parent d1edde0 commit 27b9726
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 8 deletions.
Expand Up @@ -3,7 +3,7 @@
# XBee modems in API mode

ap.CFLAGS += -DUSE_$(MODEM_PORT)
ap.CFLAGS += -D$(MODEM_PORT)_BAUD=$(MODEM_BAUD)
ap.CFLAGS += -D$(MODEM_PORT)_BAUD=$(MODEM_BAUD) -DXBEE_BAUD=$(MODEM_BAUD)

ap.CFLAGS += -DDOWNLINK -DDOWNLINK_FBW_DEVICE=$(MODEM_PORT) -DDOWNLINK_AP_DEVICE=$(MODEM_PORT) -DXBEE_UART=$(MODEM_PORT)
ap.CFLAGS += -DDOWNLINK_TRANSPORT=XBeeTransport -DDATALINK=XBEE
Expand Down
Expand Up @@ -7,7 +7,7 @@
#

ap.CFLAGS += -DUSE_$(MODEM_PORT)
ap.CFLAGS += -D$(MODEM_PORT)_BAUD=$(MODEM_BAUD)
ap.CFLAGS += -D$(MODEM_PORT)_BAUD=$(MODEM_BAUD) -DXBEE_BAUD=$(MODEM_BAUD)

ap.CFLAGS += -DDOWNLINK -DDOWNLINK_DEVICE=$(MODEM_PORT) -DXBEE_UART=$(MODEM_PORT)
ap.CFLAGS += -DDOWNLINK_TRANSPORT=XBeeTransport -DDATALINK=XBEE
Expand Down
80 changes: 74 additions & 6 deletions sw/airborne/subsystems/datalink/xbee.c
Expand Up @@ -34,27 +34,92 @@ uint8_t xbee_rssi;
struct xbee_transport xbee_tp;

#define AT_COMMAND_SEQUENCE "+++"
#define AT_INIT_PERIOD_US 2000000
#define AT_SET_MY "ATMY"
#define AT_AP_MODE "ATAP1\r"
#define AT_EXIT "ATCN\r"

uint32_t xbee_delay_time;
static uint8_t xbee_text_reply_is_ok(void)
{
char c[2];
int count = 0;

while (TransportLink(XBEE_UART,ChAvailable()))
{
char cc = TransportLink(XBEE_UART,Getch());
if (count < 2)
c[count] = cc;
count++;
}

if ((count > 2) && (c[0] == 'O') && (c[1] == 'K'))
return TRUE;

return FALSE;
}

static uint8_t xbee_try_to_enter_api(void) {

/** Switching to AT mode (FIXME: busy waiting) */
XBeePrintString(XBEE_UART,AT_COMMAND_SEQUENCE);

/** - busy wait 1.25s */
sys_time_usleep(1250000);

return xbee_text_reply_is_ok();
}

#define XBeeUartSetBaudrate(_a) TransportLink(XBEE_UART,SetBaudrate(_a))


#if XBEE_BAUD == B9600
#define XBEE_BAUD_ALTERNATE B57600
#define XBEE_ATBD_CODE "ATBD3\rATWR\r"
#pragma message "Experimental: XBEE-API@9k6 auto-baudrate 57k6 -> 9k6 (stop ground link for correct operation)"
#elif XBEE_BAUD == B57600
#define XBEE_BAUD_ALTERNATE B9600
#define XBEE_ATBD_CODE "ATBD6\rATWR\r"
#pragma message "Experimental: XBEE-API@57k6 auto-baudrate 9k6 -> 57k6 (stop ground link for correct operation)"
#else
#warning XBEE-API Non default baudrate: auto-baud disabled
#endif


void xbee_init( void ) {
xbee_tp.status = XBEE_UNINIT;
xbee_tp.trans.msg_received = FALSE;

// Empty buffer before init process
while (TransportLink(XBEE_UART,ChAvailable()))
TransportLink(XBEE_UART,Getch());

#ifndef NO_XBEE_API_INIT
/** - busy wait 1.25s */
sys_time_usleep(1250000);

/** Switching to AT mode (FIXME: busy waiting) */
XBeePrintString(XBEE_UART,AT_COMMAND_SEQUENCE);
if (! xbee_try_to_enter_api() )
{
#ifdef XBEE_BAUD_ALTERNATE

/** - busy wait 1.25s */
sys_time_usleep(1250000);
// Badly configured... try the alternate baudrate:
XBeeUartSetBaudrate(XBEE_BAUD_ALTERNATE);
if ( xbee_try_to_enter_api() )
{
// The alternate baudrate worked,
XBeePrintString(XBEE_UART,XBEE_ATBD_CODE);
}
else
{
// Complete failure, none of the 2 baudrates result in any reply
// TODO: set LED?

// Set the default baudrate, just in case everything is right
XBeeUartSetBaudrate(XBEE_BAUD);
XBeePrintString(XBEE_UART,"\r");
}

#endif
// Continue changing settings until the EXIT is issued.
}

/** Setting my address */
XBeePrintString(XBEE_UART,AT_SET_MY);
Expand All @@ -70,5 +135,8 @@ void xbee_init( void ) {

/** Switching back to normal mode */
XBeePrintString(XBEE_UART,AT_EXIT);

XBeeUartSetBaudrate(XBEE_BAUD);

#endif
}

0 comments on commit 27b9726

Please sign in to comment.