2016-01-09 18:43:05 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* drivers/sensors/lsm9ds1.c
|
|
|
|
*
|
2021-09-28 11:40:59 +02:00
|
|
|
* Licensed to the Apache Software Foundation (ASF) under one or more
|
|
|
|
* contributor license agreements. See the NOTICE file distributed with
|
|
|
|
* this work for additional information regarding copyright ownership. The
|
|
|
|
* ASF licenses this file to you under the Apache License, Version 2.0 (the
|
|
|
|
* "License"); you may not use this file except in compliance with the
|
|
|
|
* License. You may obtain a copy of the License at
|
2016-01-09 18:43:05 +01:00
|
|
|
*
|
2021-09-28 11:40:59 +02:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2016-02-01 15:57:22 +01:00
|
|
|
*
|
2021-09-28 11:40:59 +02:00
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
|
|
* License for the specific language governing permissions and limitations
|
|
|
|
* under the License.
|
2016-01-09 18:43:05 +01:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
2023-08-26 15:44:52 +02:00
|
|
|
#include "lsm9ds1_base.h"
|
2016-01-09 18:43:05 +01:00
|
|
|
|
2017-08-24 18:48:20 +02:00
|
|
|
#if defined(CONFIG_I2C) && defined(CONFIG_SENSORS_LSM9DS1)
|
2016-01-09 18:43:05 +01:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Function Prototypes
|
|
|
|
****************************************************************************/
|
2019-12-07 02:07:34 +01:00
|
|
|
|
2016-01-09 18:43:05 +01:00
|
|
|
/* Character Driver Methods */
|
|
|
|
|
|
|
|
static ssize_t lsm9ds1_read(FAR struct file *filep, FAR char *buffer,
|
|
|
|
size_t buflen);
|
|
|
|
static ssize_t lsm9ds1_write(FAR struct file *filep, FAR const char *buffer,
|
|
|
|
size_t buflen);
|
|
|
|
static int lsm9ds1_ioctl(FAR struct file *filep, int cmd,
|
|
|
|
unsigned long arg);
|
|
|
|
|
|
|
|
/* Common Register Function */
|
|
|
|
|
|
|
|
static int lsm9ds1_register(FAR const char *devpath,
|
2016-01-30 15:36:47 +01:00
|
|
|
FAR struct i2c_master_s *i2c, uint8_t addr,
|
2016-01-09 18:43:05 +01:00
|
|
|
FAR const struct lsm9ds1_ops_s *ops,
|
|
|
|
uint8_t datareg);
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Data
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static const struct file_operations g_fops =
|
|
|
|
{
|
2022-04-12 12:16:13 +02:00
|
|
|
NULL, /* open */
|
|
|
|
NULL, /* close */
|
2022-01-10 15:51:17 +01:00
|
|
|
lsm9ds1_read, /* read */
|
|
|
|
lsm9ds1_write, /* write */
|
|
|
|
NULL, /* seek */
|
|
|
|
lsm9ds1_ioctl, /* ioctl */
|
2016-01-09 18:43:05 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: lsm9ds1_read
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* The standard read method.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static ssize_t lsm9ds1_read(FAR struct file *filep, FAR char *buffer,
|
|
|
|
size_t buflen)
|
|
|
|
{
|
|
|
|
FAR struct inode *inode;
|
|
|
|
FAR struct lsm9ds1_dev_s *priv;
|
|
|
|
int ret;
|
|
|
|
size_t i;
|
|
|
|
size_t j;
|
|
|
|
size_t samplesize;
|
|
|
|
size_t nsamples;
|
|
|
|
uint16_t data;
|
|
|
|
FAR int16_t *ptr;
|
|
|
|
uint8_t regaddr;
|
|
|
|
uint8_t lo;
|
|
|
|
uint8_t hi;
|
2017-03-30 15:38:37 +02:00
|
|
|
uint32_t merge = 0;
|
2016-01-09 18:43:05 +01:00
|
|
|
|
|
|
|
/* Sanity check */
|
|
|
|
|
|
|
|
inode = filep->f_inode;
|
|
|
|
|
2023-08-29 06:16:49 +02:00
|
|
|
priv = inode->i_private;
|
2016-01-09 18:43:05 +01:00
|
|
|
|
|
|
|
DEBUGASSERT(priv != NULL);
|
|
|
|
DEBUGASSERT(priv->datareg == LSM9DS1_OUT_X_L_G ||
|
|
|
|
priv->datareg == LSM9DS1_OUT_X_L_XL ||
|
|
|
|
priv->datareg == LSM9DS1_OUT_X_L_M);
|
|
|
|
DEBUGASSERT(buffer != NULL);
|
|
|
|
|
|
|
|
samplesize = 3 * sizeof(*ptr);
|
|
|
|
nsamples = buflen / samplesize;
|
|
|
|
ptr = (FAR int16_t *)buffer;
|
|
|
|
|
|
|
|
/* Get the requested number of samples */
|
|
|
|
|
|
|
|
for (i = 0; i < nsamples; i++)
|
|
|
|
{
|
|
|
|
/* Reset the register address to the X low byte register */
|
|
|
|
|
|
|
|
regaddr = priv->datareg;
|
|
|
|
|
|
|
|
/* Read the X, Y and Z data */
|
|
|
|
|
|
|
|
for (j = 0; j < 3; j++)
|
|
|
|
{
|
|
|
|
/* Read the low byte */
|
|
|
|
|
|
|
|
ret = lsm9ds1_readreg8(priv, regaddr, &lo);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
2016-06-12 02:05:29 +02:00
|
|
|
snerr("ERROR: lsm9ds1_readreg8 failed: %d\n", ret);
|
2016-01-09 18:43:05 +01:00
|
|
|
return (ssize_t)ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
regaddr++;
|
|
|
|
|
|
|
|
/* Read the high byte */
|
|
|
|
|
|
|
|
ret = lsm9ds1_readreg8(priv, regaddr, &hi);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
2016-06-12 02:05:29 +02:00
|
|
|
snerr("ERROR: lsm9ds1_readreg8 failed: %d\n", ret);
|
2016-01-09 18:43:05 +01:00
|
|
|
return (ssize_t)ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
regaddr++;
|
|
|
|
|
|
|
|
/* The data is 16 bits in two's complement representation */
|
|
|
|
|
|
|
|
data = ((uint16_t)hi << 8) | (uint16_t)lo;
|
|
|
|
|
2017-03-30 15:38:37 +02:00
|
|
|
/* Collect entropy */
|
|
|
|
|
|
|
|
merge += data ^ (merge >> 16);
|
|
|
|
|
2016-01-09 18:43:05 +01:00
|
|
|
/* The value is positive */
|
|
|
|
|
|
|
|
if (data < 0x8000)
|
|
|
|
{
|
|
|
|
ptr[j] = (int16_t)data;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* The value is negative, so find its absolute value by taking the
|
|
|
|
* two's complement
|
|
|
|
*/
|
|
|
|
|
|
|
|
else if (data > 0x8000)
|
|
|
|
{
|
|
|
|
data = ~data + 1;
|
|
|
|
ptr[j] = -(int16_t)data;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* The value is negative and can't be represented as a positive
|
|
|
|
* int16_t value
|
|
|
|
*/
|
|
|
|
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ptr[j] = (int16_t)(-32768);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-30 15:38:37 +02:00
|
|
|
/* Feed sensor data to entropy pool */
|
|
|
|
|
|
|
|
add_sensor_randomness(merge);
|
|
|
|
|
2016-01-09 18:43:05 +01:00
|
|
|
return nsamples * samplesize;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: lsm9ds1_write
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* A dummy write method.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static ssize_t lsm9ds1_write(FAR struct file *filep, FAR const char *buffer,
|
|
|
|
size_t buflen)
|
|
|
|
{
|
|
|
|
return -ENOSYS;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: lsm9ds1_ioctl
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* The standard ioctl method.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int lsm9ds1_ioctl(FAR struct file *filep, int cmd, unsigned long arg)
|
|
|
|
{
|
|
|
|
FAR struct inode *inode;
|
|
|
|
FAR struct lsm9ds1_dev_s *priv;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* Sanity check */
|
|
|
|
|
|
|
|
inode = filep->f_inode;
|
|
|
|
|
2023-08-29 06:16:49 +02:00
|
|
|
priv = inode->i_private;
|
2016-01-09 18:43:05 +01:00
|
|
|
|
|
|
|
DEBUGASSERT(priv != NULL);
|
|
|
|
|
|
|
|
/* Handle ioctl commands */
|
|
|
|
|
|
|
|
switch (cmd)
|
|
|
|
{
|
|
|
|
/* Start converting. Arg: None. */
|
|
|
|
|
|
|
|
case SNIOC_START:
|
|
|
|
ret = priv->ops->start(priv);
|
|
|
|
break;
|
|
|
|
|
|
|
|
/* Stop converting. Arg: None. */
|
|
|
|
|
|
|
|
case SNIOC_STOP:
|
|
|
|
ret = priv->ops->stop(priv);
|
|
|
|
break;
|
|
|
|
|
|
|
|
/* Set the sample rate. Arg: uint32_t value. */
|
|
|
|
|
|
|
|
case SNIOC_SETSAMPLERATE:
|
|
|
|
ret = priv->ops->setsamplerate(priv, (uint32_t)arg);
|
2023-08-26 15:42:41 +02:00
|
|
|
sninfo("sample rate: %08" PRId32 " ret: %d\n",
|
|
|
|
(uint32_t)arg, ret);
|
2016-01-09 18:43:05 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
/* Set the full-scale range. Arg: uint32_t value. */
|
|
|
|
|
|
|
|
case SNIOC_SETFULLSCALE:
|
|
|
|
ret = priv->ops->setfullscale(priv, (uint32_t)arg);
|
2023-08-26 15:42:41 +02:00
|
|
|
sninfo("full-scale range: %08" PRId32 " ret: %d\n",
|
|
|
|
(uint32_t)arg, ret);
|
2016-01-09 18:43:05 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
/* Unrecognized commands */
|
|
|
|
|
|
|
|
default:
|
2016-06-12 02:05:29 +02:00
|
|
|
snerr("ERROR: Unrecognized cmd: %d arg: %lu\n", cmd, arg);
|
2016-01-09 18:43:05 +01:00
|
|
|
ret = -ENOTTY;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: lsm9ds1_register
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Register the LSM9DS1 accelerometer, gyroscope or magnetometer character
|
|
|
|
* device as 'devpath'.
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* devpath - The full path to the driver to register, e.g., "/dev/accel0",
|
|
|
|
* "/dev/gyro0" or "/dev/mag0".
|
|
|
|
* i2c - An I2C driver instance.
|
|
|
|
* addr - The I2C address of the LSM9DS1 accelerometer, gyroscope or
|
|
|
|
* magnetometer.
|
|
|
|
* ops - The device operations structure.
|
|
|
|
* datareg - The register address of the low byte of the X-coordinate data.
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* Zero (OK) on success; a negated errno value on failure.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int lsm9ds1_register(FAR const char *devpath,
|
2016-01-30 15:36:47 +01:00
|
|
|
FAR struct i2c_master_s *i2c, uint8_t addr,
|
2016-01-09 18:43:05 +01:00
|
|
|
FAR const struct lsm9ds1_ops_s *ops,
|
|
|
|
uint8_t datareg)
|
|
|
|
{
|
|
|
|
FAR struct lsm9ds1_dev_s *priv;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* Sanity check */
|
|
|
|
|
|
|
|
DEBUGASSERT(devpath != NULL);
|
|
|
|
DEBUGASSERT(i2c != NULL);
|
|
|
|
DEBUGASSERT(datareg == LSM9DS1_OUT_X_L_XL ||
|
|
|
|
datareg == LSM9DS1_OUT_X_L_G ||
|
|
|
|
datareg == LSM9DS1_OUT_X_L_M);
|
|
|
|
|
|
|
|
/* Initialize the device's structure */
|
|
|
|
|
2023-08-28 09:39:47 +02:00
|
|
|
priv = kmm_malloc(sizeof(*priv));
|
2016-01-09 18:43:05 +01:00
|
|
|
if (priv == NULL)
|
|
|
|
{
|
2016-06-12 02:05:29 +02:00
|
|
|
snerr("ERROR: Failed to allocate instance\n");
|
2016-01-09 18:43:05 +01:00
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
|
|
|
|
priv->i2c = i2c;
|
|
|
|
priv->addr = addr;
|
|
|
|
priv->ops = ops;
|
|
|
|
priv->samplerate = 0;
|
|
|
|
priv->datareg = datareg;
|
|
|
|
|
|
|
|
/* Configure the device */
|
|
|
|
|
|
|
|
ret = priv->ops->config(priv);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
2016-06-12 02:05:29 +02:00
|
|
|
snerr("ERROR: Failed to configure device: %d\n", ret);
|
2016-01-09 18:43:05 +01:00
|
|
|
kmm_free(priv);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Register the character driver */
|
|
|
|
|
|
|
|
ret = register_driver(devpath, &g_fops, 0666, priv);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
2016-06-12 02:05:29 +02:00
|
|
|
snerr("ERROR: Failed to register driver: %d\n", ret);
|
2016-01-09 18:43:05 +01:00
|
|
|
kmm_free(priv);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: lsm9ds1accel_register
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Register the LSM9DS1 accelerometer character device as 'devpath'.
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* devpath - The full path to the driver to register, e.g., "/dev/accel0".
|
|
|
|
* i2c - An I2C driver instance.
|
|
|
|
* addr - The I2C address of the LSM9DS1 accelerometer.
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* Zero (OK) on success; a negated errno value on failure.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2021-04-04 11:58:11 +02:00
|
|
|
int lsm9ds1accel_register(FAR const char *devpath,
|
|
|
|
FAR struct i2c_master_s *i2c,
|
2016-01-09 18:43:05 +01:00
|
|
|
uint8_t addr)
|
|
|
|
{
|
|
|
|
/* Sanity check */
|
|
|
|
|
|
|
|
DEBUGASSERT(addr == LSM9DS1ACCEL_ADDR0 || addr == LSM9DS1ACCEL_ADDR1);
|
|
|
|
|
|
|
|
return lsm9ds1_register(devpath, i2c, addr, &g_lsm9ds1accel_ops,
|
|
|
|
LSM9DS1_OUT_X_L_XL);
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: lsm9ds1gyro_register
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Register the LSM9DS1 gyroscope character device as 'devpath'.
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* devpath - The full path to the driver to register, e.g., "/dev/gyro0".
|
|
|
|
* i2c - An I2C driver instance.
|
|
|
|
* addr - The I2C address of the LSM9DS1 gyroscope.
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* Zero (OK) on success; a negated errno value on failure.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2021-04-04 11:58:11 +02:00
|
|
|
int lsm9ds1gyro_register(FAR const char *devpath,
|
|
|
|
FAR struct i2c_master_s *i2c,
|
2016-01-09 18:43:05 +01:00
|
|
|
uint8_t addr)
|
|
|
|
{
|
|
|
|
/* Sanity check */
|
|
|
|
|
|
|
|
DEBUGASSERT(addr == LSM9DS1GYRO_ADDR0 || addr == LSM9DS1GYRO_ADDR1);
|
|
|
|
|
|
|
|
return lsm9ds1_register(devpath, i2c, addr, &g_lsm9ds1gyro_ops,
|
|
|
|
LSM9DS1_OUT_X_L_G);
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: lsm9ds1mag_register
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Register the LSM9DS1 magnetometer character device as 'devpath'.
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* devpath - The full path to the driver to register, e.g., "/dev/mag0".
|
|
|
|
* i2c - An I2C driver instance.
|
|
|
|
* addr - The I2C address of the LSM9DS1 magnetometer.
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* Zero (OK) on success; a negated errno value on failure.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2021-04-04 11:58:11 +02:00
|
|
|
int lsm9ds1mag_register(FAR const char *devpath,
|
|
|
|
FAR struct i2c_master_s *i2c,
|
2016-01-09 18:43:05 +01:00
|
|
|
uint8_t addr)
|
|
|
|
{
|
|
|
|
/* Sanity check */
|
|
|
|
|
|
|
|
DEBUGASSERT(addr == LSM9DS1MAG_ADDR0 || addr == LSM9DS1MAG_ADDR1);
|
|
|
|
|
|
|
|
return lsm9ds1_register(devpath, i2c, addr, &g_lsm9ds1mag_ops,
|
|
|
|
LSM9DS1_OUT_X_L_M);
|
|
|
|
}
|
2017-08-24 18:48:20 +02:00
|
|
|
#endif /* CONFIG_I2C && CONFIG_SENSORS_LSM9DS1 */
|