mirror of
https://github.com/apache/nuttx.git
synced 2026-09-13 22:20:24 +00:00
drivers/sensors: Add driver for the LIS3DSH 3 axis accelerometer.
This commit is contained in:
parent
595f00e271
commit
0044910e33
4 changed files with 875 additions and 0 deletions
|
|
@ -38,6 +38,13 @@ config SENSOR_KXTJ9_I2C_BUS_SPEED
|
|||
|
||||
endif # SENSOR_KXTJ9
|
||||
|
||||
config LIS3DSH
|
||||
bool "STMicro LIS3DSH 3-Axis acclerometer support"
|
||||
default n
|
||||
select SPI
|
||||
---help---
|
||||
Enable driver support for the STMicro LIS3DSH 3-Axis acclerometer.
|
||||
|
||||
config LIS331DL
|
||||
bool "ST LIS331DL device support"
|
||||
default n
|
||||
|
|
|
|||
|
|
@ -53,6 +53,10 @@ ifeq ($(CONFIG_SENSOR_KXTJ9),y)
|
|||
CSRCS += kxjt9.c
|
||||
endif
|
||||
|
||||
ifeq ($(CONFIG_LIS3DSH),y)
|
||||
CSRCS += lis3dsh.c
|
||||
endif
|
||||
|
||||
ifeq ($(CONFIG_LIS331DL),y)
|
||||
CSRCS += lis331dl.c
|
||||
endif
|
||||
|
|
|
|||
587
drivers/sensors/lis3dsh.c
Normal file
587
drivers/sensors/lis3dsh.c
Normal file
|
|
@ -0,0 +1,587 @@
|
|||
/****************************************************************************
|
||||
* drivers/sensors/lis3dsh.c
|
||||
* Character driver for the LIS3DSH 3-Axis acclerometer.
|
||||
*
|
||||
* Copyright (C) 2016 DS-Automotion GmbH. All rights reserved.
|
||||
* Author: Alexander Entinger <a.entinger@ds-automotion.com>
|
||||
* Thomas Ilk
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Included Files
|
||||
****************************************************************************/
|
||||
|
||||
#include <nuttx/config.h>
|
||||
|
||||
#include <errno.h>
|
||||
#include <debug.h>
|
||||
#include <string.h>
|
||||
#include <semaphore.h>
|
||||
|
||||
#include <nuttx/kmalloc.h>
|
||||
#include <nuttx/wqueue.h>
|
||||
|
||||
#include <nuttx/fs/fs.h>
|
||||
#include <nuttx/sensors/lis3dsh.h>
|
||||
|
||||
#if defined(CONFIG_SPI) && defined(CONFIG_LIS3DSH)
|
||||
|
||||
/****************************************************************************
|
||||
* Private
|
||||
****************************************************************************/
|
||||
|
||||
struct lis3dsh_sensor_data_s
|
||||
{
|
||||
int16_t x_acc; /* Measurement result for x axis */
|
||||
int16_t y_acc; /* Measurement result for y axis */
|
||||
int16_t z_acc; /* Measurement result for z axis */
|
||||
};
|
||||
|
||||
struct lis3dsh_dev_s
|
||||
{
|
||||
FAR struct lis3dsh_dev_s *flink; /* Supports a singly linked list of
|
||||
* drivers */
|
||||
FAR struct spi_dev_s *spi; /* Pointer to the SPI instance */
|
||||
FAR struct lis3dsh_config_s *config; /* Pointer to the configuration
|
||||
* of the LIS3DSH sensor */
|
||||
sem_t datasem; /* Manages exclusive access to this
|
||||
* structure */
|
||||
struct lis3dsh_sensor_data_s data; /* The data as measured by the sensor */
|
||||
struct work_s work; /* The work queue is responsible for
|
||||
* retrieving the data from the
|
||||
* sensor after the arrival of new
|
||||
* data was signalled in an interrupt */
|
||||
};
|
||||
|
||||
/****************************************************************************
|
||||
* Private Function Prototypes
|
||||
****************************************************************************/
|
||||
|
||||
static void lis3dsh_read_register(FAR struct lis3dsh_dev_s *dev,
|
||||
uint8_t const reg_addr, uint8_t *reg_data);
|
||||
static void lis3dsh_write_register(FAR struct lis3dsh_dev_s *dev,
|
||||
uint8_t const reg_addr,
|
||||
uint8_t const reg_data);
|
||||
static void lis3dsh_reset(FAR struct lis3dsh_dev_s *dev);
|
||||
static void lis3dsh_read_measurement_data(FAR struct lis3dsh_dev_s *dev);
|
||||
static void lis3dsh_read_acclerometer_data(FAR struct lis3dsh_dev_s *dev,
|
||||
uint16_t *x_acc, uint16_t *y_acc,
|
||||
uint16_t *z_acc);
|
||||
static int lis3dsh_interrupt_handler(int irq, FAR void *context);
|
||||
static void lis3dsh_worker(FAR void *arg);
|
||||
|
||||
static int lis3dsh_open(FAR struct file *filep);
|
||||
static int lis3dsh_close(FAR struct file *filep);
|
||||
static ssize_t lis3dsh_read(FAR struct file *, FAR char *buffer,
|
||||
size_t buflen);
|
||||
static ssize_t lis3dsh_write(FAR struct file *filep, FAR const char *buffer,
|
||||
size_t buflen);
|
||||
static int lis3dsh_ioctl(FAR struct file *filep, int cmd, unsigned long arg);
|
||||
|
||||
/****************************************************************************
|
||||
* Private Data
|
||||
****************************************************************************/
|
||||
|
||||
static const struct file_operations g_lis3dsh_fops =
|
||||
{
|
||||
lis3dsh_open,
|
||||
lis3dsh_close,
|
||||
lis3dsh_read,
|
||||
lis3dsh_write,
|
||||
NULL,
|
||||
lis3dsh_ioctl
|
||||
#ifndef CONFIG_DISABLE_POLL
|
||||
, NULL
|
||||
#endif
|
||||
#ifndef CONFIG_DISABLE_PSEUDOFS_OPERATIONS
|
||||
, NULL
|
||||
#endif
|
||||
};
|
||||
|
||||
/* Single linked list to store instances of drivers */
|
||||
|
||||
static struct lis3dsh_dev_s *g_lis3dsh_list = NULL;
|
||||
|
||||
/****************************************************************************
|
||||
* Private Functions
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Name: lis3dsh_read_register
|
||||
****************************************************************************/
|
||||
|
||||
static void lis3dsh_read_register(FAR struct lis3dsh_dev_s *dev,
|
||||
uint8_t const reg_addr, uint8_t * reg_data)
|
||||
{
|
||||
/* Lock the SPI bus so that only one device can access it at the same time */
|
||||
|
||||
SPI_LOCK(dev->spi, true);
|
||||
|
||||
/* Set CS to low which selects the LIS3DSH */
|
||||
|
||||
SPI_SELECT(dev->spi, dev->config->spi_devid, true);
|
||||
|
||||
/* Transmit the register address from where we want to read - the MSB needs
|
||||
* to be set to indicate the read indication.
|
||||
*/
|
||||
|
||||
SPI_SEND(dev->spi, reg_addr | 0x80);
|
||||
|
||||
/* Write an idle byte while receiving the required data */
|
||||
|
||||
*reg_data = (uint8_t) (SPI_SEND(dev->spi, 0));
|
||||
|
||||
/* Set CS to high which deselects the LIS3DSH */
|
||||
|
||||
SPI_SELECT(dev->spi, dev->config->spi_devid, false);
|
||||
|
||||
/* Unlock the SPI bus */
|
||||
|
||||
SPI_LOCK(dev->spi, false);
|
||||
}
|
||||
|
||||
/****************************************************************************
|
||||
* Name: lis3dsh_write_register
|
||||
****************************************************************************/
|
||||
|
||||
static void lis3dsh_write_register(FAR struct lis3dsh_dev_s *dev,
|
||||
uint8_t const reg_addr,
|
||||
uint8_t const reg_data)
|
||||
{
|
||||
/* Lock the SPI bus so that only one device can access it at the same time */
|
||||
|
||||
SPI_LOCK(dev->spi, true);
|
||||
|
||||
/* Set CS to low which selects the LIS3DSH */
|
||||
|
||||
SPI_SELECT(dev->spi, dev->config->spi_devid, true);
|
||||
|
||||
/* Transmit the register address from where we want to read */
|
||||
|
||||
SPI_SEND(dev->spi, reg_addr);
|
||||
|
||||
/* Transmit the content which should be written in the register */
|
||||
|
||||
SPI_SEND(dev->spi, reg_data);
|
||||
|
||||
/* Set CS to high which deselects the LIS3DSH */
|
||||
|
||||
SPI_SELECT(dev->spi, dev->config->spi_devid, false);
|
||||
|
||||
/* Unlock the SPI bus */
|
||||
|
||||
SPI_LOCK(dev->spi, false);
|
||||
}
|
||||
|
||||
/****************************************************************************
|
||||
* Name: lis3dsh_reset
|
||||
****************************************************************************/
|
||||
|
||||
static void lis3dsh_reset(FAR struct lis3dsh_dev_s *dev)
|
||||
{
|
||||
lis3dsh_write_register(dev, LIS3DSH_CTRL_REG_6, LIS3DSH_CTRL_REG_6_BOOT_bm);
|
||||
|
||||
up_mdelay(100);
|
||||
}
|
||||
|
||||
/****************************************************************************
|
||||
* Name: lis3dsh_read_measurement_data
|
||||
****************************************************************************/
|
||||
|
||||
static void lis3dsh_read_measurement_data(FAR struct lis3dsh_dev_s *dev)
|
||||
{
|
||||
uint16_t x_acc = 0;
|
||||
uint16_t y_acc = 0;
|
||||
uint16_t z_acc = 0;
|
||||
int ret;
|
||||
|
||||
/* Read acclerometer data */
|
||||
|
||||
lis3dsh_read_acclerometer_data(dev, &x_acc, &y_acc, &z_acc);
|
||||
|
||||
/* Aquire the semaphore before the data is copied */
|
||||
|
||||
ret = sem_wait(&dev->datasem);
|
||||
if (ret != OK)
|
||||
{
|
||||
snerr("Could not aquire dev->datasem: %d\n", ret);
|
||||
return;
|
||||
}
|
||||
|
||||
/* Copy retrieve data to internal data structure */
|
||||
|
||||
dev->data.x_acc = (int16_t)x_acc;
|
||||
dev->data.y_acc = (int16_t)y_acc;
|
||||
dev->data.z_acc = (int16_t)z_acc;
|
||||
|
||||
/* Give back the semaphore */
|
||||
|
||||
sem_post(&dev->datasem);
|
||||
}
|
||||
|
||||
/****************************************************************************
|
||||
* Name: lis3dsh_read_acclerometer_data
|
||||
****************************************************************************/
|
||||
|
||||
static void lis3dsh_read_acclerometer_data(FAR struct lis3dsh_dev_s *dev,
|
||||
uint16_t * x_acc, uint16_t * y_acc,
|
||||
uint16_t * z_acc)
|
||||
{
|
||||
/* Lock the SPI bus so that only one device can access it at the same time */
|
||||
|
||||
SPI_LOCK(dev->spi, true);
|
||||
|
||||
/* Set CS to low which selects the LIS3DSH */
|
||||
|
||||
SPI_SELECT(dev->spi, dev->config->spi_devid, true);
|
||||
|
||||
/* Transmit the register address from where we want to start reading 0x80 ->
|
||||
* MSB is set -> Read Indication.
|
||||
*/
|
||||
|
||||
SPI_SEND(dev->spi, (LIS3DSH_OUT_X_L_REG | 0x80));
|
||||
|
||||
/* RX */
|
||||
|
||||
*x_acc = ((uint16_t) (SPI_SEND(dev->spi, 0)) << 0); /* LSB */
|
||||
*x_acc |= ((uint16_t) (SPI_SEND(dev->spi, 0)) << 8); /* MSB */
|
||||
|
||||
*y_acc = ((uint16_t) (SPI_SEND(dev->spi, 0)) << 0); /* LSB */
|
||||
*y_acc |= ((uint16_t) (SPI_SEND(dev->spi, 0)) << 8); /* MSB */
|
||||
|
||||
*z_acc = ((uint16_t) (SPI_SEND(dev->spi, 0)) << 0); /* LSB */
|
||||
*z_acc |= ((uint16_t) (SPI_SEND(dev->spi, 0)) << 8); /* MSB */
|
||||
|
||||
/* Set CS to high which deselects the LIS3DSH */
|
||||
|
||||
SPI_SELECT(dev->spi, dev->config->spi_devid, false);
|
||||
|
||||
/* Unlock the SPI bus */
|
||||
|
||||
SPI_LOCK(dev->spi, false);
|
||||
}
|
||||
|
||||
/****************************************************************************
|
||||
* Name: lis3dsh_interrupt_handler
|
||||
****************************************************************************/
|
||||
|
||||
static int lis3dsh_interrupt_handler(int irq, FAR void *context)
|
||||
{
|
||||
/* This function should be called upon a rising edge on the LIS3DSH new data
|
||||
* interrupt pin since it signals that new data has been measured.
|
||||
*/
|
||||
|
||||
FAR struct lis3dsh_dev_s *priv = NULL;
|
||||
int ret;
|
||||
|
||||
/* Find out which device caused the interrupt */
|
||||
|
||||
for (priv = g_lis3dsh_list; priv && priv->config->irq != irq;
|
||||
priv = priv->flink);
|
||||
DEBUGASSERT(priv != NULL);
|
||||
|
||||
/* Task the worker with retrieving the latest sensor data. We should not do
|
||||
* this in a interrupt since it might take too long. Also we cannot lock the
|
||||
* SPI bus from within an interrupt.
|
||||
*/
|
||||
|
||||
DEBUGASSERT(priv->work.worker == NULL);
|
||||
ret = work_queue(HPWORK, &priv->work, lis3dsh_worker, priv, 0);
|
||||
if (ret < 0)
|
||||
{
|
||||
snerr("Failed to queue work: %d\n", ret);
|
||||
return ret;
|
||||
}
|
||||
|
||||
return OK;
|
||||
}
|
||||
|
||||
/****************************************************************************
|
||||
* Name: lis3dsh_worker
|
||||
****************************************************************************/
|
||||
|
||||
static void lis3dsh_worker(FAR void *arg)
|
||||
{
|
||||
FAR struct lis3dsh_dev_s *priv = (FAR struct lis3dsh_dev_s *)(arg);
|
||||
DEBUGASSERT(priv != NULL);
|
||||
|
||||
/* Read out the latest sensor data */
|
||||
|
||||
lis3dsh_read_measurement_data(priv);
|
||||
}
|
||||
|
||||
/****************************************************************************
|
||||
* Name: lis3dsh_open
|
||||
****************************************************************************/
|
||||
|
||||
static int lis3dsh_open(FAR struct file *filep)
|
||||
{
|
||||
FAR struct inode *inode = filep->f_inode;
|
||||
FAR struct lis3dsh_dev_s *priv = inode->i_private;
|
||||
|
||||
DEBUGASSERT(priv != NULL);
|
||||
|
||||
/* Perform a reset */
|
||||
|
||||
lis3dsh_reset(priv);
|
||||
|
||||
/* Enable - the full scale range (FS = +/- 16 g) */
|
||||
|
||||
lis3dsh_write_register(priv,
|
||||
LIS3DSH_CTRL_REG_5, LIS3DSH_CTRL_REG_5_FSCALE_2_bm);
|
||||
|
||||
/* Enable - Auto increment of address when reading multiple bytes */
|
||||
|
||||
lis3dsh_write_register(priv,
|
||||
LIS3DSH_CTRL_REG_6, LIS3DSH_CTRL_REG_6_ADD_INC_bm);
|
||||
|
||||
/* Enable - Measurement of X-, Y-, and Z-axis - Block data update for
|
||||
* accelerating data This should prevent race conditions when reading sensor
|
||||
* data - fastest output data rate (ODR = 1600 Hz) */
|
||||
|
||||
lis3dsh_write_register(priv,
|
||||
LIS3DSH_CTRL_REG_4,
|
||||
LIS3DSH_CTRL_REG_4_XEN_bm | LIS3DSH_CTRL_REG_4_YEN_bm |
|
||||
LIS3DSH_CTRL_REG_4_ZEN_bm | LIS3DSH_CTRL_REG_4_BDU_bm |
|
||||
LIS3DSH_CTRL_REG_4_ODR_3_bm | LIS3DSH_CTRL_REG_4_ODR_0_bm);
|
||||
|
||||
/* Enable - DRDY signal enable to INT 1 */
|
||||
|
||||
lis3dsh_write_register(priv,
|
||||
LIS3DSH_CTRL_REG_3,
|
||||
LIS3DSH_CTRL_REG_3_DR_EN_bm | LIS3DSH_CTRL_REG_3_IEA_bm |
|
||||
LIS3DSH_CTRL_REG_3_IEL_bm | LIS3DSH_CTRL_REG_3_INT1_EN_bm);
|
||||
|
||||
/* Read back the content of all control registers for debug purposes */
|
||||
|
||||
{
|
||||
uint8_t reg_content = 0;
|
||||
|
||||
lis3dsh_read_register(priv, LIS3DSH_CTRL_REG_1, ®_content);
|
||||
snerr("LIS3DSH_CTRL_REG_1 = %04x\n", reg_content);
|
||||
lis3dsh_read_register(priv, LIS3DSH_CTRL_REG_2, ®_content);
|
||||
snerr("LIS3DSH_CTRL_REG_2 = %04x\n", reg_content);
|
||||
lis3dsh_read_register(priv, LIS3DSH_CTRL_REG_3, ®_content);
|
||||
snerr("LIS3DSH_CTRL_REG_3 = %04x\n", reg_content);
|
||||
lis3dsh_read_register(priv, LIS3DSH_CTRL_REG_4, ®_content);
|
||||
snerr("LIS3DSH_CTRL_REG_4 = %04x\n", reg_content);
|
||||
lis3dsh_read_register(priv, LIS3DSH_CTRL_REG_5, ®_content);
|
||||
snerr("LIS3DSH_CTRL_REG_5 = %04x\n", reg_content);
|
||||
lis3dsh_read_register(priv, LIS3DSH_CTRL_REG_6, ®_content);
|
||||
snerr("LIS3DSH_CTRL_REG_6 = %04x\n", reg_content);
|
||||
lis3dsh_read_register(priv, LIS3DSH_STATUS_REG, ®_content);
|
||||
snerr("STATUS_REG = %04x\n", reg_content);
|
||||
}
|
||||
|
||||
return OK;
|
||||
}
|
||||
|
||||
/****************************************************************************
|
||||
* Name: lis3dsh_close
|
||||
****************************************************************************/
|
||||
|
||||
static int lis3dsh_close(FAR struct file *filep)
|
||||
{
|
||||
FAR struct inode *inode = filep->f_inode;
|
||||
FAR struct lis3dsh_dev_s *priv = inode->i_private;
|
||||
|
||||
DEBUGASSERT(priv != NULL);
|
||||
|
||||
/* Perform a reset */
|
||||
|
||||
lis3dsh_reset(priv);
|
||||
|
||||
return OK;
|
||||
}
|
||||
|
||||
/****************************************************************************
|
||||
* Name: lis3dsh_read
|
||||
****************************************************************************/
|
||||
|
||||
static ssize_t lis3dsh_read(FAR struct file *filep, FAR char *buffer,
|
||||
size_t buflen)
|
||||
{
|
||||
FAR struct inode *inode = filep->f_inode;
|
||||
FAR struct lis3dsh_dev_s *priv = inode->i_private;
|
||||
FAR struct lis3dsh_sensor_data_s *data;
|
||||
int ret;
|
||||
|
||||
DEBUGASSERT(priv != NULL);
|
||||
|
||||
/* Check if enough memory was provided for the read call */
|
||||
|
||||
if (buflen < sizeof(FAR struct lis3dsh_sensor_data_s))
|
||||
{
|
||||
snerr("Not enough memory for reading out a sensor data sample\n");
|
||||
return -ENOSYS;
|
||||
}
|
||||
|
||||
/* Aquire the semaphore before the data is copied */
|
||||
|
||||
ret = sem_wait(&priv->datasem);
|
||||
if (ret != OK)
|
||||
{
|
||||
snerr("Could not aquire priv->datasem: %d\n", ret);
|
||||
return ERROR;
|
||||
}
|
||||
|
||||
/* Copy the sensor data into the buffer */
|
||||
|
||||
data = (FAR struct lis3dsh_sensor_data_s *)buffer;
|
||||
memset(data, 0, sizeof(FAR struct lis3dsh_sensor_data_s));
|
||||
|
||||
data->x_acc = priv->data.x_acc;
|
||||
data->y_acc = priv->data.y_acc;
|
||||
data->z_acc = priv->data.z_acc;
|
||||
|
||||
/* Give back the semaphore */
|
||||
|
||||
sem_post(&priv->datasem);
|
||||
|
||||
return sizeof(FAR struct lis3dsh_sensor_data_s);
|
||||
}
|
||||
|
||||
/****************************************************************************
|
||||
* Name: lis3dsh_write
|
||||
****************************************************************************/
|
||||
|
||||
static ssize_t lis3dsh_write(FAR struct file *filep, FAR const char *buffer,
|
||||
size_t buflen)
|
||||
{
|
||||
return -ENOSYS;
|
||||
}
|
||||
|
||||
/****************************************************************************
|
||||
* Name: lis3dsh_ioctl
|
||||
****************************************************************************/
|
||||
|
||||
static int lis3dsh_ioctl(FAR struct file *filep, int cmd, unsigned long arg)
|
||||
{
|
||||
int ret = OK;
|
||||
|
||||
switch (cmd)
|
||||
{
|
||||
/* Command was not recognized */
|
||||
|
||||
default:
|
||||
snerr("Unrecognized cmd: %d\n", cmd);
|
||||
ret = -ENOTTY;
|
||||
break;
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
/****************************************************************************
|
||||
* Public Functions
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Name: lis3dsh_register
|
||||
*
|
||||
* Description:
|
||||
* Register the LIS3DSH character device as 'devpath'
|
||||
*
|
||||
* Input Parameters:
|
||||
* devpath - The full path to the driver to register. E.g., "/dev/acc0"
|
||||
* spi - An instance of the SPI interface to use to communicate with
|
||||
* LIS3DSH
|
||||
* config - configuration for the LIS3DSH driver.
|
||||
*
|
||||
* Returned Value:
|
||||
* Zero (OK) on success; a negated errno value on failure.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
int lis3dsh_register(FAR const char *devpath, FAR struct spi_dev_s *spi,
|
||||
FAR struct lis3dsh_config_s *config)
|
||||
{
|
||||
FAR struct lis3dsh_dev_s *priv;
|
||||
int ret;
|
||||
|
||||
/* Sanity check */
|
||||
|
||||
DEBUGASSERT(spi != NULL);
|
||||
DEBUGASSERT(config != NULL);
|
||||
|
||||
/* Initialize the LIS3DSH device structure */
|
||||
|
||||
priv = (FAR struct lis3dsh_dev_s *)kmm_malloc(sizeof(struct lis3dsh_dev_s));
|
||||
if (priv == NULL)
|
||||
{
|
||||
snerr("Failed to allocate instance\n");
|
||||
return -ENOMEM;
|
||||
}
|
||||
|
||||
priv->spi = spi;
|
||||
priv->config = config;
|
||||
priv->work.worker = NULL;
|
||||
|
||||
sem_init(&priv->datasem, 0, 1); /* Initialize sensor data access
|
||||
* semaphore */
|
||||
|
||||
/* Setup SPI frequency and mode */
|
||||
|
||||
SPI_SETFREQUENCY(spi, LIS3DSH_SPI_FREQUENCY);
|
||||
SPI_SETMODE(spi, LIS3DSH_SPI_MODE);
|
||||
|
||||
/* Register the character driver */
|
||||
|
||||
ret = register_driver(devpath, &g_lis3dsh_fops, 0666, priv);
|
||||
if (ret < 0)
|
||||
{
|
||||
snerr("Failed to register driver: %d\n", ret);
|
||||
kmm_free(priv);
|
||||
sem_destroy(&priv->datasem);
|
||||
return -ENODEV;
|
||||
}
|
||||
|
||||
/* Since we support multiple LIS3DSH devices, we will need to add this new
|
||||
* instance to a list of device instances so that it can be found by the
|
||||
* interrupt handler based on the received IRQ number.
|
||||
*/
|
||||
|
||||
priv->flink = g_lis3dsh_list;
|
||||
g_lis3dsh_list = priv;
|
||||
|
||||
/* Attach the interrupt handler */
|
||||
|
||||
ret = priv->config->attach(priv->config, &lis3dsh_interrupt_handler);
|
||||
if (ret < 0)
|
||||
{
|
||||
snerr("Failed to attach interrupt\n");
|
||||
return -ENODEV;
|
||||
}
|
||||
|
||||
return OK;
|
||||
}
|
||||
|
||||
#endif /* CONFIG_SPI && CONFIG_LIS3DSH */
|
||||
277
include/nuttx/sensors/lis3dsh.h
Normal file
277
include/nuttx/sensors/lis3dsh.h
Normal file
|
|
@ -0,0 +1,277 @@
|
|||
/****************************************************************************
|
||||
* include/nuttx/sensors/lis3dsh.h
|
||||
*
|
||||
* Copyright (C) 2016 DS-Automotion GmbH. All rights reserved.
|
||||
* Author: Alexander Entinger <a.entinger@ds-automotion.com>
|
||||
* Thomas Ilk
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
#ifndef NUTTX_INCLUDE_NUTTX_SENSORS_LIS3DSH_H_
|
||||
#define NUTTX_INCLUDE_NUTTX_SENSORS_LIS3DSH_H_
|
||||
|
||||
/****************************************************************************
|
||||
* Included Files
|
||||
****************************************************************************/
|
||||
|
||||
#include <nuttx/irq.h>
|
||||
#include <nuttx/config.h>
|
||||
#include <nuttx/fs/ioctl.h>
|
||||
#include <nuttx/spi/spi.h>
|
||||
|
||||
#if defined(CONFIG_SPI) && defined(CONFIG_LIS3DSH)
|
||||
|
||||
/****************************************************************************
|
||||
* Pre-processor Definitions
|
||||
****************************************************************************/
|
||||
|
||||
/* LIS3DSH Register Definitions **********************************************/
|
||||
|
||||
#define LIS3DSH_INFO_REG_1 (0x0D)
|
||||
#define LIS3DSH_INFO_REG_2 (0x0E)
|
||||
#define LIS3DSH_WHO_AM_I_REG (0x0F)
|
||||
#define LIS3DSH_CTRL_REG_1 (0x21)
|
||||
#define LIS3DSH_CTRL_REG_2 (0x22)
|
||||
#define LIS3DSH_CTRL_REG_3 (0x23)
|
||||
#define LIS3DSH_CTRL_REG_4 (0x20)
|
||||
#define LIS3DSH_CTRL_REG_5 (0x24)
|
||||
#define LIS3DSH_CTRL_REG_6 (0x25)
|
||||
#define LIS3DSH_STATUS_REG (0x27)
|
||||
#define LIS3DSH_OUT_T_REG (0x0C)
|
||||
#define LIS3DSH_OFF_X_REG (0x10)
|
||||
#define LIS3DSH_OFF_Y_REG (0x11)
|
||||
#define LIS3DSH_OFF_Z_REG (0x12)
|
||||
#define LIS3DSH_CS_X_REG (0x13)
|
||||
#define LIS3DSH_CS_Y_REG (0x14)
|
||||
#define LIS3DSH_CS_Z_REG (0x15)
|
||||
#define LIS3DSH_LC_L_REG (0x16)
|
||||
#define LIS3DSH_LC_H_REG (0x17)
|
||||
#define LIS3DSH_STAT_REG (0x18)
|
||||
#define LIS3DSH_PEAK1_REG (0x19)
|
||||
#define LIS3DSH_PEAK2_REG (0x1A)
|
||||
#define LIS3DSH_VFC_REG_1 (0x1B)
|
||||
#define LIS3DSH_VFC_REG_2 (0x1C)
|
||||
#define LIS3DSH_VFC_REG_3 (0x1D)
|
||||
#define LIS3DSH_VFC_REG_4 (0x1E)
|
||||
#define LIS3DSH_THRS3_REG (0x1F)
|
||||
#define LIS3DSH_OUT_X_L_REG (0x28)
|
||||
#define LIS3DSH_OUT_X_H_REG (0x29)
|
||||
#define LIS3DSH_OUT_Y_L_REG (0x2A)
|
||||
#define LIS3DSH_OUT_Y_H_REG (0x2B)
|
||||
#define LIS3DSH_OUT_Z_L_REG (0x2C)
|
||||
#define LIS3DSH_OUT_Z_H_REG (0x2D)
|
||||
#define LIS3DSH_FIFO_CTRL_REG (0x2E)
|
||||
#define LIS3DSH_FIFO_SRC_REG (0x2F)
|
||||
#define LIS3DSH_FIFO_CTRL_REG (0x2E)
|
||||
#define LIS3DSH_ST1_REG_1 (0x40)
|
||||
#define LIS3DSH_ST1_REG_2 (0x41)
|
||||
#define LIS3DSH_ST1_REG_3 (0x42)
|
||||
#define LIS3DSH_ST1_REG_4 (0x43)
|
||||
#define LIS3DSH_ST1_REG_5 (0x44)
|
||||
#define LIS3DSH_ST1_REG_6 (0x45)
|
||||
#define LIS3DSH_ST1_REG_7 (0x46)
|
||||
#define LIS3DSH_ST1_REG_8 (0x47)
|
||||
#define LIS3DSH_ST1_REG_9 (0x48)
|
||||
#define LIS3DSH_ST1_REG_10 (0x49)
|
||||
#define LIS3DSH_ST1_REG_11 (0x4A)
|
||||
#define LIS3DSH_ST1_REG_12 (0x4B)
|
||||
#define LIS3DSH_ST1_REG_13 (0x4C)
|
||||
#define LIS3DSH_ST1_REG_14 (0x4D)
|
||||
#define LIS3DSH_ST1_REG_15 (0x4E)
|
||||
#define LIS3DSH_ST1_REG_16 (0x4F)
|
||||
#define LIS3DSH_TIM4_1_REG (0x50)
|
||||
#define LIS3DSH_TIM3_1_REG (0x51)
|
||||
#define LIS3DSH_TIM2_1_REG_1 (0x52)
|
||||
#define LIS3DSH_TIM2_1_REG_2 (0x53)
|
||||
#define LIS3DSH_TIM1_1_REG_1 (0x54)
|
||||
#define LIS3DSH_TIM1_1_REG_5 (0x55)
|
||||
#define LIS3DSH_THRS2_1_REG (0x56)
|
||||
#define LIS3DSH_THRS1_1_REG (0x57)
|
||||
#define LIS3DSH_MASK1_B_REG (0x59)
|
||||
#define LIS3DSH_MASK1_A_REG (0x5A)
|
||||
#define LIS3DSH_SETT1_REG (0x5B)
|
||||
#define LIS3DSH_PR1_REG (0x5C)
|
||||
#define LIS3DSH_TC1_REG_1 (0x5D)
|
||||
#define LIS3DSH_TC1_REG_2 (0x5E)
|
||||
#define LIS3DSH_OUTS1_REG (0x5F)
|
||||
#define LIS3DSH_ST2_REG_1 (0x60)
|
||||
#define LIS3DSH_ST2_REG_2 (0x61)
|
||||
#define LIS3DSH_ST2_REG_3 (0x62)
|
||||
#define LIS3DSH_ST2_REG_4 (0x63)
|
||||
#define LIS3DSH_ST2_REG_5 (0x64)
|
||||
#define LIS3DSH_ST2_REG_6 (0x65)
|
||||
#define LIS3DSH_ST2_REG_7 (0x66)
|
||||
#define LIS3DSH_ST2_REG_8 (0x67)
|
||||
#define LIS3DSH_ST2_REG_9 (0x68)
|
||||
#define LIS3DSH_ST2_REG_10 (0x69)
|
||||
#define LIS3DSH_ST2_REG_11 (0x6A)
|
||||
#define LIS3DSH_ST2_REG_12 (0x6B)
|
||||
#define LIS3DSH_ST2_REG_13 (0x6C)
|
||||
#define LIS3DSH_ST2_REG_14 (0x6D)
|
||||
#define LIS3DSH_ST2_REG_15 (0x6E)
|
||||
#define LIS3DSH_ST2_REG_16 (0x6F)
|
||||
#define LIS3DSH_TIM4_2_REG (0x70)
|
||||
#define LIS3DSH_TIM3_2_REG (0x71)
|
||||
#define LIS3DSH_TIM2_2_REG_1 (0x72)
|
||||
#define LIS3DSH_TIM2_2_REG_2 (0x73)
|
||||
#define LIS3DSH_TIM1_2_REG_1 (0x74)
|
||||
#define LIS3DSH_TIM1_2_REG_5 (0x75)
|
||||
#define LIS3DSH_THRS2_2_REG (0x76)
|
||||
#define LIS3DSH_THRS1_2_REG (0x77)
|
||||
#define LIS3DSH_DES2_REG (0x78)
|
||||
#define LIS3DSH_MASK2_B_REG (0x79)
|
||||
#define LIS3DSH_MASK2_A_REG (0x7A)
|
||||
#define LIS3DSH_SETT2_REG (0x7B)
|
||||
#define LIS3DSH_PR2_REG (0x7C)
|
||||
#define LIS3DSH_TC2_REG_1 (0x7D)
|
||||
#define LIS3DSH_TC2_REG_2 (0x7E)
|
||||
|
||||
/* LIS3DSH CTRL_REG_3 Definitions **********************************************/
|
||||
|
||||
#define LIS3DSH_CTRL_REG_3_DR_EN_bm (1<<7) /* DRDY signal enable to INT 1 */
|
||||
#define LIS3DSH_CTRL_REG_3_IEA_bm (1<<6) /* Interrupt signal polarity */
|
||||
#define LIS3DSH_CTRL_REG_3_IEL_bm (1<<5) /* Interrupt signal latching */
|
||||
#define LIS3DSH_CTRL_REG_3_INT2_EN_bm (1<<4) /* Interrupt 2 enable / disable */
|
||||
#define LIS3DSH_CTRL_REG_3_INT1_EN_bm (1<<3) /* Interrupt 1 enable / disable */
|
||||
#define LIS3DSH_CTRL_REG_3_VFILT_bm (1<<2) /* Vector filter enable / disable */
|
||||
#define LIS3DSH_CTRL_REG_3_STRT_bm (1<<0) /* Enable soft reset */
|
||||
|
||||
/* LIS3DSH CTRL_REG_4 Definitions **********************************************/
|
||||
|
||||
#define LIS3DSH_CTRL_REG_4_ODR_3_bm (1<<7) /* Output data rate and power mode selection bit 3 */
|
||||
#define LIS3DSH_CTRL_REG_4_ODR_2_bm (1<<6) /* Output data rate and power mode selection bit 2 */
|
||||
#define LIS3DSH_CTRL_REG_4_ODR_1_bm (1<<5) /* Output data rate and power mode selection bit 1 */
|
||||
#define LIS3DSH_CTRL_REG_4_ODR_0_bm (1<<4) /* Output data rate and power mode selection bit 0 */
|
||||
#define LIS3DSH_CTRL_REG_4_BDU_bm (1<<3) /* Enable block data update for accelerating data */
|
||||
#define LIS3DSH_CTRL_REG_4_ZEN_bm (1<<2) /* Enable Z-axis */
|
||||
#define LIS3DSH_CTRL_REG_4_YEN_bm (1<<1) /* Enable Y-axis */
|
||||
#define LIS3DSH_CTRL_REG_4_XEN_bm (1<<0) /* Enable X-axis */
|
||||
|
||||
/* LIS3DSH CTRL_REG_5 Definitions **********************************************/
|
||||
|
||||
#define LIS3DSH_CTRL_REG_5_BW_2_bm (1<<7) /* Anti-aliasing filter bandwidth bit 2 */
|
||||
#define LIS3DSH_CTRL_REG_5_BW_1_bm (1<<6) /* Anti-aliasing filter bandwidth bit 1 */
|
||||
#define LIS3DSH_CTRL_REG_5_FSCALE_2_bm (1<<5) /* Full-scale selection bit 2 */
|
||||
#define LIS3DSH_CTRL_REG_5_FSCALE_1_bm (1<<4) /* Full-scale selection bit 1 */
|
||||
#define LIS3DSH_CTRL_REG_5_FSCALE_0_bm (1<<3) /* Full-scale selection bit 0 */
|
||||
#define LIS3DSH_CTRL_REG_5_ST_2_bm (1<<2) /* Enable self-test bit 2 */
|
||||
#define LIS3DSH_CTRL_REG_5_ST_1_bm (1<<1) /* Enable self-test bit 1 */
|
||||
#define LIS3DSH_CTRL_REG_5_SIM_bm (1<<0) /* Enable SPI 4-wire interface */
|
||||
|
||||
/* LIS3DSH CTRL_REG_6 Definitions **********************************************/
|
||||
|
||||
#define LIS3DSH_CTRL_REG_6_BOOT_bm (1<<7) /* Force reboot, cleared as soon as the reboot is finished. Active high */
|
||||
#define LIS3DSH_CTRL_REG_6_FIFO_EN_bm (1<<6) /* Enable FIFO */
|
||||
#define LIS3DSH_CTRL_REG_6_WTM_EN_bm (1<<5) /* Enable FIFO watermark level use */
|
||||
#define LIS3DSH_CTRL_REG_6_ADD_INC_bm (1<<4) /* Register address automatically incremented during a multiple byte access with a serial interface */
|
||||
#define LIS3DSH_CTRL_REG_6_P1_EMPTY_bm (1<<3) /* Enable FIFO empty indication on Int1 */
|
||||
#define LIS3DSH_CTRL_REG_6_P1_WTM_bm (1<<2) /* FIFO watermark interrupt Int1 */
|
||||
#define LIS3DSH_CTRL_REG_6_P1_OVERRUN_bm (1<<1) /* FIFO overrrun interrupt on Int1 */
|
||||
#define LIS3DSH_CTRL_REG_6_P2_BOOT_bm (1<<0) /* BOOT interrupt on Int2 */
|
||||
|
||||
/* SPI BUS PARAMETERS *******************************************************/
|
||||
|
||||
#define LIS3DSH_SPI_FREQUENCY (5000000) /* 5 MHz */
|
||||
#define LIS3DSH_SPI_MODE (SPIDEV_MODE3) /* Device uses SPI Mode 3: CPOL=1, CPHA=1 */
|
||||
|
||||
/****************************************************************************
|
||||
* Public Types
|
||||
****************************************************************************/
|
||||
|
||||
/* A reference to a structure of this type must be passed to the LIS3DSH
|
||||
* driver. This structure provides information about the configuration
|
||||
* of the sensor and provides some board-specific hooks.
|
||||
*
|
||||
* Memory for this structure is provided by the caller. It is not copied
|
||||
* by the driver and is presumed to persist while the driver is active.
|
||||
*/
|
||||
|
||||
struct lis3dsh_config_s
|
||||
{
|
||||
/* Since multiple sensors can be connected to the same SPI bus we need
|
||||
* to use multiple spi device ids which are employed by NuttX to select/
|
||||
* deselect the desired LIS3DSH chip via their chip select inputs.
|
||||
*/
|
||||
|
||||
int spi_devid;
|
||||
|
||||
/* The IRQ number must be provided for each LIS3DSH device so that
|
||||
* their interrupts can be distinguished.
|
||||
*/
|
||||
|
||||
int irq;
|
||||
|
||||
/* Attach the LIS3DSH interrupt handler to the GPIO interrupt of the
|
||||
* concrete LIS3DSH instance.
|
||||
*/
|
||||
|
||||
int (*attach)(FAR struct lis3dsh_config_s *, xcpt_t);
|
||||
};
|
||||
|
||||
/****************************************************************************
|
||||
* Public Function Prototypes
|
||||
****************************************************************************/
|
||||
|
||||
#ifdef __cplusplus
|
||||
#define EXTERN extern "C"
|
||||
extern "C"
|
||||
{
|
||||
#else
|
||||
#define EXTERN extern
|
||||
#endif
|
||||
|
||||
/****************************************************************************
|
||||
* Name: lis3dsh_register
|
||||
*
|
||||
* Description:
|
||||
* Register the LIS3DSH character device as 'devpath'
|
||||
*
|
||||
* Input Parameters:
|
||||
* devpath - The full path to the driver to register. E.g., "/dev/acc0"
|
||||
* spi - An instance of the SPI interface to use to communicate with
|
||||
* LIS3DSH
|
||||
* config - configuration for the LIS3DSH driver. For details see
|
||||
* description above.
|
||||
*
|
||||
* Returned Value:
|
||||
* Zero (OK) on success; a negated errno value on failure.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
int lis3dsh_register(FAR const char *devpath, FAR struct spi_dev_s *spi,
|
||||
FAR struct lis3dsh_config_s *config);
|
||||
|
||||
#undef EXTERN
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* CONFIG_SPI && CONFIG_LIS3DSH */
|
||||
#endif /* NUTTX_INCLUDE_NUTTX_SENSORS_LIS3DSH_H_ */
|
||||
Loading…
Add table
Add a link
Reference in a new issue