From 778e4889e1897c022906c56d0764fe163e24355b Mon Sep 17 00:00:00 2001 From: Lars-Dominik Braun Date: Sat, 8 Mar 2014 17:22:49 +0100 Subject: gyroscope: Rename to gyro --- Makefile | 2 +- gyro.c | 77 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ gyro.h | 13 +++++++++++ gyroscope.c | 77 ------------------------------------------------------------- gyroscope.h | 13 ----------- main.c | 10 ++++---- 6 files changed, 96 insertions(+), 96 deletions(-) create mode 100644 gyro.c create mode 100644 gyro.h delete mode 100644 gyroscope.c delete mode 100644 gyroscope.h diff --git a/Makefile b/Makefile index b273b86..9f3ae5c 100644 --- a/Makefile +++ b/Makefile @@ -2,7 +2,7 @@ MCU = atmega88 all: sanduhr.hex -sanduhr.elf: main.c i2c.c i2c.h uart.c uart.h timer.c timer.h gyroscope.c gyroscope.h +sanduhr.elf: main.c i2c.c i2c.h uart.c uart.h timer.c timer.h gyro.c gyro.h avr-gcc -std=gnu99 -mmcu=$(MCU) -Os -o $@ $^ sanduhr.hex: sanduhr.elf diff --git a/gyro.c b/gyro.c new file mode 100644 index 0000000..f4d7529 --- /dev/null +++ b/gyro.c @@ -0,0 +1,77 @@ +#include +#include +#include +#include + +#include "i2c.h" +#include "gyro.h" + +/* device address */ +#define L3GD20 0b11010100 +/* registers */ +#define L3GD20_WHOAMI 0xf +#define L3GD20_CTRLREG1 0x20 +#define L3GD20_CTRLREG3 0x22 +#define L3GD20_CTRLREG4 0x23 + +#define sleepwhile(cond) \ + sleep_enable (); \ + while (cond) { sleep_cpu (); } \ + sleep_disable (); + +/* the first interrupt is lost */ +volatile bool drdy = true; +volatile int16_t val[3] = {0, 0, 0}; + +/* data ready interrupt + */ +ISR(PCINT0_vect) { + drdy = true; +} + +void gyroInit () { + /* set PB1 to input, with pull-up */ + DDRB = (DDRB & ~((1 << PB1))); + PORTB = (PORTB | (1 << PB1)); + /* enable interrupt PCI0 */ + PCICR = (1 << PCIE0); + /* enable interrupts on PB1/PCINT1 */ + PCMSK0 = (1 << 1); +} + +/* XXX: make nonblocking */ +void gyroStart () { + /* configuration: + * disable power-down-mode + * defaults + * enable drdy interrupt + * select 500dps + */ + uint8_t data[] = {0b00001111, 0b0, 0b00001000, 0b00010000}; + + if (!twRequest (TWM_WRITE, L3GD20, L3GD20_CTRLREG1, data, + sizeof (data)/sizeof (*data))) { + printf ("cannot start write\n"); + } + sleepwhile (twr.status == TWST_WAIT); + printf ("final twi status was %i\n", twr.status); +} + +bool gyroRead () { + if (drdy) { + drdy = false; + + if (!twRequest (TWM_READ, L3GD20, 0x28, (uint8_t *) val, 6)) { + printf ("cannot start read\n"); + } + + return true; + } else { + return false; + } +} + +volatile const int16_t *gyroGet () { + return val; +} + diff --git a/gyro.h b/gyro.h new file mode 100644 index 0000000..ac3550e --- /dev/null +++ b/gyro.h @@ -0,0 +1,13 @@ +#ifndef GYROSCOPE_H +#define GYROSCOPE_H + +#include +#include + +void gyroInit (); +void gyroStart (); +bool gyroRead (); +volatile const int16_t *gyroGet (); + +#endif /* GYROSCOPE_H */ + diff --git a/gyroscope.c b/gyroscope.c deleted file mode 100644 index 23fa197..0000000 --- a/gyroscope.c +++ /dev/null @@ -1,77 +0,0 @@ -#include -#include -#include -#include - -#include "i2c.h" -#include "gyroscope.h" - -/* device address */ -#define L3GD20 0b11010100 -/* registers */ -#define L3GD20_WHOAMI 0xf -#define L3GD20_CTRLREG1 0x20 -#define L3GD20_CTRLREG3 0x22 -#define L3GD20_CTRLREG4 0x23 - -#define sleepwhile(cond) \ - sleep_enable (); \ - while (cond) { sleep_cpu (); } \ - sleep_disable (); - -/* the first interrupt is lost */ -volatile bool drdy = true; -volatile int16_t val[3] = {0, 0, 0}; - -/* data ready interrupt - */ -ISR(PCINT0_vect) { - drdy = true; -} - -void gyroscopeInit () { - /* set PB1 to input, with pull-up */ - DDRB = (DDRB & ~((1 << PB1))); - PORTB = (PORTB | (1 << PB1)); - /* enable interrupt PCI0 */ - PCICR = (1 << PCIE0); - /* enable interrupts on PB1/PCINT1 */ - PCMSK0 = (1 << 1); -} - -/* XXX: make nonblocking */ -void gyroscopeStart () { - /* configuration: - * disable power-down-mode - * defaults - * enable drdy interrupt - * select 500dps - */ - uint8_t data[] = {0b00001111, 0b0, 0b00001000, 0b00010000}; - - if (!twRequest (TWM_WRITE, L3GD20, L3GD20_CTRLREG1, data, - sizeof (data)/sizeof (*data))) { - printf ("cannot start write\n"); - } - sleepwhile (twr.status == TWST_WAIT); - printf ("final twi status was %i\n", twr.status); -} - -bool gyroscopeRead () { - if (drdy) { - drdy = false; - - if (!twRequest (TWM_READ, L3GD20, 0x28, (uint8_t *) val, 6)) { - printf ("cannot start read\n"); - } - - return true; - } else { - return false; - } -} - -volatile const int16_t *gyroscopeGet () { - return val; -} - diff --git a/gyroscope.h b/gyroscope.h deleted file mode 100644 index 20ef373..0000000 --- a/gyroscope.h +++ /dev/null @@ -1,13 +0,0 @@ -#ifndef GYROSCOPE_H -#define GYROSCOPE_H - -#include -#include - -void gyroscopeInit (); -void gyroscopeStart (); -bool gyroscopeRead (); -volatile const int16_t *gyroscopeGet (); - -#endif /* GYROSCOPE_H */ - diff --git a/main.c b/main.c index f017109..f04c725 100644 --- a/main.c +++ b/main.c @@ -11,7 +11,7 @@ #include "i2c.h" #include "uart.h" #include "timer.h" -#include "gyroscope.h" +#include "gyro.h" static void ledInit () { /* set led1,led2 to output */ @@ -43,25 +43,25 @@ int main () { ledInit (); twInit (); uartInit (); - gyroscopeInit (); + gyroInit (); set_sleep_mode (SLEEP_MODE_IDLE); printf ("initialization done\n"); /* global interrupt enable */ sei (); - gyroscopeStart (); + gyroStart (); //timerStart (); while (1) { //sleepwhile (!timerHit ()); //printf ("running for %u seconds\n", seconds); - sleepwhile (!gyroscopeRead ()); + sleepwhile (!gyroRead ()); sleepwhile (twr.status == TWST_WAIT); switch (twr.status) { case TWST_OK: { - volatile const int16_t *val = gyroscopeGet (); + volatile const int16_t *val = gyroGet (); printf ("%i/%i/%i\n", val[0], val[1], val[2]); break; } -- cgit v1.2.3