diff --git a/fw/Makefile b/fw/Makefile index 081fb1d..084e9b2 100644 --- a/fw/Makefile +++ b/fw/Makefile @@ -30,7 +30,8 @@ OBJCOPY = $(AVR_PREFIX)objcopy #OBJDUMP = $(AVR_PREFIX)objdump SIZE = $(AVR_PREFIX)size -OBJS = $(NAME).o dispatch.o hash.o image.o reset.o sweep.o $(COMMON_OBJS) +OBJS = $(NAME).o accel.o dispatch.o hash.o image.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/accel.c b/fw/accel.c new file mode 100644 index 0000000..5370260 --- /dev/null +++ b/fw/accel.c @@ -0,0 +1,68 @@ +/* + * fw/accel.c - Acceleration sensor + * + * 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 + +#include "io.h" +#include "accel.h" + + +void (*sample)(bool x, uint16_t v) = NULL; + + +static bool chan_x; + + +static inline void admux(bool x) +{ + ADMUX = + 1 << REFS0 | /* Vref is AVcc */ + (x ? ADC_X : ADC_Y); +} + + +static inline void adcsra(bool start) +{ + ADCSRA = + 1 << ADEN | /* enable ADC */ + (start ? 1 << ADSC : 0) | + 1 << ADIE | /* enable ADC interrupts */ + 7; /* clkADC = clk/128 -> 62.5 kHz */ +} + + +ISR(ADC_vect) +{ + uint16_t v; + + v = ADC; + if (sample) + sample(chan_x, v); + + chan_x = !chan_x; + admux(chan_x); + adcsra(1); +} + + +void accel_start(void) +{ + adcsra(0); + admux(1); + chan_x = 1; +} diff --git a/fw/accel.h b/fw/accel.h new file mode 100644 index 0000000..e5c22bc --- /dev/null +++ b/fw/accel.h @@ -0,0 +1,26 @@ +/* + * fw/accel.h - Acceleration sensor + * + * 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 ACCEL_H +#define ACCEL_H + +#include +#include + + +extern void (*sample)(bool x, uint16_t v); + + +void accel_start(void); + +#endif /* !ACCEL_H */ diff --git a/fw/antorcha.c b/fw/antorcha.c index c981932..0481ef9 100644 --- a/fw/antorcha.c +++ b/fw/antorcha.c @@ -17,6 +17,7 @@ #include "rf.h" #include "dispatch.h" #include "sweep.h" +#include "accel.h" #include "image.h" @@ -47,6 +48,7 @@ int main(void) */ sweep_init(); + accel_start(); while (1) { got = rf_recv(buf, sizeof(buf)); diff --git a/fw/io.h b/fw/io.h index 2ceb318..872ab25 100644 --- a/fw/io.h +++ b/fw/io.h @@ -34,6 +34,9 @@ #define LED_B7 D, 6 #define LED_B8 D, 7 +#define ADC_X 6 +#define ADC_Y 7 + #define RF_SCLK B, 5 #define RF_MISO B, 4 #define RF_MOSI B, 3