6 * Copyright (c) 2010 ARM Limited
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:
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
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
26 * @section DESCRIPTION
28 * ADXL345, triple axis, digital interface, accelerometer.
32 * http://www.analog.com/static/imported-files/data_sheets/ADXL345.pdf
40 ADXL345::ADXL345(PinName mosi,
43 PinName cs) : spi_(mosi, miso, sck), nCS_(cs) {
45 //2MHz, allowing us to use the fastest data rates.
46 spi_.frequency(2000000);
55 int ADXL345::getDevId(void) {
57 return oneByteRead(ADXL345_DEVID_REG);
61 int ADXL345::getTapThreshold(void) {
63 return oneByteRead(ADXL345_THRESH_TAP_REG);
67 void ADXL345::setTapThreshold(int threshold) {
69 oneByteWrite(ADXL345_THRESH_TAP_REG, threshold);
73 int ADXL345::getOffset(int axis) {
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;
85 return oneByteRead(address);
89 void ADXL345::setOffset(int axis, char offset) {
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;
101 return oneByteWrite(address, offset);
105 int ADXL345::getTapDuration(void) {
107 return oneByteRead(ADXL345_DUR_REG)*625;
111 void ADXL345::setTapDuration(int duration_us) {
113 int tapDuration = duration_us / 625;
115 oneByteWrite(ADXL345_DUR_REG, tapDuration);
119 float ADXL345::getTapLatency(void) {
121 return oneByteRead(ADXL345_LATENT_REG)*1.25;
125 void ADXL345::setTapLatency(int latency_ms) {
127 int tapLatency = latency_ms / 1.25;
129 oneByteWrite(ADXL345_LATENT_REG, tapLatency);
133 float ADXL345::getWindowTime(void) {
135 return oneByteRead(ADXL345_WINDOW_REG)*1.25;
139 void ADXL345::setWindowTime(int window_ms) {
141 int windowTime = window_ms / 1.25;
143 oneByteWrite(ADXL345_WINDOW_REG, windowTime);
147 int ADXL345::getActivityThreshold(void) {
149 return oneByteRead(ADXL345_THRESH_ACT_REG);
153 void ADXL345::setActivityThreshold(int threshold) {
155 oneByteWrite(ADXL345_THRESH_ACT_REG, threshold);
159 int ADXL345::getInactivityThreshold(void) {
161 return oneByteRead(ADXL345_THRESH_INACT_REG);
165 void ADXL345::setInactivityThreshold(int threshold) {
167 return oneByteWrite(ADXL345_THRESH_INACT_REG, threshold);
171 int ADXL345::getTimeInactivity(void) {
173 return oneByteRead(ADXL345_TIME_INACT_REG);
177 void ADXL345::setTimeInactivity(int timeInactivity) {
179 oneByteWrite(ADXL345_TIME_INACT_REG, timeInactivity);
183 int ADXL345::getActivityInactivityControl(void) {
185 return oneByteRead(ADXL345_ACT_INACT_CTL_REG);
189 void ADXL345::setActivityInactivityControl(int settings) {
191 oneByteWrite(ADXL345_ACT_INACT_CTL_REG, settings);
195 int ADXL345::getFreefallThreshold(void) {
197 return oneByteRead(ADXL345_THRESH_FF_REG);
201 void ADXL345::setFreefallThreshold(int threshold) {
203 oneByteWrite(ADXL345_THRESH_FF_REG, threshold);
207 int ADXL345::getFreefallTime(void) {
209 return oneByteRead(ADXL345_TIME_FF_REG)*5;
213 void ADXL345::setFreefallTime(int freefallTime_ms) {
215 int freefallTime = freefallTime_ms / 5;
217 oneByteWrite(ADXL345_TIME_FF_REG, freefallTime);
221 int ADXL345::getTapAxisControl(void) {
223 return oneByteRead(ADXL345_TAP_AXES_REG);
227 void ADXL345::setTapAxisControl(int settings) {
229 oneByteWrite(ADXL345_TAP_AXES_REG, settings);
233 int ADXL345::getTapSource(void) {
235 return oneByteRead(ADXL345_ACT_TAP_STATUS_REG);
239 void ADXL345::setPowerMode(char mode) {
241 //Get the current register contents, so we don't clobber the rate value.
242 char registerContents = oneByteRead(ADXL345_BW_RATE_REG);
244 registerContents = (mode << 4) | registerContents;
246 oneByteWrite(ADXL345_BW_RATE_REG, registerContents);
250 int ADXL345::getPowerControl(void) {
252 return oneByteRead(ADXL345_POWER_CTL_REG);
256 void ADXL345::setPowerControl(int settings) {
258 oneByteWrite(ADXL345_POWER_CTL_REG, settings);
262 int ADXL345::getInterruptEnableControl(void) {
264 return oneByteRead(ADXL345_INT_ENABLE_REG);
268 void ADXL345::setInterruptEnableControl(int settings) {
270 oneByteWrite(ADXL345_INT_ENABLE_REG, settings);
274 int ADXL345::getInterruptMappingControl(void) {
276 return oneByteRead(ADXL345_INT_MAP_REG);
280 void ADXL345::setInterruptMappingControl(int settings) {
282 oneByteWrite(ADXL345_INT_MAP_REG, settings);
286 int ADXL345::getInterruptSource(void){
288 return oneByteRead(ADXL345_INT_SOURCE_REG);
292 int ADXL345::getDataFormatControl(void){
294 return oneByteRead(ADXL345_DATA_FORMAT_REG);
298 void ADXL345::setDataFormatControl(int settings){
300 oneByteWrite(ADXL345_DATA_FORMAT_REG, settings);
304 void ADXL345::setDataRate(int rate) {
306 //Get the current register contents, so we don't clobber the power bit.
307 char registerContents = oneByteRead(ADXL345_BW_RATE_REG);
309 registerContents &= 0x10;
310 registerContents |= rate;
312 oneByteWrite(ADXL345_BW_RATE_REG, registerContents);
316 void ADXL345::getOutput(int* readings){
320 multiByteRead(ADXL345_DATAX0_REG, buffer, 6);
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];
328 int ADXL345::getFifoControl(void){
330 return oneByteRead(ADXL345_FIFO_CTL);
334 void ADXL345::setFifoControl(int settings){
336 oneByteWrite(ADXL345_FIFO_STATUS, settings);
340 int ADXL345::getFifoStatus(void){
342 return oneByteRead(ADXL345_FIFO_STATUS);
346 int ADXL345::oneByteRead(int address) {
348 int tx = (ADXL345_SPI_READ | (address & 0x3F));
352 //Send address to read from.
354 //Read back contents of address.
355 rx = spi_.write(0x00);
362 void ADXL345::oneByteWrite(int address, char data) {
364 int tx = (ADXL345_SPI_WRITE | (address & 0x3F));
367 //Send address to write to.
369 //Send data to be written.
375 void ADXL345::multiByteRead(int startAddress, char* buffer, int size) {
377 int tx = (ADXL345_SPI_READ | ADXL345_MULTI_BYTE | (startAddress & 0x3F));
380 //Send address to start reading from.
383 for (int i = 0; i < size; i++) {
384 buffer[i] = spi_.write(0x00);
391 void ADXL345::multiByteWrite(int startAddress, char* buffer, int size) {
393 int tx = (ADXL345_SPI_WRITE | ADXL345_MULTI_BYTE | (startAddress & 0x3F));
396 //Send address to start reading from.
399 for (int i = 0; i < size; i++) {
400 buffer[i] = spi_.write(0x00);