]> git.donarmstrong.com Git - qmk_firmware.git/blob - tool/mbed/mbed-sdk/libraries/tests/peripherals/ADXL345/ADXL345.h
Squashed 'tmk_core/' changes from 7967731..b9e0ea0
[qmk_firmware.git] / tool / mbed / mbed-sdk / libraries / tests / peripherals / ADXL345 / ADXL345.h
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 #ifndef ADXL345_H
36 #define ADXL345_H
37
38 /**
39  * Includes
40  */
41 #include "mbed.h"
42
43 /**
44  * Defines
45  */
46 //Registers.
47 #define ADXL345_DEVID_REG          0x00
48 #define ADXL345_THRESH_TAP_REG     0x1D
49 #define ADXL345_OFSX_REG           0x1E
50 #define ADXL345_OFSY_REG           0x1F
51 #define ADXL345_OFSZ_REG           0x20
52 #define ADXL345_DUR_REG            0x21
53 #define ADXL345_LATENT_REG         0x22
54 #define ADXL345_WINDOW_REG         0x23
55 #define ADXL345_THRESH_ACT_REG     0x24
56 #define ADXL345_THRESH_INACT_REG   0x25
57 #define ADXL345_TIME_INACT_REG     0x26
58 #define ADXL345_ACT_INACT_CTL_REG  0x27
59 #define ADXL345_THRESH_FF_REG      0x28
60 #define ADXL345_TIME_FF_REG        0x29
61 #define ADXL345_TAP_AXES_REG       0x2A
62 #define ADXL345_ACT_TAP_STATUS_REG 0x2B
63 #define ADXL345_BW_RATE_REG        0x2C
64 #define ADXL345_POWER_CTL_REG      0x2D
65 #define ADXL345_INT_ENABLE_REG     0x2E
66 #define ADXL345_INT_MAP_REG        0x2F
67 #define ADXL345_INT_SOURCE_REG     0x30
68 #define ADXL345_DATA_FORMAT_REG    0x31
69 #define ADXL345_DATAX0_REG         0x32
70 #define ADXL345_DATAX1_REG         0x33
71 #define ADXL345_DATAY0_REG         0x34
72 #define ADXL345_DATAY1_REG         0x35
73 #define ADXL345_DATAZ0_REG         0x36
74 #define ADXL345_DATAZ1_REG         0x37
75 #define ADXL345_FIFO_CTL           0x38
76 #define ADXL345_FIFO_STATUS        0x39
77
78 //Data rate codes.
79 #define ADXL345_3200HZ      0x0F
80 #define ADXL345_1600HZ      0x0E
81 #define ADXL345_800HZ       0x0D
82 #define ADXL345_400HZ       0x0C
83 #define ADXL345_200HZ       0x0B
84 #define ADXL345_100HZ       0x0A
85 #define ADXL345_50HZ        0x09
86 #define ADXL345_25HZ        0x08
87 #define ADXL345_12HZ5       0x07
88 #define ADXL345_6HZ25       0x06
89
90 #define ADXL345_SPI_READ    0x80
91 #define ADXL345_SPI_WRITE   0x00
92 #define ADXL345_MULTI_BYTE  0x60
93
94 #define ADXL345_X           0x00
95 #define ADXL345_Y           0x01
96 #define ADXL345_Z           0x02
97
98 /**
99  * ADXL345 triple axis, digital interface, accelerometer.
100  */
101 class ADXL345 {
102
103 public:
104
105     /**
106      * Constructor.
107      *
108      * @param mosi mbed pin to use for MOSI line of SPI interface.
109      * @param miso mbed pin to use for MISO line of SPI interface.
110      * @param sck mbed pin to use for SCK line of SPI interface.
111      * @param cs mbed pin to use for not chip select line of SPI interface.
112      */
113     ADXL345(PinName mosi, PinName miso, PinName sck, PinName cs);
114
115     /**
116      * Read the device ID register on the device.
117      *
118      * @return The device ID code [0xE5]
119      */
120     int getDevId(void);
121
122     /**
123      * Read the tap threshold on the device.
124      *
125      * @return The tap threshold as an 8-bit number with a scale factor of
126      *         62.5mg/LSB.
127      */
128     int getTapThreshold(void);
129
130     /**
131      * Set the tap threshold.
132      *
133      * @param The tap threshold as an 8-bit number with a scale factor of
134      *        62.5mg/LSB.
135      */
136     void setTapThreshold(int threshold);
137
138     /**
139      * Get the current offset for a particular axis.
140      *
141      * @param axis 0x00 -> X-axis
142      *             0x01 -> Y-axis
143      *             0x02 -> Z-axis
144      * @return The current offset as an 8-bit 2's complement number with scale
145      *         factor 15.6mg/LSB.
146      */
147     int getOffset(int axis);
148
149     /**
150      * Set the offset for a particular axis.
151      *
152      * @param axis 0x00 -> X-axis
153      *             0x01 -> Y-axis
154      *             0x02 -> Z-axis
155      * @param offset The offset as an 8-bit 2's complement number with scale
156      *               factor 15.6mg/LSB.
157      */
158     void setOffset(int axis, char offset);
159
160     /**
161      * Get the tap duration required to trigger an event.
162      *
163      * @return The max time that an event must be above the tap threshold to
164      *         qualify as a tap event, in microseconds.
165      */
166     int getTapDuration(void);
167
168     /**
169      * Set the tap duration required to trigger an event.
170      *
171      * @param duration_us The max time that an event must be above the tap
172      *                    threshold to qualify as a tap event, in microseconds.
173      *                    Time will be normalized by the scale factor which is
174      *                    625us/LSB. A value of 0 disables the single/double
175      *                    tap functions.
176      */
177     void setTapDuration(int duration_us);
178
179     /**
180      * Get the tap latency between the detection of a tap and the time window.
181      *
182      * @return The wait time from the detection of a tap event to the start of
183      *         the time window during which a possible second tap event can be
184      *         detected in milliseconds.
185      */
186     float getTapLatency(void);
187
188     /**
189      * Set the tap latency between the detection of a tap and the time window.
190      *
191      * @param latency_ms The wait time from the detection of a tap event to the
192      *                   start of the time window during which a possible
193      *                   second tap event can be detected in milliseconds.
194      *                   A value of 0 disables the double tap function.
195      */
196     void setTapLatency(int latency_ms);
197
198     /**
199      * Get the time of window between tap latency and a double tap.
200      *
201      * @return The amount of time after the expiration of the latency time
202      *         during which a second valid tap can begin, in milliseconds.
203      */
204     float getWindowTime(void);
205
206     /**
207      * Set the time of the window between tap latency and a double tap.
208      *
209      * @param window_ms The amount of time after the expiration of the latency
210      *                  time during which a second valid tap can begin,
211      *                  in milliseconds.
212      */
213     void setWindowTime(int window_ms);
214
215     /**
216      * Get the threshold value for detecting activity.
217      *
218      * @return The threshold value for detecting activity as an 8-bit number.
219      *         Scale factor is 62.5mg/LSB.
220      */
221     int getActivityThreshold(void);
222
223     /**
224      * Set the threshold value for detecting activity.
225      *
226      * @param threshold The threshold value for detecting activity as an 8-bit
227      *                  number. Scale factor is 62.5mg/LSB. A value of 0 may
228      *                  result in undesirable behavior if the activity
229      *                  interrupt is enabled.
230      */
231     void setActivityThreshold(int threshold);
232
233     /**
234      * Get the threshold value for detecting inactivity.
235      *
236      * @return The threshold value for detecting inactivity as an 8-bit number.
237      *         Scale factor is 62.5mg/LSB.
238      */
239     int getInactivityThreshold(void);
240
241     /**
242      * Set the threshold value for detecting inactivity.
243      *
244      * @param threshold The threshold value for detecting inactivity as an
245      *                  8-bit number. Scale factor is 62.5mg/LSB.
246      */
247     void setInactivityThreshold(int threshold);
248
249     /**
250      * Get the time required for inactivity to be declared.
251      *
252      * @return The amount of time that acceleration must be less than the
253      *         inactivity threshold for inactivity to be declared, in
254      *         seconds.
255      */
256     int getTimeInactivity(void);
257
258     /**
259      * Set the time required for inactivity to be declared.
260      *
261      * @param inactivity The amount of time that acceleration must be less than
262      *                   the inactivity threshold for inactivity to be
263      *                   declared, in seconds. A value of 0 results in an
264      *                   interrupt when the output data is less than the
265      *                   threshold inactivity.
266      */
267     void setTimeInactivity(int timeInactivity);
268
269     /**
270      * Get the activity/inactivity control settings.
271      *
272      *      D7            D6             D5            D4
273      * +-----------+--------------+--------------+--------------+
274      * | ACT ac/dc | ACT_X enable | ACT_Y enable | ACT_Z enable |
275      * +-----------+--------------+--------------+--------------+
276      *
277      *        D3             D2               D1              D0
278      * +-------------+----------------+----------------+----------------+
279      * | INACT ac/dc | INACT_X enable | INACT_Y enable | INACT_Z enable |
280      * +-------------+----------------+----------------+----------------+
281      *
282      * See datasheet for details.
283      *
284      * @return The contents of the ACT_INACT_CTL register.
285      */
286     int getActivityInactivityControl(void);
287
288     /**
289      * Set the activity/inactivity control settings.
290      *
291      *      D7            D6             D5            D4
292      * +-----------+--------------+--------------+--------------+
293      * | ACT ac/dc | ACT_X enable | ACT_Y enable | ACT_Z enable |
294      * +-----------+--------------+--------------+--------------+
295      *
296      *        D3             D2               D1              D0
297      * +-------------+----------------+----------------+----------------+
298      * | INACT ac/dc | INACT_X enable | INACT_Y enable | INACT_Z enable |
299      * +-------------+----------------+----------------+----------------+
300      *
301      * See datasheet for details.
302      *
303      * @param settings The control byte to write to the ACT_INACT_CTL register.
304      */
305     void setActivityInactivityControl(int settings);
306
307     /**
308      * Get the threshold for free fall detection.
309      *
310      * @return The threshold value for free-fall detection, as an 8-bit number,
311      *         with scale factor 62.5mg/LSB.
312      */
313     int getFreefallThreshold(void);
314
315     /**
316      * Set the threshold for free fall detection.
317      *
318      * @return The threshold value for free-fall detection, as an 8-bit number,
319      *         with scale factor 62.5mg/LSB. A value of 0 may result in
320      *         undesirable behavior if the free-fall interrupt is enabled.
321      *         Values between 300 mg and 600 mg (0x05 to 0x09) are recommended.
322      */
323     void setFreefallThreshold(int threshold);
324
325     /**
326      * Get the time required to generate a free fall interrupt.
327      *
328      * @return The minimum time that the value of all axes must be less than
329      *         the freefall threshold to generate a free-fall interrupt, in
330      *         milliseconds.
331      */
332     int getFreefallTime(void);
333
334     /**
335      * Set the time required to generate a free fall interrupt.
336      *
337      * @return The minimum time that the value of all axes must be less than
338      *         the freefall threshold to generate a free-fall interrupt, in
339      *         milliseconds. A value of 0 may result in undesirable behavior
340      *         if the free-fall interrupt is enabled. Values between 100 ms
341      *         and 350 ms (0x14 to 0x46) are recommended.
342      */
343     void setFreefallTime(int freefallTime_ms);
344
345     /**
346      * Get the axis tap settings.
347      *
348      *      D3           D2            D1             D0
349      * +----------+--------------+--------------+--------------+
350      * | Suppress | TAP_X enable | TAP_Y enable | TAP_Z enable |
351      * +----------+--------------+--------------+--------------+
352      *
353      * (D7-D4 are 0s).
354      *
355      * See datasheet for more details.
356      *
357      * @return The contents of the TAP_AXES register.
358      */
359     int getTapAxisControl(void);
360
361     /**
362      * Set the axis tap settings.
363      *
364      *      D3           D2            D1             D0
365      * +----------+--------------+--------------+--------------+
366      * | Suppress | TAP_X enable | TAP_Y enable | TAP_Z enable |
367      * +----------+--------------+--------------+--------------+
368      *
369      * (D7-D4 are 0s).
370      *
371      * See datasheet for more details.
372      *
373      * @param The control byte to write to the TAP_AXES register.
374      */
375     void setTapAxisControl(int settings);
376
377     /**
378      * Get the source of a tap.
379      *
380      * @return The contents of the ACT_TAP_STATUS register.
381      */
382     int getTapSource(void);
383
384     /**
385      * Set the power mode.
386      *
387      * @param mode 0 -> Normal operation.
388      *             1 -> Reduced power operation.
389      */
390     void setPowerMode(char mode);
391
392     /**
393      * Set the data rate.
394      *
395      * @param rate The rate code (see #defines or datasheet).
396      */
397     void setDataRate(int rate);
398
399     /**
400      * Get the power control settings.
401      *
402      * See datasheet for details.
403      *
404      * @return The contents of the POWER_CTL register.
405      */
406     int getPowerControl(void);
407
408     /**
409      * Set the power control settings.
410      *
411      * See datasheet for details.
412      *
413      * @param The control byte to write to the POWER_CTL register.
414      */
415     void setPowerControl(int settings);
416
417     /**
418      * Get the interrupt enable settings.
419      *
420      * @return The contents of the INT_ENABLE register.
421      */
422     int getInterruptEnableControl(void);
423
424     /**
425      * Set the interrupt enable settings.
426      *
427      * @param settings The control byte to write to the INT_ENABLE register.
428      */
429     void setInterruptEnableControl(int settings);
430
431     /**
432      * Get the interrupt mapping settings.
433      *
434      * @return The contents of the INT_MAP register.
435      */
436     int getInterruptMappingControl(void);
437
438     /**
439      * Set the interrupt mapping settings.
440      *
441      * @param settings The control byte to write to the INT_MAP register.
442      */
443     void setInterruptMappingControl(int settings);
444
445     /**
446      * Get the interrupt source.
447      *
448      * @return The contents of the INT_SOURCE register.
449      */
450     int getInterruptSource(void);
451
452     /**
453      * Get the data format settings.
454      *
455      * @return The contents of the DATA_FORMAT register.
456      */
457     int getDataFormatControl(void);
458
459     /**
460      * Set the data format settings.
461      *
462      * @param settings The control byte to write to the DATA_FORMAT register.
463      */
464     void setDataFormatControl(int settings);
465
466     /**
467      * Get the output of all three axes.
468      *
469      * @param Pointer to a buffer to hold the accelerometer value for the
470      *        x-axis, y-axis and z-axis [in that order].
471      */
472     void getOutput(int* readings);
473
474     /**
475      * Get the FIFO control settings.
476      *
477      * @return The contents of the FIFO_CTL register.
478      */
479     int getFifoControl(void);
480
481     /**
482      * Set the FIFO control settings.
483      *
484      * @param The control byte to write to the FIFO_CTL register.
485      */
486     void setFifoControl(int settings);
487
488     /**
489      * Get FIFO status.
490      *
491      * @return The contents of the FIFO_STATUS register.
492      */
493     int getFifoStatus(void);
494
495 private:
496
497     SPI        spi_;
498     DigitalOut nCS_;
499
500     /**
501      * Read one byte from a register on the device.
502      *
503      * @param address Address of the register to read.
504      *
505      * @return The contents of the register address.
506      */
507     int oneByteRead(int address);
508
509     /**
510      * Write one byte to a register on the device.
511      *
512      * @param address Address of the register to write to.
513      * @param data The data to write into the register.
514      */
515     void oneByteWrite(int address, char data);
516
517     /**
518      * Read several consecutive bytes on the device.
519      *
520      * @param startAddress The address of the first register to read from.
521      * @param buffer Pointer to a buffer to store data read from the device.
522      * @param size The number of bytes to read.
523      */
524     void multiByteRead(int startAddress, char* buffer, int size);
525
526     /**
527      * Write several consecutive bytes on the device.
528      *
529      * @param startAddress The address of the first register to write to.
530      * @param buffer Pointer to a buffer which contains the data to write.
531      * @param size The number of bytes to write.
532      */
533     void multiByteWrite(int startAddress, char* buffer, int size);
534
535 };
536
537 #endif /* ADXL345_H */