diff --git a/doc/PROTOCOL b/doc/PROTOCOL index 32d4784..64173c7 100644 --- a/doc/PROTOCOL +++ b/doc/PROTOCOL @@ -31,6 +31,12 @@ Protocol 9 n 0 Parameter ACK + 10 0 0 Sample (64 bytes payload) + + 11 0 0 Sample ACK + + 13 n 0 Samples + Unlock, firmware, image, and parameter packets all have a fixed-size 64 bytes payload. Pings and acknowledgements have no payload. Pongs may have a variable-size payload. @@ -45,3 +51,23 @@ Image format Two bytes per line. LSB of first byte is LED A1, MSB of last byte is B8. Unused lines must be set to zero. + + +Sample format +============= + +Each sample packet has the following structure: + + Offset Size + 0 2 Absolute time of X sample, high 16 bits + 2 8*N N samples + +Where each sample is + + Offset Size + 0 2 Absolute time of X sample, lower 16 bits + 2 2 X sample (0-1023) + 4 2 Absolute time of Y sample, lower 16 bits + 6 2 Y sample (0-1023) + +Byte order TBD. (Defined by avr-gcc) diff --git a/fw/Makefile b/fw/Makefile index 084e9b2..9ef0e6f 100644 --- a/fw/Makefile +++ b/fw/Makefile @@ -30,7 +30,7 @@ OBJCOPY = $(AVR_PREFIX)objcopy #OBJDUMP = $(AVR_PREFIX)objdump SIZE = $(AVR_PREFIX)size -OBJS = $(NAME).o accel.o dispatch.o hash.o image.o reset.o sweep.o \ +OBJS = $(NAME).o accel.o dispatch.o hash.o image.o reset.o sample.o sweep.o \ $(COMMON_OBJS) BOOT_OBJS = boot.o flash.o fw.o $(COMMON_OBJS) COMMON_OBJS = rf.o spi.o diff --git a/fw/antorcha.c b/fw/antorcha.c index 0481ef9..ac8c3fd 100644 --- a/fw/antorcha.c +++ b/fw/antorcha.c @@ -33,6 +33,7 @@ static struct sweep sweep = { static const struct handler *protos[] = { &image_handler, &reset_handler, + &sample_handler, NULL }; diff --git a/fw/dispatch.h b/fw/dispatch.h index 7529743..1085220 100644 --- a/fw/dispatch.h +++ b/fw/dispatch.h @@ -28,6 +28,7 @@ struct handler { extern struct handler image_handler; extern struct handler reset_handler; +extern struct handler sample_handler; bool dispatch(const uint8_t *buf, uint8_t len, const struct handler **protos); diff --git a/fw/proto.h b/fw/proto.h index 48a6813..675083d 100644 --- a/fw/proto.h +++ b/fw/proto.h @@ -28,6 +28,9 @@ enum pck_type { IMAGE_ACK = 7, /* image upload acknowledgement */ PARAM = 8, /* parameter upload */ PARAM_ACK = 9, /* parameter upload acknowledgement */ + SAMPLE = 10, /* start/stop sampling */ + SAMPLE_ACK = 11, /* start/stop sampling acknowledgement */ + SAMPLES = 13, /* samples */ }; diff --git a/fw/sample.c b/fw/sample.c new file mode 100644 index 0000000..7098f78 --- /dev/null +++ b/fw/sample.c @@ -0,0 +1,82 @@ +/* + * fw/sample.c - Acceleration sensor sample protocol + * + * Written 2012 by Werner Almesberger + * Copyright 2012 Werner Almesberger + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + */ + + +#include +#include +#include + +#include + +#include "rf.h" +#include "sweep.h" +#include "accel.h" +#include "proto.h" +#include "dispatch.h" + + +#define MAX_PACKET 120 /* <- MAX_PSDU -3 (hdr) -2 (CRC) */ + +static uint8_t buf[MAX_PACKET+3] = { SAMPLES, 0, 0 }; +static uint16_t *p; + + +static void handler(bool x, uint16_t v) +{ + bool first; + uint32_t t; + + first = p == (uint16_t *) (buf+3); + if (first && !x) + return; + t = uptime(); + if (first) + *p++ = t >> 16; + *p++ = t; + *p++ = v; + if (x) + return; + if ((uint8_t *) (p+4) <= buf+MAX_PACKET) + return; + rf_send(buf, (uint8_t *) p-buf); + p = (uint16_t *) (buf+3); +} + + +static bool sample_first(uint8_t limit, const uint8_t *payload) +{ + if (payload[0]) { + buf[1] = 0; + cli(); + sample = handler; + p = (uint16_t *) (buf+3); + sei(); + } else { + cli(); + sample = NULL; + sei(); + } + return 1; +} + + +static bool sample_more(uint8_t seq, uint8_t limit, const uint8_t *payload) +{ + return 0; +} + + +struct handler sample_handler = { + .type = SAMPLE, + .first = sample_first, + .more = sample_more, +};