aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLars-Dominik Braun <lars@6xq.net>2014-04-22 16:48:50 +0200
committerLars-Dominik Braun <lars@6xq.net>2014-04-22 16:51:13 +0200
commitb05df9df58bc6fdd122abfec3012bc2537fc069c (patch)
treef0be81167c40aa7f3490e8018ea92cc21b16f2be
parent68c180f2aba4471483e1786556c2bc2a94a7d2a7 (diff)
downloadhourglass-b05df9df58bc6fdd122abfec3012bc2537fc069c.tar.gz
hourglass-b05df9df58bc6fdd122abfec3012bc2537fc069c.tar.bz2
hourglass-b05df9df58bc6fdd122abfec3012bc2537fc069c.zip
Fix interrupt stuff
Sorta polling based now, with interrupts used to wake up from sleep only. At least it works.
-rw-r--r--accel.c15
-rw-r--r--gyro.c16
-rw-r--r--i2c.c7
-rw-r--r--main.c4
4 files changed, 20 insertions, 22 deletions
diff --git a/accel.c b/accel.c
index b58f7f7..ede53ff 100644
--- a/accel.c
+++ b/accel.c
@@ -5,6 +5,7 @@
#include <avr/io.h>
#include <avr/interrupt.h>
#include <avr/sleep.h>
+#include <util/atomic.h>
#include "i2c.h"
#include "accel.h"
@@ -16,8 +17,6 @@
#define LIS302DL_CTRLREG1 0x20
#define LIS302DL_UNUSED1 0x28
-/* the first interrupt is lost */
-static volatile bool drdy = true;
/* 0, 2 and 4 are zero, as they contain the dummy register’s content */
static volatile int8_t val[6] = {0, 0, 0, 0, 0, 0};
/* currently reading from i2c */
@@ -26,13 +25,13 @@ static bool reading = false;
/* data ready interrupt
*/
ISR(PCINT1_vect) {
- drdy = true;
+ /* empty */
}
void accelInit () {
/* set interrupt lines to input with pull-up */
- DDRC = DDRC & ~((1 << PC0) | (1 << PC1));
- PORTC = PORTC | (1 << PC0) | (1 << PC1);
+ DDRC = DDRC & ~((1 << DDC0) | (1 << DDC1));
+ PORTC = PORTC | (1 << PORTC0) | (1 << PORTC1);
/* enable interrupt PCI1 for PCINT8/9 */
PCICR = PCICR | (1 << PCIE1);
/* enable interrupts from port PC0/PC1 aka PCINT8/PCINT9 */
@@ -67,13 +66,13 @@ bool accelProcess () {
reading = false;
}
} else {
- if (drdy && twr.status != TWST_WAIT) {
+ if (!((PINC >> PINC1) & 0x1) && twr.status != TWST_WAIT) {
/* new data available in device buffer and bus is free, we are
* reading the registers inbetween out_x/y/z and ignore them */
- if (!twRequest (TWM_READ, LIS302DL, LIS302DL_UNUSED1, (uint8_t *) val, 6)) {
+ if (!twRequest (TWM_READ, LIS302DL, LIS302DL_UNUSED1,
+ (uint8_t *) val, 6)) {
printf ("cannot start read\n");
} else {
- drdy = false;
reading = true;
}
}
diff --git a/gyro.c b/gyro.c
index eede345..406ae2e 100644
--- a/gyro.c
+++ b/gyro.c
@@ -29,8 +29,7 @@
#define L3GD20_CTRLREG3 0x22
#define L3GD20_CTRLREG4 0x23
-/* the first interrupt is lost */
-static volatile bool drdy = true;
+/* raw values */
static volatile int16_t val[3] = {0, 0, 0};
/* current (relative) angle, in millidegree */
static int16_t angle[3] = {0, 0, 0};
@@ -40,13 +39,13 @@ static bool reading = false;
/* data ready interrupt
*/
ISR(PCINT0_vect) {
- drdy = true;
+ /* empty */
}
void gyroInit () {
/* set PB1 to input, with pull-up */
- DDRB = (DDRB & ~((1 << PB1)));
- PORTB = (PORTB | (1 << PB1));
+ DDRB = DDRB & ~((1 << DDB1));
+ PORTB = PORTB | (1 << PORTB1);
/* enable interrupt PCI0 */
PCICR = PCICR | (1 << PCIE0);
/* enable interrupts on PB1/PCINT1 */
@@ -58,10 +57,10 @@ void gyroStart () {
/* configuration:
* disable power-down-mode
* defaults
- * enable drdy interrupt
+ * low-active (does not work?), push-pull, drdy on int2
* select 500dps
*/
- uint8_t data[] = {0b00001111, 0b0, 0b00001000, 0b00010000};
+ uint8_t data[] = {0b00001111, 0b0, 0b00101000, 0b00010000};
if (!twRequest (TWM_WRITE, L3GD20, L3GD20_CTRLREG1, data,
sizeof (data)/sizeof (*data))) {
@@ -87,12 +86,11 @@ bool gyroProcess () {
reading = false;
}
} else {
- if (drdy && twr.status != TWST_WAIT) {
+ if (((PINB >> PINB1) & 0x1) && twr.status != TWST_WAIT) {
/* new data available in device buffer and bus is free */
if (!twRequest (TWM_READ, L3GD20, 0x28, (uint8_t *) val, 6)) {
printf ("cannot start read\n");
} else {
- drdy = false;
reading = true;
}
}
diff --git a/i2c.c b/i2c.c
index 7855e88..3e4bffb 100644
--- a/i2c.c
+++ b/i2c.c
@@ -1,6 +1,8 @@
#include <stdio.h>
#include <util/twi.h>
#include <avr/interrupt.h>
+#include <stdlib.h>
+#include <assert.h>
#include "i2c.h"
#include "common.h"
@@ -67,10 +69,7 @@ void twInit () {
*/
bool twRequest (const twMode mode, const uint8_t address,
const uint8_t subaddress, uint8_t * const data, const uint8_t count) {
- /* do not start if request is pending */
- if (twr.status == TWST_WAIT) {
- return false;
- }
+ assert (twr.status != TWST_WAIT);
twr.mode = mode;
twr.address = address;
diff --git a/main.c b/main.c
index 9c35e99..4eb461e 100644
--- a/main.c
+++ b/main.c
@@ -66,7 +66,7 @@ int main () {
gyroProcess ();
accelProcess();
} else {
- accelProcess();
+ accelProcess ();
gyroProcess ();
}
checkGyro = !checkGyro;
@@ -82,6 +82,8 @@ int main () {
}
timerStop ();
+ printf ("stopped\n");
+
/* global interrupt disable */
cli ();