Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependents: ADXL362-helloworld EVAL_ADXL362_ARDZ Lab6 Lab6 ... more
For additional information check out the mbed page of the Analog Devices wiki: https://wiki.analog.com/resources/tools-software/mbed-drivers-all
Revision 0:2e21c4508cab, committed 2016-05-18
- Comitter:
- adisuciu
- Date:
- Wed May 18 15:17:10 2016 +0000
- Child:
- 1:ae171c032dc0
- Commit message:
- Initial revision
Changed in this revision
| ADXL362.cpp | Show annotated file Show diff for this revision Revisions of this file |
| ADXL362.h | Show annotated file Show diff for this revision Revisions of this file |
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/ADXL362.cpp Wed May 18 15:17:10 2016 +0000
@@ -0,0 +1,602 @@
+/**
+* @file ADXL362.cpp
+* @brief Source file for ADXL362
+* @author Analog Devices Inc.
+*
+* For support please go to:
+* Github: https://github.com/analogdevicesinc/mbed-adi
+* Support: https://ez.analog.com/community/linux-device-drivers/microcontroller-no-os-drivers
+* Product: http://www.analog.com/adxl362
+* More: https://wiki.analog.com/resources/tools-software/mbed-drivers-all
+
+********************************************************************************
+* Copyright 2016(c) Analog Devices, Inc.
+*
+* All rights reserved.
+*
+* Redistribution and use in source and binary forms, with or without
+* modification, are permitted provided that the following conditions are met:
+* - Redistributions of source code must retain the above copyright
+* notice, this list of conditions and the following disclaimer.
+* - Redistributions in binary form must reproduce the above copyright
+* notice, this list of conditions and the following disclaimer in
+* the documentation and/or other materials provided with the
+* distribution.
+* - Neither the name of Analog Devices, Inc. nor the names of its
+* contributors may be used to endorse or promote products derived
+* from this software without specific prior written permission.
+* - The use of this software may or may not infringe the patent rights
+* of one or more patent holders. This license does not release you
+* from the requirement that you obtain separate licenses from these
+* patent holders to use this software.
+* - Use of the software either in source or binary form, must be run
+* on or directly connected to an Analog Devices Inc. component.
+*
+* THIS SOFTWARE IS PROVIDED BY ANALOG DEVICES "AS IS" AND ANY EXPRESS OR
+* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, NON-INFRINGEMENT,
+* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+* IN NO EVENT SHALL ANALOG DEVICES BE LIABLE FOR ANY DIRECT, INDIRECT,
+* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+* LIMITED TO, INTELLECTUAL PROPERTY RIGHTS, PROCUREMENT OF SUBSTITUTE GOODS OR
+* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
+* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+*
+********************************************************************************/
+
+#include <stdint.h>
+#include "mbed.h"
+#include "ADXL362.h"
+
+/**
+ * ADXL362 constructor. Sets CS and SPI bus
+ * @param CS - CS pin of the ADXL362
+ * @param MOSI - MOSI pin of the ADXL362
+ * @param MISO - MISO pin of the ADXL362
+ * @param SCK- SCK pin of the ADXL362
+ */
+ADXL362::ADXL362(PinName CS, PinName MOSI, PinName MISO, PinName SCK) :
+ adxl362(MOSI, MISO, SCK), cs(CS), _int1(NULL), _int2(NULL), _int1_poll(NC), _int2_poll(
+ NC)
+{
+ cs = true; // cs is active low
+ adxl362.format(8, _SPI_MODE);
+ _temp_stored_in_fifo = false;
+ _int1_act_low = true;
+ _int2_act_low = true;
+}
+
+/**
+ * Sets ADXL362 SPI bus frequency
+ * @param hz - frequency in hz
+ */
+void ADXL362::frequency(int hz)
+{
+ adxl362.frequency(hz);
+}
+
+/**
+ * Resets the ADXL362
+ * A latency of approximately 0.5 ms is required after soft reset.
+ */
+void ADXL362::reset()
+{
+ adxl362.format(8, _SPI_MODE);
+ cs = false;
+ // Writing Code 0x52 (representing the letter, R, in ASCII or unicode) to this register immediately resets the ADXL362.
+ write_reg(SOFT_RESET, 0x52);
+ cs = true;
+
+}
+
+/**
+ * Writes the reg register with data
+ * @param reg - ADXL362_register_t register to be written
+ * @param data - data to be written
+ */
+void ADXL362::write_reg(ADXL362_register_t reg, uint8_t data)
+{
+ adxl362.format(8, _SPI_MODE);
+ cs = false;
+ adxl362.write(_WRITE_REG_CMD);
+ adxl362.write(static_cast<uint8_t>(reg));
+ adxl362.write(static_cast<uint8_t>(data));
+ cs = true;
+}
+
+/**
+ * Reads the reg register
+ * @param reg - ADXL362_register_t register to be read
+ * @return - data read from the register
+ */
+uint8_t ADXL362::read_reg(ADXL362_register_t reg)
+{
+ uint8_t ret_val;
+ adxl362.format(8, _SPI_MODE);
+ cs = false;
+ adxl362.write(_READ_REG_CMD);
+ adxl362.write(static_cast<uint8_t>(reg));
+ ret_val = adxl362.write(_DUMMY_BYTE);
+ cs = true;
+ return ret_val;
+}
+
+/**
+ * Writes 16 bit registers to the ADXL362. Performs conversion from Intel to Motorola byte order
+ * @param reg - ADXL362_register_t register to be written
+ * @param data - data to be written
+ */
+void ADXL362::write_reg_u16(ADXL362_register_t reg, uint16_t data)
+{
+ adxl362.format(8, _SPI_MODE);
+
+ cs = false;
+ adxl362.write(_WRITE_REG_CMD);
+ adxl362.write(static_cast<uint8_t>(reg));
+ adxl362.write(static_cast<uint8_t>(data & 0xff));
+ adxl362.write(static_cast<uint8_t>((data & 0xff00) >> 8));
+
+ cs = true;
+}
+
+/**
+ * Reads 16 bit registers from the ADXL362. Performs conversion from Motorola to Intel Byte order
+ * @param reg - ADXL362_register_t register to be read
+ * @return - data read from the ADXL362
+ */
+uint16_t ADXL362::read_reg_u16(ADXL362_register_t reg)
+{
+ uint16_t ret_val = 0;
+ adxl362.format(8, _SPI_MODE);
+
+ cs = false;
+ adxl362.write(_READ_REG_CMD);
+ adxl362.write(static_cast<uint8_t>(reg));
+ ret_val = adxl362.write(_DUMMY_BYTE);
+ ret_val = ret_val | (adxl362.write(_DUMMY_BYTE) << 8);
+ cs = true;
+
+ return ret_val;
+}
+
+/**
+ * Scans the X,Y,Z,T registers for data.
+ * ADXL362 needs to be in measurement mode to read data
+ * ADXL362 is set in measurement mode using ADXL362::set_mode(ADXL362::MEASUREMENT)
+ * @return a 64 bit integer with the following format 0xXXYYZZTT
+ */
+uint64_t ADXL362::scan()
+{
+ uint64_t ret_val = 0;
+ uint16_t x, y, z, t = 0;
+
+ adxl362.format(8, _SPI_MODE);
+ cs = false;
+ adxl362.write(_READ_REG_CMD);
+ adxl362.write(static_cast<uint8_t>(XDATA_L));
+
+ x = adxl362.write(_DUMMY_BYTE);
+ x = x | static_cast<uint16_t>(adxl362.write(_DUMMY_BYTE) << 8);
+ y = adxl362.write(_DUMMY_BYTE);
+ y = y | static_cast<uint16_t>(adxl362.write(_DUMMY_BYTE) << 8);
+ z = adxl362.write(_DUMMY_BYTE);
+ z = z | static_cast<uint16_t>(adxl362.write(_DUMMY_BYTE) << 8);
+ t = adxl362.write(_DUMMY_BYTE);
+ t = t | static_cast<uint16_t>(adxl362.write(_DUMMY_BYTE) << 8);
+
+ ret_val = static_cast<uint64_t>(x) << 48;
+ ret_val |= static_cast<uint64_t>(y) << 32;
+ ret_val |= static_cast<uint64_t>(z) << 16;
+ ret_val |= static_cast<uint64_t>(t);
+ cs = true;
+ return ret_val;
+}
+
+/**
+ * Reads the X 8 bit register from the ADXL362
+ * ADXL362 is set in measurement mode using ADXL362::set_mode(ADXL362::MEASUREMENT)
+ * @return 8 bit X data
+ */
+uint8_t ADXL362::scanx_u8()
+{
+ return read_reg(XDATA);
+}
+
+/**
+ * Reads the X 16 bit register from the ADXL362
+ * ADXL362 is set in measurement mode using ADXL362::set_mode(ADXL362::MEASUREMENT)
+ * @return 16 bit X data
+ */
+uint16_t ADXL362::scanx()
+{
+ return read_reg_u16(XDATA_L);
+}
+
+/**
+ * Reads the Y 8 bit register from the ADXL362
+ * ADXL362 is set in measurement mode using ADXL362::set_mode(ADXL362::MEASUREMENT)
+ * @return 8 bit Y data
+ */
+uint8_t ADXL362::scany_u8()
+{
+ return read_reg(YDATA);
+}
+
+/**
+ * Reads the Y 16 bit register from the ADXL362
+ * ADXL362 is set in measurement mode using ADXL362::set_mode(ADXL362::MEASUREMENT)
+ * @return 16 bit Y data
+ */
+uint16_t ADXL362::scany()
+{
+ return read_reg_u16(YDATA_L);
+}
+
+/**
+ * Reads the Z 8 bit register from the ADXL362
+ * ADXL362 is set in measurement mode using ADXL362::set_mode(ADXL362::MEASUREMENT)
+ * @return 8 bit Z data
+ */
+uint8_t ADXL362::scanz_u8()
+{
+ return read_reg(ZDATA);
+}
+
+/**
+ * Reads the Z 16 bit register from the ADXL362
+ * ADXL362 is set in measurement mode using ADXL362::set_mode(ADXL362::MEASUREMENT)
+ * @return 16 bit Z data
+ */
+uint16_t ADXL362::scanz()
+{
+ return read_reg_u16(ZDATA_L);
+}
+
+/**
+ * Reads the T 16 bit register from the ADXL362
+ * ADXL362 is set in measurement mode using ADXL362::set_mode(ADXL362::MEASUREMENT)
+ * @return 16 bit T data
+ */
+uint16_t ADXL362::scant()
+{
+ return read_reg_u16(TEMP_L);
+}
+
+/**
+ * Sets the STANDBY/MEASUREMENT mode of the ADXL362
+ * @param mode - ADXL362_modes_t STANDBY/MEASUREMENT mode
+ */
+void ADXL362::set_mode(ADXL362_modes_t mode)
+{
+ uint8_t reg_val;
+ reg_val = read_reg(POWER_CTL);
+ reg_val = reg_val | static_cast<uint8_t>(mode);
+ write_reg(POWER_CTL, reg_val);
+}
+
+/**
+ * Sets the activity threshold registers
+ * To enable activity/inactivity, the ACT_INACT_CTL reg must also be set
+ * using the ADXL362::set_act_inact_ctl_reg(uint8_t data) method
+ * @param threshold - activity threshold in natural format
+ */
+void ADXL362::set_activity_threshold(uint16_t threshold)
+{
+ write_reg_u16(THRESH_ACT_L, threshold);
+}
+
+/**
+ * Sets the activity time register
+ * To enable activity/inactivity, the ACT_INACT_CTL reg must also be set
+ * using the ADXL362::set_act_inact_ctl_reg(uint8_t data) method
+ * @param time - activity time
+ */
+void ADXL362::set_activity_time(uint8_t time)
+{
+ write_reg(TIME_ACT, time);
+}
+
+/**
+ * Sets the inactivity threshold register
+ * To enable activity/inactivity, the ACT_INACT_CTL reg must also be set
+ * using the ADXL362::set_act_inact_ctl_reg(uint8_t data) method
+ * @param threshold - inactivity threshold in natural format
+ */
+void ADXL362::set_inactivity_threshold(uint16_t threshold)
+{
+ write_reg_u16(THRESH_INACT_L, threshold);
+}
+
+/**
+ * Sets the inactivity time register
+ * To enable activity/inactivity, the ACT_INACT_CTL reg must also be set
+ * using the ADXL362::set_act_inact_ctl_reg(uint8_t data) method
+ * @param time - inactivity time in natural format
+ */
+void ADXL362::set_inactivity_time(uint16_t time)
+{
+ write_reg_u16(TIME_INACT_L, time);
+}
+
+/**
+ * Sets the ACT_INACT_CTL register of the ADXL362
+ * @param data - data to be written to the register
+ */
+void ADXL362::set_act_inact_ctl_reg(uint8_t data)
+{
+ write_reg(ACT_INACT_CTL, data);
+}
+
+/**
+ * Configures INT1 output of the ADXL362 for polling use
+ * @param in - uC pin connected to ADXL362's INT1
+ * @param data - data to be written to INTMAP1
+ * @param pull - (optional) configures pullup on In pin
+ */
+void ADXL362::set_polling_interrupt1_pin(PinName in, uint8_t data,
+ PinMode pull)
+{
+ if ((data & 0x7F) != 0) {
+ write_reg(INTMAP1, data);
+ _int1_poll = DigitalIn(in);
+ _int1_poll.mode(pull);
+ if (data & 0x80) {
+ _int1_act_low = true;
+ } else {
+ _int1_act_low = false;
+ }
+ }
+}
+
+/**
+ * Configures INT2 output of the ADXL362 for polling use
+ * @param in - uC pin connected to ADXL362's INT2
+ * @param data - data to be written to INTMAP2
+ * @param pull - (optional) configures pullup on In pin
+ */
+void ADXL362::set_polling_interrupt2_pin(PinName in, uint8_t data,
+ PinMode pull)
+{
+ if ((data & 0x7F) != 0) {
+ write_reg(INTMAP2, data);
+ _int2_poll = DigitalIn(in);
+ _int2_poll.mode(pull);
+ if (data & 0x80) {
+ _int2_act_low = true;
+ } else {
+ _int2_act_low = false;
+ }
+ }
+}
+
+/**
+ * Gets the active state of the INT1 pin
+ * @return true if active, false if not active
+ */
+bool ADXL362::get_int1()
+{
+ if(_int1_poll != NC) return (_int1_poll.read() != _int1_act_low); // boolean XOR
+ else return (_int1->read() != _int1_act_low);
+}
+
+/**
+ * Gets the active state of the INT2 pin
+ * @return true if active, false if not active
+ */
+bool ADXL362::get_int2()
+{
+ if(_int2_poll != NC) return (_int1_poll.read() != _int1_act_low); // boolean XOR
+ else return (_int2->read() != _int2_act_low);
+}
+
+
+/**
+ * Configures the INT1 pin of the ADXL362 to be used in interrupt mode
+ * @param in - uC pin connected to ADXL362's INT1
+ * @param data - data to be written to INTMAP1
+ * @param callback_rising - rising edge interrupt callback - can be set to NULL if no callback is required for rising edge
+ * @param callback_falling - falling edge interrupt callback - can be set to NULL if no callback is required for falling edge
+ * @param pull - (optional) configures pullup on In pin
+ */
+void ADXL362::set_interrupt1_pin(PinName in, uint8_t data,
+ void (*callback_rising)(void), void (*callback_falling)(void), PinMode pull)
+{
+ if ((data & 0x7F) != 0) {
+ write_reg(INTMAP1, data);
+ delete _int1;
+ _int1 = new InterruptIn(in);
+ _int1->mode(pull);
+ if(callback_falling != NULL) _int1->fall(callback_falling);
+ if(callback_rising != NULL) _int1->rise(callback_rising);
+ if (data & 0x80) {
+ _int1_act_low = true;
+ } else {
+ _int1_act_low = false;
+ }
+ }
+}
+
+
+/**
+ * Configures the INT2 pin of the ADXL362 to be used in interrupt mode
+ * @param in - uC pin connected to ADXL362's INT2
+ * @param data - data to be written to INTMAP2
+ * @param callback_rising - rising edge interrupt callback - can be set to NULL if no callback is required for rising edge
+ * @param callback_falling - falling edge interrupt callback - can be set to NULL if no callback is required for falling edge
+ * @param pull - (optional) configures pullup on In pin
+ */
+void ADXL362::set_interrupt2_pin(PinName in, uint8_t data,
+ void (*callback_rising)(void), void (*callback_falling)(void), PinMode pull)
+{
+ if ((data & 0x7F) != 0) {
+ write_reg(INTMAP2, data);
+ delete _int2;
+ _int2 = new InterruptIn(in);
+ _int2->mode(pull);
+ if(callback_falling != NULL) _int2->fall(callback_falling);
+ if(callback_rising != NULL) _int2->rise(callback_rising);
+ if (data & 0x80) {
+ _int2_act_low = true;
+ } else {
+ _int2_act_low = false;
+ }
+ }
+}
+
+/**
+ * Enables external interrupt registration for pin configured as INT1
+ * To enable this interrupt, it must first be configured using ADXL362::set_interrupt1_pin()
+ */
+void ADXL362::enable_interrupt1()
+{
+ _int1->enable_irq();
+}
+
+/**
+ * Enables external interrupt registration for pin configured as INT2
+ * * To enable this interrupt, it must first be configured using ADXL362::set_interrupt2_pin()
+ */
+void ADXL362::enable_interrupt2()
+{
+ _int2->enable_irq();
+}
+
+/**
+ * Disables external interrupt registration for pin configured as INT1
+ */
+void ADXL362::disable_interrupt1()
+{
+ _int1->disable_irq();
+}
+
+/**
+ * Disables external interrupt registration for pin configured as INT2
+ */
+void ADXL362::disable_interrupt2()
+{
+ _int2->disable_irq();
+}
+
+/**
+ * Sets the POWER_CTL register
+ * @param data - data to be written to the register
+ */
+void ADXL362::set_power_ctl_reg(uint8_t data)
+{
+ write_reg(POWER_CTL, data);
+}
+
+/**
+ * Sets the FILTER_CTL register
+ * @param data - data to be written to the register
+ */
+void ADXL362::set_filter_ctl_reg(uint8_t data)
+{
+ write_reg(FILTER_CTL, data);
+}
+
+/**
+ * Reads the STATUS register of the ADXL362
+ * @return - data in the status register
+ */
+uint8_t ADXL362::read_status()
+{
+ return read_reg(STATUS);
+}
+
+/**
+ * Reads the FIFO_ENTRIES_L and FIFO_ENTRIES_H register
+ * @return the number of entries in the FIFO
+ */
+uint16_t ADXL362::fifo_read_nr_of_entries()
+{
+ return read_reg_u16(FIFO_ENTRIES_L);
+}
+
+/**
+ * Setup for the FIFO
+ * @param store_temp - boolean, true - temperature will be stored in the fifo. false otherwise
+ * @param mode - ADXL362_FIFO_modes_t fifo mode
+ * @param nr_of_entries - number of entries in the FIFO
+ */
+void ADXL362::fifo_setup(bool store_temp, ADXL362_FIFO_modes_t mode, uint16_t nr_of_entries)
+{
+ uint8_t fifo_ctl = static_cast<uint8_t>(mode);
+ _temp_stored_in_fifo = store_temp;
+
+ fifo_ctl = fifo_ctl | (static_cast<uint8_t>(_temp_stored_in_fifo) << 2);
+
+ if (nr_of_entries > 0xff) {
+ fifo_ctl = fifo_ctl | static_cast<uint8_t>(AH);
+ }
+ write_reg(FIFO_CONTROL, fifo_ctl);
+ write_reg(FIFO_SAMPLES, static_cast<uint8_t>(nr_of_entries & 0xff));
+
+}
+
+/**
+ * Reads a FIFO entry
+ * @return FIFO entry
+ */
+uint16_t ADXL362::fifo_read_u16()
+{
+ uint16_t ret_val = 0;
+ adxl362.format(8, _SPI_MODE);
+
+ cs = false;
+ adxl362.write(_READ_FIFO_CMD);
+ ret_val = adxl362.write(_DUMMY_BYTE);
+ ret_val = (ret_val) | static_cast<uint16_t>(adxl362.write(_DUMMY_BYTE) << 8);
+ cs = true;
+
+ return ret_val;
+}
+
+/**
+ * Reads 3(4) bytes from the FIFO(if store_temp was set), assembles the data in the format used by the scan method
+ * ADXL362::fifo_setup() needs to be called before calling fifo_scan to ensure correct fifo operation
+ * fifo_scan and fifo_read_u16 should not be used as fifo_read_u16 disaligns the fifo therefore
+ * fifo_scan will return data from multiple samples
+ * @return scanned data from the fifo in the 0xXXYYZZTT format
+ */
+uint64_t ADXL362::fifo_scan()
+{
+
+ uint64_t ret_val = 0;
+ uint16_t x = 0, y = 0, z = 0, dummy, t = 0, sample_type;
+
+ adxl362.format(8, _SPI_MODE);
+ cs = false;
+ adxl362.write(_READ_FIFO_CMD);
+ uint8_t samples = (_temp_stored_in_fifo) ? 4 : 3;
+ for(uint8_t i = 0; i < samples; i++) {
+ dummy = adxl362.write(_DUMMY_BYTE);
+ dummy = dummy | static_cast<uint16_t>(adxl362.write(_DUMMY_BYTE) << 8);
+ sample_type = (dummy & 0xc000) >> 14;
+ dummy = dummy & 0x3fff;
+ switch(sample_type) {
+ case 0: // x
+ x = dummy;
+ break;
+ case 1: // y
+ y = dummy;
+ break;
+ case 2: // z
+ z = dummy;
+ break;
+ case 3: // temp
+ t = dummy;
+ break;
+ }
+
+ }
+
+ // format xxyyzztt
+ ret_val = static_cast<uint64_t> (x) << 48;
+ ret_val |= static_cast<uint64_t>(y) << 32;
+ ret_val |= static_cast<uint64_t>(z) << 16;
+ ret_val |= static_cast<uint64_t>(t);
+ cs = true;
+ return ret_val;
+}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/ADXL362.h Wed May 18 15:17:10 2016 +0000
@@ -0,0 +1,257 @@
+#ifndef ADXL362_H_
+#define ADXL362_H_
+
+/**
+* @file ADXL362.cpp
+* @brief Header file for ADXL362
+* @author Analog Devices Inc.
+*
+* For support please go to:
+* Github: https://github.com/analogdevicesinc/mbed-adi
+* Support: https://ez.analog.com/community/linux-device-drivers/microcontroller-no-os-drivers
+* Product: http://www.analog.com/adxl362
+* More: https://wiki.analog.com/resources/tools-software/mbed-drivers-all
+
+********************************************************************************
+* Copyright 2016(c) Analog Devices, Inc.
+*
+* All rights reserved.
+*
+* Redistribution and use in source and binary forms, with or without
+* modification, are permitted provided that the following conditions are met:
+* - Redistributions of source code must retain the above copyright
+* notice, this list of conditions and the following disclaimer.
+* - Redistributions in binary form must reproduce the above copyright
+* notice, this list of conditions and the following disclaimer in
+* the documentation and/or other materials provided with the
+* distribution.
+* - Neither the name of Analog Devices, Inc. nor the names of its
+* contributors may be used to endorse or promote products derived
+* from this software without specific prior written permission.
+* - The use of this software may or may not infringe the patent rights
+* of one or more patent holders. This license does not release you
+* from the requirement that you obtain separate licenses from these
+* patent holders to use this software.
+* - Use of the software either in source or binary form, must be run
+* on or directly connected to an Analog Devices Inc. component.
+*
+* THIS SOFTWARE IS PROVIDED BY ANALOG DEVICES "AS IS" AND ANY EXPRESS OR
+* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, NON-INFRINGEMENT,
+* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+* IN NO EVENT SHALL ANALOG DEVICES BE LIABLE FOR ANY DIRECT, INDIRECT,
+* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+* LIMITED TO, INTELLECTUAL PROPERTY RIGHTS, PROCUREMENT OF SUBSTITUTE GOODS OR
+* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
+* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+*
+********************************************************************************/
+
+#include "mbed.h"
+
+class ADXL362
+{
+public:
+
+ /* Temperature parameters */
+ typedef enum {
+ DEVID_AD = 0x00,
+ DEVID_MST = 0x01,
+ PARTID = 0x02,
+ REVID = 0x03,
+ XDATA = 0x08,
+ YDATA = 0x09,
+ ZDATA = 0x0A,
+ STATUS = 0x0B,
+ FIFO_ENTRIES_L = 0x0C,
+ FIFO_ENTRIES_H = 0x0D,
+ XDATA_L = 0x0E,
+ XDATA_H = 0x0F,
+ YDATA_L = 0x10,
+ YDATA_H = 0x11,
+ ZDATA_L = 0x12,
+ ZDATA_H = 0x13,
+ TEMP_L = 0x14,
+ TEMP_H = 0x15,
+ // Reserved = 0x16;
+ // Reserved = 0x17;
+ SOFT_RESET = 0x1F,
+ THRESH_ACT_L = 0x20,
+ THRESH_ACT_H = 0x21,
+ TIME_ACT = 0x22,
+ THRESH_INACT_L = 0x23,
+ THRESH_INACT_H = 0x24,
+ TIME_INACT_L = 0x25,
+ TIME_INACT_H = 0x26,
+ ACT_INACT_CTL = 0x27,
+ FIFO_CONTROL = 0x28,
+ FIFO_SAMPLES = 0x29,
+ INTMAP1 = 0x2A,
+ INTMAP2 = 0x2B,
+ FILTER_CTL = 0x2C,
+ POWER_CTL = 0x2D,
+ SELF_TEST = 0x2E,
+ } ADXL362_register_t;
+
+ typedef enum {
+ STANDBY = 0x00,
+ MEASUREMENT = 0x02
+ } ADXL362_modes_t;
+
+ typedef enum {
+ ERR_USER_REGS = 0x80,
+ AWAKE = 0x40,
+ INACT = 0x20,
+ ACT = 0x10,
+ FIFO_OVERRUN = 0x08,
+ FIFO_WATERMARK = 0x04,
+ FIFO_READY = 0x02,
+ DATA_READY = 0x01
+ } ADXL362_STATUS_reg_bits_t;
+
+ typedef enum {
+ LINKLOOP1 = 0x20,
+ LINKLOOP0 = 0x10,
+ DEFAULTMODE = 0x00,
+ LINKED_MODE = 0x10,
+ LOOP_MODE = 0x30,
+ INACT_REF = 0x08,
+ INACT_EN = 0x04,
+ ACT_REF = 0x02,
+ ACT_EN = 0x01
+ } ADXL362_ACT_INACT_CTL_reg_bits_t;
+
+ typedef enum {
+ AH = 0x08,
+ FIFO_TEMP = 0x04,
+ FIFO_MODE1 = 0x02,
+ FIFO_MODE = 0x01,
+ } ADXL362_FIFO_CONTROL_reg_bits_t;
+
+ typedef enum {
+ FIFO_DISABLED = 0x00,
+ FIFO_OLDEST = 0x01,
+ FIFO_STREAM = 0x02,
+ FIFO_TRIGGERED = 0x03,
+ } ADXL362_FIFO_modes_t;
+
+ typedef enum {
+ INT_LOW = 0x80,
+ INT_AWAKE = 0x40,
+ INT_INACT = 0x20,
+ INT_ACT = 0x10,
+ INT_FIFO_OVERRUN = 0x08,
+ INT_FIFO_WATERMARK = 0x04,
+ INT_FIFO_READY = 0x02,
+ INT_DATA_READY = 0x01
+ } ADXL362_INTMAP_reg_bits_t;
+
+ typedef enum {
+ RANGE1 = 0x80,
+ RANGE0 = 0x40,
+ RANGE2G = 0x00,
+ RANGE4G = 0x40,
+ RANGE8G = 0x80,
+ HALF_BW = 0x10,
+ EXT_SAMPLE = 0x08,
+ ODR2 = 0x04,
+ ODR1 = 0x02,
+ ODR0 = 0x01,
+ ODR12HZ = 0x00,
+ ODR25HZ = 0x01,
+ ODR50Hz = 0x02,
+ ODR100HZ = 0x03,
+ ODR200Hz = 0x04,
+ ODR400HZ = 0x07
+ } ADXL362_FILTER_CTL_reg_bits_t;
+
+ typedef enum {
+ EXT_CLK = 0x40,
+ LOW_NOISE1 = 0x20,
+ LOW_NOISE0 = 0x10,
+ NORMAL_OPERATION = 0x00,
+ LOW_NOISE = 0x10,
+ ULTRALOW_NOISE = 0x20,
+ WAKEUP = 0x08,
+ AUTOSLEEP = 0x04,
+ MEASURE1 = 0x02,
+ MEASURE0 = 0x01,
+ } ADXL362_POWER_CTL_reg_bits_t;
+
+ /** SPI configuration & constructor */
+ ADXL362(PinName CS = SPI_CS, PinName MOSI = SPI_MOSI, PinName MISO =
+ SPI_MISO, PinName SCK = SPI_SCK);
+ void frequency(int hz);
+
+ /** Low level SPI bus comm methods */
+ void reset(void);
+ void write_reg(ADXL362_register_t reg, uint8_t data);
+ uint8_t read_reg(ADXL362_register_t reg);
+ uint16_t read_reg_u16(ADXL362_register_t reg);
+ void write_reg_u16(ADXL362_register_t reg, uint16_t data);
+
+ /** ADXL general register R/W methods */
+ void set_power_ctl_reg(uint8_t data);
+ void set_filter_ctl_reg(uint8_t data);
+ uint8_t read_status();
+ void set_mode(ADXL362_modes_t mode);
+
+ /** ADXL X/Y/Z/T scanning methods*/
+ uint64_t scan();
+ uint8_t scanx_u8();
+ uint16_t scanx();
+ uint8_t scany_u8();
+ uint16_t scany();
+ uint8_t scanz_u8();
+ uint16_t scanz();
+ uint16_t scant();
+
+ /** ADXL362 activity methods */
+ void set_activity_threshold(uint16_t threshold);
+ void set_activity_time(uint8_t time);
+ void set_inactivity_threshold(uint16_t threshold);
+ void set_inactivity_time(uint16_t time);
+ void set_act_inact_ctl_reg(uint8_t data);
+
+ /** ADXL362 interrupt methods */
+ void set_interrupt1_pin(PinName in, uint8_t data, void (*callback_rising)(void), void (*callback_falling)(void), PinMode pull = PullNone);
+ void set_interrupt2_pin(PinName in, uint8_t data, void (*callback_rising)(void), void (*callback_falling)(void), PinMode pull = PullNone);
+ void enable_interrupt1();
+ void enable_interrupt2();
+ void disable_interrupt1();
+ void disable_interrupt2();
+
+ void set_polling_interrupt1_pin(PinName in, uint8_t data, PinMode pull = PullNone);
+ void set_polling_interrupt2_pin(PinName in, uint8_t data, PinMode pull = PullNone);
+
+ bool get_int1();
+ bool get_int2();
+
+ /** ADXL362 FIFO methods */
+ uint16_t fifo_read_nr_of_entries();
+ void fifo_setup(bool store_temp, ADXL362_FIFO_modes_t mode, uint16_t nr_of_entries);
+ uint16_t fifo_read_u16();
+ uint64_t fifo_scan();
+
+ SPI adxl362; ///< SPI instance of the ADXL362
+ DigitalOut cs; ///< DigitalOut instance for the chipselect of the ADXL362
+
+private:
+
+ InterruptIn *_int1;
+ InterruptIn *_int2;
+ DigitalIn _int1_poll;
+ DigitalIn _int2_poll;
+ bool _int1_act_low;
+ bool _int2_act_low;
+ bool _temp_stored_in_fifo;
+
+ const static uint8_t _DUMMY_BYTE = 0xAA;
+ const static uint8_t _WRITE_REG_CMD = 0x0A; // write register
+ const static uint8_t _READ_REG_CMD = 0x0B; // read register
+ const static uint8_t _READ_FIFO_CMD = 0x0D; // read FIFO
+ const static uint8_t _SPI_MODE = 0;
+};
+
+#endif
ADXL362