]> git.droids-corp.org - protos/xbee-avr.git/commitdiff
move xbee rx polling in a timer
authorOlivier Matz <zer0@droids-corp.org>
Wed, 26 Feb 2014 19:44:48 +0000 (20:44 +0100)
committerOlivier Matz <zer0@droids-corp.org>
Sun, 9 Mar 2014 21:13:08 +0000 (22:13 +0100)
main.c
main.h
xbee_user.c
xbee_user.h

diff --git a/main.c b/main.c
index c438575f4eb875cbca03fa05072376081c4a59d6..0fb75d141f468f038b1a985f5f3c08743020642a 100644 (file)
--- 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 001ecc0537757547a78e0c5d623da3bf395c0786..18ddf0514d8c19c763a1599512b4e68b717b6dfc 100644 (file)
--- a/main.h
+++ b/main.h
 #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);
-
index 471731384dee0944c022e7cd39e0a73939cc49cb..ca9d4756799b8fe7c13f55cc29a88f48e6b84657 100644 (file)
@@ -36,6 +36,7 @@
 #include <aversive/endian.h>
 #include <aversive/pgmspace.h>
 
+#include <callout.h>
 #include <rdline.h>
 #include <xbee.h>
 #include <xbee_rxtx.h>
@@ -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);
+}
index daedeee96c5578d64a7ed213dce8e60de984579a..0634d00cd0b6c25b0d2ec4ca46e4f570bc5e6d4d 100644 (file)
@@ -78,4 +78,6 @@ void xbee_unload_timeout(struct xbee_ctx *ctx);
 
 void xbee_mainloop(void);
 
+void xbeeapp_init(void);
+
 #endif /* _XBEE_USER_H_ */