From 7003c9c35bdb7f8ffef07a9eaf58bf46af55b88f Mon Sep 17 00:00:00 2001 From: Werner Almesberger Date: Wed, 20 Jun 2012 07:26:52 -0300 Subject: [PATCH] fw/: added image sweep (completely untested) --- fw/Makefile | 2 +- fw/antorcha.c | 8 ++- fw/io.h | 14 +++++ fw/proto.h | 34 ++++++++++++ fw/sweep.c | 145 ++++++++++++++++++++++++++++++++++++++++++++++++++ fw/sweep.h | 45 ++++++++++++++++ 6 files changed, 242 insertions(+), 6 deletions(-) create mode 100644 fw/sweep.c create mode 100644 fw/sweep.h diff --git a/fw/Makefile b/fw/Makefile index bcbb2a7..365acd2 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 dispatch.o hash.o reset.o $(COMMON_OBJS) +OBJS = $(NAME).o dispatch.o hash.o reset.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 3c822a0..e6477f6 100644 --- a/fw/antorcha.c +++ b/fw/antorcha.c @@ -14,13 +14,9 @@ #include #include -#include -#define F_CPU 8000000UL -#include - -#include "io.h" #include "rf.h" #include "dispatch.h" +#include "sweep.h" static const struct handler *protos[] = { @@ -39,6 +35,8 @@ int main(void) * It has also brought up RF and the underlying SPI. */ + sweep_init(); + while (1) { got = rf_recv(buf, sizeof(buf)); if (got > 2) diff --git a/fw/io.h b/fw/io.h index ab616bb..e4fc493 100644 --- a/fw/io.h +++ b/fw/io.h @@ -16,6 +16,20 @@ #include +#define LED_A1 C, 0 +#define LED_A2 C, 1 +#define LED_A3 C, 2 +#define LED_A4 C, 3 +#define LED_A5 C, 4 +#define LED_A6 C, 5 +#define LED_A7 D, 0 +#define LED_A8 D, 1 + +#define LED_B1 D, 2 +#define LED_B2 D, 3 +#define LED_B3 D, 4 +#define LED_B4 B, 6 +#define LED_B5 B, 7 #define LED_B6 D, 5 #define LED_B7 D, 6 #define LED_B8 D, 7 diff --git a/fw/proto.h b/fw/proto.h index 006cf15..48a6813 100644 --- a/fw/proto.h +++ b/fw/proto.h @@ -13,6 +13,9 @@ #ifndef PROTO_H #define PROTO_H +#include + + #define PAYLOAD 64 /* most messages use a fixed 64 bytes payload */ enum pck_type { @@ -27,4 +30,35 @@ enum pck_type { PARAM_ACK = 9, /* parameter upload acknowledgement */ }; + +struct params { + /* Timer ticks */ + + uint16_t clkT_period; /* Timer period */ + + /* Accelerator thresholds */ + + uint16_t xa_high; /* X acceleration high threshold */ + uint16_t xa_low; /* X acceleration low threshold */ + + /* Pixel offsets (in image) */ + + uint8_t px_fwd_img_first; /* first column in forward move */ + uint8_t px_fwd_img_end; /* last column in forward move */ + uint8_t px_bwd_img_first; /* first (high) col. in backward move */ + uint8_t px_bwd_img_end; /* last (low) column in backward move */ + + /* Timer periods, for imaging */ + + uint16_t tp_fwd_start; /* forward image start */ + uint16_t tp_bwd_start; /* backward image start */ + uint8_t tp_fwd_pix; /* pixel size in forward move */ + uint8_t tp_bwd_pix; /* pixel size in backward move */ + + /* Timer periods, for estimation */ + + uint16_t tp_fwd; /* forward half-period */ + uint16_t tp_bwd; /* backward half-period */ +}; + #endif /* !PROTO_H */ diff --git a/fw/sweep.c b/fw/sweep.c new file mode 100644 index 0000000..94ca0d2 --- /dev/null +++ b/fw/sweep.c @@ -0,0 +1,145 @@ +/* + * fw/image.c - Image sweep + * + * 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 "sweep.h" + + +struct line image[MAX_LINES]; + +static uint32_t t_sw; /* cumulative number of timer ticks in sweep */ + +static uint16_t wait_periods; /* number of periods to wait before image */ +static uint16_t wait_period; /* ticks in wait period */ +static uint16_t wait_short; /* ticks to wait after periods */ + +static uint16_t pixel_ticks; /* number of ticks per pixel */ +static const struct line *curr_line; +static const struct line *end_line; +static bool forward; + + +ISR(TIMER1_OVF_vect) +{ + /* update the sweep time */ + + t_sw += OCR1A; + + /* if at the end of the image, only update the time */ + + if (curr_line == end_line) + return; + + /* wait before starting the image */ + + if (wait_periods) { + if (!--wait_periods) + OCR1A = wait_short; + return; + } + + /* output the next line */ + + PORTB = (PORTB & 0x3f) | (curr_line->cb & 0xc0); + PORTC = curr_line->cb; + PORTD = curr_line->d; + + /* move to the next line */ + + if (forward) + curr_line++; + else + curr_line--; + + /* wait until the next pixel (or slow down if we're done) */ + + if (curr_line == end_line) + OCR1A = 0xffff; + else + OCR1A = pixel_ticks; +} + + +void image_sweep(const struct sweep *sweep) +{ + TCCR1B = 0; /* stop the timer */ + + cli(); + + /* shut down all the LEDs, in case we're still in a sweep */ + + PORTB &= 0x3f; + PORTC = 0; + PORTD = 0; + + /* image pointers and sweep direction */ + + forward = sweep->forward; + if (forward) { + curr_line = image+sweep->left; + end_line = image+sweep->right+1; + } else { + curr_line = image+sweep->right; + end_line = image+sweep->left-1; + } + + /* timing parameters */ + + wait_periods = sweep->wait_ticks >> 16; + if (wait_periods) { + wait_short = sweep->wait_ticks; + wait_period = 0xffff; + + /* + * Make sure we have plenty of time to update OCR1A after an + * interrupt. + */ + if (wait_short < 256) { + wait_period -= 128; + wait_short += wait_periods << 7; + } + OCR1A = wait_period; + } else { + OCR1A = sweep->wait_ticks; + } + + /* prepare the hardware timer */ + + t_sw = 0; /* reset the time */ + TCNT1 = 0; + TIFR1 = 1 << TOV1; /* clear interrupt */ + + /* start the timer */ + + TCCR1B = + 1 << WGM13 | /* WG Mode 15, continued */ + 1 << WGM12 | + 1 << CS11; /* clkIO/8 */ +} + + +void sweep_init(void) +{ + TCCR1A = + 1 << WGM11 | /* WG Mode 15 (Fast PWM to OCR1A) */ + 1 << WGM10; + + TCNT1 = 0; + + TIMSK1 = 1 << TOIE1; /* enable timer interrupts */ +} diff --git a/fw/sweep.h b/fw/sweep.h new file mode 100644 index 0000000..33eb36a --- /dev/null +++ b/fw/sweep.h @@ -0,0 +1,45 @@ +/* + * fw/sweep.h - Image sweep + * + * 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 SWEEP_H +#define SWEEP_H + +#include +#include + + +#define MAX_LINES 100 + + +struct line { + uint8_t d; /* port D0-D7 */ + uint8_t cb; /* port C0-C5, B6-B7 */ +}; + + +struct sweep { + uint32_t wait_ticks; /* number of ticks to wait before image */ + uint16_t pixel_ticks; /* number of ticks per pixel */ + uint8_t left; /* leftmost line of image */ + uint8_t right; /* rightmost line of image */ + bool forward; /* direction of movement */ +}; + + +extern struct line image[MAX_LINES]; + + +void image_sweep(const struct sweep *sweep); +void sweep_init(void); + +#endif /* !SWEEP_H */