forked from mbonani/aseba-target-thymio2
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmma7660.c
More file actions
217 lines (167 loc) · 5.12 KB
/
mma7660.c
File metadata and controls
217 lines (167 loc) · 5.12 KB
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
/*
Thymio-II Firmware
Copyright (C) 2011 Florian Vaussard <florian dot vaussard at epfl dot ch>,
Mobots group (http://mobots.epfl.ch), Robotics system laboratory (http://lsro.epfl.ch)
EPFL Ecole polytechnique federale de Lausanne (http://www.epfl.ch)
See authors.txt for more details about other contributors.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published
by the Free Software Foundation, version 3 of the License.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <i2c/i2c.h>
#include <error/error.h>
#include <clock/clock.h>
#include "mma7660.h"
#include "regulator.h"
#define XOUT 0x00
#define YOUT 0x01
#define ZOUT 0x02
#define TILT 0x03
#define SR_STATUS 0x04
#define SLEEP_CNT 0x05
#define INT_SETUP 0x06
#define MODE 0x07
#define SR 0x08
#define TAP_DETECT 0x09
#define TAP_DEBOUNCE 0x0a
// XOUT, YOUT, ZOUT bits
#define ALERT 6
#define ALERT_MASK (1 << ALERT)
#define DATA_MASK 0b111111 // with the sign bit
// INT_SETUP bits
#define GINT 4
#define PDINT 2
// MODE bits
#define ON 0
#define IPP 6 // Interrupt output open-drain (0) / push-pull (1)
#define IAH 7 // Interrupt output active low (0) / high (1)
#define MODE_CONFIG_OFF ((1 << IPP) | (1 << IAH))
#define MODE_CONFIG_ON (MODE_CONFIG_OFF | (1 << ON))
static __attribute((far)) int i2c_bus;
static __attribute((far)) int i2c_address;
static __attribute((far)) mma7660_cb cb;
static __attribute((far)) char data[4];
static __attribute((far)) unsigned char reg;
static void mma7660_i2c_cb(int i2c_id, bool status) {
static int relaunched;
int tap;
// Check is the ALERT bit is set -> invalid input
if ( (data[0] & ALERT_MASK) ||
(data[1] & ALERT_MASK) ||
(data[2] & ALERT_MASK) ||
(data[3] & ALERT_MASK)) {
// Don't fire the callback
if(!relaunched) {
mma7660_read_async();
relaunched = 1;
}
return;
}
relaunched = 0;
tap = data[3] & 0x20;
// Fix the sign bit
data[0] = ((signed char) (((unsigned char) data[0]) << 2)) >> 2;
data[1] = ((signed char) (((unsigned char) data[1]) << 2)) >> 2;
data[2] = ((signed char) (((unsigned char) data[2]) << 2)) >> 2;
cb(data[0],data[1],data[2],tap);
}
void mma7660_read_async(void) {
// Safety: If i2c is busy, ignore this
if(i2c_master_is_busy(i2c_bus))
return;
// Read XOUT, YOUT, ZOUT, status and fire the callback
reg = XOUT;
i2c_master_transfert_async(i2c_bus, i2c_address, ®, 1, (unsigned char *) data, 4, mma7660_i2c_cb);
}
static void write(unsigned char reg, unsigned char data){
unsigned char array[2];
array[0] = reg;
array[1] = data;
i2c_master_transfert_block(i2c_bus, i2c_address,
array, 2, NULL, 0);
}
void mma7660_init(int i2c, unsigned char address, mma7660_cb ucb, int prio) {
i2c_bus = i2c;
i2c_address = address;
cb = ucb;
va_get(); // Enable Va LDO
clock_delay_us(1300);
/* Configure device */
write(MODE, MODE_CONFIG_OFF); // Reset
if(prio)
write(INT_SETUP, 1 << GINT); // Enable auto interrupt on update (GINT)
else
write(INT_SETUP, 0);
/* Configure PIC */
TRISDbits.TRISD7 = 1; // Set RD7 pin as input
// CNPU2bits.CN16PUE = 1; // Enable internal pull-up resistor
/* Configure interrupts */
if(prio) {
IPC4bits.CNIP = prio; // CN interrupt priority
IFS1bits.CNIF = 0; // Clear flag
CNEN2bits.CN16IE = 1; // Enable CN16 interrupt
IEC1bits.CNIE = 1; // Enable CN interrupt
}
}
void mma7660_set_mode(int hz, int tap_en) {
int flag = IEC1bits.CNIE;
ERROR_CHECK_RANGE(hz, MMA7660_120HZ, MMA7660_0HZ, MMA7660_ERROR_INVALID_PARAM);
IEC1bits.CNIE = 0;
while(i2c_master_is_busy(i2c_bus));
// Set the device into standby mode
write(MODE, MODE_CONFIG_OFF);
if(tap_en && hz != MMA7660_120HZ)
ERROR(MMA7660_ERROR_INVALID_PARAM, &hz);
// Change
if (hz == MMA7660_0HZ)
// Stop the device
return;
else
// Set the frequency
write(SR, hz);
// Write the Tap detection register
// Axe Y and Z, treshold: 12 counts
if(tap_en)
//Disable X axis due to the vibration of motors
write(TAP_DETECT, 11+0x20);
else
write(TAP_DETECT, 0xE0);
//write the tap debounce register
// 21 counts debouce
if(tap_en)
write(TAP_DEBOUNCE, 20);
else
write(TAP_DEBOUNCE, 0);
/*
if(tap_en)
write(INT_SETUP, (1 << GINT));
else
write(INT_SETUP, (1 << GINT));
*/
// Enable the device
write(MODE, MODE_CONFIG_ON);
IEC1bits.CNIE = flag;
}
void mma7660_suspend(void) {
IEC1bits.CNIE = 0;
// Disable the device
write(MODE, MODE_CONFIG_OFF);
va_put(); // disable LDO
}
void _ISR _CNInterrupt(void)
{
// OK
IFS1bits.CNIF = 0;
// One interrupt for all CN pins!
// Check the state of the pin
if (PORTDbits.RD7 != 1)
return;
// Initiate the data transfer
mma7660_read_async();
}