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:0x99: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>
49 #include <scheduler.h>
50 #include <clock_time.h>
55 #include "eeprom_config.h"
59 struct xbeeboard xbeeboard;
60 volatile uint16_t global_ms;
61 struct callout_manager cm;
63 #define TIMEOUT_MS 1000
65 /* global xbee device */
66 struct xbee_dev *xbee_dev;
69 //static struct event stdin_read_event, xbee_read_event;
76 int xbee_cmdline_input_enabled = 1;
78 static struct xbee_ctx xbee_ctx[XBEE_MAX_CHANNEL];
80 static void hexdump(const char *title, const void *buf, unsigned int len)
82 unsigned int i, out, ofs;
83 const unsigned char *data = buf;
85 char line[LINE_LEN]; /* space needed 8+16*3+3+16 == 75 */
87 printf_P(PSTR("%s at [%p], len=%d\r\n"), title, data, len);
90 /* format 1 line in the buffer, then use printk to print them */
91 out = snprintf_P(line, LINE_LEN, PSTR("%08X"), ofs);
92 for (i=0; ofs+i < len && i<16; i++)
93 out += snprintf_P(line+out, LINE_LEN - out,
97 out += snprintf(line+out, LINE_LEN - out, " ");
98 for (i=0; ofs < len && i<16; i++, ofs++) {
99 unsigned char c = data[ofs];
100 if (!isascii(c) || !isprint(c))
102 out += snprintf_P(line+out,
106 printf_P(PSTR("%s\r\n"), line);
110 static int parse_xmit_status(struct xbee_ctx *ctx,
111 struct xbee_xmit_status_hdr *frame, unsigned len)
116 printf_P(PSTR("no context\r\n"));
120 /* see if it matches a xmit query (atcmd_query must be NULL) */
121 if (ctx->atcmd_query[0] != '\0') {
122 printf_P(PSTR("invalid response 2\r\n"));
126 /* XXX use defines for these values */
127 if (frame->delivery_status == 0x00)
128 printf_P(PSTR("Success\r\n"));
129 else if (frame->delivery_status == 0x01)
130 printf_P(PSTR("MAC ACK Failure\r\n"));
131 else if (frame->delivery_status == 0x15)
132 printf_P(PSTR("Invalid destination endpoint\r\n"));
133 else if (frame->delivery_status == 0x21)
134 printf_P(PSTR("Network ACK Failure\r\n"));
135 else if (frame->delivery_status == 0x25)
136 printf_P(PSTR("Route Not Found\r\n"));
141 static int dump_atcmd(struct xbee_ctx *ctx, struct xbee_atresp_hdr *frame,
145 const struct xbee_atcmd *cmd_pgm;
146 struct xbee_atcmd cmd;
152 } __attribute__((packed)) *result;
155 printf_P(PSTR("no context\r\n"));
159 /* get AT command from frame */
160 memcpy(atcmd_str, &frame->cmd, 2);
163 /* see if it matches query */
164 if (memcmp(atcmd_str, ctx->atcmd_query, 2)) {
165 printf_P(PSTR("invalid response <%c%c><%s><%s>\r\n"),
167 (frame->cmd >> 8) & 0xFF,
168 atcmd_str, ctx->atcmd_query);
172 /* see if it exists */
173 cmd_pgm = xbee_atcmd_lookup_name(atcmd_str);
174 if (cmd_pgm == NULL) {
175 printf_P(PSTR("unknown response\r\n"));
178 memcpy_P(&cmd, cmd_pgm, sizeof(cmd));
181 if (frame->status == 1) {
182 printf_P(PSTR("Status is error\r\n"));
185 else if (frame->status == 2) {
186 printf_P(PSTR("Invalid command\r\n"));
189 else if (frame->status == 3) {
190 printf_P(PSTR("Invalid parameter\r\n"));
193 else if (frame->status != 0) {
194 printf_P(PSTR("Unknown status error %d\r\n"), frame->status);
199 if (ctx->func != NULL)
200 ctx->func(frame, len, ctx->arg);
203 result = (void *)frame->data;
204 len -= offsetof(struct xbee_atresp_hdr, data);
205 if (cmd.flags & XBEE_ATCMD_F_PARAM_U8 && len == sizeof(uint8_t))
206 printf_P(PSTR("<%s> is 0x%x\r\n"), atcmd_str, result->u8);
207 else if (cmd.flags & XBEE_ATCMD_F_PARAM_U16 && len == sizeof(uint16_t))
208 printf_P(PSTR("<%s> is 0x%x\r\n"),
211 else if (cmd.flags & XBEE_ATCMD_F_PARAM_U32 && len == sizeof(uint32_t))
212 printf_P(PSTR("<%s> is 0x%"PRIx32"\r\n"),
215 else if (cmd.flags & XBEE_ATCMD_F_PARAM_S16 && len == sizeof(int16_t))
216 printf_P(PSTR("<%s> is %d\r\n"), atcmd_str, ntohs(result->s16));
218 printf_P(PSTR("no data, status ok\r\n"));
220 hexdump("atcmd answer", frame->data, len);
227 int xbee_recv_data(struct xbee_recv_hdr *recvframe, unsigned len)
229 unsigned int datalen;
230 struct rc_proto_hdr *rch = (struct rc_proto_hdr *) &recvframe->data;
232 if (len < sizeof(*recvframe))
235 datalen = len - sizeof(*recvframe);
236 if (datalen < sizeof(struct rc_proto_hdr))
240 case RC_PROTO_TYPE_CHANNEL: {
241 struct rc_proto_channel *rcc =
242 (struct rc_proto_channel *) recvframe->data;
244 if (datalen != sizeof(struct rc_proto_channel))
246 val = ntohs(rcc->axis[0]);
249 spi_servo_set(0, val);
252 case RC_PROTO_TYPE_RANGE: {
253 struct rc_proto_range *rcr =
254 (struct rc_proto_range *) recvframe->data;
256 if (datalen != sizeof(struct rc_proto_range))
259 if (rcr->power_level >= MAX_POWER_LEVEL)
262 rc_proto_rx_range(rcr->power_level);
273 /* socat /dev/ttyUSB0,raw,echo=0,b115200 /dev/ttyACM1,raw,echo=0,b115200 */
274 void xbee_rx(struct xbee_dev *dev, int channel, int type,
275 void *frame, unsigned len, void *opaque)
277 struct xbee_ctx *ctx = opaque;
278 int do_hexdump = xbee_hexdump;
281 printf_P(PSTR("type=0x%x, channel=%d, ctx=%p\r\n"),
284 /* if ctx is !NULL, it is an answer to a query */
286 /* XXX only delete timeout if answer matched */
287 xbee_unload_timeout(ctx);
288 if (xbee_debug && ctx->atcmd_query)
289 printf_P(PSTR("Received answer to query <%c%c>\r\n"),
290 ctx->atcmd_query[0], ctx->atcmd_query[1]);
293 /* some additional checks before sending */
295 case XBEE_TYPE_MODEM_STATUS: {
296 printf_P(PSTR("Received Modem Status frame\r\n"));
300 case XBEE_TYPE_RMT_ATRESP: {
304 #if BYTE_ORDER == LITTLE_ENDIAN
313 memcpy(&addr, frame, sizeof(addr));
314 addr.u64 = ntohll(addr.u64);
315 printf_P(PSTR("from remote address %"PRIx32"%"PRIx32"\r\n"),
316 addr.u32.high, addr.u32.low);
318 /* this answer contains an atcmd answer at offset 10 */
319 if (dump_atcmd(ctx, frame + 10, len - 10) < 0)
323 case XBEE_TYPE_ATRESP: {
324 if (dump_atcmd(ctx, frame, len) < 0)
329 case XBEE_TYPE_XMIT_STATUS: {
330 if (parse_xmit_status(ctx, frame, len) < 0)
335 case XBEE_TYPE_RECV: {
336 if (xbee_recv_data(frame, len) < 0)
341 case XBEE_TYPE_ATCMD:
342 case XBEE_TYPE_ATCMD_Q:
344 case XBEE_TYPE_EXPL_XMIT:
345 case XBEE_TYPE_RMT_ATCMD:
346 case XBEE_TYPE_EXPL_RECV:
347 case XBEE_TYPE_NODE_ID:
349 printf_P(PSTR("Invalid frame\r\n"));
355 hexdump("undecoded rx frame", frame, len);
357 /* restart command line if it was a blocking query */
359 xbee_unregister_channel(dev, channel);
360 if (ctx->foreground) {
362 rdline_newline(&xbeeboard.rdl, xbeeboard.prompt);
367 static int xbeeapp_send(struct xbee_ctx *ctx, int type, void *buf, unsigned len,
373 if (len > XBEE_MAX_FRAME_LEN) {
374 printf_P(PSTR("frame too large\r\n"));
378 /* register a channel */
379 channel = xbee_register_channel(xbee_dev, XBEE_CHANNEL_ANY,
382 printf_P(PSTR("cannot send: no free channel\r\n"));
386 /* copy context in the static struct table (avoiding a malloc) */
387 memcpy(&xbee_ctx[channel], ctx, sizeof(*ctx));
388 ctx = &xbee_ctx[channel];
389 xbee_set_opaque(xbee_dev, channel, ctx);
392 printf_P(PSTR("send frame channel=%d type=0x%x len=%d\r\n"),
395 hexdump("xmit frame", buf, len);
397 /* transmit the frame on this channel */
398 ret = xbee_proto_xmit(xbee_dev, channel, type, buf,
401 printf_P(PSTR("cannot send\r\n"));
402 xbee_unregister_channel(xbee_dev, channel);
406 ctx->channel = channel;
407 xbee_load_timeout(ctx); /* load a timeout event */
409 /* suspend command line until we have answer or timeout */
412 rdline_stop(&xbeeboard.rdl); /* don't display prompt when return */
413 xbee_stdin_disable(); /* unload file descriptor polling */
419 /* send an AT command with parameters filled by caller. Disable
420 * command line until we get the answer or until a timeout occurs */
421 int xbeeapp_send_atcmd(const char *atcmd_str,
422 void *param, unsigned param_len, int foreground,
423 int (*func)(void *frame, unsigned len, void *arg), void *arg)
427 struct xbee_atcmd_hdr atcmd;
428 char buf[XBEE_MAX_FRAME_LEN];
429 } __attribute__((packed)) frame;
431 memset(&ctx, 0, sizeof(ctx));
432 ctx.atcmd_query[0] = atcmd_str[0];
433 ctx.atcmd_query[1] = atcmd_str[1];
437 memcpy(&frame.atcmd.cmd, atcmd_str, 2);
438 memcpy(&frame.buf, param, param_len);
440 if (xbeeapp_send(&ctx, XBEE_TYPE_ATCMD, &frame,
441 sizeof(struct xbee_atcmd_hdr) +
442 param_len, foreground) < 0) {
449 int xbeeapp_send_msg(uint64_t addr, void *data,
450 unsigned data_len, int foreground)
454 struct xbee_xmit_hdr xmit;
455 char buf[XBEE_MAX_FRAME_LEN];
456 } __attribute__((packed)) frame;
458 memset(&ctx, 0, sizeof(ctx));
459 ctx.atcmd_query[0] = '\0';
461 frame.xmit.dstaddr = htonll(addr);
462 frame.xmit.reserved = htons(0xFFFE);
463 frame.xmit.bcast_radius = 0;
465 memcpy(&frame.buf, data, data_len);
467 if (xbeeapp_send(&ctx, XBEE_TYPE_XMIT, &frame,
468 sizeof(struct xbee_xmit_hdr) +
469 data_len, foreground) < 0) {
476 void xbee_stdin_enable(void)
478 xbee_cmdline_input_enabled = 1;
481 void xbee_stdin_disable(void)
483 xbee_cmdline_input_enabled = 0;
486 static void evt_timeout(struct callout_manager *cm, struct callout *clt,
489 struct xbee_ctx *ctx = arg;
494 printf_P(PSTR("Timeout\r\n"));
496 /* restart command line */
498 rdline_newline(&xbeeboard.rdl, xbeeboard.prompt);
501 xbee_unregister_channel(xbee_dev, ctx->channel);
504 void xbee_load_timeout(struct xbee_ctx *ctx)
506 callout_reset(&cm, &ctx->timeout, TIMEOUT_MS, SINGLE, evt_timeout, ctx);
509 void xbee_unload_timeout(struct xbee_ctx *ctx)
511 callout_stop(&cm, &ctx->timeout);
514 void bootloader(void)
516 #define BOOTLOADER_ADDR 0x3f000
517 if (pgm_read_byte_far(BOOTLOADER_ADDR) == 0xff) {
518 printf_P(PSTR("Bootloader is not present\r\n"));
522 /* ... very specific :( */
536 /* __asm__ __volatile__ ("ldi r31,0xf8\n"); */
537 /* __asm__ __volatile__ ("ldi r30,0x00\n"); */
538 /* __asm__ __volatile__ ("eijmp\n"); */
541 void xbee_mainloop(void)
549 /* from xbee to cmdline */
550 c = xbee_dev_recv(NULL);
552 cmdline_dev_send((uint8_t)c, NULL);
554 /* from cmdline to xbee */
555 c = cmdline_dev_recv(NULL);
556 if (c == 4) { /* CTRL-d */
557 xbee_dev_send('A', NULL);
558 xbee_dev_send('T', NULL);
559 xbee_dev_send('C', NULL);
560 xbee_dev_send('N', NULL);
561 xbee_dev_send('\n', NULL);
563 rdline_newline(&xbeeboard.rdl,
568 xbee_dev_send((uint8_t)c, NULL);
570 /* echo on cmdline */
571 cmdline_dev_send((uint8_t)c, NULL);
575 if (xbee_cmdline_input_enabled)
577 xbee_proto_rx(xbee_dev);
582 /* return time in milliseconds on unsigned 16 bits */
583 static uint16_t get_time_ms(void)
588 static void main_timer_interrupt(void)
590 static uint16_t cycles;
595 /* interrupt every 2048 cycles */
597 if (cycles >= 12000) {
603 if (global_ms & 0x80)
613 /* call scheduler every 682us with interrupt unlocked */
615 if ((cpt & 0x3) == 0)
616 scheduler_interrupt();
626 DDRA = 0x07 /* LEDs */ | 0x10 /* buzzer */;
629 uart_register_rx_event(CMDLINE_UART, emergency);
631 fdevopen(cmdline_dev_send, cmdline_dev_recv);
632 xbee_file = fdevopen(xbee_dev_send, xbee_dev_recv);
635 timer0_register_OV_intr(main_timer_interrupt);
641 printf_P(PSTR("\r\n"));
642 rdline_newline(&xbeeboard.rdl, xbeeboard.prompt);
644 callout_manager_init(&cm, get_time_ms);
645 //callout_reset(&cm, &t1, 500, PERIODICAL, do_led_blink, NULL);
647 /* initialize libxbee */
654 /* open xbee device */
655 if (xbee_open(xbee_dev, xbee_file) < 0)
658 /* register default channel with a callback */
659 if (xbee_register_channel(xbee_dev, XBEE_DEFAULT_CHANNEL,
660 xbee_rx, NULL) < 0) {
661 fprintf(stderr, "cannot register default channel\n");
666 eeprom_load_config();