You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mynewt.apache.org by GitBox <gi...@apache.org> on 2018/02/28 22:31:15 UTC
[GitHub] vrahane closed pull request #829: add driver for ADXL345 sensor
vrahane closed pull request #829: add driver for ADXL345 sensor
URL: https://github.com/apache/mynewt-core/pull/829
This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:
As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):
diff --git a/hw/drivers/sensors/adxl345/include/adxl345/adxl345.h b/hw/drivers/sensors/adxl345/include/adxl345/adxl345.h
new file mode 100644
index 000000000..39eb42d57
--- /dev/null
+++ b/hw/drivers/sensors/adxl345/include/adxl345/adxl345.h
@@ -0,0 +1,198 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+#ifndef __SENSOR_ADXL345_H__
+#define __SENSOR_ADXL345_H__
+
+#include "os/os.h"
+#include "os/os_dev.h"
+#include "sensor/sensor.h"
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+#define ADXL345_INT_OVERRUN_BIT 0x1
+#define ADXL345_INT_WATERMARK_BIT 0x2
+#define ADXL345_INT_FREEFALL_BIT 0x4
+#define ADXL345_INT_INACTIVITY_BIT 0x8
+#define ADXL345_INT_ACTIVITY_BIT 0x10
+#define ADXL345_INT_DOUBLE_TAP_BIT 0x20
+#define ADXL345_INT_SINGLE_TAP_BIT 0x40
+#define ADXL345_INT_DATA_READY_BIT 0x80
+
+
+enum adxl345_accel_range {
+ ADXL345_ACCEL_RANGE_2 = 0, /* +/- 2g */
+ ADXL345_ACCEL_RANGE_4 = 1, /* +/- 4g */
+ ADXL345_ACCEL_RANGE_8 = 2, /* +/- 8g */
+ ADXL345_ACCEL_RANGE_16 = 3, /* +/- 16g */
+};
+
+enum adxl345_power_mode {
+ ADXL345_POWER_STANDBY = 0,
+ ADXL345_POWER_SLEEP = 1,
+ ADXL345_POWER_MEASURE = 2
+};
+
+enum adxl345_sample_rate {
+ ADXL345_RATE_0_1_HZ = 0, /* 0.1Hz */
+ ADXL345_RATE_0_2_HZ = 1, /* 0.2Hz */
+ ADXL345_RATE_0_39_HZ = 2, /* 0.39Hz */
+ ADXL345_RATE_0_78_HZ = 3, /* 0.78Hz */
+ ADXL345_RATE_1_56_HZ = 4, /* 1.56Hz */
+ ADXL345_RATE_3_13_HZ = 5, /* 3.13Hz */
+ ADXL345_RATE_6_25_HZ = 6, /* 6.25Hz */
+ ADXL345_RATE_12_5_HZ = 7, /* 12.5Hz */
+ ADXL345_RATE_25_HZ = 8, /* 25Hz */
+ ADXL345_RATE_50_HZ = 9, /* 50Hz */
+ ADXL345_RATE_100_HZ = 10, /* 1001Hz */
+ ADXL345_RATE_200_HZ = 11, /* 200Hz */
+ ADXL345_RATE_400_HZ = 12, /* 400Hz */
+ ADXL345_RATE_800_HZ = 13, /* 800Hz */
+ ADXL345_RATE_1600_HZ = 14, /* 1600Hz */
+ ADXL345_RATE_3200_HZ = 15 /* 3200Hz */
+};
+
+struct adxl345_tap_settings {
+ uint8_t threshold; /* threshold compared with data to identify a tap event
+ 16g range, 62.5 mg/LSB */
+ uint8_t duration; /* maximum time data is above threshold to be a tap event
+ 625us/LSB */
+ uint8_t latency; /* time to wait after a tap before window starts for detecting
+ a double tap. 1.25ms/LSB */
+ uint8_t window; /* length of time in which a double tap can be triggered, this
+ time starts after the latency time expires. 1.25ms/LSB */
+
+ uint8_t x_enable:1; /* enables tap/double tap detection on X axis */
+ uint8_t y_enable:1; /* enables tap/double tap detection on Y axis */
+ uint8_t z_enable:1; /* enables tap/double tap detection on Z axis */
+
+ uint8_t suppress:1; /* if enabled suppresses double tap detection if acceleration
+ greater than threshold is present between taps */
+
+};
+
+struct adxl345_act_inact_enables {
+ uint8_t act_x:1; /* enable detection of activity from X axis data */
+ uint8_t act_y:1; /* enable detection of activity from Y axis data */
+ uint8_t act_z:1; /* enable detection of activity from Z axis data */
+
+ uint8_t inact_x:1; /* enable detection of inactivity from X axis data */
+ uint8_t inact_y:1; /* enable detection of inactivity from Y axis data */
+ uint8_t inact_z:1; /* enable detection of inactivity from Z axis data */
+
+ uint8_t act_ac_dc:1; /* 0 = dc-coupled operation, 1 = ac-coupled operation */
+ uint8_t inact_ac_dc:1; /* 0 = dc-coupled operation, 1 = ac-coupled operation */
+};
+
+struct adxl345_cfg {
+ enum adxl345_power_mode power_mode;
+ uint8_t low_power_enable;
+
+ enum adxl345_accel_range accel_range;
+ enum adxl345_sample_rate sample_rate;
+
+ /* offsets used to calibrate data */
+ int8_t offset_x;
+ int8_t offset_y;
+ int8_t offset_z;
+
+ /* configuration of tap/double tap detection interrupts */
+ struct adxl345_tap_settings tap_cfg;
+
+ /* configuration of freefall detection interrupt */
+ uint8_t freefall_threshold;
+ uint8_t freefall_time;
+
+ sensor_type_t mask;
+};
+
+/* Device private data */
+struct adxl345_private_driver_data {
+ struct sensor_notify_ev_ctx notify_ctx;
+ struct sensor_read_ev_ctx read_ctx;
+ uint8_t registered_mask;
+
+ uint8_t int_num;
+ uint8_t int_route;
+ uint8_t int_enable;
+};
+
+
+struct adxl345 {
+ struct os_dev dev;
+ struct sensor sensor;
+ struct adxl345_cfg cfg;
+
+ struct adxl345_private_driver_data pdd;
+};
+
+int adxl345_set_power_mode(struct sensor_itf *itf, enum adxl345_power_mode state);
+int adxl345_get_power_mode(struct sensor_itf *itf, enum adxl345_power_mode *state);
+
+int adxl345_set_low_power_enable(struct sensor_itf *itf, uint8_t enable);
+int adxl345_get_low_power_enable(struct sensor_itf *itf, uint8_t *enable);
+
+int adxl345_set_accel_range(struct sensor_itf *itf,
+ enum adxl345_accel_range range);
+int adxl345_get_accel_range(struct sensor_itf *itf,
+ enum adxl345_accel_range *range);
+
+int adxl345_set_offsets(struct sensor_itf *itf, int8_t offset_x,
+ int8_t offset_y, int8_t offset_z);
+int adxl345_get_offsets(struct sensor_itf *itf, int8_t *offset_x,
+ int8_t *offset_y, int8_t *offset_z);
+
+int adxl345_set_tap_settings(struct sensor_itf *itf, struct adxl345_tap_settings settings);
+int adxl345_get_tap_settings(struct sensor_itf *itf, struct adxl345_tap_settings *settings);
+
+int adxl345_set_active_threshold(struct sensor_itf *itf, uint8_t threshold);
+int adxl345_get_active_threshold(struct sensor_itf *itf, uint8_t *threshold);
+
+int adxl345_set_inactive_settings(struct sensor_itf *itf, uint8_t threshold, uint8_t time);
+int adxl345_get_inactive_settings(struct sensor_itf *itf, uint8_t *threshold, uint8_t *time);
+
+int adxl345_set_freefall_settings(struct sensor_itf *itf, uint8_t threshold, uint8_t time);
+int adxl345_get_freefall_settings(struct sensor_itf *itf, uint8_t *threshold, uint8_t *time);
+
+int adxl345_set_sample_rate(struct sensor_itf *itf, enum adxl345_sample_rate rate);
+int adxl345_get_sample_rate(struct sensor_itf *itf, enum adxl345_sample_rate *rate);
+
+int adxl345_setup_interrupts(struct sensor_itf *itf, uint8_t enables, uint8_t mapping);
+int adxl345_clear_interrupts(struct sensor_itf *itf, uint8_t *int_status);
+
+int adxl345_set_act_inact_enables(struct sensor_itf *itf, struct adxl345_act_inact_enables cfg);
+int adxl345_get_act_inact_enables(struct sensor_itf *itf, struct adxl345_act_inact_enables *cfg);
+
+int adxl345_init(struct os_dev *, void *);
+int adxl345_config(struct adxl345 *, struct adxl345_cfg *);
+
+int adxl345_get_accel_data(struct sensor_itf *itf, struct sensor_accel_data *sad);
+
+#if MYNEWT_VAL(ADXL345_CLI)
+int adxl345_shell_init(void);
+#endif
+
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* __SENSOR_ADXL345_H__ */
diff --git a/hw/drivers/sensors/adxl345/pkg.yml b/hw/drivers/sensors/adxl345/pkg.yml
new file mode 100644
index 000000000..8b2ac91da
--- /dev/null
+++ b/hw/drivers/sensors/adxl345/pkg.yml
@@ -0,0 +1,40 @@
+#
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements. See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership. The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License. You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied. See the License for the
+# specific language governing permissions and limitations
+# under the License.
+#
+
+pkg.name: hw/drivers/sensors/adxl345
+pkg.description: Driver for the ADXL345 3 axis accelerometer
+pkg.author: "Apache Mynewt <de...@mynewt.apache.org>"
+pkg.homepage: "http://mynewt.apache.org/"
+pkg.keywords:
+ - accelerometer
+ - spi
+ - i2c
+ - sensor
+
+pkg.deps:
+ - "@apache-mynewt-core/hw/hal"
+ - "@apache-mynewt-core/kernel/os"
+ - "@apache-mynewt-core/hw/sensor"
+
+pkg.req_apis:
+ - stats
+ - log
+
+pkg.deps.ADXL345_CLI:
+ - "@apache-mynewt-core/util/parse"
\ No newline at end of file
diff --git a/hw/drivers/sensors/adxl345/src/adxl345.c b/hw/drivers/sensors/adxl345/src/adxl345.c
new file mode 100644
index 000000000..1f3645078
--- /dev/null
+++ b/hw/drivers/sensors/adxl345/src/adxl345.c
@@ -0,0 +1,1804 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * resarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+#include <string.h>
+#include <errno.h>
+#include <assert.h>
+#include <math.h>
+
+#include "sysinit/sysinit.h"
+#include "defs/error.h"
+#include "os/os.h"
+#include "hal/hal_i2c.h"
+#include "hal/hal_spi.h"
+#include "hal/hal_gpio.h"
+#include "sensor/sensor.h"
+#include "sensor/accel.h"
+#include "adxl345/adxl345.h"
+#include "adxl345_priv.h"
+#include "log/log.h"
+#include "stats/stats.h"
+
+static struct hal_spi_settings spi_adxl345_settings = {
+ .data_order = HAL_SPI_MSB_FIRST,
+ .data_mode = HAL_SPI_MODE3,
+ .baudrate = 4000,
+ .word_size = HAL_SPI_WORD_SIZE_8BIT,
+};
+
+
+/* Define the stats section and records */
+STATS_SECT_START(adxl345_stat_section)
+ STATS_SECT_ENTRY(read_errors)
+ STATS_SECT_ENTRY(write_errors)
+STATS_SECT_END
+
+/* Define stat names for querying */
+STATS_NAME_START(adxl345_stat_section)
+ STATS_NAME(adxl345_stat_section, read_errors)
+ STATS_NAME(adxl345_stat_section, write_errors)
+STATS_NAME_END(adxl345_stat_section)
+
+/* Global variable used to hold stats data */
+STATS_SECT_DECL(adxl345_stat_section) g_adxl345stats;
+
+#define LOG_MODULE_ADXL345 (345)
+#define ADXL345_INFO(...) LOG_INFO(&_log, LOG_MODULE_ADXL345, __VA_ARGS__)
+#define ADXL345_ERR(...) LOG_ERROR(&_log, LOG_MODULE_ADXL345, __VA_ARGS__)
+static struct log _log;
+
+#define ADXL345_NOTIFY_MASK 0x01
+#define ADXL345_READ_MASK 0x02
+
+#if MYNEWT_VAL(ADXL345_INT_ENABLE)
+static void interrupt_handler(void * arg);
+static int init_intpin(struct adxl345 * adxl345, hal_gpio_irq_handler_t handler,
+ void * arg);
+static int enable_interrupt(struct sensor * sensor, uint8_t ints_to_enable);
+static int disable_interrupt(struct sensor * sensor, uint8_t ints_to_disable);
+#endif
+
+/* Exports for the sensor API */
+static int adxl345_sensor_read(struct sensor *, sensor_type_t,
+ sensor_data_func_t, void *, uint32_t);
+static int adxl345_sensor_get_config(struct sensor *, sensor_type_t,
+ struct sensor_cfg *);
+static int adxl345_sensor_set_trigger_thresh(struct sensor * sensor,
+ sensor_type_t sensor_type,
+ struct sensor_type_traits * stt);
+static int adxl345_sensor_set_notification(struct sensor * sensor,
+ sensor_event_type_t sensor_event_type);
+static int adxl345_sensor_unset_notification(struct sensor * sensor,
+ sensor_event_type_t sensor_event_type);
+static int adxl345_sensor_handle_interrupt(struct sensor * sensor);
+static int adxl345_sensor_clear_low_thresh(struct sensor *sensor,
+ sensor_type_t type);
+static int adxl345_sensor_clear_high_thresh(struct sensor *sensor,
+ sensor_type_t type);
+
+
+static const struct sensor_driver adxl345_sensor_driver = {
+ .sd_read = adxl345_sensor_read,
+ .sd_get_config = adxl345_sensor_get_config,
+ .sd_set_trigger_thresh = adxl345_sensor_set_trigger_thresh,
+ .sd_set_notification = adxl345_sensor_set_notification,
+ .sd_unset_notification = adxl345_sensor_unset_notification,
+ .sd_handle_interrupt = adxl345_sensor_handle_interrupt,
+ .sd_clear_low_trigger_thresh = adxl345_sensor_clear_low_thresh,
+ .sd_clear_high_trigger_thresh = adxl345_sensor_clear_high_thresh
+};
+
+/**
+ * Writes a single byte to the specified register using i2c
+ *
+ * @param The sensor interface
+ * @param The register address to write to
+ * @param The value to write
+ *
+ * @return 0 on success, non-zero error on failure.
+ */
+int
+adxl345_i2c_write8(struct sensor_itf *itf, uint8_t reg, uint8_t value)
+{
+ int rc;
+ uint8_t payload[2] = { reg, value };
+
+ struct hal_i2c_master_data data_struct = {
+ .address = itf->si_addr,
+ .len = 2,
+ .buffer = payload
+ };
+
+ rc = hal_i2c_master_write(itf->si_num, &data_struct,
+ OS_TICKS_PER_SEC / 10, 1);
+
+ if (rc) {
+ ADXL345_ERR("Failed to write to 0x%02X:0x%02X with value 0x%02X\n",
+ itf->si_addr, reg, value);
+ STATS_INC(g_adxl345stats, read_errors);
+ }
+
+ return rc;
+}
+
+/**
+ * Reads a single byte from the specified register using i2c
+ *
+ * @param The sensor interface
+ * @param The register address to read from
+ * @param Pointer to where the register value should be written
+ *
+ * @return 0 on success, non-zero error on failure.
+ */
+int
+adxl345_i2c_read8(struct sensor_itf *itf, uint8_t reg, uint8_t *value)
+{
+ int rc;
+
+ struct hal_i2c_master_data data_struct = {
+ .address = itf->si_addr,
+ .len = 1,
+ .buffer = ®
+ };
+
+ /* Register write */
+ rc = hal_i2c_master_write(itf->si_num, &data_struct,
+ OS_TICKS_PER_SEC / 10, 1);
+ if (rc) {
+ ADXL345_ERR("I2C access failed at address 0x%02X\n", itf->si_addr);
+ STATS_INC(g_adxl345stats, write_errors);
+ return rc;
+ }
+
+ /* Read one byte back */
+ data_struct.buffer = value;
+ rc = hal_i2c_master_read(itf->si_num, &data_struct,
+ OS_TICKS_PER_SEC / 10, 1);
+
+ if (rc) {
+ ADXL345_ERR("Failed to read from 0x%02X:0x%02X - %02X\n", itf->si_addr, reg, rc);
+ STATS_INC(g_adxl345stats, read_errors);
+ }
+ return rc;
+}
+
+/**
+ * Read multiple bytes starting from specified register over i2c
+ *
+ * @param The sensor interface
+ * @param The register address start reading from
+ * @param Pointer to where the register value should be written
+ * @param Number of bytes to read
+ *
+ * @return 0 on success, non-zero error on failure.
+ */
+int
+adxl345_i2c_readlen(struct sensor_itf *itf, uint8_t reg, uint8_t *buffer, uint8_t len)
+{
+ int rc;
+
+ struct hal_i2c_master_data data_struct = {
+ .address = itf->si_addr,
+ .len = 1,
+ .buffer = ®
+ };
+
+ /* Register write */
+ rc = hal_i2c_master_write(itf->si_num, &data_struct,
+ OS_TICKS_PER_SEC / 10, 1);
+ if (rc) {
+ ADXL345_ERR("I2C access failed at address 0x%02X\n", itf->si_addr);
+ STATS_INC(g_adxl345stats, write_errors);
+ return rc;
+ }
+
+ /* Read data */
+ data_struct.len = len;
+ data_struct.buffer = buffer;
+ rc = hal_i2c_master_read(itf->si_num, &data_struct,
+ OS_TICKS_PER_SEC / 10, 1);
+
+ if (rc) {
+ ADXL345_ERR("Failed to read from 0x%02X:0x%02X\n", itf->si_addr, reg);
+ STATS_INC(g_adxl345stats, read_errors);
+ }
+
+ return rc;
+}
+
+/**
+ * Writes a single byte to the specified register using SPI
+ *
+ * @param The sensor interface
+ * @param The register address to write to
+ * @param The value to write
+ *
+ * @return 0 on success, non-zero error on failure.
+ */
+int
+adxl345_spi_write8(struct sensor_itf *itf, uint8_t reg, uint8_t value)
+{
+ int rc;
+
+ /* Select the device */
+ hal_gpio_write(itf->si_cs_pin, 0);
+
+ /* Send the address */
+ rc = hal_spi_tx_val(itf->si_num, reg & ~ADXL345_SPI_READ_CMD_BIT);
+ if (rc == 0xFFFF) {
+ rc = SYS_EINVAL;
+ ADXL345_ERR("SPI_%u register write failed addr:0x%02X\n",
+ itf->si_num, reg);
+ STATS_INC(g_adxl345stats, write_errors);
+ goto err;
+ }
+
+ /* Read data */
+ rc = hal_spi_tx_val(itf->si_num, value);
+ if (rc == 0xFFFF) {
+ rc = SYS_EINVAL;
+ ADXL345_ERR("SPI_%u write failed addr:0x%02X:0x%02X\n",
+ itf->si_num, reg);
+ STATS_INC(g_adxl345stats, write_errors);
+ goto err;
+ }
+
+ rc = 0;
+
+err:
+ /* De-select the device */
+ hal_gpio_write(itf->si_cs_pin, 1);
+
+ return rc;
+}
+
+
+/**
+ * Reads a single byte from the specified register using SPI
+ *
+
+ * @param The register address to read from
+ * @param Pointer to where the register value should be written
+ *
+ * @return 0 on success, non-zero error on failure.
+ */
+int
+adxl345_spi_read8(struct sensor_itf *itf, uint8_t reg, uint8_t *value)
+{
+ uint16_t retval;
+ int rc = 0;
+
+ /* Select the device */
+ hal_gpio_write(itf->si_cs_pin, 0);
+
+ /* Send the address */
+ retval = hal_spi_tx_val(itf->si_num, reg | ADXL345_SPI_READ_CMD_BIT);
+
+ if (retval == 0xFFFF) {
+ rc = SYS_EINVAL;
+ ADXL345_ERR("SPI_%u register write failed addr:0x%02X\n",
+ itf->si_num, reg);
+ STATS_INC(g_adxl345stats, read_errors);
+ goto err;
+ }
+
+ /* Read data */
+ retval = hal_spi_tx_val(itf->si_num, 0);
+ if (retval == 0xFFFF) {
+ rc = SYS_EINVAL;
+ ADXL345_ERR("SPI_%u read failed addr:0x%02X\n",
+ itf->si_num, reg);
+ STATS_INC(g_adxl345stats, read_errors);
+ goto err;
+ }
+ *value = retval;
+
+err:
+ /* De-select the device */
+ hal_gpio_write(itf->si_cs_pin, 1);
+
+ return rc;
+}
+
+/**
+ * Read multiple bytes starting from specified register over SPI
+ *
+ * @param The sensor interface
+ * @param The register address start reading from
+ * @param Pointer to where the register value should be written
+ * @param Number of bytes to read
+ *
+ * @return 0 on success, non-zero on failure
+ */
+int
+adxl345_spi_readlen(struct sensor_itf *itf, uint8_t reg, uint8_t *buffer,
+ uint8_t len)
+{
+ int i;
+ uint16_t retval;
+ int rc = 0;
+
+ /* Select the device */
+ hal_gpio_write(itf->si_cs_pin, 0);
+
+ /* Send the address */
+ retval = hal_spi_tx_val(itf->si_num, reg | ADXL345_SPI_READ_CMD_BIT
+ | ADXL345_SPI_MULTIBYTE_CMD_BIT);
+
+ if (retval == 0xFFFF) {
+ rc = SYS_EINVAL;
+ ADXL345_ERR("SPI_%u register write failed addr:0x%02X\n",
+ itf->si_num, reg);
+ STATS_INC(g_adxl345stats, read_errors);
+ goto err;
+ }
+
+ for (i = 0; i < len; i++) {
+ /* Read data */
+ retval = hal_spi_tx_val(itf->si_num, 0);
+ if (retval == 0xFFFF) {
+ rc = SYS_EINVAL;
+ ADXL345_ERR("SPI_%u read failed addr:0x%02X\n",
+ itf->si_num, reg);
+ STATS_INC(g_adxl345stats, read_errors);
+ goto err;
+ }
+ buffer[i] = retval;
+ }
+
+err:
+ /* De-select the device */
+ hal_gpio_write(itf->si_cs_pin, 1);
+
+ return rc;
+}
+
+
+/**
+ * Write byte to ADXL345 sensor over different interfaces
+ *
+ * @param The sensor interface
+ * @param The register address to write to
+ * @param The value to write
+ *
+ * @return 0 on success, non-zero on failure
+ */
+int
+adxl345_write8(struct sensor_itf *itf, uint8_t reg, uint8_t value)
+{
+ int rc;
+
+ if (itf->si_type == SENSOR_ITF_I2C) {
+ rc = adxl345_i2c_write8(itf, reg, value);
+ } else {
+ rc = adxl345_spi_write8(itf, reg, value);
+ }
+
+ return rc;
+}
+
+/**
+ * Read byte data from ADXL345 sensor over different interfaces
+ *
+ * @param The sensor interface
+ * @param The register address to read from
+ * @param Pointer to where the register value should be written
+ *
+ * @return 0 on success, non-zero on failure
+ */
+int
+adxl345_read8(struct sensor_itf *itf, uint8_t reg, uint8_t *value)
+{
+ int rc;
+
+ if (itf->si_type == SENSOR_ITF_I2C) {
+ rc = adxl345_i2c_read8(itf, reg, value);
+ } else {
+ rc = adxl345_spi_read8(itf, reg, value);
+ }
+
+ return rc;
+}
+
+/**
+ * Read multiple bytes starting from specified register over different interfaces
+ *
+ * @param The sensor interface
+ * @param The register address start reading from
+ * @param Pointer to where the register value should be written
+ * @param Number of bytes to read
+ *
+ * @return 0 on success, non-zero on failure
+ */
+int
+adxl345_readlen(struct sensor_itf *itf, uint8_t reg, uint8_t *buffer,
+ uint8_t len)
+{
+ int rc;
+
+ if (itf->si_type == SENSOR_ITF_I2C) {
+ rc = adxl345_i2c_readlen(itf, reg, buffer, len);
+ } else {
+ rc = adxl345_spi_readlen(itf, reg, buffer, len);
+ }
+
+ return rc;
+}
+
+/**
+ * Sets ADXL345 into new power mode
+ *
+ * @param The sensor interface
+ * @param Power mode to set
+ *
+ * @return 0 on success, non-zero error on failure.
+ */
+int
+adxl345_set_power_mode(struct sensor_itf *itf, enum adxl345_power_mode state)
+{
+ uint8_t reg;
+ int rc;
+
+ rc = adxl345_read8(itf, ADXL345_POWER_CTL, ®);
+ if (rc) {
+ return rc;
+ }
+
+ reg &= 0xF3;
+ reg |= ((uint8_t)state) << 2;
+
+ return adxl345_write8(itf, ADXL345_POWER_CTL, (uint8_t)reg);
+}
+
+/**
+ * Gets power mode ADXL345 is currently in
+ *
+ * @param The sensor interface
+ * @param Pointer to store current power mode
+ *
+ * @return 0 on success, non-zero error on failure.
+ */
+int
+adxl345_get_power_mode(struct sensor_itf *itf, enum adxl345_power_mode *state)
+{
+ uint8_t reg;
+ int rc;
+
+ rc = adxl345_read8(itf, ADXL345_POWER_CTL, ®);
+ if (rc) {
+ return rc;
+ }
+
+ reg &= 0xC;
+ reg >>= 2;
+
+ *state = (enum adxl345_accel_range)reg;
+
+ return 0;
+}
+
+/**
+ * Sets whether low power mode is enabled. Low power mode acheives lower
+ * power consumption at the expense of higher noise, but is only effective
+ * at sample rates between 12.5Hz and 400Hz
+ *
+ * @param The sensor interface
+ * @param Setting to use for low power mode
+ *
+ * @return 0 on success, non-zero error on failure.
+ */
+int
+adxl345_set_low_power_enable(struct sensor_itf *itf, uint8_t enable)
+{
+ uint8_t reg;
+ int rc;
+
+ rc = adxl345_read8(itf, ADXL345_BW_RATE, ®);
+ if (rc) {
+ return rc;
+ }
+
+ reg &= 0x0F;
+ if (enable) {
+ reg |= 0x10;
+ }
+
+ return adxl345_write8(itf, ADXL345_BW_RATE, (uint8_t)reg);
+}
+
+/**
+ * Gets current setting of low power mode. Low power mode acheives lower
+ * power consumption at the expense of higher noise, but is only effective
+ * at sample rates between 12.5Hz and 400Hz
+ *
+ * @param The sensor interface
+ * @param Pointer to store value
+ *
+ * @return 0 on success, non-zero error on failure.
+ */
+int
+adxl345_get_low_power_enable(struct sensor_itf *itf, uint8_t *enable)
+{
+ uint8_t reg;
+ int rc;
+
+ rc = adxl345_read8(itf, ADXL345_BW_RATE, ®);
+ if (rc) {
+ return rc;
+ }
+
+ *enable = (reg & 0x10) != 0;
+
+ return 0;
+}
+
+/**
+ * Sets the accelerometer range to specified setting
+ *
+ * @param The sensor interface
+ * @param accelerometer range to set
+ *
+ * @return 0 on success, non-zero error on failure.
+ */
+int
+adxl345_set_accel_range(struct sensor_itf *itf, enum adxl345_accel_range range)
+{
+ uint8_t reg;
+ int rc;
+
+ rc = adxl345_read8(itf, ADXL345_DATA_FORMAT, ®);
+ if (rc) {
+ return rc;
+ }
+
+ reg &= 0xFC;
+ reg |= (uint8_t)range;
+
+ return adxl345_write8(itf, ADXL345_DATA_FORMAT, (uint8_t)reg);
+}
+
+/**
+ * Gets the accelerometer range currently set
+ *
+ * @param The sensor interface
+ * @param Pointer to location to store accelerometer range
+ *
+ * @return 0 on success, non-zero error on failure.
+ */
+int
+adxl345_get_accel_range(struct sensor_itf *itf, enum adxl345_accel_range *range)
+{
+ uint8_t reg;
+ int rc;
+
+ rc = adxl345_read8(itf, ADXL345_DATA_FORMAT, ®);
+ if (rc) {
+ return rc;
+ }
+
+ *range = (enum adxl345_accel_range)(reg & 0x3);
+
+ return 0;
+}
+
+/**
+ * Sets new offsets in ADXL345
+ *
+ * @param The sensor interface
+ * @param X offset
+ * @param Y offset
+ * @param Z offset
+ *
+ * @return 0 on success, non-zero error on failure.
+ */
+int
+adxl345_set_offsets(struct sensor_itf *itf, int8_t offset_x,
+ int8_t offset_y, int8_t offset_z)
+{
+ int rc;
+
+ rc = adxl345_write8(itf, ADXL345_OFSX, offset_x);
+ if (rc){
+ return rc;
+ }
+
+ rc = adxl345_write8(itf, ADXL345_OFSY, offset_y);
+ if (rc){
+ return rc;
+ }
+
+ return adxl345_write8(itf, ADXL345_OFSZ, offset_z);
+}
+
+/**
+ * Gets the offsets currently set
+ *
+ * @param The sensor interface
+ * @param Pointer to location to store X offset
+ * @param Pointer to location to store Y offset
+ * @param Pointer to location to store Z offset
+ *
+ * @return 0 on success, non-zero error on failure.
+ */
+int
+adxl345_get_offsets(struct sensor_itf *itf, int8_t *offset_x,
+ int8_t *offset_y, int8_t *offset_z)
+{
+
+ int rc;
+
+ rc = adxl345_read8(itf, ADXL345_OFSX, (uint8_t *)offset_x);
+ if (rc){
+ return rc;
+ }
+
+ rc = adxl345_read8(itf, ADXL345_OFSY, (uint8_t *)offset_y);
+ if (rc){
+ return rc;
+ }
+
+ return adxl345_read8(itf, ADXL345_OFSZ, (uint8_t *)offset_z);
+}
+
+/**
+ * Sets new tap settings in ADXL345 sensor
+ *
+ * @param The sensor interface
+ * @param Structure of new settings
+ *
+ * @return 0 on success, non-zero error on failure.
+ */
+int
+adxl345_set_tap_settings(struct sensor_itf *itf, struct adxl345_tap_settings settings)
+{
+ int rc;
+ uint8_t enables = 0;
+
+ rc = adxl345_write8(itf, ADXL345_THRESH_TAP, settings.threshold);
+ if (rc) {
+ return rc;
+ }
+
+ rc = adxl345_write8(itf, ADXL345_DUR, settings.duration);
+ if (rc) {
+ return rc;
+ }
+
+ rc = adxl345_write8(itf, ADXL345_LATENT, settings.latency);
+ if (rc) {
+ return rc;
+ }
+
+ rc = adxl345_write8(itf, ADXL345_WINDOW, settings.window);
+ if (rc) {
+ return rc;
+ }
+
+ enables |= settings.x_enable ? (1 << 2) : 0;
+ enables |= settings.y_enable ? (1 << 1) : 0;
+ enables |= settings.z_enable ? (1 << 0) : 0;
+ enables |= settings.suppress ? (1 << 3) : 0;
+
+ return adxl345_write8(itf, ADXL345_TAP_AXES, enables);
+}
+
+/**
+ * Gets the current tap settings from ADXL345 sensor
+ *
+ * @param The sensor interface
+ * @param Ponter to structure to store settings
+ *
+ * @return 0 on success, non-zero error on failure.
+ */
+int
+adxl345_get_tap_settings(struct sensor_itf *itf, struct adxl345_tap_settings *settings)
+{
+ int rc;
+ uint8_t enables;
+
+ rc = adxl345_read8(itf, ADXL345_THRESH_TAP, &(settings->threshold));
+ if (rc) {
+ return rc;
+ }
+
+ rc = adxl345_read8(itf, ADXL345_DUR, &(settings->duration));
+ if (rc) {
+ return rc;
+ }
+
+ rc = adxl345_read8(itf, ADXL345_LATENT, &(settings->latency));
+ if (rc) {
+ return rc;
+ }
+
+ rc = adxl345_read8(itf, ADXL345_WINDOW, &(settings->window));
+ if (rc) {
+ return rc;
+ }
+
+ rc = adxl345_read8(itf, ADXL345_TAP_AXES, &enables);
+ if (rc) {
+ return rc;
+ }
+
+ settings->x_enable = (enables & (1 << 2)) != 0;
+ settings->x_enable = (enables & (1 << 1)) != 0;
+ settings->x_enable = (enables & (1 << 0)) != 0;
+ settings->suppress = (enables & (1 << 3)) != 0;
+
+ return 0;
+}
+
+/**
+ * Sets new threshold for triggering activity interrupt
+ *
+ * @param The sensor interface
+ * @param threshold value (62.5mg/LSB)
+ *
+ * @return 0 on success, non-zero error on failure.
+ */
+int
+adxl345_set_active_threshold(struct sensor_itf *itf, uint8_t threshold)
+{
+ return adxl345_write8(itf, ADXL345_THRESH_ACT, threshold);
+}
+
+/**
+ * Gets current threshold for triggering activity interrupt
+ *
+ * @param The sensor interface
+ * @param Pointer to store threshold value (62.5mg/LSB)
+ *
+ * @return 0 on success, non-zero error on failure.
+ */
+int
+adxl345_get_active_threshold(struct sensor_itf *itf, uint8_t *threshold)
+{
+ return adxl345_read8(itf, ADXL345_THRESH_ACT, threshold);
+}
+
+/**
+ * Sets new threshold and time for triggering inactivity interrupt
+ *
+ * @param The sensor interface
+ * @param threshold value (62.5mg/LSB)
+ * @param time value in seconds
+ *
+ * @return 0 on success, non-zero error on failure.
+ */
+int
+adxl345_set_inactive_settings(struct sensor_itf *itf, uint8_t threshold, uint8_t time)
+{
+ int rc;
+
+ rc = adxl345_write8(itf, ADXL345_THRESH_INACT, threshold);
+ if (rc){
+ return rc;
+ }
+
+ return adxl345_write8(itf, ADXL345_TIME_INACT, time);
+}
+
+/**
+ * Gets current threshold and time set for triggering inactivity interrupt
+ *
+ * @param The sensor interface
+ * @param Pointer to store threshold value (62.5mg/LSB)
+ * @param Ponter to store time value in seconds
+ *
+ * @return 0 on success, non-zero error on failure.
+ */
+int
+adxl345_get_inactive_settings(struct sensor_itf *itf, uint8_t *threshold, uint8_t *time)
+{
+ int rc;
+
+ rc = adxl345_read8(itf, ADXL345_THRESH_INACT, threshold);
+ if (rc){
+ return rc;
+ }
+
+ return adxl345_read8(itf, ADXL345_TIME_INACT, time);
+}
+
+/**
+ * Sets new activity/inactivity interrupt axis selections
+ *
+ * @param The sensor interface
+ * @param Structure of new configuration
+ *
+ * @return 0 on success, non-zero error on failure.
+ */
+int
+adxl345_set_act_inact_enables(struct sensor_itf *itf, struct adxl345_act_inact_enables cfg)
+{
+ uint8_t reg = 0;
+
+ reg |= cfg.inact_z ? (1 << 0) : 0;
+ reg |= cfg.inact_y ? (1 << 1) : 0;
+ reg |= cfg.inact_x ? (1 << 2) : 0;
+ reg |= cfg.inact_ac_dc ? (1 << 3) : 0;
+ reg |= cfg.act_z ? (1 << 4) : 0;
+ reg |= cfg.act_y ? (1 << 5) : 0;
+ reg |= cfg.act_x ? (1 << 6) : 0;
+ reg |= cfg.act_ac_dc ? (1 << 7) : 0;
+
+ ADXL345_ERR("act_inact = 0x%x\n", reg);
+
+ return adxl345_write8(itf, ADXL345_ACT_INACT_CTL, reg);
+
+}
+
+/**
+ * Sets new activity/inactivity interrupt axis selections
+ *
+ * @param The sensor interface
+ * @param Pointer to structure to store configuration
+ *
+ * @return 0 on success, non-zero error on failure.
+ */
+int
+adxl345_get_act_inact_enables(struct sensor_itf *itf, struct adxl345_act_inact_enables *cfg)
+{
+ int rc;
+ uint8_t reg;
+
+ rc = adxl345_read8(itf, ADXL345_ACT_INACT_CTL, ®);
+ if (rc) {
+ return rc;
+ }
+
+ cfg->inact_z = (reg & (1 << 0)) != 0;
+ cfg->inact_y = (reg & (1 << 1)) != 0;
+ cfg->inact_x = (reg & (1 << 2)) != 0;
+ cfg->inact_ac_dc = (reg & (1 << 3)) != 0;
+ cfg->act_z = (reg & (1 << 4)) != 0;
+ cfg->act_y = (reg & (1 << 5)) != 0;
+ cfg->act_x = (reg & (1 << 6)) != 0;
+ cfg->act_ac_dc = (reg & (1 << 7)) != 0;
+
+ return 0;
+}
+
+/**
+ * Sets new threshold and time for triggering freefall interrupt
+ *
+ * @param The sensor interface
+ * @param threshold value (62.5mg/LSB)
+ * @param time value in seconds
+ *
+ * @return 0 on success, non-zero error on failure.
+ */
+int
+adxl345_set_freefall_settings(struct sensor_itf *itf, uint8_t threshold, uint8_t time)
+{
+ int rc;
+
+ rc = adxl345_write8(itf, ADXL345_THRESH_FF, threshold);
+ if (rc){
+ return rc;
+ }
+
+ return adxl345_write8(itf, ADXL345_TIME_FF, time);
+}
+
+/**
+ * Gets current threshold and time set for triggering freefall interrupt
+ *
+ * @param The sensor interface
+ * @param Pointer to store threshold value (62.5mg/LSB)
+ * @param Ponter to store time value in seconds
+ *
+ * @return 0 on success, non-zero error on failure.
+ */
+int
+adxl345_get_freefall_settings(struct sensor_itf *itf, uint8_t *threshold, uint8_t *time)
+{
+ int rc;
+
+ rc = adxl345_read8(itf, ADXL345_THRESH_FF, threshold);
+ if (rc){
+ return rc;
+ }
+
+ return adxl345_read8(itf, ADXL345_TIME_FF, time);
+}
+
+/**
+ * Sets new sample rate for measurements
+ *
+ * @param The sensor interface
+ * @param sample rate value
+ *
+ * @return 0 on success, non-zero error on failure.
+ */
+int
+adxl345_set_sample_rate(struct sensor_itf *itf, enum adxl345_sample_rate rate)
+{
+ return adxl345_write8(itf, ADXL345_BW_RATE, (uint8_t) rate);
+}
+
+/**
+ * Gets current sample rate settings
+ *
+ * @param The sensor interface
+ * @param Pointer to store rate value
+ *
+ * @return 0 on success, non-zero error on failure.
+ */
+int
+adxl345_get_sample_rate(struct sensor_itf *itf, enum adxl345_sample_rate *rate)
+{
+ int rc;
+ uint8_t reg;
+
+ rc = adxl345_read8(itf, ADXL345_BW_RATE, ®);
+ if (rc){
+ return rc;
+ }
+
+ *rate = (enum adxl345_sample_rate)(reg & 0xF);
+ return 0;
+}
+
+/**
+ * Configures which interrupts are enabled and which interrupt pins they are
+ * mapped to.
+ *
+ * @param The sensor interface
+ * @param which interrupts are enabled
+ * @param which interrupts are mapped to which pin
+ *
+ * @return 0 on success, non-zero error on failure
+ */
+int
+adxl345_setup_interrupts(struct sensor_itf *itf, uint8_t enables, uint8_t mapping)
+{
+ int rc;
+
+ rc = adxl345_write8(itf, ADXL345_INT_MAP, mapping);
+ if (rc) {
+ return rc;
+ }
+
+ return adxl345_write8(itf, ADXL345_INT_ENABLE, enables);
+}
+
+/**
+ * Clears interrupts (except DATA_READY, Watermark & Overrun which need data to
+ * be read to clear). Provides status as output to identify which interrupts have
+ * triggered.
+ *
+ * @param The sensor interface
+ * @param Pointer to store interrupt status
+ *
+ * @return 0 on success, non-zero error on failure
+ */
+int adxl345_clear_interrupts(struct sensor_itf *itf, uint8_t *int_status)
+{
+ return adxl345_read8(itf, ADXL345_INT_SOURCE, int_status);
+}
+
+/**
+ * Expects to be called back through os_dev_create().
+ *
+ * @param The device object associated with this accellerometer
+ * @param Argument passed to OS device init, unused
+ *
+ * @return 0 on success, non-zero error on failure.
+ */
+int
+adxl345_init(struct os_dev *dev, void *arg)
+{
+ struct adxl345 *adxl;
+ struct sensor *sensor;
+ int rc;
+
+ if (!arg || !dev) {
+ return SYS_ENODEV;
+ }
+
+ adxl = (struct adxl345 *) dev;
+
+ adxl->cfg.mask = SENSOR_TYPE_ALL;
+
+ log_register(dev->od_name, &_log, &log_console_handler, NULL, LOG_SYSLEVEL);
+
+ sensor = &adxl->sensor;
+
+ /* Initialise the stats entry */
+ rc = stats_init(
+ STATS_HDR(g_adxl345stats),
+ STATS_SIZE_INIT_PARMS(g_adxl345stats, STATS_SIZE_32),
+ STATS_NAME_INIT_PARMS(adxl345_stat_section));
+ SYSINIT_PANIC_ASSERT(rc == 0);
+ /* Register the entry with the stats registry */
+ rc = stats_register(dev->od_name, STATS_HDR(g_adxl345stats));
+ SYSINIT_PANIC_ASSERT(rc == 0);
+
+ rc = sensor_init(sensor, dev);
+ if (rc) {
+ return rc;
+ }
+
+ /* Add the accelerometer/gyroscope driver */
+ rc = sensor_set_driver(sensor, SENSOR_TYPE_ACCELEROMETER,
+ (struct sensor_driver *) &adxl345_sensor_driver);
+ if (rc) {
+ return rc;
+ }
+
+ rc = sensor_set_interface(sensor, arg);
+ if (rc) {
+ return rc;
+ }
+
+ rc = sensor_mgr_register(sensor);
+ if (rc) {
+ return rc;
+ }
+
+ if (sensor->s_itf.si_type == SENSOR_ITF_SPI) {
+ rc = hal_spi_config(sensor->s_itf.si_num, &spi_adxl345_settings);
+ if (rc == EINVAL) {
+ return rc;
+ }
+
+ rc = hal_spi_enable(sensor->s_itf.si_num);
+ if (rc) {
+ return rc;
+ }
+
+ rc = hal_gpio_init_out(sensor->s_itf.si_cs_pin, 1);
+ if (rc) {
+ return rc;
+ }
+ }
+
+#if MYNEWT_VAL(ADXL345_INT_ENABLE)
+ adxl->pdd.read_ctx.srec_sensor = sensor;
+ adxl->pdd.notify_ctx.snec_sensor = sensor;
+
+ rc = init_intpin(adxl, interrupt_handler, sensor);
+ if (rc != 0) {
+ return rc;
+ }
+#endif
+
+
+ return 0;
+}
+
+/**
+ * Configure ADXL345 sensor
+ *
+ * @param Sensor device ADXL345 structure
+ * @param Sensor device ADXL345 config
+ *
+ * @return 0 on success, non-zero on failure
+ */
+int
+adxl345_config(struct adxl345 *dev, struct adxl345_cfg *cfg)
+{
+ int rc;
+ struct sensor_itf *itf;
+
+ itf = SENSOR_GET_ITF(&(dev->sensor));
+
+ /* Check device id is correct */
+ uint8_t val = 0;
+ rc = adxl345_read8(itf, ADXL345_DEVID, &val);
+ if (rc) {
+ return rc;
+ }
+ if (val != ADXL345_DEVID_VAL) {
+ return SYS_EINVAL;
+ }
+
+ rc = adxl345_read8(itf, ADXL345_DATA_FORMAT, &val);
+ if (rc) {
+ return rc;
+ }
+
+ /* Set accelerometer into full resolution */
+ val |= 0x08;
+
+ /* Set interupt pin polarity to match local pin setting */
+ if (dev->sensor.s_itf.si_ints[dev->pdd.int_num].active) {
+ val &= ~0x20;
+ } else {
+ val |= 0x20;
+ }
+
+ rc = adxl345_write8(itf, ADXL345_DATA_FORMAT, val);
+ if (rc) {
+ return rc;
+ }
+
+ /* Setup range as per config */
+ rc = adxl345_set_accel_range(itf, cfg->accel_range);
+ if (rc) {
+ return rc;
+ }
+ dev->cfg.accel_range = cfg->accel_range;
+
+ /* setup sample rate */
+ rc = adxl345_set_sample_rate(itf, cfg->sample_rate);
+ if (rc) {
+ return rc;
+ }
+ dev->cfg.sample_rate = cfg->sample_rate;
+
+ /* setup data offsets */
+ rc = adxl345_set_offsets(itf, cfg->offset_x, cfg->offset_y, cfg->offset_z);
+ if (rc) {
+ return rc;
+ }
+ dev->cfg.offset_x = cfg->offset_x;
+ dev->cfg.offset_y = cfg->offset_y;
+ dev->cfg.offset_z = cfg->offset_z;
+
+ /* setup tap detection settings */
+ rc = adxl345_set_tap_settings(itf, cfg->tap_cfg);
+ if (rc) {
+ return rc;
+ }
+ dev->cfg.tap_cfg = cfg->tap_cfg;
+
+ /* setup activity detection settings */
+ rc = adxl345_set_active_threshold(itf, 0xFF);
+ if (rc) {
+ return rc;
+ }
+
+ rc = adxl345_set_inactive_settings(itf, 0, 0);
+ if (rc) {
+ return rc;
+ }
+
+ /* setup freefall settings */
+ rc = adxl345_set_freefall_settings(itf, cfg->freefall_threshold, cfg->freefall_time);
+ if (rc) {
+ return rc;
+ }
+ dev->cfg.freefall_threshold = cfg->freefall_threshold;
+ dev->cfg.freefall_time = cfg->freefall_time;
+
+ /* setup low power mode */
+ rc = adxl345_set_low_power_enable(itf, cfg->low_power_enable);
+ if (rc) {
+ return rc;
+ }
+ dev->cfg.low_power_enable = cfg->low_power_enable;
+
+ /* setup default interrupt config */
+ rc = adxl345_setup_interrupts(itf, 0, 0);
+ if (rc) {
+ return rc;
+ }
+
+ rc = adxl345_clear_interrupts(itf, &val);
+ if (rc) {
+ return rc;
+ }
+
+ /* Setup current power mode */
+ rc = adxl345_set_power_mode(itf, cfg->power_mode);
+ if (rc) {
+ return rc;
+ }
+ dev->cfg.power_mode = cfg->power_mode;
+
+
+ rc = sensor_set_type_mask(&(dev->sensor), cfg->mask);
+ if (rc) {
+ return rc;
+ }
+
+ dev->cfg.mask = cfg->mask;
+
+ return 0;
+}
+
+static float adxl345_convert_reg_to_ms2(int16_t val, float lsb_mg)
+{
+ float tmp = val * lsb_mg * 0.001; /* convert to g */
+ return tmp * STANDARD_ACCEL_GRAVITY; /* convert to ms2 */
+}
+
+static uint8_t adxl345_convert_ms2_to_reg(float ms2, float lsb_mg)
+{
+ float tmp = (ms2 * 1000) / STANDARD_ACCEL_GRAVITY; /* convert to mg */
+ tmp /= lsb_mg; /* convert to reg format */
+ return (uint8_t) tmp;
+}
+
+/**
+ * Reads Accelerometer data from ADXL345 sensor
+ *
+ * @param Pointer to sensor interface
+ * @param Pointer to structure to store result
+ *
+ * @return 0 on success, non-zero on failure
+ */
+int
+adxl345_get_accel_data(struct sensor_itf *itf, struct sensor_accel_data *sad)
+{
+ int rc;
+
+ uint8_t payload[6];
+ int16_t x,y,z;
+
+ float lsb_mg = 4; /* lsb is always 4mg when device is set to full resolution */
+
+ /* Get a new accelerometer sample */
+ rc = adxl345_readlen(itf, ADXL345_DATAX0, payload, 6);
+ if (rc) {
+ return rc;
+ }
+
+ x = (((int16_t)payload[1]) << 8) | payload[0];
+ y = (((int16_t)payload[3]) << 8) | payload[2];
+ z = (((int16_t)payload[5]) << 8) | payload[4];
+
+ /* calculate MS*2 values to provide to data_func */
+ sad->sad_x = adxl345_convert_reg_to_ms2(x, lsb_mg);
+ sad->sad_x_is_valid = 1;
+ sad->sad_y = adxl345_convert_reg_to_ms2(y, lsb_mg);
+ sad->sad_y_is_valid = 1;
+ sad->sad_z = adxl345_convert_reg_to_ms2(z, lsb_mg);
+ sad->sad_z_is_valid = 1;
+
+ return 0;
+}
+
+/**
+ * Read Accelerometer data from ADXL345 sensor
+ *
+ * @param Pointer to sensor structure
+ * @param Type of sensor data to read
+ * @param Pointer to function to call to output data
+ * @param Pointer to data to pass to data_func
+ * @param timeout value
+ *
+ * @return 0 on success, non-zero on failure
+ */
+static int
+adxl345_sensor_read(struct sensor *sensor, sensor_type_t type,
+ sensor_data_func_t data_func, void *data_arg, uint32_t timeout)
+{
+ (void)timeout;
+ int rc;
+
+ struct sensor_itf *itf;
+ struct sensor_accel_data sad;
+
+ /* If the read isn't looking for accel don't do anything. */
+ if (!(type & SENSOR_TYPE_ACCELEROMETER)) {
+ return SYS_EINVAL;
+ }
+
+ itf = SENSOR_GET_ITF(sensor);
+
+ rc = adxl345_get_accel_data(itf, &sad);
+ if (rc) {
+ return rc;
+ }
+
+ /* output data using data_func */
+ rc = data_func(sensor, data_arg, &sad, SENSOR_TYPE_ACCELEROMETER);
+ if (rc) {
+ return rc;
+ }
+
+ return 0;
+}
+
+static int
+adxl345_sensor_get_config(struct sensor *sensor, sensor_type_t type,
+ struct sensor_cfg *cfg)
+{
+ /* If the read isn't looking for accel, don't do anything. */
+ if (!(type & SENSOR_TYPE_ACCELEROMETER)) {
+ return SYS_EINVAL;
+ }
+
+ cfg->sc_valtype = SENSOR_VALUE_TYPE_FLOAT_TRIPLET;
+
+ return 0;
+}
+
+#if MYNEWT_VAL(ADXL345_INT_ENABLE)
+static void
+interrupt_handler(void * arg)
+{
+ struct sensor *sensor = arg;
+ sensor_mgr_put_interrupt_evt(sensor);
+}
+
+/**
+ * Initialises the local interrupt pin
+ *
+ * @param Pointer to device structure
+ * @param interrupt handler to setup
+ * @param Pointer to data to pass to irq
+ *
+ * @return 0 on success, non-zero on failure
+ */
+static int
+init_intpin(struct adxl345 * adxl345, hal_gpio_irq_handler_t handler,
+ void * arg)
+{
+ struct adxl345_private_driver_data *pdd = &adxl345->pdd;
+ hal_gpio_irq_trig_t trig;
+ int pin = -1;
+ int rc;
+ int i;
+
+ for (i = 0; i < MYNEWT_VAL(SENSOR_MAX_INTERRUPTS_PINS); i++){
+ pin = adxl345->sensor.s_itf.si_ints[i].host_pin;
+ if (pin >= 0) {
+ break;
+ }
+ }
+
+ if (pin < 0) {
+ ADXL345_ERR("Interrupt pin not configured\n");
+ return SYS_EINVAL;
+ }
+
+ pdd->int_num = i;
+ if (adxl345->sensor.s_itf.si_ints[pdd->int_num].active) {
+ trig = HAL_GPIO_TRIG_RISING;
+ } else {
+ trig = HAL_GPIO_TRIG_FALLING;
+ }
+
+ if (adxl345->sensor.s_itf.si_ints[pdd->int_num].device_pin == 1) {
+ pdd->int_route = 0x00;
+ } else if (adxl345->sensor.s_itf.si_ints[pdd->int_num].device_pin == 2) {
+ pdd->int_route = 0xFF;
+ } else {
+ ADXL345_ERR("Route not configured\n");
+ return SYS_EINVAL;
+ }
+
+ rc = hal_gpio_irq_init(pin,
+ handler,
+ arg,
+ trig,
+ HAL_GPIO_PULL_NONE);
+ if (rc != 0) {
+ ADXL345_ERR("Failed to initialise interrupt pin %d\n", pin);
+ return rc;
+ }
+
+ return 0;
+}
+
+/**
+ * Enables an interrupt in ADXL345 device
+ *
+ * @param Pointer to sensor structure
+ * @param which interrupt(s) to enable
+ *
+ * @return 0 on success, non-zero on failure
+ */
+static int
+enable_interrupt(struct sensor * sensor, uint8_t ints_to_enable)
+{
+ struct adxl345_private_driver_data *pdd;
+ struct adxl345 *adxl345;
+ struct sensor_itf *itf;
+
+ if (ints_to_enable == 0) {
+ return SYS_EINVAL;
+ }
+
+ adxl345 = (struct adxl345 *)SENSOR_GET_DEVICE(sensor);
+ itf = SENSOR_GET_ITF(sensor);
+ pdd = &adxl345->pdd;
+
+ /* if no interrupts are currently in use enable int pin */
+ if (pdd->int_enable == 0) {
+ hal_gpio_irq_enable(adxl345->sensor.s_itf.si_ints[pdd->int_num].host_pin);
+ }
+
+ /* update which interrupts are enabled */
+ pdd->int_enable |= ints_to_enable;
+
+ /* enable interrupt in device */
+ return adxl345_setup_interrupts(itf, pdd->int_enable, pdd->int_route);
+}
+
+/**
+ * Disables an interrupt in ADXL345 device
+ *
+ * @param Pointer to sensor structure
+ * @param which interrupt(s) to disable
+ *
+ * @return 0 on success, non-zero on failure
+ */
+static int
+disable_interrupt(struct sensor * sensor, uint8_t ints_to_disable)
+{
+ struct adxl345_private_driver_data *pdd;
+ struct adxl345 *adxl345;
+ struct sensor_itf *itf;
+
+ if (ints_to_disable == 0) {
+ return SYS_EINVAL;
+ }
+
+ adxl345 = (struct adxl345 *)SENSOR_GET_DEVICE(sensor);
+ itf = SENSOR_GET_ITF(sensor);
+ pdd = &adxl345->pdd;
+
+ /* update which interrupts are enabled */
+ pdd->int_enable &= ~ints_to_disable;
+
+ /* if no interrupts are now in use disable int pin */
+ if (pdd->int_enable == 0) {
+ hal_gpio_irq_disable(adxl345->sensor.s_itf.si_ints[pdd->int_num].host_pin);
+ }
+
+ /* update interrupt setup in device */
+ return adxl345_setup_interrupts(itf, pdd->int_enable, pdd->int_route);
+}
+#endif
+
+/**
+ * Handles and interrupt, firing correct events
+ *
+ * @param Pointer to sensor structure
+ *
+ * @return 0 on success, non-zero on failure
+ */
+static int
+adxl345_sensor_handle_interrupt(struct sensor * sensor)
+{
+#if MYNEWT_VAL(ADXL345_INT_ENABLE)
+ struct adxl345 * adxl345;
+ struct adxl345_private_driver_data *pdd;
+ struct sensor_itf *itf;
+ uint8_t int_status;
+
+ int rc;
+
+ adxl345 = (struct adxl345 *)SENSOR_GET_DEVICE(sensor);
+ itf = SENSOR_GET_ITF(sensor);
+
+ pdd = &adxl345->pdd;
+
+ rc = adxl345_clear_interrupts(itf, &int_status);
+ if (rc != 0) {
+ ADXL345_ERR("Cound not read int status err=0x%02x\n", rc);
+ return rc;
+ }
+
+ if ((pdd->registered_mask & ADXL345_NOTIFY_MASK) &&
+ ((int_status & ADXL345_INT_SINGLE_TAP_BIT) ||
+ (int_status & ADXL345_INT_DOUBLE_TAP_BIT))) {
+ sensor_mgr_put_notify_evt(&pdd->notify_ctx);
+ }
+
+ if ((pdd->registered_mask & ADXL345_READ_MASK) &&
+ ((int_status & ADXL345_INT_ACTIVITY_BIT) ||
+ (int_status & ADXL345_INT_INACTIVITY_BIT))) {
+ ADXL345_ERR("READ EVT 0x%02x\n", int_status);
+ sensor_mgr_put_read_evt(&pdd->read_ctx);
+ }
+
+ return 0;
+#else
+ return SYS_ENODEV;
+#endif
+}
+
+/**
+ * Sets up trigger thresholds and enables interrupts
+ *
+ * @param Pointer to sensor structure
+ * @param type of sensor
+ * @param threshold settings to configure
+ *
+ * @return 0 on success, non-zero on failure
+ */
+static int
+adxl345_sensor_set_trigger_thresh(struct sensor * sensor,
+ sensor_type_t sensor_type,
+ struct sensor_type_traits * stt)
+{
+#if MYNEWT_VAL(ADXL345_INT_ENABLE)
+ struct adxl345 * adxl345;
+ struct sensor_itf *itf;
+ int rc;
+ const struct sensor_accel_data * low_thresh;
+ const struct sensor_accel_data * high_thresh;
+ uint8_t ints_to_enable = 0;
+ float thresh;
+ struct adxl345_private_driver_data *pdd;
+ struct adxl345_act_inact_enables axis_enables = { 0 };
+
+ if (sensor_type != SENSOR_TYPE_ACCELEROMETER) {
+ return SYS_EINVAL;
+ }
+
+ adxl345 = (struct adxl345 *)SENSOR_GET_DEVICE(sensor);
+ itf = SENSOR_GET_ITF(sensor);
+ pdd = &adxl345->pdd;
+
+ low_thresh = stt->stt_low_thresh.sad;
+ high_thresh = stt->stt_high_thresh.sad;
+
+ if (low_thresh->sad_x_is_valid |
+ low_thresh->sad_y_is_valid |
+ low_thresh->sad_z_is_valid) {
+ thresh = INFINITY;
+
+ if (low_thresh->sad_x_is_valid) {
+ axis_enables.inact_x = 1;
+ if (thresh > low_thresh->sad_x) {
+ thresh = low_thresh->sad_x;
+ }
+ }
+ if (low_thresh->sad_y_is_valid) {
+ axis_enables.inact_y = 1;
+ if (thresh > low_thresh->sad_y) {
+ thresh = low_thresh->sad_y;
+ }
+ }
+ if (low_thresh->sad_z_is_valid) {
+ axis_enables.inact_z = 1;
+ if (thresh > low_thresh->sad_z) {
+ thresh = low_thresh->sad_z;
+ }
+ }
+
+ rc = adxl345_set_inactive_settings(itf,
+ adxl345_convert_ms2_to_reg(thresh, 62.5), 2);
+
+ if (rc) {
+ return rc;
+ }
+
+ ints_to_enable |= ADXL345_INT_INACTIVITY_BIT;
+ }
+
+ if (high_thresh->sad_x_is_valid |
+ high_thresh->sad_y_is_valid |
+ high_thresh->sad_z_is_valid) {
+ thresh = 0.0;
+
+ if (high_thresh->sad_x_is_valid) {
+ axis_enables.act_x = 1;
+ if (thresh < high_thresh->sad_x) {
+ thresh = high_thresh->sad_x;
+ }
+ }
+ if (high_thresh->sad_y_is_valid) {
+ axis_enables.act_y = 1;
+ if (thresh < high_thresh->sad_y) {
+ thresh = high_thresh->sad_y;
+ }
+ }
+ if (high_thresh->sad_z_is_valid) {
+ axis_enables.act_z = 1;
+ if (thresh < high_thresh->sad_z) {
+ thresh = high_thresh->sad_z;
+ }
+ }
+
+ rc = adxl345_set_active_threshold(itf,
+ adxl345_convert_ms2_to_reg(thresh, 62.5));
+ if (rc) {
+ return rc;
+ }
+
+ ints_to_enable |= ADXL345_INT_ACTIVITY_BIT;
+
+ }
+
+ rc = enable_interrupt(sensor, ints_to_enable);
+ if (rc) {
+ return rc;
+ }
+
+ rc = adxl345_set_act_inact_enables(itf, axis_enables);
+ if (rc) {
+ return rc;
+ }
+
+ pdd->read_ctx.srec_type |= sensor_type;
+ pdd->registered_mask |= ADXL345_READ_MASK;
+
+ return 0;
+#else
+ return SYS_ENODEV;
+#endif
+}
+
+/**
+ * Disable the low threshold interrupt
+ *
+ * @param ptr to sensor
+ * @param the Sensor type
+ *
+ * @return 0 on success, non-zero on failure
+ */
+static int
+adxl345_sensor_clear_low_thresh(struct sensor *sensor,
+ sensor_type_t type)
+{
+ struct adxl345 *adxl345;
+ uint8_t ints_to_disable = ADXL345_INT_INACTIVITY_BIT;
+
+ if (type != SENSOR_TYPE_ACCELEROMETER) {
+ return SYS_EINVAL;
+ }
+
+ adxl345 = (struct adxl345 *)SENSOR_GET_DEVICE(sensor);
+
+ /* if neither high or low threshs are now set disable read mask */
+ if((adxl345->pdd.int_enable & ADXL345_INT_ACTIVITY_BIT) == 0) {
+ adxl345->pdd.read_ctx.srec_type &= ~type;
+ adxl345->pdd.registered_mask &= ~ADXL345_READ_MASK;
+ }
+
+ return disable_interrupt(sensor, ints_to_disable);
+}
+
+
+/**
+ * Disable the high threshold interrupt
+ *
+ * @param ptr to sensor
+ * @param the Sensor type
+ *
+ * @return 0 on success, non-zero on failure
+ */
+static int
+adxl345_sensor_clear_high_thresh(struct sensor *sensor,
+ sensor_type_t type)
+{
+ struct adxl345 *adxl345;
+ uint8_t ints_to_disable = ADXL345_INT_ACTIVITY_BIT;
+
+ if (type != SENSOR_TYPE_ACCELEROMETER) {
+ return SYS_EINVAL;
+ }
+
+ adxl345 = (struct adxl345 *)SENSOR_GET_DEVICE(sensor);
+
+ /* if neither high or low threshs are now set disable read mask */
+ if((adxl345->pdd.int_enable & ADXL345_INT_INACTIVITY_BIT) == 0) {
+ adxl345->pdd.read_ctx.srec_type &= ~type;
+ adxl345->pdd.registered_mask &= ~ADXL345_READ_MASK;
+ }
+
+ return disable_interrupt(sensor, ints_to_disable);
+}
+
+/**
+ * Enables notifications on tap or double tap events
+ *
+ * @param Pointer to sensor structure
+ * @param which event to get notifications for
+ *
+ * @return 0 on success, non-zero on failure
+ */
+static int
+adxl345_sensor_set_notification(struct sensor * sensor,
+ sensor_event_type_t sensor_event_type)
+{
+#if MYNEWT_VAL(ADXL345_INT_ENABLE)
+ struct adxl345 * adxl345;
+ uint8_t ints_to_enable = 0;
+ struct adxl345_private_driver_data *pdd;
+ int rc;
+
+ ADXL345_ERR("Enabling notifications\n");
+
+ if ((sensor_event_type & ~(SENSOR_EVENT_TYPE_DOUBLE_TAP |
+ SENSOR_EVENT_TYPE_SINGLE_TAP)) != 0) {
+ return SYS_EINVAL;
+ }
+
+ /*XXX for now we do not support registering for both events */
+ if (sensor_event_type == (SENSOR_EVENT_TYPE_DOUBLE_TAP |
+ SENSOR_EVENT_TYPE_SINGLE_TAP)) {
+ return SYS_EINVAL;
+ }
+
+ adxl345 = (struct adxl345 *)SENSOR_GET_DEVICE(sensor);
+ pdd = &adxl345->pdd;
+
+ if (pdd->registered_mask & ADXL345_NOTIFY_MASK) {
+ return SYS_EBUSY;
+ }
+
+ /* Enable tap event*/
+ if(sensor_event_type == SENSOR_EVENT_TYPE_DOUBLE_TAP) {
+ ints_to_enable |= ADXL345_INT_DOUBLE_TAP_BIT;
+ }
+ if(sensor_event_type == SENSOR_EVENT_TYPE_SINGLE_TAP) {
+ ints_to_enable |= ADXL345_INT_SINGLE_TAP_BIT;
+ }
+
+ rc = enable_interrupt(sensor, ints_to_enable);
+ if (rc) {
+ return rc;
+ }
+
+ pdd->notify_ctx.snec_evtype |= sensor_event_type;
+ pdd->registered_mask |= ADXL345_NOTIFY_MASK;
+
+ ADXL345_ERR("Enabled notifications\n");
+
+ return 0;
+#else
+ return SYS_ENODEV;
+#endif
+}
+
+
+/**
+ * Disables notifications on tap or double tap events
+ *
+ * @param Pointer to sensor structure
+ * @param which event to get notifications for
+ *
+ * @return 0 on success, non-zero on failure
+ */
+static int
+adxl345_sensor_unset_notification(struct sensor * sensor,
+ sensor_event_type_t sensor_event_type)
+{
+#if MYNEWT_VAL(ADXL345_INT_ENABLE)
+ struct adxl345 * adxl345;
+ uint8_t ints_to_disable = 0;
+
+ if ((sensor_event_type & ~(SENSOR_EVENT_TYPE_DOUBLE_TAP |
+ SENSOR_EVENT_TYPE_SINGLE_TAP)) != 0) {
+ return SYS_EINVAL;
+ }
+
+ /*XXX for now we do not support registering for both events */
+ if (sensor_event_type == (SENSOR_EVENT_TYPE_DOUBLE_TAP |
+ SENSOR_EVENT_TYPE_SINGLE_TAP)) {
+ return SYS_EINVAL;
+ }
+
+ adxl345 = (struct adxl345 *)SENSOR_GET_DEVICE(sensor);
+
+ adxl345->pdd.notify_ctx.snec_evtype &= ~sensor_event_type;
+ adxl345->pdd.registered_mask &= ~ADXL345_NOTIFY_MASK;
+
+ ints_to_disable |= ADXL345_INT_SINGLE_TAP_BIT;
+ ints_to_disable |= ADXL345_INT_DOUBLE_TAP_BIT;
+
+ return disable_interrupt(sensor, ints_to_disable);
+
+#else
+ return SYS_ENODEV;
+#endif
+}
+
+
+
diff --git a/hw/drivers/sensors/adxl345/src/adxl345_priv.h b/hw/drivers/sensors/adxl345/src/adxl345_priv.h
new file mode 100644
index 000000000..9fb853e12
--- /dev/null
+++ b/hw/drivers/sensors/adxl345/src/adxl345_priv.h
@@ -0,0 +1,83 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+#ifndef __ADXL345_PRIV_H__
+#define __ADXL345_PRIV_H__
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+enum adxl345_registers {
+ ADXL345_DEVID = 0x00, /* R */
+ ADXL345_THRESH_TAP = 0x1D, /* R/W */
+ ADXL345_OFSX = 0x1E, /* R/W */
+ ADXL345_OFSY = 0x1F, /* R/W */
+ ADXL345_OFSZ = 0x20, /* R/W */
+ ADXL345_DUR = 0x21, /* R/W */
+ ADXL345_LATENT = 0x22, /* R/W */
+ ADXL345_WINDOW = 0x23, /* R/W */
+ ADXL345_THRESH_ACT = 0x24, /* R/W */
+ ADXL345_THRESH_INACT = 0x25, /* R/W */
+ ADXL345_TIME_INACT = 0x26, /* R/W */
+ ADXL345_ACT_INACT_CTL = 0x27, /* R/W */
+ ADXL345_THRESH_FF = 0x28, /* R/W */
+ ADXL345_TIME_FF = 0x29, /* R/W */
+ ADXL345_TAP_AXES = 0x2A, /* R/W */
+ ADXL345_ACT_TAP_STATUS = 0x2B, /* R */
+ ADXL345_BW_RATE = 0x2C, /* R/W */
+ ADXL345_POWER_CTL = 0x2D, /* R/W */
+ ADXL345_INT_ENABLE = 0x2E, /* R/W */
+ ADXL345_INT_MAP = 0x2F, /* R/W */
+ ADXL345_INT_SOURCE = 0x30, /* R */
+ ADXL345_DATA_FORMAT = 0x31, /* R/W */
+ ADXL345_DATAX0 = 0x32, /* R */
+ ADXL345_DATAX1 = 0x33, /* R */
+ ADXL345_DATAY0 = 0x34, /* R */
+ ADXL345_DATAY1 = 0x35, /* R */
+ ADXL345_DATAZ0 = 0x36, /* R */
+ ADXL345_DATAZ1 = 0x37, /* R */
+ ADXL345_FIFO_CTL = 0x38, /* R/W */
+ ADXL345_FIFO_STATUS = 0x39, /* R */
+};
+
+
+#define ADXL345_DEVID_VAL (0xE5)
+
+#define ADXL345_SPI_READ_CMD_BIT (0x80)
+#define ADXL345_SPI_MULTIBYTE_CMD_BIT (0x40)
+
+
+int adxl345_i2c_write8(struct sensor_itf *itf, uint8_t reg, uint8_t value);
+int adxl345_i2c_read8(struct sensor_itf *itf, uint8_t reg, uint8_t *value);
+int adxl345_i2c_readlen(struct sensor_itf *itf, uint8_t reg, uint8_t *buffer, uint8_t len);
+
+int adxl345_spi_write8(struct sensor_itf *itf, uint8_t reg, uint8_t value);
+int adxl345_spi_read8(struct sensor_itf *itf, uint8_t reg, uint8_t *value);
+int adxl345_spi_readlen(struct sensor_itf *itf, uint8_t reg, uint8_t *buffer, uint8_t len);
+
+int adxl345_write8(struct sensor_itf *itf, uint8_t reg, uint8_t value);
+int adxl345_read8(struct sensor_itf *itf, uint8_t reg, uint8_t *value);
+int adxl345_readlen(struct sensor_itf *itf, uint8_t reg, uint8_t *buffer, uint8_t len);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* __ADXL345_PRIV_H__ */
diff --git a/hw/drivers/sensors/adxl345/src/adxl345_shell.c b/hw/drivers/sensors/adxl345/src/adxl345_shell.c
new file mode 100644
index 000000000..ff489aeed
--- /dev/null
+++ b/hw/drivers/sensors/adxl345/src/adxl345_shell.c
@@ -0,0 +1,249 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+#include <string.h>
+#include <errno.h>
+#include "sysinit/sysinit.h"
+#include "console/console.h"
+#include "shell/shell.h"
+#include "sensor/accel.h"
+#include "adxl345/adxl345.h"
+#include "adxl345_priv.h"
+#include "parse/parse.h"
+
+#if MYNEWT_VAL(ADXL345_CLI)
+
+static int adxl345_shell_cmd(int argc, char **argv);
+
+static struct shell_cmd adxl345_shell_cmd_struct = {
+ .sc_cmd = "adxl345",
+ .sc_cmd_func = adxl345_shell_cmd
+};
+
+static struct sensor_itf g_sensor_itf = {
+ .si_type = MYNEWT_VAL(ADXL345_SHELL_ITF_TYPE),
+ .si_num = MYNEWT_VAL(ADXL345_SHELL_ITF_NUM),
+ .si_cs_pin = MYNEWT_VAL(ADXL345_SHELL_CSPIN),
+ .si_addr = MYNEWT_VAL(ADXL345_SHELL_ITF_ADDR)
+};
+
+static int
+adxl345_shell_err_too_many_args(char *cmd_name)
+{
+ console_printf("Error: too many arguments for command \"%s\"\n",
+ cmd_name);
+ return EINVAL;
+}
+
+static int
+adxl345_shell_err_unknown_arg(char *cmd_name)
+{
+ console_printf("Error: unknown argument \"%s\"\n",
+ cmd_name);
+ return EINVAL;
+}
+
+static int
+adxl345_shell_err_invalid_arg(char *cmd_name)
+{
+ console_printf("Error: invalid argument \"%s\"\n",
+ cmd_name);
+ return EINVAL;
+}
+
+static int
+adxl345_shell_help(void)
+{
+ console_printf("%s cmd [flags...]\n", adxl345_shell_cmd_struct.sc_cmd);
+ console_printf("cmd:\n");
+ console_printf("\tr [n_samples]\n");
+ console_printf("\tchipid\n");
+ console_printf("\tdump\n");
+
+ return 0;
+}
+
+static int
+adxl345_shell_cmd_read_chipid(int argc, char **argv)
+{
+ int rc;
+ uint8_t chipid;
+
+ rc = adxl345_read8(&g_sensor_itf, ADXL345_DEVID, &chipid);
+ if (rc) {
+ goto err;
+ }
+
+ console_printf("CHIP_ID:0x%02X\n", chipid);
+
+ return 0;
+err:
+ return rc;
+}
+
+static int
+adxl345_shell_cmd_read(int argc, char **argv)
+{
+ uint16_t samples = 1;
+ uint16_t val;
+ int rc;
+ struct sensor_accel_data sample;
+ char tmpstr[13];
+
+ if (argc > 3) {
+ return adxl345_shell_err_too_many_args(argv[1]);
+ }
+
+ /* Check if more than one sample requested */
+ if (argc == 3) {
+ val = parse_ll_bounds(argv[2], 1, UINT16_MAX, &rc);
+ if (rc) {
+ return adxl345_shell_err_invalid_arg(argv[2]);
+ }
+ samples = val;
+ }
+
+ while(samples--) {
+
+ rc = adxl345_get_accel_data(&g_sensor_itf, &sample);
+ if (rc) {
+ console_printf("Read failed: %d\n", rc);
+ return rc;
+ }
+
+ console_printf("x:%s ", sensor_ftostr(sample.sad_x, tmpstr, 13));
+ console_printf("y:%s ", sensor_ftostr(sample.sad_y, tmpstr, 13));
+ console_printf("z:%s\n", sensor_ftostr(sample.sad_z, tmpstr, 13));
+ }
+
+ return 0;
+}
+
+static int
+adxl345_shell_cmd_dump(int argc, char **argv)
+{
+ uint8_t val;
+
+ if (argc > 2) {
+ return adxl345_shell_err_too_many_args(argv[1]);
+ }
+
+ /* Dump all the register values for debug purposes */
+ val = 0;
+ assert(0 == adxl345_read8(&g_sensor_itf, ADXL345_DEVID, &val));
+ console_printf("0x%02X (DEVID): 0x%02X\n", ADXL345_DEVID, val);
+ assert(0 == adxl345_read8(&g_sensor_itf, ADXL345_THRESH_TAP, &val));
+ console_printf("0x%02X (THRESH_TAP): 0x%02X\n", ADXL345_THRESH_TAP, val);
+ assert(0 == adxl345_read8(&g_sensor_itf, ADXL345_OFSX, &val));
+ console_printf("0x%02X (OFSX): 0x%02X\n", ADXL345_OFSX, val);
+ assert(0 == adxl345_read8(&g_sensor_itf, ADXL345_OFSY, &val));
+ console_printf("0x%02X (OFSY): 0x%02X\n", ADXL345_OFSY, val);
+ assert(0 == adxl345_read8(&g_sensor_itf, ADXL345_OFSZ, &val));
+ console_printf("0x%02X (OFSZ): 0x%02X\n", ADXL345_OFSZ, val);
+ assert(0 == adxl345_read8(&g_sensor_itf, ADXL345_DUR, &val));
+ console_printf("0x%02X (DUR): 0x%02X\n", ADXL345_DUR, val);
+ assert(0 == adxl345_read8(&g_sensor_itf, ADXL345_LATENT, &val));
+ console_printf("0x%02X (LATENT): 0x%02X\n", ADXL345_LATENT, val);
+ assert(0 == adxl345_read8(&g_sensor_itf, ADXL345_WINDOW, &val));
+ console_printf("0x%02X (WINDOW): 0x%02X\n", ADXL345_WINDOW, val);
+ assert(0 == adxl345_read8(&g_sensor_itf, ADXL345_THRESH_ACT, &val));
+ console_printf("0x%02X (THRESH_ACT): 0x%02X\n", ADXL345_THRESH_ACT, val);
+ assert(0 == adxl345_read8(&g_sensor_itf, ADXL345_THRESH_INACT, &val));
+ console_printf("0x%02X (THRESH_INACT): 0x%02X\n", ADXL345_THRESH_INACT, val);
+ assert(0 == adxl345_read8(&g_sensor_itf, ADXL345_TIME_INACT, &val));
+ console_printf("0x%02X (TIME_INACT): 0x%02X\n", ADXL345_TIME_INACT, val);
+ assert(0 == adxl345_read8(&g_sensor_itf, ADXL345_ACT_INACT_CTL, &val));
+ console_printf("0x%02X (ACT_INACT_CTL): 0x%02X\n", ADXL345_ACT_INACT_CTL, val);
+ assert(0 == adxl345_read8(&g_sensor_itf, ADXL345_THRESH_FF, &val));
+ console_printf("0x%02X (THRESH_FF): 0x%02X\n", ADXL345_THRESH_FF, val);
+ assert(0 == adxl345_read8(&g_sensor_itf, ADXL345_TIME_FF, &val));
+ console_printf("0x%02X (TIME_FF): 0x%02X\n", ADXL345_TIME_FF, val);
+ assert(0 == adxl345_read8(&g_sensor_itf, ADXL345_TAP_AXES, &val));
+ console_printf("0x%02X (TAP_AXES): 0x%02X\n", ADXL345_TAP_AXES, val);
+ assert(0 == adxl345_read8(&g_sensor_itf, ADXL345_ACT_TAP_STATUS, &val));
+ console_printf("0x%02X (ACT_TAP_STATUS): 0x%02X\n", ADXL345_ACT_TAP_STATUS, val);
+ assert(0 == adxl345_read8(&g_sensor_itf, ADXL345_BW_RATE, &val));
+ console_printf("0x%02X (BW_RATE): 0x%02X\n", ADXL345_BW_RATE, val);
+ assert(0 == adxl345_read8(&g_sensor_itf, ADXL345_POWER_CTL, &val));
+ console_printf("0x%02X (POWER_CTL): 0x%02X\n", ADXL345_POWER_CTL, val);
+ assert(0 == adxl345_read8(&g_sensor_itf, ADXL345_INT_ENABLE, &val));
+ console_printf("0x%02X (INT_ENABLE): 0x%02X\n", ADXL345_INT_ENABLE, val);
+ assert(0 == adxl345_read8(&g_sensor_itf, ADXL345_INT_MAP, &val));
+ console_printf("0x%02X (INT_MAP): 0x%02X\n", ADXL345_INT_MAP, val);
+ assert(0 == adxl345_read8(&g_sensor_itf, ADXL345_INT_SOURCE, &val));
+ console_printf("0x%02X (INT_SOURCE): 0x%02X\n", ADXL345_INT_SOURCE, val);
+ assert(0 == adxl345_read8(&g_sensor_itf, ADXL345_DATA_FORMAT, &val));
+ console_printf("0x%02X (DATA_FORMAT): 0x%02X\n", ADXL345_DATA_FORMAT, val);
+ assert(0 == adxl345_read8(&g_sensor_itf, ADXL345_DATAX0, &val));
+ console_printf("0x%02X (DATAX0): 0x%02X\n", ADXL345_DATAX0, val);
+ assert(0 == adxl345_read8(&g_sensor_itf, ADXL345_DATAX1, &val));
+ console_printf("0x%02X (DATAX1): 0x%02X\n", ADXL345_DATAX1, val);
+ assert(0 == adxl345_read8(&g_sensor_itf, ADXL345_DATAX0, &val));
+ console_printf("0x%02X (DATAY0): 0x%02X\n", ADXL345_DATAX0, val);
+ assert(0 == adxl345_read8(&g_sensor_itf, ADXL345_DATAX1, &val));
+ console_printf("0x%02X (DATAY1): 0x%02X\n", ADXL345_DATAX1, val);
+ assert(0 == adxl345_read8(&g_sensor_itf, ADXL345_DATAY0, &val));
+ console_printf("0x%02X (DATAZ0): 0x%02X\n", ADXL345_DATAY0, val);
+ assert(0 == adxl345_read8(&g_sensor_itf, ADXL345_DATAZ1, &val));
+ console_printf("0x%02X (DATAZ1): 0x%02X\n", ADXL345_DATAZ1, val);
+ assert(0 == adxl345_read8(&g_sensor_itf, ADXL345_FIFO_CTL, &val));
+ console_printf("0x%02X (FIFO_CTL): 0x%02X\n", ADXL345_FIFO_CTL, val);
+ assert(0 == adxl345_read8(&g_sensor_itf, ADXL345_FIFO_STATUS, &val));
+ console_printf("0x%02X (FIFO_STATUS): 0x%02X\n", ADXL345_FIFO_STATUS, val);
+
+ return 0;
+}
+
+static int
+adxl345_shell_cmd(int argc, char **argv)
+{
+ if (argc == 1) {
+ return adxl345_shell_help();
+ }
+
+ /* Read command (get a new data sample) */
+ if (argc > 1 && strcmp(argv[1], "r") == 0) {
+ return adxl345_shell_cmd_read(argc, argv);
+ }
+
+ /* Chip ID */
+ if (argc > 1 && strcmp(argv[1], "chipid") == 0) {
+ return adxl345_shell_cmd_read_chipid(argc, argv);
+ }
+
+ /* Dump */
+ if (argc > 1 && strcmp(argv[1], "dump") == 0) {
+ return adxl345_shell_cmd_dump(argc, argv);
+ }
+
+ return adxl345_shell_err_unknown_arg(argv[1]);
+}
+
+int
+adxl345_shell_init(void)
+{
+ int rc;
+
+ rc = shell_cmd_register(&adxl345_shell_cmd_struct);
+ SYSINIT_PANIC_ASSERT(rc == 0);
+
+ return rc;
+}
+
+#endif
diff --git a/hw/drivers/sensors/adxl345/syscfg.yml b/hw/drivers/sensors/adxl345/syscfg.yml
new file mode 100644
index 000000000..1156ceb3c
--- /dev/null
+++ b/hw/drivers/sensors/adxl345/syscfg.yml
@@ -0,0 +1,49 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements. See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership. The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License. You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied. See the License for the
+# specific language governing permissions and limitations
+# under the License.
+#
+
+syscfg.defs:
+ ADXL345_SHELL_ITF_NUM:
+ description: 'Shell interface number for the ADXL345'
+ value: 0
+ ADXL345_SHELL_ITF_TYPE:
+ description: 'Shell interface type for the ADXL345'
+ value: 1
+ ADXL345_SHELL_CSPIN:
+ description: 'CS pin for ADXL345'
+ value : -1
+ ADXL345_SHELL_ITF_ADDR:
+ description: 'Slave address for ADXL345'
+ value : 0x1D
+ ADXL345_CLI:
+ description: 'Enable shell support for the ADXL345'
+ value: 0
+ ADXL345_INT_ENABLE:
+ description: 'Enable interrupt support, necessary for events'
+ value: 1
+ ADXL345_INT_CFG_ACTIVE:
+ description: 'Set 0 for active-low, 1 for active-high'
+ value: 1
+ ADXL345_INT_CFG_OUTPUT:
+ description: 'Set 0 for push-pull, 1 for open-drain'
+ value: 1
+ ADXL345_INT_PIN_HOST:
+ description: 'Interrupt pin number on host device connected to INT on device'
+ value: 1
+ ADXL345_INT_PIN_DEVICE:
+ description: 'Interrupt pin number 1 or 2 on accelerometer device'
+ value: 1
diff --git a/hw/sensor/creator/src/sensor_creator.c b/hw/sensor/creator/src/sensor_creator.c
index aa21deede..3148f41e6 100644
--- a/hw/sensor/creator/src/sensor_creator.c
+++ b/hw/sensor/creator/src/sensor_creator.c
@@ -54,6 +54,10 @@
#include <bma253/bma253.h>
#endif
+#if MYNEWT_VAL(ADXL345_OFB)
+#include <adxl345/adxl345.h>
+#endif
+
/* Driver definitions */
#if MYNEWT_VAL(LSM303DLHC_OFB)
static struct lsm303dlhc lsm303dlhc;
@@ -91,6 +95,10 @@ static struct bmp280 bmp280;
static struct bma253 bma253;
#endif
+#if MYNEWT_VAL(ADXL345_OFB)
+static struct adxl345 adxl345;
+#endif
+
/**
* If a UART sensor needs to be created, interface is defined in
* the following way
@@ -192,6 +200,17 @@ static struct sensor_itf i2c_0_itf_lis = {
};
#endif
+#if MYNEWT_VAL(I2C_0) && MYNEWT_VAL(ADXL345_OFB)
+static struct sensor_itf i2c_0_itf_adxl = {
+ .si_type = SENSOR_ITF_I2C,
+ .si_num = 0,
+ .si_addr = 0x1D,
+ .si_ints = {
+ { MYNEWT_VAL(ADXL345_INT_PIN_HOST), MYNEWT_VAL(ADXL345_INT_PIN_DEVICE),
+ MYNEWT_VAL(ADXL345_INT_CFG_ACTIVE)}}
+};
+#endif
+
/**
* MS5837 Sensor default configuration used by the creator package
*
@@ -508,6 +527,53 @@ config_bma253_sensor(void)
}
#endif
+/**
+ * ADXL345 Sensor default configuration used by the creator package
+ *
+ * @return 0 on success, non-zero on failure
+ */
+#if MYNEWT_VAL(ADXL345_OFB)
+static int
+config_adxl345_sensor(void)
+{
+ int rc;
+ struct os_dev *dev;
+ struct adxl345_cfg cfg;
+
+ dev = (struct os_dev *) os_dev_open("adxl345_0", OS_TIMEOUT_NEVER, NULL);
+ assert(dev != NULL);
+
+ cfg.power_mode = ADXL345_POWER_MEASURE;
+ cfg.low_power_enable = 0;
+ cfg.accel_range = ADXL345_ACCEL_RANGE_4;
+ cfg.sample_rate = ADXL345_RATE_12_5_HZ;
+
+ cfg.offset_x = 0;
+ cfg.offset_y = 0;
+ cfg.offset_z = 0;
+
+ cfg.tap_cfg.threshold = 0x30; /* 3g */
+ cfg.tap_cfg.duration = 0x10; /* 10 ms */
+ cfg.tap_cfg.latency = 0x10; /* 20 ms */
+ cfg.tap_cfg.window = 0x80; /* 160 ms */
+ cfg.tap_cfg.x_enable = 1;
+ cfg.tap_cfg.y_enable = 1;
+ cfg.tap_cfg.z_enable = 1;
+ cfg.tap_cfg.suppress = 0;
+
+ cfg.freefall_threshold = 0x07; /* 440mg */
+ cfg.freefall_time = 0x14; /* 100ms */
+
+ cfg.mask = SENSOR_TYPE_ACCELEROMETER;
+
+ rc = adxl345_config((struct adxl345 *) dev, &cfg);
+ assert(rc == 0);
+
+ os_dev_close(dev);
+ return rc;
+}
+#endif
+
/* Sensor device creation */
void
sensor_dev_create(void)
@@ -602,5 +668,14 @@ sensor_dev_create(void)
assert(rc == 0);
#endif
+#if MYNEWT_VAL(ADXL345_OFB)
+ rc = os_dev_create((struct os_dev *) &adxl345, "adxl345_0",
+ OS_DEV_INIT_PRIMARY, 0, adxl345_init, (void *)&i2c_0_itf_adxl);
+ assert(rc == 0);
+
+ rc = config_adxl345_sensor();
+ assert(rc == 0);
+#endif
+
}
diff --git a/hw/sensor/creator/syscfg.yml b/hw/sensor/creator/syscfg.yml
index cd54cdfeb..0089a20ef 100644
--- a/hw/sensor/creator/syscfg.yml
+++ b/hw/sensor/creator/syscfg.yml
@@ -46,3 +46,6 @@ syscfg.defs:
BMA253_OFB:
description: 'BMA253 is present'
value : 0
+ ADXL345_OFB:
+ description: 'ADXL345 is present'
+ value : 0
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
With regards,
Apache Git Services