2016-02-11 01:34:08 +01:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2014, OpenMote Technologies, S.L.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. 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.
|
|
|
|
* 3. Neither the name of the Institute nor the names of its contributors
|
|
|
|
* may be used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, 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.
|
|
|
|
*
|
|
|
|
* This file is part of the Contiki operating system.
|
|
|
|
*
|
|
|
|
*/
|
2016-04-11 15:32:29 +02:00
|
|
|
/*---------------------------------------------------------------------------*/
|
2016-02-11 01:34:08 +01:00
|
|
|
/**
|
2016-04-11 15:32:29 +02:00
|
|
|
* \addtogroup openmote-adxl346-sensor
|
2016-02-11 01:34:08 +01:00
|
|
|
* @{
|
|
|
|
*
|
|
|
|
* \file
|
2016-04-11 15:32:29 +02:00
|
|
|
* Driver for the ADXL346 acceleration sensor
|
2016-02-11 01:34:08 +01:00
|
|
|
*
|
|
|
|
* \author
|
|
|
|
* Pere Tuset <peretuset@openmote.com>
|
|
|
|
*/
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
#include "dev/i2c.h"
|
|
|
|
#include "dev/adxl346.h"
|
2016-04-19 22:35:31 +02:00
|
|
|
#include "lib/sensors.h"
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
#define DEBUG 0
|
|
|
|
#if DEBUG
|
|
|
|
#define PRINTF(...) printf(__VA_ARGS__)
|
|
|
|
#else
|
|
|
|
#define PRINTF(...)
|
|
|
|
#endif
|
2016-02-11 01:34:08 +01:00
|
|
|
/*---------------------------------------------------------------------------*/
|
2016-04-11 15:32:29 +02:00
|
|
|
/**
|
|
|
|
* \name ADXL346 address and device identifier
|
|
|
|
* @{
|
|
|
|
*/
|
2016-02-11 01:34:08 +01:00
|
|
|
#define ADXL346_ADDRESS (0x53)
|
|
|
|
#define ADXL346_DEVID_VALUE (0xE6)
|
2016-04-11 15:32:29 +02:00
|
|
|
/** @} */
|
|
|
|
/* -------------------------------------------------------------------------- */
|
|
|
|
/**
|
|
|
|
* \name ADXL346 register addresses
|
|
|
|
* @{
|
|
|
|
*/
|
2016-02-11 01:34:08 +01:00
|
|
|
#define ADXL346_DEVID_ADDR (0x00)
|
|
|
|
#define ADXL346_THRES_TAP_ADDR (0x1D)
|
|
|
|
#define ADXL346_OFSX_ADDR (0x1E)
|
|
|
|
#define ADXL346_OFSY_ADDR (0x1F)
|
|
|
|
#define ADXL346_OFSZ_ADDR (0x20)
|
|
|
|
#define ADXL346_DUR_ADDR (0x21)
|
|
|
|
#define ADXL346_LATENT_ADDR (0x22)
|
|
|
|
#define ADXL346_WINDOW_ADDR (0x23)
|
|
|
|
#define ADXL346_THRESH_ACT_ADDR (0x24)
|
|
|
|
#define ADXL346_THRESH_INACT_ADDR (0x25)
|
|
|
|
#define ADXL346_TIME_INACT_ADDR (0x26)
|
|
|
|
#define ADXL346_ACT_INACT_CTL_ADDR (0x27)
|
|
|
|
#define ADXL346_THRESH_FF_ADDR (0x28)
|
|
|
|
#define ADXL346_TIME_FF_ADDR (0x29)
|
|
|
|
#define ADXL346_TAP_AXES_ADDR (0x2A)
|
|
|
|
#define ADXL346_ACT_TAP_STATUS_ADDR (0x2B)
|
|
|
|
#define ADXL346_BW_RATE_ADDR (0x2C)
|
|
|
|
#define ADXL346_POWER_CTL_ADDR (0x2D)
|
|
|
|
#define ADXL346_INT_ENABLE_ADDR (0x2E)
|
|
|
|
#define ADXL346_INT_MAP_ADDR (0x2F)
|
|
|
|
#define ADXL346_INT_SOURCE_ADDR (0x30)
|
|
|
|
#define ADXL346_DATA_FORMAT_ADDR (0x31)
|
|
|
|
#define ADXL346_DATAX0_ADDR (0x32)
|
|
|
|
#define ADXL346_DATAX1_ADDR (0x33)
|
|
|
|
#define ADXL346_DATAY0_ADDR (0x34)
|
|
|
|
#define ADXL346_DATAY1_ADDR (0x35)
|
|
|
|
#define ADXL346_DATAZ0_ADDR (0x36)
|
|
|
|
#define ADXL346_DATAZ1_ADDR (0x37)
|
|
|
|
#define ADXL346_FIFO_CTL_ADDR (0x38)
|
|
|
|
#define ADXL346_FIFO_STATUS_ADDR (0x39)
|
|
|
|
#define ADXL346_TAP_SIGN_ADDR (0x3A)
|
|
|
|
#define ADXL346_ORIENT_CONF_ADDR (0x3B)
|
|
|
|
#define ADXL346_ORIENT_ADDR (0x3C)
|
2016-04-11 15:32:29 +02:00
|
|
|
/** @} */
|
|
|
|
/* -------------------------------------------------------------------------- */
|
|
|
|
/**
|
|
|
|
* \name ADXL346 register values
|
|
|
|
* @{
|
|
|
|
*/
|
2016-02-11 01:34:08 +01:00
|
|
|
#define ADXL346_INT_ENABLE_DATA_READY (1 << 7)
|
|
|
|
#define ADXL346_INT_ENABLE_SINGLE_TAP (1 << 6)
|
|
|
|
#define ADXL346_INT_ENABLE_DOUBLE_TAP (1 << 5)
|
|
|
|
#define ADXL346_INT_ENABLE_ACTIVITY (1 << 4)
|
|
|
|
#define ADXL346_INT_ENABLE_INACTIVITY (1 << 3)
|
|
|
|
#define ADXL346_INT_ENABLE_FREE_FALL (1 << 2)
|
|
|
|
#define ADXL346_INT_ENABLE_WATERMARK (1 << 1)
|
|
|
|
#define ADXL346_INT_ENABLE_OVERRUN (1 << 0)
|
|
|
|
|
|
|
|
#define ADXL346_ACT_INACT_CTL_ACT_ACDC (1 << 7)
|
|
|
|
#define ADXL346_ACT_INACT_CTL_ACT_X_EN (1 << 6)
|
|
|
|
#define ADXL346_ACT_INACT_CTL_ACT_Y_EN (1 << 5)
|
|
|
|
#define ADXL346_ACT_INACT_CTL_ACT_Z_EN (1 << 4)
|
|
|
|
#define ADXL346_ACT_INACT_CTL_INACT_ACDC (1 << 3)
|
|
|
|
#define ADXL346_ACT_INACT_CTL_INACT_X_EN (1 << 2)
|
|
|
|
#define ADXL346_ACT_INACT_CTL_INACT_Y_EN (1 << 1)
|
|
|
|
#define ADXL346_ACT_INACT_CTL_INACT_Z_EN (1 << 0)
|
|
|
|
|
|
|
|
#define ADXL346_TAP_AXES_SUPPRESS (1 << 3)
|
|
|
|
#define ADXL346_TAP_AXES_TAP_X_EN (1 << 2)
|
|
|
|
#define ADXL346_TAP_AXES_TAP_Y_EN (1 << 1)
|
|
|
|
#define ADXL346_TAP_AXES_TAP_Z_EN (1 << 0)
|
|
|
|
|
|
|
|
#define ADXL346_ACT_TAP_STATUS_ACT_X_SRC (1 << 6)
|
|
|
|
#define ADXL346_ACT_TAP_STATUS_ACT_Y_SRC (1 << 5)
|
|
|
|
#define ADXL346_ACT_TAP_STATUS_ACT_Z_SRC (1 << 4)
|
|
|
|
#define ADXL346_ACT_TAP_STATUS_ASLEEP (1 << 3)
|
|
|
|
#define ADXL346_ACT_TAP_STATUS_TAP_X_SRC (1 << 2)
|
|
|
|
#define ADXL346_ACT_TAP_STATUS_TAP_Y_SRC (1 << 1)
|
|
|
|
#define ADXL346_ACT_TAP_STATUS_TAP_Z_SRC (1 << 0)
|
|
|
|
|
|
|
|
#define ADXL346_BW_RATE_POWER (1 << 4)
|
|
|
|
#define ADXL346_BW_RATE_RATE(x) ((x) & 0x0F)
|
|
|
|
|
|
|
|
#define ADXL346_POWER_CTL_LINK (1 << 5)
|
|
|
|
#define ADXL346_POWER_CTL_AUTO_SLEEP (1 << 4)
|
|
|
|
#define ADXL346_POWER_CTL_MEASURE (1 << 3)
|
|
|
|
#define ADXL346_POWER_CTL_SLEEP (1 << 2)
|
|
|
|
#define ADXL346_POWER_CTL_WAKEUP(x) ((x) & 0x03)
|
|
|
|
|
|
|
|
#define ADXL346_DATA_FORMAT_SELF_TEST (1 << 7)
|
|
|
|
#define ADXL346_DATA_FORMAT_SPI (1 << 6)
|
|
|
|
#define ADXL346_DATA_FORMAT_INT_INVERT (1 << 5)
|
|
|
|
#define ADXL346_DATA_FORMAT_FULL_RES (1 << 3)
|
|
|
|
#define ADXL346_DATA_FORMAT_JUSTIFY (1 << 2)
|
|
|
|
#define ADXL346_DATA_FORMAT_RANGE(x) ((x) & 0x03)
|
|
|
|
#define ADXL346_DATA_FORMAT_RANGE_PM_2g (0)
|
|
|
|
#define ADXL346_DATA_FORMAT_RANGE_PM_4g (1)
|
|
|
|
#define ADXL346_DATA_FORMAT_RANGE_PM_8g (2)
|
|
|
|
#define ADXL346_DATA_FORMAT_RANGE_PM_16g (3)
|
2016-04-11 15:32:29 +02:00
|
|
|
/** @} */
|
2016-02-11 01:34:08 +01:00
|
|
|
/*---------------------------------------------------------------------------*/
|
2016-04-19 22:35:31 +02:00
|
|
|
static uint8_t enabled;
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
static void
|
2016-02-11 01:34:08 +01:00
|
|
|
adxl346_init(void)
|
|
|
|
{
|
|
|
|
uint8_t config[2];
|
|
|
|
|
|
|
|
config[0] = ADXL346_BW_RATE_ADDR;
|
|
|
|
config[1] = (ADXL346_BW_RATE_RATE(11));
|
|
|
|
i2c_burst_send(ADXL346_ADDRESS, config, sizeof(config));
|
|
|
|
|
|
|
|
config[0] = ADXL346_DATA_FORMAT_ADDR;
|
|
|
|
config[1] = (ADXL346_DATA_FORMAT_SELF_TEST |
|
|
|
|
ADXL346_DATA_FORMAT_FULL_RES |
|
|
|
|
ADXL346_DATA_FORMAT_RANGE_PM_16g);
|
|
|
|
i2c_burst_send(ADXL346_ADDRESS, config, sizeof(config));
|
|
|
|
|
|
|
|
config[0] = ADXL346_POWER_CTL_ADDR;
|
|
|
|
config[1] = (ADXL346_POWER_CTL_MEASURE);
|
|
|
|
i2c_burst_send(ADXL346_ADDRESS, config, sizeof(config));
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
2016-04-19 22:35:31 +02:00
|
|
|
static uint8_t
|
2016-02-11 01:34:08 +01:00
|
|
|
adxl346_is_present(void)
|
|
|
|
{
|
|
|
|
uint8_t is_present;
|
|
|
|
|
|
|
|
i2c_single_send(ADXL346_ADDRESS, ADXL346_DEVID_ADDR);
|
|
|
|
i2c_single_receive(ADXL346_ADDRESS, &is_present);
|
|
|
|
|
|
|
|
return is_present == ADXL346_DEVID_VALUE;
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
2016-04-19 22:35:31 +02:00
|
|
|
static uint16_t
|
|
|
|
adxl346_read_accel(uint8_t addr1, uint8_t addr2)
|
2016-02-11 01:34:08 +01:00
|
|
|
{
|
|
|
|
uint8_t acceleration[2];
|
2016-04-19 22:35:31 +02:00
|
|
|
uint16_t z;
|
2016-02-11 01:34:08 +01:00
|
|
|
|
2016-04-19 22:35:31 +02:00
|
|
|
i2c_single_send(ADXL346_ADDRESS, addr1);
|
2016-02-11 01:34:08 +01:00
|
|
|
i2c_single_receive(ADXL346_ADDRESS, &acceleration[0]);
|
2016-04-19 22:35:31 +02:00
|
|
|
i2c_single_send(ADXL346_ADDRESS, addr2);
|
2016-02-11 01:34:08 +01:00
|
|
|
i2c_single_receive(ADXL346_ADDRESS, &acceleration[1]);
|
|
|
|
|
2016-04-19 22:35:31 +02:00
|
|
|
z = (acceleration[0] << 8) | acceleration[1];
|
2016-02-11 01:34:08 +01:00
|
|
|
|
2016-04-19 22:35:31 +02:00
|
|
|
return z;
|
2016-02-11 01:34:08 +01:00
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
2016-04-19 22:35:31 +02:00
|
|
|
static int
|
|
|
|
status(int type)
|
2016-02-11 01:34:08 +01:00
|
|
|
{
|
2016-04-19 22:35:31 +02:00
|
|
|
switch(type) {
|
|
|
|
case SENSORS_ACTIVE:
|
|
|
|
case SENSORS_READY:
|
|
|
|
return enabled;
|
|
|
|
}
|
|
|
|
return 0;
|
2016-02-11 01:34:08 +01:00
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
2016-04-19 22:35:31 +02:00
|
|
|
static int
|
|
|
|
value(int type)
|
2016-02-11 01:34:08 +01:00
|
|
|
{
|
2016-04-19 22:35:31 +02:00
|
|
|
if(!enabled) {
|
|
|
|
PRINTF("ADXL346: sensor not started\n");
|
|
|
|
return ADXL346_ERROR;
|
|
|
|
}
|
2016-02-11 01:34:08 +01:00
|
|
|
|
2016-04-19 22:35:31 +02:00
|
|
|
if(type == ADXL346_READ_X) {
|
|
|
|
return adxl346_read_accel(ADXL346_DATAX0_ADDR, ADXL346_DATAX1_ADDR);
|
|
|
|
} else if(type == ADXL346_READ_Y) {
|
|
|
|
return adxl346_read_accel(ADXL346_DATAY0_ADDR, ADXL346_DATAY1_ADDR);
|
|
|
|
} else if(type == ADXL346_READ_Z) {
|
|
|
|
return adxl346_read_accel(ADXL346_DATAZ0_ADDR, ADXL346_DATAZ1_ADDR);
|
|
|
|
} else {
|
|
|
|
PRINTF("ADXL346: invalid value requested\n");
|
|
|
|
return ADXL346_ERROR;
|
|
|
|
}
|
2016-02-11 01:34:08 +01:00
|
|
|
|
2016-04-19 22:35:31 +02:00
|
|
|
return ADXL346_ERROR;
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
static int
|
|
|
|
configure(int type, int value)
|
|
|
|
{
|
|
|
|
if(type == ADXL346_ACTIVATE) {
|
|
|
|
if(!adxl346_is_present()) {
|
|
|
|
PRINTF("ADXL346: is not present\n");
|
|
|
|
return ADXL346_ERROR;
|
|
|
|
} else {
|
|
|
|
adxl346_init();
|
|
|
|
enabled = 1;
|
|
|
|
return ADXL346_SUCCESS;
|
|
|
|
}
|
|
|
|
}
|
2016-02-11 01:34:08 +01:00
|
|
|
|
2016-04-19 22:35:31 +02:00
|
|
|
return ADXL346_ERROR;
|
2016-02-11 01:34:08 +01:00
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
2016-04-19 22:35:31 +02:00
|
|
|
SENSORS_SENSOR(adxl346, ADXL346_SENSOR, value, configure, status);
|
|
|
|
/*---------------------------------------------------------------------------*/
|
2016-02-11 01:34:08 +01:00
|
|
|
/** @} */
|