Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

xbee: add timeout for AT command response (fixes #4731) (backport) #5346

Merged
merged 1 commit into from
Apr 19, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
{
/* acquire TX lock */
Expand All @@ -120,11 +136,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