From 27455749bd18fa24156ff89af6730ecd9e9ff79e Mon Sep 17 00:00:00 2001 From: Werner Almesberger Date: Thu, 21 Jun 2012 12:12:10 -0300 Subject: [PATCH 01/14] fw/: add acceleration sensor sample protocol (untested) --- doc/PROTOCOL | 26 ++++++++++++++++ fw/Makefile | 2 +- fw/antorcha.c | 1 + fw/dispatch.h | 1 + fw/proto.h | 3 ++ fw/sample.c | 84 +++++++++++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 116 insertions(+), 1 deletion(-) create mode 100644 fw/sample.c 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..6967597 --- /dev/null +++ b/fw/sample.c @@ -0,0 +1,84 @@ +/* + * 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" + + +/* @@@ keep it small for now - we're running out of RAM :-( */ +//#define MAX_PACKET 120 /* <- MAX_PSDU -3 (hdr) -2 (CRC) */ +#define MAX_PACKET 60 /* <- 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, +}; From 7356fccbfd251419e0ab7758f4ca2b5ccb6b191a Mon Sep 17 00:00:00 2001 From: Werner Almesberger Date: Thu, 21 Jun 2012 12:57:35 -0300 Subject: [PATCH 02/14] tools/Makefile: target "off" to load an empty image --- tools/Makefile | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/tools/Makefile b/tools/Makefile index c4b3802..c95f5df 100644 --- a/tools/Makefile +++ b/tools/Makefile @@ -20,7 +20,7 @@ LDLIBS = -L../../ben-wpan/tools/lib -latrf -lusb OBJS = antorcha.o hash.o -.PHONY: all update ping +.PHONY: all update ping off all: $(MAIN) @@ -32,3 +32,6 @@ update: ping: ./antorcha -P + +off: + ./antorcha /dev/null From c7494c80fac7ae9be63a3943e92adbab50694ebd Mon Sep 17 00:00:00 2001 From: Werner Almesberger Date: Thu, 21 Jun 2012 16:14:56 -0300 Subject: [PATCH 03/14] fw/accel.c: fix update() add interrupt-friendly update_irq() Still untested. --- fw/sweep.c | 14 +++++++++++++- fw/sweep.h | 2 ++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/fw/sweep.c b/fw/sweep.c index 059c286..3aac954 100644 --- a/fw/sweep.c +++ b/fw/sweep.c @@ -84,6 +84,17 @@ ISR(TIMER1_OVF_vect) } +uint32_t uptime_irq(void) +{ + uint32_t t; + + t = t_up+TCNT1; + if (TIFR1 & TOV1) + t += ICR1; + return t; +} + + uint32_t uptime(void) { uint32_t a, b; @@ -92,7 +103,8 @@ uint32_t uptime(void) do { cli(); a = t_up; - d = ICR1; + d = TCNT1; + cli(); b = t_up; sei(); } diff --git a/fw/sweep.h b/fw/sweep.h index 08b325e..b0113e4 100644 --- a/fw/sweep.h +++ b/fw/sweep.h @@ -29,7 +29,9 @@ struct sweep { extern volatile bool sweeping; +uint32_t uptime_irq(void); uint32_t uptime(void); + void sweep_image(const struct sweep *sweep); void sweep_init(void); From 58e0dda95d89dc652422961e6f5b823b41a86175 Mon Sep 17 00:00:00 2001 From: Werner Almesberger Date: Thu, 21 Jun 2012 16:15:56 -0300 Subject: [PATCH 04/14] fw/accel.c: use timer 0 to trigger a pair of conversions (X/Y) only every 1 ms --- fw/accel.c | 27 +++++++++++++++++++++++---- 1 file changed, 23 insertions(+), 4 deletions(-) diff --git a/fw/accel.c b/fw/accel.c index 5370260..637a655 100644 --- a/fw/accel.c +++ b/fw/accel.c @@ -54,8 +54,18 @@ ISR(ADC_vect) if (sample) sample(chan_x, v); - chan_x = !chan_x; - admux(chan_x); + if (chan_x) { + chan_x = 0; + admux(0); + adcsra(1); + } +} + + +ISR(TIMER0_OVF_vect) +{ + chan_x = 1; + admux(1); adcsra(1); } @@ -63,6 +73,15 @@ ISR(ADC_vect) void accel_start(void) { adcsra(0); - admux(1); - chan_x = 1; + + TCNT0 = 0; + OCR0A = 125; /* 8 MHz/64/125 = 1 kHz */ + TCCR0A = + 1 << WGM01 | /* WG Mode 7 (Fast PWM to OCR0A) */ + 1 << WGM00; + TCCR0B = + 1 << WGM02 | /* WG Mode 7, continued */ + 1 << CS01 | /* clkIO/64 */ + 1 << CS00; + TIMSK0 = 1 << TOIE0; /* interrupt on overflow */ } From 8eca4c99c74a195272740ee944affd783734fff5 Mon Sep 17 00:00:00 2001 From: Werner Almesberger Date: Thu, 21 Jun 2012 16:17:47 -0300 Subject: [PATCH 05/14] fw/sample.c: fix logic in handler() and filter incomplete samples An incomplete sample would be an X value followed by another X value, or Y followed by Y. Not entirely sure why this happens in the first place, but it does happen. --- fw/sample.c | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/fw/sample.c b/fw/sample.c index 6967597..7e76b07 100644 --- a/fw/sample.c +++ b/fw/sample.c @@ -23,33 +23,37 @@ #include "proto.h" #include "dispatch.h" +#include "io.h" /* @@@ keep it small for now - we're running out of RAM :-( */ //#define MAX_PACKET 120 /* <- MAX_PSDU -3 (hdr) -2 (CRC) */ -#define MAX_PACKET 60 /* <- MAX_PSDU -3 (hdr) -2 (CRC) */ +#define MAX_PACKET 50 /* <- MAX_PSDU -3 (hdr) -2 (CRC) */ static uint8_t buf[MAX_PACKET+3] = { SAMPLES, 0, 0 }; static uint16_t *p; +static bool expect_x; static void handler(bool x, uint16_t v) { - bool first; uint32_t t; - first = p == (uint16_t *) (buf+3); - if (first && !x) + if (x != expect_x) return; - t = uptime(); - if (first) + t = 0; //uptime_irq(); + if (p == (uint16_t *) (buf+3)) *p++ = t >> 16; *p++ = t; *p++ = v; + expect_x = !expect_x; + if (x) return; if ((uint8_t *) (p+4) <= buf+MAX_PACKET) return; + rf_send(buf, (uint8_t *) p-buf); + buf[1]++; p = (uint16_t *) (buf+3); } @@ -61,6 +65,7 @@ static bool sample_first(uint8_t limit, const uint8_t *payload) cli(); sample = handler; p = (uint16_t *) (buf+3); + expect_x = 1; sei(); } else { cli(); From 734e33cd192109cc3ac1c8e0b0bc75cb1e7196be Mon Sep 17 00:00:00 2001 From: Werner Almesberger Date: Thu, 21 Jun 2012 16:20:21 -0300 Subject: [PATCH 06/14] doc/PROTOCOL: clarify byte order in SAMPLES messages --- doc/PROTOCOL | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/PROTOCOL b/doc/PROTOCOL index 64173c7..0eed887 100644 --- a/doc/PROTOCOL +++ b/doc/PROTOCOL @@ -70,4 +70,4 @@ Where each sample is 4 2 Absolute time of Y sample, lower 16 bits 6 2 Y sample (0-1023) -Byte order TBD. (Defined by avr-gcc) +Byte order is little-endian. From 4eb1065dd7f430f37d0e8cc9b137e7dbde6ab4e3 Mon Sep 17 00:00:00 2001 From: Werner Almesberger Date: Thu, 21 Jun 2012 16:21:56 -0300 Subject: [PATCH 07/14] tools/: add sampling mode (-S) with graphical output --- tools/Makefile | 8 ++-- tools/antorcha.c | 68 +++++++++++++++++++++++++++---- tools/plot.c | 101 +++++++++++++++++++++++++++++++++++++++++++++++ tools/plot.h | 20 ++++++++++ 4 files changed, 187 insertions(+), 10 deletions(-) create mode 100644 tools/plot.c create mode 100644 tools/plot.h diff --git a/tools/Makefile b/tools/Makefile index c95f5df..6811779 100644 --- a/tools/Makefile +++ b/tools/Makefile @@ -15,10 +15,12 @@ MAIN = antorcha CFLAGS = -g -I../fw -I../../ben-wpan/tools/include \ -I../../ben-wpan/atusb/fw/include \ - -Wall -LDLIBS = -L../../ben-wpan/tools/lib -latrf -lusb + -Wall \ + $(shell sdl-config --cflags) +LDLIBS = -L../../ben-wpan/tools/lib -latrf -lusb \ + $(shell sdl-config --libs) -lSDL_gfx -OBJS = antorcha.o hash.o +OBJS = antorcha.o hash.o plot.o .PHONY: all update ping off diff --git a/tools/antorcha.c b/tools/antorcha.c index d0ca4d8..5cb969c 100644 --- a/tools/antorcha.c +++ b/tools/antorcha.c @@ -22,9 +22,11 @@ #include #include -#include #include +#include "hash.h" +#include "plot.h" + static int verbose = 1; static int debug = 0; @@ -275,6 +277,52 @@ static void image(struct atrf_dsc *dsc, const char *name) } +/* ----- Samples ----------------------------------------------------------- */ + + +static void samples(struct atrf_dsc *dsc) +{ + uint8_t buf[MAX_PSDU] = { 0, }; + int got; + uint8_t *s; + int x, y; + + buf[0] = 1; + packet(dsc, SAMPLE, 0, 0, buf, PAYLOAD); + plot_init(); + while (1) { + got = rf_recv(dsc, buf, sizeof(buf)); + if (got <= 3) + continue; + if (buf[0] != SAMPLES) + continue; + if (debug > 1) { + int i; + + for (i = 0; i != got; i++) + fprintf(stderr, " %02x", buf[i]); + fprintf(stderr, "\n"); + } + if (debug) + fprintf(stderr, "%d:", got); + s = buf+3+2; + while (s < buf+got-2) { + s += 2; + x = *s++; + x |= *s++ << 8; + s += 2; + y = *s++; + y |= *s++ << 8; + if (debug) + fprintf(stderr, "\t%d %d\n", x, y); + plot(x, y); + } + } + buf[0] = 0; + packet(dsc, SAMPLE, 0, 0, buf, PAYLOAD); +} + + /* ----- Command-line processing ------------------------------------------- */ @@ -284,7 +332,8 @@ static void usage(const char *name) "usage: %s [-d] image_file\n" "%6s %s [-d] -F firmware_file\n" "%6s %s [-d] -P\n" - , name, "", name, "", name); + "%6s %s [-d] -S\n" + , name, "", name, "", name, "", name); exit(1); } @@ -292,14 +341,14 @@ static void usage(const char *name) int main(int argc, char **argv) { const char *fw = NULL; - int do_ping = 0; + int do_ping = 0, do_sample = 0; struct atrf_dsc *dsc; int c; - while ((c = getopt(argc, argv, "dF:P")) != EOF) + while ((c = getopt(argc, argv, "dF:PS")) != EOF) switch (c) { case 'd': - debug = 1; + debug++; break; case 'F': fw = optarg; @@ -307,13 +356,16 @@ int main(int argc, char **argv) case 'P': do_ping = 1; break; + case 'S': + do_sample = 1; + break; default: usage(*argv); } - if (do_ping && fw) + if (do_ping+do_sample+!!fw > 1) usage(*argv); - if (do_ping || fw) { + if (do_ping || do_sample || fw) { if (argc != optind) usage(*argv); } else { @@ -328,6 +380,8 @@ int main(int argc, char **argv) rf_init(dsc, 8, 15); if (do_ping) ping(dsc); + else if (do_sample) + samples(dsc); else if (fw) firmware(dsc, fw); else diff --git a/tools/plot.c b/tools/plot.c new file mode 100644 index 0000000..6e61390 --- /dev/null +++ b/tools/plot.c @@ -0,0 +1,101 @@ +/* + * tools/plot.c - Sample plot + * + * 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 "SDL.h" +#include "SDL_gfxPrimitives.h" + + +#define XRES 1024 +#define YRES 1024 + +#define RADIUS 3 + +#define OLD_RGBA 0xffffff20 +#define NEW_RGBA 0xffffffff + + +static SDL_Surface *surf; +static int first = 1; +static int last_x, last_y; + +static SDL_Surface *back; +static SDL_Rect back_rect = { + .x = 0, + .y = 0, + .w = 2*RADIUS+1, + .h = 2*RADIUS+1, +}; + +static SDL_Rect front_rect = { + .w = 2*RADIUS+1, + .h = 2*RADIUS+1, +}; + + +void plot(int x, int y) +{ + if (!first) { + SDL_BlitSurface(back, &back_rect, surf, &front_rect); + SDL_LockSurface(surf); + filledCircleColor(surf, last_x, last_y, RADIUS, OLD_RGBA); + SDL_UnlockSurface(surf); + } + + front_rect.x = x; + front_rect.y = y; + x += RADIUS; + y += RADIUS; + + SDL_BlitSurface(surf, &front_rect, back, &back_rect); + SDL_LockSurface(surf); + filledCircleColor(surf, x, y, RADIUS, NEW_RGBA); + SDL_UnlockSurface(surf); + + if (!first) + SDL_UpdateRect(surf, + last_x-RADIUS, last_y-RADIUS, 2*RADIUS+1, 2*RADIUS+1); + SDL_UpdateRect(surf, x-RADIUS, y-RADIUS, 2*RADIUS+1, 2*RADIUS+1); + + first = 0; + last_x = x; + last_y = y; +} + + +void plot_init(void) +{ + const SDL_PixelFormat *f; + + if (SDL_Init(SDL_INIT_VIDEO) < 0) { + fprintf(stderr, "SDL_init: %s\n", SDL_GetError()); + exit(1); + } + atexit(SDL_Quit); + + surf = SDL_SetVideoMode(XRES+2*RADIUS+1, YRES+2*RADIUS+1, 0, + SDL_SWSURFACE); + if (!surf) { + fprintf(stderr, "SDL_SetVideoMode: %s\n", SDL_GetError()); + exit(1); + } + + f = surf->format; + back = SDL_CreateRGBSurface(SDL_SWSURFACE, 2*RADIUS+1, 2*RADIUS+1, + f->BitsPerPixel, f->Rmask, f->Gmask, f->Bmask, f->Amask); + if (!back) { + fprintf(stderr, "SDL_SetVideoMode: %s\n", SDL_GetError()); + exit(1); + } +} diff --git a/tools/plot.h b/tools/plot.h new file mode 100644 index 0000000..b2aea85 --- /dev/null +++ b/tools/plot.h @@ -0,0 +1,20 @@ +/* + * tools/plot.h - Sample plot + * + * 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. + */ + + +#ifndef PLOT_H +#define PLOT_H + +void plot(int x, int y); +void plot_init(void); + +#endif /* !PLOT_H */ From 87050bc12a828065e779d44f03fdf7e45accc8d3 Mon Sep 17 00:00:00 2001 From: Werner Almesberger Date: Thu, 21 Jun 2012 18:48:54 -0300 Subject: [PATCH 08/14] tools/: exit from sample mode by pressing Q or by closing the window --- tools/antorcha.c | 4 +++- tools/plot.c | 24 +++++++++++++++++++++++- tools/plot.h | 2 +- 3 files changed, 27 insertions(+), 3 deletions(-) diff --git a/tools/antorcha.c b/tools/antorcha.c index 5cb969c..408dbe6 100644 --- a/tools/antorcha.c +++ b/tools/antorcha.c @@ -315,9 +315,11 @@ static void samples(struct atrf_dsc *dsc) y |= *s++ << 8; if (debug) fprintf(stderr, "\t%d %d\n", x, y); - plot(x, y); + if (!plot(x, y)) + goto quit; } } +quit: buf[0] = 0; packet(dsc, SAMPLE, 0, 0, buf, PAYLOAD); } diff --git a/tools/plot.c b/tools/plot.c index 6e61390..42a0dee 100644 --- a/tools/plot.c +++ b/tools/plot.c @@ -16,6 +16,8 @@ #include "SDL.h" #include "SDL_gfxPrimitives.h" +#include "plot.h" + #define XRES 1024 #define YRES 1024 @@ -44,8 +46,10 @@ static SDL_Rect front_rect = { }; -void plot(int x, int y) +int plot(int x, int y) { + SDL_Event event; + if (!first) { SDL_BlitSurface(back, &back_rect, surf, &front_rect); SDL_LockSurface(surf); @@ -71,6 +75,24 @@ void plot(int x, int y) first = 0; last_x = x; last_y = y; + + while (SDL_PollEvent(&event)) + switch (event.type) { + case SDL_KEYDOWN: + switch (event.key.keysym.sym) { + case SDLK_q: + return 0; + default: + break; + } + break; + case SDL_QUIT: + return 0; + default: + break; + } + + return 1; } diff --git a/tools/plot.h b/tools/plot.h index b2aea85..fb6a621 100644 --- a/tools/plot.h +++ b/tools/plot.h @@ -14,7 +14,7 @@ #ifndef PLOT_H #define PLOT_H -void plot(int x, int y); +int plot(int x, int y); void plot_init(void); #endif /* !PLOT_H */ From 77bd5797d3a8d0faffc464199b20d5a41a50a688 Mon Sep 17 00:00:00 2001 From: Werner Almesberger Date: Thu, 21 Jun 2012 18:56:18 -0300 Subject: [PATCH 09/14] tools/plot.c: clear sample mode screen with C --- tools/plot.c | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/tools/plot.c b/tools/plot.c index 42a0dee..bbe3d6f 100644 --- a/tools/plot.c +++ b/tools/plot.c @@ -80,6 +80,13 @@ int plot(int x, int y) switch (event.type) { case SDL_KEYDOWN: switch (event.key.keysym.sym) { + case SDLK_c: + SDL_LockSurface(surf); + SDL_FillRect(surf, NULL, + SDL_MapRGB(surf->format, 0, 0, 0)); + SDL_UpdateRect(surf, 0, 0, 0, 0); + SDL_UnlockSurface(surf); + break; case SDLK_q: return 0; default: From 28cf696a08c1a4f26e2c83755a5110ef3a2622b5 Mon Sep 17 00:00:00 2001 From: Werner Almesberger Date: Thu, 21 Jun 2012 19:16:55 -0300 Subject: [PATCH 10/14] fw/sample.c (handler): set the time in samples (was set to zero, for debugging) --- fw/sample.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/fw/sample.c b/fw/sample.c index 7e76b07..4b1fcdf 100644 --- a/fw/sample.c +++ b/fw/sample.c @@ -40,7 +40,7 @@ static void handler(bool x, uint16_t v) if (x != expect_x) return; - t = 0; //uptime_irq(); + t = uptime_irq(); if (p == (uint16_t *) (buf+3)) *p++ = t >> 16; *p++ = t; From ea994c8ed036df3a8c22eebe8e55561c6e1ef458 Mon Sep 17 00:00:00 2001 From: Werner Almesberger Date: Thu, 21 Jun 2012 19:18:07 -0300 Subject: [PATCH 11/14] tools/antorcha.c (samples): show the timestamp when printing sample values --- tools/antorcha.c | 28 ++++++++++++++++++++++++---- 1 file changed, 24 insertions(+), 4 deletions(-) diff --git a/tools/antorcha.c b/tools/antorcha.c index 408dbe6..f47c87c 100644 --- a/tools/antorcha.c +++ b/tools/antorcha.c @@ -285,6 +285,7 @@ static void samples(struct atrf_dsc *dsc) uint8_t buf[MAX_PSDU] = { 0, }; int got; uint8_t *s; + uint16_t t_high, t_low, last; int x, y; buf[0] = 1; @@ -305,16 +306,35 @@ static void samples(struct atrf_dsc *dsc) } if (debug) fprintf(stderr, "%d:", got); - s = buf+3+2; + s = buf+3; + t_high = *s++; + t_high |= *s++ << 8; + last = 0; while (s < buf+got-2) { - s += 2; + t_low = *s++; + t_low |= *s++ << 8; + if (t_low < last) + t_high++; + last = t_low; x = *s++; x |= *s++ << 8; - s += 2; + + if (debug) + fprintf(stderr, "\t%11.6f %d", + (t_high << 16 | t_low)/1000000.0, x); + + t_low = *s++; + t_low |= *s++ << 8; + if (t_low < last) + t_high++; + last = t_low; y = *s++; y |= *s++ << 8; + if (debug) - fprintf(stderr, "\t%d %d\n", x, y); + fprintf(stderr, "\t%11.6f %d\n", + (t_high << 16 | t_low)/1000000.0, y); + if (!plot(x, y)) goto quit; } From ffcfd3d09d4c5555125418f5a6a90d99fd5b0f18 Mon Sep 17 00:00:00 2001 From: Werner Almesberger Date: Thu, 21 Jun 2012 19:38:44 -0300 Subject: [PATCH 12/14] fw/spi.c (spi_init): raise SPI speed from fosc/4 to fosc/2 --- fw/spi.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/fw/spi.c b/fw/spi.c index 0337caf..df09a36 100644 --- a/fw/spi.c +++ b/fw/spi.c @@ -26,11 +26,11 @@ void spi_init(void) { - /* SPI mode 0, MSB first, fosc/4 */ + /* SPI mode 0, MSB first, fosc/2 */ SPCR = 1 << SPE | /* enable SPI */ 1 << MSTR; /* master */ -// SPSR = 1 << SPI2X; /* enable for fosc/2 */ + SPSR = 1 << SPI2X; /* enable for fosc/2 */ } From b262e2f142e043c39d99053739330f55cf455276 Mon Sep 17 00:00:00 2001 From: Werner Almesberger Date: Thu, 21 Jun 2012 19:52:41 -0300 Subject: [PATCH 13/14] fw/spi.c (spi_init): revert the SPI speed increase (caused instability) Some more testing showed that transfers would get stuck after ~40 packets. Need to find out what happened before trying this again. --- fw/spi.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/fw/spi.c b/fw/spi.c index df09a36..0337caf 100644 --- a/fw/spi.c +++ b/fw/spi.c @@ -26,11 +26,11 @@ void spi_init(void) { - /* SPI mode 0, MSB first, fosc/2 */ + /* SPI mode 0, MSB first, fosc/4 */ SPCR = 1 << SPE | /* enable SPI */ 1 << MSTR; /* master */ - SPSR = 1 << SPI2X; /* enable for fosc/2 */ +// SPSR = 1 << SPI2X; /* enable for fosc/2 */ } From e2f228907b07219e673e1c02468e657999e853fd Mon Sep 17 00:00:00 2001 From: Werner Almesberger Date: Thu, 21 Jun 2012 21:12:20 -0300 Subject: [PATCH 14/14] fw/accel.c (adcsra): let ADC run at 125 kHz (instead of 67.5 kHz) --- fw/accel.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/fw/accel.c b/fw/accel.c index 637a655..1f45d66 100644 --- a/fw/accel.c +++ b/fw/accel.c @@ -38,11 +38,15 @@ static inline void admux(bool x) static inline void adcsra(bool start) { + /* + * The ADC needs to run at clkADC <= 200 kHz for full resolution. + * At clkADC = 125 kHz, a conversion takes about 110 us. + */ ADCSRA = 1 << ADEN | /* enable ADC */ (start ? 1 << ADSC : 0) | 1 << ADIE | /* enable ADC interrupts */ - 7; /* clkADC = clk/128 -> 62.5 kHz */ + 6; /* clkADC = clk/64 -> 126 kHz */ }