1
0
mirror of https://github.com/RIOT-OS/RIOT.git synced 2024-12-29 04:50:03 +01:00

Merge pull request #4734 from Yonezawa-T2/xbee_resp_timeout

xbee: add timeout for AT command response (fixes #4731)
This commit is contained in:
kYc0o 2016-04-19 13:34:21 +02:00
commit 724276e913
2 changed files with 38 additions and 1 deletions

View File

@ -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"

View File

@ -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
*/
@ -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);
@ -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;