]> git.gir.st - tmk_keyboard.git/blob - tool/mbed/mbed-sdk/libraries/tests/peripherals/ADXL345/ADXL345.cpp
Squashed 'tmk_core/' changes from 7967731..b9e0ea0
[tmk_keyboard.git] / tool / mbed / mbed-sdk / libraries / tests / peripherals / ADXL345 / ADXL345.cpp
1 /**
2 * @author Aaron Berk
3 *
4 * @section LICENSE
5 *
6 * Copyright (c) 2010 ARM Limited
7 *
8 * Permission is hereby granted, free of charge, to any person obtaining a copy
9 * of this software and associated documentation files (the "Software"), to deal
10 * in the Software without restriction, including without limitation the rights
11 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 * copies of the Software, and to permit persons to whom the Software is
13 * furnished to do so, subject to the following conditions:
14 *
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24 * THE SOFTWARE.
25 *
26 * @section DESCRIPTION
27 *
28 * ADXL345, triple axis, digital interface, accelerometer.
29 *
30 * Datasheet:
31 *
32 * http://www.analog.com/static/imported-files/data_sheets/ADXL345.pdf
33 */
34
35 /**
36 * Includes
37 */
38 #include "ADXL345.h"
39
40 ADXL345::ADXL345(PinName mosi,
41 PinName miso,
42 PinName sck,
43 PinName cs) : spi_(mosi, miso, sck), nCS_(cs) {
44
45 //2MHz, allowing us to use the fastest data rates.
46 spi_.frequency(2000000);
47 spi_.format(8,3);
48
49 nCS_ = 1;
50
51 wait_us(500);
52
53 }
54
55 int ADXL345::getDevId(void) {
56
57 return oneByteRead(ADXL345_DEVID_REG);
58
59 }
60
61 int ADXL345::getTapThreshold(void) {
62
63 return oneByteRead(ADXL345_THRESH_TAP_REG);
64
65 }
66
67 void ADXL345::setTapThreshold(int threshold) {
68
69 oneByteWrite(ADXL345_THRESH_TAP_REG, threshold);
70
71 }
72
73 int ADXL345::getOffset(int axis) {
74
75 int address = 0;
76
77 if (axis == ADXL345_X) {
78 address = ADXL345_OFSX_REG;
79 } else if (axis == ADXL345_Y) {
80 address = ADXL345_OFSY_REG;
81 } else if (axis == ADXL345_Z) {
82 address = ADXL345_OFSZ_REG;
83 }
84
85 return oneByteRead(address);
86
87 }
88
89 void ADXL345::setOffset(int axis, char offset) {
90
91 int address = 0;
92
93 if (axis == ADXL345_X) {
94 address = ADXL345_OFSX_REG;
95 } else if (axis == ADXL345_Y) {
96 address = ADXL345_OFSY_REG;
97 } else if (axis == ADXL345_Z) {
98 address = ADXL345_OFSZ_REG;
99 }
100
101 return oneByteWrite(address, offset);
102
103 }
104
105 int ADXL345::getTapDuration(void) {
106
107 return oneByteRead(ADXL345_DUR_REG)*625;
108
109 }
110
111 void ADXL345::setTapDuration(int duration_us) {
112
113 int tapDuration = duration_us / 625;
114
115 oneByteWrite(ADXL345_DUR_REG, tapDuration);
116
117 }
118
119 float ADXL345::getTapLatency(void) {
120
121 return oneByteRead(ADXL345_LATENT_REG)*1.25;
122
123 }
124
125 void ADXL345::setTapLatency(int latency_ms) {
126
127 int tapLatency = latency_ms / 1.25;
128
129 oneByteWrite(ADXL345_LATENT_REG, tapLatency);
130
131 }
132
133 float ADXL345::getWindowTime(void) {
134
135 return oneByteRead(ADXL345_WINDOW_REG)*1.25;
136
137 }
138
139 void ADXL345::setWindowTime(int window_ms) {
140
141 int windowTime = window_ms / 1.25;
142
143 oneByteWrite(ADXL345_WINDOW_REG, windowTime);
144
145 }
146
147 int ADXL345::getActivityThreshold(void) {
148
149 return oneByteRead(ADXL345_THRESH_ACT_REG);
150
151 }
152
153 void ADXL345::setActivityThreshold(int threshold) {
154
155 oneByteWrite(ADXL345_THRESH_ACT_REG, threshold);
156
157 }
158
159 int ADXL345::getInactivityThreshold(void) {
160
161 return oneByteRead(ADXL345_THRESH_INACT_REG);
162
163 }
164
165 void ADXL345::setInactivityThreshold(int threshold) {
166
167 return oneByteWrite(ADXL345_THRESH_INACT_REG, threshold);
168
169 }
170
171 int ADXL345::getTimeInactivity(void) {
172
173 return oneByteRead(ADXL345_TIME_INACT_REG);
174
175 }
176
177 void ADXL345::setTimeInactivity(int timeInactivity) {
178
179 oneByteWrite(ADXL345_TIME_INACT_REG, timeInactivity);
180
181 }
182
183 int ADXL345::getActivityInactivityControl(void) {
184
185 return oneByteRead(ADXL345_ACT_INACT_CTL_REG);
186
187 }
188
189 void ADXL345::setActivityInactivityControl(int settings) {
190
191 oneByteWrite(ADXL345_ACT_INACT_CTL_REG, settings);
192
193 }
194
195 int ADXL345::getFreefallThreshold(void) {
196
197 return oneByteRead(ADXL345_THRESH_FF_REG);
198
199 }
200
201 void ADXL345::setFreefallThreshold(int threshold) {
202
203 oneByteWrite(ADXL345_THRESH_FF_REG, threshold);
204
205 }
206
207 int ADXL345::getFreefallTime(void) {
208
209 return oneByteRead(ADXL345_TIME_FF_REG)*5;
210
211 }
212
213 void ADXL345::setFreefallTime(int freefallTime_ms) {
214
215 int freefallTime = freefallTime_ms / 5;
216
217 oneByteWrite(ADXL345_TIME_FF_REG, freefallTime);
218
219 }
220
221 int ADXL345::getTapAxisControl(void) {
222
223 return oneByteRead(ADXL345_TAP_AXES_REG);
224
225 }
226
227 void ADXL345::setTapAxisControl(int settings) {
228
229 oneByteWrite(ADXL345_TAP_AXES_REG, settings);
230
231 }
232
233 int ADXL345::getTapSource(void) {
234
235 return oneByteRead(ADXL345_ACT_TAP_STATUS_REG);
236
237 }
238
239 void ADXL345::setPowerMode(char mode) {
240
241 //Get the current register contents, so we don't clobber the rate value.
242 char registerContents = oneByteRead(ADXL345_BW_RATE_REG);
243
244 registerContents = (mode << 4) | registerContents;
245
246 oneByteWrite(ADXL345_BW_RATE_REG, registerContents);
247
248 }
249
250 int ADXL345::getPowerControl(void) {
251
252 return oneByteRead(ADXL345_POWER_CTL_REG);
253
254 }
255
256 void ADXL345::setPowerControl(int settings) {
257
258 oneByteWrite(ADXL345_POWER_CTL_REG, settings);
259
260 }
261
262 int ADXL345::getInterruptEnableControl(void) {
263
264 return oneByteRead(ADXL345_INT_ENABLE_REG);
265
266 }
267
268 void ADXL345::setInterruptEnableControl(int settings) {
269
270 oneByteWrite(ADXL345_INT_ENABLE_REG, settings);
271
272 }
273
274 int ADXL345::getInterruptMappingControl(void) {
275
276 return oneByteRead(ADXL345_INT_MAP_REG);
277
278 }
279
280 void ADXL345::setInterruptMappingControl(int settings) {
281
282 oneByteWrite(ADXL345_INT_MAP_REG, settings);
283
284 }
285
286 int ADXL345::getInterruptSource(void){
287
288 return oneByteRead(ADXL345_INT_SOURCE_REG);
289
290 }
291
292 int ADXL345::getDataFormatControl(void){
293
294 return oneByteRead(ADXL345_DATA_FORMAT_REG);
295
296 }
297
298 void ADXL345::setDataFormatControl(int settings){
299
300 oneByteWrite(ADXL345_DATA_FORMAT_REG, settings);
301
302 }
303
304 void ADXL345::setDataRate(int rate) {
305
306 //Get the current register contents, so we don't clobber the power bit.
307 char registerContents = oneByteRead(ADXL345_BW_RATE_REG);
308
309 registerContents &= 0x10;
310 registerContents |= rate;
311
312 oneByteWrite(ADXL345_BW_RATE_REG, registerContents);
313
314 }
315
316 void ADXL345::getOutput(int* readings){
317
318 char buffer[6];
319
320 multiByteRead(ADXL345_DATAX0_REG, buffer, 6);
321
322 readings[0] = (int)buffer[1] << 8 | (int)buffer[0];
323 readings[1] = (int)buffer[3] << 8 | (int)buffer[2];
324 readings[2] = (int)buffer[5] << 8 | (int)buffer[4];
325
326 }
327
328 int ADXL345::getFifoControl(void){
329
330 return oneByteRead(ADXL345_FIFO_CTL);
331
332 }
333
334 void ADXL345::setFifoControl(int settings){
335
336 oneByteWrite(ADXL345_FIFO_STATUS, settings);
337
338 }
339
340 int ADXL345::getFifoStatus(void){
341
342 return oneByteRead(ADXL345_FIFO_STATUS);
343
344 }
345
346 int ADXL345::oneByteRead(int address) {
347
348 int tx = (ADXL345_SPI_READ | (address & 0x3F));
349 int rx = 0;
350
351 nCS_ = 0;
352 //Send address to read from.
353 spi_.write(tx);
354 //Read back contents of address.
355 rx = spi_.write(0x00);
356 nCS_ = 1;
357
358 return rx;
359
360 }
361
362 void ADXL345::oneByteWrite(int address, char data) {
363
364 int tx = (ADXL345_SPI_WRITE | (address & 0x3F));
365
366 nCS_ = 0;
367 //Send address to write to.
368 spi_.write(tx);
369 //Send data to be written.
370 spi_.write(data);
371 nCS_ = 1;
372
373 }
374
375 void ADXL345::multiByteRead(int startAddress, char* buffer, int size) {
376
377 int tx = (ADXL345_SPI_READ | ADXL345_MULTI_BYTE | (startAddress & 0x3F));
378
379 nCS_ = 0;
380 //Send address to start reading from.
381 spi_.write(tx);
382
383 for (int i = 0; i < size; i++) {
384 buffer[i] = spi_.write(0x00);
385 }
386
387 nCS_ = 1;
388
389 }
390
391 void ADXL345::multiByteWrite(int startAddress, char* buffer, int size) {
392
393 int tx = (ADXL345_SPI_WRITE | ADXL345_MULTI_BYTE | (startAddress & 0x3F));
394
395 nCS_ = 0;
396 //Send address to start reading from.
397 spi_.write(tx);
398
399 for (int i = 0; i < size; i++) {
400 buffer[i] = spi_.write(0x00);
401 }
402
403 nCS_ = 1;
404
405 }
Imprint / Impressum