aboutsummaryrefslogtreecommitdiff
path: root/gyro.c
diff options
context:
space:
mode:
authorLars-Dominik Braun <lars@6xq.net>2014-03-08 17:22:49 +0100
committerLars-Dominik Braun <lars@6xq.net>2014-03-08 17:22:49 +0100
commit778e4889e1897c022906c56d0764fe163e24355b (patch)
tree0076e5e0a5a0674507bb7673bf4f3cb1fdd1060d /gyro.c
parent03dbe661312e9d2e3e379bcd455ddfabc48f95b6 (diff)
downloadhourglass-778e4889e1897c022906c56d0764fe163e24355b.tar.gz
hourglass-778e4889e1897c022906c56d0764fe163e24355b.tar.bz2
hourglass-778e4889e1897c022906c56d0764fe163e24355b.zip
gyroscope: Rename to gyro
Diffstat (limited to 'gyro.c')
-rw-r--r--gyro.c77
1 files changed, 77 insertions, 0 deletions
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 <stdio.h>
+#include <avr/io.h>
+#include <avr/interrupt.h>
+#include <avr/sleep.h>
+
+#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;
+}
+