123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405 |
- /**
- * @author Aaron Berk
- *
- * @section LICENSE
- *
- * Copyright (c) 2010 ARM Limited
- *
- * 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.
- *
- * @section DESCRIPTION
- *
- * ADXL345, triple axis, digital interface, accelerometer.
- *
- * Datasheet:
- *
- * http://www.analog.com/static/imported-files/data_sheets/ADXL345.pdf
- */
-
- /**
- * Includes
- */
- #include "ADXL345.h"
-
- ADXL345::ADXL345(PinName mosi,
- PinName miso,
- PinName sck,
- PinName cs) : spi_(mosi, miso, sck), nCS_(cs) {
-
- //2MHz, allowing us to use the fastest data rates.
- spi_.frequency(2000000);
- spi_.format(8,3);
-
- nCS_ = 1;
-
- wait_us(500);
-
- }
-
- int ADXL345::getDevId(void) {
-
- return oneByteRead(ADXL345_DEVID_REG);
-
- }
-
- int ADXL345::getTapThreshold(void) {
-
- return oneByteRead(ADXL345_THRESH_TAP_REG);
-
- }
-
- void ADXL345::setTapThreshold(int threshold) {
-
- oneByteWrite(ADXL345_THRESH_TAP_REG, threshold);
-
- }
-
- int ADXL345::getOffset(int axis) {
-
- int address = 0;
-
- if (axis == ADXL345_X) {
- address = ADXL345_OFSX_REG;
- } else if (axis == ADXL345_Y) {
- address = ADXL345_OFSY_REG;
- } else if (axis == ADXL345_Z) {
- address = ADXL345_OFSZ_REG;
- }
-
- return oneByteRead(address);
-
- }
-
- void ADXL345::setOffset(int axis, char offset) {
-
- int address = 0;
-
- if (axis == ADXL345_X) {
- address = ADXL345_OFSX_REG;
- } else if (axis == ADXL345_Y) {
- address = ADXL345_OFSY_REG;
- } else if (axis == ADXL345_Z) {
- address = ADXL345_OFSZ_REG;
- }
-
- return oneByteWrite(address, offset);
-
- }
-
- int ADXL345::getTapDuration(void) {
-
- return oneByteRead(ADXL345_DUR_REG)*625;
-
- }
-
- void ADXL345::setTapDuration(int duration_us) {
-
- int tapDuration = duration_us / 625;
-
- oneByteWrite(ADXL345_DUR_REG, tapDuration);
-
- }
-
- float ADXL345::getTapLatency(void) {
-
- return oneByteRead(ADXL345_LATENT_REG)*1.25;
-
- }
-
- void ADXL345::setTapLatency(int latency_ms) {
-
- int tapLatency = latency_ms / 1.25;
-
- oneByteWrite(ADXL345_LATENT_REG, tapLatency);
-
- }
-
- float ADXL345::getWindowTime(void) {
-
- return oneByteRead(ADXL345_WINDOW_REG)*1.25;
-
- }
-
- void ADXL345::setWindowTime(int window_ms) {
-
- int windowTime = window_ms / 1.25;
-
- oneByteWrite(ADXL345_WINDOW_REG, windowTime);
-
- }
-
- int ADXL345::getActivityThreshold(void) {
-
- return oneByteRead(ADXL345_THRESH_ACT_REG);
-
- }
-
- void ADXL345::setActivityThreshold(int threshold) {
-
- oneByteWrite(ADXL345_THRESH_ACT_REG, threshold);
-
- }
-
- int ADXL345::getInactivityThreshold(void) {
-
- return oneByteRead(ADXL345_THRESH_INACT_REG);
-
- }
-
- void ADXL345::setInactivityThreshold(int threshold) {
-
- return oneByteWrite(ADXL345_THRESH_INACT_REG, threshold);
-
- }
-
- int ADXL345::getTimeInactivity(void) {
-
- return oneByteRead(ADXL345_TIME_INACT_REG);
-
- }
-
- void ADXL345::setTimeInactivity(int timeInactivity) {
-
- oneByteWrite(ADXL345_TIME_INACT_REG, timeInactivity);
-
- }
-
- int ADXL345::getActivityInactivityControl(void) {
-
- return oneByteRead(ADXL345_ACT_INACT_CTL_REG);
-
- }
-
- void ADXL345::setActivityInactivityControl(int settings) {
-
- oneByteWrite(ADXL345_ACT_INACT_CTL_REG, settings);
-
- }
-
- int ADXL345::getFreefallThreshold(void) {
-
- return oneByteRead(ADXL345_THRESH_FF_REG);
-
- }
-
- void ADXL345::setFreefallThreshold(int threshold) {
-
- oneByteWrite(ADXL345_THRESH_FF_REG, threshold);
-
- }
-
- int ADXL345::getFreefallTime(void) {
-
- return oneByteRead(ADXL345_TIME_FF_REG)*5;
-
- }
-
- void ADXL345::setFreefallTime(int freefallTime_ms) {
-
- int freefallTime = freefallTime_ms / 5;
-
- oneByteWrite(ADXL345_TIME_FF_REG, freefallTime);
-
- }
-
- int ADXL345::getTapAxisControl(void) {
-
- return oneByteRead(ADXL345_TAP_AXES_REG);
-
- }
-
- void ADXL345::setTapAxisControl(int settings) {
-
- oneByteWrite(ADXL345_TAP_AXES_REG, settings);
-
- }
-
- int ADXL345::getTapSource(void) {
-
- return oneByteRead(ADXL345_ACT_TAP_STATUS_REG);
-
- }
-
- void ADXL345::setPowerMode(char mode) {
-
- //Get the current register contents, so we don't clobber the rate value.
- char registerContents = oneByteRead(ADXL345_BW_RATE_REG);
-
- registerContents = (mode << 4) | registerContents;
-
- oneByteWrite(ADXL345_BW_RATE_REG, registerContents);
-
- }
-
- int ADXL345::getPowerControl(void) {
-
- return oneByteRead(ADXL345_POWER_CTL_REG);
-
- }
-
- void ADXL345::setPowerControl(int settings) {
-
- oneByteWrite(ADXL345_POWER_CTL_REG, settings);
-
- }
-
- int ADXL345::getInterruptEnableControl(void) {
-
- return oneByteRead(ADXL345_INT_ENABLE_REG);
-
- }
-
- void ADXL345::setInterruptEnableControl(int settings) {
-
- oneByteWrite(ADXL345_INT_ENABLE_REG, settings);
-
- }
-
- int ADXL345::getInterruptMappingControl(void) {
-
- return oneByteRead(ADXL345_INT_MAP_REG);
-
- }
-
- void ADXL345::setInterruptMappingControl(int settings) {
-
- oneByteWrite(ADXL345_INT_MAP_REG, settings);
-
- }
-
- int ADXL345::getInterruptSource(void){
-
- return oneByteRead(ADXL345_INT_SOURCE_REG);
-
- }
-
- int ADXL345::getDataFormatControl(void){
-
- return oneByteRead(ADXL345_DATA_FORMAT_REG);
-
- }
-
- void ADXL345::setDataFormatControl(int settings){
-
- oneByteWrite(ADXL345_DATA_FORMAT_REG, settings);
-
- }
-
- void ADXL345::setDataRate(int rate) {
-
- //Get the current register contents, so we don't clobber the power bit.
- char registerContents = oneByteRead(ADXL345_BW_RATE_REG);
-
- registerContents &= 0x10;
- registerContents |= rate;
-
- oneByteWrite(ADXL345_BW_RATE_REG, registerContents);
-
- }
-
- void ADXL345::getOutput(int* readings){
-
- char buffer[6];
-
- multiByteRead(ADXL345_DATAX0_REG, buffer, 6);
-
- readings[0] = (int)buffer[1] << 8 | (int)buffer[0];
- readings[1] = (int)buffer[3] << 8 | (int)buffer[2];
- readings[2] = (int)buffer[5] << 8 | (int)buffer[4];
-
- }
-
- int ADXL345::getFifoControl(void){
-
- return oneByteRead(ADXL345_FIFO_CTL);
-
- }
-
- void ADXL345::setFifoControl(int settings){
-
- oneByteWrite(ADXL345_FIFO_STATUS, settings);
-
- }
-
- int ADXL345::getFifoStatus(void){
-
- return oneByteRead(ADXL345_FIFO_STATUS);
-
- }
-
- int ADXL345::oneByteRead(int address) {
-
- int tx = (ADXL345_SPI_READ | (address & 0x3F));
- int rx = 0;
-
- nCS_ = 0;
- //Send address to read from.
- spi_.write(tx);
- //Read back contents of address.
- rx = spi_.write(0x00);
- nCS_ = 1;
-
- return rx;
-
- }
-
- void ADXL345::oneByteWrite(int address, char data) {
-
- int tx = (ADXL345_SPI_WRITE | (address & 0x3F));
-
- nCS_ = 0;
- //Send address to write to.
- spi_.write(tx);
- //Send data to be written.
- spi_.write(data);
- nCS_ = 1;
-
- }
-
- void ADXL345::multiByteRead(int startAddress, char* buffer, int size) {
-
- int tx = (ADXL345_SPI_READ | ADXL345_MULTI_BYTE | (startAddress & 0x3F));
-
- nCS_ = 0;
- //Send address to start reading from.
- spi_.write(tx);
-
- for (int i = 0; i < size; i++) {
- buffer[i] = spi_.write(0x00);
- }
-
- nCS_ = 1;
-
- }
-
- void ADXL345::multiByteWrite(int startAddress, char* buffer, int size) {
-
- int tx = (ADXL345_SPI_WRITE | ADXL345_MULTI_BYTE | (startAddress & 0x3F));
-
- nCS_ = 0;
- //Send address to start reading from.
- spi_.write(tx);
-
- for (int i = 0; i < size; i++) {
- buffer[i] = spi_.write(0x00);
- }
-
- nCS_ = 1;
-
- }
|