1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
|
#include "common.h"
#include <stdio.h>
#include <string.h>
#include <avr/io.h>
#include <avr/interrupt.h>
#include <avr/sleep.h>
#include "i2c.h"
#include "gyro.h"
/* max degree +/- per second */
#define GYRO_DPS 500
/* output data rate, hertz */
#define GYRO_FREQ 95
/* millidegree per second and digit, (2*GYRO_DPS+1)/(2^16) ~= 15.274, manual
* says 17.5, so choose something in between */
#define GYRO_MDPS_PER_DIGIT 16
/* milliangle (in degree) per 32 units, 32 is a relatively small number (not
* much information lost) and the angle is very to a full integer number,
* GYRO_MDPS_PER_DIGIT/GYRO_FREQ*32 */
#define GYRO_MANGLE_PER_32 5
/* device address */
#define L3GD20 0b11010100
/* registers */
#define L3GD20_WHOAMI 0xf
#define L3GD20_CTRLREG1 0x20
#define L3GD20_CTRLREG3 0x22
#define L3GD20_CTRLREG4 0x23
/* the first interrupt is lost */
static volatile bool drdy = true;
static volatile int16_t val[3] = {0, 0, 0};
/* current (relative) angle, in millidegree */
static int16_t angle[3] = {0, 0, 0};
/* currently reading from i2c */
static bool reading = false;
/* 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 = PCICR | (1 << PCIE0);
/* enable interrupts on PB1/PCINT1 */
PCMSK0 = (1 << PCINT1);
}
/* 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);
}
/* process gyro sensor data, returns true if new data is available
*/
bool gyroProcess () {
if (reading) {
if (twr.status == TWST_OK) {
/* new data transfered, process it */
for (uint8_t i = 0; i < sizeof (angle)/sizeof (*angle); i++) {
angle[i] += (val[i] >> 5) * GYRO_MANGLE_PER_32;
}
reading = false;
return true;
} else if (twr.status == TWST_ERR) {
printf ("gyro i2c error\n");
reading = false;
}
} else {
if (drdy && 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;
}
}
}
return false;
}
const int16_t *gyroGetAngle () {
return angle;
}
void gyroResetAngle () {
memset (angle, 0, sizeof (angle));
}
volatile const int16_t *gyroGetRaw () {
return val;
}
|