aboutsummaryrefslogtreecommitdiff
path: root/accel.c
blob: c6f9bdb5a29bdbecd4d61877fd70fc17d54997ad (plain)
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
/*
Copyright (c) 2014-2015
	Lars-Dominik Braun <lars@6xq.net>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/

#include "common.h"

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <avr/io.h>
#include <avr/interrupt.h>
#include <avr/sleep.h>
#include <util/atomic.h>

#include "i2c.h"
#include "accel.h"

/* configuration */
/* horizon trigger threshold ~0.75g and duration 15*10ms */
#define HORIZON_THRESHOLD 48
#define HORIZON_DURATION 15
/* shake detect trashold ~2g */
#define SHAKE_THRESHOLD 120

/* device address */
#define LIS302DL 0b00111000
/* registers */
#define LIS302DL_WHOAMI 0xf
#define LIS302DL_CTRLREG1 0x20
#define LIS302DL_CTRLREG2 0x21
#define LIS302DL_CTRLREG3 0x22
#define LIS302DL_UNUSED1 0x28
#define LIS302DL_OUTZ 0x2D
#define LIS302DL_FFWUCFG1 0x30
#define LIS302DL_FFWUTHS1 0x32
#define LIS302DL_FFWUCFG2 0x34
#define LIS302DL_FFWUTHS2 0x36

/* bit positions in registers, see chip docs */
#define ZHIE 5

static int8_t zval;
static uint8_t shakeCount = 0;

/* horizon position */
/* current */
static horizon horizonSign = HORIZON_NONE;
static bool horizonChanged = false;

/* driver state */
#define STOPPED 0
#define START_REQUEST 1
#define STARTING_A 2
#define STARTING_B 3
#define STARTING_C 4
#define STARTING_D 5
#define STARTING_E 6
#define STARTING_F 7
#define STOP_REQUEST 8
#define STOPPING 9
#define READING 10
#define IDLE 11
static uint8_t state = STOPPED;

/* data ready interrupt
 */
ISR(PCINT1_vect) {
	const uint8_t pin = PINC;
	/* low-active */
	const bool int1 = !((pin >> PINC0) & 0x1);
	const bool int2 = !((pin >> PINC1) & 0x1);
	if (int1) {
		enableWakeup (WAKE_ACCEL_HORIZON);
	}
	if (int2) {
		enableWakeup (WAKE_ACCEL_SHAKE);
	}
}

void accelInit () {
	/* set interrupt lines to input */
	DDRC &= ~((1 << DDC0) | (1 << DDC1));
	/* enable interrupt PCI1 for PCINT8/9 */
	PCICR = PCICR | (1 << PCIE1);
	/* enable interrupts from port PC0/PC1 aka PCINT8/PCINT9 */
	PCMSK1 = (1 << PCINT9) | (1 << PCINT8);
}

void accelStart () {
	assert (state == STOPPED);
	state = START_REQUEST;
	/* make sure the current horizon is read at startup */
	enableWakeup (WAKE_ACCEL_HORIZON);
}

void accelProcess () {
	switch (state) {
		case START_REQUEST: {
			/* configuration:
			 * disable power-down-mode, enable z-axis
			 */
			static uint8_t data[] = {0b01000100};

			if (twRequest (TWM_WRITE, LIS302DL, LIS302DL_CTRLREG1, data,
					length (data))) {
				state = STARTING_A;
			}
			break;
		}

		/* set up ff_wu_1 (horizon detection) */
		case STARTING_A:
			if (shouldWakeup (WAKE_I2C)) {
				disableWakeup (WAKE_I2C);
				static uint8_t data[] = {HORIZON_THRESHOLD, HORIZON_DURATION};
				if (twRequest (TWM_WRITE, LIS302DL, LIS302DL_FFWUTHS1, data,
						length (data))) {
					state = STARTING_B;
				}
			}
			break;

		case STARTING_B:
			if (shouldWakeup (WAKE_I2C)) {
				disableWakeup (WAKE_I2C);
				/* enable interrupt on z high event */
				static uint8_t data[] = {1 << ZHIE};
				if (twRequest (TWM_WRITE, LIS302DL, LIS302DL_FFWUCFG1, data,
						length (data))) {
					state = STARTING_C;
				}
			}
			break;

		/* set up ff_wu_2 (shake detection) */
		case STARTING_C:
			if (shouldWakeup (WAKE_I2C)) {
				disableWakeup (WAKE_I2C);
				static uint8_t data[] = {SHAKE_THRESHOLD};
				if (twRequest (TWM_WRITE, LIS302DL, LIS302DL_FFWUTHS2, data,
						length (data))) {
					state = STARTING_D;
				}
			}
			break;

		case STARTING_D:
			if (shouldWakeup (WAKE_I2C)) {
				disableWakeup (WAKE_I2C);
				/* or events, enable interrupt on z high event */
				static uint8_t data[] = {1 << ZHIE};
				if (twRequest (TWM_WRITE, LIS302DL, LIS302DL_FFWUCFG2, data,
						length (data))) {
					state = STARTING_E;
				}
			}
			break;

		case STARTING_E:
			if (shouldWakeup (WAKE_I2C)) {
				disableWakeup (WAKE_I2C);
				/* push-pull, low-active, FF_WU_1 on int1, FF_WU_2 on int2 */
				static uint8_t data[] = {0b10010001};
				if (twRequest (TWM_WRITE, LIS302DL, LIS302DL_CTRLREG3, data,
						length (data))) {
					state = STARTING_F;
				}
			}
			break;

		case STARTING_F:
			if (shouldWakeup (WAKE_I2C)) {
				disableWakeup (WAKE_I2C);
				state = IDLE;
			}
			break;

		case IDLE:
			/* new data available in device buffer and bus is free */
			if (shouldWakeup (WAKE_ACCEL_HORIZON) && twRequest (TWM_READ, LIS302DL,
						LIS302DL_OUTZ, (uint8_t *) &zval, sizeof (zval))) {
				disableWakeup (WAKE_ACCEL_HORIZON);
				state = READING;
			}
			if (shouldWakeup (WAKE_ACCEL_SHAKE)) {
				disableWakeup (WAKE_ACCEL_SHAKE);
				++shakeCount;
			}
			break;

		case READING:
			if (shouldWakeup (WAKE_I2C)) {
				disableWakeup (WAKE_I2C);
				/* the bus might be in use again already */
				//assert (twr.status == TWST_OK);

				if (zval >= 0) {
					if (horizonSign != HORIZON_POS) {
						horizonChanged = true;
					}
					horizonSign = HORIZON_POS;
				} else {
					if (horizonSign != HORIZON_NEG) {
						horizonChanged = true;
					}
					horizonSign = HORIZON_NEG;
				}

				state = IDLE;
			}
			break;

		default:
			assert (0);
			break;
	}
}

horizon accelGetHorizon (bool * const changed) {
	*changed = horizonChanged;
	horizonChanged = false;
	return horizonSign;
}

uint8_t accelGetShakeCount () {
	return shakeCount/2;
}

void accelResetShakeCount () {
	shakeCount = 0;
}