diff --git a/drivers/sensors/Kconfig b/drivers/sensors/Kconfig index 3eb524dc52a..b04e4224956 100644 --- a/drivers/sensors/Kconfig +++ b/drivers/sensors/Kconfig @@ -160,6 +160,13 @@ config LIS3DSH ---help--- Enable driver support for the STMicro LIS3DSH 3-Axis accelerometer. +config LIS3DH + bool "STMicro LIS3DH 3-Axis accelerometer support" + default n + select SPI + ---help--- + Enable driver support for the STMicro LIS3DH 3-Axis accelerometer. + config LIS331DL bool "STMicro LIS331DL device support" default n diff --git a/drivers/sensors/Make.defs b/drivers/sensors/Make.defs index c4cfe3af0da..5b27819f8d7 100644 --- a/drivers/sensors/Make.defs +++ b/drivers/sensors/Make.defs @@ -146,6 +146,10 @@ ifeq ($(CONFIG_LIS3DSH),y) CSRCS += lis3dsh.c endif +ifeq ($(CONFIG_LIS3DH),y) + CSRCS += lis3dh.c +endif + ifeq ($(CONFIG_SENSORS_MAX31855),y) CSRCS += max31855.c endif diff --git a/drivers/sensors/lis3dh.c b/drivers/sensors/lis3dh.c new file mode 100644 index 00000000000..2055a7c6824 --- /dev/null +++ b/drivers/sensors/lis3dh.c @@ -0,0 +1,1032 @@ +/**************************************************************************** + * drivers/sensors/lis3dh.c + * + * Copyright (C) 2018 Extent3D. All rights reserved. + * Author: Matt Thompson + * + * 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 + +#include +#include +#include +#include + +#include +#include +#include + +#include +#include +#include + +#if defined(CONFIG_SPI) && defined(CONFIG_LIS3DH) + +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +#define LIS3DH_QUEUE_MAX 32 +#define LIS3DH_FIFOBUF_SIZE ((32 * 6) + 1) + +/**************************************************************************** + * Private Types + ****************************************************************************/ + +struct lis3dh_dev_s +{ + FAR struct lis3dh_config_s *config; /* Driver configuration */ + FAR struct spi_dev_s *spi; /* Pointer to the SPI instance */ + struct work_s work; /* Work Queue */ + uint8_t power_mode; /* The power mode used to determine mg/digit */ + uint8_t odr; /* The current output data rate */ + sem_t readsem; /* Read notification semaphore */ + uint8_t fifobuf[LIS3DH_FIFOBUF_SIZE]; /* Raw FIFO buffer */ + struct lis3dh_sensor_data_s queue[LIS3DH_QUEUE_MAX]; + sem_t queuesem; /* Queue exclusive lock */ + uint8_t queue_rpos; /* Queue read position */ + uint8_t queue_wpos; /* Queue write position */ + uint8_t queue_count; /* Number of elements in the queue */ +}; + +struct lis3dh_sample_s +{ + int16_t x; + int16_t y; + int16_t z; +}; + +/**************************************************************************** + * Private Function Prototypes + ****************************************************************************/ + +static void lis3dh_read_register(FAR struct lis3dh_dev_s *dev, + uint8_t const reg_addr, uint8_t *reg_data); +static void lis3dh_write_register(FAR struct lis3dh_dev_s *dev, + uint8_t const reg_addr, + uint8_t const reg_data); +static void lis3dh_reset(FAR struct lis3dh_dev_s *dev); +static int lis3dh_ident(FAR struct lis3dh_dev_s *dev); +static int lis3dh_read_fifo(FAR struct lis3dh_dev_s *dev); +static int lis3dh_interrupt_handler(int irq, FAR void *context, FAR void *arg); +static void lis3dh_worker(FAR void *arg); +static int lis3dh_irq_enable(FAR struct lis3dh_dev_s *dev, bool enable); +static int lis3dh_fifo_enable(FAR struct lis3dh_dev_s *dev); + +static int lis3dh_open(FAR struct file *filep); +static int lis3dh_close(FAR struct file *filep); +static ssize_t lis3dh_read(FAR struct file *, FAR char *buffer, + size_t buflen); +static ssize_t lis3dh_write(FAR struct file *filep, FAR const char *buffer, + size_t buflen); +static int lis3dh_ioctl(FAR struct file *filep, int cmd, unsigned long arg); + +/**************************************************************************** + * Private Data + ****************************************************************************/ + +static const struct file_operations g_lis3dh_fops = +{ + lis3dh_open, + lis3dh_close, + lis3dh_read, + lis3dh_write, + NULL, + lis3dh_ioctl +#ifndef CONFIG_DISABLE_POLL + , NULL +#endif +#ifndef CONFIG_DISABLE_PSEUDOFS_OPERATIONS + , NULL +#endif +}; + +/**************************************************************************** + * Public Data + ****************************************************************************/ + +/**************************************************************************** + * Private Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: lis3dh_read_register + * + * Description: + * Read a single register from the LIS3DH sensor. + * + * Input Parameters: + * dev - Pointer to device driver instance + * reg_addr - LIS3DH register address + * reg_data - Pointer to uint8_t where read value will be stored + * + ****************************************************************************/ + +static void lis3dh_read_register(FAR struct lis3dh_dev_s *dev, + uint8_t const reg_addr, uint8_t * reg_data) +{ + uint8_t buffer[2]; + + /* 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 LIS3DH */ + + SPI_SELECT(dev->spi, dev->config->spi_devid, true); + + buffer[0] = reg_addr | 0x80; + buffer[1] = 0; + SPI_EXCHANGE(dev->spi, buffer, buffer, 2); + *reg_data = buffer[1]; + + /* Set CS to high which deselects the LIS3DH */ + + SPI_SELECT(dev->spi, dev->config->spi_devid, false); + + /* Unlock the SPI bus */ + + SPI_LOCK(dev->spi, false); +} + +/**************************************************************************** + * Name: lis3dh_write_register + * + * Description: + * Write a single register to the LIS3DH sensor. + * + * Input Parameters: + * dev - Pointer to device driver instance + * reg_addr - LIS3DH register address + * reg_data - Value to write into the specified register address + * + ****************************************************************************/ + +static void lis3dh_write_register(FAR struct lis3dh_dev_s *dev, + uint8_t const reg_addr, + uint8_t const reg_data) +{ + uint8_t buffer[2]; + + /* 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 LIS3DH */ + + SPI_SELECT(dev->spi, dev->config->spi_devid, true); + + buffer[0] = reg_addr; + buffer[1] = reg_data; + SPI_EXCHANGE(dev->spi, buffer, buffer, 2); + + /* Set CS to high which deselects the LIS3DH */ + + SPI_SELECT(dev->spi, dev->config->spi_devid, false); + + /* Unlock the SPI bus */ + + SPI_LOCK(dev->spi, false); +} + +/**************************************************************************** + * Name: lis3dh_reset + * + * Description: + * Perform a software reset of the LIS3DH sensor. + * + * Input Parameters: + * dev - Pointer to device driver instance + * + ****************************************************************************/ + +static void lis3dh_reset(FAR struct lis3dh_dev_s *dev) +{ + lis3dh_write_register(dev, LIS3DH_CTRL_REG5, LIS3DH_CTRL_REG5_BOOT); + up_mdelay(100); +} + +/**************************************************************************** + * Name: lis3dh_ident + * + * Description: + * Identify an LIS3DH sensor on the SPI bus using the WHO_AM_I register. + * + * Input Parameters: + * dev - Pointer to device driver instance + * + * Returned Value: + * OK if the device responded with the correct ID + * -ENODEV if the device did not respond with the correct ID + * + ****************************************************************************/ + +static int lis3dh_ident(FAR struct lis3dh_dev_s *dev) +{ + uint8_t reg; + lis3dh_read_register(dev, LIS3DH_WHO_AM_I, ®); + + if(reg == LIS3DH_DEVICE_ID) + { + return OK; + } + return -ENODEV; +} + + +/**************************************************************************** + * Name: lis3dh_queue_lock + * + * Description: + * Locks exclusive access to the ring buffer queue + * + * Input Parameters: + * dev - Pointer to device driver instance + * + ****************************************************************************/ + +static void lis3dh_queue_lock(FAR struct lis3dh_dev_s *dev) +{ + int ret; + + ret = nxsem_wait(&dev->queuesem); + if (ret < 0) + { + snerr("ERROR: queuesem wait error: %d\n", ret); + return; + } +} + +/**************************************************************************** + * Name: lis3dh_queue_unlock + * + * Description: + * Unlocks exclusive acccess to the ring buffer queue + * + * Input Parameters: + * dev - Pointer to device driver instance + * + ****************************************************************************/ + +static void lis3dh_queue_unlock(FAR struct lis3dh_dev_s *dev) +{ + nxsem_post(&dev->queuesem); +} + +/**************************************************************************** + * Name: lis3dh_queue_push + * + * Description: + * Push a sensor measurement into the queue + * + * Input Parameters: + * dev - Pointer to device driver instance + * + ****************************************************************************/ + +static int lis3dh_queue_push(FAR struct lis3dh_dev_s *dev, + struct lis3dh_sensor_data_s *data) +{ + lis3dh_queue_lock(dev); + if(dev->queue_count >= LIS3DH_QUEUE_MAX) + { + lis3dh_queue_unlock(dev); + return -ENOMEM; + } + + dev->queue_wpos++; + dev->queue[dev->queue_wpos % LIS3DH_QUEUE_MAX] = *data; + + dev->queue_count++; + lis3dh_queue_unlock(dev); + + return OK; +} + +/**************************************************************************** + * Name: lis3dh_queue_pop + * + * Description: + * Pops a sensor measurement from the queue + * + * Input Parameters: + * dev - Pointer to device driver instance + * + * Returned Value: + * OK if a measurement was successfully dequeued + * -EAGAIN if the queue is empty + * + ****************************************************************************/ + +static int lis3dh_queue_pop(FAR struct lis3dh_dev_s *dev, + struct lis3dh_sensor_data_s *data) +{ + lis3dh_queue_lock(dev); + if(dev->queue_count == 0) + { + lis3dh_queue_unlock(dev); + return -EAGAIN; + } + + dev->queue_rpos++; + *data = dev->queue[dev->queue_rpos % LIS3DH_QUEUE_MAX]; + + dev->queue_count--; + + lis3dh_queue_unlock(dev); + + return OK; +} + +/**************************************************************************** + * Name: lis3dh_read_fifo + * + * Description: + * Reads the FIFO from the LIS3DH sensor, and pushes to the internal queue. + * + * Input Parameters: + * dev - Pointer to device driver instance + * + * Returned Value: + * + ****************************************************************************/ + +static int lis3dh_read_fifo(FAR struct lis3dh_dev_s *dev) +{ + uint8_t fifosrc; + uint8_t count; + int i; + + /* Lock the SPI bus */ + + SPI_LOCK(dev->spi, true); + + /* Read the FIFO source register */ + + dev->fifobuf[0] = LIS3DH_FIFO_SRC_REG | 0x80; + dev->fifobuf[1] = 0; + + SPI_SELECT(dev->spi, dev->config->spi_devid, true); + SPI_EXCHANGE(dev->spi, dev->fifobuf, dev->fifobuf, 2); + SPI_SELECT(dev->spi, dev->config->spi_devid, false); + + fifosrc = dev->fifobuf[1]; + + count = fifosrc & 0x1f; + + if(fifosrc & LIS3DH_FIFO_SRC_REG_OVRN_FIFO) + { + snerr("FIFO overrun\n"); + } + + memset(dev->fifobuf, 0, LIS3DH_FIFOBUF_SIZE); + dev->fifobuf[0] = LIS3DH_OUT_X_L | 0xC0; + + SPI_SELECT(dev->spi, dev->config->spi_devid, true); + SPI_EXCHANGE(dev->spi, dev->fifobuf, dev->fifobuf, 1 + count*6); + SPI_SELECT(dev->spi, dev->config->spi_devid, false); + + /* Unlock the SPI bus */ + + SPI_LOCK(dev->spi, false); + + for(i=0;ififobuf[(i*6)+1] | + (uint16_t)dev->fifobuf[(i*6)+2] << 8; + + y_raw = (uint16_t)dev->fifobuf[(i*6)+3] | + (uint16_t)dev->fifobuf[(i*6)+4] << 8; + + z_raw = (uint16_t)dev->fifobuf[(i*6)+5] | + (uint16_t)dev->fifobuf[(i*6)+6] << 8; + + /* The sensor left justifies the data in the register, so we must + * shift it to the right depending on the selected power mode + */ + + switch(dev->power_mode) + { + case LIS3DH_POWER_LOW: /* 8 bit measurements */ + x_acc = (int16_t)x_raw >> 8; + y_acc = (int16_t)y_raw >> 8; + z_acc = (int16_t)z_raw >> 8; + + data.x_acc = (float)x_acc * 0.016; + data.y_acc = (float)y_acc * 0.016; + data.z_acc = (float)z_acc * 0.016; + + break; + case LIS3DH_POWER_NORMAL: /* 10 bit measurements */ + x_acc = (int16_t)x_raw >> 6; + y_acc = (int16_t)y_raw >> 6; + z_acc = (int16_t)z_raw >> 6; + + data.x_acc = (float)x_acc * 0.004; + data.y_acc = (float)y_acc * 0.004; + data.z_acc = (float)z_acc * 0.004; + + break; + case LIS3DH_POWER_HIGH: /* 12 bit measurements */ + x_acc = (int16_t)x_raw >> 4; + y_acc = (int16_t)y_raw >> 4; + z_acc = (int16_t)z_raw >> 4; + + data.x_acc = (float)x_acc * 0.001; + data.y_acc = (float)y_acc * 0.001; + data.z_acc = (float)z_acc * 0.001; + + break; + default: + snerr("Unknown power mode\n"); + return -EINVAL; + } + + if (lis3dh_queue_push(dev, &data) == OK) + { + nxsem_post(&dev->readsem); + } + } + + return OK; +} + +/**************************************************************************** + * Name: lis3dh_interrupt_handler + * + * Description: + * Interrupt service routine, queues work on high priority work queue. + * + * Input Parameters: + * irq - Interrupt request number + * context - Context pointer + * arg - Pointer to device driver instance + * + * Returned Value: + * Returns OK to indicate the interrupt has been serviced. + * + ****************************************************************************/ + +static int lis3dh_interrupt_handler(int irq, FAR void *context, FAR void *arg) +{ + /* The interrupt handler is called when the FIFO watermark is reached */ + + FAR struct lis3dh_dev_s *priv = (FAR struct lis3dh_dev_s *)arg; + int ret; + + DEBUGASSERT(priv != NULL); + + if (work_available(&priv->work)) + { + ret = work_queue(HPWORK, &priv->work, lis3dh_worker, priv, 0); + if (ret < 0) + { + snerr("ERROR: Failed to queue work: %d\n", ret); + return ret; + } + } + + return OK; +} + +/**************************************************************************** + * Name: lis3dh_worker + * + * Description: + * Worker callback executed from high priority work queue. + * Performs reading the FIFO from the sensor and pushing samples to + * the internal device driver queue. + * + * Input Parameters: + * arg - Pointer to device driver instance + * + ****************************************************************************/ + +static void lis3dh_worker(FAR void *arg) +{ + FAR struct lis3dh_dev_s *priv = (FAR struct lis3dh_dev_s *)(arg); + + DEBUGASSERT(priv != NULL); + + /* Read the FIFO and fill the queue */ + + lis3dh_read_fifo(priv); +} + +/**************************************************************************** + * Name: lis3dh_set_power_mode + * + * Description: + * Sets the power mode of the sensor. + * + * Input Parameters: + * dev - Pointer to device driver instance + * power_mode - LIS3DH_POWER_* + * + * Returned Value: + * OK - Power mode was set successfully + * -EINVAL - Invalid power mode argument + * + ****************************************************************************/ + +static int lis3dh_set_power_mode(FAR struct lis3dh_dev_s *dev, + uint8_t power_mode) +{ + uint8_t ctrl1; + uint8_t ctrl4; + + switch(power_mode) + { + case LIS3DH_POWER_LOW: + lis3dh_read_register(dev, LIS3DH_CTRL_REG1, &ctrl1); + lis3dh_read_register(dev, LIS3DH_CTRL_REG4, &ctrl4); + + ctrl1 |= LIS3DH_CTRL_REG1_LPEN; + ctrl4 &= ~LIS3DH_CTRL_REG4_HR; + + lis3dh_write_register(dev, LIS3DH_CTRL_REG1, ctrl1); + lis3dh_write_register(dev, LIS3DH_CTRL_REG4, ctrl4); + break; + case LIS3DH_POWER_NORMAL: + lis3dh_read_register(dev, LIS3DH_CTRL_REG1, &ctrl1); + lis3dh_read_register(dev, LIS3DH_CTRL_REG4, &ctrl4); + + ctrl1 &= ~LIS3DH_CTRL_REG1_LPEN; + ctrl4 &= ~LIS3DH_CTRL_REG4_HR; + + lis3dh_write_register(dev, LIS3DH_CTRL_REG1, ctrl1); + lis3dh_write_register(dev, LIS3DH_CTRL_REG4, ctrl4); + break; + case LIS3DH_POWER_HIGH: + lis3dh_read_register(dev, LIS3DH_CTRL_REG1, &ctrl1); + lis3dh_read_register(dev, LIS3DH_CTRL_REG4, &ctrl4); + + ctrl1 &= ~LIS3DH_CTRL_REG1_LPEN; + ctrl4 |= LIS3DH_CTRL_REG4_HR; + + lis3dh_write_register(dev, LIS3DH_CTRL_REG1, ctrl1); + lis3dh_write_register(dev, LIS3DH_CTRL_REG4, ctrl4); + break; + default: + return -EINVAL; + break; + } + + dev->power_mode = power_mode; + sninfo("Power mode set to %d\n", power_mode); + + return OK; +} + +/**************************************************************************** + * Name: lis3dh_set_odr + * + * Description: + * Sets the output data rate of the sensor. + * + * Input Parameters: + * dev - Pointer to device driver instance + * odr - LIS3DH_ODR_* + * + * Returned Value: + * OK - Power mode was set successfully + * -EINVAL - Invalid odr argument + * + ****************************************************************************/ + +static int lis3dh_set_odr(FAR struct lis3dh_dev_s *dev, uint8_t odr) +{ + uint8_t ctrl1; + + if (odr > LIS3DH_CTRL_REG1_ODR_LP_5376HZ) + { + return -EINVAL; + } + + lis3dh_read_register(dev, LIS3DH_CTRL_REG1, &ctrl1); + ctrl1 |= odr & LIS3DH_CTRL_REG1_ODR_MASK; + lis3dh_write_register(dev, LIS3DH_CTRL_REG1, ctrl1); + + /* Cache the current ODR in the device structure */ + + dev->odr = odr; + sninfo("Output data rate set to %d\n", odr); + + return OK; +} + +/**************************************************************************** + * Name: lis3dh_irq_enable + * + * Description: + * Enable or disable sensor interrupt generation on FIFO watermark. + * + * Input Parameters: + * dev - Pointer to device driver instance + * enable - Boolean to enable or disable the interrupt generation + * + * Returned Value: + * OK - Interrupt generation register written. + * + ****************************************************************************/ + +static int lis3dh_irq_enable(FAR struct lis3dh_dev_s *dev, bool enable) +{ + uint8_t reg; + + reg = 0; + + if(enable == true) + { + reg = LIS3DH_CTRL_REG3_I1_WTM; + } + + lis3dh_write_register(dev, LIS3DH_CTRL_REG3, reg); + return OK; +} + +/**************************************************************************** + * Name: lis3dh_irq_enable + * + * Description: + * Enable or disable sensor interrupt generation on FIFO watermark. + * + * Input Parameters: + * dev - Pointer to device driver instance + * enable - Boolean to enable or disable the interrupt generation + * + * Returned Value: + * OK - Interrupt generation register written. + * + ****************************************************************************/ + +static int lis3dh_fifo_enable(FAR struct lis3dh_dev_s *dev) +{ + uint8_t reg; + lis3dh_write_register(dev, LIS3DH_FIFO_CTRL_REG, + LIS3DH_FIFO_CTRL_REG_MODE_STREAM | 28); + + lis3dh_read_register(dev, LIS3DH_CTRL_REG5, ®); + reg |= LIS3DH_CTRL_REG5_FIFO_EN; + lis3dh_write_register(dev, LIS3DH_CTRL_REG5, reg); + + return OK; +} + +/**************************************************************************** + * Name: lis3dh_enable + * + * Description: + * Enable sensor measurements into the on-chip FIFO. + * + * Input Parameters: + * priv - Pointer to device driver instance + * + * Returned Value: + * OK - Sensor measurements enabled. + * + ****************************************************************************/ + +static int lis3dh_enable(FAR struct lis3dh_dev_s *priv) +{ + uint8_t reg; + + /* Select the default power mode */ + + lis3dh_set_power_mode(priv, LIS3DH_POWER_NORMAL); + + /* Select the default output data rate */ + + lis3dh_set_odr(priv, LIS3DH_ODR_1344HZ); + + reg = LIS3DH_CTRL_REG4_BDU | LIS3DH_CTRL_REG4_FS_2G; + lis3dh_write_register(priv, LIS3DH_CTRL_REG4, reg); + + lis3dh_fifo_enable(priv); + + /* Enable X, Y, and Z axes */ + + lis3dh_read_register(priv, LIS3DH_CTRL_REG1, ®); + reg |= LIS3DH_CTRL_REG1_ZEN | LIS3DH_CTRL_REG1_YEN | LIS3DH_CTRL_REG1_XEN; + lis3dh_write_register(priv, LIS3DH_CTRL_REG1, reg); + + return OK; +} + +/**************************************************************************** + * Name: lis3dh_open + * + * Description: + * Character device open call. + * + * Input Parameters: + * filep - Pointer to struct file + * + * Returned Value: + * -ENODEV - Device was not identified on the SPI bus. + * OK - Sensor device was opened successfully. + * + ****************************************************************************/ + +static int lis3dh_open(FAR struct file *filep) +{ + FAR struct inode *inode = filep->f_inode; + FAR struct lis3dh_dev_s *priv = inode->i_private; + + DEBUGASSERT(priv != NULL); + + /* Perform a reset */ + + lis3dh_reset(priv); + if(lis3dh_ident(priv) < 0) + { + snerr("ERROR: Failed to identify LIS3DH on SPI bus\n"); + return -ENODEV; + } + + /* Attach the interrupt line */ + + (priv->config->irq_attach)(priv->config, lis3dh_interrupt_handler, priv); + + /* Enable interrupt generation on the sensor */ + + lis3dh_irq_enable(priv, true); + + /* Enable measurements on the sensor */ + + if(lis3dh_enable(priv) < 0) + { + snerr("ERROR: Failed to enable LIS3DH\n"); + return -ENODEV; + } + + return OK; +} + +/**************************************************************************** + * Name: lis3dh_close + * + * Description: + * Character device close call. + * + * Input Parameters: + * filep - Pointer to struct file + * + * Returned Value: + * OK - Sensor device was closed successfully. + * + ****************************************************************************/ + +static int lis3dh_close(FAR struct file *filep) +{ + FAR struct inode *inode = filep->f_inode; + FAR struct lis3dh_dev_s *priv = inode->i_private; + + DEBUGASSERT(priv != NULL); + + /* Perform a reset */ + + lis3dh_reset(priv); + + /* Detach the interrupt line */ + + (priv->config->irq_detach)(priv->config); + + return OK; +} + +/**************************************************************************** + * Name: lis3dh_read + * + * Description: + * Character device read call. Blocks until requested buffer size is full. + * + * Input Parameters: + * filep - Pointer to struct file + * buffer - Pointer to user buffer + * buflen - Size of user buffer in bytes + * + * Returned Value: + * Returns the number of bytes written to the buffer. + * -EINVAL - Supplied buffer length invalid + * + ****************************************************************************/ + +static ssize_t lis3dh_read(FAR struct file *filep, FAR char *buffer, + size_t buflen) +{ + FAR struct inode *inode = filep->f_inode; + FAR struct lis3dh_dev_s *priv = inode->i_private; + FAR struct lis3dh_sensor_data_s *data; + int count; + int remain; + + DEBUGASSERT(priv != NULL); + + if ((buflen % sizeof(FAR struct lis3dh_sensor_data_s)) != 0) + { + snerr("ERROR: Provided buffer must be multiple of sensor data size\n"); + return -EINVAL; + } + + /* Get the number of samples the user has requested */ + + count = buflen / sizeof(struct lis3dh_sensor_data_s); + + /* Cast a pointer into the user buffer */ + + data = (FAR struct lis3dh_sensor_data_s *)buffer; + + for (remain=count;remain > 0;remain--) + { + + /* Wait for data to be available in the queue */ + + nxsem_wait(&priv->readsem); + + /* Pop a sample off of the queue */ + + lis3dh_queue_pop(priv, data); + + data++; + } + return count * sizeof(struct lis3dh_sensor_data_s); +} + +/**************************************************************************** + * Name: lis3dh_write + * + * Description: + * Character device write call. Not supported. + * + * Input Parameters: + * filep - Pointer to struct file + * buffer - Pointer to user buffer + * buflen - Size of user buffer in bytes + * + * Returned Value: + * -ENOSYS + * + ****************************************************************************/ + +static ssize_t lis3dh_write(FAR struct file *filep, FAR const char *buffer, + size_t buflen) +{ + return -ENOSYS; +} + +/**************************************************************************** + * Name: lis3dh_ioctl + * + * Description: + * Character device ioctl call. Sets device parameters. + * + * Input Parameters: + * filep - Pointer to struct file + * cmd - SNIOC_* + * arg - ioctl specific argument + * + * Returned Value: + * OK - The command was executed successfully. + * -ENOTTY - The request command is not applicible to the driver. + * + ****************************************************************************/ + +static int lis3dh_ioctl(FAR struct file *filep, int cmd, unsigned long arg) +{ + FAR struct inode *inode = filep->f_inode; + FAR struct lis3dh_dev_s *priv = inode->i_private; + int ret = OK; + + switch (cmd) + { + case SNIOC_SET_POWER_MODE: + ret = lis3dh_set_power_mode(priv, arg); + break; + + case SNIOC_SET_DATA_RATE: + ret = lis3dh_set_odr(priv, arg); + break; + + default: + snerr("ERROR: Unrecognized cmd: %d\n", cmd); + ret = -ENOTTY; + break; + } + + return ret; +} + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: lis3dh_register + * + * Description: + * Register the LIS3DH character device at the specified device path + * + * Input Parameters: + * devpath - Full path of device node to register ie "/dev/accel0" + * spi - SPI bus device instance + * config - Driver instance configuration structure + * + * Returned Value: + * OK on success or a negative errno value on failure. + * + ****************************************************************************/ + +int lis3dh_register(FAR const char *devpath, FAR struct spi_dev_s *spi, + FAR struct lis3dh_config_s *config) +{ + FAR struct lis3dh_dev_s *priv; + int ret; + + DEBUGASSERT(spi != NULL); + + /* Initialize the LIS3DH device structure */ + + priv = (FAR struct lis3dh_dev_s *)kmm_malloc(sizeof(struct lis3dh_dev_s)); + if (priv == NULL) + { + snerr("ERROR: Failed to allocate instance\n"); + return -ENOMEM; + } + + priv->config = config; + priv->spi = spi; + priv->work.worker = NULL; + priv->queue_count = 0; + + /* Initialize queue semaphore */ + + nxsem_init(&priv->queuesem, 0, 1); + + /* Initialize read notification semaphore */ + + nxsem_init(&priv->readsem, 0, 0); + nxsem_setprotocol(&priv->readsem, SEM_PRIO_NONE); + + /* Setup SPI frequency and mode */ + + SPI_SETFREQUENCY(spi, LIS3DH_SPI_FREQUENCY); + SPI_SETMODE(spi, LIS3DH_SPI_MODE); + + /* Register the character driver */ + + ret = register_driver(devpath, &g_lis3dh_fops, 0666, priv); + if (ret < 0) + { + snerr("ERROR: Failed to register driver: %d\n", ret); + kmm_free(priv); + nxsem_destroy(&priv->queuesem); + nxsem_destroy(&priv->readsem); + return ret; + } + + return OK; +} + +#endif /* CONFIG_SPI && CONFIG_LIS3DH */ diff --git a/include/nuttx/sensors/ioctl.h b/include/nuttx/sensors/ioctl.h index 3f6add61d06..96ac4d5acec 100644 --- a/include/nuttx/sensors/ioctl.h +++ b/include/nuttx/sensors/ioctl.h @@ -149,4 +149,10 @@ #define SNIOC_INIT _SNIOC(0x003b) #define SNIOC_THRESHOLD _SNIOC(0x003c) +/* IOCTL commands unique to LIS3DH */ + +#define SNIOC_SET_POWER_MODE _SNIOC(0x003d) /* Arg: LIS3DH_POWER_xxx */ +#define SNIOC_SET_DATA_RATE _SNIOC(0x003e) /* Arg: LIS3DH_ODR_xxx */ +#define SNIOC_SET_DATA_FORMAT _SNIOC(0x003f) /* Arg: LIS3DH_FORMAT_xxx */ + #endif /* __INCLUDE_NUTTX_SENSORS_IOCTL_H */ diff --git a/include/nuttx/sensors/lis3dh.h b/include/nuttx/sensors/lis3dh.h new file mode 100644 index 00000000000..646274ee238 --- /dev/null +++ b/include/nuttx/sensors/lis3dh.h @@ -0,0 +1,303 @@ +/**************************************************************************** + * include/nuttx/sensors/lis3dh.h + * + * Copyright (C) 2018 Extent3D. All rights reserved. + * Author: Matt Thompson + * + * 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 __INCLUDE_NUTTX_SENSORS_LIS3DH_H +#define __INCLUDE_NUTTX_SENSORS_LIS3DH_H + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include +#include +#include +#include + +#if defined(CONFIG_SPI) && defined(CONFIG_LIS3DH) + +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +/* LIS3DH Device Identification *********************************************/ + +#define LIS3DH_DEVICE_ID (0x33) + +/* LIS3DH Register Definitions **********************************************/ + +#define LIS3DH_STATUS_REG_AUX (0x07) +#define LIS3DH_OUT_ADC1_L (0x08) +#define LIS3DH_OUT_ADC1_H (0x09) +#define LIS3DH_OUT_ADC2_L (0x0A) +#define LIS3DH_OUT_ADC2_H (0x0B) +#define LIS3DH_OUT_ADC3_L (0x0C) +#define LIS3DH_OUT_ADC3_H (0x0D) +#define LIS3DH_WHO_AM_I (0x0F) +#define LIS3DH_CTRL_REG0 (0x1E) +#define LIS3DH_TEMP_CFG_REG (0x1F) +#define LIS3DH_CTRL_REG1 (0x20) +#define LIS3DH_CTRL_REG2 (0x21) +#define LIS3DH_CTRL_REG3 (0x22) +#define LIS3DH_CTRL_REG4 (0x23) +#define LIS3DH_CTRL_REG5 (0x24) +#define LIS3DH_CTRL_REG6 (0x25) +#define LIS3DH_REFERENCE (0x26) +#define LIS3DH_STATUS_REG (0x27) +#define LIS3DH_OUT_X_L (0x28) +#define LIS3DH_OUT_X_H (0x29) +#define LIS3DH_OUT_Y_L (0x2A) +#define LIS3DH_OUT_Y_H (0x2B) +#define LIS3DH_OUT_Z_L (0x2C) +#define LIS3DH_OUT_Z_H (0x2D) +#define LIS3DH_FIFO_CTRL_REG (0x2E) +#define LIS3DH_FIFO_SRC_REG (0x2F) +#define LIS3DH_INT1_CFG (0x30) +#define LIS3DH_INT1_SRC (0x31) +#define LIS3DH_INT1_THS (0x32) +#define LIS3DH_INT1_DURATION (0x33) +#define LIS3DH_INT2_CFG (0x34) +#define LIS3DH_INT2_SRC (0x35) +#define LIS3DH_INT2_THS (0x36) +#define LIS3DH_INT2_DURATION (0x37) +#define LIS3DH_CLICK_CFG (0x38) +#define LIS3DH_CLICK_SRC (0x39) +#define LIS3DH_CLICK_THS (0x3A) +#define LIS3DH_TIME_LIMIT (0x3B) +#define LIS3DH_TIME_LATENCY (0x3C) +#define LIS3DH_TIME_WINDOW (0x3D) +#define LIS3DH_ACT_THS (0x3E) +#define LIS3DH_ACT_DUR (0x3F) + +/* LIS3DH STATUS_REG_AUX Definitions **********************************************/ + +#define LIS3DH_STATUS_REG_AUX_321OR (1 << 7) +#define LIS3DH_STATUS_REG_AUX_3OR (1 << 6) +#define LIS3DH_STATUS_REG_AUX_2OR (1 << 5) +#define LIS3DH_STATUS_REG_AUX_1OR (1 << 4) +#define LIS3DH_STATUS_REG_AUX_321DA (1 << 3) +#define LIS3DH_STATUS_REG_AUX_3DA (1 << 2) +#define LIS3DH_STATUS_REG_AUX_2DA (1 << 1) +#define LIS3DH_STATUS_REG_AUX_1DA (1 << 0) + +/* LIS3DH CTRL_REG0 Definitions **********************************************/ + +#define LIS3DH_CTRL_REG0_SDO_PU_DISC (1 << 7) /* Disconnect SDO/SA0 pull-up */ + +/* LIS3DH TEMP_CFG_REG Definitions **********************************************/ + +#define LIS3DH_TEMP_CFG_REG_ADC_EN (1 << 7) /* ADC enable */ +#define LIS3DH_TEMP_CFG_REG_TEMP_EN (1 << 6) /* Temperator sensor enable */ + +/* LIS3DH CTRL_REG1 Definitions **********************************************/ + +#define LIS3DH_CTRL_REG1_ODR_SHIFT (4) +#define LIS3DH_CTRL_REG1_ODR_MASK (0xf << LIS3DH_CTRL_REG1_ODR_SHIFT) +#define LIS3DH_CTRL_REG1_ODR_POWER_DOWN (0) +#define LIS3DH_CTRL_REG1_ODR_1HZ (0x1 << LIS3DH_CTRL_REG1_ODR_SHIFT) +#define LIS3DH_CTRL_REG1_ODR_10HZ (0x2 << LIS3DH_CTRL_REG1_ODR_SHIFT) +#define LIS3DH_CTRL_REG1_ODR_25HZ (0x3 << LIS3DH_CTRL_REG1_ODR_SHIFT) +#define LIS3DH_CTRL_REG1_ODR_50HZ (0x4 << LIS3DH_CTRL_REG1_ODR_SHIFT) +#define LIS3DH_CTRL_REG1_ODR_100HZ (0x5 << LIS3DH_CTRL_REG1_ODR_SHIFT) +#define LIS3DH_CTRL_REG1_ODR_200HZ (0x6 << LIS3DH_CTRL_REG1_ODR_SHIFT) +#define LIS3DH_CTRL_REG1_ODR_400HZ (0x7 << LIS3DH_CTRL_REG1_ODR_SHIFT) +#define LIS3DH_CTRL_REG1_ODR_LP_1600HZ (0x8 << LIS3DH_CTRL_REG1_ODR_SHIFT) +#define LIS3DH_CTRL_REG1_ODR_1344HZ (0x9 << LIS3DH_CTRL_REG1_ODR_SHIFT) +#define LIS3DH_CTRL_REG1_ODR_LP_5376HZ (0x9 << LIS3DH_CTRL_REG1_ODR_SHIFT) + +#define LIS3DH_CTRL_REG1_LPEN (1 << 3) /* Low-power mode enable */ +#define LIS3DH_CTRL_REG1_ZEN (1 << 2) /* Z axis enable */ +#define LIS3DH_CTRL_REG1_YEN (1 << 1) /* Y axis enable */ +#define LIS3DH_CTRL_REG1_XEN (1 << 0) /* X axis enable */ + +/* LIS3DH CTRL_REG2 Definitions **********************************************/ + +/* LIS3DH CTRL_REG3 Definitions **********************************************/ + +#define LIS3DH_CTRL_REG3_I1_CLICK (1 << 6) /* Click interrupt on INT1 */ +#define LIS3DH_CTRL_REG3_I1_IA1 (1 << 6) /* IA1 interrupt on INT1 */ +#define LIS3DH_CTRL_REG3_I1_IA2 (1 << 5) /* IA2 interrupt on INT1 */ +#define LIS3DH_CTRL_REG3_I1_ZYXDA (1 << 4) /* ZYX data available interrupt on INT1 */ +#define LIS3DH_CTRL_REG3_I1_321DA (1 << 3) /* 321 data available interrupt on INT1 */ +#define LIS3DH_CTRL_REG3_I1_WTM (1 << 2) /* FIFO watermark interrupt on INT1 */ +#define LIS3DH_CTRL_REG3_I1_OVERRUN (1 << 1) /* FIFO overrun interrupt on INT1 */ + +/* LIS3DH CTRL_REG4 Definitions **********************************************/ + +#define LIS3DH_CTRL_REG4_BDU (1 << 7) /* Block data update */ +#define LIS3DH_CTRL_REG4_BLE (1 << 6) /* Endian selection. 0: LSB first, 1: MSB first */ +#define LIS3DH_CTRL_REG4_FS_16G (3 << 4) /* 16g full scale range */ +#define LIS3DH_CTRL_REG4_FS_8G (2 << 4) /* 8g full scale range */ +#define LIS3DH_CTRL_REG4_FS_4G (1 << 4) /* 4g full scale range */ +#define LIS3DH_CTRL_REG4_FS_2G (0 << 4) /* 2g full scale range */ +#define LIS3DH_CTRL_REG4_HR (1 << 3) /* High resolution output enable */ +#define LIS3DH_CTRL_REG4_ST1 (2 << 1) /* Self test 1 */ +#define LIS3DH_CTRL_REG4_ST0 (1 << 1) /* Self test 0 */ +#define LIS3DH_CTRL_REG4_SIM (1 << 0) /* SPI serial interface mode selection (0: 4-wire, 1: 3-wire) */ + +/* LIS3DH CTRL_REG5 Definitions **********************************************/ + +#define LIS3DH_CTRL_REG5_BOOT (1 << 7) /* Reboot memory content */ +#define LIS3DH_CTRL_REG5_FIFO_EN (1 << 6) /* FIFO enable */ +#define LIS3DH_CTRL_REG5_LIR_INT1 (1 << 3) /* Latch interrupt request on INT1_SRC register */ +#define LIS3DH_CTRL_REG5_D4D_INT1 (1 << 2) /* 4D detection enable on INT1 */ +#define LIS3DH_CTRL_REG5_LIR_INT2 (1 << 1) /* Latch interrupt request on INT2_SRC register */ +#define LIS3DH_CTRL_REG5_D4D_INT2 (1 << 0) /* 4D detection enable on INT2 */ + +/* LIS3DH CTRL_REG6 Definitions **********************************************/ + +#define LIS3DH_CTRL_REG6_I2_CLICK (1 << 6) /* Click interrupt on INT2 */ +#define LIS3DH_CTRL_REG6_I2_IA1 (1 << 6) /* IA1 interrupt on INT2 */ +#define LIS3DH_CTRL_REG6_I2_IA2 (1 << 5) /* IA2 interrupt on INT2 */ +#define LIS3DH_CTRL_REG6_I2_BOOT (1 << 4) /* Enable boot on INT2 */ +#define LIS3DH_CTRL_REG6_I2_ACT (1 << 3) /* Enable activity interrupt on INT2 */ +#define LIS3DH_CTRL_REG6_INT_POLARITY (1 << 1) /* INT1 and INT2 pin polarity */ + +/* LIS3DH STATUS_REG Definitions **********************************************/ + +#define LIS3DH_STATUS_ZYXOR (1 << 7) /* X,Y,Z axis data overrun */ +#define LIS3DH_STATUS_ZOR (1 << 6) /* Z axis data overrun */ +#define LIS3DH_STATUS_YOR (1 << 5) /* Y axis data overrun */ +#define LIS3DH_STATUS_XOR (1 << 4) /* X axis data overrun */ +#define LIS3DH_STATUS_REG_ZYXDA (1 << 3) /* X,Y,Z axis data available */ +#define LIS3DH_STATUS_REG_ZDA (1 << 2) /* Z axis data available */ +#define LIS3DH_STATUS_REG_YDA (1 << 1) /* Y axis data available */ +#define LIS3DH_STATUS_REG_XDA (1 << 0) /* X axis data available */ + +/* LIS3DH FIFO_CTRL_REG Definitions **********************************************/ + +#define LIS3DH_FIFO_CTRL_REG_MODE_STREAM2 (3 << 6) +#define LIS3DH_FIFO_CTRL_REG_MODE_STREAM (2 << 6) +#define LIS3DH_FIFO_CTRL_REG_MODE_FIFO (1 << 6) +#define LIS3DH_FIFO_CTRL_REG_MODE_BYPASS (0 << 6) +#define LIS3DH_FIFO_CTRL_REG_TR (1 << 5) + +/* LIS3DH FIFO_SRC_REG Definitions **********************************************/ + +#define LIS3DH_FIFO_SRC_REG_WTM (1 << 7) +#define LIS3DH_FIFO_SRC_REG_OVRN_FIFO (1 << 6) +#define LIS3DH_FIFO_SRC_REG_EMPTY (1 << 5) + +/* SPI parameters ***************************************************************/ + +#define LIS3DH_SPI_FREQUENCY (9600000) /* SPI Clock Frequency */ +#define LIS3DH_SPI_MODE (SPIDEV_MODE3) /* Device uses SPI Mode 3: CPOL=1, CPHA=1 */ + +/* Power Modes ******************************************************************/ + +#define LIS3DH_POWER_LOW (0x0) /* Lower power 8bit output */ +#define LIS3DH_POWER_NORMAL (0x1) /* Normal 10bit */ +#define LIS3DH_POWER_HIGH (0x2) /* HR 12bit mode */ + +/* Output Data Rates ***********************************************************/ + +#define LIS3DH_ODR_POWER_DOWN (0) /* Disable output */ +#define LIS3DH_ODR_1HZ (0x1) /* 1Hz in all power modes */ +#define LIS3DH_ODR_10HZ (0x2) /* 10Hz in all power modes */ +#define LIS3DH_ODR_25HZ (0x3) /* 25Hz in all power modes */ +#define LIS3DH_ODR_50HZ (0x4) /* 50Hz in all power modes */ +#define LIS3DH_ODR_100HZ (0x5) /* 100Hz in all power modes */ +#define LIS3DH_ODR_200HZ (0x6) /* 200Hz in all power modes */ +#define LIS3DH_ODR_400HZ (0x7) /* 400Hz in all power modes */ +#define LIS3DH_ODR_LP_1600HZ (0x8) /* 1.6kHz in low power mode only */ +#define LIS3DH_ODR_1344HZ (0x9) /* 1.344kHz in normal and high power modes only */ +#define LIS3DH_ODR_LP_5376HZ (0x9) /* 5.376kHz in low power mode only */ + +/**************************************************************************** + * Public Types + ****************************************************************************/ + +struct lis3dh_sensor_data_s +{ + float x_acc; /* X axis acceleration */ + float y_acc; /* Y axis acceleration */ + float z_acc; /* Z axis acceleration */ +}; + +/* Configuration structure used to register the driver */ + +struct lis3dh_config_s +{ + /* SPI device ID used to select the CS line of the sensor */ + + int spi_devid; + + /* IRQ number associated with this driver instance */ + + int irq; + + /* Attach callback used to configure the interrupt line */ + + int (*irq_attach)(FAR struct lis3dh_config_s *, xcpt_t, void *arg); + int (*irq_detach)(FAR struct lis3dh_config_s *); +}; + +/**************************************************************************** + * Public Function Prototypes + ****************************************************************************/ + +#ifdef __cplusplus +#define EXTERN extern "C" +extern "C" +{ +#else +#define EXTERN extern +#endif + +/**************************************************************************** + * Name: lis3dh_register + * + * Description: + * Register the LIS3DH character device at the specified device path + * + * Input Parameters: + * devpath - Full path of device node to register ie "/dev/accel0" + * spi - SPI bus device instance + * config - Driver instance configuration structure + * + * Returned Value: + * OK on success or a negative errno value on failure. + * + ****************************************************************************/ + +int lis3dh_register(FAR const char *devpath, FAR struct spi_dev_s *spi, + FAR struct lis3dh_config_s *); + +#undef EXTERN +#ifdef __cplusplus +} +#endif + +#endif /* CONFIG_SPI && CONFIG_LIS3DH */ +#endif /* __INCLUDE_NUTTX_SENSORS_LIS3DH_H */