2011-04-03 16:26:05 +02:00
|
|
|
/****************************************************************************
|
2011-05-07 21:22:15 +02:00
|
|
|
* drivers/sensors/lis331dl.c
|
2011-04-03 16:26:05 +02:00
|
|
|
*
|
|
|
|
* Copyright (C) 2011 Uros Platise. All rights reserved.
|
|
|
|
*
|
|
|
|
* Authors: Uros Platise <uros.platise@isotel.eu>
|
|
|
|
*
|
|
|
|
* 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 NuttX 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 COPYRIGHT HOLDERS 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
|
|
|
|
* COPYRIGHT OWNER 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.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2014-04-13 21:18:06 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
2011-04-03 16:26:05 +02:00
|
|
|
#include <nuttx/config.h>
|
|
|
|
#include <assert.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
|
2011-04-06 18:40:47 +02:00
|
|
|
#include <nuttx/kmalloc.h>
|
2011-05-07 21:22:15 +02:00
|
|
|
#include <nuttx/sensors/lis331dl.h>
|
2011-04-20 15:32:49 +02:00
|
|
|
|
2014-04-13 21:18:06 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Pre-processor Definitions
|
|
|
|
****************************************************************************/
|
|
|
|
/* LIS331DL Internal Registers **********************************************/
|
2011-04-20 15:32:49 +02:00
|
|
|
|
|
|
|
#define ST_LIS331DL_WHOAMI 0x0F /* who am I register */
|
|
|
|
#define ST_LIS331DL_WHOAMI_VALUE 0x3B /* Valid result is 0x3B */
|
|
|
|
|
|
|
|
#define ST_LIS331DL_CTRL_REG1 0x20
|
|
|
|
#define ST_LIS331DL_CR1_DR 0x80 /* Data-rate selection 0: 100 Hz, 1: 400 Hz */
|
|
|
|
#define ST_LIS331DL_CR1_PD 0x40 /* Active Mode (1) / Power-down (0) */
|
|
|
|
#define ST_LIS331DL_CR1_FS 0x20 /* Full Scale (1) +-9g or Normal Scale (0) +-2g */
|
|
|
|
#define ST_LIS331DL_CR1_ST 0x18 /* Self test enable */
|
|
|
|
#define ST_LIS331DL_CR1_ZEN 0x04 /* Z-Axis Enable */
|
|
|
|
#define ST_LIS331DL_CR1_YEN 0x02 /* Y-Axis Enable */
|
|
|
|
#define ST_LIS331DL_CR1_XEN 0x01 /* X-Axis Enable */
|
|
|
|
|
|
|
|
#define ST_LIS331DL_CTRL_REG2 0x21
|
|
|
|
#define ST_LIS331DL_CTRL_REG3 0x22
|
|
|
|
|
|
|
|
#define ST_LIS331DL_HP_FILTER_RESET 0x23
|
|
|
|
|
|
|
|
#define ST_LIS331DL_STATUS_REG 0x27 /* Status Register */
|
|
|
|
#define ST_LIS331DL_SR_ZYXOR 0x80 /* OR'ed X,Y and Z data over-run */
|
|
|
|
#define ST_LIS331DL_SR_ZOR 0x40 /* individual data over-run ... */
|
|
|
|
#define ST_LIS331DL_SR_YOR 0x20
|
|
|
|
#define ST_LIS331DL_SR_XOR 0x10
|
|
|
|
#define ST_LIS331DL_SR_ZYXDA 0x08 /* OR'ed X,Y and Z data available */
|
|
|
|
#define ST_LIS331DL_SR_ZDA 0x04 /* individual data available ... */
|
|
|
|
#define ST_LIS331DL_SR_YDA 0x02
|
|
|
|
#define ST_LIS331DL_SR_XDA 0x01
|
|
|
|
|
|
|
|
#define ST_LIS331DL_OUT_X 0x29
|
|
|
|
#define ST_LIS331DL_OUT_Y 0x2B
|
|
|
|
#define ST_LIS331DL_OUT_Z 0x2D
|
|
|
|
|
2014-04-13 21:18:06 +02:00
|
|
|
/****************************************************************************
|
2011-04-03 16:26:05 +02:00
|
|
|
* Private Data Types
|
2014-04-13 21:18:06 +02:00
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
struct lis331dl_dev_s
|
|
|
|
{
|
|
|
|
struct i2c_dev_s *i2c;
|
|
|
|
uint8_t address;
|
|
|
|
struct lis331dl_vector_s a;
|
|
|
|
uint8_t cr1;
|
|
|
|
uint8_t cr2;
|
|
|
|
uint8_t cr3;
|
2011-04-03 16:26:05 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
2014-04-13 21:18:06 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* LIS331DL Access with range check
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* dev LIS331 DL Private Structure
|
|
|
|
* subaddr LIS331 Sub Address
|
|
|
|
* buf Pointer to buffer, either for read or write access
|
|
|
|
* length When >0 it denotes read access, when <0 it denotes write access
|
|
|
|
* of -length
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* Returns OK on success or errno is set.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int lis331dl_access(FAR struct lis331dl_dev_s *dev, uint8_t subaddr,
|
|
|
|
FAR uint8_t *buf, int length)
|
2011-04-03 16:26:05 +02:00
|
|
|
{
|
2014-04-13 21:18:06 +02:00
|
|
|
uint16_t flags = 0;
|
|
|
|
int retval;
|
|
|
|
|
|
|
|
if (length > 0)
|
|
|
|
{
|
|
|
|
flags = I2C_M_READ;
|
2011-04-03 16:26:05 +02:00
|
|
|
}
|
2014-04-13 21:18:06 +02:00
|
|
|
else
|
|
|
|
{
|
|
|
|
flags = I2C_M_NORESTART;
|
|
|
|
length = -length;
|
2011-04-03 16:26:05 +02:00
|
|
|
}
|
|
|
|
|
2014-04-13 21:18:06 +02:00
|
|
|
/* Check valid address ranges and set auto address increment flag */
|
|
|
|
|
|
|
|
if (subaddr == 0x0f)
|
|
|
|
{
|
|
|
|
if (length > 1)
|
|
|
|
{
|
|
|
|
length = 1;
|
|
|
|
}
|
2011-04-03 16:26:05 +02:00
|
|
|
}
|
2014-04-13 21:18:06 +02:00
|
|
|
else if (subaddr >= 0x20 && subaddr < 0x24)
|
|
|
|
{
|
|
|
|
if (length > (0x24 - subaddr))
|
|
|
|
{
|
|
|
|
length = 0x24 - subaddr;
|
|
|
|
}
|
2011-04-03 16:26:05 +02:00
|
|
|
}
|
2014-04-13 21:18:06 +02:00
|
|
|
else if (subaddr >= 0x27 && subaddr < 0x2e)
|
|
|
|
{
|
|
|
|
if (length > (0x2e - subaddr))
|
|
|
|
{
|
|
|
|
length = 0x2e - subaddr;
|
|
|
|
}
|
2011-04-03 16:26:05 +02:00
|
|
|
}
|
2014-04-13 21:18:06 +02:00
|
|
|
else if (subaddr >= 0x30 && subaddr < 0x40)
|
|
|
|
{
|
|
|
|
if (length > (0x40 - subaddr))
|
|
|
|
{
|
|
|
|
length = 0x40 - subaddr;
|
|
|
|
}
|
2011-04-03 16:26:05 +02:00
|
|
|
}
|
2014-04-13 21:18:06 +02:00
|
|
|
else
|
|
|
|
{
|
|
|
|
errno = EFAULT;
|
|
|
|
return ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (length > 1)
|
|
|
|
{
|
|
|
|
subaddr |= 0x80;
|
2011-04-03 16:26:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Create message and send */
|
2014-04-13 21:18:06 +02:00
|
|
|
|
|
|
|
struct i2c_msg_s msgv[2] =
|
|
|
|
{
|
2011-04-03 16:26:05 +02:00
|
|
|
{
|
|
|
|
.addr = dev->address,
|
|
|
|
.flags = 0,
|
|
|
|
.buffer = &subaddr,
|
|
|
|
.length = 1
|
|
|
|
},
|
|
|
|
{
|
|
|
|
.addr = dev->address,
|
|
|
|
.flags = flags,
|
|
|
|
.buffer = buf,
|
|
|
|
.length = length
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-04-13 21:18:06 +02:00
|
|
|
if ((retval = I2C_TRANSFER(dev->i2c, msgv, 2)) == OK)
|
|
|
|
{
|
|
|
|
return length;
|
|
|
|
}
|
2011-04-03 16:26:05 +02:00
|
|
|
|
2014-04-13 21:18:06 +02:00
|
|
|
return retval;
|
2011-04-03 16:26:05 +02:00
|
|
|
}
|
|
|
|
|
2014-04-13 21:18:06 +02:00
|
|
|
static int lis331dl_readregs(FAR struct lis331dl_dev_s *dev)
|
|
|
|
{
|
|
|
|
if (lis331dl_access(dev, ST_LIS331DL_CTRL_REG1, &dev->cr1, 3) != 3)
|
|
|
|
{
|
|
|
|
return ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|
2011-04-03 16:26:05 +02:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
2014-04-13 21:18:06 +02:00
|
|
|
FAR struct lis331dl_dev_s *lis331dl_init(FAR struct i2c_dev_s *i2c,
|
|
|
|
uint16_t address)
|
2011-04-03 16:26:05 +02:00
|
|
|
{
|
2014-04-13 21:18:06 +02:00
|
|
|
FAR struct lis331dl_dev_s * dev;
|
|
|
|
uint8_t retval;
|
|
|
|
|
|
|
|
ASSERT(i2c);
|
|
|
|
ASSERT(address);
|
|
|
|
|
2014-09-01 01:26:36 +02:00
|
|
|
dev = kmm_malloc(sizeof(struct lis331dl_dev_s));
|
2014-04-13 21:18:06 +02:00
|
|
|
if (dev == NULL)
|
|
|
|
{
|
|
|
|
errno = ENOMEM;
|
|
|
|
return NULL;
|
2011-05-16 17:09:39 +02:00
|
|
|
}
|
2014-04-13 21:18:06 +02:00
|
|
|
|
|
|
|
memset(dev, 0, sizeof(struct lis331dl_dev_s));
|
|
|
|
dev->i2c = i2c;
|
|
|
|
dev->address = address;
|
|
|
|
|
|
|
|
/* Probe device */
|
|
|
|
|
|
|
|
if (lis331dl_access(dev, ST_LIS331DL_WHOAMI, &retval, 1) > 0)
|
|
|
|
{
|
|
|
|
/* Check chip identification, in the future several more compatible parts
|
|
|
|
* may be added here.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (retval == ST_LIS331DL_WHOAMI_VALUE)
|
|
|
|
{
|
|
|
|
/* Copy LIS331DL registers to our private structure and power-up device */
|
|
|
|
|
|
|
|
if (lis331dl_readregs(dev)==OK && lis331dl_powerup(dev) == OK)
|
|
|
|
{
|
|
|
|
/* Normal exit point */
|
|
|
|
|
|
|
|
errno = 0;
|
|
|
|
return dev;
|
2011-04-03 16:26:05 +02:00
|
|
|
}
|
2014-04-13 21:18:06 +02:00
|
|
|
|
|
|
|
retval = errno;
|
2011-04-03 16:26:05 +02:00
|
|
|
}
|
2014-04-13 21:18:06 +02:00
|
|
|
|
|
|
|
/* Otherwise, we mark an invalid device found at given address */
|
|
|
|
|
|
|
|
retval = ENODEV;
|
2011-04-03 16:26:05 +02:00
|
|
|
}
|
2014-04-13 21:18:06 +02:00
|
|
|
else
|
|
|
|
{
|
|
|
|
/* No response at given address is marked as */
|
|
|
|
|
|
|
|
retval = EFAULT;
|
2011-04-03 16:26:05 +02:00
|
|
|
}
|
|
|
|
|
2014-04-13 21:18:06 +02:00
|
|
|
/* Error exit */
|
2011-04-03 16:26:05 +02:00
|
|
|
|
2014-09-01 01:04:02 +02:00
|
|
|
kmm_free(dev);
|
2014-04-13 21:18:06 +02:00
|
|
|
errno = retval;
|
|
|
|
return NULL;
|
|
|
|
}
|
2011-04-03 16:26:05 +02:00
|
|
|
|
2014-04-13 21:18:06 +02:00
|
|
|
int lis331dl_deinit(FAR struct lis331dl_dev_s * dev)
|
2011-04-03 16:26:05 +02:00
|
|
|
{
|
2014-04-13 21:18:06 +02:00
|
|
|
ASSERT(dev);
|
2011-04-03 16:26:05 +02:00
|
|
|
|
2014-04-13 21:18:06 +02:00
|
|
|
lis331dl_powerdown(dev);
|
2014-09-01 01:04:02 +02:00
|
|
|
kmm_free(dev);
|
2011-04-03 16:26:05 +02:00
|
|
|
|
2014-04-13 21:18:06 +02:00
|
|
|
return OK;
|
2011-04-03 16:26:05 +02:00
|
|
|
}
|
|
|
|
|
2014-04-13 21:18:06 +02:00
|
|
|
int lis331dl_powerup(FAR struct lis331dl_dev_s * dev)
|
2011-04-03 16:26:05 +02:00
|
|
|
{
|
2014-04-13 21:18:06 +02:00
|
|
|
dev->cr1 = ST_LIS331DL_CR1_PD |
|
|
|
|
ST_LIS331DL_CR1_ZEN | ST_LIS331DL_CR1_YEN | ST_LIS331DL_CR1_XEN;
|
|
|
|
dev->cr2 = 0;
|
|
|
|
dev->cr3 = 0;
|
|
|
|
|
|
|
|
if (lis331dl_access(dev, ST_LIS331DL_CTRL_REG1, &dev->cr1, -3) == 3)
|
|
|
|
{
|
|
|
|
return OK;
|
|
|
|
}
|
2011-04-03 16:26:05 +02:00
|
|
|
|
2014-04-13 21:18:06 +02:00
|
|
|
return ERROR;
|
|
|
|
}
|
2011-04-03 16:26:05 +02:00
|
|
|
|
2014-04-13 21:18:06 +02:00
|
|
|
int lis331dl_powerdown(FAR struct lis331dl_dev_s * dev)
|
2011-04-03 16:26:05 +02:00
|
|
|
{
|
2014-04-13 21:18:06 +02:00
|
|
|
dev->cr1 = ST_LIS331DL_CR1_ZEN | ST_LIS331DL_CR1_YEN | ST_LIS331DL_CR1_XEN;
|
|
|
|
dev->cr2 = 0;
|
|
|
|
dev->cr3 = 0;
|
|
|
|
|
|
|
|
if (lis331dl_access(dev, ST_LIS331DL_CTRL_REG1, &dev->cr1, -3) == 3)
|
|
|
|
{
|
|
|
|
return OK;
|
|
|
|
}
|
2011-04-03 16:26:05 +02:00
|
|
|
|
2014-04-13 21:18:06 +02:00
|
|
|
return ERROR;
|
|
|
|
}
|
2011-04-03 16:26:05 +02:00
|
|
|
|
2014-04-13 21:18:06 +02:00
|
|
|
int lis331dl_setconversion(FAR struct lis331dl_dev_s * dev, bool full, bool fast)
|
2011-04-03 16:26:05 +02:00
|
|
|
{
|
2014-04-13 21:18:06 +02:00
|
|
|
dev->cr1 = ST_LIS331DL_CR1_PD |
|
|
|
|
(full ? ST_LIS331DL_CR1_FS : 0) | (fast ? ST_LIS331DL_CR1_DR : 0) |
|
|
|
|
ST_LIS331DL_CR1_ZEN | ST_LIS331DL_CR1_YEN | ST_LIS331DL_CR1_XEN;
|
|
|
|
|
|
|
|
if (lis331dl_access(dev, ST_LIS331DL_CTRL_REG1, &dev->cr1, -1) == 1)
|
|
|
|
{
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ERROR;
|
2011-04-03 16:26:05 +02:00
|
|
|
}
|
|
|
|
|
2014-04-13 21:18:06 +02:00
|
|
|
int lis331dl_getprecision(FAR struct lis331dl_dev_s * dev)
|
|
|
|
{
|
|
|
|
if (dev->cr1 & ST_LIS331DL_CR1_FS)
|
|
|
|
{
|
|
|
|
return 9200/127; /* typ. 9.2g full scale */
|
|
|
|
}
|
2011-04-03 16:26:05 +02:00
|
|
|
|
2014-04-13 21:18:06 +02:00
|
|
|
return 2300/127; /* typ. 2.3g full scale */
|
2011-04-03 16:26:05 +02:00
|
|
|
}
|
|
|
|
|
2014-04-13 21:18:06 +02:00
|
|
|
int lis331dl_getsamplerate(FAR struct lis331dl_dev_s * dev)
|
|
|
|
{
|
|
|
|
if (dev->cr1 & ST_LIS331DL_CR1_DR)
|
|
|
|
{
|
|
|
|
return 400;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 100;
|
|
|
|
}
|
2011-04-03 16:26:05 +02:00
|
|
|
|
2014-04-13 21:18:06 +02:00
|
|
|
FAR const struct lis331dl_vector_s *
|
|
|
|
lis331dl_getreadings(FAR struct lis331dl_dev_s * dev)
|
2011-04-03 16:26:05 +02:00
|
|
|
{
|
2014-04-13 21:18:06 +02:00
|
|
|
uint8_t retval[7];
|
|
|
|
|
|
|
|
ASSERT(dev);
|
|
|
|
|
|
|
|
if (lis331dl_access(dev, ST_LIS331DL_STATUS_REG, retval, 7) == 7)
|
|
|
|
{
|
|
|
|
/* If result is not yet ready, return NULL */
|
|
|
|
|
|
|
|
if (!(retval[0] & ST_LIS331DL_SR_ZYXDA))
|
|
|
|
{
|
|
|
|
errno = EAGAIN;
|
|
|
|
return NULL;
|
2011-04-05 18:25:04 +02:00
|
|
|
}
|
2014-04-13 21:18:06 +02:00
|
|
|
|
|
|
|
dev->a.x = retval[2];
|
|
|
|
dev->a.y = retval[4];
|
|
|
|
dev->a.z = retval[6];
|
|
|
|
return &dev->a;
|
2011-04-03 16:26:05 +02:00
|
|
|
}
|
2014-04-13 21:18:06 +02:00
|
|
|
|
|
|
|
return NULL;
|
2011-04-03 16:26:05 +02:00
|
|
|
}
|