Skip to content

Commit

Permalink
Merge pull request #5346 from authmillenon/xbee/fix/pr4734-bp
Browse files Browse the repository at this point in the history
xbee: add timeout for AT command response (fixes #4731) (backport)
  • Loading branch information
kYc0o committed Apr 19, 2016
2 parents f7b3c79 + 47f0c8a commit 3c6c4a8
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 1 deletion.
1 change: 1 addition & 0 deletions drivers/include/xbee.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include <stdint.h>

#include "mutex.h"
#include "xtimer.h"
#include "periph/uart.h"
#include "periph/gpio.h"
#include "net/gnrc.h"
Expand Down
38 changes: 37 additions & 1 deletion drivers/xbee/xbee.c
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,11 @@
*/
#define RESET_DELAY (10U * 1000U)

/**
* @brief Timeout for receiving AT command response
*/
#define RESP_TIMEOUT_USEC (SEC_IN_USEC)

/**
* @brief Start delimiter in API frame mode
*/
Expand Down Expand Up @@ -102,6 +107,17 @@ static void _at_cmd(xbee_t *dev, const char *cmd)
uart_write(dev->uart, (uint8_t *)cmd, strlen(cmd));
}

static void isr_resp_timeout(void *arg)
{
xbee_t *dev = (xbee_t *)arg;

if (mutex_trylock(&(dev->resp_lock)) == 0) {
dev->int_state = XBEE_INT_STATE_IDLE;
}

mutex_unlock(&(dev->resp_lock));
}

static void _api_at_cmd(xbee_t *dev, uint8_t *cmd, uint8_t size, resp_t *resp)
{
DEBUG("xbee: AT_CMD: %s\n", cmd);
Expand All @@ -122,11 +138,31 @@ static void _api_at_cmd(xbee_t *dev, uint8_t *cmd, uint8_t size, resp_t *resp)
/* start send data */
uart_write(dev->uart, dev->tx_buf, size + 6);

uint64_t sent_time = xtimer_now64();

xtimer_t resp_timer;

resp_timer.callback = isr_resp_timeout;
resp_timer.arg = dev;

xtimer_set(&resp_timer, RESP_TIMEOUT_USEC);

/* wait for results */
while (dev->resp_limit != dev->resp_count) {
while ((dev->resp_limit != dev->resp_count) &&
(xtimer_now64() - sent_time < RESP_TIMEOUT_USEC)) {
mutex_lock(&(dev->resp_lock));
}

xtimer_remove(&resp_timer);

if (dev->resp_limit != dev->resp_count) {
DEBUG("xbee: response timeout\n");
resp->status = 255;
mutex_unlock(&(dev->tx_lock));

return;
}

/* populate response data structure */
resp->status = dev->resp_buf[3];
resp->data_len = dev->resp_limit - 5;
Expand Down

0 comments on commit 3c6c4a8

Please sign in to comment.