2 * Copyright (c) 2011, Olivier MATZ <zer0@droids-corp.org>
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are met:
7 * * Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * * Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 * * Neither the name of the University of California, Berkeley nor the
13 * names of its contributors may be used to endorse or promote products
14 * derived from this software without specific prior written permission.
16 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND ANY
17 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19 * DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS BE LIABLE FOR ANY
20 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 * avrdude -p atmega1284p -P usb -c avrispmkii -U lfuse:w:0xff:m -U hfuse:w:0x91:m -U efuse:w:0xff:m
33 #include <aversive/queue.h>
34 #include <aversive/endian.h>
35 #include <aversive/wait.h>
36 #include <aversive/error.h>
53 #include "eeprom_config.h"
57 struct xbeeboard xbeeboard;
58 volatile uint32_t global_ms;
60 #define TIMEOUT_MS 1000
62 /* global xbee device */
63 struct xbee_dev *xbee_dev;
66 //static struct event stdin_read_event, xbee_read_event;
73 int xbee_cmdline_input_enabled = 1;
75 static struct xbee_ctx xbee_ctx[XBEE_MAX_CHANNEL];
77 static void hexdump(const char *title, const void *buf, unsigned int len)
79 unsigned int i, out, ofs;
80 const unsigned char *data = buf;
82 char line[LINE_LEN]; /* space needed 8+16*3+3+16 == 75 */
84 printf_P(PSTR("%s at [%p], len=%d\r\n"), title, data, len);
87 /* format 1 line in the buffer, then use printk to print them */
88 out = snprintf_P(line, LINE_LEN, PSTR("%08X"), ofs);
89 for (i=0; ofs+i < len && i<16; i++)
90 out += snprintf_P(line+out, LINE_LEN - out,
94 out += snprintf(line+out, LINE_LEN - out, " ");
95 for (i=0; ofs < len && i<16; i++, ofs++) {
96 unsigned char c = data[ofs];
97 if (!isascii(c) || !isprint(c))
99 out += snprintf_P(line+out,
103 printf_P(PSTR("%s\r\n"), line);
107 static int parse_xmit_status(struct xbee_ctx *ctx,
108 struct xbee_xmit_status_hdr *frame, unsigned len)
113 printf_P(PSTR("no context\r\n"));
117 /* see if it matches a xmit query (atcmd_query must be NULL) */
118 if (ctx->atcmd_query[0] != '\0') {
119 printf_P(PSTR("invalid response 2\r\n"));
123 /* XXX use defines for these values */
124 if (frame->delivery_status == 0x00)
125 printf_P(PSTR("Success\r\n"));
126 else if (frame->delivery_status == 0x01)
127 printf_P(PSTR("MAC ACK Failure\r\n"));
128 else if (frame->delivery_status == 0x15)
129 printf_P(PSTR("Invalid destination endpoint\r\n"));
130 else if (frame->delivery_status == 0x21)
131 printf_P(PSTR("Network ACK Failure\r\n"));
132 else if (frame->delivery_status == 0x25)
133 printf_P(PSTR("Route Not Found\r\n"));
138 static int dump_atcmd(struct xbee_ctx *ctx, struct xbee_atresp_hdr *frame,
142 const struct xbee_atcmd *cmd_pgm;
143 struct xbee_atcmd cmd;
149 } __attribute__((packed)) *result;
152 printf_P(PSTR("no context\r\n"));
156 /* get AT command from frame */
157 memcpy(atcmd_str, &frame->cmd, 2);
160 /* see if it matches query */
161 if (memcmp(atcmd_str, ctx->atcmd_query, 2)) {
162 printf_P(PSTR("invalid response <%c%c><%s><%s>\r\n"),
164 (frame->cmd >> 8) & 0xFF,
165 atcmd_str, ctx->atcmd_query);
169 /* see if it exists */
170 cmd_pgm = xbee_atcmd_lookup_name(atcmd_str);
171 if (cmd_pgm == NULL) {
172 printf_P(PSTR("unknown response\r\n"));
175 memcpy_P(&cmd, cmd_pgm, sizeof(cmd));
178 if (frame->status == 1) {
179 printf_P(PSTR("Status is error\r\n"));
182 else if (frame->status == 2) {
183 printf_P(PSTR("Invalid command\r\n"));
186 else if (frame->status == 3) {
187 printf_P(PSTR("Invalid parameter\r\n"));
190 else if (frame->status != 0) {
191 printf_P(PSTR("Unknown status error %d\r\n"), frame->status);
196 if (ctx->func != NULL)
197 ctx->func(frame, len, ctx->arg);
200 result = (void *)frame->data;
201 len -= offsetof(struct xbee_atresp_hdr, data);
202 if (cmd.flags & XBEE_ATCMD_F_PARAM_U8 && len == sizeof(uint8_t))
203 printf_P(PSTR("<%s> is 0x%x (%d)\r\n"), atcmd_str, result->u8,
205 else if (cmd.flags & XBEE_ATCMD_F_PARAM_U16 && len == sizeof(uint16_t))
206 printf_P(PSTR("<%s> is 0x%x (%d)\r\n"),
208 ntohs(result->u16), ntohs(result->u16));
209 else if (cmd.flags & XBEE_ATCMD_F_PARAM_U32 && len == sizeof(uint32_t))
210 printf_P(PSTR("<%s> is 0x%"PRIx32" (%"PRIu32")\r\n"),
212 ntohl(result->u32), ntohs(result->u32));
213 else if (cmd.flags & XBEE_ATCMD_F_PARAM_S16 && len == sizeof(int16_t))
214 printf_P(PSTR("<%s> is %d\r\n"), atcmd_str, ntohs(result->s16));
216 printf_P(PSTR("no data, status ok\r\n"));
218 hexdump("atcmd answer", frame->data, len);
225 int xbee_recv_data(struct xbee_recv_hdr *recvframe, unsigned len)
227 unsigned int datalen;
228 struct rc_proto_hdr *rch = (struct rc_proto_hdr *) &recvframe->data;
230 if (len < sizeof(*recvframe))
233 datalen = len - sizeof(*recvframe);
234 if (datalen < sizeof(struct rc_proto_hdr))
239 case RC_PROTO_TYPE_CHANNEL: {
240 struct rc_proto_channel *rcc =
241 (struct rc_proto_channel *) recvframe->data;
243 if (datalen != sizeof(struct rc_proto_channel))
245 val = ntohs(rcc->axis[0]);
248 spi_servo_set(0, val);
251 case RC_PROTO_TYPE_RANGE: {
252 struct rc_proto_range *rcr =
253 (struct rc_proto_range *) recvframe->data;
255 if (datalen != sizeof(struct rc_proto_range))
258 if (rcr->power_level >= MAX_POWER_LEVEL)
261 rc_proto_rx_range(rcr->power_level);
266 case RC_PROTO_HELLO: {
267 struct rc_proto_hello *rch =
268 (struct rc_proto_hello *) recvframe->data;
271 printf_P(PSTR("recv hello len=%d\r\n"),
283 /* socat /dev/ttyUSB0,raw,echo=0,b115200 /dev/ttyACM1,raw,echo=0,b115200 */
284 int8_t xbeeapp_rx(struct xbee_dev *dev, int channel, int type,
285 void *frame, unsigned len, void *opaque)
287 struct xbee_ctx *ctx = opaque;
291 printf_P(PSTR("type=0x%x, channel=%d, ctx=%p\r\n"),
294 /* if ctx is !NULL, it is an answer to a query */
296 /* XXX only delete timeout if answer matched */
297 xbee_unload_timeout(ctx);
298 if (xbee_debug && ctx->atcmd_query)
299 printf_P(PSTR("Received answer to query <%c%c>\r\n"),
300 ctx->atcmd_query[0], ctx->atcmd_query[1]);
303 /* some additional checks before sending */
305 case XBEE_TYPE_MODEM_STATUS: {
306 printf_P(PSTR("Received Modem Status frame\r\n"));
310 case XBEE_TYPE_RMT_ATRESP: {
314 #if BYTE_ORDER == LITTLE_ENDIAN
323 memcpy(&addr, frame, sizeof(addr));
324 addr.u64 = ntohll(addr.u64);
325 printf_P(PSTR("from remote address %"PRIx32"%"PRIx32"\r\n"),
326 addr.u32.high, addr.u32.low);
328 /* this answer contains an atcmd answer at offset 10 */
329 if (dump_atcmd(ctx, frame + 10, len - 10) < 0)
334 case XBEE_TYPE_ATRESP: {
335 if (dump_atcmd(ctx, frame, len) < 0)
341 case XBEE_TYPE_XMIT_STATUS: {
342 if (parse_xmit_status(ctx, frame, len) < 0)
348 case XBEE_TYPE_RECV: {
349 if (xbee_recv_data(frame, len) < 0)
355 case XBEE_TYPE_ATCMD:
356 case XBEE_TYPE_ATCMD_Q:
358 case XBEE_TYPE_EXPL_XMIT:
359 case XBEE_TYPE_RMT_ATCMD:
360 case XBEE_TYPE_EXPL_RECV:
361 case XBEE_TYPE_NODE_ID:
363 printf_P(PSTR("Invalid frame\r\n"));
369 hexdump("undecoded rx frame", frame, len);
370 else if (xbee_hexdump)
371 hexdump("undecoded rx frame", frame, len);
373 /* restart command line if it was a blocking query */
375 xbee_unregister_channel(dev, channel);
376 if (ctx->foreground) {
378 rdline_newline(&xbeeboard.rdl, xbeeboard.prompt);
383 static int xbeeapp_send(struct xbee_ctx *ctx, int type, void *buf, unsigned len,
389 if (len > XBEE_MAX_FRAME_LEN) {
390 printf_P(PSTR("frame too large\r\n"));
394 /* register a channel */
395 channel = xbee_register_channel(xbee_dev, XBEE_CHANNEL_ANY,
398 printf_P(PSTR("cannot send: no free channel\r\n"));
402 /* copy context in the static struct table (avoiding a malloc) */
403 memcpy(&xbee_ctx[channel], ctx, sizeof(*ctx));
404 ctx = &xbee_ctx[channel];
405 xbee_set_opaque(xbee_dev, channel, ctx);
408 printf_P(PSTR("send frame channel=%d type=0x%x len=%d\r\n"),
411 hexdump("xmit frame", buf, len);
413 /* transmit the frame on this channel */
414 ret = xbee_tx(xbee_dev, channel, type, buf, len);
416 printf_P(PSTR("cannot send\r\n"));
417 xbee_unregister_channel(xbee_dev, channel);
421 ctx->channel = channel;
422 xbee_load_timeout(ctx); /* load a timeout event */
424 /* suspend command line until we have answer or timeout */
427 rdline_stop(&xbeeboard.rdl); /* don't display prompt when return */
428 xbee_stdin_disable(); /* unload file descriptor polling */
434 /* send an AT command with parameters filled by caller. Disable
435 * command line until we get the answer or until a timeout occurs */
436 int xbeeapp_send_atcmd(const char *atcmd_str,
437 void *param, unsigned param_len, int foreground,
438 int (*func)(void *frame, unsigned len, void *arg), void *arg)
442 struct xbee_atcmd_hdr atcmd;
443 char buf[XBEE_MAX_FRAME_LEN];
444 } __attribute__((packed)) frame;
446 memset(&ctx, 0, sizeof(ctx));
447 ctx.atcmd_query[0] = atcmd_str[0];
448 ctx.atcmd_query[1] = atcmd_str[1];
452 memcpy(&frame.atcmd.cmd, atcmd_str, 2);
453 memcpy(&frame.buf, param, param_len);
455 if (xbeeapp_send(&ctx, XBEE_TYPE_ATCMD, &frame,
456 sizeof(struct xbee_atcmd_hdr) +
457 param_len, foreground) < 0) {
464 int xbeeapp_send_msg(uint64_t addr, void *data,
465 unsigned data_len, int foreground)
469 struct xbee_xmit_hdr xmit;
470 char buf[XBEE_MAX_FRAME_LEN];
471 } __attribute__((packed)) frame;
473 memset(&ctx, 0, sizeof(ctx));
474 ctx.atcmd_query[0] = '\0';
476 frame.xmit.dstaddr = htonll(addr);
477 frame.xmit.reserved = htons(0xFFFE);
478 frame.xmit.bcast_radius = 0;
480 memcpy(&frame.buf, data, data_len);
482 if (xbeeapp_send(&ctx, XBEE_TYPE_XMIT, &frame,
483 sizeof(struct xbee_xmit_hdr) +
484 data_len, foreground) < 0) {
491 void xbee_stdin_enable(void)
493 xbee_cmdline_input_enabled = 1;
496 void xbee_stdin_disable(void)
498 xbee_cmdline_input_enabled = 0;
501 static void evt_timeout(struct callout_mgr *cm, struct callout *clt,
504 struct xbee_ctx *ctx = arg;
509 printf_P(PSTR("Timeout\r\n"));
511 /* restart command line */
513 rdline_newline(&xbeeboard.rdl, xbeeboard.prompt);
516 xbee_unregister_channel(xbee_dev, ctx->channel);
518 callout_stop(cm, clt);
521 void xbee_load_timeout(struct xbee_ctx *ctx)
523 callout_init(&ctx->timeout, evt_timeout, ctx, 0);
524 callout_schedule(&xbeeboard.mainloop_cm, &ctx->timeout, TIMEOUT_MS);
527 void xbee_unload_timeout(struct xbee_ctx *ctx)
529 callout_stop(&xbeeboard.mainloop_cm, &ctx->timeout);
532 void bootloader(void)
534 #define BOOTLOADER_ADDR 0x3f000
535 if (pgm_read_byte_far(BOOTLOADER_ADDR) == 0xff) {
536 printf_P(PSTR("Bootloader is not present\r\n"));
540 /* ... very specific :( */
554 /* __asm__ __volatile__ ("ldi r31,0xf8\n"); */
555 /* __asm__ __volatile__ ("ldi r30,0x00\n"); */
556 /* __asm__ __volatile__ ("eijmp\n"); */
559 void xbee_mainloop(void)
562 callout_manage(&xbeeboard.mainloop_cm);
567 /* from xbee to cmdline */
568 c = xbee_dev_recv(NULL);
570 cmdline_dev_send((uint8_t)c, NULL);
572 /* from cmdline to xbee */
573 c = cmdline_dev_recv(NULL);
574 if (c == 4) { /* CTRL-d */
575 xbee_dev_send('A', NULL);
576 xbee_dev_send('T', NULL);
577 xbee_dev_send('C', NULL);
578 xbee_dev_send('N', NULL);
579 xbee_dev_send('\n', NULL);
581 rdline_newline(&xbeeboard.rdl,
586 xbee_dev_send((uint8_t)c, NULL);
588 /* echo on cmdline */
589 cmdline_dev_send((uint8_t)c, NULL);
593 if (xbee_cmdline_input_enabled)
600 /* return time in milliseconds on unsigned 16 bits */
601 static uint16_t get_time_ms(void)
603 return (uint16_t)global_ms;
606 static void main_timer_interrupt(void)
608 static uint16_t cycles;
614 if (global_ms & 0x80)
624 /* interrupt every 2048 cycles */
626 if (cycles >= 12000) {
631 /* called every 682us (at 12 Mhz), but global_ms is not incremented at
634 callout_manage(&xbeeboard.intr_cm);
644 DDRA = 0x07 /* LEDs */ | 0x10 /* buzzer */;
647 uart_register_rx_event(CMDLINE_UART, emergency);
649 fdevopen(cmdline_dev_send, cmdline_dev_recv);
650 xbee_file = fdevopen(xbee_dev_send, xbee_dev_recv);
652 timer0_register_OV_intr(main_timer_interrupt);
654 callout_mgr_init(&xbeeboard.intr_cm, get_time_ms);
655 callout_mgr_init(&xbeeboard.mainloop_cm, get_time_ms);
661 /* initialize libxbee */
668 /* open xbee device */
669 if (xbee_open(xbee_dev, xbee_file) < 0)
672 /* register default channel with a callback */
673 if (xbee_register_channel(xbee_dev, XBEE_DEFAULT_CHANNEL,
674 xbeeapp_rx, NULL) < 0) {
675 fprintf(stderr, "cannot register default channel\n");
680 eeprom_load_config();
682 printf_P(PSTR("\r\n"));
683 rdline_newline(&xbeeboard.rdl, xbeeboard.prompt);