From 84514fdc4e37939d888eddf89956225ef20b57fc Mon Sep 17 00:00:00 2001 From: Olivier Matz Date: Wed, 26 Feb 2014 20:44:48 +0100 Subject: [PATCH] move xbee rx polling in a timer --- main.c | 3 +++ main.h | 9 +++++---- xbee_user.c | 47 +++++++++++++++++++++++++++++++++++++++-------- xbee_user.h | 2 ++ 4 files changed, 49 insertions(+), 12 deletions(-) diff --git a/main.c b/main.c index c438575..0fb75d1 100644 --- a/main.c +++ b/main.c @@ -166,6 +166,9 @@ int main(void) fprintf(stderr, "cannot register default channel\n"); return -1; } + + xbeeapp_init(); + sei(); eeprom_load_config(); diff --git a/main.h b/main.h index 001ecc0..18ddf05 100644 --- a/main.h +++ b/main.h @@ -59,10 +59,11 @@ #define BUZZER_OFF() cbi(PORTA, 4) /* highest priority */ -#define LED_PRIO 170 -#define TIME_PRIO 160 -#define BEEP_PRIO 130 +#define LED_PRIO 160 +#define TIME_PRIO 140 +#define BEEP_PRIO 120 #define SPI_PRIO 100 /* users of spi_servo must have lower prio */ +#define XBEE_PRIO 80 /* lowest priority */ #define MAX_POWER_LEVEL 5 @@ -76,6 +77,7 @@ struct xbeeboard { struct callout_mgr intr_cm; struct callout spi_timer; struct callout beep_timer; + struct callout xbee_rx_poll_timer; /* log */ uint8_t logs[NB_LOGS+1]; @@ -87,4 +89,3 @@ extern struct xbeeboard xbeeboard; extern volatile uint32_t global_ms; void bootloader(void); - diff --git a/xbee_user.c b/xbee_user.c index 4717313..ca9d475 100644 --- a/xbee_user.c +++ b/xbee_user.c @@ -36,6 +36,7 @@ #include #include +#include #include #include #include @@ -44,7 +45,8 @@ #include "xbee_user.h" #include "main.h" -#define XBEE_TIMEOUT_MS 1000 +#define XBEE_TIMEOUT_MS 1000 +#define XBEE_POLL_TIMER_MS 5 static struct xbee_ctx xbee_ctx[XBEE_MAX_CHANNEL]; @@ -435,6 +437,8 @@ int xbeeapp_send_atcmd(char *atcmd_str, void *param, struct xbee_ctx ctx; /* struct xbee_atcmd_hdr atcmd_hdr; -> no needed same than atcmd_str */ struct xbee_msg msg; + uint8_t prio; + int ret; memset(&ctx, 0, sizeof(ctx)); ctx.atcmd_query[0] = atcmd_str[0]; @@ -448,10 +452,11 @@ int xbeeapp_send_atcmd(char *atcmd_str, void *param, msg.iov[1].buf = param; msg.iov[1].len = param_len; - if (xbeeapp_send(&ctx, XBEE_TYPE_ATCMD, &msg, foreground) < 0) - return -1; + prio = callout_mgr_set_prio(&xbeeboard.intr_cm, XBEE_PRIO); + ret = xbeeapp_send(&ctx, XBEE_TYPE_ATCMD, &msg, foreground); + callout_mgr_set_prio(&xbeeboard.intr_cm, prio); - return 0; + return ret; } int xbeeapp_send_msg(uint64_t addr, struct xbee_msg *msg, int foreground) @@ -460,12 +465,15 @@ int xbeeapp_send_msg(uint64_t addr, struct xbee_msg *msg, int foreground) struct xbee_xmit_hdr xmit_hdr; struct xbee_msg msg2; unsigned i; + uint8_t prio; + int ret; if (msg->iovlen + 2 > XBEE_MSG_MAXIOV) { printf_P(PSTR("too many iovecs\r\n")); return -1; } + xmit_hdr.dstaddr = htonll(addr); xmit_hdr.reserved = htons(0xFFFE); xmit_hdr.bcast_radius = 0; @@ -480,10 +488,11 @@ int xbeeapp_send_msg(uint64_t addr, struct xbee_msg *msg, int foreground) memset(&ctx, 0, sizeof(ctx)); ctx.atcmd_query[0] = '\0'; - if (xbeeapp_send(&ctx, XBEE_TYPE_XMIT, &msg2, foreground) < 0) - return -1; + prio = callout_mgr_set_prio(&xbeeboard.intr_cm, XBEE_PRIO); + ret = xbeeapp_send(&ctx, XBEE_TYPE_XMIT, &msg2, foreground); + callout_mgr_set_prio(&xbeeboard.intr_cm, prio); - return 0; + return ret; } static void evt_timeout(struct callout_mgr *cm, struct callout *clt, @@ -517,8 +526,18 @@ void xbee_unload_timeout(struct xbee_ctx *ctx) callout_stop(&xbeeboard.mainloop_cm, &ctx->timeout); } +static void xbee_rx_poll_timer_cb(struct callout_mgr *cm, struct callout *tim, + void *arg) +{ + (void) arg; + xbee_rx(xbee_dev); + callout_reschedule(cm, tim, XBEE_POLL_TIMER_MS); +} + void xbee_mainloop(void) { + uint8_t prio; + while (1) { callout_manage(&xbeeboard.mainloop_cm); @@ -533,11 +552,14 @@ void xbee_mainloop(void) /* from cmdline to xbee */ c = cmdline_dev_recv(NULL); if (c == 4) { /* CTRL-d */ + prio = callout_mgr_set_prio(&xbeeboard.intr_cm, + XBEE_PRIO); xbee_dev_send('A', NULL); xbee_dev_send('T', NULL); xbee_dev_send('C', NULL); xbee_dev_send('N', NULL); xbee_dev_send('\n', NULL); + callout_mgr_set_prio(&xbeeboard.intr_cm, prio); xbee_raw = 0; rdline_newline(&xbeeboard.rdl, xbeeboard.prompt); @@ -553,7 +575,8 @@ void xbee_mainloop(void) else { if (xbee_cmdline_input_enabled) cmdline_poll(); - xbee_rx(xbee_dev); + /* xbee rx polling is done in a timer, so we can block + * the cmdline without loosing incoming packets */ } } } @@ -567,3 +590,11 @@ void xbee_stdin_disable(void) { xbee_cmdline_input_enabled = 0; } + +void xbeeapp_init(void) +{ + callout_init(&xbeeboard.xbee_rx_poll_timer, xbee_rx_poll_timer_cb, + NULL, XBEE_PRIO); + callout_schedule(&xbeeboard.intr_cm, + &xbeeboard.xbee_rx_poll_timer, XBEE_POLL_TIMER_MS); +} diff --git a/xbee_user.h b/xbee_user.h index daedeee..0634d00 100644 --- a/xbee_user.h +++ b/xbee_user.h @@ -78,4 +78,6 @@ void xbee_unload_timeout(struct xbee_ctx *ctx); void xbee_mainloop(void); +void xbeeapp_init(void); + #endif /* _XBEE_USER_H_ */ -- 2.39.5